/* Household Finance — shared styling.
 * Token system: warm paper neutrals, QX = blue, KX = pink, CPF = neutral tan.
 * Three-state theme (system / forced light / forced dark).
 */

:root {
  --paper: #f6f3ec;
  --paper-raised: #fffdf8;
  --ink: #1c1a14;
  --ink-soft: #4a4536;
  --muted: #8a8271;
  --line: #ddd6c4;
  --accent: #2f6f5e;
  --accent-ink: #163c31;
  --accent-soft: #dfe9e3;
  --qx: #3568c9;
  --qx-soft: #dde5f6;
  --kx: #c33f8c;
  --kx-soft: #f6dfec;
  --cpf-neutral: #b7ae9b;
  --good: #3f7d5c;
  --warn: #b8863a;
  --critical: #b3462c;
  /* chart series — cash green, investments pink, CPF grey, housing blue */
  --c-cash: #3f7d5c;
  --c-invest: #c33f8c;
  --c-cpf: #8a8271;
  --c-housing: #3568c9;
  --shadow: 0 1px 2px rgba(28,26,20,0.06), 0 8px 24px -12px rgba(28,26,20,0.18);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #16150f;
    --paper-raised: #201e17;
    --ink: #ece7d9;
    --ink-soft: #c8c1ac;
    --muted: #8f8873;
    --line: #322f24;
    --accent: #5fae95;
    --accent-ink: #bfe6d5;
    --accent-soft: #223229;
    --qx: #83a8ef;
    --qx-soft: #263352;
    --kx: #e690c0;
    --kx-soft: #402839;
    --cpf-neutral: #6b6555;
    --good: #6fbb92;
    --warn: #d9ab5f;
    --critical: #e08067;
    --c-cash: #6fbb92;
    --c-invest: #e690c0;
    --c-cpf: #a39a85;
    --c-housing: #83a8ef;
    --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px -12px rgba(0,0,0,0.5);
  }
}
:root[data-theme="dark"] {
  --paper: #16150f;
  --paper-raised: #201e17;
  --ink: #ece7d9;
  --ink-soft: #c8c1ac;
  --muted: #8f8873;
  --line: #322f24;
  --accent: #5fae95;
  --accent-ink: #bfe6d5;
  --accent-soft: #223229;
  --qx: #83a8ef;
  --qx-soft: #263352;
  --kx: #e690c0;
  --kx-soft: #402839;
  --cpf-neutral: #6b6555;
  --good: #6fbb92;
  --warn: #d9ab5f;
  --critical: #e08067;
  --c-cash: #6fbb92;
  --c-invest: #e690c0;
  --c-cpf: #a39a85;
  --c-housing: #83a8ef;
  --shadow: 0 1px 2px rgba(0,0,0,0.3), 0 8px 24px -12px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; }
html, body { background: var(--paper); }
body {
  color: var(--ink);
  font-family: "IBM Plex Sans", ui-sans-serif, system-ui, sans-serif;
  font-size: 14px; line-height: 1.5; margin: 0;
}
.num, .tabular { font-family: "IBM Plex Mono", ui-monospace, "SF Mono", monospace; font-variant-numeric: tabular-nums; }
h1, h2, h3 { font-family: "IBM Plex Serif", ui-serif, Georgia, serif; text-wrap: balance; }
.muted { color: var(--muted); }

.shell { max-width: 1120px; margin: 0 auto; padding: 28px 24px 80px; }

/* ---------- Header ---------- */
header.top {
  display: flex; align-items: baseline; justify-content: space-between; gap: 16px;
  padding-bottom: 16px; border-bottom: 1px solid var(--line); margin-bottom: 16px; flex-wrap: wrap;
}
header.top .brand { display: flex; align-items: baseline; gap: 10px; }
header.top h1 { font-size: 22px; font-weight: 600; margin: 0; letter-spacing: 0.2px; }
header.top .as-of { color: var(--muted); font-size: 13px; }

/* ---------- Nav ---------- */
.nav { display: flex; gap: 4px; margin-bottom: 28px; border-bottom: 1px solid var(--line); position: relative; }
.nav-item {
  font: inherit; font-size: 13px; font-weight: 500; color: var(--muted);
  background: none; border: none; border-bottom: 2px solid transparent;
  padding: 10px 4px; margin-right: 22px; cursor: pointer; transition: color 0.15s ease;
}
.nav-item:hover { color: var(--ink-soft); }
.nav-item:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.nav-item[aria-current="page"] { color: var(--ink); border-bottom-color: var(--accent); }
.nav-more { position: relative; margin-left: auto; }
.nav-more > .nav-item { margin-right: 0; }
.nav-menu {
  position: absolute; right: 0; top: 100%; z-index: 20; min-width: 160px;
  background: var(--paper-raised); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: var(--shadow); padding: 4px; display: flex; flex-direction: column;
}
.nav-menu[hidden] { display: none; }   /* class rule would otherwise beat [hidden] */
.nav-menu .nav-item {
  margin: 0; padding: 8px 12px; text-align: left; border-bottom: none; border-radius: 6px;
}
.nav-menu .nav-item:hover { background: var(--accent-soft); color: var(--ink); }
@media (prefers-reduced-motion: reduce) { .nav-item { transition: none; } }

/* ---------- View toggle (Household / QX / KX) ---------- */
.view-toggle { display: inline-flex; align-self: center; background: var(--paper-raised); border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.view-toggle button {
  font: inherit; font-size: 12px; font-weight: 500; color: var(--muted);
  background: none; border: none; cursor: pointer; padding: 6px 14px;
  transition: color 0.12s ease, background 0.12s ease;
}
.view-toggle button + button { border-left: 1px solid var(--line); }
.view-toggle button:hover { color: var(--ink-soft); }
.view-toggle button:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.view-toggle button[aria-pressed="true"] { color: var(--paper-raised); background: var(--accent); }
body[data-view="QX"] .view-toggle button[data-view="QX"] { background: var(--qx); }
body[data-view="KX"] .view-toggle button[data-view="KX"] { background: var(--kx); }
@media (prefers-reduced-motion: reduce) { .view-toggle button { transition: none; } }

.panel { display: none; }
.panel.active { display: block; }

/* ---------- Section heading ---------- */
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 34px 0 12px; flex-wrap: wrap; }
.section-head:first-child { margin-top: 0; }
.section-head h2 { font-size: 14px; font-weight: 600; margin: 0; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-soft); }
.section-head .note { font-size: 12px; color: var(--muted); }

/* ---------- KPI row (Home) ---------- */
.kpi-row {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px;
  background: var(--line); border: 1px solid var(--line); border-radius: 12px;
  overflow: hidden; box-shadow: var(--shadow);
}
.kpi { background: var(--paper-raised); padding: 20px 22px; }
.kpi-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); margin-bottom: 8px; display: flex; align-items: center; gap: 6px; }
.kpi-value { font-size: 30px; font-weight: 600; color: var(--accent-ink); line-height: 1.1; }
.kpi-value.muted { color: var(--muted); }
.kpi-delta { font-size: 12px; margin-top: 8px; }
.kpi-delta.up { color: var(--good); }
.kpi-delta.down { color: var(--critical); }
.kpi-delta.muted { color: var(--muted); }
.info { cursor: help; color: var(--muted); font-size: 11px; border: 1px solid var(--line); border-radius: 999px; width: 15px; height: 15px; display: inline-flex; align-items: center; justify-content: center; }

/* ---------- Range bar ---------- */
.range-bar { display: inline-flex; gap: 4px; margin-bottom: 10px; }
.range-bar button {
  font: inherit; font-size: 12px; font-weight: 500; color: var(--muted);
  background: var(--paper-raised); border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 12px; cursor: pointer;
}
.range-bar button:hover { color: var(--ink-soft); }
.range-bar button.is-on { background: var(--accent); color: var(--paper-raised); border-color: var(--accent); }

/* ---------- Charts ---------- */
.chart { position: relative; width: 100%; margin: 4px 0 6px; }
.chart-svg { width: 100%; height: auto; display: block; overflow: visible; }
.chart-grid { stroke: var(--line); stroke-width: 1; }
.chart-xlab { fill: var(--muted); font-size: 11px; font-family: "IBM Plex Sans", sans-serif; }
.chart-bar.is-selected rect { stroke: var(--ink); stroke-width: 1.5; }
.chart-tip {
  position: absolute; z-index: 10; pointer-events: none; background: var(--ink); color: var(--paper);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 150px; box-shadow: var(--shadow);
}
.chart-tip .tip-h { font-weight: 600; margin-bottom: 4px; }
.chart-tip .tip-t { border-top: 1px solid rgba(255,255,255,0.25); margin-top: 4px; padding-top: 4px; }
.chart-tip .k { display: inline-flex; align-items: center; gap: 5px; }
.chart-tip .k i { width: 8px; height: 8px; border-radius: 2px; display: inline-block; }
.chart-tip b { float: right; margin-left: 14px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 5px; vertical-align: baseline; }

/* ---------- Housing statement ---------- */
.housing-card {
  background: var(--paper-raised); border: 1px solid var(--line); border-radius: 12px;
  padding: 22px 24px; box-shadow: var(--shadow); display: grid; grid-template-columns: 1.1fr 1fr; gap: 30px;
}
.housing-statement { align-self: start; }
.stmt-row { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.stmt-row .k { color: var(--ink-soft); }
.stmt-row .k .tag { font-size: 10px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin-left: 8px; white-space: nowrap; }
.stmt-row .v { font-weight: 500; white-space: nowrap; }
.stmt-row.neg .v { color: var(--muted); }
.stmt-row.subtotal { border-bottom-width: 2px; }
.stmt-row.subtotal .k, .stmt-row.subtotal .v { font-weight: 600; }
.stmt-row.total { border-bottom: none; padding-top: 12px; font-size: 15px; }
.stmt-row.total .k { color: var(--ink); font-weight: 600; }
.stmt-row.total .v { font-weight: 700; color: var(--accent-ink); }
.stmt-note { font-size: 12px; color: var(--muted); margin: 12px 0 0; }

/* ---------- Metric cards ---------- */
.metric-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.metric-grid.two { grid-template-columns: 1fr 1fr; }
.metric { background: var(--paper-raised); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; box-shadow: var(--shadow); }
.metric-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); margin-bottom: 6px; }
.metric-value { font-size: 19px; font-weight: 600; }
.metric-sub { font-size: 11px; color: var(--muted); margin-top: 3px; }

/* ---------- Horizontal proportion bar ---------- */
.hbar-wrap { margin: 4px 0; }
.hbar { display: flex; height: 34px; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.hbar-seg { display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600; color: #fff; white-space: nowrap; overflow: hidden; }
.hbar-legend { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 16px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.hbar-legend i { width: 9px; height: 9px; border-radius: 2px; display: inline-block; margin-right: 5px; }

/* ---------- Horizontal bars (by purpose) ---------- */
.hbars { display: flex; flex-direction: column; gap: 6px; }
.hbars-row { display: grid; grid-template-columns: 160px 1fr 110px; align-items: center; gap: 12px; font-size: 12px; }
.hbars-k { color: var(--ink-soft); }
.hbars-track { background: var(--line); border-radius: 4px; height: 14px; overflow: hidden; }
.hbars-fill { display: block; height: 100%; background: var(--accent); }
.hbars-v { text-align: right; font-weight: 500; }

/* ---------- Table ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
.table-wrap.rows4 { overflow-y: auto; max-height: 196px; }   /* header + ~4 rows */
.table-wrap.rows4 .ledger-table thead th { position: sticky; top: 0; z-index: 1; }
.ledger-table { width: 100%; border-collapse: collapse; font-size: 12px; min-width: 720px; }
.ledger-table th { text-align: center; padding: 10px 12px; background: var(--paper-raised); color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; font-size: 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; }
.ledger-table td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: center; vertical-align: middle; }
.ledger-table td.notes { color: var(--muted); text-align: left; }
.ledger-table tr:last-child td { border-bottom: none; }

/* ---------- Stub ---------- */
.stub { background: var(--paper-raised); border: 1px dashed var(--line); border-radius: 12px; padding: 40px 32px; max-width: 620px; }
.stub-phase { display: inline-block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--accent-ink); background: var(--accent-soft); border-radius: 999px; padding: 3px 10px; margin-bottom: 12px; }
.stub h2 { font-size: 20px; margin: 0 0 10px; }
.stub p { color: var(--ink-soft); margin: 0 0 10px; }

/* ---------- States ---------- */
.empty-state, .error-state, .loading-state {
  background: var(--paper-raised); border: 1px dashed var(--line); border-radius: 10px;
  padding: 24px; text-align: center; color: var(--muted); font-size: 13px;
}
.error-state { border-color: var(--critical); color: var(--critical); }
.callout { font-size: 12px; color: var(--warn); margin: 12px 0 0; max-width: 70ch; }
.empty-note { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* ---------- Sign-in gate ---------- */
.gate { max-width: 420px; margin: 18vh auto 0; text-align: center; }
.gate h1 { font-size: 22px; margin-bottom: 8px; }
.gate p { color: var(--muted); margin-bottom: 20px; }
.gate button {
  font: inherit; font-weight: 500; background: var(--accent); color: var(--paper-raised);
  border: none; border-radius: 8px; padding: 10px 22px; cursor: pointer; font-size: 14px;
}
.gate button:hover { opacity: 0.9; }
.gate .gate-error { color: var(--critical); font-size: 13px; margin-top: 14px; }

footer.status {
  margin-top: 44px; padding-top: 16px; border-top: 1px solid var(--line);
  font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; flex-wrap: wrap; gap: 8px;
}

@media (max-width: 820px) {
  .kpi-row { grid-template-columns: 1fr; }
  .housing-card { grid-template-columns: 1fr; }
  .metric-grid, .metric-grid.two { grid-template-columns: 1fr 1fr; }
  .hbars-row { grid-template-columns: 110px 1fr 90px; }
}
