:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1b2430;
  --muted: #5d6b7c;
  --line: #dde3ea;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --red: #c62828;
  --red-bg: #fde8e8;
  --amber: #a15c00;
  --amber-bg: #fff4de;
  --green: #1b7a3d;
  --blue: #1a5fb4;
  --row-dep: #dff3e3;
  --row-groc: #dcecfb;
  --row-gas: #ebe2f7;
  --row-rent: #ecdfd0;
  --row-late: #ffd9a8;
  --orange: #b45309;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161c;
    --card: #1a222b;
    --ink: #e8edf2;
    --muted: #9aa8b8;
    --line: #2b3643;
    --accent: #2ab3a6;
    --accent-ink: #06201d;
    --red: #ff7b7b;
    --red-bg: #3a1c1f;
    --amber: #f0b256;
    --amber-bg: #34290f;
    --green: #5fd18a;
    --blue: #6cb2ff;
    --row-dep: #1d3a27;
    --row-groc: #1b3248;
    --row-gas: #2f2644;
    --row-rent: #3d2f22;
    --row-late: #4d3410;
    --orange: #ffb25b;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); line-height: 1.4; }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* gate */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.gate-card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; width: min(360px, 100%); display: grid; gap: 12px; }
.msg-err { color: var(--red); min-height: 1.2em; margin: 0; font-size: .9rem; }

/* layout */
.top { display: flex; align-items: center; gap: 12px; padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--line); }
.sticky-stack { position: sticky; top: 0; z-index: 5; }
.top h1 { font-size: 1.1rem; margin: 0; white-space: nowrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; min-width: 0; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tab { border: 0; background: transparent; color: var(--muted); padding: 8px 12px; border-radius: 8px; font: inherit; cursor: pointer; white-space: nowrap; }
.tab[aria-current="page"] { background: var(--accent); color: var(--accent-ink); }
main { max-width: 980px; margin: 0 auto; padding: 16px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; }
main > *, .grid > * { min-width: 0; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.grid { display: grid; gap: 12px; }
.cards { grid-template-columns: repeat(auto-fit, minmax(min(190px, 100%), 1fr)); }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .sub { color: var(--muted); font-size: .85rem; }
.stat.bad .value { color: var(--red); }
.stat.good .value { color: var(--green); }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.between { justify-content: space-between; }

/* forms */
label { display: grid; gap: 4px; font-size: .85rem; color: var(--muted); }
input, select { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; min-width: 0; }
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; align-items: end; }
.check { display: flex; align-items: center; gap: 8px; color: var(--ink); font-size: .95rem; }
.check input { width: auto; }
.btn { font: inherit; cursor: pointer; border-radius: 8px; padding: 8px 14px; border: 1px solid var(--line); background: var(--card); color: var(--ink); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--red); }
.btn.small { padding: 4px 8px; font-size: .85rem; }

/* tables */
.scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 8px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; white-space: nowrap; }
td.num, th.num { text-align: right; }
td input.amt { width: 7.5em; text-align: right; padding: 4px 6px; }
td input.gross { width: 6.5em; padding: 2px 6px; font-size: .8rem; margin-left: 4px; }
.sub-name { color: var(--muted); font-size: .8rem; }
tr.lowest td.bal, tr.lowest td.bal strong { color: var(--red); font-weight: 800; }
tr.monthlow td.bal { color: var(--amber); font-weight: 700; }
tr.neg td.bal { color: var(--red); }
tr.derived td:nth-child(2) { color: var(--muted); }
tr.past td:first-child { color: var(--red); }
.tag { display: inline-block; font-size: .7rem; padding: 1px 6px; border-radius: 999px; background: var(--amber-bg); color: var(--amber); margin-left: 6px; vertical-align: middle; }
.sync { color: var(--muted); font-size: .8rem; margin-left: auto; margin-right: 8px; }
.practice { background: #fff3c4; color: #5c4400; border-bottom: 2px solid #e0b400; padding: 8px 16px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.practice[hidden] { display: none; }
.sync.bad { color: var(--red); font-weight: 600; }
.cloud-card { min-width: min(92vw, 420px); }
.tag.debit { background: var(--blue-bg, var(--amber-bg)); color: var(--blue, var(--amber)); }
.tag.red { background: var(--red-bg); color: var(--red); }
.dep { color: var(--green); }
.big { font-size: 2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.pill-ok { color: var(--green); font-weight: 700; }
.pill-bad { color: var(--red); font-weight: 700; }
.notice { background: var(--amber-bg); color: var(--amber); border-radius: 8px; padding: 10px 12px; font-size: .9rem; }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--ink); width: min(560px, calc(100vw - 24px)); padding: 16px; }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.toast { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; }

@media (max-width: 640px) {
  .top h1 { display: none; }
  main { padding: 10px; }
  th, td { padding: 6px 4px; }
  td input.amt { width: 6.2em; }
}
.flyout > td { background: var(--bg); padding: 8px 12px 12px; }
.weeks th, .weeks td { font-size: .9rem; }
#log-jump, #cleared-log { scroll-margin-top: 110px; }
tr.is-dep:not(.neg) td, tr.is-dep.lowest td { background: var(--row-dep); }
tr.is-groc:not(.neg) td, tr.is-groc.lowest td { background: var(--row-groc); }
tr.is-gas:not(.neg) td, tr.is-gas.lowest td { background: var(--row-gas); }
tr.is-rent:not(.neg) td, tr.is-rent.lowest td { background: var(--row-rent); }
tr.is-dep td.dep { color: var(--ink); }
tr.neg:not(.lowest) td { background: var(--red-bg); }
.negbar { background: var(--red-bg); color: var(--red); border-bottom: 2px solid var(--red); padding: 8px 16px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-weight: 600; }
.negbar[hidden] { display: none; }
#neg-first { scroll-margin-top: 170px; }
tr.pastdue:not(.neg) td { background: var(--row-late); }
.tag.orange { background: var(--row-late); color: var(--orange); font-weight: 600; }
.duebar { background: var(--row-late); color: var(--orange); border-bottom: 2px solid var(--orange); padding: 8px 16px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-weight: 600; }
.duebar[hidden] { display: none; }
#due-first { scroll-margin-top: 190px; }

/* Calendar */
.cal { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); min-width: 760px; border-top: 1px solid var(--line); border-left: 1px solid var(--line); }
.cal-head { padding: 6px 8px; font-weight: 600; font-size: .85rem; color: var(--muted); border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.cal-day { min-height: 104px; padding: 4px 6px 6px; border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 3px; }
.cal-day.outside { background: var(--bg-soft, rgba(128,128,128,.08)); }
.cal-day.today { outline: 2px solid var(--blue); outline-offset: -2px; }
.cal-num { font-size: .8rem; color: var(--muted); }
.chip { font-size: .75rem; line-height: 1.25; padding: 2px 5px; border-radius: 5px; background: var(--row-late-off, rgba(128,128,128,.12)); display: flex; justify-content: space-between; gap: 4px; }
.chip .cn { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chip b { white-space: nowrap; font-weight: 600; }
.chip i { font-style: normal; font-size: .65rem; color: var(--muted); }
.chip.is-dep { background: var(--row-dep); }
.chip.is-groc { background: var(--row-groc); }
.chip.is-gas { background: var(--row-gas); }
.chip.is-rent { background: var(--row-rent); }
.chip.pastdue { background: var(--row-late); }
.chip.tithe { opacity: .65; }
.cal-bal { margin-top: auto; font-size: .78rem; font-weight: 600; text-align: right; }
.cal-bal.neg { color: var(--red); }
.cal-bal.low { color: var(--orange); }
