:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1c1c1a;
  --muted: #5f5f5a;
  --line: #d9d9d3;
  --accent: #1f5f8b;
  --accent-text: #ffffff;
  --warn-bg: #fff4d6;
  --warn-text: #5c4300;
  --err: #a3201a;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #161615;
    --surface: #202020;
    --text: #ececea;
    --muted: #a3a39d;
    --line: #3a3a37;
    --accent: #6fb1e0;
    --accent-text: #0e1a24;
    --warn-bg: #3a3014;
    --warn-text: #f1dc9a;
    --err: #ff8f87;
  }
}
:root[data-theme="dark"] {
  --bg: #161615; --surface: #202020; --text: #ececea; --muted: #a3a39d;
  --line: #3a3a37; --accent: #6fb1e0; --accent-text: #0e1a24;
  --warn-bg: #3a3014; --warn-text: #f1dc9a; --err: #ff8f87;
}

* { box-sizing: border-box; }
/* Author styles that set display (buttons, grids) would otherwise show hidden elements. */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
main { max-width: 52rem; margin: 0 auto; padding: 16px; }
h1 { font-size: 1.4rem; margin: 0.5rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 1.5rem 0 0.5rem; }
.tag {
  font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--muted); border: 1px solid var(--line); border-radius: 4px;
  padding: 1px 6px; vertical-align: middle;
}
.notice {
  background: var(--warn-bg); color: var(--warn-text);
  border-radius: 8px; padding: 10px 12px; font-size: 0.9rem; margin: 0 0 1rem;
}
.error { color: var(--err); font-size: 0.9rem; margin: 0.5rem 0; }
.note { color: var(--muted); font-size: 0.9rem; }

form {
  display: grid; gap: 14px; grid-template-columns: 1fr;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; padding: 14px;
}
.field label { display: block; font-weight: 600; margin-bottom: 4px; }
.field input, .field select {
  width: 100%; min-height: 44px; padding: 8px 10px;
  font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px;
}
.field input:focus-visible, .field select:focus-visible,
.modes input:focus-visible + span {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.field .msg { color: var(--err); font-size: 0.85rem; min-height: 1.2em; margin-top: 2px; }

.modes { border: 0; padding: 0; margin: 0 0 0.75rem; display: grid; gap: 6px; }
.modes legend { font-weight: 600; margin-bottom: 4px; padding: 0; }
.modes label { display: flex; gap: 8px; align-items: center; min-height: 36px; }

.headline {
  display: grid; grid-template-columns: 1fr; gap: 8px; margin: 0.5rem 0 1rem;
}
.headline div {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px;
}
.headline dt { color: var(--muted); font-size: 0.85rem; }
.headline dd { margin: 0; font-size: 1.25rem; font-weight: 650; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
table { border-collapse: collapse; width: 100%; font-size: 0.85rem; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 10px; text-align: right; white-space: nowrap; }
#years th:first-child, #years td:first-child, #years th:nth-child(2), #years td:nth-child(2),
#brackets th:first-child, #brackets td:first-child { text-align: left; }
thead th { position: sticky; top: 0; background: var(--surface); border-bottom: 1px solid var(--line); }
tbody tr:nth-child(even) { background: color-mix(in srgb, var(--line) 25%, transparent); }

@media (min-width: 640px) {
  main { padding: 24px; }
  form { grid-template-columns: 1fr 1fr; }
  .headline { grid-template-columns: repeat(2, 1fr); }
}
@media (min-width: 900px) {
  .headline { grid-template-columns: repeat(4, 1fr); }
}

/* Admin page and the button that opens it */
.button, button {
  display: inline-block; min-height: 44px; padding: 9px 16px;
  font: inherit; font-weight: 600; text-decoration: none; cursor: pointer;
  color: var(--accent-text); background: var(--accent);
  border: 0; border-radius: 8px;
}
.button.small { min-height: 0; padding: 3px 10px; font-size: 0.8rem; margin-left: 8px; vertical-align: middle; }
.button:focus-visible, button:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.back { margin: 0.25rem 0; font-size: 0.9rem; }
a { color: var(--accent); }
.issues { list-style: none; margin: 0 0 1rem; padding: 0; display: grid; gap: 8px; }
.issue { padding: 10px 12px; border-radius: 8px; border-left: 4px solid var(--line); background: var(--surface); }
.issue.error { border-left-color: var(--err); }
.issue.warning { border-left-color: #b8860b; background: var(--warn-bg); color: var(--warn-text); }
.issue.info { border-left-color: var(--accent); }
.issue.ok { border-left-color: #2e7d32; }
tr.chosen td { font-weight: 700; background: color-mix(in srgb, var(--accent) 18%, transparent); }
.actions { margin-top: 1rem; }
section > .field { max-width: 20rem; margin: 0.75rem 0; }

.month-year { display: grid; grid-template-columns: 3fr 2fr; gap: 8px; }
.month-year select, .month-year input {
  width: 100%; min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}

.manual { display: grid; grid-template-columns: 1fr; gap: 8px; max-width: 28rem; }
.actions button + button { margin-left: 8px; }
h3 { font-size: 1rem; margin: 1.25rem 0 0.25rem; }
#premiums td:nth-child(3) { white-space: normal; word-break: break-all; text-align: left; }
@media (min-width: 640px) { .manual { grid-template-columns: 1fr 1fr; } }

/* Accounts page */
main.wide { max-width: 64rem; }
.account-rows { display: grid; gap: 12px; }
.account-row, .account-head { display: grid; gap: 8px; align-items: center; grid-template-columns: 1fr 1fr; }
.account-row {
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px;
}
.account-head { display: none; }
.account-row input[type="text"], .account-row select {
  min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px; width: 100%;
}
.account-row select[hidden] { display: none; }
.account-row .col-desc, .account-row .col-owner, .account-row .col-basis, .account-row .col-amount,
.account-row .col-msg { grid-column: 1 / -1; }
.account-row .col-actions { grid-column: 1 / -1; justify-self: start; display: flex; gap: 8px; }
.account-row .col-msg { color: var(--err); font-size: 0.85rem; }
.account-row .col-msg:empty { display: none; }
.check { display: inline-flex; align-items: center; gap: 6px; min-height: 44px; cursor: pointer; white-space: nowrap; }
.check input { width: 22px; height: 22px; }
button.secondary { background: transparent; color: var(--accent); border: 1px solid var(--accent); }
/* The initial boxes share the end-of-plan grid, so each sits directly above the
   box with the same size: initial total over total at death, and so on. */
.headline.initial { margin-bottom: 0; }

/* One fixed set of columns, so every line and the heading above them line up. */
@media (min-width: 640px) {
  .account-row, .account-head {
    grid-template-columns: 13rem 9rem 4.5rem 7rem 7.5rem 7.5rem 6.5rem;
    justify-content: start;
  }
  .account-head {
    display: grid; padding: 0 11px 4px; font-size: 0.8rem; font-weight: 600; color: var(--muted);
  }
  .account-head span:nth-child(5), .account-head span:nth-child(6) { text-align: right; }
  .account-row .col-desc { grid-column: 1; }
  .account-row .col-tax { grid-column: 2; }
  .account-row .col-roth { grid-column: 3; }
  .account-row .col-owner { grid-column: 4; }
  .account-row .col-amount { grid-column: 5; text-align: right; }
  .account-row .col-basis { grid-column: 6; text-align: right; }
  .account-row .col-actions { grid-column: 7; }
  .account-row .col-msg { grid-column: 1 / -1; }
}
code { font-size: 0.9em; }

dialog {
  max-width: 28rem; width: calc(100% - 32px); padding: 18px;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px;
}
dialog::backdrop { background: rgb(0 0 0 / 0.45); }
dialog h2 { margin-top: 0; }
dialog .actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 0; }

/* Scenarios: the list is wider than a question box, and reads left to right. */
#open-dialog, #import-dialog { max-width: 44rem; }
#export-dialog { max-width: 32rem; }
.scn-table th, .scn-table td { text-align: left; white-space: normal; }
.scn-table .row-actions { white-space: nowrap; }
.scn-table .row-actions button + button { margin-left: 6px; }
/* On a phone each scenario becomes a small card, so its buttons are not off-screen. */
@media (max-width: 640px) {
  .scn-table thead { display: none; }
  .scn-table, .scn-table tbody, .scn-table tr, .scn-table td { display: block; }
  .scn-table tr { display: flex; flex-direction: column; padding: 8px 10px; border-bottom: 1px solid var(--line); }
  .scn-table td { padding: 1px 0; }
  .scn-table td:nth-child(2) { font-weight: 600; font-size: 1rem; order: -1; }
  .scn-table td:nth-child(1)::before { content: "Group: "; color: var(--muted); }
  .scn-table td:nth-child(3)::before { content: "Saved: "; color: var(--muted); }
  .scn-table .row-actions { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 6px; white-space: normal; }
  .scn-table .row-actions button + button { margin-left: 0; }
}

.ceiling-control { margin: 0.5rem 0 1rem; }
.ceiling-control .field { max-width: 24rem; margin-bottom: 8px; }
.ceiling-control .field select, .ceiling-control .field input {
  width: 100%; min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}

/* Under its radio option, indented to line up with the option's text. */
.modes .ceiling-control {
  margin: 0 0 6px 30px; padding-left: 12px; border-left: 2px solid var(--line);
}

/* The custom amount sits to the right of the bracket menu, and wraps below it on a narrow screen. */
.ceiling-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-start; }
.ceiling-row .field { flex: 0 1 24rem; }
.ceiling-row #ceiling-custom-field { flex: 0 1 14rem; }

/* The ages to convert between: two short boxes side by side. */
.window-control { margin: 0.25rem 0 1rem; }
.window-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.window-row .field { flex: 0 0 auto; }
.window-row label { white-space: nowrap; }
.window-row input {
  width: 7rem; min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
.window-row input.is-default { color: var(--muted); }
.window-control .msg { color: var(--err); font-size: 0.85rem; min-height: 1.2em; margin-top: 4px; }

/* Against no conversions */
#comparison h2 { margin-top: 1rem; }
#compare th:first-child, #compare td:first-child { text-align: left; }
#compare td.diff { font-weight: 650; }

/* The page's steps */
.step { margin: 0 0 1.25rem; }
.step > h2, details.step > summary { font-size: 1.1rem; margin: 1.25rem 0 0.5rem; font-weight: 650; }
details.step > summary { cursor: pointer; }
.step .field input, .step .field select {
  width: 100%; min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
.step .field { max-width: 24rem; margin-bottom: 8px; }
#form { grid-template-columns: 1fr; }
@media (min-width: 640px) { #form { grid-template-columns: 1fr 1fr; } }
input.is-default { color: var(--muted); }
.ss-block { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 12px; }
.ss-block h3 { margin: 0 0 0.5rem; font-size: 1rem; }
.ss-basis { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.ss-age { display: flex; gap: 8px; }
.ss-age .field { max-width: 6rem; }
.assumptions-grid { display: grid; gap: 4px 16px; grid-template-columns: 1fr; }
@media (min-width: 640px) { .assumptions-grid { grid-template-columns: 1fr 1fr; } }
.needs { background: var(--warn-bg); color: var(--warn-text); border-radius: 10px; padding: 4px 16px 10px; margin-bottom: 1rem; }
.needs h2 { font-size: 1rem; margin: 0.6rem 0 0.25rem; }
.needs ul { margin: 0; padding-left: 1.2rem; }
.warning {
  background: var(--warn-bg); color: var(--warn-text); border-left: 4px solid #b8860b;
  border-radius: 8px; padding: 10px 12px; margin: 0.5rem 0;
}

/* The Accounts page's household facts on one line, wrapping on a narrow screen. */
.household-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.household-row .field { flex: 1 1 12rem; max-width: 16rem; margin-bottom: 0; }

/* Heading with Clear all at its right; the round plus and minus buttons. */
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.section-head h2 { margin: 1.25rem 0 0.5rem; }
button.small { min-height: 0; padding: 4px 12px; font-size: 0.85rem; }
.icon-button {
  min-height: 44px; min-width: 44px; padding: 0; font-size: 1.4rem; line-height: 1; text-align: center;
}
.add-row { margin: 0.5rem 0 0; }
.account-row .col-actions .icon-button { width: 44px; }

/* The three return rates, side by side. */
.rates-row {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px;
  align-items: end; max-width: 44rem;
}
.rates-row .field { max-width: none; margin-bottom: 0; }
/* Only a phone is too narrow to keep all three on one row. */
@media (max-width: 480px) { .rates-row { grid-template-columns: 1fr; } }
.rates-row input {
  width: 100%; min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}

/* Export: the list of scenarios to include. */
.export-list { border: 1px solid var(--line); border-radius: 8px; margin: 0 0 12px; padding: 6px 10px; max-height: 14rem; overflow-y: auto; }
.export-list legend { font-weight: 600; padding: 0 4px; }
.check-row { display: grid; grid-template-columns: 24px 1fr; column-gap: 8px; align-items: start; min-height: 36px; padding: 6px 0; font-weight: 400; margin: 0; }
.check-row input { grid-row: 1 / span 2; margin: 3px 0 0; width: 18px; height: 18px; }
.check-row .note { grid-column: 2; margin: 0; }
.check-row .note:empty { display: none; }
.scn-table select { max-width: 100%; font: inherit; min-height: 36px; padding: 4px 6px; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 6px; }
dialog .field { margin-bottom: 10px; }
dialog .field input { width: 100%; min-height: 44px; padding: 8px 10px; font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; }
