:root {
  --bg: #ffffff;
  --bg-soft: #f6f8fb;
  --bg-active: #eef4ff;
  --border: #e3e7ee;
  --text: #1d2433;
  --muted: #667085;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --done: #1f9d55;
  --bubble-user: #eef4ff;
  --warn: #b54708;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151c;
    --bg-soft: #181c25;
    --bg-active: #1d2a44;
    --border: #2a303c;
    --text: #e6e9ef;
    --muted: #98a2b3;
    --accent: #5b8ff9;
    --accent-text: #0b0e14;
    --done: #3ccf7e;
    --bubble-user: #1d2a44;
    --warn: #f79009;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 20px; font-weight: 600; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.learner { color: var(--muted); font-size: 14px; cursor: pointer; }

.layout { flex: 1; display: flex; min-height: 0; }
.sidebar {
  width: 290px; flex-shrink: 0; border-right: 1px solid var(--border);
  overflow-y: auto; background: var(--bg);
}
.sidebar.collapsed { display: none; }
.overall { padding: 14px 16px; border-bottom: 1px solid var(--border); font-size: 13px; color: var(--muted); }
.overall-row { display: flex; justify-content: space-between; margin-bottom: 8px; }
.bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--done); width: 0; transition: width .3s; }

.module-head { padding: 14px 16px 6px; font-weight: 600; }
.module-sub { font-size: 12px; color: var(--muted); font-weight: 400; }
.lesson-link {
  display: flex; gap: 10px; align-items: flex-start; width: 100%;
  padding: 10px 16px 10px 20px; border: 0; background: none; text-align: left; cursor: pointer;
  border-left: 3px solid transparent;
}
.lesson-link:hover { background: var(--bg-soft); }
.lesson-link.active { background: var(--bg-active); border-left-color: var(--accent); }
.lesson-link .num { font-size: 12px; color: var(--muted); display: block; }
.lesson-link .score { font-size: 12px; color: var(--done); display: block; }
.status {
  width: 18px; height: 18px; border-radius: 50%; flex-shrink: 0; margin-top: 2px;
  border: 2px solid var(--border); display: grid; place-items: center; font-size: 11px;
}
.status.in_progress { border-color: var(--accent); border-right-color: transparent; }
.status.completed { background: var(--done); border-color: var(--done); color: #fff; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.lesson-head { display: flex; align-items: center; gap: 12px; padding: 12px 20px 6px; }
.lesson-title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-btns { display: flex; gap: 8px; }
.tabs { display: flex; align-items: center; gap: 4px; padding: 4px 20px 10px; border-bottom: 1px solid var(--border); }
.tab { border: 0; background: none; padding: 6px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.tab.active { background: var(--bg-active); color: var(--accent); font-weight: 600; }
.tab-spacer { flex: 1; }
.link-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 13px; text-decoration: underline; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); display: inline-grid; place-items: center; cursor: pointer; font-size: 18px; flex-shrink: 0;
}
.icon-btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.icon-btn.on { background: var(--bg-active); border-color: var(--accent); }
.icon-btn:disabled { opacity: .5; cursor: default; }
.icon-btn.listening { animation: pulse 1s infinite; border-color: #e5484d; }
@keyframes pulse { 50% { box-shadow: 0 0 0 5px rgba(229, 72, 77, .25); } }

.pane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.pane.hidden { display: none; }

.chat { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 16px; }
.msg { max-width: 760px; width: 100%; margin: 0 auto; }
.msg.assistant { display: flex; gap: 12px; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; font-size: 22px;
  display: grid; place-items: center; background: var(--bg-soft); border: 2px solid var(--accent);
}
.msg.assistant .body { flex: 1; min-width: 0; }
.msg.assistant .name { font-weight: 700; margin-bottom: 2px; }
.msg.user .body {
  margin-left: auto; width: fit-content; max-width: 80%;
  background: var(--bubble-user); padding: 10px 14px; border-radius: 14px 14px 4px 14px; white-space: pre-wrap;
}
.msg.flagged .body { border: 1px solid var(--warn); }
.msg .body p:first-child { margin-top: 0; }
.msg .body p:last-child { margin-bottom: 0; }
.msg .body table { border-collapse: collapse; }
.msg .body td, .msg .body th { border: 1px solid var(--border); padding: 4px 8px; }
.msg .thumbs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.msg .thumbs img { max-height: 120px; border-radius: 8px; }
.file-chip { font-size: 13px; padding: 2px 8px; border: 1px solid var(--border); border-radius: 6px; }
.typing span { display: inline-block; width: 7px; height: 7px; margin-right: 4px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 50% { opacity: .2; } }
.error { color: var(--warn); font-size: 14px; max-width: 760px; margin: 0 auto; width: 100%; }

.disclaimer { text-align: center; font-size: 11px; color: var(--muted); padding: 2px; font-style: italic; }
.composer {
  margin: 6px auto 16px; width: calc(100% - 32px); max-width: 790px;
  border: 1px solid var(--border); border-radius: 16px; padding: 10px 12px; background: var(--bg);
}
.composer textarea { width: 100%; border: 0; outline: 0; resize: none; background: transparent; }
.composer-row { display: flex; gap: 8px; align-items: center; }
.grow { flex: 1; }
.attach-row { display: flex; gap: 6px; flex-wrap: wrap; }
.attach-row:empty { display: none; }
.attach-row .file-chip { cursor: pointer; }

.resources { display: flex; flex-wrap: wrap; gap: 8px; padding: 12px 20px; border-bottom: 1px solid var(--border); }
.resources a { font-size: 14px; color: var(--accent); }
.resources video { width: 100%; max-width: 520px; border-radius: 8px; }
#course-frame { flex: 1; border: 0; width: 100%; background: #fff; }

dialog { border: 1px solid var(--border); border-radius: 16px; background: var(--bg); color: var(--text); padding: 24px; width: min(380px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
dialog h2 { margin: 0; }
dialog p { margin: 0; color: var(--muted); }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
dialog input, dialog select { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg-soft); }
.btn-primary { padding: 10px; border: 0; border-radius: 10px; background: var(--accent); color: var(--accent-text); font-weight: 600; cursor: pointer; }

@media (max-width: 760px) {
  .sidebar { position: fixed; z-index: 5; top: 57px; bottom: 0; left: 0; box-shadow: 0 0 24px rgba(0,0,0,.2); }
  .lesson-head, .tabs { padding-left: 16px; padding-right: 16px; }
  .chat { padding: 16px; }
}
.lesson-link > span:last-child { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.icon-btn svg { width: 18px; height: 18px; }
