:root {
  --bg: #fafaf7;
  --surface: #ffffff;
  --surface-2: #f2f1ec;
  --text: #1f1e1b;
  --muted: #5f5c55;
  --border: #e3e1d9;
  --border-strong: #b9b5aa;
  --accent: #c2410c;
  --accent-soft: #fdeee6;
  --good: #15803d;
  --good-soft: #e7f5ec;
  --bad: #b91c1c;
  --bad-soft: #fdecec;
  --code-bg: #1e1d1a;
  --code-text: #f3f1ea;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 8px rgba(0, 0, 0, .04);
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #171614;
    --surface: #201f1c;
    --surface-2: #2a2925;
    --text: #efede6;
    --muted: #a8a49a;
    --border: #3a3833;
    --border-strong: #5c5850;
    --accent: #fb923c;
    --accent-soft: #3a2415;
    --good: #4ade80;
    --good-soft: #15291c;
    --bad: #f87171;
    --bad-soft: #3a1818;
    --code-bg: #0f0e0c;
  }
}

:root[data-theme="dark"] {
  --bg: #171614;
  --surface: #201f1c;
  --surface-2: #2a2925;
  --text: #efede6;
  --muted: #a8a49a;
  --border: #3a3833;
  --border-strong: #5c5850;
  --accent: #fb923c;
  --accent-soft: #3a2415;
  --good: #4ade80;
  --good-soft: #15291c;
  --bad: #f87171;
  --bad-soft: #3a1818;
  --code-bg: #0f0e0c;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  line-height: 1.65;
  font-size: 16px;
}

a { color: var(--accent); }
a:hover { text-decoration: none; }

code {
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .9em;
  background: var(--surface-2);
  padding: .1em .35em;
  border-radius: 4px;
}

pre {
  background: var(--code-bg);
  color: var(--code-text);
  padding: 16px;
  border-radius: var(--radius);
  overflow-x: auto;
  font-size: 14px;
  line-height: 1.55;
  margin: 0;
}
pre code { background: none; padding: 0; color: inherit; font-size: inherit; }

/* ---------- Header ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.site-header .inner {
  max-width: 1180px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.brand {
  font-weight: 700;
  color: var(--text);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand .dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent);
}
.nav { display: flex; gap: 4px; flex-wrap: wrap; margin-left: auto; }
.nav a {
  color: var(--muted);
  text-decoration: none;
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 15px;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.theme-toggle {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font: inherit;
  font-size: 14px;
}

/* ---------- Layout ---------- */
.container { max-width: 1180px; margin: 0 auto; padding: 32px 16px 64px; }
.narrow { max-width: 820px; }

.hero { padding: 24px 0 8px; }
.hero h1 { font-size: clamp(28px, 5vw, 42px); line-height: 1.2; margin: 0 0 12px; }
.hero p { color: var(--muted); font-size: 18px; margin: 0 0 20px; max-width: 720px; }

h2 { font-size: 24px; margin: 40px 0 12px; line-height: 1.3; }
h3 { font-size: 19px; margin: 28px 0 8px; }

.muted { color: var(--muted); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  text-decoration: none;
  border: none;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
}
.btn:hover { filter: brightness(1.06); }
.btn.secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn.small { padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
}

.grid { display: grid; gap: 16px; }
.grid.cols-3 { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); }

.stat-row { display: flex; gap: 16px; flex-wrap: wrap; margin: 16px 0; }
.stat { flex: 1 1 160px; }
.stat .num { font-size: 28px; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: 14px; }

.progress {
  height: 10px;
  background: var(--surface-2);
  border-radius: 99px;
  overflow: hidden;
}
.progress > span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 99px;
  transition: width .3s;
}

.tag {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 99px;
  background: var(--surface-2);
  color: var(--muted);
}
.tag.accent { background: var(--accent-soft); color: var(--accent); }
.tag.good { background: var(--good-soft); color: var(--good); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 15px;
  margin: 12px 0;
}
th, td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
th { background: var(--surface-2); font-weight: 600; }
.table-wrap { overflow-x: auto; }

/* ---------- Month cards ---------- */
.month-card h3 { margin: 6px 0 6px; }
.month-card ul { padding-left: 18px; margin: 8px 0 12px; }
.month-card li { margin: 2px 0; }
.month-card li a { text-decoration: none; color: var(--text); }
.month-card li a:hover { color: var(--accent); }
.month-card li.done a::after { content: " ✓"; color: var(--good); font-weight: 700; }

/* ---------- Lesson page ---------- */
.lesson-layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 32px;
}
.sidebar {
  position: sticky;
  top: 72px;
  align-self: start;
  max-height: calc(100vh - 88px);
  overflow-y: auto;
  font-size: 14px;
}
.sidebar details { margin-bottom: 8px; }
.sidebar summary {
  cursor: pointer;
  font-weight: 600;
  padding: 6px 8px;
  border-radius: 6px;
}
.sidebar summary:hover { background: var(--surface-2); }
.sidebar ol { list-style: none; padding: 0 0 0 8px; margin: 4px 0; }
.sidebar li a {
  display: block;
  padding: 5px 8px;
  border-radius: 6px;
  text-decoration: none;
  color: var(--muted);
}
.sidebar li a:hover { background: var(--surface-2); color: var(--text); }
.sidebar li a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.sidebar li.done a::before { content: "✓ "; color: var(--good); font-weight: 700; }

.lesson-meta { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.lesson { min-width: 0; overflow-wrap: anywhere; }
.lesson h1 { font-size: clamp(26px, 4vw, 34px); line-height: 1.25; margin: 4px 0 12px; }
.lesson section { margin-bottom: 8px; }
.lesson ul, .lesson ol { padding-left: 22px; }
.lesson li { margin: 4px 0; }

.objectives {
  background: var(--accent-soft);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin: 16px 0 24px;
}
.objectives h2 { margin: 0 0 6px; font-size: 17px; }
.objectives ul { margin: 0; }

.callout {
  border-left: 4px solid var(--accent);
  background: var(--surface);
  padding: 12px 16px;
  border-radius: 0 var(--radius) var(--radius) 0;
  margin: 16px 0;
}
.callout.tip { border-color: var(--good); }
.callout.warn { border-color: var(--bad); }

.code-block { margin: 16px 0; }
.code-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 6px 12px;
  font-size: 13px;
  color: var(--muted);
}
.code-head + pre { border-radius: 0 0 var(--radius) var(--radius); }
.copy-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 6px;
  font-size: 12px;
  padding: 2px 8px;
  cursor: pointer;
}

.exercise { margin: 16px 0; }
.exercise-head { display: flex; align-items: center; gap: 10px; }
.exercise-head h3 { margin: 0; font-size: 17px; }
.exercise-head input { width: 18px; height: 18px; accent-color: var(--good); }
.exercise details { margin-top: 10px; }
.exercise summary {
  cursor: pointer;
  color: var(--accent);
  font-weight: 600;
  font-size: 14px;
}
.exercise details > div { margin-top: 8px; }

.quiz-q { margin: 18px 0; }
.quiz-q .q { font-weight: 600; margin-bottom: 8px; }
.quiz-q label {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 6px 0;
  cursor: pointer;
  background: var(--surface);
}
.quiz-q label:hover { background: var(--surface-2); }
.quiz-q input { margin-top: 5px; accent-color: var(--accent); }
.quiz-q label.correct { border-color: var(--good); background: var(--good-soft); }
.quiz-q label.wrong { border-color: var(--bad); background: var(--bad-soft); }
.quiz-q .explain {
  display: none;
  font-size: 14px;
  color: var(--muted);
  margin-top: 6px;
  padding-left: 4px;
}
.quiz-q.revealed .explain { display: block; }
.quiz-result { font-weight: 700; margin-top: 12px; }

.lesson-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

/* ---------- Exam page ---------- */
.exam-bar {
  position: sticky;
  top: 60px;
  z-index: 5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 16px;
  margin: 16px 0;
  box-shadow: var(--shadow);
}
.timer { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 18px; }
.timer.low { color: var(--bad); }
.score-big { font-size: 44px; font-weight: 800; line-height: 1.1; }
.pass { color: var(--good); }
.fail { color: var(--bad); }

.steps { counter-reset: step; list-style: none; padding: 0; }
.steps li {
  counter-increment: step;
  position: relative;
  padding: 0 0 18px 48px;
}
.steps li::before {
  content: counter(step);
  position: absolute;
  left: 0; top: 0;
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
}

.site-footer {
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 14px;
  text-align: center;
  padding: 24px 16px;
}

@media (max-width: 860px) {
  .lesson-layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; max-height: none; }
  .sidebar-toggle { display: inline-flex; }
  .sidebar.collapsed { display: none; }
}
.sidebar-toggle { display: none; margin-bottom: 12px; }

/* ---------- Flow diagrams, examples, recap ---------- */
.flow {
  margin: 16px 0 24px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-x: auto;
}
.flow-svg { display: block; width: 100%; min-width: 560px; height: auto; }
.example-card { margin: 14px 0; }
.example-card h3 { margin: 0 0 6px; font-size: 17px; }
.example-card .tag { margin-bottom: 6px; }
.recap {
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  padding: 18px 20px;
  background: var(--surface);
  margin: 24px 0;
}
.recap h2 { margin-top: 0; }
.recap .cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 20px; }
.recap h3 { margin: 4px 0 8px; font-size: 16px; }
.recap ul { margin: 0; padding-left: 20px; }
.recap .tips li::marker { content: "💡 "; }
.tree {
  font-family: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13.5px;
  line-height: 1.6;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  overflow-x: auto;
  white-space: pre;
}
.tree .c { color: var(--muted); }
.tree .d { color: var(--accent); font-weight: 600; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.legend span::before { content: ""; display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; border: 1.5px solid var(--border-strong); }
.legend .l-start::before { background: var(--accent-soft); border-color: var(--accent); border-radius: 6px; }
.legend .l-step::before { background: var(--surface); }
.legend .l-dec::before { background: var(--surface-2); border-color: var(--accent); border-style: dashed; }
.legend .l-end::before { background: var(--good-soft); border-color: var(--good); border-radius: 6px; }
.legend .l-loop::before { border: none; border-top: 2px solid var(--accent); height: 0; width: 16px; border-radius: 0; }
.tree a { color: var(--text); text-decoration: none; border-bottom: 1px dotted var(--border-strong); }
.tree a:hover { color: var(--accent); }

/* ---------- Question bank ---------- */
.bank-bar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; }
.bank-levels { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 99px;
  padding: 4px 12px;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.chip.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); font-weight: 600; }
.bank-stats { font-size: 14px; color: var(--muted); }
.bank-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
.quiz-q .q pre { margin: 8px 0; font-weight: 400; }
