:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --line: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --good: #006300;
  --critical: #d03b3b;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --series-other: #b5b3ab;
  --radius: 14px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --line: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --good: #0ca30c;
    --critical: #e05656;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --series-other: #5c5b57;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
a { color: var(--accent); }
.num { font-variant-numeric: tabular-nums; }
[hidden] { display: none !important; }

/* ---------- shell */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 20px; flex: 1; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%;
  display: grid; place-items: center;
  background: transparent; border: 0; color: var(--ink-2);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }

main { padding: 16px 16px calc(var(--tabbar-h) + var(--safe-b) + 24px); max-width: 720px; margin: 0 auto; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 500;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--accent); }

.banner {
  margin: 12px 16px 0; padding: 10px 12px; border-radius: 10px;
  background: var(--surface-2); color: var(--ink-2); font-size: 14px;
}

/* ---------- cards & common */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; margin-bottom: 14px;
}
.card h2 { font-size: 15px; font-weight: 600; margin-bottom: 12px; display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.card h2 small { font-weight: 400; color: var(--muted); font-size: 13px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.over { color: var(--critical); }
.good { color: var(--good); }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; min-width: 0; }
.stat .label { font-size: 12px; color: var(--muted); }
.stat .value { font-size: 17px; font-weight: 650; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.period-nav {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin-bottom: 14px;
}
.period-nav .label { font-weight: 600; text-align: center; flex: 1; }
.period-nav .label small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; }
.period-nav button {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); font-size: 22px; line-height: 1;
}
.period-nav button:disabled { opacity: .3; }

.meter { position: relative; height: 8px; border-radius: 4px; background: var(--surface-2); overflow: visible; }
.meter > span { display: block; height: 100%; border-radius: 4px; background: var(--accent); max-width: 100%; }
.meter.is-over > span { background: var(--critical); }
.meter > i { position: absolute; top: -4px; bottom: -4px; width: 2px; background: var(--ink-2); border-radius: 1px; }

/* ---------- home */
.hero { text-align: center; padding: 8px 0 4px; }
.hero .label { color: var(--muted); font-size: 14px; }
.hero .big { font-size: 46px; font-weight: 750; letter-spacing: -0.02em; margin: 2px 0 12px; }
.hero .sub { color: var(--ink-2); font-size: 14px; margin-top: 10px; }
.hero .meter { margin: 0 8px; }

.log-btn {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; min-height: 96px; margin: 22px 0;
  border: 0; border-radius: 24px;
  background: var(--accent); color: var(--accent-ink);
  font-size: 26px; font-weight: 700; letter-spacing: .01em;
  box-shadow: 0 8px 24px color-mix(in srgb, var(--accent) 35%, transparent);
}
.log-btn:active { transform: scale(.98); }
.log-btn svg { width: 32px; height: 32px; }

.pending-note { font-size: 13px; color: var(--ink-2); background: var(--surface-2); border-radius: 10px; padding: 8px 12px; margin-bottom: 14px; }

/* ---------- transactions */
.tx-list { display: flex; flex-direction: column; }
.tx {
  display: grid; grid-template-columns: 36px 1fr auto; gap: 10px; align-items: center;
  width: 100%; padding: 10px 0; text-align: left;
  background: none; border: 0; border-bottom: 1px solid var(--line);
}
.tx:last-child { border-bottom: 0; }
.tx-emoji { font-size: 24px; text-align: center; }
.tx-main { min-width: 0; }
.tx-main b { display: block; font-weight: 600; font-size: 15px; }
.tx-main small { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-amt { text-align: right; font-weight: 600; white-space: nowrap; }
.tx-amt small { display: block; color: var(--muted); font-weight: 400; font-size: 12px; }
.tx .flag { color: var(--critical); }

.day-head {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 13px; color: var(--muted); font-weight: 600;
  margin: 18px 0 2px; padding-bottom: 4px;
}
.day-head:first-child { margin-top: 0; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 8px 14px; font-size: 14px; white-space: nowrap;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.filters select, .filters input { flex: 1 1 140px; min-width: 0; }

/* ---------- category rows */
.group-head { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin: 6px 0 8px; }
.cat-row { display: block; padding: 10px 0; border-bottom: 1px solid var(--line); text-decoration: none; color: inherit; }
.cat-row:last-child { border-bottom: 0; }
.cat-row .top { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.cat-row .name { font-weight: 600; }
.cat-row .bottom { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); margin-top: 5px; }

/* ---------- charts */
.chart-box { position: relative; height: 230px; }
.chart-box.tall { height: 260px; }
.donut-wrap { display: grid; grid-template-columns: 170px 1fr; gap: 16px; align-items: center; }
.donut-box { position: relative; height: 170px; }
.donut-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; }
.donut-center b { font-size: 16px; }
.donut-center small { color: var(--muted); font-size: 12px; }
.legend { list-style: none; margin: 0; padding: 0; font-size: 14px; }
.legend li { display: grid; grid-template-columns: 12px 1fr auto; gap: 8px; align-items: center; padding: 4px 0; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; }
.legend .pct { color: var(--muted); font-size: 12px; margin-left: 4px; }
@media (max-width: 420px) {
  .donut-wrap { grid-template-columns: 1fr; }
  .donut-box { height: 200px; }
}

.split { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 10px; }
.split .stat .value { font-size: 18px; }

table.months { width: 100%; border-collapse: collapse; font-size: 14px; }
table.months th { text-align: right; font-weight: 500; color: var(--muted); font-size: 12px; padding: 6px 4px; border-bottom: 1px solid var(--line); }
table.months th:first-child, table.months td:first-child { text-align: left; }
table.months td { text-align: right; padding: 10px 4px; border-bottom: 1px solid var(--line); }
table.months tr[data-period] { cursor: pointer; }
table.months tr[data-period]:active { background: var(--surface-2); }

/* ---------- forms */
input[type=text], input[type=email], input[type=password], input[type=date], input[type=number], select {
  width: 100%; min-height: 48px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--ink);
  font-size: 16px; /* ≥16px stops iOS zooming on focus */
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }

.btn {
  min-height: 48px; padding: 10px 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--critical); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.row-actions { display: flex; gap: 10px; margin-top: 8px; }
.row-actions .btn { flex: 1; }

/* ---------- log sheet */
.sheet {
  position: fixed; inset: 0; z-index: 20;
  background: var(--page);
  display: flex; flex-direction: column;
  padding-top: var(--safe-t);
  animation: sheet-in .18s ease-out;
}
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.sheet-head h2 { flex: 1; font-size: 18px; text-align: center; }
.sheet-head .icon-btn { flex: none; }
.sheet-body { flex: 1; overflow-y: auto; padding: 20px 16px calc(24px + var(--safe-b)); max-width: 560px; width: 100%; margin: 0 auto; }
.crumbs { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; }
.crumbs button { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 14px; }

.pick-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.pick {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px;
  min-height: 120px; padding: 14px; border-radius: 20px;
  background: var(--surface); border: 2px solid var(--line);
  font-size: 18px; font-weight: 600; text-align: center;
}
.pick:active { transform: scale(.98); }
.pick .emoji { font-size: 56px; line-height: 1; }
.pick.card-pick { min-height: 88px; }
.pick.last { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.pick .hint { font-size: 12px; font-weight: 400; color: var(--muted); }

.amount-input { font-size: 34px !important; font-weight: 700; text-align: center; min-height: 72px !important; }
.seg { display: flex; gap: 8px; }
.seg button { flex: 1; min-height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); font-size: 16px; }
.seg button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- settings editor */
.edit-row { display: grid; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.edit-row.cat { grid-template-columns: 1fr 1fr 110px 40px; }
.edit-row.card-row { grid-template-columns: 1fr 40px; }
.edit-row.person { grid-template-columns: 64px 1fr 40px; }
.edit-row input { min-height: 42px; padding: 8px 10px; }
.edit-row .icon-btn { width: 40px; height: 40px; }
@media (max-width: 480px) {
  .edit-row.cat { grid-template-columns: 1fr 96px 40px; }
  .edit-row.cat .grp { grid-column: 1 / 3; grid-row: 2; }
}
.edit-head { display: grid; gap: 8px; font-size: 12px; color: var(--muted); }
.edit-head.cat { grid-template-columns: 1fr 1fr 110px 40px; }
@media (max-width: 480px) { .edit-head.cat { display: none; } }
.archived { margin-top: 12px; font-size: 14px; }
.archived button { border: 0; background: none; color: var(--accent); padding: 4px; }
.dirty-note { font-size: 13px; color: var(--ink-2); margin-top: 8px; }

/* ---------- login & toast */
.login { max-width: 380px; margin: 12vh auto 0; padding: 0 16px; }
.login h1 { font-size: 28px; margin-bottom: 6px; }
.login p { color: var(--muted); margin: 0 0 24px; }
.error { color: var(--critical); font-size: 14px; margin-top: 10px; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); z-index: 30;
  transform: translateX(-50%);
  background: var(--ink); color: var(--page);
  padding: 12px 18px; border-radius: 12px; font-size: 15px; max-width: calc(100% - 32px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
}
