/* ============================================================
   JavaBot Academy
   Layout: sticky topbar + 3-column workspace (lesson / world / code)
            that collapses to tabbed panes under 1040px.
   Color:  slate-violet neutrals, amber signal accent, teal good,
            coral bad. Pixel-art surfaces get hard 2px edges.
   Type:   Archivo (display) / Inter (text) / JetBrains Mono (code)
            / Silkscreen (pixel badges only)
   ============================================================ */

:root {
  --bg:          #f2f2f7;
  --bg-2:        #e8e8f0;
  --surface:     #ffffff;
  --surface-2:   #fafaff;
  --surface-3:   #f0f0f7;
  --line:        #dedee9;
  --line-strong: #c3c3d4;
  --ink:         #16151f;
  --ink-2:       #4e4c61;
  --ink-3:       #7d7b92;
  --accent:      #b06a00;
  --accent-bold: #8a5200;
  --accent-ink:  #ffffff;
  --accent-soft: #fdf0d6;
  --accent-line: #e8c184;
  --good:        #0f7b6c;
  --good-soft:   #dcf4ef;
  --bad:         #c3304a;
  --bad-soft:    #fde8ec;
  --warn:        #9a5b00;
  --focus:       #3b5bdb;
  --checker-a:   #dcdbe6;
  --checker-b:   #c9c8d6;
  --grid-bg:     #fbfbfe;
  --grid-line:   #e2e2ee;
  --grid-dot:    #c9c9db;
  --wall:        #2b2937;
  --token:       #0f9b89;
  --token-edge:  #0b6b5e;
  --goal:        #b06a00;
  --shadow-1:    0 1px 2px rgba(20,18,36,.06), 0 2px 8px rgba(20,18,36,.05);
  --shadow-2:    0 8px 30px rgba(20,18,36,.12);
  --radius:      12px;
  --radius-sm:   8px;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --display: "Archivo", "Segoe UI", system-ui, sans-serif;
  --text: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  --pixel: "Silkscreen", var(--mono);
  --syn-kw:   #8a3ffc;
  --syn-type: #0f6fbe;
  --syn-str:  #0f7b4a;
  --syn-num:  #a63b00;
  --syn-com:  #8a8799;
  --syn-fn:   #9c4221;
  --syn-op:   #4e4c61;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:          #0f0e16;
    --bg-2:        #15141f;
    --surface:     #1a1926;
    --surface-2:   #201f2e;
    --surface-3:   #262537;
    --line:        #2e2d40;
    --line-strong: #434159;
    --ink:         #eeedf6;
    --ink-2:       #b3b1c6;
    --ink-3:       #878599;
    --accent:      #ffb224;
    --accent-bold: #ffc558;
    --accent-ink:  #221700;
    --accent-soft: #2e2413;
    --accent-line: #5c4615;
    --good:        #2dd4bf;
    --good-soft:   #12332f;
    --bad:         #ff7a8a;
    --bad-soft:    #351a21;
    --warn:        #ffc166;
    --focus:       #8aa4ff;
    --checker-a:   #55546e;
    --checker-b:   #47465e;
    --grid-bg:     #14131d;
    --grid-line:   #262535;
    --grid-dot:    #35344a;
    --wall:        #cfcde0;
    --token:       #2dd4bf;
    --token-edge:  #0d8c7d;
    --goal:        #ffb224;
    --shadow-1:    0 1px 2px rgba(0,0,0,.4), 0 2px 10px rgba(0,0,0,.3);
    --shadow-2:    0 10px 36px rgba(0,0,0,.5);
    --syn-kw:   #c79bff;
    --syn-type: #7cc4ff;
    --syn-str:  #8ae0a8;
    --syn-num:  #ffbd7a;
    --syn-com:  #6f6d85;
    --syn-fn:   #ffd08a;
    --syn-op:   #b3b1c6;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg:          #0f0e16;
  --bg-2:        #15141f;
  --surface:     #1a1926;
  --surface-2:   #201f2e;
  --surface-3:   #262537;
  --line:        #2e2d40;
  --line-strong: #434159;
  --ink:         #eeedf6;
  --ink-2:       #b3b1c6;
  --ink-3:       #878599;
  --accent:      #ffb224;
  --accent-bold: #ffc558;
  --accent-ink:  #221700;
  --accent-soft: #2e2413;
  --accent-line: #5c4615;
  --good:        #2dd4bf;
  --good-soft:   #12332f;
  --bad:         #ff7a8a;
  --bad-soft:    #351a21;
  --warn:        #ffc166;
  --focus:       #8aa4ff;
  --checker-a:   #55546e;
  --checker-b:   #47465e;
  --grid-bg:     #14131d;
  --grid-line:   #262535;
  --grid-dot:    #35344a;
  --wall:        #cfcde0;
  --token:       #2dd4bf;
  --token-edge:  #0d8c7d;
  --goal:        #ffb224;
  --shadow-1:    0 1px 2px rgba(0,0,0,.4), 0 2px 10px rgba(0,0,0,.3);
  --shadow-2:    0 10px 36px rgba(0,0,0,.5);
  --syn-kw:   #c79bff;
  --syn-type: #7cc4ff;
  --syn-str:  #8ae0a8;
  --syn-num:  #ffbd7a;
  --syn-com:  #6f6d85;
  --syn-fn:   #ffd08a;
  --syn-op:   #b3b1c6;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Class-based display rules below would otherwise beat the browser's own
   [hidden] rule, so state it once with priority. */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--text);
  font-size: 15px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

h1, h2, h3, h4 {
  font-family: var(--display);
  font-weight: 700;
  letter-spacing: -.016em;
  line-height: 1.18;
  text-wrap: balance;
  margin: 0;
}
h3, h4 { line-height: 1.3; }

svg { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

:where(button, a, input, textarea, select, [tabindex]):focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

kbd {
  font-family: var(--mono);
  font-size: 10px;
  padding: 1px 4px;
  border-radius: 4px;
  border: 1px solid currentColor;
  opacity: .55;
  line-height: 1.4;
}

.muted  { color: var(--ink-3); }
.small  { font-size: 12.5px; }
.eyebrow {
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: .13em;
  text-transform: uppercase;
  color: var(--accent);
  margin: 0 0 6px;
}
.pix-badge, .pix-stamp { font-family: var(--pixel); letter-spacing: .02em; }
.pix-badge {
  font-size: 10px;
  background: var(--ink);
  color: var(--bg);
  padding: 4px 7px 3px;
  border-radius: 4px;
  white-space: nowrap;
}
.pix-stamp {
  font-size: 11px;
  color: var(--accent);
  margin: 0 0 10px;
}

/* ---------------- app frame ---------------- */
#app { display: flex; flex-direction: column; height: 100%; min-height: 0; }

.topbar {
  position: sticky;
  top: env(safe-area-inset-top, 0px);
  z-index: 20;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
  flex: none;
}
body::before {
  content: "";
  position: fixed;
  inset: 0 0 auto;
  height: env(safe-area-inset-top, 0px);
  background: var(--surface);
  z-index: 21;
}
.topbar-in {
  display: flex;
  align-items: center;
  gap: 14px;
  padding-block: 9px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

.brand {
  display: flex; align-items: center; gap: 9px;
  background: none; border: 0; padding: 0; cursor: pointer;
  color: var(--ink); font-family: var(--display);
  font-size: 16px; font-weight: 800; letter-spacing: -.02em;
}
.brand-mark { color: var(--accent); display: grid; place-items: center; }
.brand-mark .eye { fill: var(--bg); stroke: none; }
.brand-thin { font-weight: 500; color: var(--ink-3); margin-left: 5px; }
@media (max-width: 560px) { .brand-thin { display: none; } }

.nav {
  display: flex; gap: 2px; margin-left: 6px;
  background: var(--surface-3); padding: 3px; border-radius: 10px;
  /* the one element allowed to shrink, so a narrow header never pushes the
     page sideways - it scrolls inside itself instead */
  min-width: 0; overflow-x: auto;
  scrollbar-width: none; -ms-overflow-style: none;
}
.nav::-webkit-scrollbar { display: none; }
.brand, .topbar-right { flex: none; }
.nav-btn {
  flex: none;
  font: 500 13px var(--text);
  color: var(--ink-2);
  background: none; border: 0; cursor: pointer;
  padding: 6px 11px; border-radius: 7px;
  white-space: nowrap;
}
.nav-btn:hover { color: var(--ink); }
.nav-btn.is-on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-1); }

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.chip {
  display: inline-flex; align-items: center; gap: 7px;
  font: 500 12.5px var(--text);
  color: var(--ink-2);
  background: var(--surface-3);
  border: 1px solid var(--line);
  border-radius: 99px;
  padding: 4px 11px 4px 5px;
  white-space: nowrap;
}
.chip-btn { cursor: pointer; }
.chip-btn:hover { border-color: var(--line-strong); color: var(--ink); }
#charChipCanvas { width: 22px; height: 22px; image-rendering: pixelated; border-radius: 5px; background: var(--surface); }
.chip-progress { color: var(--accent); padding-left: 8px; }
.chip-progress span { color: var(--ink-2); font-variant-numeric: tabular-nums; }
@media (max-width: 860px) { .chip-progress, #charChipName { display: none; } }

.icon-btn {
  display: grid; place-items: center;
  width: 32px; height: 32px;
  border: 1px solid var(--line);
  background: var(--surface-3);
  color: var(--ink-2);
  border-radius: 9px; cursor: pointer;
}
.icon-btn:hover { color: var(--ink); border-color: var(--line-strong); }
.i-sun, .i-moon { display: none; }
#themeBtn.show-sun .i-sun { display: block; }
#themeBtn.show-moon .i-moon { display: block; }
.i-sun .rays { stroke-width: 1.8; }

#main { flex: 1; min-height: 0; display: flex; }
.view { flex: 1; min-height: 0; min-width: 0; display: none; }
.view.is-on { display: flex; flex-direction: column; }
.view-in {
  flex: 1; min-height: 0; overflow: auto;
  padding-block: 26px 44px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* ---------------- workspace ---------------- */
.workspace {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(270px, 320px) minmax(320px, 1fr) minmax(360px, 440px);
  gap: 12px;
  padding: 12px;
  padding-left: max(12px, env(safe-area-inset-left, 0px));
  padding-right: max(12px, env(safe-area-inset-right, 0px));
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  display: flex; flex-direction: column;
  min-height: 0; min-width: 0;
  overflow: hidden;
}
.panel-head {
  flex: none;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  min-height: 46px;
}
.panel-title { font-size: 13.5px; font-weight: 600; letter-spacing: 0; }
.count-pill {
  margin-left: auto; font: 500 11.5px var(--mono);
  background: var(--surface-3); border: 1px solid var(--line);
  padding: 1px 8px; border-radius: 99px; color: var(--ink-3);
}
.panel-body { flex: 1; min-height: 0; overflow: auto; padding: 14px; }
.panel-foot { flex: none; border-top: 1px solid var(--line); padding: 9px 12px; background: var(--surface-2); }

/* Sub-tabs sit inside one panel and swap its body (Lesson / Commands). They
   are quieter than the top-level tabs so the two levels of navigation do not
   compete. */
.subtabs {
  flex: none; display: flex; gap: 2px;
  padding: 7px 12px 0; border-bottom: 1px solid var(--line);
  background: var(--surface-2);
}
.subtab {
  font: 500 12px var(--text); color: var(--ink-3);
  background: none; border: 0; border-bottom: 2px solid transparent;
  padding: 5px 10px 7px; cursor: pointer; border-radius: 6px 6px 0 0;
}
.subtab:hover { color: var(--ink-2); }
.subtab.is-on { color: var(--accent-bold); border-bottom-color: var(--accent); }

/* ---------------- commands reference ---------------- */
.cmd-lede { font-size: 13.4px; color: var(--ink-2); margin: 0 0 18px; }
.commands-body h3 { font-size: 14px; margin: 22px 0 10px; letter-spacing: -.01em; }
.commands-body h3:first-of-type { margin-top: 0; }
.commands-body h4 {
  font: 500 10.5px var(--mono); letter-spacing: .11em; text-transform: uppercase;
  color: var(--ink-3); margin: 16px 0 7px;
}
.cmd { padding: 7px 0; border-bottom: 1px dashed var(--line); }
.cmd:last-of-type { border-bottom: 0; }
.cmd-top {
  display: flex; align-items: center; gap: 7px; flex-wrap: wrap; margin: 0 0 3px;
}
.cmd-name {
  font: 500 12.2px var(--mono); color: var(--ink);
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: 5px; padding: 2px 6px;
  /* a long signature in a 300px panel breaks rather than scrolls the panel */
  overflow-wrap: anywhere;
}
.cmd-ret {
  font: 500 10px var(--mono); letter-spacing: .08em; text-transform: uppercase;
  color: var(--accent-bold); background: var(--accent-soft);
  border: 1px solid var(--accent-line); border-radius: 4px; padding: 1px 5px;
}
.cmd-doc { margin: 0; font-size: 12.8px; color: var(--ink-2); line-height: 1.55; }
.commands-body .note { margin-top: 10px; }

/* ---------------- lab objective ---------------- */
.objective {
  border-left: 3px solid var(--accent); padding: 2px 0 2px 11px; margin: 0 0 14px;
}
.obj-head {
  display: flex; align-items: baseline; gap: 10px; margin: 0 0 3px;
  font: 500 10.5px var(--mono); letter-spacing: .11em; text-transform: uppercase;
  color: var(--accent-bold);
}
.obj-count { margin-left: auto; color: var(--ink-3); letter-spacing: .06em; }
.obj-text { margin: 0; font-size: 13px; color: var(--ink-2); }

.ghost-btn {
  display: inline-flex; align-items: center; gap: 6px;
  font: 500 12.5px var(--text);
  color: var(--ink-2);
  background: var(--surface-3);
  border: 1px solid var(--line);
  border-radius: 8px; padding: 5px 9px; cursor: pointer;
}
.ghost-btn:hover { color: var(--ink); border-color: var(--line-strong); background: var(--surface); }
.ghost-btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 35%, var(--line)); }
.ghost-btn.is-on { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  font: 600 13px var(--text);
  color: var(--ink);
  background: var(--surface-3);
  border: 1px solid var(--line-strong);
  border-radius: 9px;
  padding: 7px 13px; cursor: pointer;
}
.btn:hover { background: var(--surface); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; transform: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent-bold); border-color: var(--accent-bold); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink-2); }
.btn-small { font-size: 12px; padding: 5px 10px; }

/* ---------------- lesson pane ---------------- */
.lesson-id { display: flex; align-items: center; gap: 9px; min-width: 0; }
.chapter-name {
  font: 500 12.5px var(--text); color: var(--ink-3);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.panel-head .ghost-btn { margin-left: auto; }

.lesson-body { font-size: 14px; color: var(--ink-2); }
.lesson-body h3 {
  font-size: 17px; color: var(--ink); margin: 0 0 8px;
}
.lesson-body h4 {
  font-size: 12px; font-family: var(--mono); font-weight: 500;
  text-transform: uppercase; letter-spacing: .1em;
  color: var(--ink-3); margin: 20px 0 8px;
}
.lesson-body p { margin: 0 0 11px; }
.lesson-body ul, .lesson-body ol { margin: 0 0 12px; padding-left: 20px; }
.lesson-body li { margin-bottom: 5px; }
.lesson-body code {
  font-family: var(--mono); font-size: .88em;
  background: var(--surface-3); border: 1px solid var(--line);
  padding: 1px 4px; border-radius: 5px; color: var(--ink);
  overflow-wrap: anywhere;
}
.lesson-body pre {
  font-family: var(--mono); font-size: 12.5px; line-height: 1.6;
  background: var(--surface-3); border: 1px solid var(--line);
  border-left: 3px solid var(--accent-line);
  border-radius: var(--radius-sm);
  padding: 10px 12px; margin: 0 0 13px;
  overflow-x: auto;
}
.lesson-body pre code { background: none; border: 0; padding: 0; }
.lesson-body strong { color: var(--ink); font-weight: 600; }
.lesson-body .note {
  background: var(--accent-soft);
  border: 1px solid var(--accent-line);
  border-radius: var(--radius-sm);
  padding: 10px 12px; margin: 0 0 13px;
  font-size: 13.2px; color: var(--ink-2);
}
.lesson-body .note strong { color: var(--accent-bold); }
.goalbox {
  display: flex; gap: 9px; align-items: flex-start;
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 10px 12px; margin: 0 0 16px;
  font-size: 13.4px;
}
.goalbox svg { flex: none; color: var(--accent); margin-top: 2px; }
.goalbox b { color: var(--ink); display: block; font-size: 11px; font-family: var(--mono);
  letter-spacing: .1em; text-transform: uppercase; color: var(--ink-3); font-weight: 500; }

/* The step target. Par is a goal, not a gate, so it is styled as a readout
   rather than as a requirement: it goes green when met and stays quiet
   otherwise. */
.parbox {
  display: flex; align-items: center; gap: 11px;
  margin: -8px 0 16px; padding: 7px 12px;
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font: 500 11px var(--mono); color: var(--ink-3);
  letter-spacing: .09em; text-transform: uppercase;
  font-variant-numeric: tabular-nums;
}
.parbox:empty { display: none; }
.par-star { flex: none; display: grid; place-items: center; color: var(--line-strong); }
.par-star svg { fill: currentColor; stroke: none; }
.par-cell { display: inline-flex; align-items: baseline; gap: 6px; }
.par-cell b { font-size: 15px; color: var(--ink); letter-spacing: -.01em; }
.par-note { margin-left: auto; text-transform: none; letter-spacing: .02em; font-size: 11.5px; }
.parbox.is-under {
  border-color: var(--good); background: var(--good-soft);
}
.parbox.is-under .par-star, .parbox.is-under .par-note { color: var(--good); }
.parbox.is-under .par-cell b { color: var(--good); }

.reqs { list-style: none; padding: 0; margin: 4px 0 14px; }
.reqs li {
  display: flex; gap: 8px; align-items: flex-start;
  font-size: 13.2px; padding: 5px 0;
  border-bottom: 1px dashed var(--line);
}
.reqs li:last-child { border-bottom: 0; }
.reqs .rq { flex: none; width: 16px; height: 16px; margin-top: 2px; color: var(--ink-3); }
.reqs li.done { color: var(--ink-3); }
.reqs li.done .rq { color: var(--good); }
.reqs li.done .rq-label { text-decoration: line-through; }

.hint-card {
  background: var(--surface-3); border: 1px dashed var(--line-strong);
  border-radius: var(--radius-sm); padding: 10px 12px; margin: 12px 0 0;
  font-size: 13.2px;
}
.hint-card b { font-family: var(--mono); font-size: 11px; letter-spacing: .09em;
  text-transform: uppercase; color: var(--accent); display: block; margin-bottom: 4px; }
.solution-wrap { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; }

.lesson-foot { display: flex; align-items: center; gap: 8px; }
.foot-nav { margin-left: auto; display: flex; gap: 6px; }
.foot-nav .ghost-btn { font-family: var(--mono); padding: 5px 11px; }

/* ---------------- stage ---------------- */
.stage-title { font-size: 15px; letter-spacing: -.01em; }
.stage-meta { margin-left: auto; display: flex; gap: 12px; }
.meta-item {
  display: inline-flex; align-items: center; gap: 5px;
  font: 500 12px var(--mono); color: var(--ink-3);
  font-variant-numeric: tabular-nums;
}
.meta-item svg { stroke-width: 2; }

.stage-canvas {
  flex: 1; min-height: 0; position: relative;
  display: grid; place-items: center;
  padding: 14px;
  background:
    radial-gradient(circle at 1px 1px, var(--grid-dot) 1px, transparent 0) 0 0 / 16px 16px,
    var(--grid-bg);
}
#worldCanvas, #sandCanvas {
  max-width: 100%; max-height: 100%;
  border-radius: var(--radius-sm);
  touch-action: none;
}
#sandCanvas { cursor: crosshair; }

.speech {
  position: absolute;
  background: var(--ink); color: var(--bg);
  font: 500 12.5px var(--text);
  padding: 5px 9px; border-radius: 8px;
  pointer-events: none; max-width: 180px;
  transform: translate(-50%, -100%);
  box-shadow: var(--shadow-2);
}
.speech::after {
  content: ""; position: absolute; left: 50%; bottom: -5px;
  width: 9px; height: 9px; background: var(--ink);
  transform: translateX(-50%) rotate(45deg);
}

.stage-overlay {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(3px);
  padding: 18px; z-index: 5;
}
.overlay-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-2);
  padding: 20px; max-width: 360px; width: 100%;
  text-align: center;
  animation: pop .22s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes pop { from { transform: scale(.94); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .overlay-card { animation: none; } }
.overlay-card h3 { font-size: 19px; margin-bottom: 7px; }
.overlay-text { color: var(--ink-2); font-size: 13.5px; margin: 0 0 14px; }
.overlay-stats { list-style: none; display: flex; gap: 8px; justify-content: center; padding: 0; margin: 0 0 16px; flex-wrap: wrap; }
.overlay-stats li {
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 10px;
  font: 500 11.5px var(--mono); color: var(--ink-3);
  display: flex; flex-direction: column; gap: 1px; min-width: 70px;
}
.overlay-stats li b { font-size: 15px; color: var(--ink); font-weight: 700; font-variant-numeric: tabular-nums; }
.overlay-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

.controls {
  flex: none;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  background: var(--surface-2);
}
.speed { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.speed-label { font: 500 11px var(--mono); letter-spacing: .09em; text-transform: uppercase; color: var(--ink-3); }
.speed output { font: 500 12px var(--mono); color: var(--ink-2); min-width: 26px; font-variant-numeric: tabular-nums; }
.speed input[type=range] { width: 92px; accent-color: var(--accent); }
@media (max-width: 420px) { .speed { margin-left: 0; width: 100%; } .speed input[type=range] { flex: 1; width: auto; } }

.statusbar {
  flex: none;
  display: flex; align-items: center; gap: 8px;
  padding: 7px 12px;
  border-top: 1px solid var(--line);
  font: 500 12.2px var(--text);
  color: var(--ink-2);
  min-height: 34px;
}
.status-dot { flex: none; width: 7px; height: 7px; border-radius: 99px; background: var(--ink-3); }
.statusbar.ok    { color: var(--good); background: var(--good-soft); }
.statusbar.ok    .status-dot { background: var(--good); }
.statusbar.err   { color: var(--bad); background: var(--bad-soft); }
.statusbar.err   .status-dot { background: var(--bad); }
.statusbar.busy  .status-dot { background: var(--accent); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { .statusbar.busy .status-dot { animation: none; } }

/* ---------------- code editor ---------------- */
.file-tab {
  display: inline-flex; align-items: center; gap: 7px;
  font: 500 12.5px var(--mono); color: var(--ink-2);
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: 7px; padding: 4px 10px;
}
.code-head-actions { margin-left: auto; display: flex; gap: 6px; }
.code-head-actions .ghost-btn { margin-left: 0; }

.editor-host {
  flex: 1; min-height: 140px;
  position: relative; display: flex;
  background: var(--surface);
  overflow: hidden;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 21px;
  tab-size: 4;
}
.ed-gutter {
  flex: none; width: 46px;
  padding: 10px 8px 10px 0;
  text-align: right;
  color: var(--ink-3);
  background: var(--surface-2);
  border-right: 1px solid var(--line);
  user-select: none;
  overflow: hidden;
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}
.ed-gutter span { display: block; height: 21px; position: relative; }
.ed-gutter span.cur { color: var(--accent); font-weight: 700; }
.ed-gutter span.bad { color: var(--bad); font-weight: 700; }
.ed-gutter span.bad::before {
  content: ""; position: absolute; left: -40px; top: 7px;
  width: 7px; height: 7px; border-radius: 99px; background: var(--bad);
}
.ed-scroll { flex: 1; min-width: 0; position: relative; overflow: auto; }
.ed-layer {
  margin: 0;
  padding: 10px 14px 60px;
  white-space: pre;
  min-height: 100%;
  min-width: 100%;
  font: inherit;
  letter-spacing: 0;
}
.ed-highlight {
  position: absolute; inset: 0;
  pointer-events: none;
  color: var(--ink);
}
.ed-input {
  position: relative;
  display: block;
  width: 100%;
  background: transparent;
  color: transparent;
  caret-color: var(--accent);
  border: 0; outline: 0; resize: none;
  overflow: hidden;
  font: inherit;
  letter-spacing: 0;
}
.ed-input::selection { background: color-mix(in srgb, var(--focus) 32%, transparent); }
.ed-rowmark {
  position: absolute; left: 0; right: 0; height: 21px;
  pointer-events: none;
}
.ed-rowmark.active { background: color-mix(in srgb, var(--accent) 15%, transparent); }
.ed-rowmark.error  { background: color-mix(in srgb, var(--bad) 13%, transparent); border-left: 2px solid var(--bad); }

.tok-kw   { color: var(--syn-kw); font-weight: 500; }
.tok-type { color: var(--syn-type); }
.tok-str  { color: var(--syn-str); }
.tok-num  { color: var(--syn-num); }
.tok-com  { color: var(--syn-com); font-style: italic; }
.tok-fn   { color: var(--syn-fn); }
.tok-op   { color: var(--syn-op); }
.tok-robot { color: var(--accent); font-weight: 500; }

.console {
  flex: none;
  border-top: 1px solid var(--line);
  background: var(--surface-2);
  display: flex; flex-direction: column;
  max-height: 38%;
  min-height: 92px;
}
.console-head {
  flex: none; display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-bottom: 1px solid var(--line);
}
.console-title { font: 500 11px var(--mono); letter-spacing: .1em; text-transform: uppercase; color: var(--ink-3); }
.console-head .ghost-btn { margin-left: auto; padding: 3px 8px; font-size: 11.5px; }
.console-body {
  flex: 1; min-height: 0; overflow: auto;
  padding: 8px 12px 12px;
  font: 400 12.4px var(--mono);
  line-height: 1.65;
}
.console-empty { color: var(--ink-3); margin: 0; font-family: var(--text); font-size: 12.5px; }
.console-body p { margin: 0 0 2px; overflow-wrap: anywhere; }
.c-out  { color: var(--ink-2); }
.c-err  { color: var(--bad); }
.c-ok   { color: var(--good); }
.c-warn { color: var(--warn); }
.c-err .where, .c-warn .where {
  color: var(--ink-3); font-size: 11.5px;
}
.c-caret { color: var(--ink-3); white-space: pre; }

/* ---------------- characters ---------------- */
.creator { max-width: 1180px; margin: 0 auto; width: 100%; }
.section-head { margin-bottom: 22px; max-width: 62ch; }
.section-head h1 { font-size: clamp(24px, 3.4vw, 34px); margin-bottom: 8px; }
.lede { color: var(--ink-2); font-size: 15px; margin: 0; }

.creator-grid { display: grid; grid-template-columns: minmax(300px, 1fr) minmax(300px, 400px); gap: 16px; align-items: start; }
@media (max-width: 900px) { .creator-grid { grid-template-columns: 1fr; } }

.tool-row { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.tool {
  display: grid; place-items: center;
  width: 30px; height: 30px;
  border: 1px solid var(--line); background: var(--surface-3);
  color: var(--ink-2); border-radius: 8px; cursor: pointer;
}
.tool:hover { color: var(--ink); border-color: var(--line-strong); }
.tool.is-on { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent); }
.tool-sep { width: 1px; height: 20px; background: var(--line); margin: 0 3px; }

.sprite-stage {
  padding: 16px; display: grid; place-items: center;
  background: var(--surface);
}
#spriteCanvas {
  width: min(100%, 440px); height: auto; aspect-ratio: 1;
  image-rendering: pixelated;
  border: 2px solid var(--line-strong);
  border-radius: 6px;
  cursor: crosshair;
  touch-action: none;
  /* the checkerboard sits on the canvas itself, so transparent pixels read as
     transparent and both black and white pixels stay visible on top of it */
  background: repeating-conic-gradient(var(--checker-a) 0 25%, var(--checker-b) 0 50%) 0 0 / 12.5% 12.5%;
}

.palette {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(26px, 1fr));
  gap: 5px; padding: 12px; border-top: 1px solid var(--line);
}
.sw {
  aspect-ratio: 1; border-radius: 6px; cursor: pointer;
  border: 2px solid transparent;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.18);
  padding: 0;
}
.sw.is-on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--surface); }
.palette-foot {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 0 12px 12px; font-size: 12.5px; color: var(--ink-3);
}
.swatch-custom, .swatch-erase { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.swatch-custom input[type=color] { width: 26px; height: 26px; border: 1px solid var(--line); border-radius: 6px; background: none; padding: 0; cursor: pointer; }
.hint-text { margin-left: auto; font-family: var(--mono); font-size: 11.5px; }

.creator-side { display: grid; gap: 16px; }
.preview-row { display: flex; gap: 10px; padding: 14px; flex-wrap: wrap; justify-content: space-around; }
.preview-row figure { margin: 0; text-align: center; }
.preview-row canvas {
  width: 72px; height: 72px; image-rendering: pixelated;
  background: var(--grid-bg); border: 1px solid var(--line); border-radius: 8px;
}
.preview-row figcaption { font: 500 10.5px var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); margin-top: 5px; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; flex: 1; }
.field span { font: 500 11px var(--mono); letter-spacing: .09em; text-transform: uppercase; color: var(--ink-3); }
.field input {
  font: 500 13.5px var(--text);
  background: var(--surface-3); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; min-width: 0;
}
.field input:hover { border-color: var(--line-strong); }
.name-row { display: flex; gap: 10px; align-items: flex-end; padding: 0 14px 12px; flex-wrap: wrap; }
.side-actions { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 14px 14px; border-top: 1px solid var(--line); padding-top: 12px; }

.share-box { padding: 0 14px 14px; display: grid; gap: 8px; }
.share-box textarea {
  font: 400 11.5px var(--mono);
  background: var(--surface-3); color: var(--ink);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; resize: vertical; width: 100%;
  overflow-wrap: anywhere;
}
.share-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.roster { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; padding: 14px; }
.roster-card {
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: 10px; padding: 9px; cursor: pointer;
  display: grid; gap: 6px; justify-items: center; text-align: center;
  position: relative;
}
.roster-card:hover { border-color: var(--line-strong); }
.roster-card.is-on { border-color: var(--accent); background: var(--accent-soft); }
.roster-card canvas { width: 54px; height: 54px; image-rendering: pixelated; }
.roster-card .rname {
  font: 500 11.5px var(--text); color: var(--ink);
  max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.roster-card .rtag { font: 400 10px var(--mono); color: var(--ink-3); letter-spacing: .06em; text-transform: uppercase; }
.roster-card .del {
  position: absolute; top: 3px; right: 3px;
  width: 20px; height: 20px; border-radius: 6px;
  display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line);
  color: var(--ink-3); cursor: pointer; opacity: 0;
}
.roster-card:hover .del, .roster-card:focus-within .del { opacity: 1; }
.roster-card .del:hover { color: var(--bad); border-color: var(--bad); }
.roster-empty { grid-column: 1/-1; color: var(--ink-3); font-size: 13px; }

/* ---------------- sandbox tools ---------------- */
.sand-body { display: grid; gap: 14px; align-content: start; }
.brush-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.brush {
  font: 500 12.5px var(--text); color: var(--ink-2);
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 4px; cursor: pointer;
}
.brush:hover { color: var(--ink); border-color: var(--line-strong); }
.brush.is-on { background: var(--accent-soft); border-color: var(--accent-line); color: var(--accent); }
.size-row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.small-field { max-width: 92px; }
.sand-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.divider { height: 1px; background: var(--line); }

/* ---------------- tabs (narrow screens) ---------------- */
.tabbar {
  display: none;
  gap: 3px; padding: 8px 12px 0;
  padding-left: max(12px, env(safe-area-inset-left, 0px));
  padding-right: max(12px, env(safe-area-inset-right, 0px));
}
.tab {
  flex: 1; font: 500 13px var(--text); color: var(--ink-3);
  background: var(--surface-3); border: 1px solid var(--line);
  border-bottom: 0; border-radius: 9px 9px 0 0;
  padding: 7px 10px; cursor: pointer;
}
.tab.is-on { background: var(--surface); color: var(--ink); }

/* ---------------- guide ---------------- */
.guide { max-width: 1120px; margin: 0 auto; width: 100%; }
.guide-hero { max-width: 64ch; margin-bottom: 26px; }
.guide-hero h1 { font-size: clamp(25px, 3.6vw, 36px); margin-bottom: 9px; }
.guide-toc { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 26px; }
.guide-toc a {
  font: 500 12.5px var(--text); text-decoration: none;
  color: var(--ink-2); background: var(--surface);
  border: 1px solid var(--line); border-radius: 99px; padding: 5px 12px;
}
.guide-toc a:hover { color: var(--ink); border-color: var(--line-strong); }

.gsection { margin-bottom: 34px; scroll-margin-top: 70px; }
.gsection > h2 {
  font-size: 20px; margin-bottom: 4px;
  padding-bottom: 8px; border-bottom: 2px solid var(--line);
}
.gsection > .ghead-note { color: var(--ink-3); font-size: 13.5px; margin: 8px 0 16px; }
.gcards { display: grid; grid-template-columns: repeat(auto-fit, minmax(290px, 1fr)); gap: 14px; }
.gcard {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow-1); min-width: 0;
}
.gcard h3 { font-size: 15px; margin-bottom: 7px; }
.gcard p { color: var(--ink-2); font-size: 13.6px; margin: 0 0 10px; }
.gcard p:last-child { margin-bottom: 0; }
.gcard pre, .guide pre {
  font: 400 12.5px/1.65 var(--mono);
  background: var(--surface-3); border: 1px solid var(--line);
  border-left: 3px solid var(--accent-line);
  border-radius: var(--radius-sm); padding: 11px 13px; margin: 0 0 11px;
  overflow-x: auto;
}
.gcard code, .guide code {
  font-family: var(--mono); font-size: .89em;
  background: var(--surface-3); border: 1px solid var(--line);
  padding: 1px 4px; border-radius: 5px; overflow-wrap: anywhere;
}
.gcard pre code, .guide pre code { background: none; border: 0; padding: 0; }

.anatomy {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow-1);
  overflow-x: auto;
}
.anatomy-code { font: 500 clamp(13px, 1.6vw, 17px)/2.1 var(--mono); white-space: nowrap; }
.anatomy-code .part { position: relative; padding: 2px 3px; border-radius: 4px; }
.anatomy-code .p1 { background: color-mix(in srgb, var(--syn-kw) 18%, transparent); color: var(--syn-kw); }
.anatomy-code .p2 { background: color-mix(in srgb, var(--syn-type) 18%, transparent); color: var(--syn-type); }
.anatomy-code .p3 { background: color-mix(in srgb, var(--syn-fn) 22%, transparent); color: var(--syn-fn); }
.anatomy-code .p4 { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }
.anatomy-legend { list-style: none; padding: 0; margin: 16px 0 0; display: grid; gap: 9px; }
.anatomy-legend li { display: flex; gap: 10px; font-size: 13.4px; color: var(--ink-2); align-items: flex-start; }
.anatomy-legend .key {
  flex: none; font: 500 11.5px var(--mono); padding: 2px 7px; border-radius: 5px;
  margin-top: 1px; min-width: 86px; text-align: center;
}
.anatomy-legend .k1 { background: color-mix(in srgb, var(--syn-kw) 18%, transparent); color: var(--syn-kw); }
.anatomy-legend .k2 { background: color-mix(in srgb, var(--syn-type) 18%, transparent); color: var(--syn-type); }
.anatomy-legend .k3 { background: color-mix(in srgb, var(--syn-fn) 22%, transparent); color: var(--syn-fn); }
.anatomy-legend .k4 { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-1); }
table.api { width: 100%; border-collapse: collapse; font-size: 13.3px; min-width: 520px; }
table.api th, table.api td { text-align: left; padding: 9px 13px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.api th {
  font: 500 10.5px var(--mono); letter-spacing: .1em; text-transform: uppercase;
  color: var(--ink-3); background: var(--surface-2); position: sticky; top: 0;
}
table.api tr:last-child td { border-bottom: 0; }
table.api td.sig { font-family: var(--mono); font-size: 12.4px; color: var(--ink); white-space: nowrap; }
table.api td.ret { font-family: var(--mono); font-size: 12.4px; color: var(--syn-type); white-space: nowrap; }
table.api td.desc { color: var(--ink-2); }

.err-row { display: grid; gap: 10px; }
.err-item {
  background: var(--surface); border: 1px solid var(--line);
  border-left: 3px solid var(--bad);
  border-radius: var(--radius-sm); padding: 12px 14px;
}
.err-item .msg { font: 500 12.6px var(--mono); color: var(--bad); margin: 0 0 6px; overflow-wrap: anywhere; }
.err-item .fix { font-size: 13.4px; color: var(--ink-2); margin: 0; }

/* ---------------- drawer ---------------- */
.drawer-scrim, .modal-scrim {
  position: fixed; inset: 0; z-index: 60;
  background: color-mix(in srgb, var(--ink) 42%, transparent);
  backdrop-filter: blur(3px);
  display: flex;
}
.modal-scrim { place-items: center; justify-content: center; padding: 18px; }
.drawer {
  margin-left: auto; width: min(460px, 100%);
  background: var(--surface);
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column;
  box-shadow: var(--shadow-2);
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  animation: slide .2s ease-out;
}
@keyframes slide { from { transform: translateX(22px); opacity: .4; } }
@media (prefers-reduced-motion: reduce) { .drawer { animation: none; } }
.drawer-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.drawer-head h2 { font-size: 16px; }
.drawer-head .icon-btn { margin-left: auto; }
.drawer-body { flex: 1; min-height: 0; overflow: auto; padding: 14px 16px; }
.drawer-foot {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 11px 16px; border-top: 1px solid var(--line); background: var(--surface-2);
}

.chapter-group { margin-bottom: 20px; }
.chapter-group h3 {
  display: flex; align-items: baseline; gap: 10px;
  font: 500 11px var(--mono); letter-spacing: .11em; text-transform: uppercase;
  color: var(--ink-2); margin-bottom: 3px;
}
.lab-count { margin-left: auto; color: var(--ink-3); font-size: 10px; }
.lab-objective {
  font-size: 12.4px; color: var(--ink-3); margin: 0 0 10px; line-height: 1.5;
}
.lvl-list { display: grid; gap: 6px; }
.lvl-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; text-align: left;
  background: var(--surface-3); border: 1px solid var(--line);
  border-radius: 9px; padding: 8px 11px; cursor: pointer;
  font: 500 13.4px var(--text); color: var(--ink);
}
.lvl-item:hover { border-color: var(--line-strong); }
.lvl-item.is-on { border-color: var(--accent); background: var(--accent-soft); }
.lvl-num {
  flex: none; font-family: var(--mono); font-size: 11px; font-weight: 500;
  width: 32px; height: 22px; display: grid; place-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: 5px;
  color: var(--ink-3);
}
.lvl-item.solved .lvl-num { background: var(--good); border-color: var(--good); color: var(--surface); }
.lvl-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lvl-par {
  flex: none; font: 500 10px var(--mono); color: var(--ink-3);
  letter-spacing: .07em; text-transform: uppercase;
}
.lvl-check { flex: none; color: var(--good); opacity: 0; display: inline-flex; align-items: center; gap: 5px; }
.lvl-item.solved .lvl-check { opacity: 1; }
/* the star only shows on levels cleared at or under par */
.lvl-check > svg:first-child { display: none; color: var(--accent); fill: currentColor; stroke: none; }
.lvl-item.starred .lvl-check > svg:first-child { display: block; }

/* ---------------- modal + toast ---------------- */
.modal {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; box-shadow: var(--shadow-2);
  padding: clamp(20px, 4vw, 30px);
  max-width: 480px; width: 100%;
  max-height: 100%; overflow: auto;
}
.modal h2 { font-size: clamp(20px, 3vw, 25px); margin-bottom: 10px; }
.modal p { color: var(--ink-2); font-size: 14px; margin: 0 0 14px; }
.welcome-list { list-style: none; padding: 0; margin: 0 0 20px; display: grid; gap: 9px; }
.welcome-list li {
  font-size: 13.6px; color: var(--ink-2);
  padding-left: 22px; position: relative;
}
.welcome-list li::before {
  content: ""; position: absolute; left: 0; top: 7px;
  width: 9px; height: 9px; border-radius: 2px;
  background: var(--accent);
}
.welcome-list strong { color: var(--ink); }
.modal-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.toast {
  position: fixed; z-index: 90;
  left: 50%; transform: translateX(-50%);
  bottom: calc(22px + env(safe-area-inset-bottom, 0px));
  background: var(--ink); color: var(--bg);
  font: 500 13px var(--text);
  padding: 9px 16px; border-radius: 99px;
  box-shadow: var(--shadow-2);
  max-width: calc(100% - 32px);
  text-align: center;
}

/* ---------------- responsive ---------------- */
@media (max-width: 1180px) {
  .workspace { grid-template-columns: minmax(250px, 300px) minmax(280px, 1fr); grid-template-rows: 1fr; }
  .pane-code, .pane-sandcode { grid-column: 1 / -1; grid-row: 2; min-height: 320px; }
  .workspace { grid-template-rows: minmax(300px, 1fr) minmax(320px, .95fr); }
}
@media (max-width: 820px) {
  .tabbar { display: flex; }
  .workspace {
    display: block; overflow: auto;
    padding-top: 0;
  }
  .workspace .panel { display: none; border-radius: 0 10px 10px 10px; height: calc(100% - 2px); }
  .workspace .panel.is-on { display: flex; }
  .pane-code, .pane-sandcode { grid-column: auto; grid-row: auto; min-height: 0; }
  .editor-host { min-height: 240px; }
  .nav-btn { padding: 6px 9px; font-size: 12.5px; }
}
@media (max-width: 480px) {
  body { font-size: 14.5px; }
  .nav { margin-left: 0; }
  .topbar-in { gap: 8px; }
  .controls { gap: 6px; }
  .btn { padding: 7px 11px; font-size: 12.5px; }
  .btn kbd { display: none; }
  .brush-grid { grid-template-columns: repeat(2, 1fr); }
}
