:root {
  --bg: #fbfaf7;
  --panel: #ffffff;
  --ink: #17202f;
  --muted: #5d6675;
  --line: #e4e1da;
  --accent: #d9820b;
  --accent-ink: #ffffff;
  --navy: #14213d;
  --code-bg: #f3f1ec;
  --error: #b3261e;
  --error-bg: #fdecea;
  --shadow: 0 1px 2px rgba(20, 33, 61, .06), 0 4px 16px rgba(20, 33, 61, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1522;
    --panel: #161e2e;
    --ink: #e8eaf0;
    --muted: #9aa3b5;
    --line: #273149;
    --accent: #fca311;
    --accent-ink: #14213d;
    --code-bg: #1d2638;
    --error: #ff8a80;
    --error-bg: #3a1d1d;
    --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
a { color: var(--accent); }
code, kbd, pre, textarea { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
code { background: var(--code-bg); padding: .1em .35em; border-radius: 4px; font-size: .92em; }
kbd { border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 .35em; font-size: .85em; }
.muted { color: var(--muted); }

.bar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--ink); font-weight: 700; font-size: 18px; }
.bar nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.bar nav a { color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: 8px; white-space: nowrap; }
.bar nav a:hover, .bar nav a.active { color: var(--ink); background: var(--code-bg); }
.status { font-size: 13px; color: var(--muted); white-space: nowrap; }
.status.ready::before { content: "●"; color: #2e9d5b; margin-right: 6px; }
.status.busy::before { content: "●"; color: var(--accent); margin-right: 6px; }

.view { flex: 1; min-height: 0; }
#repl { display: flex; flex-direction: column; }
#repl[hidden], .view[hidden], .completions[hidden] { display: none; }
.transcript { flex: 1; overflow-y: auto; padding: 16px; max-width: 920px; width: 100%; margin: 0 auto; }
.welcome h1 { font-size: clamp(26px, 5vw, 38px); line-height: 1.15; margin: 12px 0 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0; }
.chip {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  border-radius: 999px; padding: 6px 12px; font: 14px ui-monospace, Menlo, monospace; cursor: pointer;
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip:hover { border-color: var(--accent); }

.entry { margin: 14px 0; }
.entry .in {
  display: flex; gap: 10px; align-items: baseline; cursor: pointer;
  font: 15px/1.5 ui-monospace, Menlo, monospace; white-space: pre-wrap; word-break: break-word; color: var(--muted);
}
.entry .in::before { content: "›"; color: var(--accent); font-weight: 700; }
.out { margin: 6px 0 0 18px; overflow-x: auto; }
.out pre { margin: 0; white-space: pre; font-size: 14px; }
.out .notes {
  margin: 6px 0 4px; padding: 6px 12px; border-left: 3px solid var(--line); color: var(--muted);
  font-size: 14px; line-height: 1.5;
}
.out .notes p { margin: .3em 0; }
.out .notes table { border-collapse: collapse; font-size: 13px; }
.out .notes td, .out .notes th { border: 1px solid var(--line); padding: 2px 6px; }
.out .notes pre { font-size: 12.5px; }
.out .katex-display { margin: .3em 0; text-align: left; }
.out .katex-display > .katex { text-align: left; }
.out .error { color: var(--error); background: var(--error-bg); border-radius: 8px; padding: 8px 12px; font: 14px ui-monospace, Menlo, monospace; white-space: pre-wrap; }
.plot { background: #fff; border-radius: 10px; border: 1px solid var(--line); overflow: hidden; max-width: 680px; }
.plot svg { display: block; width: 100%; height: auto; }
.plot .js-plotly-plot { width: 100%; }
.plot-3d { max-width: 680px; }

.prompt {
  position: relative; display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap;
  padding: 12px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--panel);
}
.prompt textarea {
  flex: 1 1 300px; min-width: 0; resize: none; max-height: 40vh;
  font-size: 16px; line-height: 1.45; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg); color: var(--ink);
}
.prompt textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.prompt-actions { display: flex; gap: 6px; }
button, .button {
  font: inherit; font-size: 15px; border-radius: 10px; padding: 9px 14px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel); color: var(--ink); text-decoration: none; display: inline-block;
}
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost:hover, .button.ghost:hover { border-color: var(--accent); }
button:disabled { opacity: .55; cursor: default; }
.completions {
  position: absolute; bottom: 100%; left: 16px; right: 16px; max-height: 40vh; overflow-y: auto;
  display: flex; flex-wrap: wrap; gap: 6px; padding: 8px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
}

.page { max-width: 860px; margin: 0 auto; padding: 16px 16px 64px; overflow-wrap: break-word; }
.page h1 { font-size: clamp(26px, 5vw, 36px); line-height: 1.2; }
.lead { color: var(--muted); font-size: 18px; }
.group { margin: 28px 0; }
.group h2 { margin-bottom: 4px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; margin-top: 12px; }
.card {
  display: block; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); color: var(--ink); text-decoration: none; box-shadow: var(--shadow);
}
.card:hover { border-color: var(--accent); }
.card small { color: var(--muted); display: block; }

.lesson-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.lesson-tools { display: flex; gap: 6px; }
.crumb { color: var(--muted); text-decoration: none; }
article .md img { max-width: 100%; }
article .md table { border-collapse: collapse; display: block; overflow-x: auto; }
article .md th, article .md td { border: 1px solid var(--line); padding: 4px 8px; }
article .md .katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.cell { margin: 14px 0; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); overflow: hidden; }
.cell textarea {
  display: block; width: 100%; border: 0; resize: none; background: var(--code-bg); color: var(--ink);
  font-size: 15px; line-height: 1.45; padding: 10px 12px; overflow: hidden;
}
.cell textarea:focus { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell .cell-bar { display: flex; justify-content: space-between; align-items: center; padding: 6px 8px 6px 12px; gap: 8px; }
.cell .cell-bar span { font-size: 13px; color: var(--muted); }
.cell .cell-bar button { padding: 5px 12px; font-size: 14px; }
.cell .out { margin: 0; padding: 0 12px 12px; }
.cell .out:empty { display: none; }
.out .note { color: var(--muted); font-size: 14px; margin-top: 4px; }
#lesson-body .hint { color: var(--muted); font-size: 14px; border-left: 3px solid var(--accent); padding: 4px 0 4px 12px; margin: 0 0 18px; }
.pager { display: flex; justify-content: space-between; gap: 10px; margin-top: 28px; flex-wrap: wrap; }

.toast {
  position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 20; font-size: 14px;
}
@media (max-width: 600px) {
  .hide-sm { display: none; }
  .bar { gap: 8px; }
  .brand span { display: none; }
  .prompt-actions { width: 100%; justify-content: flex-end; }
  .out { margin-left: 0; }
}

/* ---------- files ---------- */
.files-toggle { font: inherit; font-size: 14px; color: var(--muted); background: none; border: 1px solid var(--line); border-radius: 8px; padding: 4px 10px; cursor: pointer; white-space: nowrap; }
.files-toggle:hover, .files-toggle[aria-expanded="true"] { color: var(--ink); border-color: var(--accent); }
.files-toggle .count:not(:empty) { display: inline-block; min-width: 18px; margin-left: 4px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: var(--accent-ink); font-size: 12px; text-align: center; }
.files { position: fixed; z-index: 20; top: 64px; right: 16px; width: min(360px, calc(100vw - 32px)); max-height: calc(100vh - 96px); overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 8px 30px rgba(20, 33, 61, .18); padding: 12px 14px; }
.files-head { display: flex; align-items: center; gap: 8px; }
.files-head strong { flex: 1; }
.files .add { font-size: 20px; line-height: 1; width: 32px; height: 32px; padding: 0; border-radius: 8px; }
.icon { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 15px; padding: 4px 6px; border-radius: 6px; }
.icon:hover { color: var(--ink); background: var(--code-bg); }
.small { font-size: 13px; }
.file-list { list-style: none; margin: 8px 0; padding: 0; }
.file-list li { display: flex; align-items: center; gap: 8px; padding: 4px 0; border-bottom: 1px solid var(--line); }
.file-name { flex: 1; min-width: 0; text-align: left; background: none; border: none; color: var(--ink); font: 14px ui-monospace, "SF Mono", Menlo, Consolas, monospace; padding: 4px 0; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-name:hover { color: var(--accent); }
.file-rename { flex: 1; min-width: 0; font: 14px ui-monospace, "SF Mono", Menlo, Consolas, monospace; padding: 3px 6px; border: 1px solid var(--accent); border-radius: 6px; background: var(--bg); color: var(--ink); }
.file-meta { font-size: 12px; color: var(--muted); white-space: nowrap; }
.file-actions { display: flex; }
.files-foot { display: flex; align-items: flex-end; gap: 8px; }
.files-foot p { flex: 1; margin: 4px 0 0; }
.file-chip { display: flex; align-items: center; gap: 10px; margin: 6px 0; font-size: 14px; }
.plot-downloads { display: flex; align-items: center; gap: 4px; margin: 4px 0 8px; font-size: 13px; }
button.link { background: none; border: none; color: var(--accent); cursor: pointer; font: inherit; padding: 2px 6px; border-radius: 6px; }
button.link:hover { background: var(--code-bg); text-decoration: underline; }
.modal { position: fixed; inset: 0; z-index: 40; background: rgba(15, 21, 34, .45); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-box { background: var(--panel); color: var(--ink); border-radius: 12px; padding: 18px 20px; max-width: 420px; width: 100%; box-shadow: 0 8px 30px rgba(0, 0, 0, .25); }
.modal-box p { margin: 0 0 14px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
@media (max-width: 600px) { .files { top: 56px; right: 8px; width: calc(100vw - 16px); } }
