:root {
  --ground: #F4EFE6;
  --paper: #FFFDF9;
  --ink: #1C1B1A;
  --ink-2: #3F3B36;
  --muted: #5E5A54;
  --rule: #8A8378;
  --line: #D9D2C5;
  --accent: #C2410C;
  --accent-ink: #FFFFFF;
  --accent-soft: #FBE7DC;
  --good: #166534;
  --warn: #92400E;
  --bad: #B91C1C;
  --interest: #C2410C;
  --fees: #7C2D12;
  --principal: #1C1B1A;
  --display: "Fraunces", Georgia, "Times New Roman", serif;
  --mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, monospace;
  --sans: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
/* The hidden attribute must win over component display rules (grid, flex). */
[hidden] { display: none !important; }
html { color-scheme: light; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-underline-offset: 2px; }
a:hover { color: #9A3412; }
::selection { background: var(--accent-soft); }
.sr-only { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.skip { position: absolute; left: 16px; top: -60px; z-index: 100; padding: 10px 14px; background: var(--ink); color: var(--ground); font-family: var(--mono); font-weight: 600; text-decoration: none; }
.skip:focus { top: 16px; }

/* ---------- masthead ---------- */
.top { max-width: 1120px; margin: 0 auto; padding: 24px 16px 0; }
.mast { display: flex; justify-content: space-between; align-items: baseline; gap: 16px; flex-wrap: wrap; padding-bottom: 10px; border-bottom: 2px solid var(--ink); }
.word { margin: 0; font-family: var(--display); font-size: clamp(44px, 7vw, 64px); line-height: 1; font-weight: 800; letter-spacing: -0.02em; }
.brand { font-family: var(--mono); font-size: 12px; font-weight: 500; letter-spacing: 0.12em; text-transform: uppercase; color: var(--muted); text-decoration: none; }
.brand:hover { color: var(--ink); }
.tag { margin: 10px 0 0; font-family: var(--display); font-style: italic; font-size: 18px; color: var(--ink-2); max-width: 60ch; }

/* ---------- layout ---------- */
.layout { max-width: 1120px; margin: 0 auto; padding: 20px 16px 40px; display: grid; grid-template-columns: minmax(0, 1fr); gap: 20px; }
@media (min-width: 920px) {
  .layout { grid-template-columns: 400px minmax(0, 1fr); align-items: start; }
  .inputs { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; }
}
.results { display: grid; grid-template-columns: minmax(0, 1fr); gap: 16px; min-width: 0; }

.paper, .card { background: var(--paper); border: 1px solid var(--line); box-shadow: 0 6px 20px rgba(28, 27, 26, 0.06); padding: 18px; }
.kicker { margin: 0 0 12px; font-family: var(--mono); font-size: 12px; font-weight: 600; letter-spacing: 0.12em; text-transform: uppercase; color: var(--ink); }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.hint { margin: 6px 0 0; font-size: 13px; color: var(--muted); }
.hint:empty, .msg:empty { display: none; }
.msg { margin: 6px 0 0; font-size: 13px; font-weight: 600; color: var(--bad); }
.label { display: block; margin: 0 0 6px; font-family: var(--mono); font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-2); }
.field { margin-top: 16px; }
.prose { margin: 0; color: var(--ink-2); }
.line-note { margin: 10px 0 0; font-family: var(--mono); font-size: 14px; color: var(--ink); }

/* ---------- offer tabs + chips ---------- */
.offer-bar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
.offer-bar .kicker { margin: 0; }
.offer-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab { display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; font-family: var(--mono); font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink); background: transparent; border: 1px solid var(--ink); cursor: pointer; }
.tab[aria-pressed="true"] { background: var(--ink); color: var(--ground); }
.tab-remove { min-width: 36px; justify-content: center; padding: 0 10px; }

.chips { margin: 16px 0 0; padding: 0; border: 0; display: flex; flex-wrap: wrap; gap: 6px; min-width: 0; }
.chips legend { padding: 0; width: 100%; }
.chip { min-height: 36px; padding: 0 11px; font-family: var(--mono); font-size: 12px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--ink); background: transparent; border: 1px solid var(--ink); cursor: pointer; }
.chip:hover { background: var(--accent-soft); }
.chip[aria-pressed="true"] { background: var(--ink); color: var(--ground); }
.chips-main .chip { min-height: 44px; font-size: 13px; padding: 0 14px; }
.more-presets .chips { margin-top: 10px; }
.row.note { color: var(--warn); font-size: 12.5px; font-family: var(--sans); }

/* ---------- inputs ---------- */
.money { display: flex; align-items: center; gap: 8px; height: 50px; padding: 0 12px; background: var(--paper); border: 1px solid var(--ink); }
.money:focus-within, .plain:focus { outline: 3px solid var(--accent); outline-offset: 1px; }
.affix { font-family: var(--mono); font-size: 14px; color: var(--muted); white-space: nowrap; }
.affix[hidden] { display: none; }
.money input, .plain { width: 100%; min-width: 0; border: 0; background: transparent; color: var(--ink); font-family: var(--mono); font-size: 18px; font-weight: 500; outline: none; }
.plain { height: 50px; padding: 0 12px; border: 1px solid var(--ink); background: var(--paper); font-size: 16px; }

.seg { margin: 16px 0 0; padding: 0; border: 0; display: grid; gap: 6px; min-width: 0; }
.seg legend { padding: 0; }
.seg-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.seg-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.seg-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (min-width: 520px) { .inputs .seg-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.seg label { position: relative; display: block; }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.seg span { display: flex; align-items: center; justify-content: center; text-align: center; min-height: 44px; padding: 6px 8px; font-family: var(--mono); font-size: 12.5px; font-weight: 600; color: var(--ink); border: 1px solid var(--ink); background: var(--paper); cursor: pointer; line-height: 1.25; }
.seg span:hover { background: var(--accent-soft); }
.seg input:checked + span { background: var(--ink); color: var(--ground); }
.seg input:focus-visible + span { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (max-width: 380px) { .seg-3 { grid-template-columns: minmax(0, 1fr); } }

.more { margin-top: 16px; border-top: 1px dashed var(--rule); padding-top: 10px; }
.more summary { cursor: pointer; font-family: var(--mono); font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-2); min-height: 32px; display: flex; align-items: center; }

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

.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 46px; padding: 0 18px; font-family: var(--mono); font-size: 13px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ground); background: var(--ink); border: 1px solid var(--ink); cursor: pointer; }
.btn:hover { background: #000; }
.btn.ghost { color: var(--ink); background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
:is(.btn, .chip, .tab, summary, a, .table-scroll):focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- receipt ---------- */
.receipt { position: relative; background: var(--paper); border: 1px solid var(--line); box-shadow: 0 6px 20px rgba(28, 27, 26, 0.08); padding: 18px 18px 26px; font-family: var(--mono); }
.receipt-head { display: flex; justify-content: space-between; gap: 12px; font-size: 12px; letter-spacing: 0.08em; color: var(--muted); border-bottom: 1px dashed var(--rule); padding-bottom: 8px; }
.row { display: flex; justify-content: space-between; gap: 16px; font-size: 14px; padding: 5px 0; }
.row .v { text-align: right; font-variant-numeric: tabular-nums; }
.row.sep { border-top: 1px dashed var(--rule); margin-top: 6px; padding-top: 10px; }
.row.total { font-weight: 600; font-size: 15px; }
.row.total .v { font-size: 22px; }
.row.sub { color: var(--muted); font-size: 13px; }
.stamp { margin-top: 14px; padding: 12px 14px; background: var(--accent); color: var(--accent-ink); }
.stamp p { margin: 0; }
.stamp-k { font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase; }
.stamp-v { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-top: 2px !important; }
#eir { font-family: var(--display); font-size: clamp(44px, 9vw, 60px); line-height: 1; font-weight: 800; font-variant-numeric: tabular-nums; }
.stamp-q { font-size: 13px; }
.stamp-sub { margin-top: 6px !important; font-size: 13px; }
.tear { position: absolute; left: 0; right: 0; bottom: -7px; height: 14px; background: radial-gradient(circle at 7px 7px, var(--ground) 6px, transparent 6.5px) repeat-x; background-size: 16px 14px; }

.verdict { margin: 4px 0 0; font-family: var(--display); font-size: 19px; line-height: 1.4; color: var(--ink); }
.verdict:empty { display: none; }
.verdict b { color: var(--accent); }
.verdict.small { font-size: 16px; margin-top: 12px; }
.verdict.error { color: var(--bad); }

/* ---------- cost bar ---------- */
.bar { display: flex; height: 16px; border: 1px solid var(--ink); background: var(--paper); overflow: hidden; }
.bar span { display: block; height: 100%; transition: width 0.3s ease; }
.bar .k-principal { background: var(--principal); }
.bar .k-interest { background: var(--interest); }
.bar .k-fees { background: repeating-linear-gradient(135deg, var(--fees) 0 4px, #B45309 4px 8px); }
.legend { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-family: var(--mono); font-size: 13px; }
.legend li { display: inline-flex; align-items: center; gap: 6px; }
.legend i { display: inline-block; width: 12px; height: 12px; border: 1px solid var(--ink); }
.legend .k-principal i { background: var(--principal); }
.legend .k-interest i { background: var(--interest); }
.legend .k-fees i { background: var(--fees); }

/* ---------- tables ---------- */
.table-scroll { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 13px; font-variant-numeric: tabular-nums; }
.table th, .table td { padding: 8px 0 8px 12px; text-align: right; border-bottom: 1px dashed var(--line); white-space: nowrap; }
.table th:first-child, .table td:first-child { padding-left: 0; text-align: left; }
.table th { font-size: 11px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-2); border-bottom: 1px solid var(--ink); }
.table tr:last-child td { border-bottom: 0; }
.schedule-wrap-limit { max-height: 420px; overflow-y: auto; }
#schedule-wrap { max-height: 440px; overflow-y: auto; }
#schedule-toggle { margin-top: 12px; display: none; }
@media (max-width: 640px) {
  #schedule-wrap { max-height: none; }
  .schedule:not(.all) tr.mid { display: none; }
  .schedule:not(.all) tr.gap td { display: table-cell; }
  #schedule-toggle { display: inline-flex; }
}
.schedule tr.gap { display: none; }
@media (max-width: 640px) { .schedule:not(.all) tr.gap { display: table-row; } }
.schedule tr.gap td { text-align: center; color: var(--muted); }
.compare td.win { color: var(--good); font-weight: 600; }
.compare td.win::after { content: " ✓"; }
.compare th[scope="row"] { text-align: left; font-weight: 500; text-transform: none; letter-spacing: 0; font-size: 13px; color: var(--ink); border-bottom: 1px dashed var(--line); padding-left: 0; }

/* ---------- payoff + meter ---------- */
.payoff { margin-top: 14px; border-top: 1px dashed var(--rule); padding-top: 10px; font-family: var(--mono); }
.payoff .big { font-size: 22px; font-weight: 600; }
.payoff .save { color: var(--good); font-weight: 600; }
.meter { position: relative; height: 12px; margin-top: 14px; border: 1px solid var(--ink); background: linear-gradient(90deg, #DCFCE7 0 20%, #FEF3C7 20% 35%, #FEE2E2 35% 100%); }
.meter span { position: absolute; top: -5px; width: 4px; height: 20px; margin-left: -2px; background: var(--ink); left: 0; transition: left 0.3s ease; }
.band-comfortable { color: var(--good); font-weight: 600; }
.band-careful { color: var(--warn); font-weight: 600; }
.band-risky { color: var(--bad); font-weight: 600; }

/* ---------- actions, toast, footer ---------- */
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.toast { min-height: 20px; font-family: var(--mono); font-size: 13px; color: var(--good); font-weight: 600; }
.foot { max-width: 1120px; margin: 0 auto; padding: 0 16px 48px; font-size: 13px; color: var(--muted); }
.foot p { margin: 0 0 8px; max-width: 80ch; }

@media (max-width: 420px) {
  .paper, .card { padding: 16px 14px; }
  .receipt { padding: 16px 14px 24px; }
  .row { font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) { .bar span, .meter span { transition: none; } }

@media print {
  body { background: #fff; }
  .inputs, .actions, .skip, #schedule-toggle, .toast, .more { display: none !important; }
  .layout { grid-template-columns: 1fr; padding: 0; }
  .card, .receipt { box-shadow: none; break-inside: avoid; }
  #schedule-wrap { max-height: none; overflow: visible; }
  .schedule tr.mid { display: table-row !important; }
  .schedule tr.gap { display: none !important; }
  .tear { display: none; }
}
