:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --ink: #1f2430;
  --muted: #6b7280;
  --line: #e5e8ec;
  --accent: #2563eb;
  --accent-soft: #eaf1fe;
  --mark: #ffe58a;
  --topbar-h: 56px;
  --sidebar-w: 330px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--ink); background: var(--bg); line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

/* ---- 頂列 ---- */
.topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 14px; padding: 0 18px;
  background: var(--panel); border-bottom: 1px solid var(--line); z-index: 30;
}
.site-title { font-size: 17px; font-weight: 800; margin: 0; white-space: nowrap; letter-spacing: .5px; }
.menu-toggle { display: block; font-size: 20px; background: none; border: none; cursor: pointer; color: var(--ink); padding: 2px 6px; border-radius: 6px; }
.menu-toggle:hover { background: var(--bg); color: var(--accent); }
/* 桌機收合側欄：隱藏共筆清單，主閱讀區自動填滿 */
.layout.sidebar-collapsed > .sidebar { display: none; }
.search-wrap { flex: 1; display: flex; gap: 8px; max-width: 760px; margin-left: auto; }
#search {
  flex: 1; padding: 10px 16px; border: 1px solid var(--line); border-radius: 22px;
  font-size: 14.5px; outline: none; background: var(--bg); transition: border-color .15s, background .15s;
}
#search:focus { border-color: var(--accent); background: #fff; box-shadow: 0 0 0 3px var(--accent-soft); }
#subjectFilter, #searchScope { border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; background: var(--bg); font-size: 13px; color: var(--ink); }
#searchScope { flex-shrink: 0; }

/* ---- 本共筆頁內搜尋 ---- */
.find-bar {
  position: sticky; top: 0; z-index: 15; display: flex; align-items: center; gap: 8px;
  padding: 8px 16px; background: #fff8e0; border-bottom: 1px solid #f1e6b8;
}
.find-bar[hidden] { display: none; }
.find-label { font-size: 12.5px; color: #8a6d1a; font-weight: 700; }
.find-count { font-size: 13px; color: var(--ink); min-width: 66px; }
.find-nav { border: 1px solid var(--line); background: #fff; border-radius: 6px; cursor: pointer; font-size: 13px; padding: 3px 10px; color: var(--ink); line-height: 1.4; }
.find-nav:hover { border-color: var(--accent); color: var(--accent); }
#findClose { margin-left: auto; }
mark.find-hit { background: #ffe58a; padding: 0 1px; border-radius: 2px; }
mark.find-hit.current { background: #ff9e3d; color: #fff; box-shadow: 0 0 0 2px rgba(255,158,61,.45); }

/* ---- 版面 ---- */
.layout { display: flex; padding-top: var(--topbar-h); height: 100%; }
.sidebar {
  width: var(--sidebar-w); flex-shrink: 0; height: calc(100vh - var(--topbar-h));
  overflow-y: auto; background: var(--panel); border-right: 1px solid var(--line); padding: 12px;
}
.main { flex: 1; height: calc(100vh - var(--topbar-h)); overflow-y: auto; }
.content { max-width: 880px; margin: 0 auto; padding: 30px 40px 90px; }

/* ---- 導覽樹 ---- */
.tree ul { list-style: none; margin: 2px 0; padding-left: 12px; }
.tree > details { margin-bottom: 2px; border-radius: 8px; }
.tree > details[open] { background: #fbfcfd; padding-bottom: 4px; }
.tree summary {
  cursor: pointer; padding: 8px 10px; border-radius: 8px; font-weight: 700; font-size: 14.5px;
  list-style: none; display: flex; align-items: center; justify-content: space-between;
}
.tree summary::-webkit-details-marker { display: none; }
.tree summary::after { content: "▸"; color: var(--muted); font-size: 11px; transition: transform .15s; }
.tree details[open] > summary::after { transform: rotate(90deg); }
.tree summary:hover { background: var(--bg); }
.tree summary .count { color: var(--muted); font-weight: 500; font-size: 11.5px; background: var(--bg); border-radius: 10px; padding: 1px 8px; margin-left: auto; margin-right: 8px; }
.tree .sem-label { font-size: 11.5px; color: var(--muted); padding: 6px 10px 2px; font-weight: 600; }
.tree a.note-link {
  display: block; padding: 6px 10px; border-radius: 7px; color: var(--ink);
  text-decoration: none; font-size: 13.5px; line-height: 1.5;
}
.tree a.note-link:hover { background: var(--accent-soft); }
.tree a.note-link.active { background: var(--accent); color: #fff; }

/* ---- 各科總覽入口（側欄）---- */
.subj-overview {
  display: block; margin: 2px 6px 4px; padding: 5px 10px; font-size: 12.5px; font-weight: 600;
  color: var(--accent); text-decoration: none; background: var(--accent-soft); border-radius: 7px;
}
.subj-overview:hover { background: #dbe8fe; }

/* ---- 各科總覽頁 ---- */
.ov { max-width: 900px; margin: 0 auto; }
.ov-head { border-bottom: 2px solid var(--line); padding-bottom: 12px; margin-bottom: 18px; }
.ov-head h1 { margin: 0 0 4px; font-size: 24px; }
.ov-sub { color: var(--muted); font-size: 13.5px; }
.ov-course { margin-bottom: 16px; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; background: #fbfcfd; }
.ov-course-title { display: inline-block; font-size: 15.5px; font-weight: 800; color: var(--ink); text-decoration: none; margin-bottom: 8px; }
.ov-course-title:hover { color: var(--accent); }
.ov-sem { font-size: 11.5px; font-weight: 500; color: var(--muted); background: var(--bg); border-radius: 8px; padding: 1px 7px; }
.ov-secs { display: flex; flex-wrap: wrap; gap: 6px; }
.ov-sec { font-size: 13px; text-decoration: none; padding: 4px 11px; border-radius: 14px; border: 1px solid var(--line); background: #fff; color: var(--ink); line-height: 1.5; }
.ov-sec:hover { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.ov-sec.lv2 { font-weight: 700; background: #eef4ff; border-color: #cfe0fb; }

/* ---- 搜尋結果 ---- */
.sr-head { font-size: 12px; color: var(--muted); padding: 6px 6px 10px; font-weight: 600; }
.sr-empty { color: var(--muted); font-size: 13px; padding: 8px; }
.sr-item { padding: 10px 12px; border-radius: 10px; margin-bottom: 8px; background: var(--bg); border: 1px solid transparent; }
.sr-item:hover { border-color: var(--line); }
.sr-title { font-weight: 700; font-size: 14px; cursor: pointer; color: var(--accent); line-height: 1.4; }
.sr-title:hover { text-decoration: underline; }
.sr-meta { font-size: 11px; color: var(--muted); margin: 1px 0 6px; }
.sr-subs[data-loading] { font-size: 11.5px; color: var(--muted); }
.sr-sub { padding: 5px 8px; border-left: 2px solid var(--line); margin: 4px 0; cursor: pointer; border-radius: 0 6px 6px 0; }
.sr-sub:hover { border-left-color: var(--accent); background: var(--accent-soft); }
.sr-sub-title { font-size: 12.5px; font-weight: 600; }
.sr-sub-ex { font-size: 11.5px; color: var(--muted); margin-top: 1px; }
mark { background: var(--mark); color: inherit; padding: 0 1px; border-radius: 2px; }

/* ---- 內文 ---- */
.welcome { padding: 36px 6px; color: var(--muted); }
.welcome h2 { color: var(--ink); font-size: 24px; }
.welcome-stat { font-weight: 600; }
.note-crumb { font-size: 13px; color: var(--muted); }
.content h1 { font-size: 27px; margin: 4px 0 2px; line-height: 1.35; }
.note-info { font-size: 12.5px; color: var(--muted); margin-bottom: 22px; }
.note-toc {
  background: var(--bg); border: 1px solid var(--line); border-radius: 12px;
  padding: 14px 18px; margin-bottom: 28px;
}
.note-toc-title { font-weight: 800; font-size: 12.5px; color: var(--muted); margin-bottom: 8px; letter-spacing: .5px; }
.note-toc ul { margin: 0; padding-left: 18px; list-style: none; }
.note-toc > ul { padding-left: 4px; }
.note-toc li { margin: 2px 0; }
.note-toc a { color: var(--accent); text-decoration: none; font-size: 13.5px; }
.note-toc a:hover { text-decoration: underline; }

.note-body { font-size: 15.5px; }
.note-body h2, .note-body h3, .note-body h4, .note-body h5, .note-body h6 {
  scroll-margin-top: calc(var(--topbar-h) + 14px); line-height: 1.45;
}
/* 標題階層：主題(h2) > §(h3) > 一.(h4) > 1.(h5) > (1)(h6) */
.note-body h2 { font-size: 23px; margin-top: 42px; padding: 6px 0 6px 12px; border-left: 5px solid var(--accent); background: var(--accent-soft); border-radius: 0 6px 6px 0; }
.note-body h3 { font-size: 19px; margin-top: 32px; border-bottom: 2px solid var(--line); padding-bottom: 5px; color: #1d4ed8; }
.note-body h4 { font-size: 16.5px; margin-top: 22px; margin-left: 0.3em; }
.note-body h5 { font-size: 15px; margin-top: 16px; margin-left: 1.6em; color: #374151; }
.note-body h6 { font-size: 14.5px; font-weight: 600; margin-top: 12px; margin-left: 3.2em; color: #4b5563; }
/* 大綱清單：移除多餘項目符號（手動編號已在文字內），靠縮排呈現階層 */
/* 大綱列舉項：以手動編號開頭的段落用 ol-N class 依階層縮排 */
.note-body ul { list-style: disc; margin: 8px 0; padding-left: 1.4em; }
.note-body li { margin: 4px 0; }
.note-body p.ol-1 { margin: 4px 0 4px 0.2em; }
.note-body p.ol-2 { margin: 3px 0 3px 1.8em; }
.note-body p.ol-3 { margin: 3px 0 3px 3.4em; color: #374151; }
.note-body p.ol-4 { margin: 3px 0 3px 5.0em; color: #4b5563; }
.note-body ol { padding-left: 1.6em; }
.note-body p { margin: 10px 0; }
.note-body img { max-width: 100%; height: auto; display: block; margin: 14px auto; border-radius: 8px; box-shadow: 0 1px 6px rgba(0,0,0,.08); }
.note-body .tbl-wrap { overflow-x: auto; margin: 16px 0; }
.note-body table { border-collapse: collapse; margin: 0 auto; font-size: 13.5px; }
.note-body th, .note-body td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; vertical-align: top; }
.note-body th { background: var(--bg); font-weight: 700; text-align: center; }
.note-body code { background: var(--bg); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.note-body strong { color: #0f172a; }
.note-body blockquote { margin: 12px 0; padding: 7px 14px; border-left: 3px solid #cbd5e1; background: #f8fafc; color: #475569; font-size: 14px; border-radius: 0 6px 6px 0; }
.note-body blockquote::before { content: "🖼 圖內文字　"; font-size: 11.5px; color: #94a3b8; }

/* ---- 行動裝置 ---- */
.overlay { position: fixed; inset: var(--topbar-h) 0 0 0; background: rgba(0,0,0,.35); z-index: 19; }
@media (max-width: 820px) {
  .menu-toggle { display: block; }
  .site-title { display: none; }
  .sidebar {
    position: fixed; top: var(--topbar-h); left: 0; z-index: 20; width: 86%; max-width: 340px;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: 2px 0 16px rgba(0,0,0,.18);
  }
  .sidebar.open { transform: translateX(0); }
  .content { padding: 20px 18px 70px; }
  #subjectFilter { display: none; }
  .notes-panel { width: 86% !important; max-width: 360px; box-shadow: -2px 0 16px rgba(0,0,0,.18); }
  .panel-toggle .pt-label { display: none; }
}

/* ---- 書籤（放左側欄，與共筆清單切換）---- */
.pt-badge { margin-left: 6px; background: #e7b500; color: #fff; font-size: 11px; font-weight: 700; border-radius: 10px; padding: 0 6px; }
.panel-toggle.active { background: #fff6d6; border-color: #e7c84a; color: #9a7d12; }
.bm-view-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 4px 4px 10px; margin-bottom: 6px; border-bottom: 1px solid var(--line);
}
.bm-view-title { font-weight: 800; font-size: 15px; }
.bm-empty { color: var(--muted); font-size: 13px; padding: 20px 14px; text-align: center; line-height: 1.9; }

.bm-subj { border: 1px solid #f1e6b8; border-radius: 8px; background: #fffdf3; margin-bottom: 8px; }
.bm-subj > summary {
  cursor: pointer; padding: 9px 12px; font-weight: 700; font-size: 14.5px; list-style: none;
  display: flex; align-items: center; gap: 6px;
}
.bm-subj > summary::-webkit-details-marker { display: none; }
.bm-subj > summary::before { content: "▸"; color: #b8930b; font-size: 11px; transition: transform .15s; }
.bm-subj[open] > summary::before { transform: rotate(90deg); }
.bm-subj-name { flex: 1; }
.bm-subj > summary .count { color: var(--muted); font-weight: 500; font-size: 11.5px; background: #fff; border: 1px solid #f1e6b8; border-radius: 10px; padding: 1px 8px; }

.bm-group { margin: 2px 0 6px; }
.bm-course { font-size: 11.5px; font-weight: 700; color: #8a6d1a; padding: 4px 10px 1px; }
.bm-item { display: flex; align-items: center; gap: 4px; padding: 1px 8px 1px 14px; }
.bm-item.lv-2 .bm-link { padding-left: 14px; }
.bm-item.lv-3 .bm-link { padding-left: 24px; }
.bm-item.lv-4 .bm-link { padding-left: 34px; color: var(--muted); }
.bm-link { flex: 1; color: var(--ink); text-decoration: none; font-size: 13px; padding: 4px 6px; border-radius: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bm-link:hover { background: var(--accent-soft); }
.bm-del { background: none; border: none; color: var(--muted); cursor: pointer; font-size: 13px; padding: 2px 5px; border-radius: 4px; }
.bm-del:hover { background: #fde2e2; color: #c0392b; }

/* 小節旁的書籤星號（標題與 (一) 段落）— 放大、平時即可見 */
.note-body h2, .note-body h3, .note-body h4, .note-body p.ol-1 { position: relative; }
/* 跳轉錨點上方留白，避免貼齊容器頂端 */
.note-body h1, .note-body h2, .note-body h3, .note-body h4, .note-body p.ol-1 { scroll-margin-top: 16px; }
.sec-bm {
  border: none; background: none; cursor: pointer; color: #c2a94a; font-size: 1.4em; line-height: 1;
  margin-left: 10px; padding: 0 4px; vertical-align: middle; opacity: .5; transition: opacity .12s, color .12s, transform .12s;
}
.note-body h2:hover .sec-bm, .note-body h3:hover .sec-bm,
.note-body h4:hover .sec-bm, .note-body p.ol-1:hover .sec-bm { opacity: 1; }
.sec-bm:hover { color: #e0a800; transform: scale(1.25); }
.sec-bm.on { opacity: 1; font-size: 1.5em; filter: drop-shadow(0 0 1px rgba(180,120,0,.5)); }

/* 已收藏的小節：整段明顯高亮（金色左條＋底色），不易忽略 */
.note-body .secbm-on {
  background: linear-gradient(90deg, #fff3c4, #fffdf3 70%);
  border-left: 5px solid #f5b301; border-radius: 4px;
  padding-top: 3px; padding-bottom: 3px; padding-left: 12px;
  box-shadow: 0 1px 4px rgba(213,164,12,.18);
}
.note-body p.ol-1.secbm-on { margin-left: 0.2em; }

/* ---- 筆記側欄 ---- */
.panel-toggle {
  flex-shrink: 0; font-size: 13.5px; font-weight: 600; padding: 8px 14px; border-radius: 18px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink); cursor: pointer; transition: all .15s;
}
.panel-toggle:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.notes-panel {
  width: 340px; flex-shrink: 0; height: calc(100vh - var(--topbar-h)); background: var(--panel);
  border-left: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px;
  position: relative;
}
.notes-panel[hidden] { display: none; }
.notes-resizer {
  position: absolute; left: -3px; top: 0; width: 7px; height: 100%; cursor: ew-resize;
  z-index: 5; touch-action: none;
}
.notes-resizer::before {
  content: ""; position: absolute; left: 3px; top: 0; width: 1px; height: 100%; background: transparent;
}
.notes-resizer:hover::before, body.notes-resizing .notes-resizer::before { background: var(--accent); width: 2px; left: 2px; }
body.notes-resizing { cursor: ew-resize; user-select: none; }
.notes-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.notes-title { font-weight: 800; font-size: 15px; }
.notes-actions { display: flex; gap: 6px; }
.np-btn { font-size: 12.5px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); cursor: pointer; color: var(--ink); }
.np-btn:hover { border-color: var(--accent); color: var(--accent); }
.notes-scope { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
#notesScope { flex: 1; min-width: 130px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-size: 13px; color: var(--ink); }
.gen-controls { display: flex; gap: 4px; width: 100%; }
#genSelect { flex: 1; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); font-size: 12.5px; color: var(--ink); }
.gen-controls .np-btn { padding: 4px 8px; }
.notes-for { font-size: 12px; color: var(--muted); margin-bottom: 8px; padding: 6px 8px; background: var(--bg); border-radius: 6px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
/* 格式工具列 */
.notes-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 3px; margin-bottom: 6px; }
.tb-btn {
  min-width: 26px; height: 28px; padding: 0 7px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); color: var(--ink); font-size: 12.5px; line-height: 1; cursor: pointer;
}
.tb-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.tb-btn:active { transform: translateY(1px); }
.tb-sep { width: 1px; align-self: stretch; margin: 2px 3px; background: var(--line); }

/* 編輯器（contenteditable） */
.notes-editor {
  flex: 1; width: 100%; overflow-y: auto; border: 1px solid var(--line); border-radius: 10px; padding: 12px;
  font-family: inherit; font-size: 14px; line-height: 1.7; outline: none; background: #fffef9; color: var(--ink);
}
.notes-editor:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.notes-editor.disabled { background: #f3f4f6; color: var(--muted); cursor: not-allowed; }
.notes-editor:empty::before { content: attr(data-placeholder); color: var(--muted); }
.notes-editor ul, .notes-editor ol { margin: 4px 0; padding-left: 24px; }
.notes-editor li { margin: 2px 0; }
.notes-editor h4 { font-size: 15px; font-weight: 800; margin: 8px 0 4px; color: var(--accent); }
/* 待辦清單 */
.notes-editor ul[data-check] { list-style: none; padding-left: 6px; }
.notes-editor ul[data-check] > li { position: relative; padding-left: 24px; cursor: pointer; }
.notes-editor ul[data-check] > li::before {
  content: "☐"; position: absolute; left: 0; top: 0; font-size: 15px; color: var(--muted);
}
.notes-editor ul[data-check] > li[data-check="1"]::before { content: "☑"; color: var(--accent); }
.notes-editor ul[data-check] > li[data-check="1"] { color: var(--muted); text-decoration: line-through; }
.notes-hint { font-size: 11px; color: var(--muted); margin-top: 8px; line-height: 1.5; }

@media (max-width: 820px) {
  .notes-panel { position: fixed; top: var(--topbar-h); right: 0; z-index: 21; height: calc(100vh - var(--topbar-h)); }
}
