/* Family Finances — one stylesheet, tokens first. Chart colors follow the
   dataviz reference palette (validated fixed order, light + dark steps). */

:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --ring: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e8f1fc;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --critical-ink: #b42828;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --band: rgba(137, 135, 129, 0.16);
  --seq-100: #cde2fb; --seq-200: #9ec5f4; --seq-300: #6da7ec; --seq-400: #3987e5;
  --seq-500: #256abf; --seq-600: #184f95; --seq-700: #0d366b;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 4px 16px rgba(11, 11, 11, 0.06);
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
    --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --accent-soft: #16263a;
    --good-ink: #0ca30c; --critical-ink: #e66767;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --band: rgba(195, 194, 183, 0.14);
    --seq-100: #104281; --seq-200: #184f95; --seq-300: #1c5cab; --seq-400: #256abf;
    --seq-500: #3987e5; --seq-600: #6da7ec; --seq-700: #9ec5f4;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321;
  --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --ring: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --accent-soft: #16263a;
  --good-ink: #0ca30c; --critical-ink: #e66767;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --band: rgba(195, 194, 183, 0.14);
  --seq-100: #104281; --seq-200: #184f95; --seq-300: #1c5cab; --seq-400: #256abf;
  --seq-500: #3987e5; --seq-600: #6da7ec; --seq-700: #9ec5f4;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--page); color: var(--ink); font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased; min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h1 { font-size: 22px; } h2 { font-size: 17px; } h3 { font-size: 15px; }
.muted { color: var(--muted); }
.ink2 { color: var(--ink-2); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.num { font-variant-numeric: tabular-nums; }
.hidden { display: none !important; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1 1 auto; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---- shell -------------------------------------------------------------- */
.topbar {
  position: sticky; top: 0; z-index: 20; background: var(--surface);
  border-bottom: 1px solid var(--ring);
}
.topbar-inner { max-width: 1240px; margin: 0 auto; padding: 10px 16px 0; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; flex: none; display: block; }
.brand h1 { font-size: 17px; }
.who { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); font-size: 12px; border: 1px solid var(--ring);
}
.pill.dev { background: #fff2cc; color: #6b4b00; border-color: #e0c060; }
nav.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; margin-top: 8px; }
nav.tabs::-webkit-scrollbar { display: none; }
nav.tabs a {
  padding: 9px 12px; color: var(--ink-2); text-decoration: none; white-space: nowrap;
  border-bottom: 2px solid transparent; font-size: 14px;
}
nav.tabs a:hover { color: var(--ink); }
nav.tabs a.on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
main { max-width: 1240px; margin: 0 auto; padding: 18px 16px 64px; }

/* ---- filter row (one row, above what it scopes) ------------------------------ */
.filters {
  display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: end; margin-bottom: 16px;
  padding: 12px 14px; background: var(--surface); border: 1px solid var(--ring); border-radius: var(--radius);
}
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); min-width: 0; }
.filters select { min-width: 220px; max-width: 100%; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); cursor: pointer; }
select, input[type=text], input[type=number], input[type=month], textarea {
  background: var(--surface); border: 1px solid var(--axis); border-radius: 8px; padding: 7px 9px;
  min-height: 36px; max-width: 100%;
}
textarea { width: 100%; min-height: 120px; resize: vertical; }
select:focus, input:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.seg { display: inline-flex; border: 1px solid var(--axis); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 11px; cursor: pointer; font-size: 13px; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--axis); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }

/* ---- buttons --------------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 7px 13px;
  border-radius: 8px; border: 1px solid var(--axis); background: var(--surface); cursor: pointer;
  min-height: 36px; font-size: 14px; color: var(--ink); text-decoration: none;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--critical-ink); }
.btn.small { min-height: 28px; padding: 3px 9px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.link { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: inherit; }

/* ---- cards / grid ---------------------------------------------------------------- */
.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 900px) { .grid.cols-2, .grid.cols-3 { grid-template-columns: minmax(0, 1fr); } }
.card {
  background: var(--surface); border: 1px solid var(--ring); border-radius: var(--radius);
  padding: 16px; min-width: 0; box-shadow: var(--shadow);
}
.card > header { display: flex; align-items: baseline; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.card > header .sub { color: var(--muted); font-size: 13px; }
.card > header .end { margin-left: auto; }
.section-title { margin: 22px 0 10px; display: flex; align-items: baseline; gap: 10px; }

/* ---- figures ----------------------------------------------------------------- */
.hero { display: flex; flex-wrap: wrap; gap: 20px 40px; align-items: flex-end; }
.hero .label { font-size: 13px; color: var(--ink-2); }
.hero .value { font-size: 48px; font-weight: 600; letter-spacing: -0.02em; line-height: 1.05; }
.hero .meta { color: var(--muted); font-size: 13px; margin-top: 4px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-top: 16px; }
@media (max-width: 520px) { .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; } .tile .value { font-size: 20px; } .hero .value { font-size: 40px; } }
.tile { background: var(--surface); border: 1px solid var(--ring); border-radius: var(--radius); padding: 12px 14px; }
.tile .label { font-size: 13px; color: var(--ink-2); }
.tile .value { font-size: 24px; font-weight: 600; margin-top: 2px; letter-spacing: -0.01em; }
.tile .delta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.tile .delta.good { color: var(--good-ink); }
.tile .delta.bad { color: var(--critical-ink); }

/* ---- callouts --------------------------------------------------------------------- */
.callout { border: 1px solid var(--ring); border-left: 4px solid var(--warning); border-radius: 10px;
  padding: 10px 14px; background: var(--surface); }
.callout.info { border-left-color: var(--accent); }
.callout.bad { border-left-color: var(--critical); }
.callout h3 { margin-bottom: 6px; }
.status { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--ink-2); }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.good::before { background: var(--good); }
.status.warn::before { background: var(--warning); }
.status.bad::before { background: var(--critical); }

/* ---- tables ------------------------------------------------------------------------ */
.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.t th, table.t td { padding: 7px 10px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.t th { color: var(--ink-2); font-weight: 600; font-size: 12px; white-space: nowrap; background: var(--surface); position: sticky; top: 0; }
table.t td.r, table.t th.r { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.t tr.group td { background: var(--surface-2); font-weight: 600; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: var(--accent-soft); }
table.t tr.detail td { background: var(--surface-2); }
.neg { color: var(--critical-ink); }
.pos { color: var(--good-ink); }

/* ---- charts ------------------------------------------------------------------------- */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .axis text, .chart .lbl { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .endlbl { fill: var(--ink-2); font-size: 12px; font-weight: 600; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .zero { stroke: var(--ink-2); stroke-width: 1; }
.chart .xhair { stroke: var(--ink-2); stroke-width: 1; }
.chart .ref { stroke: var(--critical); stroke-width: 1.5; }
.chart .reflbl { fill: var(--critical-ink); font-size: 11px; }
.chart .hit { fill: transparent; }
.chart .mark-hover { filter: brightness(1.12); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 0 0 8px; font-size: 12.5px; color: var(--ink-2); }
.legend .key { display: inline-flex; align-items: center; gap: 6px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.legend .ln { width: 16px; height: 2px; border-radius: 2px; display: inline-block; }
.tip {
  position: absolute; pointer-events: none; z-index: 5; min-width: 150px; max-width: 280px;
  background: var(--surface); border: 1px solid var(--ring); border-radius: 10px; box-shadow: var(--shadow);
  padding: 8px 10px; font-size: 12.5px; opacity: 0; transition: opacity .08s;
}
.tip.on { opacity: 1; }
.tip .tt { color: var(--ink-2); font-weight: 600; margin-bottom: 4px; }
.tip .tr { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.tip .tr .k { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.tip .tr .k i { width: 12px; height: 2px; border-radius: 2px; display: inline-block; }
.tip .tr b { font-variant-numeric: tabular-nums; color: var(--ink); }
.heat { border-collapse: separate; border-spacing: 2px; width: 100%; font-size: 13px; }
.heat th { font-size: 12px; color: var(--ink-2); font-weight: 600; padding: 6px; text-align: center; }
.heat th.rowh { text-align: left; white-space: nowrap; }
.heat td { padding: 10px 6px; text-align: center; border-radius: 6px; font-variant-numeric: tabular-nums; cursor: pointer; }
.heat td.sel { outline: 2px solid var(--ink); outline-offset: -2px; }

/* ---- scenario editor ------------------------------------------------------------------ */
.fieldgroup { border-top: 1px solid var(--grid); padding-top: 12px; margin-top: 12px; }
.fieldgroup h3 { margin-bottom: 8px; }
.field { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr) auto; gap: 8px 12px; align-items: center;
  padding: 6px 0; border-bottom: 1px dashed transparent; }
.field .name { font-size: 14px; }
.field .help { font-size: 12px; color: var(--muted); }
.field .val input, .field .val select { width: 100%; }
.field.over .name::after { content: " • changed"; color: var(--accent); font-size: 12px; }
.field textarea { min-height: 80px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; }
@media (max-width: 700px) { .field { grid-template-columns: minmax(0, 1fr); } }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 2px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--ring); font-size: 12px; color: var(--ink-2); }
.chip.agent { background: var(--accent-soft); }

/* ---- lists -------------------------------------------------------------------------------- */
.list { display: flex; flex-direction: column; }
.list .item { padding: 10px 2px; border-bottom: 1px solid var(--grid); display: flex; gap: 12px; align-items: flex-start; }
.list .item:last-child { border-bottom: 0; }
.list .item .main { flex: 1 1 auto; min-width: 0; }
.list .item .title { font-weight: 600; }
.list .item .desc { color: var(--ink-2); font-size: 13px; margin-top: 2px; }
.events { list-style: none; padding: 0; margin: 0; }
.events li { padding: 5px 0; display: flex; gap: 10px; font-size: 13.5px; border-bottom: 1px solid var(--grid); }
.events li .y { color: var(--muted); width: 42px; flex: none; font-variant-numeric: tabular-nums; }
.md p { margin: 0 0 8px; } .md ul { margin: 0 0 8px; padding-left: 18px; }
.md h1, .md h2, .md h3, .md h4 { margin: 10px 0 6px; font-size: 15px; }
.md code { background: var(--surface-2); padding: 1px 4px; border-radius: 4px; font-size: 12.5px; }
.md table { border-collapse: collapse; font-size: 13px; margin-bottom: 8px; }
.md td, .md th { border: 1px solid var(--grid); padding: 3px 6px; }

/* ---- sign-in -------------------------------------------------------------------------------- */
.gate { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.gate .card { width: min(420px, 100%); text-align: center; padding: 28px 22px; }
.gate .brand { justify-content: center; margin-bottom: 14px; }
.gate p { color: var(--ink-2); }
.gate .btn { width: 100%; margin-top: 10px; min-height: 44px; }
.err { color: var(--critical-ink); font-size: 13px; margin-top: 10px; white-space: pre-wrap; }
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: var(--page); padding: 10px 16px; border-radius: 10px; font-size: 14px;
  box-shadow: var(--shadow); max-width: calc(100vw - 32px);
}
.busy { opacity: .55; transition: opacity .15s; }
.disclaimer { font-size: 12px; color: var(--muted); margin-top: 10px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 13.5px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; }
details summary { cursor: pointer; color: var(--ink-2); font-size: 13px; }

/* ---- dialogs ---------------------------------------------------------------------------------- */
dialog.dlg {
  border: 1px solid var(--ring); border-radius: var(--radius); background: var(--surface); color: var(--ink);
  padding: 18px; width: min(440px, calc(100vw - 32px)); box-shadow: var(--shadow);
}
dialog.dlg::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog.dlg h3 { margin-bottom: 10px; }
dialog.dlg input { width: 100%; }
dialog.dlg .row.end { justify-content: flex-end; margin-top: 14px; }
.btn.danger-fill { background: var(--critical); border-color: var(--critical); color: #fff; }
