/* ==========================================================================
   閱讀介面版面：工具列、側欄（目錄）、麵包屑列、閱讀欄、設定面板、群組頁
   RWD：≥1024 桌機（側欄常駐可拖曳）；768–1023 平板（側欄抽屜）；<768 手機
   ========================================================================== */

body.reader { display: flex; flex-direction: column; min-height: 100vh; }

/* ---------- 工具列 ---------- */
.topbar {
  position: sticky; top: 0; z-index: 40;
  height: var(--topbar-h);
  display: flex; align-items: center; gap: .25rem;
  padding: 0 .5rem 0 .25rem;
  background: var(--bg);
  border-bottom: 1px solid var(--rule);
}
.brand {
  display: flex; align-items: center;
  min-width: 0; margin-right: .5rem;
  font-family: var(--font-serif); font-weight: 600; font-size: 1.05rem; letter-spacing: .08em;
  color: var(--head); text-decoration: none; white-space: nowrap;
}
.brand-text { overflow: hidden; text-overflow: ellipsis; }
.topbar-spacer { flex: 1; }

.topbar-search {
  display: flex; align-items: center; gap: .25rem;
  width: min(360px, 34vw);
}
.topbar-search .input { min-height: 34px; background: var(--surface); border-color: transparent; }
.topbar-search .input:focus { background: var(--bg); border-color: var(--primary); }
.search-toggle { display: none; }

/* ---------- 外殼：側欄 + 內容 ---------- */
.shell { flex: 1; display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); }
html.sidebar-hidden .shell { grid-template-columns: minmax(0, 1fr); }
html.sidebar-hidden .sidebar { display: none; }

.sidebar {
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  display: flex; flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--rule);
}
.sidebar-head {
  display: flex; align-items: center; gap: .5rem;
  padding: .6rem .75rem .35rem;
}
.sidebar-title { font-size: .8rem; letter-spacing: .2em; color: var(--muted); flex: 1; }
.toc-tools { display: flex; align-items: center; gap: .25rem; padding: 0 .75rem .5rem; }
.toc-tools .input { min-height: 32px; font-size: .875rem; }
.toc-count { font-size: .75rem; color: var(--muted); min-width: 3.2em; text-align: center; }
.toc-tools .btn-icon { width: 32px; height: 32px; min-height: 32px; color: var(--muted); }
.toc-tools .btn-icon .icon { width: 1.1rem; height: 1.1rem; }
.toc-tools .btn-icon:hover { color: var(--text); }
.toc-actions { display: flex; gap: .25rem; padding: 0 .75rem .4rem; }

.tree { flex: 1; overflow-y: auto; overflow-x: hidden; padding: .25rem .5rem 3rem; font-size: .9375rem; scroll-padding-block: 30%; overscroll-behavior: contain; }

.resizer {
  position: absolute; top: 0; right: -3px; width: 7px; height: 100%;
  cursor: col-resize; z-index: 2;
}
.resizer:hover, html.resizing .resizer { background: var(--primary-soft); }
html.resizing { cursor: col-resize; user-select: none; }

.scrim { display: none; position: fixed; inset: var(--topbar-h) 0 0 0; background: var(--scrim); z-index: 44; }

/* ---------- 目錄樹 ---------- */
.tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul ul { padding-left: .8rem; border-left: 1px solid var(--rule); margin-left: .62rem; }
/* 第 6 層以下不再縮排（科判最深 18 層），只靠導引線與字級區分 */
.tree ul ul ul ul ul ul ul { padding-left: .15rem; margin-left: .3rem; }
.tree ul ul ul ul ul ul ul .node-label { font-size: .9rem; }
.tree li { margin: 0; }
.node { display: flex; align-items: flex-start; gap: .1rem; border-radius: 4px; }
.node:hover { background: var(--surface-2); }
.twisty {
  flex: none; width: 1.35rem; height: 1.7rem; margin-top: .05rem;
  border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 0;
  display: inline-flex; align-items: center; justify-content: center; border-radius: 4px;
}
/* 展開箭頭以 CSS 邊框繪製 */
.twisty::before {
  content: ""; width: .4rem; height: .4rem;
  border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor;
  transform: translateX(-.05rem) rotate(-45deg);
  transition: transform var(--ease);
}
li[aria-expanded="true"] > .node > .twisty::before { transform: translateY(-.1rem) rotate(45deg); }
li:not(.has-children) > .node > .twisty { visibility: hidden; }
.node-label {
  flex: 1; min-width: 0;
  display: block;
  padding: .3rem .4rem .3rem .1rem;
  color: var(--text); text-decoration: none; line-height: 1.35;
  overflow-wrap: anywhere;
}
.node-label:hover { color: var(--primary); }
.node-page { display: inline-block; margin-left: .45em; font-size: .72rem; color: var(--faint); font-variant-numeric: tabular-nums; white-space: nowrap; }
li.is-group > .node > .node-label { font-family: var(--font-serif); font-weight: 600; letter-spacing: .04em; color: var(--head); }
li.is-book > .node > .node-label { font-weight: 500; }
li.is-current > .node { background: var(--primary-soft); }
li.is-current > .node > .node-label { color: var(--primary); font-weight: 500; }
li.is-current > .node { box-shadow: inset 3px 0 0 var(--accent); }
li[aria-expanded="false"] > ul { display: none; }
.node-label mark { background: var(--hl); color: inherit; padding: 0 .05em; border-radius: 2px; }
li.is-match > .node > .node-label .node-text { color: var(--accent); }
li.is-match-cur > .node { outline: 2px solid var(--accent); outline-offset: -2px; }
.tree-empty { padding: 1rem; color: var(--muted); font-size: .875rem; text-align: center; }

/* ---------- 內容區 ---------- */
.content { min-width: 0; display: flex; flex-direction: column; }

.crumbbar {
  position: sticky; top: var(--topbar-h); z-index: 30;
  min-height: var(--crumbbar-h);
  display: flex; align-items: center; gap: .75rem;
  padding: .25rem 1.25rem;
  background: var(--bg);
  border-bottom: 1px solid var(--rule);
  font-size: .875rem;
}
.crumbs { flex: 1; min-width: 0; display: flex; align-items: center; overflow: hidden; white-space: nowrap; }
/* 每層都要能截斷：窄螢幕下 flex 會把 crumb 壓得比內容窄，沒有 ellipsis 的話文字會溢出、疊到下一層上面 */
.crumb { color: var(--muted); min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.crumb a { color: var(--primary); text-decoration: none; }
.crumb a:hover { text-decoration: underline; text-underline-offset: .2em; }
.crumb + .crumb::before { content: "›"; margin: 0 .45em; color: var(--muted); }
.crumb.is-current { color: var(--text); font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
.crumb-page { color: var(--muted); font-size: .8em; margin-left: .35em; font-variant-numeric: tabular-nums; }
.doc-switch { display: flex; gap: .15rem; flex: none; }
.doc-switch .btn { font-size: .8125rem; color: var(--muted); min-height: 30px; padding: 0 .5em; }
.doc-switch .btn:hover { color: var(--primary); }
.doc-switch .btn .icon { width: 1em; height: 1em; }

.page { flex: 1; width: 100%; padding: 2rem var(--gutter) 40vh; }
/* 版心：max-width 以 em 定義，隨字級縮放時每行字數不變 */
.page-inner { max-width: var(--measure); margin: 0 auto; font-size: var(--fs); }
.page-inner > * { font-size: 1rem; }
.page-inner > .doc { font-size: 1em; }

/* 卷首（每本書一次） */
.doc-head { margin: 0 auto 2.5rem; text-align: center; font-family: var(--font-serif); }
.doc-kicker { font-size: .8rem; letter-spacing: .3em; color: var(--muted); margin-bottom: .8rem; font-family: var(--font-ui); }
.doc-title { font-family: var(--font-kai); font-weight: 400; font-size: 1.9rem; line-height: 1.35; color: var(--head); letter-spacing: .06em; }
.doc-meta { margin-top: .9rem; color: var(--muted); font-size: .9rem; line-height: 1.7; }

.doc-foot { margin: 4rem auto 0; display: flex; justify-content: space-between; gap: 1rem; border-top: 1px solid var(--rule); padding-top: 1.25rem; font-family: var(--font-ui); font-size: .9rem; }
.doc-foot a { text-decoration: none; color: var(--primary); display: inline-flex; align-items: center; gap: .3em; max-width: 48%; }
.doc-foot a:hover { text-decoration: underline; text-underline-offset: .2em; }
.doc-foot .icon { width: 1em; height: 1em; flex: none; }
.doc-foot .ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.loading, .error-box { text-align: center; color: var(--muted); padding: 4rem 1rem; font-family: var(--font-ui); }
.error-box strong { display: block; color: var(--text); font-size: 1.1rem; margin-bottom: .5rem; }

/* 群組（書單）頁 */
.group-view { max-width: 60rem; margin: 0 auto; font-family: var(--font-serif); }
/* 群組標題：楷體、磁青字、不加框不加底；與 home.css 的 .shelf-title 同款，改要一起改 */
.group-title { margin: 0 0 .5rem; color: var(--head); font-family: var(--font-kai); font-size: 1.5rem; letter-spacing: .2em; line-height: 1.3; }
.group-lead { color: var(--muted); font-size: .95rem; line-height: 1.9; margin-bottom: 1.5rem; }
.book-list { list-style: none; border-top: 1px solid var(--rule); }
.book-item { border-bottom: 1px solid var(--rule); }
.book-link { display: block; padding: 1.1rem .25rem; text-decoration: none; color: inherit; }
.book-link:hover .book-title { color: var(--primary); }
.book-title { font-size: 1.2rem; font-weight: 600; letter-spacing: .04em; color: var(--head); line-height: 1.4; transition: color var(--ease); }
.book-authors { margin-top: .35rem; color: var(--muted); font-size: .9rem; line-height: 1.6; overflow-wrap: anywhere; }

/* ---------- 浮動元件 ---------- */
.backtop {
  position: fixed; right: 1.25rem; bottom: 1.25rem; z-index: 35;
  width: 42px; height: 42px; border-radius: 50%;
  background: var(--bg); border: 1px solid var(--rule); color: var(--primary);
  box-shadow: var(--shadow);
  opacity: 0; transform: translateY(8px); pointer-events: none;
  transition: opacity var(--ease), transform var(--ease);
}
.backtop.is-visible { opacity: 1; transform: none; pointer-events: auto; }

.fullscreen-exit {
  position: fixed; right: 1.25rem; top: 1rem; z-index: 60;
  background: var(--bg); border: 1px solid var(--rule); box-shadow: var(--shadow);
  opacity: .55;
}
.fullscreen-exit:hover { opacity: 1; }
html:not(.fullscreen) .fullscreen-exit { display: none; }
html.fullscreen .topbar, html.fullscreen .sidebar, html.fullscreen .crumbbar { display: none; }
html.fullscreen .shell { grid-template-columns: minmax(0, 1fr); }

/* ---------- 設定面板 ---------- */
.settings-pop {
  position: fixed; top: calc(var(--topbar-h) + 6px); right: 8px; z-index: 50;
  width: 340px; max-height: calc(100vh - var(--topbar-h) - 16px); overflow: auto;
  padding: 1rem 1.1rem 1.2rem;
  background: var(--bg); border: 1px solid var(--rule); border-radius: var(--radius); box-shadow: var(--shadow);
}
.settings-pop[hidden] { display: none; }
.settings-row { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .55rem 0; border-top: 1px solid var(--rule); }
.settings-row:first-of-type { border-top: 0; padding-top: 0; }
.settings-label { font-size: .8125rem; color: var(--muted); letter-spacing: .1em; flex: none; }
.settings-group-title { font-size: .75rem; color: var(--muted); letter-spacing: .2em; margin: .9rem 0 .2rem; }
.stepper { display: inline-flex; align-items: center; gap: .25rem; }
.stepper .btn-icon { width: 32px; height: 32px; min-height: 32px; border: 1px solid var(--rule); }
.stepper output { min-width: 3.2em; text-align: center; font-variant-numeric: tabular-nums; }
.swatches { display: inline-flex; gap: .5rem; }
.swatch {
  width: 34px; height: 34px; border-radius: 50%; border: 2px solid var(--rule); cursor: pointer; padding: 0;
  font-size: 0; position: relative;
}
.swatch[aria-pressed="true"] { border-color: var(--primary); box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--primary); }
.swatch-white { background: #fff; }
.swatch-paper { background: #f6f1e6; }
.swatch-gray { background: #ece8e7; }
.swatch-dark { background: #16202b; }
.swatch-auto { background: linear-gradient(135deg, #fff 50%, #16202b 50%); }
.swatch-name { display: block; font-size: .7rem; color: var(--muted); text-align: center; margin-top: .25rem; letter-spacing: .05em; }
.swatch-wrap { display: flex; flex-direction: column; align-items: center; }
.seg-font .serif { font-family: var(--font-serif); }
.seg-font .sans { font-family: var(--font-sans); }

/* ---------- 響應式 ---------- */
@media (max-width: 1023px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; top: var(--topbar-h); left: 0; bottom: 0; height: auto;
    width: min(360px, 88vw); z-index: 45;
    transform: translateX(-100%); transition: transform var(--ease);
    box-shadow: none;
  }
  html.sidebar-open .sidebar { transform: none; box-shadow: var(--shadow); }
  html.sidebar-open .scrim { display: block; }
  html.sidebar-hidden .sidebar { display: flex; }
  .resizer { display: none; }
  .topbar-search { width: min(280px, 30vw); }
}

@media (max-width: 767px) {
  :root { --gutter: 1rem; }
  .brand { font-size: .95rem; letter-spacing: .04em; }
  .topbar-search { display: none; }
  .search-toggle { display: inline-flex; }
  html.search-open .topbar-search {
    display: flex; position: absolute; left: .5rem; right: .5rem; top: calc(var(--topbar-h) + 6px); width: auto;
    background: var(--bg); padding: .35rem; border: 1px solid var(--rule); border-radius: var(--radius); box-shadow: var(--shadow);
  }
  .crumbbar { padding: .25rem .75rem; }
  /* 手機：麵包屑只留上一層與目前章節 */
  .crumb:not(.is-current):not(:has(+ .crumb.is-current)) { display: none; }
  .crumb:has(+ .crumb.is-current)::before { content: none; margin: 0; }
  /* 位置不夠時先犧牲上一層：讀者要看的是「現在在哪」，上一層只是回去的入口，留幾個字可點就夠 */
  .crumb:not(.is-current) { flex-shrink: 999; min-width: 3.5em; }
  .doc-switch .btn span { display: none; }
  .page { padding-top: 1.25rem; }
  .doc-title { font-size: 1.5rem; }
  .settings-pop {
    top: auto; right: 0; left: 0; bottom: 0; width: auto; max-height: 82vh;
    border-radius: 14px 14px 0 0; border-bottom: 0;
  }
  .group-title { font-size: 1.2rem; letter-spacing: .12em; }   /* 與首頁相同，13 字在 360px 一行放得下 */
  .backtop { right: .75rem; bottom: .75rem; }
}

/* ---------- 檢索回跳導覽列（ui/hitbar.js） ---------- */
.hitbar {
  position: fixed; top: calc(var(--topbar-h) + var(--crumbbar-h) + 8px); left: 50%; transform: translateX(-50%); z-index: 36;
  display: flex; align-items: center; gap: .15rem;
  max-width: calc(100vw - 1.5rem); padding: .2rem .3rem .2rem .85rem;
  background: var(--bg); border: 1px solid var(--rule); border-radius: 999px; box-shadow: var(--shadow);
  font-family: var(--font-ui); font-size: .875rem;
}
.hitbar-q { font-family: var(--font-serif); font-weight: 600; color: var(--head); max-width: 14em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hitbar-count { color: var(--muted); margin: 0 .5rem 0 .35rem; white-space: nowrap; }
.hitbar .btn-icon { width: 32px; height: 32px; min-height: 32px; }
.hitbar .btn-icon .icon { width: 1.15rem; height: 1.15rem; }
html.fullscreen .hitbar { top: 10px; }

@media (max-width: 767px) {
  .hitbar {
    top: auto; bottom: .75rem; left: .75rem; right: .75rem; transform: none; max-width: none;
    justify-content: space-between; border-radius: 12px; padding: .25rem .3rem .25rem .7rem;
  }
  .hitbar-q { max-width: 7em; }
  html.has-hits .backtop { bottom: 4.25rem; }
}
