:root {
  --bg: #faf8f5;
  --bg-elev: #ffffff;
  --bg-sunken: #f1ede7;
  --fg: #1f1d1a;
  --fg-muted: #6b655d;
  --border: #e4ded5;
  --accent: #c2410c;
  --accent-soft: #fde8dc;
  --accent-fg: #ffffff;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --warn: #a16207;
  --warn-soft: #fef3c7;
  --code-bg: #f4f1ec;
  --term-bg: #1b1917;
  --term-fg: #e8e2d8;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  --sidebar-w: 300px;
  --tok-kw: #9f1239;
  --tok-type: #0f766e;
  --tok-string: #15803d;
  --tok-number: #b45309;
  --tok-comment: #8a847b;
  --tok-macro: #6d28d9;
  --tok-fn: #1d4ed8;
  --tok-attr: #8a847b;
  --tok-lifetime: #b45309;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15130f;
    --bg-elev: #1e1b17;
    --bg-sunken: #0f0e0b;
    --fg: #ece7df;
    --fg-muted: #a39c92;
    --border: #2d2924;
    --accent: #f97316;
    --accent-soft: #3b2113;
    --accent-fg: #15130f;
    --ok: #4ade80;
    --ok-soft: #14301f;
    --bad: #f87171;
    --bad-soft: #3a1717;
    --warn: #fbbf24;
    --warn-soft: #3a2d0f;
    --code-bg: #0f0e0b;
    --term-bg: #0b0a08;
    --term-fg: #e8e2d8;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
    --tok-kw: #fb7185;
    --tok-type: #2dd4bf;
    --tok-string: #86efac;
    --tok-number: #fbbf24;
    --tok-comment: #7c766d;
    --tok-macro: #c4b5fd;
    --tok-fn: #93c5fd;
    --tok-attr: #7c766d;
    --tok-lifetime: #fbbf24;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

code, pre, kbd, .mono {
  font-family: ui-monospace, "Cascadia Code", "JetBrains Mono", Consolas, "SF Mono", Menlo, monospace;
}

a { color: var(--accent); }

button {
  font: inherit;
  cursor: pointer;
}

#app {
  display: flex;
  min-height: 100vh;
}

/* ------------------------------------------------------------------ */
/* Sidebar                                                             */
/* ------------------------------------------------------------------ */

#sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.side-header {
  padding: 20px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  margin-bottom: 16px;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 800;
}

.level-title {
  font-weight: 600;
  font-size: 14px;
  letter-spacing: 0.01em;
}

.xp-bar {
  height: 8px;
  margin: 8px 0 6px;
  border-radius: 999px;
  background: var(--bg-sunken);
  overflow: hidden;
}

.xp-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.6s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.xp-text {
  font-size: 12px;
  color: var(--fg-muted);
}

#chapter-list {
  padding: 8px;
  flex: 1;
}

.chapter-link {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--fg);
}

.chapter-link:hover { background: var(--bg-sunken); }
.chapter-link.active { background: var(--accent-soft); }
.chapter-link.empty { opacity: 0.5; }

.ring {
  flex: 0 0 32px;
  width: 32px;
  height: 32px;
}

.ring circle.track { stroke: var(--border); }
.ring circle.fill {
  stroke: var(--accent);
  transition: stroke-dashoffset 0.5s ease;
}
.ring.done circle.fill { stroke: var(--ok); }
.ring text {
  fill: var(--fg-muted);
  font-size: 11px;
  font-weight: 600;
}

.chapter-link-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chapter-link-title {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chapter-link-sub {
  font-size: 12px;
  color: var(--fg-muted);
}

.next-pill {
  margin-left: auto;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--accent);
}

.side-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}

.link-btn {
  background: none;
  border: 0;
  padding: 0;
  color: var(--fg-muted);
  font-size: 13px;
  text-decoration: underline;
}
.link-btn:hover { color: var(--fg); }

/* ------------------------------------------------------------------ */
/* Main                                                                */
/* ------------------------------------------------------------------ */

#content {
  flex: 1;
  min-width: 0;
  padding: 32px 40px 120px;
  max-width: 860px;
  margin: 0 auto;
}

.loading, .error-box {
  color: var(--fg-muted);
  padding: 40px 0;
}

.error-box {
  color: var(--bad);
  white-space: pre-wrap;
}

.chapter-header {
  margin-bottom: 24px;
}

.chapter-kicker {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.chapter-header h1 {
  margin: 4px 0 6px;
  font-size: 30px;
  line-height: 1.2;
}

.chapter-subtitle {
  color: var(--fg-muted);
  margin: 0;
}

.chapter-stats {
  display: flex;
  gap: 18px;
  margin-top: 14px;
  font-size: 13px;
  color: var(--fg-muted);
}

.chapter-stats strong { color: var(--fg); }

.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
  margin: 10px 0 22px;
  font-size: 13px;
  color: var(--fg-muted);
}

/* Cards */

.card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 16px;
  overflow: hidden;
  transition: opacity 0.2s;
}

.card.locked {
  opacity: 0.45;
  pointer-events: none;
  filter: saturate(0.5);
}

.card.collapsed .card-body { display: none; }

.card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  cursor: pointer;
  user-select: none;
}

.card-head:hover { background: var(--bg-sunken); }

.card-type {
  flex: 0 0 auto;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg-sunken);
  color: var(--fg-muted);
}

.card-type.predict, .card-type.quiz { background: var(--accent-soft); color: var(--accent); }
.card-type.exercise { background: var(--warn-soft); color: var(--warn); }
.card-type.code { background: var(--bg-sunken); }

.card-title {
  flex: 1;
  font-weight: 600;
  font-size: 16px;
  min-width: 0;
}

.card-meta {
  font-size: 12px;
  color: var(--fg-muted);
  white-space: nowrap;
}

.card-check {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  display: inline-grid;
  place-items: center;
  font-size: 13px;
  color: transparent;
  flex: 0 0 auto;
}
.card.done .card-check {
  border-color: var(--ok);
  background: var(--ok);
  color: #fff;
}

.card-body {
  padding: 4px 22px 20px;
  border-top: 1px solid var(--border);
}

.card-body > :first-child { margin-top: 16px; }

.card-body h2, .card-body h3, .card-body h4 {
  font-size: 17px;
  margin: 20px 0 6px;
}

.card-body p { margin: 10px 0; }
.card-body ul, .card-body ol { padding-left: 24px; margin: 8px 0; }
.card-body li { margin: 4px 0; }
.card-body em { font-style: italic; }

.card-body code {
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 5px;
  font-size: 0.9em;
}

pre.code {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.5;
  margin: 12px 0;
}

pre.code code {
  background: none;
  padding: 0;
  font-size: inherit;
}

blockquote {
  margin: 12px 0;
  padding: 8px 14px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
}

blockquote.compiler {
  border-left-color: var(--bad);
  background: var(--bad-soft);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 13px;
}

blockquote p { margin: 4px 0; }

hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }

.tok-kw { color: var(--tok-kw); font-weight: 600; }
.tok-type { color: var(--tok-type); }
.tok-string { color: var(--tok-string); }
.tok-number { color: var(--tok-number); }
.tok-comment { color: var(--tok-comment); font-style: italic; }
.tok-macro { color: var(--tok-macro); }
.tok-fn { color: var(--tok-fn); }
.tok-attr { color: var(--tok-attr); }
.tok-lifetime { color: var(--tok-lifetime); }

/* Buttons */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--fg);
  font-weight: 600;
  font-size: 14px;
  transition: transform 0.08s, background 0.15s;
}

.btn:hover { background: var(--bg-sunken); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.5; cursor: default; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
}
.btn.primary:hover { filter: brightness(1.07); background: var(--accent); }

.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.small { padding: 5px 10px; font-size: 13px; }

.row {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-top: 14px;
}

.row .spacer { flex: 1; }

.muted { color: var(--fg-muted); font-size: 13px; }

/* Quiz / predict */

.question {
  font-weight: 600;
  margin: 14px 0 8px;
}

.options {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.option {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
  text-align: left;
  color: var(--fg);
  font-size: 15px;
  line-height: 1.45;
}

.option:hover { border-color: var(--accent); }
.option.selected { border-color: var(--accent); background: var(--accent-soft); }
.option.correct { border-color: var(--ok); background: var(--ok-soft); }
.option.wrong { border-color: var(--bad); background: var(--bad-soft); }
.option.missed { border-style: dashed; border-color: var(--ok); }
.option:disabled { cursor: default; }

.option .bullet {
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  margin-top: 3px;
  border-radius: 50%;
  border: 2px solid var(--border);
}
.option.multi .bullet { border-radius: 5px; }
.option.selected .bullet { border-color: var(--accent); background: var(--accent); }
.option.correct .bullet { border-color: var(--ok); background: var(--ok); }
.option.wrong .bullet { border-color: var(--bad); background: var(--bad); }

.fill-input {
  font: inherit;
  padding: 9px 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  min-width: 220px;
}
.fill-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.feedback {
  margin-top: 14px;
  padding: 12px 16px;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.feedback.good { background: var(--ok-soft); border-color: var(--ok); }
.feedback.bad { background: var(--bad-soft); border-color: var(--bad); }
.feedback .verdict { font-weight: 700; margin-bottom: 4px; }
.feedback p { margin: 6px 0; }

/* Playground output */

.term {
  background: var(--term-bg);
  color: var(--term-fg);
  border-radius: 10px;
  padding: 12px 14px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  margin-top: 12px;
  max-height: 420px;
  overflow: auto;
}

.term .err { color: #fca5a5; }
.term .ok { color: #86efac; }
.term .warn { color: #fcd34d; }
.term .dim { color: #9c958b; }
.term a { color: #93c5fd; }
.term:empty { display: none; }

/* Exercise panel */

.exercise-kind {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--warn);
}

.file-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  padding: 10px 12px;
  background: var(--bg-sunken);
  border-radius: 10px;
  font-size: 13px;
}

.file-row code {
  background: none;
  padding: 0;
  font-size: 13px;
  word-break: break-all;
}

.status-pill {
  font-size: 12px;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--bg-sunken);
  color: var(--fg-muted);
}
.status-pill.passed { background: var(--ok-soft); color: var(--ok); }
.status-pill.failed, .status-pill.compile_error, .status-pill.timeout { background: var(--bad-soft); color: var(--bad); }
.status-pill.requirements_unmet { background: var(--warn-soft); color: var(--warn); }

.checks { list-style: none; padding: 0; margin: 10px 0 0; font-size: 13px; }
.checks li::before { content: "✓ "; color: var(--ok); }
.checks li.unmet::before { content: "✗ "; color: var(--bad); }

.hints { margin-top: 14px; }

.hint {
  padding: 10px 14px;
  border-left: 3px solid var(--warn);
  background: var(--warn-soft);
  border-radius: 0 8px 8px 0;
  margin-top: 8px;
  font-size: 14.5px;
}

.hint p { margin: 2px 0; }

.solution {
  margin-top: 14px;
}

.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--fg-muted);
  cursor: pointer;
}

.card.flash {
  animation: flash 0.9s ease-out;
}

@keyframes flash {
  0% { box-shadow: 0 0 0 0 rgba(21, 128, 61, 0.0); border-color: var(--ok); }
  25% { box-shadow: 0 0 0 6px rgba(21, 128, 61, 0.25); border-color: var(--ok); }
  100% { box-shadow: var(--shadow); }
}

.continue-row {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}

.chapter-end {
  text-align: center;
  padding: 36px 20px;
  color: var(--fg-muted);
}

.chapter-end h2 { color: var(--fg); margin: 8px 0; }

/* Toasts */

#toasts {
  position: fixed;
  right: 20px;
  bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}

.toast {
  background: var(--fg);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in 0.25s ease-out;
}

.toast.xp { background: var(--ok); color: #fff; }
.toast.level { background: var(--accent); color: var(--accent-fg); font-size: 15px; }
.toast.error { background: var(--bad); color: #fff; }

@keyframes toast-in {
  from { transform: translateY(12px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* Dialog */

.dialog-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: grid;
  place-items: center;
  z-index: 60;
}

.dialog {
  background: var(--bg-elev);
  border-radius: 14px;
  padding: 22px 24px;
  max-width: 420px;
  width: calc(100% - 32px);
  box-shadow: var(--shadow);
}

.dialog h3 { margin: 0 0 8px; }
.dialog p { margin: 6px 0 16px; color: var(--fg-muted); }
.dialog .row { justify-content: flex-end; }

/* Home */

.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
  margin-top: 20px;
}

.home-card {
  display: block;
  text-align: left;
  padding: 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--fg);
  box-shadow: var(--shadow);
}
.home-card:hover { border-color: var(--accent); }
.home-card .num { color: var(--accent); font-weight: 700; font-size: 12px; }
.home-card .t { font-weight: 600; margin: 4px 0; }
.home-card .s { font-size: 13px; color: var(--fg-muted); }
.home-card.soon { opacity: 0.55; }

/* Mobile */

.menu-btn { display: none; }

@media (max-width: 860px) {
  #app { flex-direction: column; }
  #sidebar {
    width: 100%;
    flex: 0 0 auto;
    height: auto;
    position: static;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  #chapter-list { display: none; }
  #sidebar.open #chapter-list { display: block; }
  .menu-btn { display: inline-flex; }
  #content { padding: 20px 16px 100px; }
  .chapter-header h1 { font-size: 24px; }
}

/* ------------------------------------------------------------------ */
/* Exercise editor (CodeMirror, or the plain textarea fallback)        */
/* ------------------------------------------------------------------ */

.editor-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  font-size: 13px;
  color: var(--fg-muted);
}

.editor-bar code {
  background: none;
  padding: 0;
  font-size: 13px;
  word-break: break-all;
}

.editor-bar .spacer { flex: 1; }

.editor {
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
  min-height: 120px;
}

.editor:focus-within { border-color: var(--accent); }

.editor .cm-editor { max-height: 560px; }
.editor .cm-scroller { overflow: auto; }
.editor .cm-gutters { border-right: 1px solid var(--border); }

@media (prefers-color-scheme: light) {
  .editor .cm-gutters { background: var(--bg-sunken); color: var(--fg-muted); }
  .editor .cm-activeLineGutter { background: var(--border); }
  .editor .cm-activeLine { background: rgba(0, 0, 0, 0.03); }
}

.plain-editor {
  display: block;
  width: 100%;
  min-height: 240px;
  margin: 0;
  padding: 12px 14px;
  border: 0;
  background: var(--code-bg);
  color: var(--fg);
  font-family: ui-monospace, "Cascadia Code", "JetBrains Mono", Consolas, "SF Mono", Menlo, monospace;
  font-size: 13.5px;
  line-height: 1.55;
  resize: vertical;
  tab-size: 4;
  outline: none;
  white-space: pre;
  overflow-wrap: normal;
  overflow-x: auto;
}

details.tests { margin-top: 10px; }
details.tests summary {
  cursor: pointer;
  font-size: 13px;
  color: var(--fg-muted);
  user-select: none;
}
details.tests summary:hover { color: var(--fg); }
details.tests pre.code { margin-top: 8px; max-height: 360px; overflow: auto; }

.side-footer {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
}

.side-footer .link-btn { cursor: pointer; }

a.btn { text-decoration: none; }
