/* Inside the switch: animated walkthroughs. Uses the tokens of style.css. */

/* ---------- chapter links ---------- */
.chapter-links { list-style: none; padding: 0; margin: 22px 0 0; display: flex; flex-wrap: wrap; gap: 10px; }
.chapter-links a {
  display: inline-flex; align-items: center; gap: 10px;
  padding: 9px 14px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 600; font-size: 0.93rem;
}
.chapter-links a:hover { border-color: var(--accent); text-decoration: none; }
.chapter-links span:first-child { font: 700 0.78rem/1 var(--font-mono); color: var(--accent); }

/* ---------- player layout ----------
   The stage takes the full width so its labels stay legible; with JS the active
   step is shown as a caption below it, without JS all steps are listed. */
.player { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; max-width: 980px; }
.player-stage { min-width: 0; }

/* ---------- the stage: a dark instrument panel in both themes ---------- */
.stage, .sb-panel {
  --s-bg: #0a1018;
  --s-panel: #101a26;
  --s-panel-2: #132030;
  --s-line: #25354a;
  --s-line-2: #344a63;
  --s-text: #dce5ef;
  --s-muted: #8496aa;
  --s-teal: #3fd3be;
  --s-amber: #f5b544;
  --s-violet: #a78bfa;
  --s-blue: #60a5fa;
  --s-slate: #52637a;
  --s-red: #f87171;
  /* the three flows of chapter 3: checked for colour-blind separation on this surface */
  --f1: #3b82f6;
  --f2: #d97706;
  --f3: #0d9488;
}
.stage {
  position: relative; overflow: hidden; border-radius: 18px;
  background:
    radial-gradient(120% 90% at 15% 0%, rgba(63, 211, 190, 0.10) 0%, transparent 45%),
    radial-gradient(90% 80% at 100% 100%, rgba(167, 139, 250, 0.10) 0%, transparent 50%),
    var(--s-bg);
  border: 1px solid #1c2a3b;
  box-shadow: 0 24px 60px -20px rgba(5, 12, 24, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.05);
}
.stage::before {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background-image: radial-gradient(rgba(255, 255, 255, 0.07) 1px, transparent 1px);
  background-size: 20px 20px;
  -webkit-mask-image: radial-gradient(ellipse at center, #000 40%, transparent 85%);
  mask-image: radial-gradient(ellipse at center, #000 40%, transparent 85%);
}
.stage svg { position: relative; display: block; width: 100%; height: auto; padding: 10px; }
/* on phones the diagram keeps a readable size and scrolls sideways */
.stage-scroll-hint { display: none; margin: 6px 2px 0; font: 0.78rem var(--font-mono); color: var(--muted); }
@media (max-width: 700px) {
  .stage { overflow-x: auto; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; }
  .stage svg { min-width: 640px; }
  .stage::before { right: auto; width: 100%; min-width: 640px; }
  .stage.wide svg, .stage.wide::before { min-width: 780px; }
  .stage-scroll-hint { display: block; }
}

.stage text { font-family: var(--font-sans); fill: var(--s-text); }
.stage .t-title { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; }
.stage .t-label { font-size: 13px; font-weight: 650; }
.stage .t-small { font-size: 11.5px; }
.stage .t-sub { font: 10.5px var(--font-mono); fill: var(--s-muted); }
.stage .t-tiny { font-size: 9.5px; }
.stage .t-tag { font: 600 9.5px var(--font-mono); letter-spacing: 0.1em; fill: var(--s-muted); }
.stage .t-row { font: 10.5px var(--font-mono); fill: var(--s-text); }
.stage .t-port { font: 600 10.5px var(--font-mono); fill: var(--s-text); }
.stage .t-lane { font: 9px var(--font-mono); fill: var(--s-muted); }
.stage .t-qn { font: 8.5px var(--font-mono); fill: var(--s-muted); }
.stage .t-stage { font: 700 12px var(--font-mono); fill: var(--s-muted); }
.stage .t-chip { font: 600 9.5px var(--font-mono); fill: var(--s-text); }
.stage .t-loop { font: 600 10px var(--font-mono); fill: var(--s-violet); }
.stage .t-hit { font: 700 10.5px var(--font-mono); fill: var(--s-amber); }
.stage .t-field { font: 11px var(--font-mono); fill: var(--s-text); transition: fill 0.4s; }
.stage .t-note { font: 12px var(--font-mono); fill: var(--s-text); }
.stage .t-dim { fill: var(--s-muted); }
.stage .t-teal { fill: var(--s-teal); }
.stage .t-amber { fill: var(--s-amber); }
.stage .t-violet { fill: var(--s-violet); }

.stage .box {
  fill: var(--s-panel); stroke: var(--s-line); stroke-width: 1.2;
  transition: stroke 0.35s, fill 0.35s, filter 0.35s;
}
.stage .box.alt { fill: var(--s-panel-2); }
.stage .box.shell { fill: rgba(16, 26, 38, 0.55); }
.stage .box.port { fill: #142235; }
.stage .box.track { fill: var(--s-panel-2); }
.stage .box.phv { fill: rgba(63, 211, 190, 0.08); stroke: rgba(63, 211, 190, 0.5); }
.stage .divider { stroke: var(--s-line); stroke-dasharray: 3 5; fill: none; }
.stage .chip { fill: var(--s-panel); stroke: var(--s-line-2); stroke-width: 1; }
.stage .chip-loop { stroke: rgba(167, 139, 250, 0.6); fill: rgba(167, 139, 250, 0.10); }
.stage .lane { fill: rgba(255, 255, 255, 0.035); stroke: var(--s-line); stroke-width: 1; transition: stroke 0.3s, fill 0.3s; }
.stage .row-bg { fill: transparent; transition: fill 0.35s; }
.stage .clock-face { fill: var(--s-panel); stroke: var(--s-line-2); stroke-width: 1.2; }
.stage .clock-hand { stroke: var(--s-muted); stroke-width: 1.4; stroke-linecap: round; fill: none; }

.stage .wire { fill: none; stroke: var(--s-line-2); stroke-width: 1.6; transition: stroke 0.35s, filter 0.35s; }
.stage .wire.dash { stroke-dasharray: 4 5; }
.stage .wire.loop { stroke: rgba(167, 139, 250, 0.7); }
.stage .mpath { fill: none; stroke: none; }

/* states set by the script */
.stage .on { stroke: var(--s-teal); filter: drop-shadow(0 0 6px rgba(63, 211, 190, 0.55)); }
.stage .on-amber { stroke: var(--s-amber); filter: drop-shadow(0 0 6px rgba(245, 181, 68, 0.55)); }
.stage .on-violet { stroke: var(--s-violet); filter: drop-shadow(0 0 6px rgba(167, 139, 250, 0.6)); }
.stage .wire.on, .stage .wire.on-violet { stroke-dasharray: 6 6; animation: flow 0.9s linear infinite; }
.stage .lane.on-amber { fill: rgba(245, 181, 68, 0.12); }
.stage .row-bg.new { fill: rgba(167, 139, 250, 0.28); }
.stage .row-bg.hit { fill: rgba(245, 181, 68, 0.26); }
.stage text.changed { fill: var(--s-amber); }
.stage .t-stage.lit { fill: var(--s-teal); }
.stage .tick .clock-face { stroke: var(--s-amber); filter: drop-shadow(0 0 6px rgba(245, 181, 68, 0.7)); }
.stage .tick .clock-hand { stroke: var(--s-amber); }
@keyframes flow { to { stroke-dashoffset: -12; } }

.stage .fx { opacity: 0; transition: opacity 0.45s; }
.stage .fx.show { opacity: 1; }
.stage .mover { opacity: 0; transition: opacity 0.25s; }
.stage .mover.show { opacity: 1; }

/* packets */
.stage .pkt-shell { fill: #0b1320; stroke: rgba(255, 255, 255, 0.45); stroke-width: 1; }
.stage .pkt-shell.copy { stroke: var(--s-violet); stroke-dasharray: 3 2; }
.stage .seg { transition: transform 0.35s; }
.stage .seg-eth { fill: var(--s-teal); }
.stage .seg-ip { fill: var(--s-blue); }
.stage .seg-l4 { fill: var(--s-violet); }
.stage .seg-pay { fill: var(--s-slate); }
.stage .hero-pkt .pkt-shell, .stage #s1-pkt .pkt-shell { filter: drop-shadow(0 0 7px rgba(245, 181, 68, 0.55)); stroke: var(--s-amber); }
.stage .msg { fill: var(--s-violet); filter: drop-shadow(0 0 7px rgba(167, 139, 250, 0.8)); }
.stage .marker { fill: var(--s-amber); filter: drop-shadow(0 0 6px rgba(245, 181, 68, 0.9)); }

/* queues and scheduling (chapter 3) */
.stage .qp rect { stroke: var(--s-bg); stroke-width: 1; transition: fill 0.25s; }
.stage .qp.f1 rect { fill: var(--f1); }
.stage .qp.f2 rect { fill: var(--f2); }
.stage .qp.f3 rect { fill: var(--f3); }
.stage .qp text { font: 700 8px var(--font-mono); fill: #06101a; }
.stage .qp.drop rect { fill: #ef4444; }
.stage #q-r1.hit { fill: rgba(59, 130, 246, 0.3); }
.stage #q-r2.hit { fill: rgba(217, 119, 6, 0.3); }
.stage #q-r3.hit { fill: rgba(13, 148, 136, 0.34); }
.stage .t-red { fill: var(--s-red); }
.stage .cap-line { fill: none; stroke: var(--s-red); stroke-width: 1.4; stroke-dasharray: 3 3; }
.stage .lane.wait { stroke: rgba(248, 113, 113, 0.6); fill: rgba(248, 113, 113, 0.07); }
.stage .clock-marks { stroke: var(--s-line-2); stroke-width: 1; fill: none; }
.stage #q-clock.tick .clock-face { stroke: var(--s-violet); filter: drop-shadow(0 0 6px rgba(167, 139, 250, 0.7)); }
.stage #q-clock.tick .clock-hand { stroke: var(--s-violet); }

/* header blocks (chapter 3) */
.stage .hb { stroke-width: 1.4; }
.stage .hb-eth { fill: rgba(63, 211, 190, 0.12); stroke: rgba(63, 211, 190, 0.7); }
.stage .hb-ip { fill: rgba(96, 165, 250, 0.12); stroke: rgba(96, 165, 250, 0.7); }
.stage .hb-l4 { fill: rgba(167, 139, 250, 0.12); stroke: rgba(167, 139, 250, 0.7); }
.stage .hb-pay { fill: rgba(82, 99, 122, 0.22); stroke: rgba(132, 150, 170, 0.6); }
.stage .hb-cus { fill: rgba(245, 181, 68, 0.14); stroke: var(--s-amber); filter: drop-shadow(0 0 10px rgba(245, 181, 68, 0.35)); }
.stage .hblock { transition: opacity 0.4s; }
.stage .hidden-block { opacity: 0; }

/* ---------- caption and controls ---------- */
.stage-caption { display: none; }
.js .stage-caption {
  display: block; margin-top: 14px; padding: 16px 20px 6px; border-radius: 14px;
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
  min-height: 7.5em;
}
.js .section-alt .stage-caption { background: var(--bg); box-shadow: none; }
.stage-caption strong { display: flex; gap: 10px; align-items: baseline; margin-bottom: 4px; font-size: 1.05rem; }
.stage-caption strong .n { font: 700 0.78rem/1 var(--font-mono); color: var(--accent); }
.stage-caption p { margin: 0 0 10px; color: var(--muted); max-width: 62em; }
.stage-caption .code { margin: 0 0 12px; font-size: 0.8rem; max-width: 46em; }
.stage-caption.swap { animation: caption-in 0.35s ease; }
@keyframes caption-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.controls { display: none; }
.js .controls { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.ctrl-btn {
  flex: none; display: inline-grid; place-items: center; width: 38px; height: 38px; padding: 0;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer;
}
.ctrl-btn:hover { border-color: var(--accent); }
.ctrl-btn svg { width: 16px; height: 16px; fill: currentColor; }
.ctrl-btn.play { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.progress { flex: 1; display: flex; gap: 5px; min-width: 0; }
.progress button {
  flex: 1; height: 22px; padding: 0; border: 0; background: none; cursor: pointer; position: relative;
}
.progress button::before {
  content: ""; position: absolute; left: 0; right: 0; top: 9px; height: 4px; border-radius: 2px; background: var(--border);
}
.progress button.done::before { background: var(--accent); }
.progress button.active::before { background: linear-gradient(90deg, var(--accent), var(--amber)); }
.step-count { flex: none; font: 600 0.78rem/1 var(--font-mono); color: var(--muted); min-width: 3.2em; text-align: right; }

/* ---------- steps list ---------- */
.steps { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.step-item {
  border: 1px solid var(--border); border-radius: 12px; padding: 12px 16px 4px;
  background: var(--surface); transition: border-color 0.25s, box-shadow 0.25s, opacity 0.25s;
}
.section-alt .step-item { background: var(--bg); }
.js .step-item { opacity: 0.62; cursor: pointer; }
.js .step-item.active { opacity: 1; border-color: var(--accent); box-shadow: var(--shadow); }
.step-title {
  display: flex; gap: 10px; align-items: baseline; width: 100%; padding: 0; margin: 0 0 6px;
  border: 0; background: none; color: var(--text); font: 650 1rem/1.35 var(--font-sans); text-align: left; cursor: pointer;
}
.step-title .n { font: 700 0.76rem/1 var(--font-mono); color: var(--accent); }
.step-item p { color: var(--muted); font-size: 0.93rem; margin-bottom: 10px; }
.step-item .code { margin: 0 0 12px; font-size: 0.78rem; }

.js .steps { display: none; }

/* ---------- mode toggle (chapter 4) and scenario buttons (chapter 3) ---------- */
.mode-toggle, .sb-presets { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-bottom: 12px; font-size: 0.88rem; color: var(--muted); }
.mode-toggle span, .sb-presets span { margin-right: 4px; }
.mode-toggle button, .sb-presets button {
  font: 600 0.82rem/1 var(--font-mono); padding: 8px 12px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted);
}
.mode-toggle button[aria-pressed="true"], .sb-presets button[aria-pressed="true"] { background: var(--amber-soft); border-color: var(--amber); color: var(--text); }

/* ---------- live queue model (chapter 3) ---------- */
.sandbox { display: none; }
.js .sandbox {
  display: block; max-width: 980px; margin-top: 40px; padding: 24px;
  border: 1px solid var(--border); border-radius: 18px; background: var(--surface); box-shadow: var(--shadow);
}
@media (max-width: 600px) { .js .sandbox { padding: 16px; } .sb-panel { padding: 12px 12px 10px; } }
.sb-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: 12px 24px; margin-bottom: 18px; }
.sb-head h3 { margin: 0 0 6px; }
.sb-head .eyebrow { margin-bottom: 4px; }
.sb-lead { margin: 0; max-width: 50em; color: var(--muted); font-size: 0.93rem; }
.sb-actions { display: flex; gap: 8px; }
.sb-actions .btn { min-width: 6.2em; justify-content: center; }
.sb-presets { margin-bottom: 6px; }
.sb-note { margin: 0 0 18px; min-height: 3em; color: var(--muted); font-size: 0.9rem; max-width: 62em; }

.sb-grid { display: grid; grid-template-columns: minmax(0, 290px) minmax(0, 1fr); gap: 22px; align-items: start; }
@media (max-width: 860px) { .sb-grid { grid-template-columns: minmax(0, 1fr); } }

.sb-controls fieldset { min-width: 0; margin: 0 0 16px; padding: 0; border: 0; }
.sb-controls legend { padding: 0; margin-bottom: 8px; font: 600 0.72rem var(--font-mono); letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.sb-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 10px; align-items: center; margin-bottom: 8px; font-size: 0.86rem; }
.sb-row .sb-name { grid-column: 1; grid-row: 1; display: inline-flex; align-items: center; gap: 8px; min-width: 0; }
.sb-row output { grid-column: 2; grid-row: 1; font: 600 0.78rem var(--font-mono); color: var(--text); white-space: nowrap; }
.sb-row input[type="range"] { grid-column: 1 / -1; grid-row: 2; width: 100%; margin: 2px 0 0; accent-color: var(--accent); }
.sb-row input:disabled { opacity: 0.4; }
.sb-check { display: flex; align-items: center; gap: 8px; margin: 4px 0 8px; font-size: 0.86rem; }
.sb-check input { accent-color: var(--accent); width: 16px; height: 16px; }
.key { flex: none; display: inline-block; width: 16px; height: 0; border-top: 2.5px solid var(--muted); border-radius: 2px; }
.key.f1 { border-color: #3b82f6; }
.key.f2 { border-color: #d97706; }
.key.f3 { border-color: #0d9488; }

.sb-panel { padding: 14px 16px 12px; border-radius: 14px; background: var(--s-bg); border: 1px solid #1c2a3b; color: var(--s-text); }
.sb-panel + .sb-panel { margin-top: 14px; }
.sb-panel-head { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 10px; font: 600 0.7rem var(--font-mono); letter-spacing: 0.06em; text-transform: uppercase; color: var(--s-muted); }
.sb-clock { color: var(--s-text); letter-spacing: 0; text-transform: none; font-variant-numeric: tabular-nums; }
.sb-lane { display: grid; grid-template-columns: 2.4em minmax(0, 1fr) 6.4em; gap: 10px; align-items: center; height: 19px; font: 0.72rem var(--font-mono); color: var(--s-muted); }
.sb-lane.tx { margin-top: 8px; padding-top: 8px; height: 28px; border-top: 1px dashed var(--s-line); }
.sb-track { height: 10px; overflow: hidden; border-radius: 3px; background: rgba(255, 255, 255, 0.04); box-shadow: inset 0 0 0 1px var(--s-line); }
.sb-fill { display: block; width: 0; height: 100%; border-radius: 0 4px 4px 0; background: var(--s-slate); }
.sb-fill.f1 { background: var(--f1); }
.sb-fill.f2 { background: var(--f2); }
.sb-fill.f3 { background: var(--f3); }
.sb-val { text-align: right; color: var(--s-text); font-variant-numeric: tabular-nums; }
.sb-val.full { color: var(--s-red); }

.sb-legend { display: flex; flex-wrap: wrap; gap: 4px 16px; margin: 0 0 4px; padding: 0; list-style: none; font: 0.74rem var(--font-mono); color: var(--s-text); }
.sb-legend li { display: inline-flex; align-items: center; gap: 7px; }
.sb-legend .ref-key { width: 16px; border-top: 1.5px dashed var(--s-muted); }
.sb-plot { position: relative; border-radius: 8px; }
.sb-plot:focus-visible { outline: 2px solid var(--s-teal); outline-offset: 3px; }
.sb-plot svg { display: block; width: 100%; height: auto; touch-action: pan-y; }
.sb-plot text { font: 10px var(--font-mono); fill: var(--s-muted); }
.sb-plot .dl { fill: var(--s-text); }
.sb-plot .grid { stroke: rgba(255, 255, 255, 0.07); stroke-width: 1; }
.sb-plot .axis { stroke: var(--s-line-2); stroke-width: 1; }
.sb-plot .ref { stroke: var(--s-muted); stroke-width: 1; stroke-dasharray: 4 4; }
.sb-plot .ln { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.sb-plot .ln.f1, .sb-plot .tick-f1 { stroke: var(--f1); }
.sb-plot .ln.f2, .sb-plot .tick-f2 { stroke: var(--f2); }
.sb-plot .ln.f3, .sb-plot .tick-f3 { stroke: var(--f3); }
.sb-plot .xh { stroke: rgba(255, 255, 255, 0.4); stroke-width: 1; }
.sb-plot .dot { stroke: var(--s-bg); stroke-width: 2; }
.sb-plot .dot.f1 { fill: var(--f1); }
.sb-plot .dot.f2 { fill: var(--f2); }
.sb-plot .dot.f3 { fill: var(--f3); }
.sb-tip {
  position: absolute; top: 6px; min-width: 12.5em; padding: 8px 10px; pointer-events: none;
  border-radius: 8px; border: 1px solid var(--s-line-2); background: #101a26; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  font: 0.72rem var(--font-mono); color: var(--s-text);
}
.sb-tip[hidden] { display: none; }
.sb-tip .when { color: var(--s-muted); margin-bottom: 4px; }
.sb-tip .row { display: flex; align-items: center; gap: 8px; line-height: 1.6; }
.sb-tip .row i { flex: none; width: 12px; border-top: 2px solid; }
.sb-tip .row b { min-width: 3.2em; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.sb-tip .row span { color: var(--s-muted); }

.sb-table-wrap { margin-top: 20px; overflow-x: auto; }
.sb-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
.sb-table caption { padding-bottom: 6px; text-align: left; font: 600 0.72rem var(--font-mono); letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.sb-table th, .sb-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: right; white-space: nowrap; }
.sb-table th { font-size: 0.78rem; font-weight: 600; color: var(--muted); }
.sb-table th:first-child, .sb-table td:first-child { text-align: left; padding-left: 0; }
.sb-table td { font-family: var(--font-mono); font-variant-numeric: tabular-nums; }
.sb-table td:first-child { font-family: var(--font-sans); }
.sb-table td:first-child .key { margin-right: 8px; vertical-align: middle; }
.sb-table .warn { color: var(--amber); }

@media (prefers-reduced-motion: reduce) {
  .stage .wire.on, .stage .wire.on-violet { animation: none; }
  .stage-caption.swap { animation: none; }
}
