/* Tokens. Dark values are their own validated steps, not an automatic flip. */
:root {
  color-scheme: light;
  --bg: #f9f9f7;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-secondary: #52514e;
  --muted: #6b6a66;
  --border: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --danger: #c62828;
  --success: #006300;
  --warning-text: #8a5a00;
  --warning-bg: #fff3cd;
  /* chart series (validated: scripts/validate_palette.js, light surface #fcfcfb) */
  --series-income: #2a78d6;
  --series-expense: #eb6834;
  --div-pos: #2a78d6;
  --div-neg: #e34948;
  /* ordinal heatmap ramp, steps 250-650 (light end clears 2:1) */
  --heat-1: #86b6ef; --heat-1-ink: #0b0b0b;
  --heat-2: #5598e7; --heat-2-ink: #0b0b0b;
  --heat-3: #2a78d6; --heat-3-ink: #ffffff;
  --heat-4: #1c5cab; --heat-4-ink: #ffffff;
  --heat-5: #104281; --heat-5-ink: #ffffff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --text: #f0efec;
    --text-secondary: #c3c2b7;
    --muted: #9a9890;
    --border: #2c2c2a;
    --axis: #383835;
    --accent: #6da7ec;
    --on-accent: #0b0b0b;
    --danger: #e66767;
    --success: #0ca30c;
    --warning-text: #fab219;
    --warning-bg: #2d2410;
    --series-income: #3987e5;
    --series-expense: #d95926;
    --div-pos: #3987e5;
    --div-neg: #e66767;
    /* dark ramp: near-zero sits closest to the surface (no darker than step 600) */
    --heat-1: #184f95; --heat-1-ink: #f0efec;
    --heat-2: #256abf; --heat-2-ink: #f0efec;
    --heat-3: #3987e5; --heat-3-ink: #0b0b0b;
    --heat-4: #6da7ec; --heat-4-ink: #0b0b0b;
    --heat-5: #9ec5f4; --heat-5-ink: #0b0b0b;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --text: #f0efec;
  --text-secondary: #c3c2b7;
  --muted: #9a9890;
  --border: #2c2c2a;
  --axis: #383835;
  --accent: #6da7ec;
  --on-accent: #0b0b0b;
  --danger: #e66767;
  --success: #0ca30c;
  --warning-text: #fab219;
  --warning-bg: #2d2410;
  --series-income: #3987e5;
  --series-expense: #d95926;
  --div-pos: #3987e5;
  --div-neg: #e66767;
  --heat-1: #184f95; --heat-1-ink: #f0efec;
  --heat-2: #256abf; --heat-2-ink: #f0efec;
  --heat-3: #3987e5; --heat-3-ink: #0b0b0b;
  --heat-4: #6da7ec; --heat-4-ink: #0b0b0b;
  --heat-5: #9ec5f4; --heat-5-ink: #0b0b0b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a { color: var(--accent); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar .brand { font-weight: 700; text-decoration: none; color: var(--text); }
.topbar nav { display: flex; align-items: center; gap: 1rem; }
.topbar nav form { margin: 0; }

.container { max-width: 1100px; margin: 2rem auto; padding: 0 1.5rem; }
.container.narrow { max-width: 420px; }

input, select, textarea {
  width: 100%;
  padding: 0.5rem;
  font: inherit;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}
label { display: block; margin: 0.75rem 0 0.25rem; }

button, .btn {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.5rem 1rem;
  font: inherit;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent, #fff);
  cursor: pointer;
  text-decoration: none;
}
.btn-secondary { background: var(--surface); color: var(--accent); }
button.link {
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
}

.messages { list-style: none; max-width: 1100px; margin: 1rem auto 0; padding: 0 1.5rem; }
.messages li { padding: 0.5rem 1rem; border-radius: 6px; background: var(--surface); }
.messages .error { color: var(--danger); }
.messages .success { color: var(--success); }
.errorlist { color: var(--danger); padding-left: 1rem; }

h2 { margin-top: 2rem; }
.muted { color: var(--muted); }
.narrow-form { max-width: 420px; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin-bottom: 1rem; }
.filters select, .filters input[type="search"] { width: auto; min-width: 200px; }
.filters button { margin-top: 0; }
label.inline { display: inline-flex; align-items: center; gap: 0.35rem; margin: 0.5rem 0; }
label.inline input { width: auto; }
.inline-form { margin-bottom: 1rem; }
fieldset { margin-top: 1.25rem; border: 1px solid var(--border); border-radius: 8px; }

.topbar .count {
  display: inline-block;
  min-width: 1.4rem;
  padding: 0 0.35rem;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  text-align: center;
}

.groups { display: grid; gap: 0.75rem; }
.group {
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.group-head { display: flex; justify-content: space-between; gap: 1rem; }
.group-head .num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.group-form { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin-top: 0.5rem; }
.group-form select { width: auto; min-width: 220px; }
.group-form input[type="text"] { width: auto; flex: 1; min-width: 180px; }
.group-form button { margin-top: 0; }
.negative { color: var(--danger); }
.positive { color: var(--success); }
.badge { background: var(--bg); color: var(--muted); }

.details { display: grid; grid-template-columns: max-content 1fr; gap: 0.25rem 1rem; }
.details dt { color: var(--muted); }
.details dd { margin: 0; }

table { width: 100%; border-collapse: collapse; background: var(--surface); font-size: 0.92rem; }
th, td { padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--border); text-align: left; }
th { background: var(--bg); font-weight: 600; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
td.negative { color: var(--danger); }
td.positive { color: var(--success); }

.stats { display: flex; gap: 1rem; flex-wrap: wrap; }
.stats div {
  padding: 0.75rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.stats strong { font-size: 1.4rem; display: block; }
.warnings li { color: var(--warning-text); }
.badge { font-size: 0.75rem; padding: 0.1rem 0.4rem; border-radius: 4px; }
.badge.warning { background: var(--warning-bg); color: var(--warning-text); }
.pagination { display: flex; gap: 1rem; margin: 1rem 0; }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; padding: 0.75rem 1rem; }
  .topbar nav { flex-wrap: wrap; gap: 0.75rem; }
  .container { padding: 0 1rem; margin-top: 1.25rem; }
}

/* --- Dashboard ------------------------------------------------------------- */
.dash-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 0.5rem 1rem; }
.dash-head h1 { margin: 0; }
.dash-head .filters { margin: 0; }
.dash-head .filters select { min-width: 140px; }
.period { margin: 0.5rem 0 1.25rem; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 0.75rem; }
.tile { padding: 1rem 1.25rem; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.tile-label { color: var(--text-secondary); font-size: 0.9rem; }
.tile-value { font-size: 1.6rem; font-weight: 600; margin: 0.15rem 0; }
.tile-sub { color: var(--muted); font-size: 0.85rem; }
.tile.hero .tile-value { font-size: 3rem; line-height: 1.1; }

.viz-root { margin-top: 1rem; }
.card {
  margin-top: 1rem;
  padding: 1rem 1.25rem 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  min-width: 0;
}
.card h2 { margin: 0 0 0.5rem; font-size: 1.05rem; }
.card-note { margin: -0.25rem 0 0.6rem; font-size: 0.82rem; }
.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 0 1rem; }

.legend { display: flex; flex-wrap: wrap; gap: 0.25rem 1rem; list-style: none; margin: 0 0 0.5rem; padding: 0;
  color: var(--text-secondary); font-size: 0.85rem; }
.legend li { display: inline-flex; align-items: center; gap: 0.4rem; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; flex: none; }
.s-income { fill: var(--series-income); background: var(--series-income); }
.s-expense { fill: var(--series-expense); background: var(--series-expense); }
.s-pos { fill: var(--div-pos); background: var(--div-pos); }
.s-neg { fill: var(--div-neg); background: var(--div-neg); }

.chart { width: 100%; }
.chart svg { display: block; overflow: visible; }
.chart .grid { stroke: var(--border); stroke-width: 1; shape-rendering: crispEdges; }
.chart .axis { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .hit { fill: transparent; outline: none; cursor: default; }
.chart .hit:hover, .chart .hit:focus-visible { fill: var(--text); fill-opacity: 0.04; }
.chart-note { margin: 0.25rem 0 0; color: var(--muted); font-size: 0.8rem; }

.viz-tooltip {
  position: absolute;
  z-index: 10;
  max-width: 280px;
  padding: 0.5rem 0.65rem;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.12);
  font-size: 0.85rem;
  pointer-events: none;
}
.viz-tooltip div { display: flex; align-items: center; gap: 0.4rem; color: var(--text-secondary); }

.table-view { margin-top: 0.75rem; }
.table-view summary { cursor: pointer; color: var(--accent); font-size: 0.9rem; }
.table-view table { margin-top: 0.5rem; }
tfoot th, tfoot td { font-weight: 600; }

.hbars { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.35rem; }
.hbar-row { display: grid; grid-template-columns: minmax(0, 11rem) 1fr auto; align-items: center; gap: 0.6rem;
  padding: 0.15rem 0.25rem; border-radius: 6px; outline: none; color: inherit; text-decoration: none; }
.hbar-row:hover, .hbar-row:focus-visible { background: var(--bg); }
a.hbar-row .hbar-name { color: var(--accent); }
a.hbar-row:focus-visible { box-shadow: 0 0 0 2px var(--accent); }
.heatmap th[scope="row"] a { color: inherit; }
.crumbs { margin: 0 0 0.25rem; font-size: 0.9rem; }
.hbar-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 0.88rem; }
.hbar-track { display: block; height: 14px; }
.hbar { display: block; height: 14px; min-width: 2px; background: var(--series-income);
  border-radius: 0 4px 4px 0; }
.hbar-value { font-size: 0.85rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; white-space: nowrap; }

.table-scroll { overflow-x: auto; }
.heatmap th[scope="row"] { white-space: nowrap; background: var(--surface); font-weight: 500; }
.heatmap { border-collapse: separate; border-spacing: 2px; }
.heatmap th, .heatmap td { border: 0; border-radius: 4px; }
.heatmap td { font-size: 0.82rem; }
.heatmap td.total { font-weight: 600; }
.heat-1 { background: var(--heat-1); color: var(--heat-1-ink); }
.heat-2 { background: var(--heat-2); color: var(--heat-2-ink); }
.heat-3 { background: var(--heat-3); color: var(--heat-3-ink); }
.heat-4 { background: var(--heat-4); color: var(--heat-4-ink); }
.heat-5 { background: var(--heat-5); color: var(--heat-5-ink); }
.heat-legend .swatch { width: 14px; }
