:root {
  --bg: #F4EFE6;
  --surface: #FFFDF9;
  --surface-2: #EFE8DC;
  --ink: #111827;
  --muted: #6b6559;
  --line: #E3DACB;
  --accent: #D9480F;
  --accent-ink: #fff;
  --pos: #15803d;
  --neg: #b91c1c;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(17, 24, 39, .06), 0 4px 16px rgba(17, 24, 39, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161c;
    --surface: #1d2028;
    --surface-2: #262a34;
    --ink: #F4EFE6;
    --muted: #a39d92;
    --line: #2f3440;
    --accent: #f06a33;
    --pos: #4ade80;
    --neg: #f87171;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; padding: 28px 24px; box-shadow: var(--shadow); text-align: center;
}
.login-logo { width: 84px; height: 84px; border-radius: 20px; }
.login-card h1 { margin: 10px 0 18px; font-size: 22px; }
.login-card .field { text-align: left; }
.error { color: var(--neg); font-size: 14px; margin: 8px 0 0; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.brand img { width: 32px; height: 32px; border-radius: 8px; }
.tabs {
  margin-left: auto; display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px;
}
.tabs button {
  border: 0; background: transparent; padding: 7px 16px; border-radius: 999px; cursor: pointer;
  font-weight: 600; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
@media (max-width: 480px) {
  .brand span { display: none; }
}

main { max-width: 1100px; margin: 0 auto; padding: 14px 16px 40px; }
.grid { display: grid; gap: 14px; grid-template-columns: 1fr; }
.col { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
@media (min-width: 860px) {
  .grid { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; }
}

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow);
}
.card-title { font-weight: 700; margin-bottom: 10px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.hint { margin: -4px 0 10px; }
.big { font-size: 30px; font-weight: 750; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }

/* ---------- buttons & inputs ---------- */
.btn {
  border: 1px solid var(--line); background: var(--surface); padding: 9px 14px; border-radius: 10px;
  cursor: pointer; font-weight: 600; touch-action: manipulation;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.btn.primary:hover { opacity: .9; }
.btn.accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.danger { color: var(--neg); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.icon-btn {
  border: 0; background: transparent; width: 38px; height: 38px; border-radius: 10px; cursor: pointer;
  display: inline-grid; place-items: center; font-size: 24px; line-height: 1; color: var(--ink);
}
.icon-btn:hover { background: var(--surface-2); }
input, select, textarea {
  width: 100%; border: 1px solid var(--line); background: var(--bg); border-radius: 10px;
  padding: 9px 11px; min-height: 42px;
}
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
select { width: auto; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 600; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; min-width: 0; }
.row > input[type="date"] { flex: 0 0 150px; }

/* ---------- add expense ---------- */
.add-card { display: flex; flex-direction: column; gap: 12px; }
.amount-row { display: flex; align-items: baseline; gap: 6px; border-bottom: 2px solid var(--line); }
.amount-row:focus-within { border-color: var(--accent); }
.amount-row input {
  border: 0; background: transparent; font-size: 34px; font-weight: 750; padding: 2px 0; outline: none;
  font-variant-numeric: tabular-nums; min-height: 0;
}
.amount-row .cur { font-size: 26px; color: var(--muted); font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 6px 11px;
  cursor: pointer; font-size: 14px; flex: none; white-space: nowrap; line-height: 1.3; display: inline-flex; gap: 5px; align-items: center;
  touch-action: manipulation;
}
.chip[aria-checked="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.chip.member[aria-checked="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- month nav ---------- */
.month-nav { display: flex; align-items: center; justify-content: center; gap: 8px; margin-bottom: 12px; }
.month-nav h2 { margin: 0; font-size: 18px; min-width: 160px; text-align: center; text-transform: capitalize; }

/* ---------- breakdown ---------- */
.summary-head { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 12px; gap: 10px; }
.breakdown { display: flex; flex-direction: column; gap: 9px; }
.bd-row { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; cursor: pointer; }
.bd-row .name { display: flex; gap: 6px; align-items: center; min-width: 0; }
.bd-row .name span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bd-bar { grid-column: 1 / -1; height: 7px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bd-bar > div { height: 100%; border-radius: 99px; }
.empty { color: var(--muted); text-align: center; padding: 18px 8px; font-size: 14px; }

/* ---------- trend ---------- */
.trend { display: flex; align-items: flex-end; gap: 4px; height: 140px; }
.trend .bar-wrap { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; cursor: pointer; min-width: 0; }
.trend .bar-area { flex: 1; width: 100%; display: flex; align-items: flex-end; }
.trend .bar { width: 100%; background: var(--line); border-radius: 5px 5px 2px 2px; min-height: 2px; }
.trend .bar-wrap.current .bar { background: var(--accent); }
.trend .lbl { font-size: 11px; color: var(--muted); }
.trend .bar-wrap:hover .bar { opacity: .8; }

/* ---------- lists ---------- */
.day-head {
  font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
  padding: 10px 2px 4px; display: flex; justify-content: space-between;
}
.item {
  display: flex; align-items: center; gap: 10px; padding: 9px 4px; border-top: 1px solid var(--line);
  cursor: pointer; border-radius: 8px;
}
.day-head + .item { border-top: 0; }
.item:hover { background: var(--surface-2); }
.item .ico {
  width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; flex: none; font-size: 16px;
}
.item .main { flex: 1; min-width: 0; }
.item .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .amt { font-weight: 700; }

/* ---------- people ---------- */
.hero { margin-bottom: 14px; }
.person { border-top: 1px solid var(--line); padding: 12px 2px; }
.person:first-child { border-top: 0; padding-top: 2px; }
.person-head { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.avatar {
  width: 38px; height: 38px; border-radius: 50%; background: var(--surface-2); display: grid; place-items: center;
  font-weight: 700; flex: none;
}
.person-head .main { flex: 1; min-width: 0; }
.person-head .bal { font-weight: 750; font-size: 17px; text-align: right; }
.person-actions { display: flex; gap: 6px; margin-top: 8px; padding-left: 48px; flex-wrap: wrap; }
.archived { opacity: .55; }

/* ---------- modal ---------- */
dialog {
  border: 0; padding: 0; border-radius: 18px; width: min(520px, calc(100vw - 24px));
  background: var(--surface); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.3);
  max-height: calc(100dvh - 40px);
}
dialog::backdrop { background: rgba(17, 24, 39, .45); }
.modal-body { padding: 18px; }
.modal-body h3 { margin: 0 0 14px; font-size: 18px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }
.modal-actions .spacer { flex: 1; }
@media (max-width: 560px) {
  dialog { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 18px 18px 0 0; padding-bottom: env(safe-area-inset-bottom); }
}
.seg { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; margin-bottom: 12px; }
.seg label { flex: 1; text-align: center; }
.seg input { position: absolute; opacity: 0; pointer-events: none; width: 0; }
.seg span { display: block; padding: 8px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--muted); }
.seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.history { max-height: 45dvh; overflow: auto; margin: 0 -4px; }
.cat-edit { display: grid; grid-template-columns: 52px 44px 1fr auto; gap: 6px; align-items: center; margin-bottom: 6px; }
.cat-edit input[type="color"] { padding: 2px; height: 42px; }
.cat-edit input.emoji { text-align: center; padding: 6px; }
.sep { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-weight: 600;
  z-index: 100; display: flex; gap: 12px; align-items: center; max-width: calc(100vw - 32px);
}
.toast button { background: transparent; border: 0; color: var(--accent); font-weight: 700; cursor: pointer; padding: 0; }
