:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --sunken: #efede7;
  --ink: #1e2227;
  --muted: #5f6670;
  --faint: #8b9099;
  --rule: #dcd8ce;
  --rule-strong: #c7c2b5;
  --accent: #2456c4;
  --accent-soft: #e4ebfa;
  --accent-ink: #ffffff;
  --pass: #1d7a3a;
  --pass-soft: #e3f2e7;
  --fail: #b8352b;
  --fail-soft: #fbe8e5;
  --warn: #8f5f00;
  --warn-soft: #fbf0d9;
  --selection: #c9d7f5;
  --active-line: #f3f1ea;
  --tok-keyword: #7b3fa0;
  --tok-string: #2b7a3d;
  --tok-number: #a64d12;
  --tok-comment: #858b94;
  --tok-def: #1f5cc0;
  --tok-class: #8a5a00;
  --tok-operator: #4b5058;
  --tok-punct: #6b717a;
  --tok-bool: #a64d12;
  --sans: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --mono: "Cascadia Mono", "Cascadia Code", Consolas, "JetBrains Mono", "DejaVu Sans Mono", ui-monospace, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171a;
    --surface: #1b1e23;
    --sunken: #202329;
    --ink: #e4e6e9;
    --muted: #a0a6ae;
    --faint: #7c838c;
    --rule: #2d3138;
    --rule-strong: #3b4048;
    --accent: #7ea6ff;
    --accent-soft: #1f2b45;
    --accent-ink: #0f1420;
    --pass: #5cc98a;
    --pass-soft: #17301f;
    --fail: #ff8a80;
    --fail-soft: #3a1d1b;
    --warn: #e6b450;
    --warn-soft: #362a12;
    --selection: #2f4470;
    --active-line: #1f2228;
    --tok-keyword: #d3a6ff;
    --tok-string: #9fd49a;
    --tok-number: #f2b27c;
    --tok-comment: #7f8791;
    --tok-def: #82b4ff;
    --tok-class: #e8c178;
    --tok-operator: #c3c8cf;
    --tok-punct: #9aa1aa;
    --tok-bool: #f2b27c;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #15171a;
  --surface: #1b1e23;
  --sunken: #202329;
  --ink: #e4e6e9;
  --muted: #a0a6ae;
  --faint: #7c838c;
  --rule: #2d3138;
  --rule-strong: #3b4048;
  --accent: #7ea6ff;
  --accent-soft: #1f2b45;
  --accent-ink: #0f1420;
  --pass: #5cc98a;
  --pass-soft: #17301f;
  --fail: #ff8a80;
  --fail-soft: #3a1d1b;
  --warn: #e6b450;
  --warn-soft: #362a12;
  --selection: #2f4470;
  --active-line: #1f2228;
  --tok-keyword: #d3a6ff;
  --tok-string: #9fd49a;
  --tok-number: #f2b27c;
  --tok-comment: #7f8791;
  --tok-def: #82b4ff;
  --tok-class: #e8c178;
  --tok-operator: #c3c8cf;
  --tok-punct: #9aa1aa;
  --tok-bool: #f2b27c;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::selection { background: var(--selection); }
code, pre, kbd { font-family: var(--mono); }
[hidden] { display: none !important; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--rule-strong); background: var(--surface); color: var(--ink);
  padding: 6px 14px; border-radius: 6px; cursor: pointer; font-size: 15px; line-height: 1.4;
  white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.quiet { border-color: transparent; background: transparent; color: var(--muted); padding: 4px 8px; }
.btn.quiet:hover { color: var(--ink); background: var(--sunken); }
.btn.danger { color: var(--fail); }
kbd { font-size: 12px; border: 1px solid var(--rule-strong); border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; color: var(--muted); }

/* Reading pages */
.page { max-width: 820px; margin: 0 auto; padding: 40px 20px 80px; }
.page h1 { font-size: 30px; line-height: 1.2; margin: 0 0 12px; letter-spacing: -.01em; }
.page h2 { font-size: 21px; margin: 44px 0 10px; letter-spacing: -.005em; }
.page h3 { font-size: 17px; margin: 28px 0 8px; }
.page h4 { font-size: 16px; margin: 22px 0 6px; }
.lead { color: var(--muted); margin: 0 0 8px; max-width: 64ch; }
.crumbs { font-size: 14px; margin-bottom: 20px; color: var(--muted); }
.crumbs a { color: var(--muted); }

.prose { max-width: 70ch; }
.prose p, .prose li { margin: 0 0 10px; }
.prose ul, .prose ol { padding-left: 22px; margin: 0 0 12px; }
.prose code { background: var(--sunken); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.prose pre.code {
  background: var(--sunken); border-radius: 6px; padding: 12px 14px; overflow-x: auto;
  font-size: 14px; line-height: 1.5; margin: 0 0 14px;
}
.prose pre.code code { background: none; padding: 0; font-size: inherit; }
.prose blockquote { margin: 0 0 14px; padding: 2px 0 2px 14px; border-left: 3px solid var(--rule-strong); color: var(--ink); }
.prose table { border-collapse: collapse; margin: 0 0 14px; font-size: 15px; display: block; max-width: 100%; overflow-x: auto; }
.prose th, .prose td { text-align: left; padding: 5px 14px 5px 0; border-bottom: 1px solid var(--rule); vertical-align: top; }
.prose th { font-weight: 600; color: var(--muted); }
.prose h3 { font-size: 17px; margin: 22px 0 8px; }
.prose h4 { font-size: 15px; margin: 18px 0 6px; }

/* Calm lists with fine rules */
.rows { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--rule); }
.rows > li { border-bottom: 1px solid var(--rule); }
.row {
  display: grid; grid-template-columns: 7.5rem 1fr auto; gap: 16px; align-items: baseline;
  padding: 11px 4px; color: var(--ink);
}
a.row:hover { background: var(--sunken); text-decoration: none; }
.row .k { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 15px; }
.row .v { min-width: 0; }
.row .v small { display: block; color: var(--muted); font-size: 14px; margin-top: 2px; }
.row .s { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.row.current { box-shadow: inset 3px 0 0 var(--accent); padding-left: 12px; }
.row .s.done { color: var(--pass); }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--rule-strong); vertical-align: 1px; }
.status-dot.passed { background: var(--pass); }
.status-dot.tried { background: var(--warn); }

.tasks { list-style: none; margin: 8px 0 0; padding: 0; border-top: 1px solid var(--rule); }
.tasks > li { border-bottom: 1px solid var(--rule); }
.tasks a { display: grid; grid-template-columns: 3.4rem 1fr auto; gap: 12px; padding: 9px 4px; color: var(--ink); align-items: baseline; }
.tasks a:hover { background: var(--sunken); text-decoration: none; }
.tasks .n { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 15px; }
.tasks .st { color: var(--muted); font-size: 14px; white-space: nowrap; }
.tasks .st.passed { color: var(--pass); }

.today {
  margin: 28px 0 8px; padding: 18px 0 20px; border-top: 1px solid var(--rule); border-bottom: 1px solid var(--rule);
}
.today h2 { margin: 0 0 6px; }
.today p { margin: 0 0 14px; color: var(--muted); max-width: 64ch; }
.meter { height: 4px; background: var(--rule); border-radius: 2px; overflow: hidden; margin: 0 0 16px; max-width: 420px; }
.meter > i { display: block; height: 100%; background: var(--pass); border-radius: 2px; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.actions .btn { white-space: normal; text-align: left; }

.settings { display: grid; grid-template-columns: 12rem 1fr; gap: 12px 20px; align-items: center; border-top: 1px solid var(--rule); padding-top: 16px; }
.settings label { color: var(--muted); }
.settings select, .settings input[type="date"] { background: var(--surface); border: 1px solid var(--rule-strong); border-radius: 6px; padding: 4px 8px; }
.note { color: var(--muted); font-size: 14px; }
.foot { margin-top: 56px; padding-top: 14px; border-top: 1px solid var(--rule); color: var(--muted); font-size: 14px; display: flex; gap: 16px; flex-wrap: wrap; justify-content: space-between; }
.pystate.ready { color: var(--pass); }
.pystate.error { color: var(--fail); }
.pager { display: flex; justify-content: space-between; margin-top: 40px; padding-top: 14px; border-top: 1px solid var(--rule); gap: 16px; }

/* Workspace */
.ws { height: 100vh; display: flex; flex-direction: column; overflow: hidden; }
.ws-bar {
  display: flex; align-items: center; gap: 14px; padding: 8px 14px; border-bottom: 1px solid var(--rule);
  background: var(--bg); min-height: 52px; flex-wrap: wrap;
}
.ws-bar .back { color: var(--muted); font-size: 14px; white-space: nowrap; }
.ws-bar h1 { font-size: 16px; margin: 0; font-weight: 600; flex: 1 1 auto; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ws-bar .tools { display: flex; align-items: center; gap: 8px; }
.timer { font-variant-numeric: tabular-nums; font-family: var(--mono); font-size: 15px; color: var(--muted); padding: 0 6px; white-space: nowrap; }
.timer.over { color: var(--warn); }
.timer.critical { color: var(--fail); font-weight: 600; }
.ws-main { flex: 1 1 auto; display: flex; min-height: 0; }
.ws-left { flex: 0 0 auto; width: 38%; min-width: 260px; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.ws-right { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.split-v { flex: 0 0 6px; cursor: col-resize; background: var(--bg); border-left: 1px solid var(--rule); }
.split-v:hover, .split-h:hover, .split-v.drag, .split-h.drag { background: var(--accent-soft); }
.split-h { flex: 0 0 6px; cursor: row-resize; background: var(--bg); border-top: 1px solid var(--rule); }

.tabs { display: flex; gap: 2px; padding: 0 10px; border-bottom: 1px solid var(--rule); flex: 0 0 auto; overflow-x: auto; }
.tab {
  border: none; background: none; padding: 10px 10px 9px; cursor: pointer; color: var(--muted); font-size: 15px;
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }
.tab:disabled { opacity: .45; cursor: default; }
.panel { flex: 1 1 auto; overflow: auto; padding: 18px 20px 40px; min-height: 0; }
.panel.code-panel { padding: 0; display: flex; flex-direction: column; }
.panel h2 { font-size: 19px; margin: 0 0 10px; }
.panel.prose pre.code { font-size: 13.5px; }
.panel details { margin-top: 22px; border-top: 1px solid var(--rule); padding-top: 10px; }
.panel summary { cursor: pointer; color: var(--muted); font-weight: 600; padding: 4px 0; }
.panel .meta { color: var(--muted); font-size: 14px; margin: -4px 0 14px; }
.hint { border-top: 1px solid var(--rule); padding: 12px 0 2px; }
.hint .hn { color: var(--muted); font-size: 14px; margin-bottom: 4px; }
.locked { color: var(--muted); }
.buglist { list-style: none; padding: 0; margin: 8px 0 18px; border-top: 1px solid var(--rule); }
.buglist li { display: grid; grid-template-columns: 2rem 1fr; padding: 8px 0; border-bottom: 1px solid var(--rule); }
.buglist li > span { color: var(--muted); font-variant-numeric: tabular-nums; }
.buglist li p { margin: 0; }

.file-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 4px 8px 4px 14px; border-bottom: 1px solid var(--rule); background: var(--bg); flex: 0 0 auto; }
.file-head .fname { font-family: var(--mono); font-size: 14px; color: var(--muted); }
.editor-box { flex: 1 1 60%; min-height: 120px; background: var(--surface); position: relative; overflow: hidden; }
.editor-box .cm-editor { height: 100%; }
.code-panel .viewer { flex: 1 1 auto; min-height: 0; background: var(--surface); overflow: hidden; }
.code-panel .viewer .cm-editor { height: 100%; }
.fallback-editor {
  width: 100%; height: 100%; border: 0; resize: none; padding: 10px 14px; background: var(--surface); color: var(--ink);
  font-family: var(--mono); font-size: 15px; line-height: 1.5; tab-size: 4; outline: none; white-space: pre; overflow: auto;
}
.loading-box { padding: 16px; color: var(--muted); }

.results { flex: 1 1 40%; min-height: 120px; display: flex; flex-direction: column; background: var(--bg); }
.results-head { display: flex; align-items: center; gap: 12px; padding: 6px 14px; border-bottom: 1px solid var(--rule); flex: 0 0 auto; }
.results-head .sum { font-weight: 600; font-variant-numeric: tabular-nums; }
.results-head .sum.all { color: var(--pass); }
.results-head .sum.some { color: var(--fail); }
.results-head .bar { flex: 1 1 auto; max-width: 260px; height: 4px; background: var(--fail-soft); border-radius: 2px; overflow: hidden; }
.results-head .bar > i { display: block; height: 100%; background: var(--pass); }
.results-head .bar.empty { background: var(--rule); }
.results-head .spacer { flex: 1 1 auto; }
.results-body { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
.test-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1 1 55%; min-height: 40px; }
.t { border-bottom: 1px solid var(--rule); }
.t-row {
  width: 100%; display: grid; grid-template-columns: 1.4rem 1fr auto; gap: 8px; align-items: baseline;
  padding: 5px 14px; border: 0; background: none; text-align: left; cursor: pointer; font-family: var(--mono); font-size: 14px;
}
.t-row:hover { background: var(--sunken); }
.t-icon { font-weight: 700; }
.t-pass .t-icon { color: var(--pass); }
.t-fail .t-icon, .t-error .t-icon, .t-timeout .t-icon { color: var(--fail); }
.t-pending .t-icon, .t-running .t-icon { color: var(--faint); }
.t-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.t-name .cls { color: var(--faint); }
.t-time { color: var(--faint); font-size: 12px; }
.t-detail { padding: 2px 14px 12px 36px; }
.t-detail pre {
  margin: 6px 0 0; padding: 10px 12px; background: var(--fail-soft); border-radius: 6px; white-space: pre-wrap; word-break: break-word;
  font-size: 13px; line-height: 1.45; max-height: 340px; overflow: auto;
}
.t-detail pre.out { background: var(--sunken); }
.t-detail .lbl { font-size: 12px; color: var(--muted); margin-top: 8px; }
.t-detail .btn { margin-top: 8px; font-size: 13px; padding: 3px 10px; }
.tb-link { color: var(--accent); cursor: pointer; text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }

.console { flex: 1 1 45%; min-height: 60px; display: flex; flex-direction: column; border-top: 1px solid var(--rule); }
.console-head { display: flex; align-items: center; gap: 8px; padding: 4px 8px 4px 14px; font-size: 14px; color: var(--muted); flex: 0 0 auto; }
.console-head .spacer { flex: 1 1 auto; }
.console pre { margin: 0; padding: 6px 14px 14px; overflow: auto; flex: 1 1 auto; font-size: 13.5px; line-height: 1.45; white-space: pre-wrap; word-break: break-word; }
.console .sep { color: var(--faint); }
.console .err { color: var(--fail); }
.stdin-box { padding: 0 14px 8px; flex: 0 0 auto; }
.stdin-box textarea {
  width: 100%; min-height: 64px; max-height: 140px; resize: vertical; border: 1px solid var(--rule-strong); border-radius: 6px;
  background: var(--surface); font-family: var(--mono); font-size: 13.5px; padding: 6px 8px;
}
.stdin-box .note { margin: 2px 0 0; font-size: 13px; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 8px; font-size: 15px; pointer-events: none;
  transition: opacity .18s ease, transform .18s ease; max-width: min(560px, calc(100vw - 32px)); z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Syntax colours (classHighlighter) */
.tok-keyword { color: var(--tok-keyword); }
.tok-string, .tok-string2 { color: var(--tok-string); }
.tok-number { color: var(--tok-number); }
.tok-bool, .tok-atom { color: var(--tok-bool); }
.tok-comment { color: var(--tok-comment); font-style: italic; }
.tok-variableName.tok-definition, .tok-propertyName.tok-definition { color: var(--tok-def); }
.tok-className, .tok-typeName { color: var(--tok-class); }
.tok-operator { color: var(--tok-operator); }
.tok-punctuation { color: var(--tok-punct); }
.tok-variableName2 { color: var(--tok-class); }
.tok-invalid { color: var(--fail); }

/* Exam */
.score { font-size: 44px; font-weight: 700; letter-spacing: -.02em; line-height: 1.1; margin: 8px 0 4px; font-variant-numeric: tabular-nums; }
.score small { font-size: 20px; color: var(--muted); font-weight: 500; }
.rules { margin: 0 0 20px; padding-left: 22px; }
.rules li { margin-bottom: 6px; }

@media (max-width: 900px) {
  .ws { height: auto; min-height: 100vh; overflow: visible; }
  .ws-main { flex-direction: column; }
  .ws-left { width: 100% !important; min-width: 0; max-height: 46vh; border-bottom: 1px solid var(--rule); }
  .split-v, .split-h { display: none; }
  .editor-box { flex: none; height: 56vh; }
  .results { flex: none; min-height: 300px; }
  .row { grid-template-columns: 5.5rem 1fr auto; gap: 10px; }
  .settings { grid-template-columns: 1fr; }
}
@media (max-width: 560px) {
  .page { padding: 24px 16px 60px; }
  .page h1 { font-size: 25px; }
  .row { grid-template-columns: 1fr auto; }
  .row .k { grid-column: 1 / -1; }
  .ws-bar .tools { width: 100%; justify-content: flex-end; flex-wrap: wrap; }
}
