:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface-1: #ffffff;
  --surface-2: #f0efea;
  --border: #e2e0d8;
  --text-primary: #191919;
  --text-secondary: #666666;
  --accent: #1b64be;
  --accent-ink: #ffffff;
  --grid: #ebe9e3;
  --danger: #dc3c3d;
  --good: #0a7d1c;
  --warn-bg: #fff4dc;
  --warn-ink: #7a5200;

  /* categorical slots — fixed order, colour follows the account */
  --series-1: #257de0;
  --series-2: #f47230;
  --series-3: #00b894;
  --series-4: #f2a600;
  --series-5: #ee7fac;
  --series-6: #008a17;
  --series-7: #6a4ce2;
  --series-8: #ec4647;

  --radius: 10px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121212;
    --surface-1: #191919;
    --surface-2: #222222;
    --border: #2f2f2f;
    --text-primary: #efefef;
    --text-secondary: #9c9c9c;
    --accent: #338ced;
    --accent-ink: #0b0b0b;
    --grid: #262626;
    --good: #0aac23;
    --warn-bg: #3a2c08;
    --warn-ink: #f6c95a;

    --series-1: #338ced;
    --series-2: #e76624;
    --series-3: #00a888;
    --series-4: #d28e05;
    --series-5: #e24d8b;
    --series-6: #0aac23;
    --series-7: #9887ef;
    --series-8: #eb6263;
  }
}
:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #121212;
    --surface-1: #191919;
    --surface-2: #222222;
    --border: #2f2f2f;
    --text-primary: #efefef;
    --text-secondary: #9c9c9c;
    --accent: #338ced;
    --accent-ink: #0b0b0b;
    --grid: #262626;
    --good: #0aac23;
    --warn-bg: #3a2c08;
    --warn-ink: #f6c95a;

    --series-1: #338ced;
    --series-2: #e76624;
    --series-3: #00a888;
    --series-4: #d28e05;
    --series-5: #e24d8b;
    --series-6: #0aac23;
    --series-7: #9887ef;
    --series-8: #eb6263;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font: 14px/1.45 var(--font);
}
h1, h2 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 18px; }
h2 { font-size: 15px; }

/* top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 24px; border-bottom: 1px solid var(--border);
  background: var(--surface-1); position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, var(--series-3), var(--series-1) 55%, var(--series-7));
}
.topbar-actions { display: flex; gap: 6px; }

/* buttons */
.btn {
  font: inherit; font-weight: 550; cursor: pointer;
  border-radius: 8px; padding: 6px 12px; border: 1px solid var(--border);
  background: var(--surface-1); color: var(--text-primary);
}
.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.08); }
.btn.ghost { border-color: transparent; color: var(--text-secondary); }
.btn.ghost:hover { color: var(--text-primary); }
.btn:disabled { opacity: .45; cursor: not-allowed; }

/* layout */
.layout {
  display: grid; grid-template-columns: minmax(340px, 430px) 1fr;
  gap: 20px; padding: 20px 24px 40px; align-items: start;
  max-width: 1500px; margin: 0 auto;
}
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 14px; }
  .results { order: -1; }
}
.inputs, .results { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.results { position: sticky; top: 72px; }
@media (max-width: 960px) { .results { position: static; } }

.panel {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px;
}
.panel > h2 { margin-bottom: 12px; }
.panel-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.panel-head h2 { margin-right: auto; }
.hint { color: var(--text-secondary); font-size: 12.5px; margin: 0 0 12px; }

/* fields */
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
@media (max-width: 420px) { .grid3 { grid-template-columns: 1fr 1fr; } }
.field {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: var(--text-secondary); min-width: 0;
}
.field input, .field select, .add-row select {
  font: inherit; font-size: 14px; color: var(--text-primary);
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 7px; padding: 6px 8px; width: 100%; min-width: 0;
  font-variant-numeric: tabular-nums;
}
.field input:focus, .field select:focus, .acc-name:focus, .inline-num:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent;
}
.field input::placeholder { color: var(--text-secondary); opacity: .7; }
.money { position: relative; display: block; }
.money .cur {
  position: absolute; left: 8px; top: 50%; transform: translateY(-50%);
  color: var(--text-secondary); font-size: 13px; pointer-events: none;
}
.money input { padding-left: 20px; }
.field.check { flex-direction: row; align-items: center; gap: 8px; grid-column: span 2; color: var(--text-primary); font-size: 13px; }
.field.check input { width: auto; }

.retire { border: 0; padding: 0; margin: 14px 0 0; display: flex; flex-direction: column; gap: 6px; }
.retire legend { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; padding: 0; }
.radio { display: flex; align-items: center; gap: 8px; }
.inline-num {
  font: inherit; width: 64px; padding: 3px 6px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
}

/* accounts */
.add-row { display: flex; gap: 6px; }
.add-row select { width: auto; }
.accounts { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.account {
  border: 1px solid var(--border); border-radius: 9px; padding: 10px 12px 8px;
  background: var(--surface-1);
  border-left: 4px solid var(--swatch, var(--border));
}
.acc-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; background: var(--swatch); flex: none; display: none; }
.acc-name {
  font: inherit; font-weight: 600; font-size: 14.5px; flex: 1; min-width: 0;
  border: 1px solid transparent; background: transparent; color: var(--text-primary);
  border-radius: 6px; padding: 3px 6px; margin-left: -6px;
}
.acc-name:hover { border-color: var(--border); }
.acc-tools { display: flex; gap: 2px; }
.icon {
  font: inherit; width: 26px; height: 26px; border-radius: 6px; cursor: pointer;
  border: 0; background: transparent; color: var(--text-secondary);
}
.icon:hover { background: var(--surface-2); color: var(--text-primary); }
.icon.danger:hover { color: var(--danger); }
.icon:disabled { opacity: .3; cursor: default; background: transparent; }

.advanced { margin-top: 8px; }
.advanced summary {
  cursor: pointer; font-size: 12px; color: var(--text-secondary); padding: 2px 0;
  list-style-position: inside;
}
.advanced summary:hover { color: var(--text-primary); }
.advanced[open] summary { margin-bottom: 8px; }

/* tiles */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
@media (max-width: 1200px) { .tiles { grid-template-columns: repeat(2, 1fr); } }
.tile {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px;
}
.tile .k { font-size: 12px; color: var(--text-secondary); }
.tile .v { font-size: 24px; font-weight: 680; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .s { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.tile.hero { background: var(--accent); border-color: var(--accent); }
.tile.hero .k, .tile.hero .s, .tile.hero .v { color: var(--accent-ink); }
.tile.hero .k, .tile.hero .s { opacity: .85; }

.banner {
  grid-column: 1 / -1; background: var(--warn-bg); color: var(--warn-ink);
  border-radius: 8px; padding: 8px 12px; font-size: 13px;
}
.banner::before { content: "⚠ "; }

/* segmented controls */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 2px; }
.seg button {
  font: inherit; font-size: 12.5px; border: 0; background: transparent; color: var(--text-secondary);
  padding: 4px 10px; border-radius: 6px; cursor: pointer;
}
.seg button[aria-pressed="true"] { background: var(--surface-1); color: var(--text-primary); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

/* legend */
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin: 2px 0 8px; font-size: 12.5px; color: var(--text-primary); }
.legend .item { display: inline-flex; align-items: center; gap: 6px; }
.legend .key { width: 10px; height: 10px; border-radius: 3px; }
.legend .line-key { width: 16px; height: 0; border-top: 2px dashed var(--text-secondary); }
.legend .vline-key { width: 0; height: 12px; border-left: 2px solid var(--text-primary); }

/* chart */
.chart-wrap { position: relative; height: 420px; }
@media (max-width: 960px) { .chart-wrap { height: 320px; } }
#chart { width: 100%; height: 100%; display: block; overflow: visible; }
#chart text { font-family: var(--font); font-size: 11px; fill: var(--text-secondary); font-variant-numeric: tabular-nums; }
#chart .gridline { stroke: var(--grid); stroke-width: 1; }
#chart .baseline { stroke: var(--border); stroke-width: 1; }
#chart .band { stroke: var(--surface-1); stroke-width: 2; stroke-linejoin: round; }
#chart .target { stroke: var(--text-secondary); stroke-width: 2; stroke-dasharray: 5 4; fill: none; }
#chart .retire-line { stroke: var(--text-primary); stroke-width: 1.5; }
#chart .retire-label, #chart .direct-label { fill: var(--text-primary); font-weight: 600; }
#chart .direct-label { font-size: 11.5px; }
#chart .label-pill { fill: var(--surface-1); opacity: .92; }
#chart .crosshair { stroke: var(--text-primary); stroke-width: 1; opacity: .5; }
#chart .runout { fill: var(--danger); opacity: .08; }

.tooltip {
  position: absolute; pointer-events: none; z-index: 3;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 190px;
  box-shadow: 0 6px 20px rgba(0,0,0,.12);
}
.tooltip .t-head { font-weight: 650; margin-bottom: 4px; color: var(--text-primary); }
.tooltip .t-row { display: flex; align-items: center; gap: 6px; color: var(--text-primary); }
.tooltip .t-row .key { width: 8px; height: 8px; border-radius: 2px; flex: none; }
.tooltip .t-row .n { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }
.tooltip .t-total { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 4px; font-weight: 650; }
.tooltip .t-note { color: var(--text-secondary); margin-top: 4px; }

/* table */
.table-wrap { overflow: auto; max-height: 480px; border: 1px solid var(--border); border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-size: 12.5px; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th:first-child, td:first-child { text-align: left; }
thead th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; color: var(--text-secondary); }
tr.retired td:first-child::after { content: " ●"; color: var(--accent); }
tr.short td { color: var(--danger); }

.footnote { color: var(--text-secondary); font-size: 12px; margin: 12px 0 0; }
