:root {
  --bg: #f5f6f8;
  --card: #fff;
  --text: #1d2433;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-soft: #f0f1f3;
  --accent: #2563eb;
  --good: #047857;
  --mid: #b45309;
  --bad: #b91c1c;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

h1 { font-size: 17px; margin: 0; font-weight: 650; }
h2 { font-size: 15px; margin: 0; font-weight: 650; }
h3 { font-size: 12px; margin: 12px 12px 4px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.muted { color: var(--muted); font-weight: 400; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 12px 8px; }
code { background: #f3f4f6; padding: 0 4px; border-radius: 4px; font-size: 12.5px; }

button {
  font: inherit;
  border: 1px solid #d1d5db;
  background: #fff;
  border-radius: 7px;
  padding: 5px 10px;
  cursor: pointer;
}
button:hover { background: #f3f4f6; }
button.ghost { border-color: transparent; }
button.ghost:hover { border-color: #d1d5db; }
button.small { padding: 3px 8px; font-size: 12.5px; }
button.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}
.topbar .spacer { flex: 1; }
.preset { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.preset select { max-width: 320px; }

.layout {
  display: grid;
  grid-template-columns: 370px minmax(0, 1fr);
  gap: 16px;
  padding: 16px;
}
body.params-hidden .layout { grid-template-columns: minmax(0, 1fr); }
body.params-hidden aside { display: none; }

aside {
  position: sticky; top: 64px;
  max-height: calc(100vh - 80px);
  overflow: auto;
  padding-right: 2px;
}

main { min-width: 0; }

/* ---------- parameter sections ---------- */
details.section {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: 10px;
  padding-bottom: 6px;
}
details.section > summary {
  padding: 10px 12px;
  font-weight: 650;
  cursor: pointer;
  list-style: none;
  display: flex; justify-content: space-between;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::after { content: "▾"; color: var(--muted); }
details.section:not([open]) > summary::after { content: "▸"; }
details.section:not([open]) { padding-bottom: 0; }
details.section.disabled .list-head,
details.section.disabled #oneOffList,
details.section.disabled #recList,
details.section.disabled .list-actions,
details.section.disabled h3 { opacity: .4; pointer-events: none; }

.field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 150px;
  align-items: center;
  gap: 8px;
  padding: 3px 12px;
}
.field label { font-size: 13px; }
.inp { display: flex; align-items: center; gap: 6px; }
.inp span { font-size: 12px; color: var(--muted); white-space: nowrap; }

input, select {
  font: inherit;
  font-size: 13px;
  width: 100%;
  min-width: 0;
  padding: 5px 7px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}
input[data-fmt] { text-align: right; font-variant-numeric: tabular-nums; }
input:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--accent); }
input.invalid { border-color: var(--bad); background: #fef2f2; }
input[type="checkbox"] { width: auto; }

.seg { display: flex; margin: 0 12px 4px; border: 1px solid #d1d5db; border-radius: 8px; overflow: hidden; }
.seg label { flex: 1; cursor: pointer; }
.seg input { display: none; }
.seg span { display: block; text-align: center; padding: 6px 4px; font-size: 12.5px; }
.seg input:checked + span { background: var(--accent); color: #fff; }

.list-head, .list-row { display: grid; gap: 6px; align-items: center; padding: 2px 12px; }
.list-head { font-size: 11.5px; color: var(--muted); }
.fix-row { grid-template-columns: 18px 1fr 1fr 1.25fr 22px; }
.one-row { grid-template-columns: 1.7fr 1.1fr 40px 22px; }
.one-row input[type="checkbox"] { justify-self: center; }
.rec-item { padding: 4px 12px 6px; border-bottom: 1px dashed var(--line-soft); }
.rec-item .rec-top { display: grid; grid-template-columns: 1fr 22px; gap: 6px; }
.rec-item .rec-bottom { display: grid; grid-template-columns: 1fr 1.25fr 0.9fr; gap: 6px; margin-top: 4px; }
.rec-item .sub { font-size: 11px; color: var(--muted); }
.del { border: none; background: none; color: var(--muted); padding: 0; font-size: 16px; line-height: 1; }
.del:hover { color: var(--bad); background: none; }
.range { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list-actions { display: flex; align-items: center; gap: 10px; padding: 6px 12px 2px; }
.list-actions .hint { margin: 0; }

.fixbar { display: flex; height: 18px; margin: 6px 12px 4px; border-radius: 5px; overflow: hidden; font-size: 10.5px; }
.fixbar div { display: flex; align-items: center; justify-content: center; color: #fff; white-space: nowrap; overflow: hidden; border-right: 1px solid #fff; }

/* ---------- main ---------- */
#warnings .warn {
  background: #fffbeb; border: 1px solid #fde68a; color: #92400e;
  border-radius: var(--radius); padding: 8px 12px; margin-bottom: 10px; font-size: 13px;
}
#flaws .flaw {
  background: #fef2f2; border: 1px solid #fca5a5; border-left: 4px solid var(--bad); color: #7f1d1d;
  border-radius: var(--radius); padding: 8px 12px; margin-bottom: 10px; font-size: 13px;
}
#flaws .flaw > b:first-child { color: var(--bad); }

.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(205px, 1fr)); gap: 10px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; margin: 2px 0; }
.kpi .sub { font-size: 12px; color: var(--muted); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; margin-top: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 6px; }
.toolbar { display: flex; gap: 6px; flex-wrap: wrap; }
.toolbar button { font-size: 12.5px; padding: 3px 9px; }
.card > .hint { margin: 4px 0 0; }

.chart-main { height: 470px; }
.chart-pay { height: 250px; }

.table-wrap { overflow: auto; border: 1px solid var(--line-soft); border-radius: 8px; }
.table-wrap.tall { max-height: 620px; }
table.data { border-collapse: separate; border-spacing: 0; font-size: 12.5px; font-variant-numeric: tabular-nums; white-space: nowrap; width: 100%; }
table.data th, table.data td { padding: 4px 9px; text-align: right; border-bottom: 1px solid var(--line-soft); }
table.data th { position: sticky; top: 0; z-index: 2; background: #f9fafb; font-weight: 600; color: #374151; }
table.data th:first-child, table.data td:first-child { position: sticky; left: 0; text-align: left; background: #fff; z-index: 1; }
table.data th:first-child { z-index: 3; background: #f9fafb; }
table.data td.l { text-align: left; }
tr.year { cursor: pointer; }
tr.year:hover td { background: #f8fafc; }
tr.year td:first-child::before { content: "▸ "; color: var(--muted); }
tr.year.open td:first-child::before { content: "▾ "; }
tr.month td { color: #4b5563; background: #fbfcfd; }
tr.month td:first-child { padding-left: 24px; background: #fbfcfd; }
tr.fixend td { border-bottom: 2px solid #c7d2fe; }
.badge { display: inline-block; font-size: 10.5px; padding: 0 5px; border-radius: 4px; background: #eef2ff; color: #4338ca; margin-left: 4px; }
.p-good { color: var(--good); font-weight: 600; }
.p-mid { color: var(--mid); font-weight: 600; }
.p-bad { color: var(--bad); font-weight: 600; }

details.method > summary { cursor: pointer; list-style: none; }
details.method > summary::-webkit-details-marker { display: none; }
details.method > summary h2::after { content: " ▸"; color: var(--muted); }
details.method[open] > summary h2::after { content: " ▾"; }
details.method h3 { margin: 14px 0 4px; }
details.method p { margin: 6px 0; max-width: 980px; }
.sources { font-size: 12px; color: var(--muted); }

/* ---------- certainty and payoff bar ---------- */
.viewbar {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 12px; margin-bottom: 10px;
}
.vb-row { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 16px; }
.vb-row + .vb-row { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line-soft); }
.vb-row.off .vb-item:not(:first-child), .vb-row.off button { opacity: .5; }
.vb-item input[type="month"] { width: auto; }
.vb-item input[type="checkbox"] { accent-color: #0f766e; }

.vb-item input[type="range"].wide, .vb-row > input[type="range"].wide { width: 260px; padding: 0; }
.vb-row > input[type="range"] { accent-color: #0f766e; }

/* scenario tags and cards */
.tag { display: inline-block; min-width: 18px; text-align: center; font-size: 11px; font-weight: 700; color: #fff; border-radius: 4px; padding: 0 4px; line-height: 17px; }
.tag-a { background: #2563eb; }
.tag-b { background: #ea580c; }
.kpi.scen-a { box-shadow: inset 3px 0 0 #2563eb; }
.kpi.scen-b { box-shadow: inset 3px 0 0 #ea580c; }
.kpi.warn-card { border-color: #fecaca; background: #fef2f2; }
.badge.buy { background: #ffedd5; color: #c2410c; }
.seg-inline { margin: 0; min-width: 360px; }
.seg-inline span span { display: inline; }
.seg input:checked + span .tag { box-shadow: 0 0 0 1px #fff; }

.good { color: #15803d; font-weight: 600; }
.bad { color: var(--bad); }
.kpi.accent { border-color: #0f766e; box-shadow: inset 3px 0 0 #0f766e; }
.kpi.off .value { color: #9ca3af; font-weight: 500; }
.badge.payoff { background: #ccfbf1; color: #0f766e; }
.badge.flaw { background: #fee2e2; color: #b91c1c; }
.vb-item { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.vb-label { font-weight: 600; }
.vb-item input[type="range"] { width: 180px; padding: 0; accent-color: var(--port, #059669); }
.vb-item output { min-width: 40px; font-weight: 650; font-variant-numeric: tabular-nums; }
.vb-item select { width: auto; }
.viewbar .hint { margin: 0; }

/* ---------- legend chips ---------- */
.legend { display: flex; flex-wrap: wrap; gap: 4px 6px; margin: 2px 0 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; padding: 2px 9px; border-radius: 999px;
  border: 1px solid var(--line); background: #fff; color: var(--text);
}
.chip:hover { background: #f3f4f6; }
.chip.off { color: #9ca3af; }
.chip.off .sw { --c: #d1d5db !important; } /* beats the inline colour */
.sw { display: inline-block; width: 16px; height: 3px; border-radius: 2px; background: var(--c); }
.sw.dash { height: 0; background: none; border-top: 2px dashed var(--c); }
.sw.dot { height: 0; background: none; border-top: 2px dotted var(--c); }
.sw.area { width: 14px; height: 10px; background: var(--c); opacity: .35; }
.sw.bar { width: 10px; height: 10px; }

/* ---------- hover hints ---------- */
#tip {
  position: fixed; z-index: 100; max-width: 340px;
  background: #111827; color: #f9fafb; font-size: 12.5px; line-height: 1.45;
  padding: 8px 10px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
  pointer-events: none;
}
.kpi .label[data-tip], th[data-tip], [data-metric][data-tip] {
  cursor: help; text-decoration: underline dotted #cbd5e1; text-underline-offset: 3px;
}

/* ---------- pending recompute ---------- */
.pending {
  position: fixed; right: 18px; bottom: 18px; z-index: 60;
  display: inline-flex; align-items: center; gap: 8px;
  padding: 5px 12px 5px 7px; border-radius: 999px;
  background: rgba(255, 255, 255, .94); border: 1px solid var(--line);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .08);
  font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.pending:hover { background: #fff; color: var(--text); border-color: #d1d5db; }
.pending[hidden] { display: none; }
.pending svg { width: 18px; height: 18px; transform: rotate(-90deg); }
.pending circle { fill: none; stroke-width: 2.5; }
.pending .trk { stroke: var(--line); }
.pending .arc { stroke: var(--accent); stroke-dasharray: 100; stroke-linecap: round; }
.pending.busy svg { animation: pending-spin .9s linear infinite; }
.pending.busy .arc { stroke-dashoffset: 72; }
@keyframes pending-spin { from { transform: rotate(-90deg); } to { transform: rotate(270deg); } }
@media (prefers-reduced-motion: reduce) { .pending.busy svg { animation: none; } }

.tt-h { font-weight: 600; margin-bottom: 4px; }
.tt-r { display: flex; justify-content: space-between; gap: 16px; }
.tt-r b { font-weight: 600; font-variant-numeric: tabular-nums; }

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  aside { position: static; max-height: none; }
  .topbar { flex-wrap: wrap; }
}
