/* Tokens: reference data-viz palette (blue/red diverging pair, neutral net line). */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --income: #2a78d6;
  --expense: #e34948;
  --net: #52514e;
  --good: #006300;
  --bad: #d03b3b;
  --accent: #2a78d6;
  --wash: rgba(11, 11, 11, 0.05);
  --field: #ffffff;
  --radius: 12px;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --income: #3987e5; --expense: #e66767; --net: #c3c2b7;
    --good: #0ca30c; --accent: #3987e5; --wash: rgba(255, 255, 255, 0.06); --field: #121211;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --income: #3987e5; --expense: #e66767; --net: #c3c2b7;
  --good: #0ca30c; --accent: #3987e5; --wash: rgba(255, 255, 255, 0.06); --field: #121211;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--page); color: var(--ink); }
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 600; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

/* ---------- buttons & fields */
button { cursor: pointer; border-radius: 8px; border: 1px solid transparent; padding: 8px 14px; background: none; white-space: nowrap; }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--ink); color: var(--surface); }
button.primary:hover:not(:disabled) { opacity: .88; }
button.ghost { border-color: var(--border); }
button.ghost:hover:not(:disabled) { background: var(--wash); }
button.link { border: 0; padding: 4px 0; color: var(--ink-2); text-decoration: underline; text-underline-offset: 3px; }
button.small { padding: 4px 10px; font-size: 13px; }
button.danger { color: var(--bad); }
label { display: grid; gap: 4px; font-size: 13px; color: var(--ink-2); }
input, select {
  background: var(--field); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; min-width: 0; font-size: 15px; color: var(--ink);
}
input[type="number"] { font-variant-numeric: tabular-nums; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.error { color: var(--bad); font-size: 13px; }
.hint { color: var(--ink-2); font-size: 13px; }

/* ---------- login / password */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; display: grid; gap: 14px; }

/* ---------- layout */
.topbar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  max-width: 1120px; margin: 0 auto; padding: 16px;
}
.brand { font-weight: 600; }
.topbar nav { display: flex; gap: 8px; flex-wrap: wrap; }
.wrap { max-width: 1120px; margin: 0 auto; padding: 0 16px 48px; display: grid; gap: 16px; }
.wrap > *, .kpis > * { min-width: 0; } /* let wide tables scroll inside their card, not widen the page */
#dash.loading .kpi, #dash.loading .chart-wrap { opacity: .5; transition: opacity .15s; }

.filters { display: flex; flex-wrap: wrap; gap: 6px; }
.filters button { border: 1px solid var(--border); background: var(--surface); padding: 6px 12px; font-size: 14px; }
.filters button:hover { background: var(--wash); }
.filters button[aria-pressed="true"] { background: var(--ink); color: var(--surface); border-color: var(--ink); font-weight: 600; }

.banner {
  border: 1px solid var(--border); border-left: 3px solid var(--bad); background: var(--surface);
  border-radius: 8px; padding: 10px 14px; font-size: 14px; color: var(--ink-2);
}

/* ---------- stat tiles */
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.kpi { display: grid; gap: 2px; align-content: start; }
.kpi-label { font-weight: 600; }
.kpi-sub { color: var(--muted); font-size: 13px; }
.kpi-value { font-size: 34px; font-weight: 600; margin-top: 10px; line-height: 1.15; }
.kpi-value.up { color: var(--good); }
.kpi-value.down { color: var(--bad); }
.kpi-state { color: var(--ink-2); font-size: 13px; }
.kpi-break { display: flex; gap: 24px; margin: 12px 0 0; flex-wrap: wrap; }
.kpi-break div { display: grid; align-content: start; }
.kpi-break dt { color: var(--muted); font-size: 12px; }
.kpi-break dd { margin: 0; font-weight: 600; }
.kpi-count { color: var(--muted); font-size: 12px; }
.kpi-extra { color: var(--muted); font-size: 12px; margin-top: 6px; }

/* ---------- chart */
.chart-head { display: flex; justify-content: space-between; gap: 12px 24px; flex-wrap: wrap; align-items: flex-start; margin-bottom: 8px; }
.chart-sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.legend { list-style: none; margin: 0; padding: 0; display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); }
.legend li { display: flex; align-items: center; gap: 6px; }
.legend .key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; }
.legend .key-net { width: 16px; height: 2px; border-radius: 1px; }
.key-income { background: var(--income); }
.key-expense { background: var(--expense); }
.key-net { background: var(--net); }
.chart-wrap { position: relative; }
.chart { width: 100%; touch-action: pan-y; }
.chart:focus-visible { outline-offset: 4px; border-radius: 8px; }
.chart svg { display: block; overflow: visible; }
.chart .tick { fill: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart .bar.income { fill: var(--income); }
.chart .bar.expense { fill: var(--expense); }
.chart .net { fill: none; stroke: var(--net); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .net-dot { fill: var(--net); stroke: var(--surface); stroke-width: 2; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .end-label {
  fill: var(--ink-2); font-size: 12px; font-weight: 600;
  paint-order: stroke; stroke: var(--surface); stroke-width: 4px; stroke-linejoin: round;
}
.tooltip {
  position: absolute; z-index: 2; pointer-events: none; min-width: 170px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .12); padding: 10px 12px; font-size: 13px;
}
.tip-title { color: var(--ink-2); margin-bottom: 6px; }
.tip-row { display: grid; grid-template-columns: 14px auto 1fr; gap: 8px; align-items: center; }
.tip-row strong { font-variant-numeric: tabular-nums; text-align: right; }
.tip-row span:last-child { color: var(--muted); }
.tip-row .key { width: 12px; height: 3px; border-radius: 2px; }
.tip-note { color: var(--muted); font-size: 12px; margin-top: 4px; }
#btn-table { margin-top: 8px; font-size: 13px; }

/* ---------- tables */
.table-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
table.data td { padding: 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
table.data .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
#table-wrap { margin-top: 8px; }

/* ---------- nodes */
.section-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.node-form { border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin-bottom: 16px; display: grid; gap: 12px; }
.node-form .grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.node-form .wide { grid-column: 1 / -1; }
.nodes .node-name { font-weight: 600; }
.small-note { color: var(--muted); font-size: 12px; margin-top: 2px; }
.nodes tr.node-removed td { color: var(--ink-2); }
.actions { display: flex; gap: 6px; flex-wrap: nowrap; justify-content: flex-end; }
.actions-h { width: 1%; }
.pill { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--ink-2); }
.pill-active { border-color: var(--good); color: var(--good); }
.pill-ending { border-color: var(--axis); }
.pill-removed { color: var(--muted); }
.pill-edited { color: var(--ink-2); }
tr.sub td { background: var(--wash); }
.inline-form { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: end; }
.inline-form label { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--ink); }
.inline-form .hint { flex-basis: 100%; }
.charges { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 6px; }
.charges li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.charge-date { min-width: 110px; font-variant-numeric: tabular-nums; }
.charges input { width: 130px; padding: 4px 8px; }
.empty { color: var(--ink-2); }

@media (max-width: 760px) {
  .kpis { grid-template-columns: 1fr; }
  .node-form .grid { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 30px; }
  /* node rows become stacked cards */
  table.nodes thead { display: none; }
  table.nodes, table.nodes tbody, table.nodes tr, table.nodes td { display: block; width: 100%; }
  table.nodes tr { padding: 12px 0; border-bottom: 1px solid var(--grid); }
  table.nodes tr.sub { padding: 12px; border-bottom: 0; }
  table.nodes td { border: 0; padding: 0; text-align: left; }
  table.nodes td.num { text-align: left; }
  table.nodes td[data-label]::before { content: attr(data-label); display: block; color: var(--muted); font-size: 12px; margin-top: 8px; }
  table.nodes .actions { justify-content: flex-start; flex-wrap: wrap; margin-top: 10px; }
}
@media (max-width: 460px) {
  .node-form .grid { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: flex-start; }
}
