/* ============================================================================
   Ledger — personal finance dashboard
   Register: product (design serves the task). Colour strategy: restrained.
   Chart palette is the validated dataviz reference instance; do not re-step it
   without re-running scripts/validate_palette.js against both surfaces.
   ========================================================================== */

:root {
  color-scheme: light;

  --surface-1: #fcfcfb;      /* chart + panel surface */
  --plane:     #f9f9f7;      /* page plane */
  --panel-2:   #f2f1ed;      /* second neutral layer: sidebar, toolbars */
  --ink:       #0b0b0b;
  --ink-2:     #52514e;
  --muted:     #6f6d67;      /* darkened from #898781 to clear 4.5:1 on plane */
  --axis-ink:  #898781;      /* axis/tick labels only (large, non-body) */
  --grid:      #e1e0d9;
  --baseline:  #c3c2b7;
  --hairline:  rgba(11, 11, 11, .10);
  --wash:      rgba(11, 11, 11, .04);
  --good:      #006300;
  --bad:       #b3312f;

  --s1: #2a78d6;  --s2: #eb6834;  --s3: #1baf7a;  --s4: #eda100;
  --s5: #e87ba4;  --s6: #008300;  --s7: #4a3aa7;  --s8: #e34948;

  --st-good: #0ca30c;  --st-warn: #fab219;
  --st-serious: #ec835a;  --st-critical: #d03b3b;

  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --r-sm: 6px;  --r-md: 10px;  --r-lg: 14px;

  --z-sticky: 100;  --z-dropdown: 200;  --z-tooltip: 300;

  --ease: cubic-bezier(.22, 1, .36, 1);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #1a1a19;  --plane: #0d0d0d;  --panel-2: #141413;
    --ink: #ffffff;  --ink-2: #c3c2b7;  --muted: #9b9992;  --axis-ink: #898781;
    --grid: #2c2c2a;  --baseline: #383835;
    --hairline: rgba(255, 255, 255, .12);  --wash: rgba(255, 255, 255, .05);
    --good: #0ca30c;  --bad: #e66767;
    --s1: #3987e5;  --s2: #d95926;  --s3: #199e70;  --s4: #c98500;
    --s5: #d55181;  --s6: #008300;  --s7: #9085e9;  --s8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;  --plane: #0d0d0d;  --panel-2: #141413;
  --ink: #ffffff;  --ink-2: #c3c2b7;  --muted: #9b9992;  --axis-ink: #898781;
  --grid: #2c2c2a;  --baseline: #383835;
  --hairline: rgba(255, 255, 255, .12);  --wash: rgba(255, 255, 255, .05);
  --good: #0ca30c;  --bad: #e66767;
  --s1: #3987e5;  --s2: #d95926;  --s3: #199e70;  --s4: #c98500;
  --s5: #d55181;  --s6: #008300;  --s7: #9085e9;  --s8: #e66767;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--plane); color: var(--ink);
  font: 400 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0; font-weight: 600; letter-spacing: -.011em; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
a { color: inherit; }
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--s1); outline-offset: 2px; border-radius: 3px; }

/* ---------------------------------------------------------------- shell */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--panel-2); border-right: 1px solid var(--hairline);
  padding: 20px 14px; position: sticky; top: 0; height: 100vh;
  overflow-y: auto; display: flex; flex-direction: column; gap: 22px;
}
.brand { padding: 0 8px; }
.brand strong { display: block; font-size: .95rem; letter-spacing: -.01em; }
.brand span { display: block; font-size: .78rem; color: var(--muted); margin-top: 2px; }

.nav { display: flex; flex-direction: column; gap: 1px; }
.nav button {
  display: flex; align-items: center; gap: 9px; width: 100%; text-align: left;
  padding: 8px 10px; background: none; border: 0; border-radius: var(--r-sm);
  color: var(--ink-2); font-size: .875rem; cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.nav button:hover { background: var(--wash); color: var(--ink); }
.nav button[aria-current="true"] { background: var(--surface-1); color: var(--ink); font-weight: 550; }
.nav button .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .38; flex: none; }
.nav button[aria-current="true"] .dot { background: var(--s1); opacity: 1; }
.nav-count { margin-left: auto; font-size: .74rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.theme-btn { width: 100%; margin-bottom: 14px; }
.side-foot { margin-top: auto; font-size: .74rem; color: var(--muted); padding: 0 8px; line-height: 1.5; }
.side-foot code { font-size: .95em; }

.main { min-width: 0; display: flex; flex-direction: column; }

/* filter row — one row, above everything it scopes */
.toolbar {
  position: sticky; top: 0; z-index: var(--z-sticky);
  display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; overflow-x: auto;
  padding: 12px 26px; background: color-mix(in srgb, var(--plane) 92%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--hairline);
}
.field {
  flex: none;
  display: inline-flex; align-items: center; gap: 6px; padding: 6px 10px;
  background: var(--surface-1); border: 1px solid var(--hairline);
  border-radius: var(--r-sm); font-size: .82rem; color: var(--ink-2);
}
.field select, .field input {
  border: 0; background: none; font: inherit; color: var(--ink); padding: 0;
  min-width: 0;
}
.field select:focus, .field input:focus { outline: none; }
.field label { color: var(--muted); white-space: nowrap; }
.field input[type="search"] { width: 168px; }
.ghost {
  flex: none;
  padding: 6px 11px; background: none; border: 1px solid var(--hairline);
  border-radius: var(--r-sm); font-size: .82rem; color: var(--ink-2); cursor: pointer;
  transition: background .15s var(--ease), color .15s var(--ease);
}
.ghost:hover { background: var(--wash); color: var(--ink); }
.ghost[aria-pressed="true"] { background: var(--ink); color: var(--plane); border-color: var(--ink); }
.spacer { flex: 1 1 auto; min-width: 4px; }
.scope-note { flex: none; white-space: nowrap; font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.view { padding: 26px; display: none; }
.view[data-active] { display: block; }
.view > header { margin-bottom: 20px; max-width: 68ch; }
.view > header p { margin: 6px 0 0; color: var(--ink-2); font-size: .9rem; }

/* ---------------------------------------------------------------- panels */
.panel {
  background: var(--surface-1); border: 1px solid var(--hairline);
  border-radius: var(--r-lg); padding: 18px;
}
.panel > h3 { margin-bottom: 2px; }
.panel > .sub { margin: 0 0 14px; font-size: .8rem; color: var(--muted); }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.g3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }

/* hero + stat tiles */
.hero { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 30px; margin-bottom: 6px; }
.hero .figure { font-size: clamp(2.6rem, 5vw, 3.4rem); font-weight: 600; letter-spacing: -.03em; line-height: 1; }
.hero .cap { font-size: .82rem; color: var(--muted); margin-bottom: 8px; }
.hero .note { font-size: .84rem; color: var(--ink-2); max-width: 42ch; }

.tiles { display: grid; gap: 12px; grid-template-columns: repeat(6, 1fr); }
@media (max-width: 1300px) { .tiles { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 640px)  { .tiles { grid-template-columns: repeat(2, 1fr); } }
.tile {
  background: var(--surface-1); border: 1px solid var(--hairline);
  border-radius: var(--r-md); padding: 13px 14px;
}
.tile .label { font-size: .78rem; color: var(--muted); }
.tile .value { font-size: 1.5rem; font-weight: 600; letter-spacing: -.02em; margin-top: 3px; }
.tile .delta { font-size: .78rem; margin-top: 3px; color: var(--ink-2); }
.tile .delta.up { color: var(--good); }
.tile .delta.down { color: var(--bad); }

/* meters */
.meter { margin-top: 9px; }
.meter .track { height: 6px; border-radius: 3px; background: color-mix(in srgb, var(--s1) 18%, transparent); overflow: hidden; }
.meter .fill { height: 100%; border-radius: 3px; background: var(--s1); }
.meter .fill.warn { background: var(--st-warn); }
.meter .fill.crit { background: var(--st-critical); }
.meter .cap { display: flex; justify-content: space-between; font-size: .75rem; color: var(--muted); margin-top: 5px; font-variant-numeric: tabular-nums; }

/* ---------------------------------------------------------------- charts */
.chart { width: 100%; display: block; overflow: visible; }
.chart-wrap { position: relative; overflow-x: auto; }
.chart text { font-family: var(--font); }
.tick { fill: var(--axis-ink); font-size: 11px; }
.gridline { stroke: var(--grid); stroke-width: 1; }
.axisline { stroke: var(--baseline); stroke-width: 1; }
.dlabel { fill: var(--ink-2); font-size: 11px; font-weight: 550; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 10px 0 0; font-size: .8rem; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 11px; height: 11px; border-radius: 3px; display: inline-block; flex: none; }
.legend i.line { height: 2px; width: 15px; border-radius: 1px; }

.tip {
  position: fixed; z-index: var(--z-tooltip); pointer-events: none; opacity: 0;
  background: var(--surface-1); border: 1px solid var(--hairline);
  border-radius: var(--r-sm); padding: 8px 10px; font-size: .8rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .13); min-width: 128px;
  transition: opacity .1s linear;
}
.tip[data-show] { opacity: 1; }
.tip .t-head { color: var(--muted); font-size: .74rem; margin-bottom: 5px; }
.tip .t-row { display: flex; align-items: baseline; gap: 8px; }
.tip .t-row + .t-row { margin-top: 3px; }
.tip .t-key { width: 14px; height: 2px; border-radius: 1px; flex: none; align-self: center; }
.tip .t-val { font-weight: 600; font-variant-numeric: tabular-nums; }
.tip .t-name { color: var(--ink-2); font-size: .78rem; }

/* table-view twin */
.twin { margin-top: 12px; }
.twin summary {
  cursor: pointer; font-size: .78rem; color: var(--muted); list-style: none;
  padding: 4px 0;
}
.twin summary::-webkit-details-marker { display: none; }
.twin summary::before { content: "▸ "; }
.twin[open] summary::before { content: "▾ "; }
.twin summary:hover { color: var(--ink); }

/* ---------------------------------------------------------------- tables */
.tbl-wrap { overflow-x: auto; border: 1px solid var(--hairline); border-radius: var(--r-md); }
table { width: 100%; border-collapse: collapse; font-size: .84rem; }
thead th {
  position: sticky; top: 0; background: var(--surface-1); text-align: left;
  font-weight: 550; font-size: .76rem; color: var(--muted); padding: 9px 12px;
  border-bottom: 1px solid var(--hairline); white-space: nowrap; z-index: 1;
}
tbody td { padding: 8px 12px; border-bottom: 1px solid var(--hairline); vertical-align: top; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--wash); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--good); }
.neg { color: var(--ink); }
.dim { color: var(--muted); }
.wrap-any { word-break: break-word; }

.chip {
  display: inline-block; padding: 1px 7px; border-radius: 999px; font-size: .72rem;
  background: var(--wash); color: var(--ink-2); white-space: nowrap;
}
.chip.int { background: color-mix(in srgb, var(--s1) 14%, transparent); color: var(--s1); }
.chip.dec { background: color-mix(in srgb, var(--st-critical) 15%, transparent); color: var(--st-critical); }
.chip.pend { background: color-mix(in srgb, var(--s4) 18%, transparent); color: var(--s4); }

.acct-dot { width: 8px; height: 8px; border-radius: 2px; display: inline-block; margin-right: 6px; flex: none; }

/* people sections — rows never collapse */
.person { border: 1px solid var(--hairline); border-radius: var(--r-md); overflow: hidden; }
.person + .person { margin-top: 10px; }
.person > summary {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; cursor: pointer;
  background: var(--surface-1); list-style: none;
}
.person > summary::-webkit-details-marker { display: none; }
.person > summary:hover { background: var(--wash); }
.person .who { font-weight: 550; }
.person .rel { font-size: .74rem; color: var(--muted); }
.person .sums { margin-left: auto; display: flex; gap: 16px; font-size: .8rem; font-variant-numeric: tabular-nums; }
.person .body { border-top: 1px solid var(--hairline); }

.empty { padding: 34px; text-align: center; color: var(--muted); font-size: .88rem; }

.callout {
  border: 1px solid var(--hairline); border-radius: var(--r-md);
  background: var(--surface-1); padding: 14px 16px; font-size: .875rem; line-height: 1.6;
}
.callout h4 { margin: 0 0 5px; font-size: .9rem; }
.callout p { margin: 0; color: var(--ink-2); }
.callout + .callout { margin-top: 10px; }
.callout.flag { background: color-mix(in srgb, var(--st-warn) 9%, var(--surface-1)); }

ul.facts { margin: 0; padding-left: 18px; color: var(--ink-2); font-size: .875rem; line-height: 1.7; }
ul.facts strong { color: var(--ink); font-weight: 550; }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 900px) {
  .theme-btn { width: auto; margin: 0; }
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static; height: auto; flex-direction: row; align-items: center;
    gap: 14px; overflow-x: auto; padding: 10px 14px;
    border-right: 0; border-bottom: 1px solid var(--hairline);
  }
  .brand { flex: none; }
  .brand span { display: none; }
  .nav { flex-direction: row; gap: 4px; }
  .nav button { white-space: nowrap; padding: 7px 11px; }
  .nav-count { display: none; }
  .side-foot { margin: 0; padding: 0; }
  .side-foot br, .side-foot code { display: none; }
  .side-foot { font-size: 0; }
  .view, .toolbar { padding-left: 16px; padding-right: 16px; }
  .field input[type="search"] { width: 128px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

@media print {
  .sidebar, .toolbar { display: none; }
  .view { display: block !important; }
}

/* settings rows */
.match {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 11px 0; border-bottom: 1px solid var(--hairline);
}
.match:last-child { border-bottom: 0; }
.match > div:first-child { flex: 1 1 260px; min-width: 0; }
.match-acts { display: flex; gap: 6px; font-size: .8rem; color: var(--muted); }

/* editor */
.modal-back {
  position: fixed; inset: 0; z-index: 400; display: grid; place-items: center;
  background: rgba(0,0,0,.45); padding: 20px; overflow-y: auto;
}
.modal {
  background: var(--surface-1); border: 1px solid var(--hairline);
  border-radius: var(--r-lg); padding: 20px; width: 100%; max-width: 480px;
}
.modal h3 { margin-bottom: 2px; }
.modal .sub { margin: 0 0 14px; font-size: .8rem; color: var(--muted); }
.egrid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.egrid > label:nth-child(3), .egrid > label:last-child { grid-column: 1 / -1; }
.efield { display: block; }
.efield span { display: block; font-size: .74rem; color: var(--muted); margin-bottom: 4px; }
.efield input {
  width: 100%; padding: 7px 9px; font: inherit; font-size: .86rem;
  background: var(--plane); color: var(--ink);
  border: 1px solid var(--hairline); border-radius: var(--r-sm);
}
.modal-acts { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.ghost.primary { background: var(--ink); color: var(--plane); border-color: var(--ink); }
@media (max-width: 520px) { .egrid { grid-template-columns: 1fr; } }
