:root {
  --bg: #f4f6fb;
  --bg-grad: radial-gradient(1200px 600px at 15% -10%, #dfe8ff 0%, transparent 60%),
             radial-gradient(900px 500px at 100% 0%, #e6dcff 0%, transparent 55%);
  --card: #ffffff;
  --card-brd: #e3e8f2;
  --ink: #101828;
  --ink-2: #475467;
  --ink-3: #8a94a6;
  --accent: #3b5bdb;
  --accent-ink: #ffffff;
  --accent-soft: #eef2ff;
  --good: #0e9f6e;
  --bad: #d64545;
  --shadow: 0 1px 2px rgba(16,24,40,.05), 0 12px 32px -12px rgba(16,24,40,.16);
  --radius: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --bg-grad: radial-gradient(1200px 600px at 15% -10%, #16244a 0%, transparent 60%),
               radial-gradient(900px 500px at 100% 0%, #241a45 0%, transparent 55%);
    --card: #141c2e;
    --card-brd: #253049;
    --ink: #eef2f9;
    --ink-2: #a9b4c8;
    --ink-3: #74809a;
    --accent: #7b93ff;
    --accent-ink: #0b1220;
    --accent-soft: #1c2748;
    --good: #34d399;
    --bad: #f87171;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 16px 40px -16px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 400 16px/1.55 ui-sans-serif, -apple-system, "Segoe UI", Roboto, Inter, system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg-grad), var(--bg);
  background-attachment: fixed;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

.wrap { width: 100%; max-width: 780px; margin: 0 auto; padding: 0 16px; }

/* ------------------------------------------------------------------ header */

.masthead { padding: 28px 0 8px; }
.brand { display: flex; gap: 14px; align-items: flex-start; }
.brand-mark {
  flex: none;
  display: grid; place-items: center;
  width: 46px; height: 46px;
  border-radius: 13px;
  background: var(--accent); color: var(--accent-ink);
  font-weight: 800; font-size: 14px; letter-spacing: .04em;
}
.masthead h1 { margin: 2px 0 2px; font-size: 22px; line-height: 1.2; letter-spacing: -.02em; }
.masthead p { margin: 0; color: var(--ink-2); font-size: 14px; }

/* ------------------------------------------------------------------- cards */

.card {
  background: var(--card);
  border: 1px solid var(--card-brd);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin: 16px 0;
}

.card-title {
  margin: 0 0 14px;
  font-size: 12px; font-weight: 700;
  letter-spacing: .09em; text-transform: uppercase;
  color: var(--ink-3);
}
.card-title:not(:first-child) { margin-top: 26px; }

/* ------------------------------------------------------------------ fields */

.field { margin-bottom: 16px; border: 0; padding: 0; min-width: 0; }
.field:last-child { margin-bottom: 0; }

label, legend {
  display: block;
  margin-bottom: 6px;
  padding: 0;
  font-size: 13px; font-weight: 600;
  color: var(--ink-2);
}
.opt { font-weight: 400; color: var(--ink-3); }
.unit-suffix { font-weight: 400; color: var(--ink-3); }

input[type=text], input[type=number], input[type=date], select {
  width: 100%;
  padding: 11px 12px;
  font: inherit;
  font-size: 16px; /* keeps iOS from zooming on focus */
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--card-brd);
  border-radius: 10px;
  min-height: 44px;
}
input:focus-visible, select:focus-visible, button:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input[type=date] { -webkit-appearance: none; appearance: none; }

.row { display: grid; grid-template-columns: 1fr; gap: 0; }
@media (min-width: 560px) {
  .row { grid-template-columns: 1fr 1fr; gap: 14px; }
  .row > .field { margin-bottom: 16px; }
}

.money { display: grid; grid-template-columns: 92px 1fr; gap: 8px; }

.hint { margin: 6px 0 0; font-size: 12px; color: var(--ink-3); }

.linkbtn {
  float: right;
  border: 0; background: none; padding: 0;
  color: var(--accent); font: inherit; font-size: 12px; font-weight: 600;
  cursor: pointer; text-decoration: underline;
}

/* -------------------------------------------------------------- segmented */

.segmented { display: flex; gap: 6px; }
.segmented label { flex: 1; margin: 0; min-width: 0; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px;
  padding: 9px 4px;
  min-height: 46px;
  border: 1px solid var(--card-brd);
  border-radius: 10px;
  font-size: 14px; font-weight: 600;
  color: var(--ink-2);
  text-align: center;
  cursor: pointer;
  transition: background .12s, border-color .12s, color .12s;
}
.segmented small { font-size: 10.5px; font-weight: 500; color: var(--ink-3); letter-spacing: .02em; }
.segmented input:checked + span {
  background: var(--accent); border-color: var(--accent); color: var(--accent-ink);
}
.segmented input:checked + span small { color: var(--accent-ink); opacity: .75; }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.segmented label[hidden] { display: none; }

/* ------------------------------------------------------------------ result */

.headline { text-align: center; padding: 24px 18px; }
.eyebrow {
  margin: 0; font-size: 12px; font-weight: 700;
  letter-spacing: .09em; text-transform: uppercase; color: var(--ink-3);
}
.bignum {
  margin: 8px 0 4px;
  font-size: clamp(36px, 11vw, 52px);
  font-weight: 750;
  letter-spacing: -.03em;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.headline .sub { margin: 0; color: var(--ink-2); font-size: 14px; }
.chip {
  display: inline-block;
  margin: 14px 0 0;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12.5px; font-weight: 600;
}
@media (prefers-color-scheme: dark) { .chip { color: var(--ink); } }

/* Centre-aligned so the fraction bar sits on the expression's optical line;
   a baseline row would hang the denominator below everything else. */
.calc {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 9px;
  padding: 16px 14px;
  border-radius: 12px;
  background: var(--accent-soft);
  font-variant-numeric: tabular-nums;
  font-size: 15px;
  line-height: 1.35;
}
.calc b { font-weight: 700; }
.calc .op { color: var(--ink-3); }
.calc .frac {
  display: inline-flex; flex-direction: column;
  text-align: center; line-height: 1.25; font-size: 12.5px;
}
.calc .frac span:first-child { border-bottom: 1.5px solid currentColor; padding: 0 6px 2px; }
.calc .frac span:last-child { padding: 2px 6px 0; }

/* ---------------------------------------------------------------- timeline */

.timeline { margin: 18px 0 4px; }
.tl-bar {
  position: relative;
  height: 34px;
  border-radius: 8px;
  background: var(--accent-soft);
  overflow: hidden;
  border: 1px solid var(--card-brd);
}
.tl-fill {
  position: absolute; top: 0; bottom: 0; right: 0;
  background: var(--accent);
  display: grid; place-items: center;
}
.tl-fill span { font-size: 11px; font-weight: 700; color: var(--accent-ink); white-space: nowrap; padding: 0 6px; }
.tl-labels {
  display: flex; justify-content: space-between; gap: 8px;
  margin-top: 6px; font-size: 11.5px; color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}

/* --------------------------------------------------------------------- kv */

.kv { margin: 18px 0 0; display: grid; grid-template-columns: 1fr auto; gap: 0; }
.kv dt, .kv dd {
  margin: 0; padding: 10px 0;
  border-top: 1px solid var(--card-brd);
  font-size: 14px;
}
.kv dt { color: var(--ink-2); padding-right: 12px; }
.kv dd { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ tables */

.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -4px; padding: 0 4px; }
table.ledger, table.rules { width: 100%; border-collapse: collapse; font-size: 14px; }
table.ledger th, table.ledger td, table.rules th, table.rules td {
  padding: 10px 8px;
  text-align: left;
  border-bottom: 1px solid var(--card-brd);
  vertical-align: top;
}
table.ledger th, table.rules th {
  font-size: 11.5px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase; color: var(--ink-3);
  white-space: nowrap;
}
table.ledger .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.ledger tfoot td { font-weight: 700; border-bottom: 0; border-top: 2px solid var(--card-brd); }
table.ledger td:first-child { min-width: 150px; }
.neg { color: var(--bad); }
.pos { color: var(--good); }
.muted-row td { color: var(--ink-3); font-style: italic; }

/* On narrow screens tables become stacked records so nothing scrolls sideways. */
@media (max-width: 559px) {
  .tablewrap { overflow: visible; margin: 0; padding: 0; }
  table.ledger, table.ledger tbody, table.ledger tfoot, table.ledger tr, table.ledger td { display: block; width: 100%; }
  table.ledger thead { display: none; }
  table.ledger tbody tr { padding: 12px 0; border-bottom: 1px solid var(--card-brd); }
  table.ledger tbody tr:last-child { border-bottom: 0; }
  table.ledger td {
    border: 0; padding: 3px 0;
    display: flex; align-items: baseline; justify-content: space-between; gap: 16px;
    text-align: right;
  }
  table.ledger td::before {
    content: attr(data-label);
    flex: none;
    color: var(--ink-3); font-weight: 500; font-size: 13px; text-align: left;
  }
  table.ledger td:first-child {
    display: block; text-align: left;
    font-weight: 650; color: var(--ink); font-style: normal;
    margin-bottom: 4px; min-width: 0;
  }
  table.ledger td:first-child::before { content: none; }
  table.ledger tfoot tr { padding-top: 12px; border-top: 2px solid var(--card-brd); }
  table.ledger tfoot td { border-top: 0; }
  table.ledger tfoot td:last-child { justify-content: flex-end; font-size: 17px; }

  table.rules, table.rules tbody, table.rules tr, table.rules td { display: block; width: 100%; }
  table.rules thead { display: none; }
  table.rules tr { padding: 10px 0; border-bottom: 1px solid var(--card-brd); }
  table.rules td { border: 0; padding: 1px 0; }
  table.rules td:first-child { display: inline; font-weight: 700; color: var(--ink); }
  table.rules td:nth-child(2) { display: inline; }
  table.rules td:nth-child(2)::before { content: ' · '; }
  table.rules td:last-child { margin-top: 4px; }
}

/* ----------------------------------------------------------------- actions */

.actions { display: flex; flex-direction: column; gap: 10px; }
.btn {
  width: 100%;
  min-height: 48px;
  padding: 13px 18px;
  border: 1px solid var(--accent);
  border-radius: 12px;
  background: var(--accent); color: var(--accent-ink);
  font: inherit; font-size: 15px; font-weight: 650;
  cursor: pointer;
  transition: filter .12s;
}
.btn:hover { filter: brightness(1.07); }
.btn:active { filter: brightness(.94); }
.btn.ghost { background: transparent; color: var(--accent); }
.actions .hint { text-align: center; min-height: 16px; margin: 0; }

/* ------------------------------------------------------------------ errors */

.errors { border-color: var(--bad); }
.errors ul { margin: 0; padding-left: 20px; color: var(--bad); font-size: 14px; }
.errors li + li { margin-top: 4px; }

/* -------------------------------------------------------------- disclosure */

.disclosure summary {
  cursor: pointer;
  font-size: 14px; font-weight: 650;
  list-style: none;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.disclosure summary::-webkit-details-marker { display: none; }
.disclosure summary::after { content: '＋'; color: var(--ink-3); font-weight: 400; }
.disclosure[open] summary::after { content: '−'; }
.disclosure[open] summary { margin-bottom: 14px; }
.prose { font-size: 14px; color: var(--ink-2); }
.prose h3 { font-size: 13px; color: var(--ink); margin: 22px 0 8px; letter-spacing: -.01em; }
.prose p { margin: 0 0 10px; }
.prose ul { margin: 0 0 10px; padding-left: 20px; }
.prose li { margin-bottom: 4px; }
.prose strong { color: var(--ink); }
.formula {
  padding: 12px; border-radius: 10px;
  background: var(--accent-soft); color: var(--ink);
  font-size: 13.5px; font-weight: 600; text-align: center;
}
.tiny { font-size: 12px; color: var(--ink-3); }

.note { margin: -4px 0 14px; font-size: 13.5px; color: var(--ink-2); }

.foot { padding: 8px 16px 32px; text-align: center; font-size: 12px; color: var(--ink-3); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
