/* Giáo trình số tương tác — kiểu dáng học thuật, sáng/tối theo hệ thống. */
:root {
  --bg: #fbfaf7;
  --bg-elev: #ffffff;
  --bg-soft: #f2f0ea;
  --fg: #1d1f24;
  --fg-muted: #5c6270;
  --border: #dedad0;
  --accent: #243b6b;
  --accent-2: #8a2b2b;
  --link: #1f4fa3;
  --focus: #2f6fe0;
  --ok: #2d6a3e;
  --warn-bg: #fff6e0;
  --warn-fg: #6b4a00;
  --err: #a32020;
  --box-def: #2b5797;
  --box-thm: #7a2e8e;
  --box-ex: #2d6a3e;
  --box-rem: #6b6b6b;
  --box-proto: #b5651d;
  --box-exer: #a8325e;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.06);
  --serif: "Source Serif 4", "Source Serif Pro", "Noto Serif", "Georgia", "Times New Roman", serif;
  --sans: "Inter", "Segoe UI", "Roboto", "Noto Sans", system-ui, -apple-system, sans-serif;
  --mono: "JetBrains Mono", "Fira Code", "Consolas", "Menlo", monospace;
  --topbar-h: 56px;
  --sidebar-w: 290px;
  --tutor-w: 400px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171c;
    --bg-elev: #1c1f26;
    --bg-soft: #232731;
    --fg: #e6e6e3;
    --fg-muted: #a0a6b3;
    --border: #343946;
    --accent: #9db7ec;
    --accent-2: #e59a9a;
    --link: #8fb3ff;
    --focus: #8fb3ff;
    --ok: #7fcf95;
    --warn-bg: #3a3020;
    --warn-fg: #f1d38c;
    --err: #ff8f8f;
    --box-def: #7ea6e6;
    --box-thm: #c995da;
    --box-ex: #7fcf95;
    --box-rem: #a0a0a0;
    --box-proto: #e3a465;
    --box-exer: #ec8fb3;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font-family: var(--sans); font-size: 15px; line-height: 1.5;
  overflow: hidden;
}
a { color: var(--link); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 3px; }
button, input, select, textarea { font: inherit; color: inherit; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: -999px; top: 4px; z-index: 100; background: var(--bg-elev); padding: 6px 10px; }
.skip-link:focus { left: 8px; }
.muted { color: var(--fg-muted); }
.error { color: var(--err); }
[hidden] { display: none !important; }

/* --------------------------------------------------------- thanh trên */
.topbar {
  height: var(--topbar-h); display: flex; align-items: center; gap: 12px;
  padding: 0 14px; border-bottom: 1px solid var(--border); background: var(--bg-elev);
  position: relative; z-index: 30;
}
.brand { display: flex; flex-direction: column; text-decoration: none; color: var(--fg); min-width: 0; line-height: 1.15; }
.brand-title { font-family: var(--serif); font-weight: 700; font-size: 18px; color: var(--accent); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { font-size: 12px; color: var(--fg-muted); white-space: nowrap; }
.search { position: relative; flex: 1; max-width: 520px; margin-left: auto; }
.search input {
  width: 100%; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--bg-soft);
}
.search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; max-height: 60vh; overflow: auto;
  margin: 0; padding: 4px; list-style: none; background: var(--bg-elev); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow); z-index: 40;
}
.search-results li { padding: 8px 10px; border-radius: 6px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.search-results li[aria-selected="true"], .search-results li:hover { background: var(--bg-soft); }
.search-results li.empty { cursor: default; color: var(--fg-muted); }
.sr-title { font-weight: 600; }
.sr-snippet { font-size: 13px; color: var(--fg-muted); }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; flex: none;
  border: 1px solid transparent; border-radius: 8px; background: transparent; cursor: pointer;
}
.icon-btn:hover { background: var(--bg-soft); }
.icon-btn svg, .chat-toggle svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.chat-toggle {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 8px;
  border: 1px solid var(--accent); background: transparent; color: var(--accent); cursor: pointer; font-weight: 600; flex: none;
}
.chat-toggle[aria-expanded="true"] { background: var(--accent); color: var(--bg-elev); }
.only-narrow { display: none; }

/* --------------------------------------------------------- bố cục */
.layout {
  display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--tutor-w);
  height: calc(100dvh - var(--topbar-h));
}
.layout.chat-collapsed { grid-template-columns: var(--sidebar-w) minmax(0, 1fr) 0; }
.layout.chat-collapsed .tutor { display: none; }

.sidebar {
  border-right: 1px solid var(--border); background: var(--bg-elev); overflow: auto; position: relative;
  display: flex; flex-direction: column;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px 4px; }
.sidebar-title { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--fg-muted); }
.sidebar-foot { margin: auto 0 0; padding: 12px 14px; font-size: 12px; color: var(--fg-muted); border-top: 1px solid var(--border); }
.toc { padding: 4px 6px 12px; }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc-row { display: flex; align-items: flex-start; gap: 2px; }
.toc-toggle { width: 26px; height: 30px; flex: none; border: 0; background: none; cursor: pointer; border-radius: 6px; display: grid; place-items: center; }
.toc-toggle:hover { background: var(--bg-soft); }
.chev { width: 8px; height: 8px; border-right: 2px solid var(--fg-muted); border-bottom: 2px solid var(--fg-muted); transform: rotate(-45deg); transition: transform .15s; }
.toc-toggle[aria-expanded="true"] .chev { transform: rotate(45deg); }
.toc a { display: block; text-decoration: none; color: var(--fg); padding: 5px 8px; border-radius: 6px; flex: 1; min-width: 0; }
.toc a:hover { background: var(--bg-soft); }
.toc a[aria-current] { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--accent); font-weight: 600; }
.toc-chapter-link { font-weight: 600; }
.toc-chapter-link .toc-num { display: block; font-size: 11px; font-weight: 600; color: var(--fg-muted); text-transform: uppercase; letter-spacing: .05em; }
.toc-sections { margin-left: 26px !important; border-left: 1px solid var(--border); padding-left: 4px !important; }
.toc-sections a { font-size: 14px; padding: 3px 8px; }
.toc-sections .lvl-2 a { padding-left: 20px; font-size: 13px; color: var(--fg-muted); }
.toc-sections .lvl-3 a { padding-left: 32px; font-size: 13px; color: var(--fg-muted); }
.toc-num { font-variant-numeric: tabular-nums; color: var(--fg-muted); }

/* --------------------------------------------------------- trình đọc */
.reader { overflow: auto; scroll-behavior: smooth; outline: none; position: relative; overscroll-behavior: contain; }
@media (prefers-reduced-motion: reduce) { .reader { scroll-behavior: auto; } }
.chapter {
  max-width: 46rem; margin: 0 auto; padding: 28px 32px 80px;
  font-family: var(--serif); font-size: 18px; line-height: 1.7;
  hyphens: auto; overflow-wrap: break-word;
}
.chapter h1, .chapter h2, .chapter h3, .chapter h4 { font-family: var(--serif); line-height: 1.3; color: var(--accent); scroll-margin-top: 16px; position: relative; }
.chapter h1 { font-size: 1.9em; margin: .2em 0 .8em; }
.chapter h2 { font-size: 1.45em; margin: 1.8em 0 .6em; padding-bottom: .2em; border-bottom: 1px solid var(--border); }
.chapter h3 { font-size: 1.2em; margin: 1.5em 0 .5em; }
.chapter h4 { font-size: 1.05em; margin: 1.3em 0 .4em; }
.heading-anchor { margin-left: .35em; font-size: .7em; color: var(--fg-muted); text-decoration: none; opacity: 0; transition: opacity .15s; }
.chapter :is(h1,h2,h3,h4):hover .heading-anchor, .heading-anchor:focus { opacity: 1; }
.chapter [tabindex="-1"]:focus { outline: none; }
.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0% { background: color-mix(in srgb, var(--accent) 22%, transparent); } 100% { background: transparent; } }
.chapter p { margin: .7em 0; }
.chapter code { font-family: var(--mono); font-size: .85em; background: var(--bg-soft); padding: .1em .35em; border-radius: 4px; }
.chapter pre { background: var(--bg-soft); padding: 12px 14px; border-radius: 8px; overflow: auto; font-size: 14px; line-height: 1.5; border: 1px solid var(--border); }
.chapter pre code { background: none; padding: 0; font-size: inherit; }
.chapter blockquote { margin: 1em 0; padding: .2em 1em; border-left: 3px solid var(--border); color: var(--fg-muted); }
.chapter img { max-width: 100%; height: auto; }
.figure { margin: 1.4em auto; text-align: center; }
.figure img { background: #fff; border-radius: 6px; padding: 6px; }
.figure figcaption { font-size: .88em; color: var(--fg-muted); margin-top: .4em; }
.table-wrap { overflow-x: auto; margin: 1em 0; }
.chapter table { border-collapse: collapse; font-size: .9em; margin: 0 auto; }
.chapter th, .chapter td { border: 1px solid var(--border); padding: 6px 10px; }
.chapter th { background: var(--bg-soft); }
.math-display, .katex-display { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.katex { font-size: 1.08em; }
.math-error { color: var(--err); }

/* Hộp định nghĩa / định lý / … */
.box {
  --c: var(--box-rem);
  margin: 1.2em 0; padding: .6em 1em; border-left: 4px solid var(--c);
  background: color-mix(in srgb, var(--c) 7%, var(--bg)); border-radius: 0 8px 8px 0;
}
.box > p:first-child, .box > .box-title { margin-top: .3em; }
.box > p:first-child > strong:first-child, .box-title strong { color: var(--c); font-family: var(--sans); font-size: .92em; letter-spacing: .01em; }
.box-definition { --c: var(--box-def); }
.box-theorem, .box-proposition, .box-lemma, .box-corollary { --c: var(--box-thm); }
.box-theorem, .box-proposition, .box-lemma, .box-corollary { font-style: normal; }
.box-example { --c: var(--box-ex); }
.box-remark, .box-note { --c: var(--box-rem); }
.box-protocol, .box-algorithm { --c: var(--box-proto); }
.box-exercise { --c: var(--box-exer); }
.box-warning { --c: var(--err); }
.box-solution { --c: var(--ok); }
.box-proof { --c: var(--border); background: transparent; border-left-style: dotted; }
.box-proof::after { content: "∎"; display: block; text-align: right; color: var(--fg-muted); }
.exercise-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: .4em 0 .3em; font-family: var(--sans); }
.small-btn {
  display: inline-flex; align-items: center; gap: 4px; font-size: 13px; padding: 4px 12px; border-radius: 999px;
  border: 1px solid var(--box-exer); background: var(--bg-elev); color: var(--box-exer); cursor: pointer; text-decoration: none; font-weight: 600;
}
.small-btn:hover { background: color-mix(in srgb, var(--box-exer) 12%, var(--bg-elev)); }
.small-btn.secondary { border-color: var(--ok); color: var(--ok); }

.chapter-nav { display: flex; justify-content: space-between; gap: 16px; margin-top: 3em; padding-top: 1em; border-top: 1px solid var(--border); font-family: var(--sans); font-size: 14px; }
.chapter-nav a { text-decoration: none; max-width: 48%; }
.chapter-nav .next { text-align: right; margin-left: auto; }
.back-link { font-family: var(--sans); font-size: 14px; }

.cover { text-align: center; padding: 2em 0 1em; border-bottom: 1px solid var(--border); }
.cover h1 { font-size: 2.3em; margin: .2em 0; }
.cover-kicker { text-transform: uppercase; letter-spacing: .12em; font-size: 12px; font-family: var(--sans); color: var(--fg-muted); }
.cover-sub { font-size: 1.25em; font-style: italic; margin: 0; }
.cover-author { font-weight: 600; margin-top: 1em; }
.cover-chapters { padding-left: 0; list-style: none; }
.cover-chapters li { padding: .35em 0; border-bottom: 1px dashed var(--border); }
.cover-chapters a { text-decoration: none; }
.cover-chapters .toc-num { display: inline-block; min-width: 6.5em; font-family: var(--sans); font-size: .8em; text-transform: uppercase; }

/* --------------------------------------------------------- trợ giảng */
.tutor {
  border-left: 1px solid var(--border); background: var(--bg-elev);
  display: flex; flex-direction: column; min-height: 0; min-width: 0; position: relative;
}
.tutor-head { display: flex; align-items: center; gap: 6px; padding: 10px 10px 6px 14px; }
.tutor-title { margin: 0; font-size: 16px; flex: 1; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--bg-soft); color: var(--fg-muted); border: 1px solid var(--border); white-space: nowrap; }
.badge[data-backend="proxy"], .badge[data-backend="apikey"] { color: var(--ok); border-color: var(--ok); }
.mode-row { display: flex; align-items: center; gap: 8px; padding: 0 14px 8px; flex-wrap: wrap; font-size: 13px; border-bottom: 1px solid var(--border); }
.mode-row select { flex: 1; min-width: 150px; padding: 5px 8px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg-soft); }
.link-btn { border: 0; background: none; color: var(--link); cursor: pointer; padding: 0; text-decoration: underline; font-size: 13px; }
.chat-log { flex: 1; overflow: auto; position: relative; overscroll-behavior: contain; padding: 12px; display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.msg { max-width: 100%; }
.msg-body { padding: 8px 12px; border-radius: 12px; overflow-wrap: anywhere; }
.msg-body > :first-child { margin-top: 0; }
.msg-body > :last-child { margin-bottom: 0; }
.msg-body p { margin: .5em 0; }
.msg-body pre { overflow: auto; background: var(--bg-soft); padding: 8px; border-radius: 6px; font-size: 13px; }
.msg-body .katex-display, .msg-body .math-display { overflow-x: auto; }
.msg.user { align-self: flex-end; max-width: 88%; }
.msg.user .msg-body { background: var(--accent); color: var(--bg-elev); border-bottom-right-radius: 4px; }
.msg.user .msg-body a { color: inherit; }
.msg.assistant .msg-body { background: var(--bg-soft); border-bottom-left-radius: 4px; }
.msg.assistant.kind-answer .msg-body { font-family: var(--serif); font-size: 16px; line-height: 1.6; }
.msg.system { align-self: center; font-size: 12px; color: var(--fg-muted); text-align: center; }
.msg .note { font-size: 13px; background: var(--warn-bg); color: var(--warn-fg); padding: 6px 8px; border-radius: 6px; }
.cite { font-weight: 700; text-decoration: none; }
.cite:hover { text-decoration: underline; }
.sources { margin: 6px 4px 0; font-size: 13px; }
.sources-title, .passages-title { margin: 0 0 4px; font-weight: 700; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--fg-muted); }
.sources ol { margin: 0; padding-left: 1.8em; }
.tag { display: inline-block; margin-left: 6px; font-size: 11px; padding: 0 6px; border-radius: 999px; border: 1px solid var(--box-exer); color: var(--box-exer); font-weight: 400; }
.passages { display: flex; flex-direction: column; gap: 6px; }
.passage { background: var(--bg-elev); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; }
.passage summary { cursor: pointer; font-weight: 600; font-size: 14px; }
.passage-body { font-family: var(--serif); font-size: 15px; line-height: 1.6; }
.passage-body .box { margin: .6em 0; }
.passage-body img { max-width: 100%; }
.passage-link { font-size: 13px; }
.cite-num { color: var(--accent); }
.dots { display: inline-flex; gap: 3px; vertical-align: middle; }
.dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--fg-muted); animation: blink 1.2s infinite; }
.dots i:nth-child(2) { animation-delay: .2s; } .dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.chat-form { display: flex; gap: 8px; padding: 10px 12px 4px; border-top: 1px solid var(--border); align-items: flex-end; }
.chat-form textarea { flex: 1; resize: vertical; min-height: 44px; max-height: 40vh; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); }
.primary { background: var(--accent); color: var(--bg-elev); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 16px; cursor: pointer; font-weight: 600; }
.primary:disabled { opacity: .5; cursor: progress; }
.disclaimer { margin: 0; padding: 2px 12px 10px; font-size: 11.5px; color: var(--fg-muted); }

/* --------------------------------------------------------- hộp thoại */
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--bg-elev); color: var(--fg); max-width: min(560px, 94vw); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { padding: 18px 20px; }
dialog h2 { margin: 0 0 10px; font-size: 18px; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; margin: 0 0 12px; }
legend { font-weight: 600; font-size: 14px; padding: 0 4px; }
.radio { display: flex; gap: 10px; padding: 6px 0; align-items: flex-start; cursor: pointer; }
.radio input { margin-top: 4px; }
.field { display: flex; flex-direction: column; gap: 4px; margin: 0 0 12px; font-size: 14px; }
.field input { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--bg); }
.warn { margin: 4px 0 0; font-size: 12.5px; background: var(--warn-bg); color: var(--warn-fg); padding: 8px; border-radius: 6px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions button:not(.primary) { padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border); background: transparent; cursor: pointer; }
.scrim { display: none; }
.noscript { padding: 20px; }

/* --------------------------------------------------------- màn hình hẹp */
@media (max-width: 1200px) {
  :root { --sidebar-w: 250px; --tutor-w: 360px; }
  .chapter { padding: 24px 24px 80px; }
}
@media (max-width: 960px) {
  .only-narrow { display: inline-grid; }
  .layout, .layout.chat-collapsed { grid-template-columns: minmax(0, 1fr); }
  .layout.chat-collapsed .tutor { display: flex; }
  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; width: min(86vw, 330px); z-index: 60;
    transform: translateX(-102%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  .sidebar.open { transform: none; }
  .tutor {
    position: fixed; left: 0; right: 0; bottom: 0; height: min(82dvh, 720px); z-index: 50;
    border-left: 0; border-top: 1px solid var(--border); border-radius: 16px 16px 0 0;
    transform: translateY(102%); transition: transform .2s ease; box-shadow: 0 -6px 24px rgba(0,0,0,.18);
  }
  .tutor.open { transform: none; }
  .tutor::before { content: ""; display: block; width: 40px; height: 4px; border-radius: 2px; background: var(--border); margin: 8px auto 0; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 45; }
  .scrim[hidden] { display: none; }
    .chapter { font-size: 17px; padding: 18px 16px 90px; }
  .brand-sub { display: none; }
  .topbar { gap: 6px; padding: 0 8px; }
  .search { max-width: none; }
  .chat-toggle span { display: none; }
  .chat-toggle { padding: 7px 9px; }
}
@media (max-width: 520px) {
  .brand { display: none; }
}
@media print {
  .topbar, .sidebar, .tutor, .exercise-actions, .chapter-nav, .heading-anchor { display: none !important; }
  body { overflow: visible; }
  .layout { display: block; height: auto; }
  .reader { overflow: visible; }
}
.box-proof > .box-title strong, .box-proof > p:first-child > strong:first-child { color: var(--fg-muted); font-style: italic; }
