/* DCF Studio: ledger-paper look. Warm paper, ink, one deep green, one ochre for terminal value. */

:root {
  --bg: #f1ede2;
  --rule: rgba(60, 50, 20, 0.055);
  --surface: #fffdf8;
  --surface-2: #f7f2e6;
  --field: #ffffff;
  --ink: #1b2226;
  --muted: #566058;
  --line: #ddd4bf;
  --line-strong: #a69d86;
  --accent: #0e6b5c;
  --accent-ink: #ffffff;
  --accent-soft: #d6e9e2;
  --tv: #a85d14;
  --tv-bar: #c9822f;
  --bad: #b03a26;
  --warn-bg: #fbeed3;
  --bad-bg: #f8dfd8;
  --info-bg: #e4efe9;
  --pos: #2f9b82;
  --neg: #d4583a;
  --heat-max: 52;
  --c-fcf: #0e6b5c;
  --c-pv: #86bcae;
  --focus: #0b5ed7;
  --shadow: 0 1px 0 rgba(40, 30, 10, 0.05), 0 8px 24px -16px rgba(40, 30, 10, 0.25);
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1417;
    --rule: rgba(255, 255, 255, 0.025);
    --surface: #161d21;
    --surface-2: #1c252a;
    --field: #0f1519;
    --ink: #ece7da;
    --muted: #a5aea8;
    --line: #2b373d;
    --line-strong: #566268;
    --accent: #52c5a8;
    --accent-ink: #06201a;
    --accent-soft: #1d3a34;
    --tv: #e7a95a;
    --tv-bar: #d8923d;
    --bad: #f28d76;
    --warn-bg: #3a2d14;
    --bad-bg: #40211b;
    --info-bg: #1b2f2a;
    --pos: #2f9b82;
    --neg: #c9533a;
    --heat-max: 50;
    --c-fcf: #52c5a8;
    --c-pv: #2f7566;
    --focus: #7fb2ff;
    --shadow: 0 1px 0 rgba(0, 0, 0, 0.4), 0 10px 28px -16px rgba(0, 0, 0, 0.7);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background-color: var(--bg);
  background-image: repeating-linear-gradient(to bottom, transparent 0, transparent 31px, var(--rule) 31px, var(--rule) 32px);
  color: var(--ink);
  font: 15px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
h1, h2 { font-family: var(--serif); font-weight: 600; margin: 0; letter-spacing: -0.01em; }
p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); text-underline-offset: 2px; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

.wrap { width: 100%; max-width: 1360px; margin-inline: auto; padding-inline: 16px; }
.skip { position: absolute; left: 16px; top: -48px; background: var(--ink); color: var(--bg); padding: 8px 12px; border-radius: 6px; z-index: 30; }
.skip:focus { top: 8px; }

/* ---------- header ---------- */
.top { padding-top: 18px; border-bottom: 1px solid var(--line); background: var(--bg); }
.top-inner { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 14px 24px; }
.brand { display: flex; align-items: center; gap: 12px; }
.mark { width: 40px; height: 40px; flex: none; }
.mark rect { fill: var(--accent); }
.mark path { fill: var(--accent-ink); }
h1 { font-size: 1.7rem; line-height: 1.1; }
.tag { color: var(--muted); font-style: italic; font-family: var(--serif); font-size: 0.95rem; }
.toolbar { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; }
.preset { display: grid; gap: 2px; }
.preset label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
select, button {
  min-height: 40px; border: 1px solid var(--line-strong); border-radius: 8px; background: var(--surface); padding: 6px 12px; cursor: pointer;
}
select { min-width: 210px; }
button:hover, select:hover { border-color: var(--ink); }
button:active { transform: translateY(1px); }
#btn-share, #btn-apply-wacc { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost { border-color: transparent; background: transparent; color: var(--muted); text-decoration: underline; text-underline-offset: 3px; }
.blurb { color: var(--muted); font-size: 0.9rem; padding-block: 10px 12px; min-height: 2.9em; }

/* ---------- live bar (phones and tablets) ---------- */
.livebar { position: sticky; top: 0; z-index: 20; background: var(--ink); color: var(--bg); font-size: 0.82rem; }
.livebar-inner { display: flex; justify-content: space-between; gap: 8px; padding-block: 7px; white-space: nowrap; }
.livebar strong { font-family: var(--mono); font-weight: 600; }

/* ---------- layout ---------- */
.layout { display: grid; gap: 20px; padding-block: 20px 28px; }
.results { display: grid; gap: 18px; min-width: 0; outline: none; }

.inputs { display: grid; gap: 14px; align-content: start; }
fieldset { margin: 0; padding: 14px 14px 16px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); min-width: 0; }
legend { padding: 0 8px; margin-left: -4px; font-family: var(--serif); font-weight: 600; font-size: 1.02rem; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.field { display: grid; gap: 4px; align-content: start; min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field[hidden] { display: none; }
label, .label { font-size: 0.8rem; color: var(--muted); line-height: 1.25; }
.control { position: relative; }
input[type="number"], input[type="text"] {
  width: 100%; min-height: 40px; padding: 6px 10px; border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--field); font-family: var(--mono); font-variant-numeric: tabular-nums;
}
input[type="text"] { font-family: var(--sans); }
.control input { padding-right: 2.7rem; }
.unit { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 0.8rem; pointer-events: none; font-family: var(--mono); }
input[aria-invalid="true"] { border-color: var(--bad); box-shadow: inset 0 0 0 1px var(--bad); }
.note { color: var(--muted); font-size: 0.82rem; margin-top: 10px; }
.panel > .note { margin: -2px 0 14px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; }
.seg label { display: block; color: var(--ink); font-size: 0.85rem; cursor: pointer; position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: block; text-align: center; padding: 9px 6px; min-height: 40px; }
.seg label + label { border-left: 1px solid var(--line-strong); }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.seg label:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: -3px; }
.check { display: flex; gap: 10px; align-items: flex-start; color: var(--ink); font-size: 0.88rem; cursor: pointer; }
.check input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }

/* ---------- KPI cards ---------- */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.kpi h2 { font-size: 0.78rem; font-family: var(--sans); text-transform: uppercase; letter-spacing: 0.09em; color: var(--muted); font-weight: 600; }
.big { font-family: var(--serif); font-size: 1.9rem; line-height: 1.15; margin-top: 4px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.sub { color: var(--muted); font-size: 0.82rem; margin-top: 2px; }
.kpi.hero { grid-column: 1 / -1; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.kpi.hero h2, .kpi.hero .sub { color: inherit; opacity: 0.88; }
.kpi.hero .big { font-size: 3rem; letter-spacing: -0.02em; }
#kpi-tv-card { grid-column: 1 / -1; }
#kpi-tv-card[data-level="warn"] { background: var(--warn-bg); border-color: var(--tv); }
#kpi-tv-card[data-level="bad"] { background: var(--bad-bg); border-color: var(--bad); }
#kpi-tv-card[data-level="warn"] .big { color: var(--tv); }
#kpi-tv-card[data-level="bad"] .big { color: var(--bad); }

/* ---------- panels ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.panel > h2 { font-size: 1.15rem; padding-bottom: 8px; margin-bottom: 12px; border-bottom: 3px double var(--line-strong); }
.unit-note { font-family: var(--mono); font-size: 0.72rem; color: var(--muted); font-weight: 400; margin-left: 6px; }
.banner { padding: 12px 14px; border-radius: 10px; background: var(--bad-bg); border: 1px solid var(--bad); font-size: 0.9rem; }

.stack { position: relative; display: flex; height: 38px; border-radius: 8px; overflow: visible; margin: 8px 0 32px; }
.stack > div { height: 100%; display: grid; align-items: center; padding-inline: 10px; font: 600 0.78rem var(--mono); white-space: nowrap; overflow: hidden; }
.stack .s-fcf { background: var(--c-fcf); color: var(--accent-ink); border-radius: 8px 0 0 8px; }
.stack .s-tv { justify-items: end; background: var(--tv-bar); color: #1b1408; border-radius: 0 8px 8px 0; }
.stack .s-only { border-radius: 8px; }
.stack .tick { display: block; overflow: visible; height: auto; position: absolute; top: -4px; bottom: -4px; width: 0; border-left: 2px dashed var(--ink); padding: 0; pointer-events: none; background: none; }
.stack .tick span { position: absolute; top: 100%; left: 4px; margin-top: 4px; font: 0.7rem var(--mono); color: var(--muted); white-space: nowrap; }
.legend { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 22px; font-size: 0.86rem; }
.legend li { display: flex; align-items: center; gap: 8px; }
.legend.inline { margin-bottom: 6px; font-size: 0.8rem; color: var(--muted); }
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.sw-fcf { background: var(--c-fcf); }
.sw-pv { background: var(--c-pv); }
.sw-tv { background: var(--tv-bar); }
.legend b { font-family: var(--mono); font-weight: 600; }

.facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px 16px; margin: 0; }
.facts div { display: flex; justify-content: space-between; gap: 10px; border-bottom: 1px dotted var(--line-strong); padding-block: 4px; font-size: 0.86rem; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; font-family: var(--mono); font-variant-numeric: tabular-nums; text-align: right; }
.facts .total { font-weight: 700; border-bottom: 3px double var(--line-strong); }
.notes { list-style: none; margin: 14px 0 0; padding: 0; display: grid; gap: 8px; }
.notes li { padding: 9px 12px 9px 14px; border-radius: 8px; font-size: 0.86rem; border-left: 4px solid var(--accent); background: var(--info-bg); }
.notes li.warn { border-color: var(--tv); background: var(--warn-bg); }
.notes li.bad { border-color: var(--bad); background: var(--bad-bg); }
.notes b { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.08em; margin-right: 6px; }

/* ---------- tables ---------- */
.scroll { overflow-x: auto; border-radius: 6px; }
table { border-collapse: collapse; width: 100%; font-family: var(--mono); font-size: 0.8rem; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 10px; text-align: right; white-space: nowrap; }
.fcf th:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); z-index: 1; font-family: var(--sans); font-weight: 500; min-width: 150px; box-shadow: 1px 0 0 var(--line); }
.fcf thead th { color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--line-strong); }
.fcf tbody tr { border-bottom: 1px solid var(--line); }
.fcf tr.sub-row td, .fcf tr.sub-row th { color: var(--muted); font-size: 0.74rem; }
.fcf tr.total th, .fcf tr.total td { font-weight: 700; border-top: 1px solid var(--ink); border-bottom: 3px double var(--ink); background: var(--surface-2); }
.fcf tr.total th { background: var(--surface-2); }
.fcf td.neg { color: var(--bad); }
.sens { width: auto; min-width: 100%; }
.sens th { text-align: center; font-weight: 600; color: var(--muted); font-size: 0.75rem; }
.sens thead th, .sens tbody th { background: var(--surface-2); }
.sens td { text-align: center; min-width: 62px; color: var(--ink); border: 1px solid var(--surface); }
.sens td.hot { background: color-mix(in srgb, var(--pos) calc(var(--t) * var(--heat-max) * 1%), var(--surface)); }
.sens td.cold { background: color-mix(in srgb, var(--neg) calc(var(--t) * var(--heat-max) * 1%), var(--surface)); }
.sens td.base { outline: 2px solid var(--ink); outline-offset: -2px; font-weight: 700; }
.sens td.na { color: var(--muted); background: repeating-linear-gradient(45deg, transparent 0 5px, var(--line) 5px 6px); }
.sens tbody th { text-align: center; }
.sens .corner { font-family: var(--sans); font-size: 0.7rem; text-align: left; line-height: 1.2; }

/* ---------- charts ---------- */
.charts { display: grid; gap: 18px; }
.chart { width: 100%; min-height: 200px; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { font: 11px var(--mono); fill: var(--muted); }
.chart .row-label { fill: var(--ink); font: 12px var(--sans); }
.chart .val { fill: var(--ink); font-weight: 600; }
.chart .gl { stroke: var(--line); stroke-width: 1; }
.chart .zero { stroke: var(--ink); stroke-width: 1.2; }
.chart .b-fcf { fill: var(--c-fcf); }
.chart .b-pv { fill: var(--c-pv); }
.chart .w-add { fill: var(--c-fcf); }
.chart .w-tv { fill: var(--tv-bar); }
.chart .w-sub { fill: var(--bad); }
.chart .w-total { fill: var(--ink); }
.chart .link { stroke: var(--line-strong); stroke-dasharray: 3 3; }

/* ---------- WACC build ---------- */
.wb { display: grid; gap: 16px; }
.wb-out { display: grid; gap: 12px; align-content: start; }
.wb-out .facts { grid-template-columns: 1fr; }
.wb-out button { justify-self: start; }

.foot { padding-block: 8px 40px; color: var(--muted); font-size: 0.84rem; display: grid; gap: 4px; border-top: 1px solid var(--line); padding-top: 18px; }
.noscript { padding: 20px 16px; }
.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 0.88rem; transition: opacity 0.2s, transform 0.2s; z-index: 40; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.results[data-state="error"] .kpi .big { color: var(--muted); }
.results[data-state="error"] .kpi.hero .big { color: inherit; opacity: 0.6; }

@media (min-width: 640px) {
  .wrap { padding-inline: 24px; }
  .charts { grid-template-columns: 1fr 1fr; }
  .wb { grid-template-columns: 1.6fr 1fr; }
}

@media (min-width: 1000px) {
  .livebar { display: none; }
  .layout { grid-template-columns: 340px minmax(0, 1fr); gap: 24px; align-items: start; }
  .inputs { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; padding-right: 4px; scrollbar-width: thin; }
  .kpis { grid-template-columns: 1.25fr 1fr 1fr; }
  .kpi.hero { grid-column: auto; grid-row: span 2; display: flex; flex-direction: column; justify-content: center; }
  #kpi-tv-card { grid-column: span 2; }
}

@media (max-width: 480px) {
  h1 { font-size: 1.45rem; }
  .toolbar { width: 100%; }
  .preset { flex: 1 1 100%; }
  select { width: 100%; }
  .toolbar button { flex: 1 1 auto; min-height: 44px; }
  .kpi.hero .big { font-size: 2.6rem; }
  .facts { grid-template-columns: 1fr; }
  .panel { padding: 14px 12px; }
  .sens th, .sens td { padding: 7px 3px; }
  .sens td { min-width: 0; }
}

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

@media print {
  .livebar, .toolbar, .skip, .toast { display: none; }
  body { background: #fff; }
}
