:root {
  --bg-page: #f4f5f9;
  --bg-dark: #1e2129;
  --bg-dark-2: #262a35;
  --bg-editor: #1b1f28;
  --bg-sidebar: #171a21;
  --accent: #635bff;
  --accent-soft: #e9e8ff;
  --success: #00b38a;
  --success-soft: #e3f7f0;
  --danger: #e5484d;
  --danger-soft: #fdecec;
  --warn: #e6a23c;
  --text: #2b2f3a;
  --text-mid: #5a6070;
  --text-light: #a8adb8;
  --border: #e3e6ee;
  --mono: "SFMono-Regular", "JetBrains Mono", "Fira Code", Consolas, "Liberation Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --radius: 10px;
  --topbar-h: 52px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg-page);
  overflow: hidden;
}

button { font-family: inherit; cursor: pointer; }

.hidden { display: none !important; }

/* ---------- Topbar ---------- */
#topbar {
  height: var(--topbar-h);
  background: var(--bg-dark);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  position: relative;
  z-index: 30;
}

.icon-btn {
  background: transparent;
  border: none;
  color: #cfd3de;
  padding: 6px;
  border-radius: 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: rgba(255,255,255,.08); color: #fff; }

.brand { display: flex; align-items: baseline; gap: 8px; white-space: nowrap; }
.brand-logo {
  font-family: var(--mono);
  font-weight: 700;
  color: var(--accent);
  background: rgba(99,91,255,.18);
  padding: 2px 7px;
  border-radius: 6px;
  font-size: 14px;
}
.brand-name { font-weight: 700; font-size: 16px; letter-spacing: .3px; }
.brand-sub { color: var(--text-light); font-size: 12px; }

.topbar-center { flex: 1; display: flex; justify-content: center; max-width: 420px; margin: 0 auto; }
.top-progress { display: flex; align-items: center; gap: 10px; width: 100%; }
.top-progress-track { flex: 1; height: 6px; background: rgba(255,255,255,.14); border-radius: 3px; overflow: hidden; }
.top-progress-fill { height: 100%; width: 0%; background: linear-gradient(90deg, var(--accent), #8b5cf6); border-radius: 3px; transition: width .3s ease; }
.top-progress-label { font-size: 12px; color: var(--text-light); min-width: 34px; text-align: right; }

.topbar-actions { display: flex; gap: 8px; }

.btn {
  border: none;
  border-radius: 7px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: filter .15s ease, transform .05s ease;
}
.btn:active { transform: translateY(1px); }
.btn-run { background: var(--accent); color: #fff; }
.btn-run:hover { filter: brightness(1.08); }
.btn-ghost { background: transparent; color: #cfd3de; border: 1px solid rgba(255,255,255,.22); }
.btn-ghost:hover { background: rgba(255,255,255,.08); color: #fff; }

/* ---------- Shell layout ---------- */
#shell { display: flex; height: calc(100vh - var(--topbar-h)); }

#sidebar {
  width: 290px;
  min-width: 290px;
  background: var(--bg-sidebar);
  color: #c9cdd6;
  overflow-y: auto;
  transition: margin-left .2s ease;
}
#sidebar.closed { margin-left: -290px; }

#course-nav { padding: 12px 10px 40px; }

.nav-heading { display: flex; align-items: center; gap: 8px; padding: 10px 10px 6px; color: #8b90a0; font-size: 11px; font-weight: 700; letter-spacing: 1.2px; text-transform: uppercase; }

.nav-module { margin: 4px 0; border-radius: 8px; }
.nav-module-head {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  background: transparent;
  border: none;
  color: #d5d9e2;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
}
.nav-module-head:hover { background: rgba(255,255,255,.06); }
.nav-module-head .caret { margin-left: auto; transition: transform .15s ease; color: #7c8191; display: inline-flex; }
.nav-module.open .caret { transform: rotate(90deg); }
.nav-module-index {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--accent);
  background: rgba(99,91,255,.16);
  border-radius: 5px;
  padding: 1px 6px;
  min-width: 22px;
  text-align: center;
}
.nav-module.done .nav-module-head { color: var(--success); }

.nav-lessons { display: none; padding-left: 6px; }
.nav-module.open .nav-lessons { display: block; }

.nav-lesson {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  background: transparent;
  border: none;
  color: #b8bdca;
  padding: 7px 10px 7px 14px;
  border-radius: 7px;
  font-size: 13px;
  text-align: left;
}
.nav-lesson:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-lesson.active { background: rgba(99,91,255,.22); color: #fff; }
.nav-lesson .lnum { font-family: var(--mono); font-size: 11px; color: #7c8191; min-width: 18px; }
.nav-lesson .ltitle { flex: 1; }
.nav-lesson .ldone { color: var(--success); display: inline-flex; }

/* ---------- Main ---------- */
#main { flex: 1; display: flex; min-width: 0; }

.pane { min-width: 0; }

#lesson-pane {
  width: 44%;
  max-width: 640px;
  min-width: 360px;
  background: #fff;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 28px 30px 60px;
}

#workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-editor);
}

/* ---------- Lesson content ---------- */
.lesson-breadcrumb { font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase; color: var(--accent); margin: 0 0 6px; }
.lesson-title { font-size: 26px; line-height: 1.25; margin: 0 0 4px; color: var(--text); }
.lesson-meta { display: flex; align-items: center; gap: 12px; color: var(--text-mid); font-size: 12.5px; margin-bottom: 18px; }
.lesson-meta .est { display: inline-flex; align-items: center; gap: 5px; }
.lesson-meta .objective { flex: 1; }

.lesson-content h2 { font-size: 19px; margin: 26px 0 8px; }
.lesson-content h3 { font-size: 16px; margin: 20px 0 6px; }
.lesson-content p { margin: 10px 0; }
.lesson-content ul, .lesson-content ol { margin: 10px 0; padding-left: 22px; }
.lesson-content li { margin: 4px 0; }
.lesson-content code:not(pre code) {
  font-family: var(--mono);
  font-size: .9em;
  background: #f0f1f6;
  color: #b5266b;
  padding: 1px 5px;
  border-radius: 4px;
}
.lesson-content pre.code {
  background: #10131a;
  color: #dce1ea;
  border-radius: 8px;
  padding: 14px 16px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.6;
  margin: 12px 0;
  tab-size: 4;
}
.lesson-content pre.code code { background: transparent; padding: 0; color: inherit; font-size: inherit; }

.lesson-content .tip {
  background: var(--accent-soft);
  border-left: 4px solid var(--accent);
  border-radius: 0 8px 8px 0;
  padding: 10px 14px;
  margin: 12px 0;
  font-size: 13px;
}
.lesson-content .tip strong { color: #3b349c; }
.lesson-content .warn-box {
  background: #fff6e5;
  border-left: 4px solid var(--warn);
  border-radius: 0 8px 8px 0;
  padding: 10px 14px;
  margin: 12px 0;
  font-size: 13px;
}
.lesson-content table.inline { border-collapse: collapse; margin: 12px 0; width: 100%; font-size: 13px; }
.lesson-content table.inline th, .lesson-content table.inline td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.lesson-content table.inline th { background: #f6f7fb; }

.tasks {
  list-style: none;
  padding: 0;
  margin: 12px 0 6px;
  counter-reset: task;
}
.tasks li {
  counter-increment: task;
  position: relative;
  padding: 8px 12px 8px 46px;
  margin: 6px 0;
  background: #fafbfd;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.tasks li::before {
  content: counter(task);
  position: absolute;
  left: 12px;
  top: 9px;
  width: 22px;
  height: 22px;
  background: var(--accent);
  color: #fff;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tasks li code { font-family: var(--mono); font-size: .9em; background: #eef0f6; padding: 1px 4px; border-radius: 3px; }

.lesson-nav { display: flex; justify-content: space-between; margin-top: 34px; padding-top: 20px; border-top: 1px solid var(--border); gap: 10px; }
.lesson-nav .btn { border-radius: 8px; padding: 9px 16px; font-size: 13.5px; }
.btn-next { background: var(--success); color: #fff; }
.btn-next:hover { filter: brightness(1.07); }
.btn-prev { background: #fff; color: var(--text-mid); border: 1px solid var(--border); }
.btn-prev:hover { background: #f5f6fa; }
.btn-next:disabled, .btn-prev:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- Quiz ---------- */
.quiz {
  margin-top: 26px;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: #fbfcff;
}
.quiz-head {
  background: linear-gradient(90deg, var(--accent), #7a5cf0);
  color: #fff;
  padding: 10px 14px;
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.quiz-q { padding: 14px 16px 4px; font-weight: 600; }
.quiz-opts { padding: 6px 16px 14px; display: flex; flex-direction: column; gap: 8px; }
.quiz-opt {
  display: flex;
  align-items: center;
  gap: 10px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13px;
  background: #fff;
  text-align: left;
  transition: border-color .12s ease, background .12s ease;
}
.quiz-opt:hover { border-color: #b9b3ff; }
.quiz-opt .marker {
  width: 18px; height: 18px;
  border-radius: 50%;
  border: 2px solid #c7cbe0;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: transparent;
}
.quiz-opt.correct { border-color: var(--success); background: var(--success-soft); }
.quiz-opt.correct .marker { border-color: var(--success); background: var(--success); color: #fff; }
.quiz-opt.wrong { border-color: var(--danger); background: var(--danger-soft); }
.quiz-opt.wrong .marker { border-color: var(--danger); background: var(--danger); color: #fff; }
.quiz-opt.wrong .marker::after { content: "\2715"; }
.quiz-opt.correct .marker::after { content: "\2713"; }
.quiz-feedback { display: none; padding: 0 16px 14px; font-size: 13px; }
.quiz-feedback.show { display: block; }
.quiz-feedback.ok { color: var(--success); }
.quiz-feedback.bad { color: var(--danger); }

/* ---------- Editor ---------- */
#editor { flex: 1 1 55%; min-height: 0; display: flex; flex-direction: column; border-bottom: 1px solid #2a2f3b; }

.panel-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  background: #14171f;
  padding: 6px 10px 0;
  min-height: 38px;
  flex-shrink: 0;
}
.ptab {
  background: transparent;
  border: none;
  color: #8b90a0;
  font-size: 12px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 6px 6px 0 0;
  font-family: var(--mono);
}
.ptab:hover { color: #cfd3de; background: rgba(255,255,255,.05); }
.ptab.active { color: #fff; background: var(--bg-editor); box-shadow: inset 0 2px 0 var(--accent); }
.panel-tabs-spacer { flex: 1; }
.linky {
  background: transparent;
  border: none;
  color: #9aa0ae;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 8px;
  border-radius: 6px;
}
.linky:hover { color: #fff; background: rgba(255,255,255,.07); }

.editor-files { flex: 1; min-height: 0; position: relative; }

.editor-file { position: absolute; inset: 0; }
.editor-file.hidden { display: none; }

/* Editor core styles (highlight + textarea overlay) are applied
   inline by js/editor.js so the overlay stays aligned. */

/* highlight token colors (dark editor) */
.tok-comment { color: #6b7280; font-style: italic; }
.tok-tag { color: #e06c75; }
.tok-tag-name { color: #e06c75; }
.tok-attr { color: #d19a66; }
.tok-string { color: #98c379; }
.tok-keyword { color: #c678dd; }
.tok-number { color: #d19a66; }
.tok-selector { color: #e5c07b; }
.tok-class { color: #61afef; }
.tok-prop { color: #d19a66; }
.tok-value { color: #98c379; }
.tok-func { color: #61afef; }

/* ---------- Output ---------- */
#output { flex: 1 1 45%; min-height: 0; display: flex; flex-direction: column; }

#output-view { flex: 1; min-height: 0; position: relative; }
.output-pane { position: absolute; inset: 0; display: none; }
.output-pane.active { display: block; }

#preview-pane { background: #fff; }
#preview-frame { width: 100%; height: 100%; border: none; background: #fff; }

#console-pane { background: #10131a; }
#console-output {
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12.5px;
  line-height: 1.6;
  overflow-y: auto;
  height: 100%;
  color: #dce1ea;
}
.console-line { display: flex; gap: 8px; padding: 1px 0; white-space: pre-wrap; word-break: break-word; }
.console-line .prompt { color: #7c8191; flex-shrink: 0; }
.console-line.log .msg { color: #dce1ea; }
.console-line.warn .msg { color: #e5c07b; }
.console-line.error .msg { color: #e06c75; }
.console-line.info .msg { color: #61afef; }
.console-line.group { color: #7c8191; font-style: italic; }
.console-empty { color: #6b7280; padding: 14px; font-style: italic; }

.output-hint { color: #6b7280; font-size: 11px; margin-right: 6px; }

/* ---------- Home / overview ---------- */
.home-hero {
  padding: 34px 34px 10px;
}
.home-hero h1 { font-size: 30px; margin: 0 0 8px; }
.home-hero p { color: var(--text-mid); font-size: 15px; max-width: 600px; margin: 6px 0 18px; }
.home-hero .hero-cta { display: flex; gap: 10px; flex-wrap: wrap; }

.stat-row { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 8px; }
.stat {
  background: var(--accent-soft);
  border-radius: 10px;
  padding: 10px 16px;
  text-align: center;
  min-width: 90px;
}
.stat .n { font-size: 22px; font-weight: 800; color: var(--accent); font-family: var(--mono); }
.stat .l { font-size: 11px; color: var(--text-mid); }

.home-modules { padding: 14px 34px 60px; }
.home-modules h2 { font-size: 18px; margin: 20px 0 10px; }
.home-attribution {
  font-size: 13px;
  color: var(--text-mid);
  padding: 0 34px 40px;
  margin: 0;
}
.home-attribution a { color: var(--accent, #2563eb); }
.module-card {
  display: flex;
  gap: 14px;
  width: 100%;
  text-align: left;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  margin: 8px 0;
  transition: box-shadow .15s ease, transform .15s ease, border-color .15s ease;
}
.module-card:hover { box-shadow: 0 6px 18px rgba(35,40,60,.1); transform: translateY(-1px); border-color: #c7c2ff; }
.module-card .mc-index {
  font-family: var(--mono);
  font-size: 12px;
  color: #fff;
  background: var(--accent);
  border-radius: 8px;
  padding: 2px 8px;
  height: fit-content;
}
.module-card .mc-body { flex: 1; }
.module-card .mc-title { font-weight: 700; font-size: 15px; color: var(--text); }
.module-card .mc-desc { font-size: 13px; color: var(--text-mid); margin: 3px 0 6px; }
.module-card .mc-progress { font-size: 11.5px; color: var(--success); font-weight: 600; }
.module-card .mc-arrow { color: var(--accent); align-self: center; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-dark);
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 100;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
  animation: toastIn .2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translateX(-50%) translateY(8px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: rgba(120,128,150,.4); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: rgba(120,128,150,.6); }
::-webkit-scrollbar-track { background: transparent; }
#lesson-pane::-webkit-scrollbar-thumb { background: #c9cedb; }
#sidebar::-webkit-scrollbar-thumb { background: #3a3f4d; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  #lesson-pane { width: 46%; min-width: 320px; }
}
@media (max-width: 900px) {
  #main { flex-direction: column; }
  #lesson-pane { width: 100%; max-width: none; border-right: none; border-bottom: 1px solid var(--border); height: 46vh; }
  #workspace { height: 54vh; }
  .brand-sub { display: none; }
}

/* ---------- AI Reviewer ---------- */
#review-pane { background: #fff; overflow: auto; }
.review-inner { padding: 20px 24px; max-width: 720px; }
.review-hero h3 { margin: 0 0 6px; font-size: 16px; }
.review-hero p { margin: 0 0 14px; color: var(--text-mid); font-size: 13px; }
.review-actions { margin-top: 16px; }
.review-actions .btn-run { background: var(--accent); }
.review-status { padding: 8px 12px; border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
.review-status.loading { background: var(--accent-soft); color: #4338ca; }
.review-status.error { background: var(--danger-soft); color: var(--danger); }
.review-status.ok { background: var(--success-soft); color: var(--success); }
.review-output {
  white-space: pre-wrap;
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--text);
  background: #fbfbfd;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 18px;
  min-height: 60px;
}

/* ---------- Settings modal ---------- */
.modal { position: fixed; inset: 0; z-index: 60; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 17, 23, 0.55); }
.modal-card {
  position: relative;
  background: #fff;
  border-radius: 14px;
  width: min(460px, calc(100vw - 40px));
  padding: 24px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.25);
}
.modal-card h2 { margin: 0 0 8px; font-size: 18px; }
.modal-note { color: var(--text-mid); font-size: 12.5px; margin: 6px 0 14px; line-height: 1.5; }
.modal-close {
  position: absolute; top: 12px; right: 14px;
  background: transparent; border: none;
  font-size: 22px; color: var(--text-mid); line-height: 1;
}
.modal-close:hover { color: var(--text); }
.modal-card .field { display: block; margin: 10px 0; }
.modal-card .field span { display: block; font-size: 12px; font-weight: 600; color: var(--text-mid); margin-bottom: 4px; }
.modal-card .field input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 13px;
  color: var(--text);
  background: #fff;
}
.modal-card .field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.btn-ghost-dark { background: transparent; color: var(--text-mid); border: 1px solid var(--border); padding: 8px 16px; border-radius: 8px; }
.btn-ghost-dark:hover { background: #f1f2f6; color: var(--text); }
#ai-settings-status.error { color: var(--danger); }
#ai-settings-status.ok { color: var(--success); }
