:root {
  --bg: #f6f7f9;
  --card: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --brand: #0f766e;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --track: #eef0f3;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f14;
    --card: #151b23;
    --ink: #e5e7eb;
    --muted: #9ca3af;
    --line: #253041;
    --brand: #2dd4bf;
    --good: #4ade80;
    --warn: #fbbf24;
    --bad: #f87171;
    --track: #1f2937;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 18px; margin: 0; }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: transparent; color: var(--ink); font-size: 26px; cursor: pointer;
}
main { max-width: 640px; margin: 0 auto; padding: 12px 16px 24px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 12px;
}
.hero { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 16px; }
.hero .big { grid-column: 1 / -1; }
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }
.big .value { font-size: 32px; }
.neg { color: var(--bad); }
.pos { color: var(--good); }
.group-title {
  font-size: 13px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 18px 4px 8px;
}
.cat { padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.cat:first-child { border-top: 0; }
.cat .row { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.cat .name { font-weight: 550; }
.cat .nums { font-variant-numeric: tabular-nums; font-size: 14px; color: var(--muted); white-space: nowrap; }
.bar { height: 8px; background: var(--track); border-radius: 99px; overflow: hidden; margin-top: 6px; }
.bar > span { display: block; height: 100%; border-radius: 99px; background: var(--good); }
.bar > span.warn { background: var(--warn); }
.bar > span.bad { background: var(--bad); }
.left { font-size: 13px; margin-top: 4px; }
.tx-day { font-size: 13px; color: var(--muted); margin: 16px 4px 6px; }
.tx { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); cursor: pointer; }
.tx:first-child { border-top: 0; }
.tx .main { flex: 1; min-width: 0; }
.tx .merchant { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 520; }
.tx .meta { font-size: 13px; color: var(--muted); }
.tx .amt { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.chip {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 99px;
  background: var(--track); color: var(--ink);
}
.chip.none { background: color-mix(in srgb, var(--warn) 20%, transparent); }
.filters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.filters button {
  flex: none; border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 99px; padding: 6px 12px; font-size: 14px;
}
.filters button.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.budget-row { display: flex; align-items: center; gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); }
.budget-row:first-child { border-top: 0; }
.budget-row span { flex: 1; }
.budget-row input { width: 120px; text-align: right; }
input, select {
  font: inherit; color: var(--ink); background: var(--bg);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}
label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 12px; }
label > input, label > select, label > .amount-row { margin-top: 4px; }
.amount-row { display: flex; gap: 8px; }
.amount-row input { font-size: 24px; font-weight: 600; }
.amount-row select { width: 96px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
button.primary, button.ghost {
  font: inherit; border-radius: 10px; padding: 10px 16px; cursor: pointer; border: 1px solid transparent;
}
button.primary { background: var(--brand); color: #fff; font-weight: 600; }
button.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
button.danger { color: var(--bad); }
.actions { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.spacer { flex: 1; }
.fab {
  position: fixed; right: 18px; bottom: calc(84px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--brand); color: #fff; font-size: 30px; line-height: 1;
  box-shadow: 0 6px 18px rgba(0,0,0,.2); cursor: pointer; z-index: 6;
}
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs a {
  flex: 1; text-align: center; padding: 14px 0; color: var(--muted);
  text-decoration: none; font-size: 14px; font-weight: 550;
}
.tabs a.on { color: var(--brand); }
dialog {
  width: min(560px, 100vw); max-height: 92vh; margin: auto auto 0; border: 0; padding: 0;
  border-radius: 18px 18px 0 0; background: var(--card); color: var(--ink);
}
@media (min-width: 600px) { dialog { margin: auto; border-radius: 18px; } }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { padding: 18px 16px calc(18px + env(safe-area-inset-bottom)); }
dialog h2 { margin: 0 0 12px; font-size: 18px; }
small { display: block; margin-top: 4px; color: var(--brand); min-height: 1em; }
.empty { text-align: center; color: var(--muted); padding: 32px 0; }
.error { color: var(--bad); }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(150px + env(safe-area-inset-bottom)); background: var(--ink); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 20;
}

.receipts { display: flex; gap: 8px; flex-wrap: wrap; }
.receipts img { width: 96px; height: 128px; object-fit: cover; border-radius: 10px; border: 1px solid var(--line); }
.icon-btn:disabled { opacity: .3; }

/* ---- Wider screens ---- */
.save-bar .primary { width: 100%; }
.brand { display: none; }

@media (min-width: 720px) {
  main { max-width: 1180px; padding: 20px 28px 40px; }
  .groups { columns: 3 320px; column-gap: 20px; }
  .group { break-inside: avoid; display: flow-root; }
  .group .card { margin-bottom: 4px; }
  .group-title { margin-top: 22px; }
  .hero { grid-template-columns: repeat(4, 1fr); padding: 20px 24px; }
  .hero .big { grid-column: 1 / -1; }
  .bills { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
  .bills > .card { margin-bottom: 0; }
  .save-bar {
    position: sticky; bottom: calc(64px + env(safe-area-inset-bottom)); z-index: 4;
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 10px 16px; margin: 16px 0;
  }
  .save-bar .tx-day { margin: 0; font-size: 15px; color: var(--ink); font-weight: 600; }
  .save-bar .primary { width: auto; }
  #view > .card, #view > .tx-day, #view > .filters { max-width: none; }
}

@media (min-width: 1024px) {
  body:has(.tabs) { padding-bottom: 0; padding-left: 232px; }
  .top { padding: 14px 28px; justify-content: flex-start; gap: 8px; }
  .top h1 { font-size: 22px; min-width: 200px; text-align: center; order: 0; }
  .top #prev-month, .top #prev { order: -1; }
  .tabs {
    top: 0; bottom: 0; right: auto; width: 232px; flex-direction: column; gap: 4px;
    padding: 20px 14px; border-top: 0; border-right: 1px solid var(--line);
  }
  .brand {
    display: flex; align-items: center; gap: 10px; padding: 0 10px 22px;
    font-weight: 700; font-size: 17px; color: var(--ink);
  }
  .brand img { width: 30px; height: 30px; border-radius: 8px; }
  .tabs a {
    flex: none; text-align: left; padding: 11px 14px; border-radius: 10px; font-size: 15px;
  }
  .tabs a:hover { background: var(--track); color: var(--ink); }
  .tabs a.on { background: color-mix(in srgb, var(--brand) 14%, transparent); color: var(--brand); }
  .tabs .tab-spacer { flex: 1; }
  .fab {
    top: 12px; right: 28px; bottom: auto; width: auto; height: 42px; border-radius: 10px;
    padding: 0 18px; font-size: 15px; font-weight: 600; box-shadow: none; position: fixed;
  }
  .fab::after { content: ' Add spending'; }
  .save-bar { bottom: 16px; }
  .tx:hover, .cat:hover { background: color-mix(in srgb, var(--track) 60%, transparent); }
  .tx, .cat { margin: 0 -16px; padding-left: 16px; padding-right: 16px; }
  .toast { bottom: 32px; }
  #view:has(.tx) { max-width: 880px; margin-left: 0; }
  dialog { margin: auto; border-radius: 18px; }
}

/* ---- Sign-in ---- */
.login {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center;
  padding: 16px; background: var(--bg);
}
.login[hidden] { display: none; }
.login form { width: min(380px, 100%); padding: 28px 24px; text-align: left; }
.login img { display: block; margin: 0 auto 8px; border-radius: 12px; }
.login h1 { text-align: center; font-size: 22px; margin: 0 0 20px; }
.login .primary { width: 100%; margin-top: 4px; }
.login .error { min-height: 1.2em; margin: 0 0 8px; font-size: 14px; }
body.signed-out .fab { display: none; }
.side-only { display: none; }
.tab-spacer { display: none; }
@media (min-width: 1024px) {
  .tabs a.side-only { display: block; }
  .tabs .tab-spacer { display: block; }
}
