/* 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-content-max: 760px;
  --textbook-prose-max: 820px;       /* 段落舒適閱讀寬度 */
  --textbook-wide-max: 1040px;       /* 內容欄寬：表格／程式碼可破出到此 */
  --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;
}

/* 目錄收合鈕：桌機隱藏，手機版（≤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;
}
.textbook-toc-chapter > a {
  display: block;
  font-weight: 600;
  color: var(--textbook-brand-dark);
  text-decoration: none;
  padding: 4px 0;
}
.textbook-toc-chapter > a:hover {
  color: var(--textbook-brand);
}
.textbook-toc-sections {
  list-style: none;
  padding-left: 16px;
  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 8px;
  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;
  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;
}

/* === 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-content-max);
  margin: 0 auto;
  border: 1px solid var(--textbook-border);
  border-radius: 8px;
  overflow: hidden;
  background: white;
}
.textbook-rendered .textbook-figure-frame iframe {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
}
.textbook-rendered .textbook-figure figcaption {
  margin-top: 8px;
  color: var(--textbook-muted);
  font-size: 14px;
  font-style: italic;
}

/* === 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;
}

/* === 筆記抽屜 === */
.textbook-note-drawer,
.textbook-note-drawer *,
.textbook-note-drawer *::before,
.textbook-note-drawer *::after {
  /* OJ 預設 box-sizing: content-box，footer/header 加 padding 會超出 panel 寬度。
     強制 border-box 讓 padding 算在 width 內。 */
  box-sizing: border-box;
}
.textbook-note-drawer {
  position: fixed;
  inset: 0;
  z-index: 1000;
}
.textbook-note-drawer[hidden] { display: none; }
.textbook-note-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  animation: textbook-fade-in 0.15s ease-out;
}
@keyframes textbook-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
.textbook-note-panel {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(560px, 92vw);
  min-width: 320px;
  max-width: 100vw;
  background: white;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 30px rgba(0, 0, 0, 0.15);
  animation: textbook-slide-in 0.2s ease-out;
}
.textbook-note-panel.is-resizing {
  /* drag 時關 transition 避免閃 */
  transition: none !important;
  user-select: none;
}
/* 左邊 6px 寬的 resize handle */
.textbook-note-resize-handle {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 6px;
  cursor: col-resize;
  background: transparent;
  z-index: 2;
}
.textbook-note-resize-handle::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 1px;
  width: 4px;
  height: 40px;
  margin-top: -20px;
  background: var(--textbook-border);
  border-radius: 2px;
  opacity: 0.6;
  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);
}
@keyframes textbook-slide-in {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}
.textbook-note-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--textbook-border);
}
.textbook-note-header h2 {
  margin: 0;
  font-size: 16px;
  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: 20px !important;
  color: var(--textbook-muted) !important;
  cursor: pointer;
  padding: 4px 8px !important;
  border-radius: 4px !important;
  box-shadow: none !important;
}
.textbook-note-close:hover {
  background: var(--textbook-bg-soft) !important;
  color: var(--textbook-text) !important;
}
/* note body = textarea + preview 容器，依 data-mode 切換 layout */
.textbook-note-body {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;  /* 讓 children overflow scroll 而非撐爆 */
}
.textbook-note-body[data-mode="edit"] .textbook-note-preview { display: none; }
.textbook-note-body[data-mode="preview"] .textbook-note-textarea { display: none; }
.textbook-note-body[data-mode="split"] .textbook-note-textarea,
.textbook-note-body[data-mode="split"] .textbook-note-preview {
  flex: 1 1 50%;
}
.textbook-note-body[data-mode="split"] .textbook-note-preview {
  border-left: 1px solid var(--textbook-border);
}

.textbook-note-textarea {
  flex: 1;
  padding: 16px 20px;
  border: 0;
  resize: none;
  font-size: 15px;
  font-family: 'Fira Code', Consolas, Monaco, monospace;
  line-height: 1.6;
  outline: none;
  color: var(--textbook-text);
  background: white;
  min-width: 0;  /* 防 flex item 縮不下來 */
}
.textbook-note-preview {
  flex: 1;
  padding: 16px 20px;
  overflow-y: auto;
  font-size: 15px;
  line-height: 1.7;
  background: var(--textbook-bg-soft);
  min-width: 0;
}
.textbook-note-preview-empty {
  color: var(--textbook-muted);
  font-style: italic;
}

/* Edit / Preview tab toggle */
.textbook-note-mode-toggle {
  display: inline-flex;
  border: 1px solid var(--textbook-border);
  border-radius: 6px;
  overflow: hidden;
  margin: 0 8px;
}
.textbook-note-mode-btn {
  background: white !important;
  color: var(--textbook-muted) !important;
  border: 0 !important;
  padding: 6px 12px !important;
  font-size: 13px !important;
  cursor: pointer;
  box-shadow: none !important;
}
.textbook-note-mode-btn + .textbook-note-mode-btn {
  border-left: 1px solid var(--textbook-border) !important;
}
.textbook-note-mode-btn:hover {
  background: var(--textbook-bg-soft) !important;
  color: var(--textbook-text) !important;
}
.textbook-note-mode-btn.is-active {
  background: var(--textbook-brand) !important;
  color: white !important;
}
.textbook-note-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-top: 1px solid var(--textbook-border);
  background: var(--textbook-bg-soft);
}
.textbook-note-status {
  font-size: 13px;
  color: var(--textbook-muted);
}
.textbook-note-status.is-dirty {
  color: var(--textbook-brand-dark);
  font-weight: 600;
}
.textbook-note-save {
  padding: 8px 18px !important;
  background: var(--textbook-brand) !important;
  color: white !important;
  border: 0 !important;
  border-radius: 6px !important;
  font-size: 14px !important;
  font-weight: 600 !important;
  cursor: pointer;
  box-shadow: none !important;
}
.textbook-note-save:hover {
  background: var(--textbook-brand-light) !important;
}

/* === 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-snippet {
  font-size: 14px;
  color: var(--textbook-text);
  line-height: 1.6;
}
.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-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; }
