:root {
  --bg: #ffffff;
  --bg-elev: #f8f8fc;
  --bg-soft: #f0eef9;
  --border: #deddea;
  --text: #20202a;
  --text-mute: #626273;
  --text-dim: #9a98aa;
  --accent: #6d4aff;
  --accent-dark: #4e32c7;
  --accent-soft: #eeebff;
  --cyan: #0891b2;
  --amber: #d97706;
  --green: #15935a;
  --red: #dc3f55;
  --code-bg: #f2f1f7;
  --mono: "SF Mono", Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", sans-serif;
}

html { box-sizing: border-box; }
*, *::before, *::after { box-sizing: inherit; }

body {
  margin: 0;
  color: var(--text);
  background: var(--bg);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.66;
}

main { max-width: 880px; margin: 0 auto; padding: 32px 28px 120px; }

.topnav {
  position: sticky;
  top: 0;
  z-index: 20;
  border-bottom: 1px solid var(--border);
  background: rgba(255,255,255,.91);
  backdrop-filter: saturate(180%) blur(12px);
}

.topnav .inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 11px 28px;
  display: flex;
  align-items: center;
  gap: 14px;
  font-size: 13px;
}

.crumb { color: var(--text-mute); }
.crumb-link { color: var(--accent); text-decoration: none; }
.crumb-link:hover { text-decoration: underline; }
.step-pill {
  margin-left: auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  color: var(--text-mute);
  font-family: var(--mono);
  font-size: 11px;
  white-space: nowrap;
}

h1 {
  margin: 17px 0 5px;
  font-size: clamp(29px, 5vw, 40px);
  line-height: 1.13;
  font-weight: 680;
  letter-spacing: -.025em;
}

h1 + .subtitle { margin: 0 0 28px; color: var(--text-mute); font-size: 17px; max-width: 790px; }
h2 {
  margin: 40px 0 11px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  font-size: 21px;
  line-height: 1.35;
  font-weight: 630;
  letter-spacing: -.008em;
}
h3 { margin: 26px 0 8px; font-size: 17px; font-weight: 650; }
p { margin: 10px 0; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
strong { font-weight: 650; }
ul, ol { padding-left: 23px; }
li { margin: 5px 0; }

/* linear-spine strip: makes the forced-move chain scannable on every lesson */
.chain {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin: 16px 0 22px;
}
.chain > span {
  position: relative;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev);
  font-size: 12.5px;
  line-height: 1.45;
}
.chain > span b {
  display: block;
  margin-bottom: 2px;
  color: var(--accent);
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.chain > span.this { border-color: #bdb3ff; background: var(--accent-soft); }
.chain > span + span::before {
  content: "→";
  position: absolute;
  left: -9px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim);
  font-size: 12px;
}
@media (max-width: 640px) {
  .chain { grid-template-columns: 1fr; }
  .chain > span + span::before { content: "↓"; left: 50%; top: -8px; transform: translateX(-50%); }
}

.reasoning-chain {
  margin: 18px 0 22px;
  padding: 0;
  list-style: none;
  counter-reset: reasoning-step;
}
.reasoning-chain > li {
  position: relative;
  margin: 0;
  padding: 14px 14px 14px 48px;
  border-left: 2px solid var(--accent-soft);
  line-height: 1.62;
}
.reasoning-chain > li + li { border-top: 1px solid var(--border); }
.reasoning-chain > li::before {
  position: absolute;
  top: 14px;
  left: 10px;
  display: grid;
  width: 25px;
  height: 25px;
  place-items: center;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  content: counter(reasoning-step);
  counter-increment: reasoning-step;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 700;
}

code {
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 13.5px;
}
pre {
  margin: 14px 0;
  padding: 14px 16px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.55;
}
pre code { padding: 0; border: 0; background: transparent; }

.math {
  font-family: "STIX Two Math", "Latin Modern Math", Cambria, Georgia, serif;
  font-style: italic;
  font-size: 1.04em;
}
.math.block {
  display: block;
  margin: 17px 0;
  padding: 13px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-elev);
  text-align: center;
  font-size: 1.08em;
  overflow-x: auto;
}

table { width: 100%; margin: 15px 0; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th {
  color: var(--text-mute);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
}
tr:hover td { background: var(--bg-elev); }

.callout {
  margin: 17px 0;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: 4px 8px 8px 4px;
  background: var(--bg-elev);
}
.callout.warn { border-left-color: var(--amber); }
.callout.ok { border-left-color: var(--green); }
.callout.bad { border-left-color: var(--red); }
.callout .label {
  margin-bottom: 3px;
  color: var(--text-mute);
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: .09em;
  text-transform: uppercase;
}

.ascii {
  margin: 16px 0;
  padding: 14px;
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.48;
  white-space: pre;
}

.topic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
  margin: 16px 0;
}
.topic-card {
  min-height: 88px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--bg-elev);
}
.topic-card .k {
  color: var(--accent);
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: .07em;
  text-transform: uppercase;
}
.topic-card .v { margin-top: 3px; font-weight: 620; line-height: 1.45; }
.topic-card .s { margin-top: 4px; color: var(--text-mute); font-size: 13px; line-height: 1.45; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.chip {
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--code-bg);
  color: var(--text-mute);
  font-family: var(--mono);
  font-size: 10px;
}

.toc-grid { display: grid; grid-template-columns: 1fr; gap: 11px; margin: 23px 0; }
.toc-card {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-elev);
  color: inherit;
  text-decoration: none;
  transition: transform .14s ease, border-color .14s ease, box-shadow .14s ease;
}
.toc-card:hover {
  transform: translateY(-1px);
  border-color: #a99cff;
  box-shadow: 0 8px 24px rgba(42, 30, 100, .07);
  text-decoration: none;
}
.toc-card .num { flex: 0 0 34px; color: var(--text-dim); font-family: var(--mono); font-size: 21px; }
.toc-card .ttl { margin-bottom: 3px; color: var(--text); font-size: 16px; font-weight: 650; }
.toc-card .sum { color: var(--text-mute); font-size: 14px; line-height: 1.48; }

.hero-loop {
  position: relative;
  margin: 24px 0 30px;
  padding: 22px;
  overflow: hidden;
  border: 1px solid #d9d5f4;
  border-radius: 14px;
  background:
    radial-gradient(circle at 90% 8%, rgba(109,74,255,.16), transparent 31%),
    linear-gradient(145deg, #faf9ff, #f4f7fb);
}
.hero-loop::after {
  content: "";
  position: absolute;
  width: 170px; height: 170px;
  right: -72px; bottom: -100px;
  border: 34px solid rgba(8,145,178,.09);
  border-radius: 50%;
}
.loop-row { position: relative; z-index: 1; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; align-items: center; }
.loop-node {
  padding: 12px 9px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: rgba(255,255,255,.88);
  text-align: center;
}
.loop-node b { display: block; color: var(--accent); font-family: var(--mono); font-size: 12px; }
.loop-node span { color: var(--text-mute); font-size: 12px; }
.loop-arrow { display: none; }

.widget {
  margin: 23px 0;
  padding: 17px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg-elev);
}
.widget .title { font-size: 14px; font-weight: 650; }
.widget .hint { margin: 2px 0 13px; color: var(--text-mute); font-size: 13px; }
.widget .controls { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; margin: 10px 0 12px; }
.widget canvas {
  display: block;
  width: 100%;
  height: 250px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: #fff;
}
.widget .readout {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(125px, 1fr));
  gap: 8px;
  margin-top: 10px;
}
.metric { padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; background: var(--code-bg); }
.metric .k { color: var(--text-mute); font-family: var(--mono); font-size: 9px; letter-spacing: .07em; text-transform: uppercase; }
.metric .v { margin-top: 1px; font-family: var(--mono); font-size: 16px; }

button {
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-soft);
  color: var(--text);
  font: 13px var(--sans);
  cursor: pointer;
}
button:hover { border-color: #bdb6eb; background: var(--accent-soft); }
button.primary { border-color: var(--accent); background: var(--accent); color: #fff; font-weight: 650; }
button.primary:hover { background: var(--accent-dark); }

label.inline { display: inline-flex; gap: 8px; align-items: center; color: var(--text-mute); font-size: 12px; }
label.inline .val { min-width: 35px; color: var(--text); font-family: var(--mono); }
input[type="range"] {
  width: min(190px, 42vw);
  height: 4px;
  appearance: none;
  border-radius: 5px;
  background: var(--border);
}
input[type="range"]::-webkit-slider-thumb {
  width: 14px; height: 14px;
  appearance: none;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
select {
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: 12px var(--mono);
}

.legend { display: flex; flex-wrap: wrap; gap: 12px; margin: 8px 0; color: var(--text-mute); font-size: 11px; }
.legend span::before { content: ""; display: inline-block; width: 9px; height: 9px; margin-right: 5px; border-radius: 50%; background: var(--dot, var(--accent)); }

.prompt-list li { margin: 9px 0; }
.prompt-list em { color: var(--text-mute); }

footer.lesson-nav {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 60px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  font-size: 14px;
}
.lesson-nav a {
  display: inline-flex;
  flex: 1;
  max-width: 49%;
  flex-direction: column;
  gap: 2px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  text-decoration: none;
}
.lesson-nav a:hover { background: var(--bg-elev); }
.lesson-nav a.next { align-items: flex-end; text-align: right; }
.lesson-nav .dir { color: var(--text-mute); font-family: var(--mono); font-size: 10px; letter-spacing: .08em; text-transform: uppercase; }
.lesson-nav .ttl { color: var(--accent); }

@media (max-width: 640px) {
  main { padding: 23px 18px 80px; }
  .topnav .inner { flex-wrap: wrap; gap: 7px; padding: 9px 18px; }
  .step-pill { width: 100%; margin-left: 0; }
  .loop-row { grid-template-columns: 1fr 1fr; }
  .widget { padding: 13px; }
  .widget canvas { height: 220px; }
  footer.lesson-nav { flex-direction: column; }
  .lesson-nav a { max-width: none; }
  .lesson-nav a.next { align-items: flex-start; text-align: left; }
  table { display: block; overflow-x: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
