:root {
  --bg: #f5f6fa; --panel: #ffffff; --ink: #151827; --muted: #5a6072; --line: #e1e4ee;
  --accent: #3b4cca; --accent-ink: #ffffff; --accent-soft: #eceefe;
  --ok: #157f3b; --ok-soft: #e7f6ec; --warn: #9a5800; --warn-soft: #fff4e0; --bad: #b42318; --bad-soft: #fdecea;
  --bar: #c7cdf6; --gold: #b7791f;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .07);
  --radius: 12px;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #0e1018; --panel: #171a26; --ink: #e8eaf3; --muted: #9ca3b8; --line: #2a2f40;
  --accent: #9aa6ff; --accent-ink: #10132a; --accent-soft: #232848;
  --ok: #4ade80; --ok-soft: #12301d; --warn: #fbbf24; --warn-soft: #33270b; --bad: #f87171; --bad-soft: #3a1414;
  --bar: #3a4170; --gold: #f6c453;
  --shadow: none;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
.hidden { display: none !important; }
h2 { font-size: 15px; margin: 0 0 10px; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; min-width: 0; white-space: nowrap; }
.backlink { text-decoration: none; color: var(--muted); }
.backlink:hover { color: var(--ink); }
.sep { color: var(--line); }
.topactions { display: flex; align-items: center; gap: 10px; }
.freebadge { font-size: 12px; color: var(--ok); background: var(--ok-soft); padding: 3px 10px; border-radius: 99px; white-space: nowrap; }

button, input, select, textarea { font: inherit; color: inherit; }
.ghost { background: transparent; border: 1px solid var(--line); color: var(--ink); border-radius: 8px; padding: 7px 12px; cursor: pointer; }
.ghost:hover { border-color: var(--muted); }
.primary { background: var(--accent); color: var(--accent-ink); border: 1px solid var(--accent); border-radius: 8px; padding: 7px 14px; cursor: pointer; font-weight: 600; }
.primary:hover { filter: brightness(1.08); }
.small { padding: 4px 10px; font-size: 13px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--accent); text-decoration: underline; cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible, .drop:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

.wrap { max-width: 1200px; margin: 0 auto; padding: 24px 20px 40px; }
.hero h1 { font-size: clamp(26px, 4vw, 34px); line-height: 1.15; margin: 0 0 6px; letter-spacing: -.01em; }
.lede { color: var(--muted); margin: 0 0 14px; max-width: 660px; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.exlabel { font-size: 13px; color: var(--muted); margin-right: 2px; }
.chip {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  border-radius: 99px; padding: 6px 12px; font-size: 13px; cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.undobar { margin: 0 0 14px; padding: 8px 12px; border-radius: 8px; background: var(--accent-soft); font-size: 14px; }

/* token colours: six hues, tuned per theme */
:root {
  --t0: #fde2a7; --t1: #c9e7ff; --t2: #d5f5d0; --t3: #f9d1e4; --t4: #e2d7ff; --t5: #ffd8c2;
}
:root[data-theme="dark"] {
  --t0: #5a4512; --t1: #173d5c; --t2: #1f4a24; --t3: #5a1e3c; --t4: #352a63; --t5: #5c3216;
}
h3 { font-size: 13px; margin: 16px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.grid { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); gap: 16px; margin-bottom: 16px; align-items: start; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; min-width: 0; }
.panelhead { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.panelhead h2 { margin: 0; }
.headbtns { display: flex; gap: 6px; }
.muted { color: var(--muted); }
.small-note { font-weight: 400; font-size: 12px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field.inline { flex-direction: row; align-items: center; gap: 6px; }
input[type="number"], select { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 8px; min-width: 0; }
.check { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; cursor: pointer; }
.hint { color: var(--muted); font-size: 13px; margin: 8px 0 0; }
.hint.warn { color: var(--warn); }
details.sub { margin-top: 10px; border-top: 1px dashed var(--line); padding-top: 8px; }
details.sub > summary { cursor: pointer; font-size: 14px; color: var(--accent); }
.famnotes p { font-size: 13px; color: var(--muted); margin: 6px 0; }
.famnotes strong { color: var(--ink); }

.drop { position: relative; border-radius: 10px; }
.drop.over textarea { border-color: var(--accent); background: var(--accent-soft); }
#text {
  width: 100%; min-height: 300px; resize: vertical; display: block;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 12px;
  font: 13.5px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.drophint { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-weight: 600; color: var(--accent); pointer-events: none; }
.filebox { margin-top: 10px; overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ftable td.fname { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ftable .kind { font-size: 11px; color: var(--muted); margin-left: 6px; }
.x { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; padding: 2px 6px; }
.x:hover { color: var(--bad); }
.busy { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 13px; color: var(--muted); }
.spinner { width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.bigstats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.big { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; display: flex; flex-direction: column; gap: 2px; background: var(--bg); }
.big .bl { font-size: 12px; color: var(--muted); }
.big strong { font-size: 30px; line-height: 1.1; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.exact, .est, .assumed { display: inline-block; font-size: 11px; font-weight: 600; border-radius: 99px; padding: 1px 8px; align-self: flex-start; white-space: nowrap; }
.exact { color: var(--ok); background: var(--ok-soft); }
.est { color: var(--warn); background: var(--warn-soft); }
.assumed { color: var(--muted); background: var(--accent-soft); }
.minis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px 12px; margin: 12px 0 0; }
.minis div { min-width: 0; }
.minis dt { font-size: 12px; color: var(--muted); }
.minis dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.ttable .range { display: block; font-size: 11.5px; color: var(--muted); }
.ttable .fam { font-weight: 600; }
.ttable .sub2 { display: block; font-size: 11.5px; color: var(--muted); font-weight: 400; }

.vizpanel { margin-bottom: 16px; }
.vizctl { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; }
.viz {
  max-height: 460px; overflow: auto; border: 1px solid var(--line); border-radius: 10px; padding: 12px;
  background: var(--bg); white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
  font: 13.5px/1.9 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.viz .empty { margin: 0; color: var(--muted); font-family: inherit; white-space: normal; }
.tk { border-radius: 3px; padding: 1px 0; color: var(--ink); }
.tk.c0 { background: var(--t0); } .tk.c1 { background: var(--t1); } .tk.c2 { background: var(--t2); }
.tk.c3 { background: var(--t3); } .tk.c4 { background: var(--t4); } .tk.c5 { background: var(--t5); }
.tk.multi { outline: 1px dashed var(--muted); outline-offset: -1px; }
.tk sub { font-size: 9px; color: var(--muted); margin-left: 1px; vertical-align: -2px; }
.tk:hover { outline: 2px solid var(--accent); outline-offset: -1px; }
.vizfoot { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-top: 8px; font-size: 13px; }
.vizfoot .muted { flex: 1 1 200px; }

.ctlrow { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 10px; }
.ctlrow .field { flex: 1 1 140px; }
.fitbars { display: flex; flex-direction: column; gap: 8px; margin: 6px 0 10px; }
.fit { display: grid; grid-template-columns: 52px minmax(0, 1fr) 118px; gap: 10px; align-items: center; font-size: 13px; }
.fit .wl { font-weight: 600; font-variant-numeric: tabular-nums; }
.fit .track { position: relative; height: 14px; border-radius: 7px; background: var(--accent-soft); overflow: hidden; }
.fit .fill-in { position: absolute; left: 0; top: 0; bottom: 0; background: var(--accent); }
.fit .fill-out { position: absolute; top: 0; bottom: 0; background: var(--bar); }
.fit.over .fill-in { background: var(--bad); }
.fit .wv { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.fit.ok .wv { color: var(--ok); }
.fit.over .wv { color: var(--bad); }
.fit.tight .wv { color: var(--warn); }
.customwin { margin-top: 2px; }
.customwin input { width: 140px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 4px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }

.tablewrap { overflow-x: auto; max-height: 520px; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; }
.ctable thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; }
.ctable td.model { min-width: 160px; }
.ctable .mname { font-weight: 600; }
.ctable .mvend { display: block; font-size: 11.5px; color: var(--muted); }
.ctable .rate { display: block; font-size: 11px; color: var(--muted); }
.ctable td.tot { font-weight: 700; }
.ctable tr.cheapest td.tot { color: var(--ok); }

.related { margin: 4px 0 0; padding: 14px 16px; border-radius: var(--radius); background: var(--accent-soft); font-size: 14px; }
.related p { margin: 0; }
.foot { max-width: 1200px; margin: 0 auto; padding: 10px 20px 40px; color: var(--muted); font-size: 12.5px; }
.foot a { color: var(--muted); }

@media (max-width: 900px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 560px) {
  .topbar { padding: 8px 16px; }
  .freebadge { display: none; }
  .wrap { padding: 16px 16px 32px; }
  .panel { padding: 14px 12px; }
  #text { min-height: 220px; }
  .big strong { font-size: 24px; }
  .minis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fit { grid-template-columns: 44px minmax(0, 1fr) 96px; gap: 6px; font-size: 12px; }
  .viz { max-height: 360px; }
}
@media print {
  .topbar, .examples, .headbtns, .vizfoot button, .related { display: none; }
}
.stack { display: flex; flex-direction: column; gap: 16px; margin-bottom: 16px; }
.fitpanel .fitbars, .fitpanel .ctlrow { max-width: 760px; }
