:root {
  --bg: #ffffff;
  --bg-elev: #f7f8fa;
  --bg-soft: #eef1f5;
  --border: #dde2ea;
  --text: #1f2430;
  --text-mute: #5b6573;
  --text-dim: #94a3b8;
  --accent: #2563eb;
  --accent-2: #d97706;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --code-bg: #f1f3f7;
  --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;
  padding: 0;
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.65;
  background: var(--bg);
  color: var(--text);
}

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

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

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

header.topnav .crumb { color: var(--text-mute); }
header.topnav a.crumb-link {
  color: var(--accent);
  text-decoration: none;
}
header.topnav a.crumb-link:hover { text-decoration: underline; }

header.topnav .step-pill {
  margin-left: auto;
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-mute);
  font-family: var(--mono);
  font-size: 11px;
  white-space: nowrap;
}

h1 {
  font-size: 30px;
  line-height: 1.2;
  margin: 16px 0 4px;
  font-weight: 650;
  letter-spacing: -0.01em;
}

h1 + .subtitle {
  color: var(--text-mute);
  font-size: 17px;
  margin: 0 0 28px;
}

h2 {
  font-size: 21px;
  margin: 38px 0 10px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
  padding-bottom: 6px;
  letter-spacing: -0.005em;
}

h3 {
  font-size: 17px;
  margin: 26px 0 8px;
  font-weight: 600;
}

p { margin: 10px 0; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

code {
  font-family: var(--mono);
  font-size: 13.5px;
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 4px;
  border: 1px solid var(--border);
}

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

pre code {
  background: transparent;
  border: none;
  padding: 0;
}

ul, ol { padding-left: 22px; }
li { margin: 4px 0; }

table {
  border-collapse: collapse;
  width: 100%;
  margin: 14px 0;
  font-size: 14px;
}

th, td {
  text-align: left;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

th {
  font-size: 12px;
  font-family: var(--mono);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-mute);
  font-weight: 600;
}

tr:hover td { background: var(--bg-elev); }

.math {
  font-family: "Latin Modern Math", "STIX Two Math", Cambria, Georgia, serif;
  font-style: italic;
  font-size: 1.04em;
}

.math.block {
  display: block;
  text-align: center;
  margin: 16px 0;
  padding: 12px 8px;
  background: var(--bg-elev);
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 1.08em;
}

.callout {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-left: 3px solid var(--accent);
  padding: 12px 16px;
  margin: 16px 0;
  border-radius: 4px 8px 8px 4px;
}

.callout.warn { border-left-color: var(--warn); }
.callout.bad { border-left-color: var(--bad); }
.callout.ok { border-left-color: var(--ok); }

.callout .label {
  font-size: 11px;
  font-family: var(--mono);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-mute);
  margin-bottom: 4px;
}

.toc-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin: 24px 0;
}

.toc-card {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  padding: 16px 18px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-radius: 10px;
  text-decoration: none;
  color: inherit;
  transition: border-color 0.12s ease, background 0.12s ease;
}

.toc-card:hover {
  border-color: var(--accent);
  background: #fbfcff;
  text-decoration: none;
}

.toc-card .num {
  font-family: var(--mono);
  font-size: 22px;
  color: var(--text-dim);
  width: 32px;
  flex-shrink: 0;
}

.toc-card .ttl {
  font-weight: 600;
  font-size: 16px;
  color: var(--text);
  margin-bottom: 4px;
}

.toc-card .sum {
  color: var(--text-mute);
  font-size: 14px;
  line-height: 1.5;
}

.topic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
  margin: 16px 0;
}

.topic-card {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-radius: 8px;
  padding: 12px 14px;
}

.topic-card .k {
  font-family: var(--mono);
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.topic-card .v {
  color: var(--text);
  font-weight: 600;
  margin-top: 2px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 12px 0;
}

.chip {
  font-family: var(--mono);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--code-bg);
  color: var(--text-mute);
}

.prompt-list li {
  margin: 8px 0;
}

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

footer.lesson-nav {
  margin-top: 60px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 14px;
}

footer.lesson-nav a {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  max-width: 48%;
  text-decoration: none;
}

footer.lesson-nav a:hover {
  background: var(--bg-elev);
  text-decoration: none;
}

footer.lesson-nav a.next {
  text-align: right;
  align-items: flex-end;
}

footer.lesson-nav .dir {
  font-size: 11px;
  color: var(--text-mute);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-family: var(--mono);
}

footer.lesson-nav .ttl { color: var(--accent); }

/* ── interactive widgets (ported from the RL track for parity) ── */
.widget {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 18px;
  margin: 22px 0;
}
.widget .title {
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
  margin-bottom: 4px;
}
.widget .hint {
  font-size: 13px;
  color: var(--text-mute);
  margin-bottom: 14px;
}
.widget .controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin: 8px 0 12px;
}

button {
  font-family: var(--sans);
  font-size: 13px;
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}
button:hover { background: #e8ecf2; border-color: #cbd5e1; }
button.primary {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
  font-weight: 600;
}
button.primary:hover { background: #3b82f6; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border);
  border-radius: 4px;
  outline: none;
  width: 200px;
  max-width: min(200px, 44vw);
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}
input[type="range"]::-moz-range-thumb {
  width: 14px;
  height: 14px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  border: none;
}

input[type="text"], input[type="number"] {
  background: var(--code-bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 8px;
  font-family: var(--mono);
  font-size: 13px;
}

label.inline {
  font-size: 13px;
  color: var(--text-mute);
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  max-width: 100%;
}
label.inline .val {
  font-family: var(--mono);
  color: var(--text);
  min-width: 40px;
  display: inline-block;
}

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 8px;
  margin: 10px 0;
}
.kpi {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
.kpi .k { font-size: 11px; color: var(--text-mute); font-family: var(--mono); text-transform: uppercase; letter-spacing: 0.06em; }
.kpi .v { font-size: 18px; font-family: var(--mono); margin-top: 2px; }

.dist-canvas {
  width: 100%;
  height: 160px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: block;
}

.widget details { margin-top: 12px; }
.widget summary { cursor: pointer; color: var(--text-mute); font-size: 13px; }

.diagram {
  display: block;
  width: 100%;
  height: auto;
  margin: 14px 0;
}
.diagram text { font-family: var(--sans); fill: var(--text); }
.diagram .node-bg { fill: var(--bg-elev); stroke: var(--border); stroke-width: 1; }
.diagram .node-bg.active { stroke: var(--accent); stroke-width: 2; }
.diagram .edge { stroke: var(--text-dim); stroke-width: 1.2; fill: none; }
.diagram .edge.active { stroke: var(--accent); stroke-width: 2.2; }
.diagram .edge-label { fill: var(--text-mute); font-size: 11px; font-family: var(--mono); }

.tag {
  font-family: var(--mono);
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 3px;
  border: 1px solid var(--border);
  background: var(--code-bg);
  color: var(--text-mute);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  margin-left: 6px;
  vertical-align: 2px;
}

@media (max-width: 640px) {
  main { padding: 24px 18px 80px; }
  header.topnav .inner {
    padding: 10px 18px;
    gap: 8px;
    flex-wrap: wrap;
  }
  header.topnav .step-pill {
    margin-left: 0;
    width: 100%;
  }
  h1 { font-size: 26px; }
  .toc-card { padding: 14px; gap: 12px; }
  footer.lesson-nav {
    flex-direction: column;
  }
  footer.lesson-nav a {
    max-width: none;
  }
  footer.lesson-nav a.next {
    text-align: left;
    align-items: flex-start;
  }
}
