:root {
  --bg: #EEEFE9;
  --card: #FDFDF8;
  --ink: #23251D;
  --muted: #65675E;
  --rule: #BFC1B7;
  --chip: #E5E7E0;
  --orange: #F54E00;
  --blue: #1D4AFF;
  --green: #2F8F46;
  --red: #D42F2F;
  --yellow: #F7A501;
  --shadow: 3px 3px 0 var(--ink);
  --bar-h: 64px;              /* the sidebar head and the chapter bar share one height, so their rules line up */
  font-family: "Avenir Next", "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }

/* ---- app shell: sidebar + main ------------------------------------------------------------------ */
.app { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.body { flex: 1; display: flex; min-height: 0; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

/* The fixed app bar: brand on the left (as wide as the sidebar, so the two columns line up), chapter bar on the right. */
.appbar {
  flex: none; display: flex; height: var(--bar-h); background: var(--card); border-bottom: 1.5px solid var(--ink);
  position: sticky; top: 0; z-index: 50;
}

.sidebar {
  flex: 0 0 280px; display: flex; flex-direction: column; min-height: 0;
  background: var(--card); border-right: 1.5px solid var(--ink);
}
.app.no-sidebar .sidebar { display: none; }
.side-head {
  flex: none; width: 280px; box-sizing: border-box; display: flex; flex-direction: column; justify-content: center;
  gap: 1px; padding: 0 16px; border-right: 1.5px solid var(--ink); color: var(--ink); text-decoration: none;
}
.side-head:focus-visible { outline: 2px solid var(--blue); outline-offset: -3px; }
.app.no-sidebar .side-head { width: auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; letter-spacing: -0.2px; }
.brand b { color: var(--orange); }
.logo {
  width: 28px; font-size: 13px; height: 26px; border-radius: 6px; background: var(--orange); color: white;
  display: grid; place-items: center; font-weight: 800; border: 1.5px solid var(--ink);
}
.course { margin: 0; padding-left: 34px; font-size: 13px; line-height: 1.2; color: var(--muted); }
.toc { flex: 1; overflow-y: auto; padding: 6px 0 14px; scrollbar-width: thin; }
.toc-group { margin-top: 10px; }
.toc-group h2 {
  display: flex; justify-content: space-between; margin: 0; padding: 6px 16px 4px;
  font-size: 12.5px; font-weight: 700; color: var(--muted); letter-spacing: 0;
}
.toc-group h2 span { font-weight: 500; font-variant-numeric: tabular-nums; }
.toc-item {
  display: grid; grid-template-columns: 26px 1fr auto; align-items: start; gap: 10px;
  padding: 7px 14px 7px 16px; color: var(--ink); text-decoration: none; font-size: 13.5px; line-height: 1.3;
  border-left: 4px solid transparent;
}
.toc-item:hover { background: var(--chip); }
.toc-item:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; }
.toc-item .n {
  width: 26px; height: 22px; display: grid; place-items: center; border-radius: 5px;
  font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--muted);
  border: 1.5px solid var(--rule); background: var(--bg);
}
.toc-item .t { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; padding-top: 2px; }
.toc-item .tag {
  font-size: 11px; font-weight: 700; padding: 2px 6px; border-radius: 5px; margin-top: 1px;
  border: 1.5px solid var(--ink); background: #FFE3D4; color: #9C3200;
}
.toc-item.lab .n { border-color: var(--ink); color: var(--ink); background: var(--card); }
.toc-item.active { background: var(--bg); border-left-color: var(--orange); font-weight: 600; }
.toc-item.active .n { background: var(--orange); border-color: var(--ink); color: white; }
.toc-foot { margin: 0; padding: 10px 16px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--rule); }
.scrim { display: none; }

/* ---- chapter bar + controls ------------------------------------------------------------------ */
.chapterbar {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 12px; padding: 0 16px;
}
.chapter-id { display: flex; align-items: center; gap: 12px; min-width: 0; }
.chnum {
  flex: none; min-width: 38px; height: 38px; padding: 0 6px; display: grid; place-items: center;
  font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; color: white;
  background: var(--orange); border: 1.5px solid var(--ink); border-radius: 8px; box-shadow: var(--shadow);
}
.chapter-id h1 { margin: 0; font-size: 18px; line-height: 1.2; font-weight: 700; letter-spacing: -0.2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.controls {
  display: flex; align-items: center; gap: 18px; flex-wrap: wrap; padding: 10px 16px 0;
}
.controls[hidden] { display: none; }

/* segmented control: every choice visible, one click to switch */
.seg { display: inline-flex; border: 1.5px solid var(--ink); border-radius: 7px; background: var(--card); overflow: hidden; }
.seg button {
  font: inherit; font-size: 13px; font-weight: 500; padding: 5px 11px; border: 0; background: transparent; color: var(--ink);
  cursor: pointer; border-right: 1px solid var(--rule); white-space: nowrap;
}
.seg button:last-child { border-right: 0; }
.seg button:hover { background: var(--chip); }
.seg button[aria-checked="true"] { background: var(--ink); color: var(--card); font-weight: 600; }
.seg button:focus-visible { outline: 2px solid var(--blue); outline-offset: -3px; }

.btn {
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer; border-radius: 6px; padding: 6px 12px;
  border: 1.5px solid var(--ink); background: var(--card); color: var(--ink); display: inline-flex; gap: 8px; align-items: center;
  text-decoration: none;
}
.btn.icon { padding: 6px; border-color: var(--rule); }
.btn.primary { background: var(--orange); color: white; box-shadow: var(--shadow); }
.btn.primary:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--ink); }
.btn.primary:disabled { opacity: 0.6; cursor: progress; }
.btn.ghost { border-color: var(--rule); }
.btn.ghost.on { border-color: var(--ink); background: var(--chip); }
.btn:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
kbd { font-family: Menlo, monospace; font-size: 11px; opacity: 0.85; }
.status { font-size: 13px; color: var(--muted); font-family: Menlo, monospace; }
.status.ok { color: var(--green); }
.status.err { color: var(--red); }
.status.busy { color: var(--blue); }
.spacer { flex: 1; }

/* ---- chapter mode: the guide's pages + video + outline ------------------------------------------ */
.seg.mode button { padding: 6px 14px; font-size: 14px; }
.seg.mode button[aria-selected="true"] { background: var(--ink); color: var(--card); font-weight: 600; }
.seg.mode button:disabled { color: var(--rule); cursor: not-allowed; background: transparent; }

/* Chapter mode: the video leads. The guide opens beside it on request (.with-pdf); the whole-guide view is PDF-only. */
.reader {
  flex: 1; min-height: 0; display: grid; gap: 16px; padding: 12px 16px 16px;
  grid-template-columns: minmax(0, 1fr) 340px; grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "video side" ". side"; align-content: start;
}
.reader[hidden] { display: none; }
.rs-video { grid-area: video; background: #000; align-self: start; }
.rs-video[hidden] { display: none; }
.rs-video video { display: block; width: 100%; aspect-ratio: 16 / 9; max-height: calc(100vh - 150px); background: #000; }
.reader-side { grid-area: side; }
.pdf-panel { grid-area: pdf; }
.reader.with-pdf {
  grid-template-columns: minmax(360px, 38%) minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "video pdf" "side pdf";
}
.reader.guide-view { grid-template-columns: 340px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); grid-template-areas: "side pdf"; }
.reader.with-pdf .reader-side, .reader.guide-view .reader-side { min-height: 0; }
.pdf-panel { min-height: 0; }
.pdf-panel[hidden] { display: none; }
.panel-head.pdf-bar { align-items: center; gap: 10px; padding-right: 8px; overflow: visible; }
.pdf-page { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.seg.zoom button { padding: 3px 10px; font-size: 13px; }
.btn.small { font-size: 13px; padding: 4px 10px; }
.pdf-stage { position: relative; flex: 1; min-height: 0; background: #DCDDD5; }
.pdf-container { position: absolute; inset: 0; overflow: auto; }
.pdf-container:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; }
.pdfViewer { padding: 14px 0 24px; }
.pdfViewer .page { margin: 0 auto 14px; border: 1.5px solid var(--ink); box-shadow: var(--shadow); box-sizing: content-box; }
/* pages outside the current chapter are dimmed, so the chapter reads as one block inside the whole guide */
.pdfViewer .page.outside { opacity: 0.55; }
.pdf-loading { padding: 40px; color: var(--muted); }

.reader-side { display: flex; flex-direction: column; gap: 16px; min-height: 0; overflow: auto; padding: 0 4px 4px 0; }
.rs-outline { flex: none; }
.outline { margin: 0; padding: 6px 0 8px; list-style: none; }
.outline li { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: stretch; border-left: 4px solid transparent; }
.outline li.here { border-left-color: var(--orange); background: var(--bg); }
.outline .seek {
  display: grid; grid-template-columns: 38px minmax(0, 1fr) auto; gap: 4px; align-items: baseline; padding: 7px 8px 7px 12px;
  font: inherit; font-size: 14px; line-height: 1.35; text-align: left; color: var(--ink); background: none; border: 0; cursor: pointer;
}
.outline .seek:hover { background: var(--chip); }
.outline .sn { font-weight: 700; color: var(--orange); font-variant-numeric: tabular-nums; }
.outline .tm { color: var(--muted); font-size: 12.5px; font-variant-numeric: tabular-nums; padding-left: 8px; }
.outline .pg {
  align-self: center; margin-right: 10px; padding: 1px 6px; border-radius: 5px; font-size: 12px; color: var(--muted);
  text-decoration: none; border: 1px solid var(--rule); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.outline .pg:hover { color: var(--ink); border-color: var(--ink); background: var(--card); }
.outline .seek:focus-visible, .outline .pg:focus-visible { outline: 2px solid var(--blue); outline-offset: -2px; }
.outline .grp { padding: 10px 16px 2px; font-size: 12.5px; font-weight: 700; color: var(--muted); }
.rs-actions { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.rs-actions .btn { justify-content: center; text-align: center; }
.rs-actions [hidden] { display: none; }
.no-lab { margin: 0; font-size: 13.5px; color: var(--muted); }

.sidebar .guide-link { margin-top: 8px; }
.guide-link .n { color: var(--ink); border-color: var(--ink); background: var(--card); }
.guide-link .sub { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }

/* ---- layout ----------------------------------------------------------------------------------- */
.layout { flex: 1; display: flex; min-height: 0; padding: 12px 16px 16px; gap: 0; }
.layout[hidden] { display: none; }
.panel {
  background: var(--card); border: 1.5px solid var(--ink); border-radius: 8px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; min-width: 0; min-height: 0; overflow: hidden;
}
.guide { flex: 0 0 30%; }
.guide.hidden, .guide.hidden + .gutter { display: none; }
.editor-panel { flex: 1 1 35%; }
.output-panel { flex: 1 1 35%; }
.gutter { flex: 0 0 12px; cursor: col-resize; position: relative; }
.gutter::after { content: ""; position: absolute; left: 5px; top: 45%; width: 2px; height: 36px; border-radius: 2px; background: var(--rule); }
.gutter:hover::after { background: var(--orange); }

.panel-head {
  display: flex; align-items: stretch; gap: 0; border-bottom: 1.5px solid var(--ink); background: var(--chip);
  min-height: 36px; overflow-x: auto; scrollbar-width: thin;
}
.kicker { padding: 9px 14px; font-size: 12px; font-weight: 700; color: var(--orange); text-transform: uppercase; letter-spacing: 0.6px; }
.tab {
  font: inherit; font-size: 13px; padding: 8px 14px; border: 0; border-right: 1px solid var(--rule); background: transparent;
  color: var(--muted); cursor: pointer; white-space: nowrap; display: flex; align-items: center; gap: 6px;
}
.tab:hover { color: var(--ink); }
.tab.active { background: var(--card); color: var(--ink); font-weight: 600; box-shadow: inset 0 -3px 0 var(--orange); }
.tab.readonly { font-style: italic; }
.tab .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--orange); }
.badge { font-size: 11px; font-weight: 700; padding: 1px 6px; border-radius: 9px; color: white; background: var(--muted); }
.badge.ok { background: var(--green); }
.badge.err { background: var(--red); }
.monaco { flex: 1; min-height: 0; }

.footer {
  border-top: 1px solid var(--rule); padding: 6px 12px; font-family: Menlo, monospace; font-size: 12px; color: var(--muted);
  max-height: 30%; overflow: auto; white-space: pre-wrap;
}
.footer:empty { display: none; }
.footer .err { color: var(--red); }

/* ---- exercise sheet ------------------------------------------------------------------------- */
.readme { overflow: auto; padding: 4px 20px 24px; font-size: 14.5px; line-height: 1.55; }
.readme h1 { font-size: 21px; line-height: 1.25; margin: 14px 0 6px; }
.readme h2 { font-size: 17px; margin: 22px 0 6px; padding-top: 12px; border-top: 1px dashed var(--rule); }
.readme h3 { font-size: 15px; margin: 16px 0 4px; }
.readme code { font-family: Menlo, monospace; font-size: 12.5px; background: var(--chip); padding: 1px 4px; border-radius: 4px; }
.readme pre { background: #23251D; color: #EEEFE9; padding: 10px 12px; border-radius: 6px; overflow-x: auto; }
.readme pre code { background: none; color: inherit; padding: 0; }
.readme blockquote { margin: 10px 0; padding: 6px 12px; border-left: 4px solid var(--blue); background: #E8ECFF; border-radius: 0 6px 6px 0; }
.readme table { border-collapse: collapse; font-size: 13px; margin: 8px 0; }
.readme th, .readme td { border: 1px solid var(--rule); padding: 4px 8px; text-align: left; vertical-align: top; }
.readme th { background: var(--chip); }
.readme details { margin: 8px 0; padding: 6px 10px; border: 1.5px solid var(--rule); border-radius: 6px; background: #FFF7E6; }
.readme details[open] { border-color: var(--yellow); }
.readme summary { cursor: pointer; font-weight: 600; color: #9A5B00; }
.readme a { color: var(--blue); }
.readme input[type=checkbox] { accent-color: var(--orange); }

/* ---- video ------------------------------------------------------------------------------------- */
.video-dialog { border: 1.5px solid var(--ink); border-radius: 10px; padding: 0; background: #000; box-shadow: 6px 6px 0 var(--ink); max-width: 90vw; }
.video-dialog::backdrop { background: rgba(35, 37, 29, 0.6); }
.video-dialog video { display: block; width: min(1280px, 88vw); aspect-ratio: 16 / 9; }
.video-dialog .close { position: absolute; top: 8px; right: 8px; z-index: 2; background: var(--card); }

@media (max-width: 1100px) {
  .reader, .reader.with-pdf, .reader.guide-view {
    grid-template-columns: minmax(0, 1fr); grid-template-rows: auto; grid-template-areas: "video" "pdf" "side"; overflow: auto;
  }
  .pdf-panel { height: 80vh; }
  .reader-side { overflow: visible; }
}
@media (max-width: 900px) {
  .sidebar { position: fixed; inset: var(--bar-h) auto 0 0; z-index: 40; width: min(300px, 86vw); box-shadow: 6px 0 0 rgba(35, 37, 29, 0.25); }
  .app.no-sidebar .sidebar { display: none; }
  .app:not(.no-sidebar) .scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(35, 37, 29, 0.4); }
  .chapter-id h1 { font-size: 16px; white-space: normal; }
  .appbar { height: auto; min-height: var(--bar-h); }
  .side-head, .app.no-sidebar .side-head { width: auto; }
  .side-head { padding: 0 12px; }
  .side-head .course, .side-head .brand > span:last-child { display: none; }
  .chapterbar { flex-wrap: wrap; padding: 10px 16px; }
  .chapter-id { flex: 1 1 160px; }
  .layout { flex-direction: column; overflow: auto; }
  .guide, .editor-panel, .output-panel { flex: 0 0 auto; height: 70vh; margin-bottom: 12px; }
  .gutter { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}

/* ---- Python lab: task header, output console, stdin, checks ------------------------------------ */
.task-head { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.task-kind {
  flex: none; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.5px; padding: 2px 7px;
  border-radius: 5px; border: 1.5px solid var(--ink); background: #FFE3D4; color: #9C3200;
}
.task-kind.example { background: #E8ECFF; color: #1A3399; }
.task-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab .ok { color: var(--green); font-weight: 800; }
.tab .ex { color: var(--blue); font-size: 11px; font-weight: 700; }

.out-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.out-pane[hidden] { display: none; }
.stdin-box { flex: none; border-bottom: 1px solid var(--rule); padding: 8px 12px 10px; background: var(--bg); }
.stdin-label { display: block; font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 4px; }
.stdin-label .hint { font-weight: 400; }
.stdin-box textarea {
  width: 100%; resize: vertical; min-height: 44px; font: 13px/1.45 Menlo, monospace; color: var(--ink);
  background: var(--card); border: 1.5px solid var(--rule); border-radius: 6px; padding: 6px 8px;
}
.stdin-box textarea:focus { outline: none; border-color: var(--blue); }
.console {
  flex: 1; min-height: 0; overflow: auto; margin: 0; padding: 10px 14px; font: 13.5px/1.5 Menlo, monospace;
  white-space: pre-wrap; word-break: break-word; background: var(--card);
}
.console .err { color: var(--red); }
.console .muted, .muted { color: var(--muted); }
.console .meta { display: block; margin-top: 10px; padding-top: 6px; border-top: 1px dashed var(--rule); color: var(--muted); font-size: 12px; }
.console .echo { color: var(--blue); }

.checks-list { flex: 1; overflow: auto; padding: 10px 12px 16px; }
.checks-list .summary { margin: 2px 0 10px; font-weight: 700; }
.checks-list .summary.ok { color: var(--green); }
.checks-list .summary.err { color: var(--red); }
.case { border: 1.5px solid var(--rule); border-radius: 7px; margin-bottom: 8px; background: var(--card); }
.case > summary {
  list-style: none; cursor: pointer; display: flex; gap: 8px; align-items: baseline; padding: 7px 10px; font-size: 13.5px;
}
.case > summary::-webkit-details-marker { display: none; }
.case .mark { font-weight: 800; }
.case.pass { border-color: #A8D5B3; }
.case.pass .mark { color: var(--green); }
.case.fail { border-color: var(--red); }
.case.fail .mark { color: var(--red); }
.case .why { color: var(--muted); font-size: 12.5px; margin-left: auto; text-align: right; }
.case .body { padding: 0 10px 10px; display: grid; gap: 6px; }
.case .io { display: grid; grid-template-columns: 70px minmax(0, 1fr); gap: 4px 8px; font-size: 12px; }
.case .io b { color: var(--muted); font-weight: 600; padding-top: 4px; }
.case .io pre { margin: 0; padding: 5px 8px; background: var(--bg); border-radius: 5px; font: 12.5px/1.45 Menlo, monospace; white-space: pre-wrap; overflow-x: auto; }
.case .io pre.bad { background: #FBE3E3; }

.toc-item .prog { font-size: 11px; font-weight: 700; color: var(--muted); padding-top: 3px; font-variant-numeric: tabular-nums; }
.toc-item .prog.done { color: var(--green); }
.readme .task-anchor { scroll-margin-top: 8px; }
.readme h2.here { color: var(--orange); }
