:root,
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0f17;
  --bg-elev: #121826;
  --bg-elev-2: #171f30;
  --line: #243049;
  --line-soft: #1b2337;
  --text: #e6edf7;
  --muted: #8b9ab5;
  --accent: #4fd1a5;
  --accent-ink: #06231b;
  --accent-hover: #63e0b6;
  --accent-2: #58a6ff;
  --down: #f0616d;
  --warn: #f2b544;
  --topbar: linear-gradient(180deg, #131a29, #0d1220);
  --btn-hover: #1f2942;
  --btn-hover-border: #33456b;
  --code-bg: #0e1524;
  --code-text: #b9c6df;
  --field-bg: #0e1524;
  --tooltip-bg: #0a1120f2;
  --tooltip-border: #33456b;
  --row-hover: #1a2438;
  --shadow: 0 8px 24px #0008;
  --radius: 12px;

  /* chart palette */
  --c-plot-bg: #121826;
  --c-grid: #1f2a41;
  --c-axis: #8b9ab5;
  --c-frame: #2a3653;
  --c-line: #4fd1a5;
  --c-line-2: #58a6ff;
  --c-area-2: rgba(88, 166, 255, 0.18);
  /* The "drawn on top of the budget" band in the stacked chart. */
  --c-extra: #a78bfa;
  --c-marker: #e6edf7;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f1f5f9;
  --bg-elev: #ffffff;
  --bg-elev-2: #f8fafc;
  --line: #d8e0ea;
  --line-soft: #eef2f7;
  --text: #0f172a;
  --muted: #5b6779;
  --accent: #0f9d76;
  --accent-ink: #ffffff;
  --accent-hover: #0c8763;
  --accent-2: #1d4ed8;
  --down: #d4323f;
  --warn: #b45309;
  --topbar: linear-gradient(180deg, #ffffff, #eef2f8);
  --btn-hover: #e8eef6;
  --btn-hover-border: #b6c3d4;
  --code-bg: #eef2f7;
  --code-text: #334155;
  --field-bg: #ffffff;
  --tooltip-bg: #ffffffF2;
  --tooltip-border: #b6c3d4;
  --row-hover: #eef4fb;
  --shadow: 0 8px 24px #0f172a26;
  --radius: 12px;

  /* chart palette */
  --c-plot-bg: #ffffff;
  --c-grid: #e5eaf1;
  --c-axis: #5b6779;
  --c-frame: #cbd5e1;
  --c-line: #0f9d76;
  --c-line-2: #1d4ed8;
  --c-area-2: rgba(29, 78, 216, 0.16);
  --c-extra: #7c3aed;
  --c-marker: #0f172a;
}

* { box-sizing: border-box; }
/* Components below set `display`, which would otherwise beat the `hidden` attribute. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: clip;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Segoe UI", Roboto, -apple-system, system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  transition: background-color .2s ease, color .2s ease;
}

h1, h2, h3 { margin: 0; font-weight: 650; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px clamp(14px, 1.8vw, 32px);
  background: var(--topbar);
  border-bottom: 1px solid var(--line);
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 19px; letter-spacing: .3px; }
.logo { font-size: 26px; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg-elev-2);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover:not(:disabled) { background: var(--btn-hover); border-color: var(--btn-hover-border); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.small { padding: 6px 10px; font-size: 12px; }
.btn.icon { padding: 8px 10px; font-size: 15px; line-height: 1; }
/* Inline SVG icons are drawn in the button's own colour so they follow hover and the theme. */
.btn .ico { display: block; width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.danger { color: var(--down); }
.btn.danger:hover:not(:disabled) { border-color: var(--down); }

main { padding: 22px clamp(14px, 1.8vw, 32px); max-width: 1400px; margin: 0 auto; }
main > section:not([hidden]) ~ section { margin-top: 24px; }

.linklike { color: var(--accent-2); cursor: pointer; text-decoration: underline; background: none; border: 0; padding: 0; font: inherit; }
.linklike:hover { text-decoration: none; }
code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; color: var(--code-text); }

/* Intro */
.intro { text-align: center; }
.intro h2 { font-size: clamp(20px, 2.4vw, 28px); margin: 0 auto 10px; max-width: 900px; line-height: 1.3; }
.intro-lede { color: var(--muted); font-size: 15px; line-height: 1.6; max-width: 820px; margin: 0 auto 22px; }
.intro-list {
  list-style: none;
  margin: 0 auto 24px;
  padding: 0;
  max-width: 1100px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 10px 28px;
  text-align: left;
}
.intro-list li { display: flex; gap: 10px; font-size: 13px; line-height: 1.55; color: var(--muted); }
.intro-list strong { color: var(--text); }
.intro-icon { font-size: 16px; line-height: 1.4; flex: 0 0 auto; }
.intro-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* Notices */
.notice {
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  background: var(--bg-elev);
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: 13px;
}
.notice.error { border-left-color: var(--down); color: var(--down); }
.notice.warn { border-left-color: var(--warn); }

/* Wizard */
.wizard {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-width: 1000px;
  margin: 0 auto;
  overflow: hidden;
}

.steps {
  display: flex;
  gap: 4px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-elev-2);
  overflow-x: auto;
}
.step-pip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
}
.step-pip:hover:not(:disabled) { color: var(--text); }
.step-pip:disabled { cursor: not-allowed; opacity: .55; }
.step-pip .pip-no {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  font-size: 11px;
  font-weight: 700;
}
.step-pip.is-done .pip-no { border-color: var(--accent); color: var(--accent); }
.step-pip.is-active { color: var(--text); border-color: var(--btn-hover-border); background: var(--btn-hover); }
.step-pip.is-active .pip-no,
.step-pip.is-active.is-done .pip-no { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.wizard-body { padding: 20px clamp(16px, 2vw, 26px); }
.wizard-body h2 { font-size: 18px; margin-bottom: 4px; }
.step-lede { color: var(--muted); font-size: 13px; margin: 0 0 18px; max-width: 760px; }

.wizard-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px clamp(16px, 2vw, 26px);
  border-top: 1px solid var(--line);
  background: var(--bg-elev-2);
}
.wizard-foot .spacer { flex: 1 1 auto; }
.wizard-progress { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }

/* Forms */
.field-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px 18px;
}
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span, .field > label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .6px; }
.field > .field-hint { color: var(--muted); font-size: 11px; text-transform: none; letter-spacing: 0; }
.field > .field-error { color: var(--down); font-size: 11px; text-transform: none; letter-spacing: 0; }
/* A value the plan works out for you, laid out like an input but not editable. */
.field > .field-static {
  padding: 9px 12px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  font-weight: 650;
}

input[type="text"],
input[type="number"],
input[type="date"],
select {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--field-bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
}
input[type="number"] { font-variant-numeric: tabular-nums; }
input:focus, select:focus { outline: none; border-color: var(--accent); }
input.is-invalid { border-color: var(--down); }
input::placeholder { color: var(--muted); opacity: .8; }

.check {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-elev-2);
  cursor: pointer;
  font-size: 13px;
}
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.check.is-on { border-color: var(--accent); }

/* Withdrawal strategy picker (wizard step 5): one card per rule, the chosen one showing its rate. */
.strategy-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
}
.strategy-option {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
  min-width: 0;
}
.strategy-option.is-on { border-color: var(--accent); }
.strategy-option > input[type="radio"] { accent-color: var(--accent); width: 16px; height: 16px; margin-top: 3px; flex: none; }
.strategy-body { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.strategy-title { font-size: 14px; font-weight: 650; }
.strategy-blurb { color: var(--muted); font-size: 12px; }
.strategy-rate { display: grid; gap: 10px; margin-top: 6px; }

fieldset.group {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px 16px 16px;
  margin: 0 0 16px;
  background: var(--bg-elev-2);
  min-width: 0;
}
fieldset.group legend {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .6px;
  padding: 0 6px;
}
.group-note { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.group-note strong { color: var(--text); }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-elev); }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
.grid th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--bg-elev-2);
  color: var(--muted);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .6px;
  text-align: right;
  padding: 8px;
  border-bottom: 1px solid var(--line);
  white-space: nowrap;
}
.grid td {
  text-align: right;
  padding: 7px 8px;
  border-bottom: 1px solid var(--line-soft);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.grid th.t-left, .grid td.t-left { text-align: left; font-variant-numeric: normal; }
.grid tbody tr:hover { background: var(--row-hover); }
.grid tfoot td {
  font-weight: 700;
  border-top: 1px solid var(--line);
  border-bottom: 0;
  background: var(--bg-elev-2);
}
.grid td.actions { white-space: nowrap; display: flex; gap: 6px; justify-content: flex-end; }
.grid .num-pos { color: var(--accent); }
.grid .num-neg { color: var(--down); }
.grid tbody tr.is-retire td { background: color-mix(in srgb, var(--warn) 12%, transparent); }
.grid tbody tr.is-depleted td { color: var(--down); }
.tag {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-elev-2);
  color: var(--muted);
  font-size: 11px;
}
.tag.owner-you { border-color: var(--accent); color: var(--accent); }
.tag.owner-spouse { border-color: var(--accent-2); color: var(--accent-2); }
.tag.owner-joint { border-color: var(--warn); color: var(--warn); }
.tag.kind-income { border-color: var(--accent); color: var(--accent); }
.tag.kind-expense { border-color: var(--line); color: var(--muted); }

/* The budget grid is edited in place, so its cells hold controls rather than text. */
.grid th.t-center, .grid td.t-center { text-align: center; }
.budget-grid td { padding: 5px 8px; }
.budget-grid input[type="text"],
.budget-grid input[type="number"],
.budget-grid select { padding: 6px 8px; font-size: 13px; min-width: 96px; }
.budget-grid input[type="number"] { text-align: right; max-width: 120px; min-width: 80px; }
.budget-grid td:nth-child(5) { text-align: right; }

/* Drag to reorder */
.drag-cell { width: 30px; padding-left: 6px !important; padding-right: 0 !important; }
.drag-handle {
  display: block;
  width: 24px;
  padding: 4px 0;
  border: 0;
  border-radius: 6px;
  background: none;
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: grab;
  touch-action: none;
}
.drag-handle:hover { background: var(--btn-hover); color: var(--text); }
.drag-handle:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; color: var(--text); }
.drag-handle:active { cursor: grabbing; }
.is-dragging { opacity: 0.4; }
/* A line shows where the row will land. Table rows ignore box-shadow, so the cells carry it. */
tr.drop-before > td { box-shadow: inset 0 2px 0 var(--accent); }
tr.drop-after > td { box-shadow: inset 0 -2px 0 var(--accent); }
.acct-row.drop-before { box-shadow: inset 0 3px 0 var(--accent); }
.acct-row.drop-after { box-shadow: inset 0 -3px 0 var(--accent); }
.budget-grid td:first-child input { min-width: 160px; }
.budget-grid input[type="checkbox"] { accent-color: var(--accent); width: 16px; height: 16px; }
.budget-grid tfoot td { padding: 8px; }
.empty { color: var(--muted); font-size: 13px; padding: 16px; text-align: center; }

/* Editable account rows in the wizard */
.acct-rows { display: flex; flex-direction: column; gap: 10px; }
.acct-row {
  display: grid;
  grid-template-columns: auto 1.3fr .9fr 1.25fr 1fr .95fr .8fr .6fr auto;
  gap: 10px;
  align-items: end;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-elev-2);
}
.acct-row .field > span { font-size: 10px; }
.acct-row .row-del { align-self: end; justify-self: end; width: auto; margin-bottom: 1px; }
.acct-row .drag-cell { align-self: end; margin-bottom: 3px; }
@media (max-width: 1000px) {
  .acct-row { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .acct-row .row-del { grid-column: 1 / -1; justify-self: end; }
}

/* Results */
.results-head {
  display: flex;
  align-items: center;
  gap: 10px 16px;
  flex-wrap: wrap;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 16px;
}
.results-head h2 { font-size: 16px; padding-right: 12px; }
.stats { display: flex; align-items: baseline; flex-wrap: wrap; gap: 6px 0; flex: 1 1 auto; min-width: 0; }
.stat { display: inline-flex; flex-direction: column; gap: 2px; padding: 0 14px; border-left: 1px solid var(--line); white-space: nowrap; }
.stat .k { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .6px; }
.stat .v { font-size: 17px; font-weight: 650; font-variant-numeric: tabular-nums; }
.stat .v.up { color: var(--accent); }
.stat .v.down { color: var(--down); }

.panel {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px 16px;
  margin-bottom: 16px;
}
.panel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.panel-head h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .6px; color: var(--muted); }
.panel-head .spacer { flex: 1 1 auto; }
.panel-note { color: var(--muted); font-size: 12px; margin: 10px 0 0; }

/* A chart shape rather than a full-width strip: capped and centred, 16:9 until it gets narrow. */
.chart-wrap {
  position: relative;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  aspect-ratio: 16 / 9;
  min-height: 260px;
}
.chart-wrap canvas { width: 100%; height: 100%; display: block; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; color: var(--muted); font-size: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-elev);
  color: var(--text);
  padding: 0;
  max-width: min(720px, 94vw);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog-body { padding: 16px 18px 18px; display: flex; flex-direction: column; gap: 14px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* Contact card, in the dialog behind the ✉ in the top bar */
.contact-card { display: flex; flex-direction: column; gap: 10px; max-width: 52ch; }
.contact-card p { font-size: 13px; line-height: 1.6; color: var(--muted); }
.contact-id { display: flex; align-items: center; gap: 12px; }
.contact-avatar {
  width: 96px;
  height: 96px;
  flex: 0 0 auto;
  border-radius: 50%;
  object-fit: cover;
  background: var(--bg-elev);
  box-shadow: 0 0 0 2px var(--accent);
}
.contact-name { font-size: 16px !important; font-weight: 600; color: var(--text) !important; }
.contact-role { font-size: 12px !important; }
.contact-email { font-size: 14px !important; color: var(--text) !important; user-select: all; }
.contact-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.contact-fineprint { font-size: 11px !important; opacity: .85; }

/* Options dialog, behind the ⚙ in the top bar */
.options-card { display: flex; flex-direction: column; gap: 14px; max-width: 60ch; }
.options-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.options-label { font-size: 14px; font-weight: 600; }
.options-note { font-size: 12px; color: var(--muted); margin-top: 2px; }
.options-privacy { font-size: 12px; line-height: 1.5; color: var(--muted); }
.options-disclaimer { font-size: 12px; line-height: 1.5; color: var(--muted); }
.options-optout { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); cursor: pointer; }
.options-optout input { accent-color: var(--accent); }

.footer { text-align: center; color: var(--muted); font-size: 12px; padding: 24px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

@media (max-width: 720px) {
  .stat { padding: 0 10px; }
  .wizard-foot { flex-wrap: wrap; }
}

/* Monte Carlo success figure */
.mc-summary { margin: 0 0 14px; }
.mc-figure {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
.mc-figure > span { color: var(--muted); font-size: 13px; line-height: 1.5; }
.mc-figure > span b { color: var(--text); font-size: 14px; }
.mc-rate {
  flex: 0 0 auto;
  min-width: 104px;
  text-align: center;
  font-size: 40px;
  font-weight: 750;
  letter-spacing: -1px;
  color: var(--muted);
}
.mc-rate.is-good { color: var(--accent); }
.mc-rate.is-warn { color: var(--warn); }
.mc-rate.is-bad { color: var(--down); }
.mc-withdrawal {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}
.mc-withdrawal b { font-size: 15px; font-weight: 750; color: var(--text); }
.mc-withdrawal b.is-good { color: var(--accent); }
.mc-withdrawal b.is-warn { color: var(--warn); }
.mc-withdrawal b.is-bad { color: var(--down); }

@media (max-width: 560px) {
  .mc-figure { flex-direction: column; align-items: flex-start; gap: 8px; }
}

/* Print, which is also "Save as PDF": the results page on paper, nothing else. The chrome, the
   wizard and every control that would be dead ink are dropped, and panels are kept whole. */
@media print {
  @page { margin: 12mm; }
  html, body { background: #fff; }
  body { transition: none; }
  .topbar, .footer, #intro, #wizard, dialog { display: none !important; }
  /* Buttons only do something on screen: the pencils, the gear and the drag grips. */
  .panel-head .btn, .grip { display: none !important; }
  main { padding: 0; max-width: none; }
  .panel, .results-head, .mc-figure, .chart-wrap { break-inside: avoid; page-break-inside: avoid; }
  .panel { margin-bottom: 12px; box-shadow: none; }
  /* Scroll boxes have to become full height or printing clips them to one screenful. */
  .table-wrap, .grid-wrap { overflow: visible !important; max-height: none !important; }
  .grid th { position: static; }
  table.grid { font-size: 11px; }
  .chart-wrap { max-width: 100%; min-height: 0; }
}
