:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-raised: #ffffff;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.1);
  --series-1: #2a78d6; /* portfolio balance */
  --series-2: #eb6834; /* cumulative contributions */
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --warning-text: #7a5200;
  --critical: #d03b3b;
  --input-bg: #ffffff;
  --input-border: #d8d7d0;
  --focus-ring: #2a78d6;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-raised: #212120;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.1);
    --series-1: #3987e5;
    --series-2: #d95926;
    --good: #0ca30c;
    --good-text: #0ca30c;
    --warning: #fab219;
    --warning-text: #fab219;
    --critical: #e66767;
    --input-bg: #262624;
    --input-border: #3a3a37;
    --focus-ring: #3987e5;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-raised: #212120;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --border: rgba(255, 255, 255, 0.1);
  --series-1: #3987e5;
  --series-2: #d95926;
  --good: #0ca30c;
  --good-text: #0ca30c;
  --warning: #fab219;
  --warning-text: #fab219;
  --critical: #e66767;
  --input-bg: #26262d;
  --input-border: #3a3a37;
  --focus-ring: #3987e5;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--text-primary);
  line-height: 1.4;
}

.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 24px 20px 64px;
}

header.app-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

header.app-header h1 {
  font-size: 1.5rem;
  margin: 0 0 4px;
}

header.app-header p {
  margin: 0;
  color: var(--text-secondary);
  max-width: 60ch;
  font-size: 0.9rem;
}

.header-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

button {
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--input-border);
  background: var(--surface-raised);
  color: var(--text-primary);
  padding: 8px 12px;
}

button:hover {
  border-color: var(--focus-ring);
}

button.primary {
  background: var(--series-1);
  border-color: var(--series-1);
  color: #fff;
}

.layout {
  display: grid;
  /* minmax(0, …) lets the results column shrink below its widest table, so the
     table scrolls inside .table-scroll instead of widening the page. */
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}

@media (max-width: 860px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
  }
}

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

.card + .card {
  margin-top: 16px;
}

.card h2 {
  font-size: 0.95rem;
  margin: 0 0 12px;
  color: var(--text-primary);
}

.card h3.section-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin: 18px 0 8px;
}

.card h3.section-label:first-of-type {
  margin-top: 0;
}

.field {
  margin-bottom: 10px;
}

.field label {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.field label .hint {
  color: var(--text-muted);
  font-weight: 400;
}

.field .input-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.field .input-row .prefix,
.field .input-row .suffix {
  color: var(--text-muted);
  font-size: 0.85rem;
}

input[type="number"],
input[type="text"] {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.9rem;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

input[type="number"]:focus,
input[type="text"]:focus {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--series-1);
}

.assumption-note {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin: -4px 0 12px;
}

/* Stat tiles */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
}

.stat-tile .label {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.stat-tile .value {
  font-size: 1.5rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.stat-tile .sub {
  font-size: 0.78rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

.status-badge.on-track {
  background: color-mix(in srgb, var(--good) 16%, transparent);
  color: var(--good-text);
}

.status-badge.close {
  background: color-mix(in srgb, var(--warning) 20%, transparent);
  color: var(--warning-text);
}

.status-badge.behind {
  background: color-mix(in srgb, var(--critical) 16%, transparent);
  color: var(--critical);
}

.status-badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}

/* Chart */
.chart-card .chart-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 4px;
}

.legend {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 8px;
}

.legend .item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend .key {
  width: 16px;
  height: 2px;
  border-radius: 2px;
  display: inline-block;
}

.legend .key.dashed {
  background: none;
  border-top: 2px dashed var(--series-2);
  height: 0;
}

.chart-container {
  position: relative;
}

svg.chart {
  width: 100%;
  height: 340px;
  display: block;
  overflow: hidden;
}

svg.chart rect[tabindex]:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: -2px;
}

.chart-tooltip {
  position: absolute;
  pointer-events: none;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.78rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
  transform: translate(-50%, -110%);
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.08s ease;
  z-index: 5;
}

.chart-tooltip.visible {
  opacity: 1;
}

.chart-tooltip .row {
  display: flex;
  justify-content: space-between;
  gap: 14px;
}

.chart-tooltip .row .val {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.chart-tooltip .row .key-line {
  display: inline-block;
  width: 10px;
  height: 2px;
  margin-right: 5px;
  vertical-align: middle;
}

.chart-tooltip .title {
  font-weight: 600;
  margin-bottom: 4px;
  color: var(--text-primary);
}

.table-toggle {
  margin-top: 10px;
  font-size: 0.82rem;
  color: var(--series-1);
  background: none;
  border: none;
  padding: 0;
  text-decoration: underline;
}

table.yearly {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin-top: 12px;
}

table.yearly th,
table.yearly td {
  text-align: right;
  padding: 6px 8px;
  border-bottom: 1px solid var(--gridline);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table.yearly th:first-child,
table.yearly td:first-child {
  text-align: left;
}

table.yearly thead th {
  color: var(--text-muted);
  font-weight: 500;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.table-scroll {
  overflow-x: auto;
}

.narrative {
  font-size: 0.88rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

footer.app-footer {
  margin-top: 28px;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

footer.app-footer a {
  color: var(--text-muted);
}

/* --- Derived/auto values, advanced disclosure, and warnings ------------- */

.legend .key.target {
  background: none;
  border-top: 1.5px dashed var(--text-muted);
  height: 0;
}

.legend .key.goal {
  background: var(--good);
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.field .derived {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 3px;
}

.field.is-auto input[type="number"] {
  opacity: 0.6;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.toggle-row input[type="checkbox"] {
  accent-color: var(--series-1);
  width: 15px;
  height: 15px;
  flex: none;
}

select {
  width: 100%;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.9rem;
  color: var(--text-primary);
  font-family: inherit;
}

select:focus {
  outline: 2px solid var(--focus-ring);
  outline-offset: 1px;
}

details.advanced {
  margin-top: 14px;
  border-top: 1px solid var(--gridline);
  padding-top: 12px;
}

details.advanced > summary {
  cursor: pointer;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  list-style: revert;
}

details.advanced > summary:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
  border-radius: 4px;
}

details.advanced[open] > summary {
  margin-bottom: 10px;
}

/* Flags: things the projection wants the reader to know about */
.flags {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.flag {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  font-size: 0.8rem;
  line-height: 1.45;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-raised);
  color: var(--text-secondary);
}

.flag .icon {
  flex: none;
  font-weight: 700;
  font-size: 0.85rem;
  line-height: 1.35;
}

.flag.warn .icon {
  color: var(--warning-text);
}
.flag.crit .icon {
  color: var(--critical);
}
.flag.ok .icon {
  color: var(--good-text);
}
.flag.info .icon {
  color: var(--text-muted);
}

.flag strong {
  color: var(--text-primary);
  font-weight: 600;
}

/* Spending breakdown inside a stat tile */
.breakdown {
  margin-top: 8px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  display: grid;
  gap: 2px;
}

.breakdown .brow {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-variant-numeric: tabular-nums;
}

.breakdown .brow.total {
  border-top: 1px solid var(--gridline);
  margin-top: 3px;
  padding-top: 3px;
  color: var(--text-primary);
  font-weight: 600;
}

.privacy-note {
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 6px;
}

@media (prefers-reduced-motion: reduce) {
  .chart-tooltip {
    transition: none;
  }
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Stress-test table */
.stress-table td:first-child {
  white-space: normal;
  min-width: 190px;
}

.stress-table .scenario-note {
  font-weight: 400;
  color: var(--text-muted);
  font-size: 0.74rem;
  margin-top: 2px;
  white-space: normal;
}

.stress-table td.delta {
  font-weight: 600;
}

.stress-table td.delta.warn {
  color: var(--warning-text);
}

.stress-table td.delta.crit {
  color: var(--critical);
}

.stress-card .narrative {
  margin-bottom: 12px;
}

/* --- Print report -------------------------------------------------------
   Hidden on screen; the only thing that prints. Everything here is authored
   against print, so the app chrome never reaches paper.                     */

#print-report { display: none; }

@media print {
  @page { size: A4; margin: 15mm 13mm; }

  body > .wrap > *:not(#print-report) { display: none !important; }
  body { background: #fff; color: #1a1a1a; font-size: 10pt; }
  .wrap { max-width: none; padding: 0; }

  #print-report { display: block; }

  #print-report h1 { font-size: 22pt; margin: 0 0 2mm; letter-spacing: -0.4pt; }
  #print-report h2 {
    font-size: 13pt; margin: 8mm 0 2.5mm; padding-bottom: 1.2mm;
    border-bottom: 1.5pt solid #1F3864; color: #1F3864; break-after: avoid;
  }
  .rep-sub { color: #666; font-size: 9pt; margin: 0 0 4mm; }
  .rep-fine { font-size: 8pt; color: #666; }

  .rep-hero {
    background: #1F3864 !important; color: #fff !important;
    padding: 6mm; border-radius: 2mm; margin: 3mm 0 5mm;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .rep-hero .cap { font-size: 9pt; opacity: .8; text-transform: uppercase; letter-spacing: .6pt; }
  .rep-hero .big { font-size: 30pt; font-weight: 700; line-height: 1; margin: 1.5mm 0; }
  .rep-hero .note { font-size: 10pt; opacity: .9; }

  .rep-cards { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 3mm; margin: 3mm 0; }
  .rep-card { border: 0.6pt solid #ccc; border-radius: 1.5mm; padding: 3mm; }
  .rep-card .lab { font-size: 7.5pt; color: #666; text-transform: uppercase; letter-spacing: .4pt; }
  .rep-card .val { font-size: 14pt; font-weight: 700; margin-top: 1mm; }
  .rep-card .note { font-size: 7.5pt; color: #666; margin-top: 1mm; }

  .rep-two { display: grid; grid-template-columns: 1fr 1fr; gap: 5mm; }
  .rep-kv { width: 100%; font-size: 8.5pt; border-collapse: collapse; }
  .rep-kv td { border-bottom: 0.4pt solid #eee; padding: 1mm 0; }
  .rep-kv td:first-child { color: #555; }
  .rep-kv td:last-child { text-align: right; font-weight: 600; }

  .rep-table { width: 100%; border-collapse: collapse; font-size: 8.2pt; margin: 2mm 0 3mm; }
  .rep-table th {
    background: #1F3864 !important; color: #fff !important; padding: 1.6mm 1.2mm;
    text-align: left; font-weight: 600;
    -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  .rep-table td { padding: 1.3mm 1.2mm; border-bottom: 0.4pt solid #ddd; vertical-align: top; }
  .rep-num th, .rep-num td { text-align: right; font-variant-numeric: tabular-nums; }
  .rep-num th:nth-child(-n+3), .rep-num td:nth-child(-n+3) { text-align: left; }
  .rep-num td.b { font-weight: 600; }
  .rep-table tr.hit { background: #E8F4E8 !important; font-weight: 700;
    -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .rep-table tr.hit td { border-top: 1pt solid #0C7C0C; border-bottom: 1pt solid #0C7C0C; }
  .rep-table tr { break-inside: avoid; }

  .rep-flag { border-left: 2.5pt solid #999; padding: 2mm 0 2mm 3mm; margin: 2.5mm 0;
              font-size: 9pt; break-inside: avoid; }
  .rep-flag.good { border-color: #0C7C0C; }
  .rep-flag.warn { border-color: #C88A00; }
  .rep-flag.crit { border-color: #C0392B; }
  .rep-flag.info { border-color: #999; }
  .rep-flag b { display: block; margin-bottom: 0.8mm; }

  #print-report ul { margin: 1.5mm 0 3mm; padding-left: 5mm; }
  #print-report li { margin-bottom: 1.2mm; }
  .rep-pb { break-before: page; }
  .rep-foot { margin-top: 6mm; padding-top: 2mm; border-top: 0.6pt solid #ccc;
              font-size: 7.5pt; color: #777; }
}

/* --- Scoreboard: the answer, pinned ----------------------------------- */
.scoreboard {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 4px 16px;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 2px rgba(11, 11, 11, 0.05), 0 1px 3px rgba(11, 11, 11, 0.04);
  scroll-margin-top: 16px;
}
@media (min-width: 861px) {
  .scoreboard { position: sticky; top: 8px; z-index: 5; box-shadow: 0 -10px 0 0 var(--page), 0 2px 10px rgba(11, 11, 11, 0.08); }
}
@media (max-width: 640px) {
  .scoreboard { grid-template-columns: 1fr 1fr; gap: 10px 16px; }
}
.sb-k { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.03em; }
.sb-v { font-size: 1.15rem; font-weight: 700; color: var(--text-primary); margin-top: 2px; }
.sb-v span:not(.status-badge):not(.dot) { font-size: 0.8rem; font-weight: 500; color: var(--text-muted); }
.sb-v .status-badge { font-size: 0.85rem; }

/* --- Phone summary bar --------------------------------------------------- */
.mbar {
  position: fixed; left: 10px; right: 10px; bottom: 10px; z-index: 40;
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px;
  padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface-raised); color: var(--text-primary);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18); font: inherit; font-size: 0.9rem; text-align: left; cursor: pointer;
}
.mbar[hidden] { display: none; }
.mb-main { font-weight: 700; }
.mb-sub { color: var(--text-secondary); }
.mb-go { margin-left: auto; font-weight: 600; color: var(--series-1); }
body.has-mbar { padding-bottom: 84px; }
@media print { .mbar, .scoreboard { display: none !important; } }
