:root {
  --bg: #101418;
  --surface: #182027;
  --surface-2: #212b34;
  --line: #2a3540;
  --text: #e8edf1;
  --muted: #8b9aa7;
  --accent: #5ec2a4;
  --pos: #5ec2a4;
  --neg: #f07a72;
  --warn: #e9b45a;
  --bar: #2d3a45;
  --radius: 16px;
  --tabbar-h: 64px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --surface-2: #eef2f5;
    --line: #e1e7ec;
    --text: #15202a;
    --muted: #66778a;
    --accent: #1f8f72;
    --pos: #1f8f72;
    --neg: #d4473d;
    --warn: #b77c12;
    --bar: #e3e9ee;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}
button, input, select { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.num { font-variant-numeric: tabular-nums; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 8px) 8px 8px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}
.topbar h1 { flex: 1; margin: 0; font-size: 20px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#backBtn:not([hidden]) + h1 { margin-left: -4px; }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; background: transparent; border-radius: 50%; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { padding: 4px 12px 24px; max-width: 640px; margin: 0 auto; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 12px; font-weight: 550;
}
.tabbar a svg { width: 22px; height: 22px; }
.tabbar a.active { color: var(--accent); }
.tabbar a.active svg { stroke-width: 2.4; }

/* Cards and lists */
.card { background: var(--surface); border-radius: var(--radius); margin: 0 0 12px; overflow: hidden; }
.section-label {
  margin: 18px 6px 8px; font-size: 13px; font-weight: 600; letter-spacing: .04em;
  text-transform: uppercase; color: var(--muted); display: flex; justify-content: space-between;
}
.row {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px;
  border-top: 1px solid var(--line); text-decoration: none; color: inherit;
}
.row:first-child { border-top: 0; }
a.row:active, button.row:active { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .name { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .amt { font-weight: 600; text-align: right; white-space: nowrap; }
.row .amt small { display: block; font-weight: 400; font-size: 12px; color: var(--muted); }
.chev { color: var(--muted); width: 18px; height: 18px; flex: none; }

/* Month switcher */
.month-switch { display: flex; align-items: center; justify-content: space-between; margin: 4px 0 12px; }
.month-switch .label { font-size: 17px; font-weight: 600; }
.month-switch button {
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--surface); display: grid; place-items: center; cursor: pointer;
}
.month-switch button:disabled { opacity: .35; }

/* Summary */
.summary { padding: 18px 16px 16px; }
.summary .big-label { font-size: 13px; color: var(--muted); }
.summary .big { font-size: 34px; font-weight: 700; letter-spacing: -.01em; margin: 2px 0 14px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px; }
.stat .l { font-size: 12px; color: var(--muted); }
.stat .v { font-weight: 650; font-size: 15px; }

/* Budget group */
.group-head {
  width: 100%; display: flex; align-items: center; gap: 8px; padding: 14px 16px;
  border: 0; background: transparent; text-align: left; cursor: pointer;
}
.group-head .gname { flex: 1; font-weight: 650; }
.group-head .gbal { font-weight: 650; }
.group-head svg { width: 18px; height: 18px; color: var(--muted); transition: transform .15s; }
.group.collapsed .group-head svg { transform: rotate(-90deg); }
.group.collapsed .cats { display: none; }
.cats .row { padding-top: 10px; padding-bottom: 10px; }
.cat .bar { height: 5px; background: var(--bar); border-radius: 3px; margin-top: 6px; overflow: hidden; }
.cat .bar i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }
.cat .bar i.over { background: var(--neg); }
.cat .bar i.near { background: var(--warn); }
.pill {
  font-weight: 650; padding: 3px 10px; border-radius: 999px; font-size: 14px; white-space: nowrap;
  background: var(--surface-2);
}
.pill.pos { background: color-mix(in srgb, var(--pos) 16%, transparent); }
.pill.neg { background: color-mix(in srgb, var(--neg) 18%, transparent); }
.pill.zero { color: var(--muted); }

/* Transactions */
.day-label { margin: 16px 6px 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.tx .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--warn); display: inline-block; margin-right: 6px; vertical-align: middle; }
.search {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); margin: 4px 0 4px; outline: none;
}
.search:focus { border-color: var(--accent); }
.more {
  display: block; width: 100%; margin: 14px 0; padding: 13px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 600; color: var(--accent);
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 6px 0 2px; scrollbar-width: none; }
.chips button {
  flex: none; padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--surface);
  font-size: 13px; cursor: pointer;
}
.chips button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.split-line { font-size: 13px; color: var(--muted); padding: 0 16px 10px 16px; margin-top: -6px; }

/* Forms */
.form { padding: 16px; }
.form label { display: block; font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.form label:first-child { margin-top: 0; }
.form input, .form select {
  width: 100%; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2); outline: none;
}
.form input:focus { border-color: var(--accent); }
.form .hint { font-size: 12px; color: var(--muted); margin-top: 6px; }
.btn {
  display: block; width: 100%; margin-top: 18px; padding: 14px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; font-weight: 650; cursor: pointer;
}
.btn.ghost { background: transparent; color: var(--neg); border: 1px solid var(--line); }
.intro { padding: 8px 6px 4px; }
.intro h2 { margin: 6px 0 4px; font-size: 22px; }
.intro p { margin: 0 0 10px; color: var(--muted); }

/* States */
.empty, .error { text-align: center; padding: 36px 20px; color: var(--muted); }
.error { color: var(--neg); }
.error .btn { max-width: 220px; margin: 16px auto 0; }
.skel { height: 56px; margin: 0 0 10px; border-radius: var(--radius); background: var(--surface); position: relative; overflow: hidden; }
.skel::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--text) 6%, transparent), transparent);
  animation: shimmer 1.2s infinite;
}
@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 14px);
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px;
  transition: .2s; z-index: 20; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
