/* ===== Tokens ===== */
:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-2: #f0f3f7;
  --text: #16202c;
  --muted: #5a6777;
  --border: #dde3ea;
  --accent: #007d9c;
  --accent-soft: #e1f4f9;
  --accent-text: #00647d;
  --ok: #17804a;
  --ok-soft: #e3f5eb;
  --warn: #9a5b00;
  --warn-soft: #fdf2df;
  --bad: #b4233c;
  --bad-soft: #fde8ec;
  --code-bg: #0f1722;
  --code-text: #e6edf3;
  --code-muted: #8b98a8;
  --tok-kw: #ff9e7a;
  --tok-type: #7fd4f5;
  --tok-str: #a5d98b;
  --tok-num: #f5c97f;
  --tok-com: #7d8a9b;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1218; --surface: #141b24; --surface-2: #1a232f; --text: #e4e9ef;
    --muted: #97a3b2; --border: #273241; --accent: #3cc3e6; --accent-soft: #0f2e3a;
    --accent-text: #7fd8f0; --ok: #4cc98a; --ok-soft: #10301f; --warn: #f0b552;
    --warn-soft: #33260f; --bad: #f2708a; --bad-soft: #3a1620; --code-bg: #0a0f15;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1218; --surface: #141b24; --surface-2: #1a232f; --text: #e4e9ef;
  --muted: #97a3b2; --border: #273241; --accent: #3cc3e6; --accent-soft: #0f2e3a;
  --accent-text: #7fd8f0; --ok: #4cc98a; --ok-soft: #10301f; --warn: #f0b552;
  --warn-soft: #33260f; --bad: #f2708a; --bad-soft: #3a1620; --code-bg: #0a0f15;
  --shadow: none; color-scheme: dark;
}

/* ===== Base ===== */
* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: 72px; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 17px/1.65 var(--font); -webkit-font-smoothing: antialiased;
}
a { color: var(--accent-text); text-underline-offset: 2px; }
a:hover { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; }
h2 { font-size: 1.75rem; margin: 0 0 .35rem; }
h3 { font-size: 1.2rem; margin: 2rem 0 .6rem; }
p { margin: 0 0 1rem; }
code { font-family: var(--mono); font-size: .88em; background: var(--surface-2); padding: .1em .35em; border-radius: 4px; }
img { max-width: 100%; height: auto; display: block; }
table { border-collapse: collapse; width: 100%; font-size: .92rem; }
th, td { text-align: left; padding: .55rem .7rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ===== Top bar ===== */
.topbar {
  position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
}
.topbar-inner { max-width: 1240px; margin: 0 auto; padding: .6rem 1rem; display: flex; align-items: center; gap: .75rem; }
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; display: flex; align-items: center; gap: .5rem; }
.brand-mark { width: 28px; height: 28px; border-radius: 7px; background: var(--accent); color: #fff; display: grid; place-items: center; font: 700 .8rem var(--mono); }
.team-links { display: flex; gap: .35rem; margin-left: auto; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.team-links a, .team-links span {
  font-size: .82rem; padding: .3rem .65rem; border-radius: 999px; border: 1px solid var(--border);
  text-decoration: none; color: var(--text); white-space: nowrap;
}
.team-links a:hover { border-color: var(--accent); }
.team-links .here { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.team-links .pending { color: var(--muted); border-style: dashed; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 8px;
  width: 36px; height: 36px; display: grid; place-items: center; cursor: pointer; flex: none;
}
.icon-btn:hover { border-color: var(--accent); }

/* ===== Hero ===== */
.hero { max-width: 1240px; margin: 0 auto; padding: 3.5rem 1rem 2rem; }
.eyebrow { font-size: .78rem; text-transform: uppercase; letter-spacing: .08em; color: var(--accent-text); font-weight: 700; margin-bottom: .6rem; }
.hero h1 { font-size: clamp(2rem, 4.5vw, 3.1rem); margin: 0 0 .8rem; max-width: 18ch; }
.hero .lede { font-size: 1.15rem; color: var(--muted); max-width: 62ch; }
.hero-actions { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: 1.4rem; }
.btn {
  display: inline-flex; align-items: center; gap: .45rem; padding: .6rem 1rem; border-radius: 8px;
  font: 600 .92rem var(--font); text-decoration: none; cursor: pointer; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.btn:hover { border-color: var(--accent); color: var(--text); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); color: #fff; }
.btn[aria-disabled="true"] { opacity: .55; pointer-events: none; }
.thesis {
  margin-top: 2rem; padding: 1.1rem 1.3rem; border-left: 4px solid var(--accent); background: var(--surface);
  border-radius: 0 var(--radius) var(--radius) 0; box-shadow: var(--shadow); max-width: 820px;
}
.thesis strong { display: block; font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; color: var(--accent-text); margin-bottom: .25rem; }

/* ===== Layout ===== */
.layout { max-width: 1240px; margin: 0 auto; padding: 0 1rem 4rem; display: grid; grid-template-columns: 1fr; gap: 2.5rem; }
@media (min-width: 1080px) { .layout { grid-template-columns: 230px minmax(0, 1fr); } }
.toc { display: none; }
@media (min-width: 1080px) {
  .toc { display: block; position: sticky; top: 76px; align-self: start; max-height: calc(100vh - 96px); overflow-y: auto; font-size: .88rem; }
}
.toc ol { list-style: none; padding: 0; margin: 0; counter-reset: toc; }
.toc li a {
  display: block; padding: .32rem .7rem; color: var(--muted); text-decoration: none; border-left: 2px solid var(--border);
}
.toc li a:hover { color: var(--text); }
.toc li a.active { color: var(--accent-text); border-left-color: var(--accent); font-weight: 600; }
.toc-title { font-size: .72rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin: 0 0 .6rem .7rem; }
.mobile-toc { margin-bottom: 1rem; }
@media (min-width: 1080px) { .mobile-toc { display: none; } }
.mobile-toc summary { cursor: pointer; font-weight: 600; padding: .7rem 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.mobile-toc ol { margin: .5rem 0 0; padding: .5rem 1rem .5rem 2rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }

main { min-width: 0; }
section.chapter { padding: 2.5rem 0; border-top: 1px solid var(--border); max-width: 860px; }
section.chapter:first-of-type { border-top: 0; padding-top: 0; }
.sec-label { font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; color: var(--accent-text); font-weight: 700; }
.sec-intro { color: var(--muted); font-size: 1.05rem; margin-bottom: 1.5rem; }

/* ===== Cards & grids ===== */
.grid { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); margin: 1rem 0 1.5rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.1rem; box-shadow: var(--shadow); }
.card h4 { margin: 0 0 .35rem; font-size: 1rem; }
.card p { margin: 0; font-size: .93rem; color: var(--muted); }
.card .num { font: 700 .8rem var(--mono); color: var(--accent-text); }
.chips { display: flex; flex-wrap: wrap; gap: .5rem; margin: 1rem 0; }
.chip { padding: .35rem .8rem; border-radius: 999px; background: var(--accent-soft); color: var(--accent-text); font-size: .88rem; font-weight: 600; }
ul.tight { padding-left: 1.2rem; margin: 0 0 1rem; }
ul.tight li { margin: .3rem 0; }

.callout { border-radius: var(--radius); padding: .9rem 1.1rem; margin: 1.2rem 0; font-size: .95rem; border: 1px solid; }
.callout.info { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.callout.warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.callout.bad { background: var(--bad-soft); border-color: color-mix(in srgb, var(--bad) 35%, transparent); }
.callout.ok { background: var(--ok-soft); border-color: color-mix(in srgb, var(--ok) 35%, transparent); }
.callout strong:first-child { display: block; margin-bottom: .2rem; }

.formula {
  font: 1.05rem/1.5 var(--mono); text-align: center; padding: 1rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); margin: 1rem 0; overflow-x: auto;
}
.formula small { display: block; font: .82rem var(--font); color: var(--muted); margin-top: .35rem; }

/* ===== Figures ===== */
figure { margin: 1.5rem 0; }
figure .frame { background: #f8fafc; border: 1px solid var(--border); border-radius: var(--radius); padding: .5rem; overflow: hidden; }
figcaption { font-size: .86rem; color: var(--muted); margin-top: .5rem; }
figcaption b { color: var(--text); }
figure button.zoom { all: unset; cursor: zoom-in; display: block; }
.lightbox { position: fixed; inset: 0; background: rgba(5, 10, 15, .85); display: none; place-items: center; z-index: 50; padding: 1rem; cursor: zoom-out; }
.lightbox.open { display: grid; }
.lightbox img { max-height: 92vh; max-width: 96vw; background: #f8fafc; border-radius: 8px; }

/* ===== Code ===== */
.code {
  background: var(--code-bg); color: var(--code-text); border-radius: var(--radius); margin: 1rem 0;
  overflow: hidden; border: 1px solid #1f2a37;
}
.code-head {
  display: flex; align-items: center; gap: .5rem; padding: .45rem .6rem .45rem .9rem; border-bottom: 1px solid #1f2a37;
  font: .78rem var(--mono); color: var(--code-muted);
}
.code-head .fname { margin-right: auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.code-head button, .code-head a {
  font: 600 .74rem var(--font); color: var(--code-text); background: #1c2735; border: 1px solid #2b394a;
  padding: .25rem .6rem; border-radius: 6px; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.code-head button:hover, .code-head a:hover { border-color: var(--accent); color: #fff; }
.code pre { margin: 0; padding: .9rem 1rem; overflow-x: auto; font: .86rem/1.6 var(--mono); tab-size: 4; }
.code pre code { background: none; padding: 0; font-size: inherit; color: inherit; }
.tok-kw { color: var(--tok-kw); }
.tok-type { color: var(--tok-type); }
.tok-str { color: var(--tok-str); }
.tok-num { color: var(--tok-num); }
.tok-com { color: var(--tok-com); font-style: italic; }

.io { display: grid; grid-template-columns: 1fr; border-top: 1px solid #1f2a37; }
.io > div { padding: .7rem 1rem; }
.io .label { font: 700 .68rem var(--font); letter-spacing: .08em; text-transform: uppercase; color: var(--code-muted); margin-bottom: .25rem; display: flex; gap: .5rem; align-items: center; }
.io pre { padding: 0; font-size: .84rem; white-space: pre-wrap; }
.io .out pre { color: #b7f0c8; }
.io .out.err pre { color: #ffb3c0; }
.tag { font: 700 .62rem var(--font); padding: .08rem .4rem; border-radius: 4px; letter-spacing: .04em; }
.tag.verified { background: #12391f; color: #8be3aa; }
.tag.sample { background: #3a2a0c; color: #f3c46a; }

/* Tabs */
.tabs { margin: 1rem 0; }
.tablist { display: flex; gap: .25rem; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.tablist button {
  font: 600 .88rem var(--font); background: none; border: 0; border-bottom: 2px solid transparent; color: var(--muted);
  padding: .55rem .9rem; cursor: pointer; margin-bottom: -1px;
}
.tablist button[aria-selected="true"] { color: var(--accent-text); border-bottom-color: var(--accent); }
.tabpanel[hidden] { display: none; }

/* Predict-the-output */
details.predict { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin: 1rem 0; }
details.predict summary { cursor: pointer; padding: .8rem 1rem; font-weight: 600; list-style: none; display: flex; gap: .6rem; align-items: center; }
details.predict summary::-webkit-details-marker { display: none; }
details.predict summary::before { content: "?"; display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: #fff; font-size: .8rem; flex: none; }
details.predict[open] summary::before { content: "✓"; background: var(--ok); }
details.predict .answer { padding: 0 1rem 1rem; font-size: .95rem; }

/* Tables */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); margin: 1rem 0 1.5rem; }
.table-wrap table th:first-child, .table-wrap table td:first-child { padding-left: 1rem; }
.table-wrap caption { caption-side: top; text-align: left; padding: .8rem 1rem .2rem; font-size: .86rem; color: var(--muted); }
.table-wrap caption b { color: var(--text); }
tr.is-go td { background: var(--accent-soft); }
tr.dim td { opacity: .3; }
td.lang { font-weight: 700; white-space: nowrap; }
.pending-cell { color: var(--warn); font: 600 .82rem var(--mono); }

/* ===== Widgets ===== */
.widget { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem; margin: 1.2rem 0 1.6rem; box-shadow: var(--shadow); }
.widget-title { font-weight: 700; display: flex; align-items: center; gap: .5rem; margin-bottom: .2rem; }
.widget-title::before { content: "▶"; font-size: .7rem; color: var(--accent); }
.widget-sub { font-size: .88rem; color: var(--muted); margin-bottom: 1rem; }
.controls { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: 1rem; }
.controls label { font-size: .88rem; color: var(--muted); display: flex; align-items: center; gap: .4rem; }
.controls select, .controls input[type="number"] { font: inherit; font-size: .88rem; padding: .3rem .4rem; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); }
.wbtn { font: 600 .85rem var(--font); padding: .45rem .8rem; border-radius: 7px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); cursor: pointer; }
.wbtn:hover:not(:disabled) { border-color: var(--accent); }
.wbtn.go { background: var(--accent); border-color: var(--accent); color: #fff; }
.wbtn.danger { color: var(--bad); }
.wbtn:disabled { opacity: .45; cursor: not-allowed; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { font: 600 .82rem var(--font); border: 0; background: var(--surface); color: var(--muted); padding: .4rem .75rem; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-text); }

/* Channel simulator */
.chan-stage { display: grid; grid-template-columns: 1fr; gap: .8rem; }
@media (min-width: 640px) { .chan-stage { grid-template-columns: 1fr 1.4fr 1fr; align-items: stretch; } }
.lane { background: var(--surface-2); border-radius: 8px; padding: .7rem; min-height: 92px; }
.lane h5 { margin: 0 0 .5rem; font-size: .72rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.slots { display: flex; gap: .35rem; flex-wrap: wrap; }
.slot { width: 42px; height: 42px; border-radius: 7px; border: 2px dashed var(--border); display: grid; place-items: center; font: 700 .9rem var(--mono); }
.slot.full { border-style: solid; border-color: var(--accent); background: var(--accent-soft); color: var(--accent-text); }
.gor { font: 600 .78rem var(--mono); padding: .25rem .5rem; border-radius: 6px; background: var(--warn-soft); color: var(--warn); display: inline-block; margin: 0 .25rem .25rem 0; }
.chan-status { font-size: .85rem; margin-top: .6rem; color: var(--muted); }
.chan-status b { color: var(--text); }
.log { font: .8rem/1.5 var(--mono); background: var(--code-bg); color: var(--code-text); border-radius: 8px; padding: .6rem .8rem; height: 132px; overflow-y: auto; margin-top: .8rem; }
.log .e-ok { color: #8be3aa; } .log .e-block { color: #f3c46a; } .log .e-bad { color: #ff9cb0; } .log .e-info { color: #8fd3ec; }

/* Slice visualizer */
.slice-viz { display: grid; gap: .9rem; }
.slice-row { display: flex; flex-wrap: wrap; align-items: center; gap: .8rem; }
.hdr { font: .82rem/1.35 var(--mono); border: 2px solid var(--accent); border-radius: 8px; padding: .4rem .6rem; background: var(--accent-soft); min-width: 150px; }
.hdr.ghost { opacity: .35; border-style: dashed; }
.hdr b { display: block; font-family: var(--font); font-size: .78rem; color: var(--accent-text); }
.arr { display: flex; gap: 3px; }
.arr .cell { width: 38px; height: 38px; display: grid; place-items: center; font: 700 .9rem var(--mono); border: 2px solid var(--text); background: var(--surface); }
.arr .cell.hot { background: var(--warn-soft); border-color: var(--warn); }
.arr .cell.spare { border-style: dashed; color: var(--muted); }
.arr-label { font: 600 .75rem var(--mono); color: var(--muted); }
.arr.garbage { opacity: .35; }
.step-caption { font-size: .93rem; padding: .7rem .9rem; background: var(--surface-2); border-radius: 8px; min-height: 3.2em; }
.step-caption code { background: var(--surface); }

/* GC calculator */
.range-row { display: grid; grid-template-columns: 1fr; gap: .2rem; margin-bottom: .8rem; }
@media (min-width: 560px) { .range-row { grid-template-columns: 170px 1fr 90px; align-items: center; gap: .8rem; } }
.range-row label { font-size: .88rem; font-weight: 600; }
.range-row output { font: 600 .88rem var(--mono); text-align: right; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
.bar { position: relative; height: 34px; background: var(--surface-2); border-radius: 7px; overflow: hidden; margin: .8rem 0 .4rem; }
.bar .live { position: absolute; left: 0; top: 0; bottom: 0; background: var(--accent); }
.bar .head { position: absolute; top: 0; bottom: 0; background: repeating-linear-gradient(45deg, color-mix(in srgb, var(--accent) 35%, transparent) 0 6px, transparent 6px 12px); }
.bar .limit { position: absolute; top: -2px; bottom: -2px; width: 3px; background: var(--bad); }
.legend { display: flex; flex-wrap: wrap; gap: 1rem; font-size: .8rem; color: var(--muted); }
.legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: .35rem; vertical-align: -1px; background: var(--c); }
.readout { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .6rem; margin-top: .9rem; }
.readout div { background: var(--surface-2); border-radius: 8px; padding: .55rem .75rem; }
.readout small { display: block; font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.readout b { font: 700 1.05rem var(--mono); }

/* Steal loop steps */
.steps { counter-reset: s; list-style: none; padding: 0; display: grid; gap: .5rem; margin: 1rem 0; }
.steps li { counter-increment: s; display: flex; gap: .8rem; align-items: flex-start; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: .65rem .9rem; font-size: .95rem; }
.steps li::before { content: counter(s); flex: none; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; background: var(--accent-soft); color: var(--accent-text); font: 700 .8rem var(--mono); }

/* Toast */
.toast { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 8px; font-size: .9rem; opacity: 0; transition: .2s; pointer-events: none; z-index: 60; max-width: calc(100vw - 32px); }
.toast.show { opacity: 1; transform: translateX(-50%); }

/* Footer */
footer.site { border-top: 1px solid var(--border); background: var(--surface); }
.foot-inner { max-width: 1240px; margin: 0 auto; padding: 2.5rem 1rem; display: grid; gap: 2rem; grid-template-columns: 1fr; }
@media (min-width: 820px) { .foot-inner { grid-template-columns: 1.2fr 1fr; } }
.foot-inner h4 { margin: 0 0 .7rem; font-size: .8rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.member-list { list-style: none; padding: 0; margin: 0; display: grid; gap: .6rem; }
.member-list li { display: flex; flex-direction: column; }
.member-list .role { font-size: .85rem; color: var(--muted); }
.refs { font-size: .85rem; color: var(--muted); padding-left: 1.2rem; }
.refs li { margin: .35rem 0; }

@media (max-width: 640px) {
  body { font-size: 16px; }
  .team-links { display: none; }
  .hero { padding-top: 2rem; }
}
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } * { transition: none !important; } }
