/* One dark theme on every device, whatever the system light/dark setting */
:root {
  color-scheme: dark;
  --bg: #12141b;
  --panel: #1a1d26;
  --side: #161921;
  --line: #2a2e3b;
  --text: #e8eaf2;
  --muted: #9aa0b4;
  --accent: #7b8cff;
  --accent-soft: #252b4d;
  --accent-text: #aab5ff;
  --user: #4a5be0;
  --code: #222633;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.3);
  --radius: 14px;
  --ok: #6fd6a0;
  --ok-soft: #1d3a2c;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.65 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
.app { display: flex; height: 100vh; height: 100dvh; }

/* ---------- Course picker ---------- */
.picking .app, body:not(.picking) .picker { display: none; }
.picker { min-height: 100vh; min-height: 100dvh; display: flex; justify-content: center; padding: 11vh 20px 40px; }
.picker-inner { width: 100%; max-width: 760px; }
.picker-brand { display: flex; align-items: center; gap: 10px; font-weight: 650; margin-bottom: 30px; }
.picker h1 { font-size: 28px; line-height: 1.3; margin: 0 0 6px; font-weight: 650; letter-spacing: -.2px; }
.picker-sub { color: var(--muted); margin: 0 0 20px; }
.course-search {
  display: flex; align-items: center; gap: 10px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 6px 6px 6px 14px; box-shadow: var(--shadow);
}
.course-search:focus-within { border-color: var(--accent); }
.course-search svg { flex: none; color: var(--muted); }
.course-search input {
  flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text);
  font: inherit; font-size: 16px; padding: 8px 0;
}
.course-search input::-webkit-search-cancel-button { display: none; }
.go { flex: none; border: 0; background: var(--accent); color: #fff; border-radius: 10px; padding: 8px 20px; font-weight: 600; }
.course-list { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-top: 18px; }
.course {
  text-align: left; border: 1px solid var(--line); background: var(--panel); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); transition: border-color .15s, transform .15s;
}
.course:hover { border-color: var(--accent); transform: translateY(-1px); }
.course-top { display: flex; justify-content: space-between; align-items: center; }
.course .code { font-weight: 700; color: var(--accent-text); letter-spacing: .3px; }
.course .tag { font-size: 11px; color: var(--ok); background: var(--ok-soft); padding: 1px 8px; border-radius: 99px; }
.course-title { font-weight: 650; font-size: 17px; margin: 4px 0 4px; }
.course-desc { color: var(--muted); font-size: 13.5px; line-height: 1.55; }
.course-meta { margin-top: 10px; font-size: 12px; color: var(--muted); }
.picker-note { color: var(--muted); font-size: 14px; margin: 14px 2px 0; }

/* ---------- Sidebar ---------- */
.side {
  width: 280px; flex: none; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 18px 14px 12px;
}
.brand { display: flex; gap: 10px; align-items: center; padding: 0 6px 16px; }
.logo {
  width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 14px; letter-spacing: .5px;
}
.brand-text { min-width: 0; }
.brand-title { font-weight: 650; font-size: 15px; }
.brand-sub { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.new-chat {
  display: flex; align-items: center; justify-content: center; gap: 6px; width: 100%;
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 9px; font-weight: 550;
  box-shadow: var(--shadow);
}
.new-chat:hover { border-color: var(--accent); color: var(--accent-text); }
.side-label {
  display: flex; justify-content: space-between; align-items: center; margin: 20px 6px 6px;
  font-size: 12px; color: var(--muted); font-weight: 600; letter-spacing: .3px;
}
.link { border: 0; background: none; color: var(--accent-text); font-size: 12px; padding: 0; }
.lectures { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.lectures li {
  display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; font-size: 13px;
  cursor: pointer; user-select: none;
}
.lectures li:hover { background: var(--accent-soft); }
.lectures li.on { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }
.lectures .num {
  flex: none; width: 34px; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.lectures li.on .num { color: var(--accent-text); }
.lectures .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lectures .pg { font-size: 11px; color: var(--muted); }
.side-foot { font-size: 12px; color: var(--muted); padding: 10px 6px 0; border-top: 1px solid var(--line); margin-top: 8px; }

/* ---------- Main area ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.top {
  height: 54px; flex: none; display: flex; align-items: center; gap: 10px; padding: 0 20px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.top-title { font-weight: 600; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.switch { flex: none; margin-left: auto; }
.scope-pill .short { display: none; }
.scope-pill {
  font-size: 12px; background: var(--accent-soft); color: var(--accent-text); padding: 2px 10px;
  border-radius: 99px; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex-shrink: 1;
}
.icon-btn { border: 0; background: none; padding: 6px; border-radius: 8px; color: var(--muted); display: grid; place-items: center; }
.icon-btn:hover { background: var(--accent-soft); color: var(--text); }
.menu { display: none; }
.chat { flex: 1; overflow-y: auto; padding: 28px 20px 12px; scroll-behavior: smooth; }
.chat > * { max-width: 820px; margin-left: auto; margin-right: auto; }

.welcome { text-align: center; padding-top: 8vh; }
.welcome h1 { font-size: 26px; margin: 0 0 8px; font-weight: 650; letter-spacing: -.2px; }
.welcome p { color: var(--muted); margin: 0 auto 28px; max-width: 520px; }
.suggest { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }
.suggest button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 12px 14px;
  font-size: 14px; box-shadow: var(--shadow); text-align: left; transition: border-color .15s, transform .15s;
}
.suggest button:hover { border-color: var(--accent); transform: translateY(-1px); }
.suggest small { display: block; color: var(--muted); font-size: 12px; margin-bottom: 2px; }

.msg { margin-bottom: 26px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble {
  background: var(--user); color: #fff; padding: 10px 15px; border-radius: 16px 16px 4px 16px;
  max-width: 80%; white-space: pre-wrap; word-break: break-word;
}
.msg.bot .card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 20px; box-shadow: var(--shadow);
}

/* Retrieval trace */
.trace { margin: -2px 0 12px; font-size: 13px; color: var(--muted); }
.trace summary { cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px; user-select: none; }
.trace summary::-webkit-details-marker { display: none; }
.trace summary::before { content: "▸"; font-size: 11px; transition: transform .15s; }
.trace[open] summary::before { transform: rotate(90deg); }
.trace ol { margin: 8px 0 0; padding: 0 0 0 4px; list-style: none; border-left: 2px solid var(--line); }
.trace li { padding: 3px 0 3px 12px; }
.trace .q { color: var(--text); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }
.spinner {
  width: 12px; height: 12px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  display: inline-block; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Answer body */
.answer { word-break: break-word; }
.answer > :first-child { margin-top: 0; }
.answer > :last-child { margin-bottom: 0; }
.answer h1, .answer h2, .answer h3, .answer h4 { font-size: 16px; margin: 18px 0 8px; }
.answer p { margin: 8px 0; }
.answer ul, .answer ol { padding-left: 22px; margin: 8px 0; }
.answer li { margin: 3px 0; }
.answer code { background: var(--code); padding: 1px 5px; border-radius: 5px; font-size: 13px; font-family: ui-monospace, Menlo, monospace; }
.answer pre { background: var(--code); padding: 12px 14px; border-radius: 10px; overflow-x: auto; }
.answer pre code { background: none; padding: 0; }
.answer table { border-collapse: collapse; margin: 10px 0; font-size: 14px; display: block; overflow-x: auto; }
.answer th, .answer td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.answer th { background: var(--code); }
.answer blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--line); color: var(--muted); }
.answer .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.cite {
  display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 4px; margin: 0 1px;
  border: 0; border-radius: 5px; background: var(--accent-soft); color: var(--accent-text);
  font-size: 11px; font-weight: 650; vertical-align: 2px; line-height: 1;
}
.cite:hover { background: var(--accent); color: #fff; }
.caret::after { content: "▋"; color: var(--accent); animation: blink 1s steps(1) infinite; margin-left: 2px; }
@keyframes blink { 50% { opacity: 0; } }
.err { color: #ff8a8a; }

/* Source cards */
.sources { margin-top: 14px; padding-top: 12px; border-top: 1px dashed var(--line); }
.sources-h { font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 8px; }
.src-list { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; }
.src {
  flex: none; width: 190px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
  padding: 8px 10px; text-align: left; font-size: 12px; line-height: 1.45;
}
.src:hover { border-color: var(--accent); }
.src .n { display: inline-grid; place-items: center; min-width: 17px; height: 17px; border-radius: 4px; background: var(--accent); color: #fff; font-size: 10.5px; font-weight: 700; margin-right: 4px; }
.src .l { color: var(--muted); }
.src .t { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; margin-top: 3px; font-weight: 550; color: var(--text); }
.src.dim { opacity: .55; }
.meta { margin-top: 10px; font-size: 11.5px; color: var(--muted); }

/* Input box */
.composer { flex: none; padding: 8px 20px calc(14px + env(safe-area-inset-bottom)); background: linear-gradient(to top, var(--bg) 70%, transparent); }
.composer .box {
  max-width: 820px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow);
}
.composer .box:focus-within { border-color: var(--accent); }
textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text);
  font: inherit; max-height: 180px; padding: 6px 0;
}
.send {
  flex: none; width: 38px; height: 38px; border-radius: 11px; border: 0; background: var(--accent); color: #fff;
  display: grid; place-items: center;
}
.send:disabled { opacity: .4; cursor: default; }
.hint { max-width: 820px; margin: 6px auto 0; text-align: center; font-size: 11.5px; color: var(--muted); }

/* Slide viewer */
.modal { position: fixed; inset: 0; background: rgba(10, 12, 20, .55); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal[hidden] { display: none; }
.modal-card { background: var(--panel); border-radius: 16px; width: min(960px, 100%); max-height: 100%; display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 14px 16px 10px; }
.modal-lec { font-size: 12px; color: var(--muted); }
.modal-title { font-weight: 600; }
.modal-img { background: #fff; display: grid; place-items: center; min-height: 200px; overflow: auto; }
.modal-img img { width: 100%; height: auto; display: block; }
.modal-nav { display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; font-size: 13px; color: var(--muted); }
.ghost { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 12px; font-size: 13px; }
.ghost:disabled { opacity: .4; cursor: default; }

/* Narrow screens */
@media (max-width: 860px) {
  .side { position: fixed; z-index: 40; inset: 0 auto 0 0; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .side.open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 30; display: none; }
  .scrim.show { display: block; }
  .menu { display: grid; }
  .top { padding: 0 12px; }
  .chat { padding: 18px 16px 8px; }
  .composer { padding-left: 12px; padding-right: 12px; }
  .suggest { grid-template-columns: 1fr; }
  .welcome { padding-top: 3vh; }
  .welcome h1 { font-size: 22px; }
  .picker { padding-top: 6vh; }
  .picker h1 { font-size: 23px; }
  .course-list { grid-template-columns: 1fr; }
  .top-title .ttl, .scope-pill .long { display: none; }
  .scope-pill .short { display: inline; }
  .msg.bot .card { padding: 14px 14px; }
  .msg.user .bubble { max-width: 90%; }
}
