:root {
  color-scheme: light;
  --surface: #fcfcfb;
  --surface-2: #f3f2ef;
  --border: #dddcd7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6f6e69;
  --accent: #2a78d6;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --band-outer: #cde2fb;
  --band-inner: #86b6ef;
  --critical: #d03b3b;
  /* Rich, broke or dead: ordinal blue ramp (validated --ordinal), neutral for dead. */
  --tier-1: #86b6ef;
  --tier-2: #3987e5;
  --tier-3: #1c5cab;
  --tier-4: #0d366b;
  --state-dead: #a09f98;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #3a3a37;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #a09f98;
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --band-outer: #104281;
    --band-inner: #1c5cab;
    --tier-1: #184f95;
    --tier-2: #2a78d6;
    --tier-3: #6da7ec;
    --tier-4: #b7d3f6;
    --state-dead: #52514e;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface: #1a1a19;
  --surface-2: #242422;
  --border: #3a3a37;
  --text: #f0efec;
  --text-2: #c3c2b7;
  --muted: #a09f98;
  --accent: #3987e5;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --band-outer: #104281;
  --band-inner: #1c5cab;
  --tier-1: #184f95;
  --tier-2: #2a78d6;
  --tier-3: #6da7ec;
  --tier-4: #b7d3f6;
  --state-dead: #52514e;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  padding: 16px;
  background: var(--surface);
  color: var(--text);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}
h1 { margin: 0; font-size: 20px; }
header p { margin: 4px 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
code { font-size: 12px; }

main {
  display: grid;
  grid-template-columns: minmax(280px, 340px) 1fr;
  gap: 16px;
  align-items: start;
}
@media (max-width: 800px) { main { grid-template-columns: 1fr; } }

fieldset {
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0 0 12px;
  padding: 8px 12px 12px;
}
legend { font-weight: 600; padding: 0 4px; }
label { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 6px 0; color: var(--text-2); }
input, select, button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 6px;
}
label input, label select { width: 9em; }
button { cursor: pointer; background: var(--surface-2); }
details summary { cursor: pointer; color: var(--text-2); margin: 6px 0; }

.row { display: grid; grid-template-columns: 4.5em 1fr 6.5em 1.6em 2em; gap: 4px; margin: 4px 0; align-items: center; }
#holdings .row { padding-bottom: 6px; border-bottom: 1px solid var(--border); }
.row .note { grid-column: 1 / -1; }
input.note { width: 100%; margin-top: 4px; font-size: 12px; }
input.invalid { border-color: var(--critical); outline-color: var(--critical); }
.addrow { display: flex; gap: 4px; margin-top: 6px; }
.addrow select { flex: 1; min-width: 0; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 0 0 12px; }
[data-params] label { display: inline-flex; margin: 0; gap: 4px; }
[data-params] input { width: 4.5em; }
[data-params] input[data-money] { width: 6em; }
.row input, .row select { width: 100%; }
.row.head { margin-bottom: 0; }
input.check, label input.check { width: auto; justify-self: center; }
.prow { margin: 6px 0; padding-bottom: 6px; border-bottom: 1px solid var(--border); }
.ptop { display: grid; grid-template-columns: 1fr 4.5em 6.5em 2em; gap: 4px; }
.ptop input, .ptop select { width: 100%; min-width: 0; }
.pbottom { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; margin-top: 4px; color: var(--text-2); }
.pbottom input { width: 4em; }
.person { border-bottom: 1px solid var(--border); padding-bottom: 6px; margin-bottom: 6px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px 8px; }
.grid3 label { margin: 2px 0; }
.grid3 input { width: 4em; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 8px; margin-bottom: 12px; }
.tile { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.tile .v { font-size: 22px; font-weight: 600; }
.tile .k { color: var(--muted); font-size: 12px; }

nav[role="tablist"] { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
nav button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
#viewOptions { min-height: 24px; color: var(--text-2); }
#viewOptions label { display: inline-flex; justify-content: flex-start; gap: 6px; margin-right: 12px; }
#viewOptions input { width: auto; }

.chart { min-height: 380px; overflow-x: auto; }
.chart svg { max-width: 100%; height: auto; font-size: 11px; }
.banner { border: 1px solid var(--border); background: var(--surface-2); border-radius: 8px; padding: 8px 12px; margin-bottom: 12px; }
.banner.error { border-color: var(--critical); }
table { border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
td, th { padding: 2px 8px; text-align: right; border-bottom: 1px solid var(--border); }

.otop { display: grid; grid-template-columns: 1fr 2em; gap: 4px; }
[data-params] select { width: auto; max-width: 9em; }
#modelParams label input { width: 9em; }
.scroll { overflow-x: auto; max-width: 100%; }
.chart svg.clickable { cursor: crosshair; }

/* Grid items default to min-width: auto; let wide tables scroll inside .scroll instead of widening the page. */
main > * { min-width: 0; }

.headroom p { margin: 2px 0 6px; }
.headroom table { margin: 2px 0; }
.headroom td:first-child, .headroom th:first-child { text-align: left; }
.headroom td.up { color: var(--text); font-weight: 600; }
.headroom td.down { color: var(--critical); font-weight: 600; }
label.sharemode { display: inline-flex; gap: 6px; margin: 0 0 0 8px; }
label.sharemode input { width: auto; }
input[readonly][data-money] { background: var(--surface-2); color: var(--text-2); }

/* Plot tooltips default to a white box: follow the theme instead (Plot sets it in a zero-specificity :where rule). */
.chart svg { --plot-background: var(--surface); }

/* Compare table */
.cmp table { font-size: 14px; margin: 12px 0; }
.cmp td, .cmp th { padding: 6px 12px; }
.cmp tbody th { text-align: left; font-weight: normal; color: var(--text-2); }
.cmp th.plan { border-bottom: 2px solid var(--series-1); }
/* One path table */
.pathtable { margin-top: 8px; }
.pathtable td:nth-child(3), .pathtable th:nth-child(3), .pathtable td:last-child { text-align: left; }
.pathtable tr.picked td { background: var(--surface-2); font-weight: 600; }
.pathtable tr.ruin td { color: var(--critical); }
.pathtable tfoot td { border-top: 2px solid var(--border); font-weight: 600; }
#headroomShift { margin-top: 4px; }
#viewOptions select { width: auto; max-width: 100%; }

/* Side by side: the inputs scroll on their own and stay in view, so a
   parameter at the bottom can be changed while the results stay visible. */
@media (min-width: 801px) {
  #inputs {
    position: sticky;
    top: 8px;
    max-height: calc(100vh - 16px);
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-right: 6px;
  }
}

/* Holdings: description and cost basis share the second line */
.row .noteline { grid-column: 1 / -1; display: flex; gap: 4px; }
.row .noteline .note { flex: 1; margin-top: 4px; }
.row .noteline .basis { width: 7em; margin-top: 4px; font-size: 12px; }
/* Taxes */
.rrow .otop { grid-template-columns: 1fr 2em; }
.rrow .otop .note { margin-top: 0; }
.rrow .pbottom input { width: 3.6em; }
.rrow .pbottom input.wide { width: 6em; }
.rrow .pbottom label { display: inline-flex; margin: 0; gap: 4px; }
.rrow .pbottom select { width: auto; }
#sourceTax input { width: 3.6em; }
#sourceTax td, #sourceTax th { padding: 2px 4px; }

/* Rental property line under a holding */
.row .propline { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; color: var(--text-2); }
.row .propline input { width: 4em; }
.row .propline select { width: auto; }

/* Top of the inputs: wealth today and the one-section toggle; stays in view
   while the inputs scroll. */
.formbar {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 4px 0 8px;
  margin-bottom: 8px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 12px;
}
.formbar p { margin: 0; flex-basis: 100%; }
.formbar label { display: inline-flex; gap: 6px; margin: 0; color: var(--text); }
.formbar select { flex: 1; min-width: 8em; }

.toolbar .scenarios { display: inline-flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.toolbar .scenarios input { width: 11em; }
.toolbar .scenarios select { width: auto; max-width: 14em; }

.headroom { margin-top: 12px; }
#headroomTable.stale { opacity: 0.45; transition: opacity 0.2s; }
