:root {
  --bg: #fbfaf7; --fg: #1d1f21; --muted: #6b6f76; --border: #dcd9d2; --surface: #f1efe9;
  --accent: #1f6feb; --error: #c62828; --ok: #2e7d32;
  --kw: #8a3ffc; --kind: #b7410e; --mod: #00796b; --ty: #0b6aa2; --str: #2e7d32; --num: #b26a00;
  --com: #8b8f96; --call: #3b4b9a; --const: #a3215f;
  --mono: ui-monospace, "JetBrains Mono", "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191c; --fg: #e3e3e0; --muted: #9aa0a6; --border: #33373d; --surface: #202328;
    --accent: #6ea8fe; --error: #ff7b72; --ok: #7ee787;
    --kw: #c4a7ff; --kind: #ffa657; --mod: #56d4bc; --ty: #79c0ff; --str: #a5d6a7; --num: #f2cc60;
    --com: #7d848c; --call: #a5b4fc; --const: #ff9bce;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { height: 100dvh; display: flex; flex-direction: column; background: var(--bg); color: var(--fg); font: 15px/1.5 system-ui, sans-serif; }
button, select { font: inherit; font-size: 14px; color: var(--fg); background: var(--surface); border: 1px solid var(--border); border-radius: 5px; padding: 3px 10px; cursor: pointer; }
button:hover, select:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: progress; }

header { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 18px; margin-right: 8px; letter-spacing: .02em; }
#status { color: var(--muted); font-size: 13px; margin-left: auto; }
#tools { display: contents; }
#std-reference { font-size: 14px; color: var(--accent); text-decoration: none; }
#panes { display: none; }

main { flex: 1; display: grid; grid-template-columns: minmax(320px, 40%) 1fr; min-height: 0; }
@media (max-width: 900px) { main { grid-template-columns: 1fr; grid-template-rows: 45% 55%; } }

#tutorial { display: flex; flex-direction: column; border-right: 1px solid var(--border); min-height: 0; }
#tutorial nav { display: flex; gap: 6px; padding: 8px 12px; border-bottom: 1px solid var(--border); }
#tutorial nav select { flex: 1; min-width: 0; }
#lesson-body { overflow-y: auto; padding: 4px 20px 40px; }
#lesson-body h2 { font-size: 20px; }
#lesson-body code { font-family: var(--mono); font-size: 13px; background: var(--surface); padding: 1px 4px; border-radius: 3px; }
#lesson-body table { border-collapse: collapse; font-size: 14px; margin: 8px 0; }
#lesson-body th, #lesson-body td { border: 1px solid var(--border); padding: 3px 8px; text-align: left; }
#lesson-body pre { font-family: var(--mono); font-size: 13px; line-height: 1.45; padding: 10px 12px; border-radius: 6px; overflow-x: auto; margin: 0; }
.program { position: relative; margin: 12px 0 4px; }
.program pre { background: var(--surface); border: 1px solid var(--border); }
.program .load { position: absolute; top: 6px; right: 6px; font-size: 12px; padding: 1px 8px; }
pre.expected { color: var(--muted); border-left: 3px solid var(--border); border-radius: 0; margin: 4px 0 12px; white-space: pre-wrap; }

#work { display: grid; grid-template-rows: auto 1fr minmax(140px, 38%); min-height: 0; }
#editor { display: flex; overflow: auto; border-bottom: 1px solid var(--border); position: relative; }
#gutter, #highlight, #code { font-family: var(--mono); font-size: 14px; line-height: 1.5; tab-size: 4; margin: 0; padding: 10px 0; }
#gutter { color: var(--muted); text-align: right; padding: 10px 8px 10px 12px; user-select: none; min-width: 3.5em; background: var(--surface); position: sticky; left: 0; z-index: 2; }
#gutter .err { color: var(--error); font-weight: 700; }
#code-wrap { position: relative; flex: 1; min-width: max-content; }
#highlight, #code { padding-left: 12px; padding-right: 24px; white-space: pre; overflow: hidden; }
#highlight { pointer-events: none; }
#code { position: absolute; inset: 0; width: 100%; height: 100%; resize: none; border: 0; outline: 0; background: transparent; color: transparent; caret-color: var(--fg); }
#code::selection { background: color-mix(in srgb, var(--accent) 30%, transparent); }

.kw { color: var(--kw); } .kind { color: var(--kind); font-weight: 600; } .mod { color: var(--mod); }
.ty { color: var(--ty); } .str { color: var(--str); } .num { color: var(--num); } .com { color: var(--com); font-style: italic; }
.call { color: var(--call); } .const { color: var(--const); } .attr { color: var(--mod); } .label { color: var(--kind); }

#output-wrap { display: flex; flex-direction: column; min-height: 0; }
#output-head { display: flex; justify-content: space-between; padding: 4px 12px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--border); }
#verdict.ok { color: var(--ok); } #verdict.bad { color: var(--error); }
#output { flex: 1; overflow: auto; margin: 0; padding: 8px 12px; font-family: var(--mono); font-size: 13px; line-height: 1.45; white-space: pre-wrap; }
#output a.pos { color: var(--accent); cursor: pointer; text-decoration: underline dotted; }
#output .error { color: var(--error); font-weight: 600; }
#output .note { color: var(--muted); }
#fixes { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px 8px; }
#fixes button { font-size: 12px; }

.sep { width: 1px; align-self: stretch; background: var(--border); margin: 0 2px; }
#unchecked-label { font-size: 14px; display: flex; align-items: center; gap: 4px; cursor: pointer; color: var(--muted); }
#unchecked-label:has(input:checked) { color: var(--error); }

#files { display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-bottom: 1px solid var(--border); background: var(--surface); font-size: 13px; }
#tabs { display: flex; gap: 2px; flex-wrap: wrap; }
#tabs[hidden] { display: none; }
.tab { font-family: var(--mono); padding: 2px 8px; border: 1px solid transparent; border-radius: 4px; cursor: pointer; color: var(--muted); display: inline-flex; align-items: center; gap: 4px; }
.tab.active { color: var(--fg); background: var(--bg); border-color: var(--border); }
.tab.err { color: var(--error); }
.tab .close { border: 0; background: none; padding: 0 2px; font-size: 13px; line-height: 1; color: inherit; }
#add-file { font-size: 12px; padding: 1px 8px; margin-left: auto; }
#hover { position: fixed; z-index: 10; max-width: min(400px, calc(100vw - 16px)); max-height: 300px; overflow: auto; margin: 0; padding: 6px 10px; font-family: var(--mono); font-size: 12px; line-height: 1.4; white-space: pre-wrap; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 4px 16px rgb(0 0 0 / .15); pointer-events: none; }

#lesson-body h3 { font-size: 16px; margin: 20px 0 4px; }
.module { position: relative; margin: 12px 0 4px; }
.module pre { background: var(--surface); border: 1px dashed var(--border); }
.program .file, .module .file { display: block; font-family: var(--mono); font-size: 12px; color: var(--muted); margin-bottom: 2px; }
details.fold { margin: 12px 0 4px; }
details.fold > summary { cursor: pointer; color: var(--muted); font-size: 13px; }
pre.expected .what { display: block; font-family: system-ui, sans-serif; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; margin-bottom: 2px; }
pre.expected.unchecked { border-left-color: var(--error); }

/* Phones: one pane at a time, chosen by `body[data-pane]`; the tools scroll in one row. */
@media (max-width: 700px) {
  header { padding: 6px 8px; gap: 6px; }
  .brand { font-size: 16px; margin-right: 0; }
  button, select { padding: 5px 10px; }
  #panes { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
  #panes button { border: 0; border-radius: 0; background: none; color: var(--muted); }
  #panes button + button { border-left: 1px solid var(--border); }
  #panes button[aria-pressed="true"] { background: var(--surface); color: var(--fg); font-weight: 600; }
  /* The fade says the row scrolls; the padding lets its last button scroll clear of it. */
  #tools { order: 1; flex: 1 0 100%; display: flex; align-items: center; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-right: 32px; mask-image: linear-gradient(to right, #000 calc(100% - 32px), transparent); }
  #tools > * { flex: none; }
  #examples { max-width: 9em; }
  main { grid-template-columns: 1fr; grid-template-rows: 1fr; }
  #tutorial, #work { grid-area: 1 / 1; border-right: 0; }
  body:not([data-pane="lesson"]) #tutorial, body[data-pane="lesson"] #work { display: none; }
  body[data-pane="code"] #work { grid-template-rows: auto 1fr; }
  body[data-pane="code"] #output-wrap, body[data-pane="output"] #files, body[data-pane="output"] #editor { display: none; }
  body[data-pane="output"] #work { grid-template-rows: 1fr; }
  #lesson-body { padding: 4px 14px 40px; }
  #lesson-body table { display: block; overflow-x: auto; }
}
/* iOS Safari zooms the page into a focused text field under 16px. */
@supports (-webkit-touch-callout: none) {
  #gutter, #highlight, #code { font-size: 16px; }
}
