/* Textbook 線上語法書 — 主要 CSS */

:root {
  --textbook-brand: #00A896;
  --textbook-brand-light: #028090;
  --textbook-brand-dark: #05668d;
  --textbook-text: #2c3e50;
  --textbook-muted: #6c757d;
  --textbook-border: #e0e0e0;
  --textbook-bg-soft: #f8f9fa;
  --textbook-code-bg: #f6f8fa;
  --textbook-highlight-bg: #fff2a8;
  --textbook-highlight-border: #ffd45c;
  --textbook-content-max: 760px;
  --textbook-prose-max: 820px;       /* 段落舒適閱讀寬度 */
  --textbook-wide-max: 1040px;       /* 內容欄寬：表格／程式碼可破出到此 */
  --textbook-figure-max: 980px;
  --textbook-sidebar-w: 260px;
}

/* === Layout === */
body, #page-container, #content {
  /* textbook 用更寬的 container 配置 sticky sidebar */
}

.textbook-container {
  max-width: 1360px;
  margin: 24px auto;
  padding: 0 16px;
  color: var(--textbook-text);
  font-family: -apple-system, BlinkMacSystemFont, 'Noto Sans TC', sans-serif;
  font-size: 16px;
  line-height: 1.7;
}

.textbook-section-layout {
  display: grid;
  grid-template-columns: var(--textbook-sidebar-w) minmax(0, 1fr);
  gap: 32px;
  align-items: start;
  /* 2026-08-01：sidebar 版面（book/chapter/section）不吃 1360px 置中框——
     寬螢幕的置中左邊距全給版面用，sidebar 貼近視窗左緣；主內容自身有
     --textbook-wide-max(1040)/--textbook-prose-max(820) 上限，閱讀寬度不受影響。 */
  max-width: none;
}
/* 目錄與置中框左緣的間隙來自 #content 的左右 margin（各 ~4%）——只拔這層；
   #page-container 的 1498px 置中框保留（Anna 2026-08-01 拍板）。
   仿 problem.html 的 body:has(.merged-page) 先例，只影響 textbook sidebar 版面頁。 */
body:has(.textbook-section-layout) #content {
  margin-left: 0;
  margin-right: 0;
  width: auto;
}

/* 目錄收合鈕：桌機隱藏，手機版（≤900px）才顯示 */
.textbook-toc-toggle {
  display: none;
  width: 100%;
  text-align: left;
  padding: 10px 14px;
  margin: 0 0 4px;
  background: var(--textbook-brand);
  color: #fff;
  border: 0;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.textbook-toc-toggle:hover { background: var(--textbook-brand-light); }

@media (max-width: 900px) {
  .textbook-section-layout {
    grid-template-columns: 1fr;
  }
  .textbook-toc-sidebar {
    position: static !important;
    max-height: none !important;
  }
  /* 手機版：目錄預設收合，點「展開目錄」才展開 → 內容優先出現 */
  .textbook-toc-toggle {
    display: block;
  }
  .textbook-toc-sidebar:not(.toc-open) .textbook-toc {
    display: none;
  }
}

/* === Breadcrumb === */
.textbook-breadcrumb {
  font-size: 13px;
  color: var(--textbook-muted);
  margin-bottom: 16px;
}
.textbook-breadcrumb a {
  color: var(--textbook-brand);
  text-decoration: none;
}
.textbook-breadcrumb a:hover {
  text-decoration: underline;
}

/* === List page === */
.textbook-header {
  text-align: center;
  margin-bottom: 32px;
  padding-bottom: 16px;
  border-bottom: 2px solid var(--textbook-brand);
}
.textbook-header h1 {
  margin: 0;
  font-size: 32px;
  color: var(--textbook-brand);
}
.textbook-subtitle {
  color: var(--textbook-muted);
  margin: 8px 0 0;
}
.textbook-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}
.textbook-card {
  display: block;
  background: white;
  border: 1px solid var(--textbook-border);
  border-radius: 10px;
  padding: 20px;
  text-decoration: none;
  color: inherit;
  transition: transform 0.2s, box-shadow 0.2s;
}
.textbook-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 16px rgba(0, 168, 150, 0.15);
}
.textbook-cover {
  width: 100%;
  height: auto;
  border-radius: 6px;
  margin-bottom: 12px;
}
.textbook-card-title {
  margin: 8px 0 4px;
  font-size: 20px;
  color: var(--textbook-text);
}
.textbook-card-subtitle {
  margin: 0;
  color: var(--textbook-muted);
  font-size: 14px;
}
.textbook-level {
  display: inline-block;
  background: var(--textbook-brand);
  color: white;
  padding: 2px 10px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  margin-bottom: 8px;
}
.textbook-draft-badge,
.textbook-draft-banner {
  display: inline-block;
  background: #fff3cd;
  color: #856404;
  border: 1px solid #ffeeba;
  padding: 4px 10px;
  border-radius: 4px;
  font-size: 12px;
  margin-top: 8px;
}
.textbook-draft-banner {
  display: block;
  margin: 12px 0;
  padding: 8px 14px;
}
.textbook-empty {
  text-align: center;
  color: var(--textbook-muted);
  padding: 48px 0;
}

/* === Book page === */
.textbook-book-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--textbook-border);
}
.textbook-book-header h1 {
  margin: 8px 0 4px;
  font-size: 32px;
  line-height: 1.3;
  color: var(--textbook-brand-dark);
}
.textbook-book-intro {
  background: var(--textbook-bg-soft);
  padding: 16px 20px;
  border-radius: 8px;
  margin: 16px 0 24px;
}
/* === Book 封面 / 起始頁 === */
.textbook-book-cover-img {
  width: 100%;
  max-width: 320px;
  height: auto;
  border-radius: 10px;
  margin-bottom: 16px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.1);
}
/* 「開始閱讀」CTA */
.textbook-start-cta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 24px 0;
  padding: 18px 24px;
  background: var(--textbook-brand);
  color: white;
  border-radius: 10px;
  text-decoration: none;
  box-shadow: 0 4px 14px rgba(0, 168, 150, 0.25);
  transition: transform 0.15s, box-shadow 0.15s, background 0.15s;
}
.textbook-start-cta:hover {
  background: var(--textbook-brand-light);
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(0, 168, 150, 0.32);
  text-decoration: none;
}
.textbook-start-cta-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.textbook-start-cta-label {
  font-size: 18px;
  font-weight: 700;
}
.textbook-start-cta-sub {
  font-size: 13px;
  opacity: 0.92;
}
.textbook-start-cta-arrow {
  font-size: 26px;
  font-weight: 700;
  flex-shrink: 0;
}
/* 統計 + 我的筆記 */
.textbook-book-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 8px;
  color: var(--textbook-muted);
  font-size: 14px;
}
.textbook-toc-title {
  margin-top: 32px;
  color: var(--textbook-brand-dark);
}

/* === TOC (book + sidebar 共用) === */
.textbook-toc {
  list-style: none;
  padding-left: 0;
  margin: 0;
}
.textbook-toc-chapter {
  margin: 12px 0;
  position: relative;
}
.textbook-toc-chapter > a {
  display: block;
  font-weight: 600;
  color: var(--textbook-brand-dark);
  text-decoration: none;
  padding: 4px 26px 4px 0; /* 右側讓位給 chevron */
}
/* 單元手風琴 chevron（2026-08-01）：預設只展開當前單元 */
.textbook-toc-ch-toggle {
  position: absolute;
  top: 2px;
  right: 0;
  width: 24px;
  height: 24px;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  color: var(--textbook-muted);
  border-radius: 4px;
}
.textbook-toc-ch-toggle:hover {
  color: var(--textbook-brand);
  background: var(--textbook-bg-soft);
}
.textbook-toc-ch-toggle .fa {
  transition: transform 0.15s ease;
}
.textbook-toc-chapter.is-collapsed > .textbook-toc-ch-toggle .fa {
  transform: rotate(-90deg);
}
.textbook-toc-chapter.is-collapsed > .textbook-toc-sections {
  display: none;
}
.textbook-toc-chapter > a:hover {
  color: var(--textbook-brand);
}
.textbook-toc-sections {
  list-style: none;
  padding-left: 8px; /* 2026-08-01 16→8：縮排收緊、內容加寬 */
  margin: 4px 0 0;
  border-left: 2px solid var(--textbook-border);
}
.textbook-toc-sections li {
  margin: 4px 0;
}
.textbook-toc-sections a {
  display: block;
  padding: 4px 6px;
  color: var(--textbook-text);
  text-decoration: none;
  font-size: 14px;
  border-radius: 4px;
}
.textbook-toc-sections a:hover {
  background: var(--textbook-bg-soft);
  color: var(--textbook-brand);
}
.textbook-toc-num {
  color: var(--textbook-muted);
  font-size: 0.9em;
  margin-right: 4px;
}

/* === Sidebar specific === */
.textbook-toc-sidebar {
  position: sticky;
  top: calc(var(--navbar-h) + 16px);
  max-height: calc(100vh - var(--navbar-h) - 32px);
  overflow-y: auto;
  background: white;
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  padding: 16px 8px; /* 2026-08-01 左右 16→8：目錄內容加寬 */
  font-size: 14px;
}
.textbook-toc-book-link {
  display: block;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--textbook-border);
  color: var(--textbook-brand);
  text-decoration: none;
  font-weight: 600;
}
/* 側欄搜尋（全頁面共用，Enter 送出） */
.textbook-toc-search {
  margin-bottom: 14px;
}
.textbook-toc-search input {
  width: 100%;
  box-sizing: border-box;
  padding: 7px 12px;
  border: 1px solid var(--textbook-border);
  border-radius: 6px;
  font-size: 13px;
  background: white;
}
.textbook-toc-search input:focus {
  outline: none;
  border-color: var(--textbook-brand);
  box-shadow: 0 0 0 3px rgba(0, 168, 150, 0.18);
}
.textbook-toc-current-chapter > a {
  background: var(--textbook-bg-soft);
  border-left: 3px solid var(--textbook-brand);
  padding-left: 6px;
}
.textbook-toc-current-section > a {
  background: rgba(0, 168, 150, 0.1);
  color: var(--textbook-brand-dark) !important;
  font-weight: 600;
}

/* === Chapter page === */
.textbook-chapter-header {
  margin-bottom: 24px;
}
.textbook-chapter-header h1 {
  margin: 4px 0;
  font-size: 28px;
  line-height: 1.3;
  color: var(--textbook-brand-dark);
}
.textbook-chapter-num {
  display: inline-block;
  font-size: 14px;
  color: var(--textbook-muted);
}
.textbook-chapter-intro {
  background: var(--textbook-bg-soft);
  padding: 16px 20px;
  border-radius: 8px;
  margin: 16px 0;
}
.textbook-chapter-sections {
  margin-top: 16px;
  list-style: none;
  padding-left: 0;
}
.textbook-chapter-sections li {
  margin: 8px 0;
}
.textbook-chapter-sections a {
  color: var(--textbook-brand-dark);
  text-decoration: none;
  font-size: 16px;
}
.textbook-chapter-sections a:hover {
  text-decoration: underline;
}

/* === Section page === */
.textbook-section-main {
  min-width: 0;  /* 防 overflow 撐爆 grid */
}
.textbook-section-content {
  max-width: var(--textbook-wide-max);
  margin: 0 auto;
}
/* 段落／標題／清單／callout 維持舒適閱讀寬度（prose-max）；
   表格與程式碼不在此列 → 可破出到內容欄全寬（wide-max） */
.textbook-section-header,
.textbook-actions,
.textbook-section-nav,
.textbook-rendered > p,
.textbook-rendered > ul,
.textbook-rendered > ol,
.textbook-rendered > h2,
.textbook-rendered > h3,
.textbook-rendered > h4,
.textbook-rendered > h5,
.textbook-rendered > h6,
.textbook-rendered > blockquote,
.textbook-rendered > .adm {
  max-width: var(--textbook-prose-max);
}
.textbook-section-header h1 {
  margin: 0 0 8px;
  color: var(--textbook-brand-dark);
  font-size: 28px;
}
.textbook-section-num {
  color: var(--textbook-muted);
  margin-right: 4px;
}
.textbook-rendered {
  font-size: 16px;
  line-height: 1.8;
}
/* code / pre 關閉連字，避免 != >= -> 等被字型合成成 ≠ ≥ → 等符號。
   原本只手改在 prod static、未進 git（drift）；此處補回 app 源碼成單一真相。 */
.textbook-rendered code,
.textbook-rendered pre,
.textbook-rendered .codehilite,
.textbook-problem-code {
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0, "calt" 0;
}
.textbook-rendered h2,
.textbook-rendered h3,
.textbook-rendered h4 {
  margin-top: 28px;
  color: var(--textbook-brand-dark);
}
.textbook-rendered h2 { font-size: 24px; }
.textbook-rendered h3 { font-size: 20px; }
.textbook-rendered h4 { font-size: 17px; }
/* 固定 header (header.aaoj-shell, 高 57px) 會蓋住 anchor 跳轉的目標標題，
   加 scroll-margin-top 把跳轉落點往下推到 header 之下（57px + 呼吸空間）。 */
.textbook-rendered :where(h1, h2, h3, h4, h5, h6) {
  scroll-margin-top: 72px;
}
.textbook-rendered .anchor {
  color: var(--textbook-border);
  text-decoration: none;
  margin-right: 6px;
}
.textbook-rendered .anchor:hover {
  color: var(--textbook-brand);
}
.textbook-rendered p { margin: 12px 0; }
.textbook-rendered code {
  background: var(--textbook-code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Fira Code', Consolas, Monaco, monospace;
  font-size: 0.92em;
  color: #c7254e;
}
.textbook-rendered pre {
  background: var(--textbook-code-bg);
  padding: 12px 16px;
  border-radius: 8px;
  overflow-x: auto;
  border: 1px solid var(--textbook-border);
}
.textbook-rendered pre code {
  background: transparent;
  padding: 0;
  color: inherit;
  font-size: 14px;
}
.textbook-rendered .codehilite {
  background: var(--textbook-code-bg);
  border-radius: 8px;
  margin: 12px 0;
  border: 1px solid var(--textbook-border);
  position: relative;            /* 複製按鈕定位錨點 */
}
.textbook-rendered .codehilite pre {
  background: transparent;
  border: 0;
  margin: 0;
}
/* === 程式碼區塊：行號 + 複製按鈕（textbook-code.js 注入）===
   行號與程式碼放進同一 <table> 的同一列（同列共享高度）→ 任何瀏覽器 / 字型 /
   空行 / 含中文的行都不會錯位。JS 把 .codehilite 的 <pre> 換成 .tb-code-scroll>table。 */
.textbook-rendered .codehilite pre,
.textbook-rendered .codehilite pre code {
  font-size: 14px;             /* JS 未執行時的 fallback 樣式 */
  line-height: 24px;
}
.textbook-rendered .tb-code-scroll {
  overflow-x: auto;
  padding: 10px 0;
}
.textbook-rendered .tb-code-table {
  width: auto;                 /* 蓋掉 .textbook-rendered table{width:100%} */
  margin: 0;
  border-collapse: collapse;
  background: transparent;
  font-family: 'Fira Code', Consolas, Monaco, monospace;
  font-size: 14px;
  line-height: 24px;
}
.textbook-rendered .tb-code-table td {
  border: 0;                   /* 蓋掉 .textbook-rendered td{border;padding} */
  padding: 0;
  background: transparent;
  vertical-align: top;
  text-align: left;
}
.textbook-rendered .tb-code-table .tb-no {
  position: sticky;            /* 橫向捲動時行號欄固定 */
  left: 0;
  text-align: right;
  padding: 0 12px 0 16px;
  color: #aab2bd;
  background: var(--textbook-code-bg);
  border-right: 1px solid var(--textbook-border);
  white-space: pre;
  user-select: none;
  -webkit-user-select: none;
}
.textbook-rendered .tb-code-table .tb-lc {
  padding: 0 16px;
  white-space: pre;
}
/* 行內容包在 <code> 內（讓 pygments `code .kt` 上色規則生效）；
   但要 reset 掉 .textbook-rendered code 的行內程式碼粉底/padding 樣式 */
.textbook-rendered .tb-code-table .tb-lc code {
  background: transparent;
  padding: 0;
  border-radius: 0;
  color: inherit;
  font: inherit;
}
.textbook-rendered .tb-code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--textbook-muted);
  background: white;
  border: 1px solid var(--textbook-border);
  border-radius: 6px;
  cursor: pointer;
  opacity: 0.85;
  transition: opacity 0.15s, color 0.15s, border-color 0.15s, background 0.15s;
}
.textbook-rendered .tb-code-copy:hover {
  opacity: 1;
  color: var(--textbook-brand);
  border-color: var(--textbook-brand);
}
.textbook-rendered .tb-code-copy.copied {
  color: white;
  background: var(--textbook-brand);
  border-color: var(--textbook-brand);
  opacity: 1;
}
.textbook-rendered table {
  border-collapse: collapse;
  width: 100%;
  margin: 16px 0;
}
.textbook-rendered th,
.textbook-rendered td {
  border: 1px solid var(--textbook-border);
  padding: 8px 12px;
  text-align: left;
  color: var(--textbook-text);   /* 蓋掉 OJ 全域 th { color: white } */
}
.textbook-rendered th {
  background: var(--textbook-brand);
  color: white;                   /* 配深底用白字（OJ 預設值正好對） */
  font-weight: 600;
  border-color: var(--textbook-brand);
}
/* textbook 的 table 被 markdown_render 包進 .md-table-scroll，會吃到全域
   `.md-table-scroll > table thead th { background:#f6f8fa }`（specificity 0,1,3），
   蓋掉上面 `.textbook-rendered th` 的 teal 底卻留下白字 → 白字配淺灰看不見。
   用更高 specificity（0,2,4）把 teal 底白字搶回來，cell 邊框也對齊 textbook 風格。 */
.textbook-rendered .md-table-scroll > table thead th {
  background: var(--textbook-brand);
  color: #fff;
  border-color: var(--textbook-brand);
}
.textbook-rendered .md-table-scroll > table th,
.textbook-rendered .md-table-scroll > table td {
  border-color: var(--textbook-border);
}
.textbook-rendered blockquote {
  border-left: 4px solid var(--textbook-brand);
  background: var(--textbook-bg-soft);
  padding: 8px 16px;
  margin: 16px 0;
  color: var(--textbook-text);
}
.textbook-rendered ul,
.textbook-rendered ol {
  padding-left: 28px;
}
.textbook-rendered li {
  margin: 6px 0;
}
.textbook-rendered a {
  color: var(--textbook-brand);
  text-decoration: none;
}
.textbook-rendered a:hover {
  text-decoration: underline;
}
/* 表頭連結：th 背景與連結色同為品牌 teal，teal 字配 teal 底＝隱形
   （2026-08-01 s10-9「跟 C 字串比一比」表頭事故）→ 表頭內連結改白字加底線。 */
.textbook-rendered th a,
.textbook-rendered .md-table-scroll > table thead th a {
  color: #fff;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* === Task list checkbox (GFM `- [ ]` / `- [x]`，preprocess 階段轉 span) === */
/* 含 checkbox 方框的清單項目去掉預設項目符號（disc 黑點），只留方框。
   混在一般清單裡的非 task 項目不受影響。:has() 為現代瀏覽器 baseline。 */
.textbook-rendered li:has(> .textbook-task-box) {
  list-style: none;
}
.textbook-rendered .textbook-task-box {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 1.5px solid var(--textbook-muted);
  border-radius: 3px;
  vertical-align: -2px;
  margin-right: 6px;
  position: relative;
  flex: none;
}
.textbook-rendered .textbook-task-box.is-checked {
  background: var(--textbook-brand);
  border-color: var(--textbook-brand);
}
.textbook-rendered .textbook-task-box.is-checked::after {
  content: '';
  position: absolute;
  top: 1px;
  left: 4px;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

/* === Admonition (4 種：info / success / warning / danger) === */
.textbook-rendered .adm {
  border-radius: 8px;
  margin: 18px 0;
  padding: 14px 18px;
  border-left: 4px solid;
}
.textbook-rendered .adm-title {
  font-weight: 600;
  margin-bottom: 8px;
}
.textbook-rendered .adm-body > p:first-child {
  margin-top: 0;
}
.textbook-rendered .adm-body > p:last-child {
  margin-bottom: 0;
}
.textbook-rendered .adm-info {
  background: #e3f2fd;
  border-color: #2196f3;
}
.textbook-rendered .adm-info .adm-title { color: #1565c0; }
.textbook-rendered .adm-success {
  background: #e8f5e9;
  border-color: #4caf50;
}
.textbook-rendered .adm-success .adm-title { color: #2e7d32; }
.textbook-rendered .adm-warning {
  background: #fff8e1;
  border-color: #ff9800;
}
.textbook-rendered .adm-warning .adm-title { color: #ef6c00; }
.textbook-rendered .adm-danger {
  background: #ffebee;
  border-color: #f44336;
}
.textbook-rendered .adm-danger .adm-title { color: #c62828; }

/* === Figure (圖解) === */
.textbook-rendered .textbook-figure {
  margin: 20px 0;
  text-align: center;
}
.textbook-rendered .textbook-figure-frame {
  aspect-ratio: 840 / 640;
  width: 100%;
  max-width: var(--textbook-figure-max);
  margin: 0 auto;
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  overflow: hidden;
  background: white;
  position: relative;
}
.textbook-rendered .textbook-figure-frame iframe {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
}
.textbook-rendered .textbook-figure-open {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 6px;
  background: rgba(5, 102, 141, 0.94);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.22);
}
.textbook-rendered .textbook-figure-open:hover,
.textbook-rendered .textbook-figure-open:focus {
  background: var(--textbook-brand-dark);
  color: #fff;
  text-decoration: none;
}
.textbook-rendered .textbook-figure figcaption {
  margin-top: 8px;
  color: var(--textbook-muted);
  font-size: 14px;
  font-style: italic;
}

body.tb-figure-open {
  overflow: hidden;
}
.textbook-figure-lightbox {
  position: fixed;
  inset: 0;
  z-index: 980;
  display: flex;
  padding: 24px;
  background: rgba(17, 24, 39, 0.94);
}
.textbook-figure-lightbox[hidden] {
  display: none;
}
.textbook-figure-lightbox-dialog {
  width: min(1180px, calc(100vw - 48px));
  height: min(900px, calc(100vh - 48px));
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.textbook-figure-lightbox-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #fff;
}
.textbook-figure-lightbox-title {
  font-size: 15px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.textbook-figure-lightbox-close {
  flex: none;
  border: 1px solid rgba(255, 255, 255, 0.6) !important;
  border-radius: 6px !important;
  padding: 6px 10px !important;
  background: rgba(255, 255, 255, 0.12) !important;
  color: #fff !important;
  font-size: 15px !important;
  line-height: 1 !important;
  cursor: pointer;
  box-shadow: none !important;
  text-shadow: none !important;
}
.textbook-figure-lightbox-close:hover,
.textbook-figure-lightbox-close:focus {
  background: rgba(255, 255, 255, 0.22) !important;
}
.textbook-figure-lightbox-frame {
  flex: 1;
  min-height: 0;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}
.textbook-figure-lightbox-frame iframe {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
}

@media (max-width: 700px) {
  .textbook-figure-lightbox {
    padding: 12px;
  }
  .textbook-figure-lightbox-dialog {
    width: calc(100vw - 24px);
    height: calc(100vh - 24px);
  }
  .textbook-rendered .textbook-figure-open {
    top: 8px;
    right: 8px;
    padding: 6px 8px;
  }
}

/* === Math (KaTeX) === */
.textbook-rendered .math-display {
  text-align: center;
  margin: 16px 0;
  overflow-x: auto;
}

/* === Section nav (prev/next) === */
.textbook-section-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--textbook-border);
  gap: 16px;
}
.textbook-nav-prev,
.textbook-nav-next {
  flex: 1;
  padding: 14px 18px;
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  text-decoration: none;
  color: var(--textbook-text);
  background: var(--textbook-bg-soft);
  transition: background 0.2s, border-color 0.2s, box-shadow 0.2s, transform 0.15s;
}
.textbook-nav-prev:hover,
.textbook-nav-next:hover {
  background: white;
  border-color: var(--textbook-brand);
  color: var(--textbook-brand-dark);
  box-shadow: 0 4px 12px rgba(0, 168, 150, 0.12);
  transform: translateY(-1px);
}
.textbook-nav-next { text-align: right; }
.textbook-nav-disabled {
  background: transparent;
  border-style: dashed;
  color: var(--textbook-muted);
  cursor: not-allowed;
  font-style: italic;
}
.textbook-nav-disabled:hover {
  /* disabled 不該有 hover 動效 */
  background: transparent;
  border-color: var(--textbook-border);
  color: var(--textbook-muted);
  box-shadow: none;
  transform: none;
}

/* === 第二階段：mark-read + note 互動 === */

.textbook-actions {
  display: flex;
  gap: 12px;
  margin: 28px 0 16px;
  padding: 14px 18px;
  background: var(--textbook-bg-soft);
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  align-items: center;
  flex-wrap: wrap;
}
/* NOTE: OJ style.css 對 <button> 強制 color: white !important + 其他樣式
   （CLAUDE.md memory: DMOJ button CSS override）。textbook 的 button 必須
   全部用 !important counter，否則文字會白色看不見。 */
.textbook-mark-read-btn,
.textbook-note-btn {
  display: inline-flex !important;
  align-items: center;
  gap: 8px;
  padding: 8px 14px !important;
  border: 1px solid var(--textbook-border) !important;
  border-radius: 6px !important;
  background: white !important;
  color: var(--textbook-text) !important;
  font-size: 14px !important;
  font-weight: normal !important;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  text-shadow: none !important;
  box-shadow: none !important;
}
.textbook-mark-read-btn:hover,
.textbook-note-btn:hover {
  background: var(--textbook-bg-soft) !important;
  border-color: var(--textbook-brand) !important;
  color: var(--textbook-brand-dark) !important;
}
.textbook-mark-read-btn.is-read {
  background: var(--textbook-brand) !important;
  color: white !important;
  border-color: var(--textbook-brand) !important;
}
.textbook-mark-read-btn.is-read:hover {
  background: var(--textbook-brand-light) !important;
}
.textbook-mark-read-icon {
  font-weight: bold;
  font-size: 16px;
  width: 16px;
  text-align: center;
}
.textbook-note-btn.has-note {
  background: rgba(0, 168, 150, 0.12) !important;
  border-color: var(--textbook-brand) !important;
  color: var(--textbook-brand-dark) !important;
  font-weight: 600 !important;
}

/* === 筆記：FAB + 停靠面板 + 選字引用（2026-07-04 改版）=== */
:root { --tb-note-w: 480px; }

/* 版面位移：面板開啟時把課文往左推，讓兩者不重疊、課文仍可捲動閱讀 */
body.tb-note-open .textbook-container {
  margin-right: calc(var(--tb-note-w) + 24px);
  transition: margin-right 0.2s ease;
}

/* ---- 浮動按鈕 FAB（讀到哪都一鍵開，快捷鍵 n）---- */
.textbook-note-fab {
  position: fixed;
  right: 24px;
  bottom: 28px;
  z-index: 450;
  display: inline-flex !important;
  align-items: center;
  gap: 8px;
  padding: 12px 20px !important;
  border: 0 !important;
  border-radius: 999px !important;
  background: var(--textbook-brand) !important;
  color: #fff !important;
  font-size: 15px !important;
  font-weight: 600 !important;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.22) !important;
  text-shadow: none !important;
  transition: right 0.2s ease, background 0.15s, transform 0.1s, box-shadow 0.15s;
}
.textbook-note-fab:hover {
  background: var(--textbook-brand-light) !important;
  transform: translateY(-1px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.28) !important;
}
.textbook-note-fab-icon { font-size: 18px; line-height: 1; }
/* 有筆記：右上角小圓點 */
.textbook-note-fab.has-note::after {
  content: '';
  position: absolute;
  top: 6px;
  right: 8px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ffd54a;
  border: 2px solid var(--textbook-brand);
}
.textbook-note-fab.is-open { background: var(--textbook-brand-dark) !important; }
body.tb-note-open .textbook-note-fab { right: calc(var(--tb-note-w) + 24px); }

/* ---- 選字動作列（選課文文字後冒出：加入筆記 + 問老師）---- */
.textbook-sel-actions {
  position: fixed;
  z-index: 460;
  display: flex;
  gap: 6px;
}
.textbook-sel-actions[hidden] { display: none !important; }
.textbook-quote-btn,
.textbook-ask-btn {
  padding: 6px 12px !important;
  border: 0 !important;
  border-radius: 6px !important;
  color: #fff !important;
  font-size: 13px !important;
  font-weight: 600 !important;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.25) !important;
  text-shadow: none !important;
  white-space: nowrap;
}
.textbook-quote-btn { background: var(--textbook-brand-dark) !important; }
.textbook-quote-btn:hover { background: var(--textbook-brand) !important; }
.textbook-ask-btn { background: #7c3aed !important; }   /* 紫：與筆記 teal 系區隔，一眼分得出兩種動作 */
.textbook-ask-btn:hover { background: #8b5cf6 !important; }

/* ---- 停靠面板 ---- */
.textbook-note-dock,
.textbook-note-dock *,
.textbook-note-dock *::before,
.textbook-note-dock *::after {
  /* OJ 預設 box-sizing: content-box；padding 會超出 panel 寬度，強制 border-box。 */
  box-sizing: border-box;
}
.textbook-note-dock {
  position: fixed;
  top: 57px;        /* fallback；實際由 JS syncDockTop() 量 header.aaoj-shell 底緣（會隨 banner 變高） */
  right: 0;
  bottom: 0;
  width: var(--tb-note-w);
  max-width: 100vw;
  z-index: 440;
}
.textbook-note-dock[hidden] { display: none; }
.textbook-note-panel {
  position: relative;
  width: 100%;
  height: 100%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--textbook-border);
  box-shadow: -8px 0 30px rgba(0, 0, 0, 0.12);
  animation: textbook-slide-in 0.2s ease-out;
}
@keyframes textbook-slide-in {
  from { transform: translateX(24px); opacity: 0.4; }
  to { transform: translateX(0); opacity: 1; }
}
.textbook-note-panel.is-resizing { user-select: none; }
.textbook-note-resize-handle {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 8px;
  cursor: col-resize;
  background: transparent;
  z-index: 3;
}
.textbook-note-resize-handle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 2px;
  width: 4px;
  height: 44px;
  margin-top: -22px;
  background: var(--textbook-border);
  border-radius: 2px;
  opacity: 0.55;
  transition: opacity 0.15s, background 0.15s;
}
.textbook-note-resize-handle:hover::before,
.textbook-note-panel.is-resizing .textbook-note-resize-handle::before {
  opacity: 1;
  background: var(--textbook-brand);
}
.textbook-note-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--textbook-border);
}
.textbook-note-header h2 {
  margin: 0;
  font-size: 15px;
  color: var(--textbook-brand-dark);
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.textbook-note-close {
  background: transparent !important;
  border: 0 !important;
  font-size: 18px !important;
  color: var(--textbook-muted) !important;
  cursor: pointer;
  padding: 2px 8px !important;
  border-radius: 4px !important;
  box-shadow: none !important;
  flex: none;
}
.textbook-note-close:hover {
  background: var(--textbook-bg-soft) !important;
  color: var(--textbook-text) !important;
}
/* 「編輯」鈕：只在閱讀模式（開啟時有內容→先顯示預覽）露出，點下切到編輯 */
.textbook-note-edit-btn {
  display: none;
  align-items: center;
  gap: 5px;
  padding: 4px 12px !important;
  border: 1px solid var(--textbook-brand) !important;
  border-radius: 6px !important;
  background: var(--textbook-brand) !important;
  color: #fff !important;
  font-size: 13px !important;
  font-weight: 600 !important;
  cursor: pointer;
  box-shadow: none !important;
  text-shadow: none !important;
  flex: none;
}
.textbook-note-edit-btn:hover { background: var(--textbook-brand-light) !important; }
.textbook-note-dock.is-reading .textbook-note-edit-btn { display: inline-flex; }
/* 閱讀模式：隱藏編輯工具列與 footer（自動儲存提示/儲存鈕是編輯的事）→ 乾淨的閱讀面 */
.textbook-note-editor-wrap .md-editor.is-reading .md-editor-toolbar { display: none; }
.textbook-note-dock.is-reading .textbook-note-footer { display: none; }
.textbook-note-editor-wrap .md-editor.is-reading .md-preview { padding: 16px 20px; }
/* 讓共用 md-editor 撐滿面板、內部可捲動 */
.textbook-note-editor-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
}
.textbook-note-editor-wrap .md-editor {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: 0;
}
.textbook-note-editor-wrap .md-body {
  flex: 1;
  min-height: 0;
}
/* 停靠面板窄（~480px）→ split 改「上下」分割（編輯上、預覽下），
   兩區各拿滿面板寬度，比左右各半好用。只 scope 在此，不動 notes/profile 頁。 */
.textbook-note-editor-wrap .md-body[data-mode="split"] {
  flex-direction: column;
}
.textbook-note-editor-wrap .md-body[data-mode="split"] .md-preview {
  border-left: 0;
  border-top: 1px solid var(--textbook-border);
}
/* 覆蓋 profile.css 的 min-height:320 / resize，讓編輯區與預覽區隨面板高度均分、內部各自捲動 */
.textbook-note-editor-wrap .md-body textarea,
.textbook-note-editor-wrap .md-body .md-preview {
  flex: 1 1 0;
  min-height: 0;
  height: auto;
  overflow-y: auto;
  resize: none;
}
.textbook-note-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 16px;
  border-top: 1px solid var(--textbook-border);
  background: var(--textbook-bg-soft);
}
.textbook-note-hint { font-size: 12px; color: var(--textbook-muted); }
.textbook-note-hint kbd {
  font-family: inherit;
  background: #fff;
  border: 1px solid var(--textbook-border);
  border-radius: 3px;
  padding: 0 4px;
  font-size: 11px;
}
.textbook-note-status { font-size: 12px; font-weight: 600; white-space: nowrap; }
.textbook-note-status.is-saved { color: var(--textbook-brand-dark); }
.textbook-note-status.is-dirty { color: #c77700; }
.textbook-note-status.is-saving { color: var(--textbook-muted); }
.textbook-note-status.is-error { color: #c0392b; }

/* TOC 側欄「有筆記」📝 標記 */
.textbook-toc-note-dot { display: none; margin-left: 4px; font-size: 11px; }
.textbook-toc-sections li.has-note > a .textbook-toc-note-dot,
.textbook-toc-current-section.has-note > a .textbook-toc-note-dot { display: inline; }

/* 手機版：底部 bottom-sheet 全寬，不位移課文 */
@media (max-width: 900px) {
  body.tb-note-open .textbook-container { margin-right: 0; }
  .textbook-note-dock {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100% !important;
    height: 82vh;
  }
  .textbook-note-panel {
    border-left: 0;
    border-top: 1px solid var(--textbook-border);
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.14);
  }
  .textbook-note-resize-handle { display: none; }
  body.tb-note-open .textbook-note-fab { display: none; }
  .textbook-note-fab { right: 16px; bottom: 16px; }
}

/* === sidebar 已讀 section icon === */
.textbook-toc-current-section.is-read > a::before,
.textbook-toc-sections li.is-read > a::before {
  content: '✓ ';
  color: var(--textbook-brand);
  font-weight: bold;
  margin-right: 2px;
}

/* === 我的筆記入口連結 === */
.textbook-header-actions {
  margin: 8px 0 0;
}
.textbook-mynotes-link {
  display: inline-block;
  padding: 6px 14px;
  background: var(--textbook-bg-soft);
  border: 1px solid var(--textbook-border);
  border-radius: 6px;
  text-decoration: none;
  color: var(--textbook-brand-dark);
  font-size: 14px;
  transition: background 0.15s, border-color 0.15s;
}
.textbook-mynotes-link:hover {
  background: white;
  border-color: var(--textbook-brand);
  color: var(--textbook-brand-dark);
}

/* === 我的筆記總覽 === */
.textbook-mynotes-header {
  margin-bottom: 24px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--textbook-border);
}
.textbook-mynotes-header h1 {
  font-size: 26px;
  color: var(--textbook-brand-dark);
  margin: 4px 0 6px;
}
.textbook-mynotes-meta {
  color: var(--textbook-muted);
  font-size: 14px;
  margin: 0;
}
.textbook-mynotes-meta a {
  color: var(--textbook-brand);
  text-decoration: none;
}
.textbook-mynotes-empty {
  text-align: center;
  color: var(--textbook-muted);
  padding: 60px 0;
}
.textbook-mynotes-list {
  list-style: none;
  padding: 0;
  margin: 0;
}
.textbook-mynotes-item {
  margin: 16px 0;
  padding: 18px 22px;
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  background: white;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.textbook-mynotes-item:hover {
  border-color: var(--textbook-brand);
  box-shadow: 0 4px 12px rgba(0, 168, 150, 0.08);
}
.textbook-mynotes-item-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 10px;
  margin-bottom: 12px;
  border-bottom: 1px dashed var(--textbook-border);
}
.textbook-mynotes-item-link {
  text-decoration: none;
  color: var(--textbook-text);
  font-size: 14px;
  flex: 1;
  min-width: 0;
}
.textbook-mynotes-item-link:hover {
  color: var(--textbook-brand-dark);
}
.textbook-mynotes-book {
  color: var(--textbook-muted);
}
.textbook-mynotes-chapter {
  color: var(--textbook-muted);
}
.textbook-mynotes-section {
  font-weight: 600;
  color: var(--textbook-brand-dark);
}
.textbook-mynotes-sep {
  color: var(--textbook-border);
  margin: 0 4px;
}
.textbook-mynotes-time {
  font-size: 12px;
  color: var(--textbook-muted);
  white-space: nowrap;
}
.textbook-mynotes-content {
  font-size: 15px;
  line-height: 1.7;
}
.textbook-mynotes-content > *:first-child { margin-top: 0; }
.textbook-mynotes-content > *:last-child { margin-bottom: 0; }

/* === 搜尋頁 === */
.textbook-search-header {
  margin-bottom: 24px;
}
.textbook-search-header h1 {
  font-size: 26px;
  color: var(--textbook-brand-dark);
  margin: 4px 0 14px;
}
.textbook-search-form {
  display: flex;
  gap: 8px;
}
.textbook-search-input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--textbook-border);
  border-radius: 6px;
  font-size: 16px;
}
.textbook-search-input:focus {
  outline: none;
  border-color: var(--textbook-brand);
  box-shadow: 0 0 0 3px rgba(0, 168, 150, 0.18);
}
.textbook-search-form button {
  padding: 10px 18px !important;
  background: var(--textbook-brand) !important;
  color: white !important;
  border: 0 !important;
  border-radius: 6px !important;
  font-size: 14px !important;
  cursor: pointer;
  box-shadow: none !important;
}
.textbook-search-form button:hover {
  background: var(--textbook-brand-light) !important;
}
.textbook-search-meta {
  color: var(--textbook-muted);
  font-size: 14px;
}
.textbook-search-empty {
  color: var(--textbook-muted);
  text-align: center;
  padding: 40px 0;
}
.textbook-search-results {
  list-style: none;
  padding: 0;
  margin: 16px 0;
}
.textbook-search-result {
  margin: 12px 0;
}
.textbook-search-result a {
  display: block;
  padding: 14px 18px;
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  text-decoration: none;
  color: var(--textbook-text);
  background: white;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.textbook-search-result a:hover {
  border-color: var(--textbook-brand);
  box-shadow: 0 4px 10px rgba(0, 168, 150, 0.1);
}
.textbook-search-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--textbook-brand-dark);
  margin-bottom: 4px;
}
.textbook-search-chapter {
  font-size: 13px;
  color: var(--textbook-muted);
  margin-bottom: 6px;
}
.textbook-search-subtitle {
  font-size: 14px;
  font-weight: 600;
  color: var(--textbook-brand);
  margin-bottom: 4px;
}
.textbook-search-subtitle-mark {
  color: var(--textbook-muted);
  font-weight: 400;
  margin-right: 5px;
}
.textbook-search-snippet {
  font-size: 14px;
  color: var(--textbook-text);
  line-height: 1.6;
}
.textbook-search-highlight {
  background: var(--textbook-highlight-bg);
  color: inherit;
  border-bottom: 1px solid var(--textbook-highlight-border);
  border-radius: 3px;
  padding: 0 2px;
}
.textbook-search-badge {
  display: inline-block;
  font-size: 11px;
  background: var(--textbook-brand);
  color: white;
  padding: 1px 7px;
  border-radius: 8px;
  margin-left: 6px;
  vertical-align: middle;
}
/* 子標題命中：比標題命中弱一階，用外框而非實心 */
.textbook-search-badge-sub {
  background: transparent;
  color: var(--textbook-brand);
  border: 1px solid var(--textbook-brand);
  padding: 0 6px;
}

/* === 相關練習題 === */
.textbook-related-problems {
  margin: 32px 0 16px;
  padding: 18px 22px;
  background: linear-gradient(180deg, rgba(0, 168, 150, 0.08), rgba(0, 168, 150, 0.02));
  border-left: 4px solid var(--textbook-brand);
  border-radius: 8px;
}
.textbook-related-problems h2 {
  margin: 0 0 14px;
  font-size: 18px;
  color: var(--textbook-brand-dark);
}
.textbook-problem-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
}
.textbook-problem-card {
  margin: 0;
}
.textbook-problem-card a {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px;
  background: white;
  border: 1px solid var(--textbook-border);
  border-radius: 6px;
  text-decoration: none;
  color: var(--textbook-text);
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
}
.textbook-problem-card a:hover {
  border-color: var(--textbook-brand);
  box-shadow: 0 4px 10px rgba(0, 168, 150, 0.12);
  transform: translateY(-1px);
}
.textbook-problem-code {
  font-family: 'Fira Code', Consolas, monospace;
  font-size: 12px;
  color: var(--textbook-muted);
}
.textbook-problem-name {
  font-weight: 600;
  color: var(--textbook-brand-dark);
}
.textbook-problem-label {
  display: inline-block;
  font-size: 11px;
  background: var(--textbook-brand);
  color: white;
  padding: 1px 8px;
  border-radius: 10px;
  align-self: flex-start;
}
/* 外部 OJ 題目卡 — 用左側 4px 色條視覺區隔（顏色貼近各 OJ 品牌色）*/
.textbook-problem-card:not(.textbook-problem-aacpoj) a {
  border-left-width: 4px;
  border-left-style: solid;
}
.textbook-problem-codeforces a    { border-left-color: #1f8acb; }
.textbook-problem-atcoder a       { border-left-color: #222; }
.textbook-problem-cses a          { border-left-color: #6a4c93; }
.textbook-problem-ncoj a          { border-left-color: #c0392b; }
.textbook-problem-tioj a          { border-left-color: #e67e22; }
