/* ===== Design tokens ===== */
:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #2b2822;
  --text-dim: #8a8478;
  --border: #e4dfd5;
  --accent: #8c6f4e;        /* 書卷棕 */
  --accent-ink: #ffffff;
  --danger: #b4544a;
  --mark: #f5e4a8;
  --progress-track: #ddd3bf; /* 進度條軌道：要看得見（Dino 2026-08-16 拍板） */
  --shadow: 0 1px 3px rgba(60, 50, 30, .08), 0 4px 16px rgba(60, 50, 30, .06);
  --radius: 12px;
  --font-ui: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  --font-read: "Noto Serif TC", "PMingLiU", Georgia, serif;
}

/* 閱讀器主題（body[data-theme] 切換） */
body[data-theme="sepia"] {
  --bg: #f3e9d2; --surface: #faf3e3; --text: #4a3f2e; --text-dim: #94856b;
  --border: #e0d3b4; --mark: #efd98f; --progress-track: #d9c69a;
}
body[data-theme="dark"] {
  --bg: #17181c; --surface: #1f2127; --text: #c9c7c0; --text-dim: #9a978e;
  --border: #2e3038; --accent: #c99a5b; --accent-ink: #17181c; --mark: #5a4d22;
  --progress-track: #3c3f49;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-ui); line-height: 1.6;
}
button { font-family: inherit; cursor: pointer; }
a { color: var(--accent); text-decoration: none; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 10px; padding: 8px 14px; font-size: 14px;
  transition: background .15s, border-color .15s;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-icon { min-width: 44px; min-height: 44px; padding: 8px; font-size: 16px; line-height: 1; }

/* 統一線性 SVG 圖示（stroke 1.75、currentColor，跟著主題換色） */
svg.icon { width: 20px; height: 20px; vertical-align: -4px; }

/* 鍵盤焦點可見 */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.toolbar button:focus-visible,
.drawer-body .item:focus-visible,
.drawer-body .item-del:focus-visible { outline-offset: -2px; }

/* ===== 登入頁 ===== */
.login-wrap {
  min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px;
}
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 32px 28px; text-align: center;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { margin: 0 0 20px; color: var(--text-dim); font-size: 13px; }
.login-card input {
  width: 100%; padding: 10px 12px; margin-bottom: 12px; font-size: 16px;
  border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--text);
}
.login-card .btn { width: 100%; padding: 10px; font-size: 15px; }
.login-error { color: var(--danger); font-size: 13px; min-height: 20px; margin: 8px 0 0; }
.pw-field { position: relative; }
.pw-field input { padding-right: 48px; }
.pw-toggle {
  position: absolute; right: 2px; top: 50%; transform: translateY(-50%);
  min-width: 44px; min-height: 44px; background: none; border: none; color: var(--text-dim);
}
.pw-toggle:hover { color: var(--accent); }

/* ===== 書架 ===== */
.topbar {
  position: static; display: flex; align-items: center; gap: 12px;
  padding: 14px 18px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { margin: 0; font-size: 18px; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.shelf { max-width: 900px; margin: 0 auto; padding: 18px; }
.shelf-empty { text-align: center; color: var(--text-dim); padding: 60px 20px; }
.book-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.book-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; display: flex; flex-direction: column; gap: 8px;
}
.book-card h2 { margin: 0; font-size: 16px; line-height: 1.4; }
.book-meta { color: var(--text-dim); font-size: 12.5px; }
.book-progress-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.book-progress-bar > div { height: 100%; background: var(--accent); border-radius: 3px; }
.book-actions { display: flex; gap: 8px; margin-top: 4px; }
.book-actions .btn { flex: 1; }
.book-warn { color: var(--danger); font-size: 12px; }

/* 上傳進度浮層 */
.overlay {
  position: fixed; inset: 0; background: rgba(20, 16, 8, .45);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 100;
}
.overlay[hidden] { display: none; }
.overlay-card {
  width: 100%; max-width: 420px; background: var(--surface); border-radius: var(--radius);
  padding: 24px; box-shadow: var(--shadow);
}
.overlay-card h3 { margin: 0 0 10px; font-size: 16px; }
.overlay-card .steps { font-size: 13.5px; color: var(--text-dim); white-space: pre-line; }
.upload-bar { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; margin-top: 12px; }
.upload-bar > div { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

/* ===== 閱讀器 ===== */
/* 頂欄與底部工具列同步顯示/隱藏（點內文叫出、捲動收起、可置頂固定） */
.reader-top {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  transform: translateY(-110%); transition: transform .2s ease;
}
.reader-top.show { transform: none; }
.reader-top .titles { flex: 1; min-width: 0; }
.reader-top .book-title { font-size: 13px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.reader-top .chapter-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.reader-progress {
  position: fixed; top: 0; left: 0; right: 0; z-index: 49; /* 恆顯；頂欄滑入時蓋過它 */
  height: 4px; background: var(--progress-track);
}
.reader-progress > div { height: 100%; width: 0; background: var(--accent); }

.reader-main {
  max-width: 720px; margin: 0 auto; padding: 28px 20px 80px;
  font-family: var(--font-read); font-size: var(--read-size, 19px);
  line-height: var(--read-lh, 1.9);
}
.reader-main h2.ch-title { font-size: 1.25em; margin: 0 0 1em; }
.reader-main p { margin: 0 0 1em; text-align: justify; overflow-wrap: break-word; }
.reader-main mark {
  background: var(--mark); color: inherit; border-radius: 3px; padding: 0 1px; cursor: pointer;
}
.reader-main mark.has-note { border-bottom: 2px solid var(--accent); }

.reader-nav {
  display: flex; gap: 10px; max-width: 720px; margin: 0 auto;
  padding: 0 20px calc(96px + env(safe-area-inset-bottom)); /* 底部留空，避免被工具列蓋住 */
}
.reader-nav .btn { flex: 1; padding: 12px; }

/* 底部工具列：點內文切換顯示、捲動時自動收起（不常駐蓋內容） */
.toolbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: flex; justify-content: space-around; align-items: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  transform: translateY(110%); transition: transform .2s ease;
}
.toolbar.show { transform: none; }
.toolbar button {
  background: none; border: none; color: var(--text-dim); font-size: 12px;
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 4px 10px;
}
.toolbar button .icon,
.toolbar button .ico { display: block; margin: 0 auto 2px; }
.toolbar button .ico { font-size: 16px; font-weight: 600; line-height: 20px; height: 20px; }
.toolbar button:hover, .toolbar button.active { color: var(--accent); }

/* 側欄（章節/書籤/註解 列表） */
.drawer {
  position: fixed; top: 0; bottom: 0; right: 0; width: min(340px, 88vw); z-index: 120;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: var(--shadow);
  transform: translateX(100%); transition: transform .22s ease; display: flex; flex-direction: column;
}
.drawer.open { transform: translateX(0); }
.drawer-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.drawer-body { flex: 1; overflow-y: auto; }
.drawer-body .item {
  display: block; width: 100%; text-align: left; background: none; border: none;
  border-bottom: 1px solid var(--border); padding: 12px 16px; color: var(--text); font-size: 14px;
}
.drawer-body .item:hover { background: var(--bg); }
.drawer-body .item.current { color: var(--accent); font-weight: 600; }
.drawer-body .item .sub { display: block; color: var(--text-dim); font-size: 12px; margin-top: 2px; }
.drawer-body .item-row { display: flex; align-items: center; }
.drawer-body .item-row .item { flex: 1; border-bottom: none; }
.drawer-body .item-row { border-bottom: 1px solid var(--border); }
.drawer-body .item-del { background: none; border: none; color: var(--text-dim); padding: 12px; font-size: 15px; }
.drawer-body .item-del:hover { color: var(--danger); }
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 110; }
.drawer-backdrop[hidden] { display: none; }

/* 設定面板 */
.settings {
  position: fixed; left: 50%; bottom: 70px; transform: translateX(-50%); z-index: 60;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px 18px; width: min(360px, 92vw);
}
.settings[hidden] { display: none; }
.settings { max-height: calc(100dvh - 100px); overflow-y: auto; } /* 橫向短螢幕不被裁切 */
.settings .row { display: flex; align-items: center; gap: 10px; margin: 8px 0; font-size: 14px; }
.settings .row .lab { width: 56px; flex: 0 0 auto; color: var(--text-dim); }
.settings .row .btn { flex: 1; }
.settings .row input, .settings .row select {
  flex: 1; min-width: 0; padding: 11px 12px; font-size: 14px; /* 44px 觸控高度 */
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text);
}
.settings .settings-divider { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 12px; }

/* 工具列鈕啟用態沿用既有 .active（置頂鈕也用它） */

/* 章節側欄：搜尋列（固定表頭式導覽） */
.drawer-search {
  position: sticky; top: 0; z-index: 1; background: var(--surface);
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.drawer-search input {
  width: 100%; padding: 11px 12px; font-size: 14px; /* 44px 觸控高度 */
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text);
}

/* 章節側欄：已看過收納群組；.action 是動作列（如加書籤），用 accent 色跟分類標題區隔 */
.drawer-body .group-head { font-weight: 600; color: var(--text-dim); background: var(--bg); }
.drawer-body .group-head.action { color: var(--accent); background: var(--surface); }
.drawer-body .group-head .arrow { display: inline-block; width: 1em; transition: transform .15s; }
.drawer-body .group-head.open .arrow { transform: rotate(90deg); }

/* 偽裝模式：iframe 蓋全頁 + 透明擋板 */
#disguise { position: fixed; inset: 0; z-index: 9999; background: #fff; }
#disguise[hidden] { display: none; }
#disguise iframe { width: 100%; height: 100%; border: 0; display: block; }
#disguiseShield { position: absolute; inset: 0; }

/* 選取文字浮動工具列 */
.sel-toolbar {
  position: absolute; z-index: 130; display: flex; gap: 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 6px;
}
.sel-toolbar[hidden] { display: none; }
.sel-toolbar button {
  border: none; background: none; font-size: 13px; padding: 6px 10px; color: var(--text);
  border-radius: 6px; display: inline-flex; align-items: center; gap: 4px; min-height: 40px;
}
.sel-toolbar button .icon { width: 16px; height: 16px; }
.sel-toolbar button:hover { background: var(--bg); }

/* 註解檢視小卡 */
.note-pop {
  position: absolute; z-index: 130; width: min(300px, 86vw);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px; font-size: 14px;
}
.note-pop[hidden] { display: none; }
.note-pop .anchor-quote { color: var(--text-dim); font-size: 12.5px; border-left: 3px solid var(--accent); padding-left: 8px; margin-bottom: 8px; }
.note-pop textarea {
  width: 100%; min-height: 64px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); padding: 8px; font-family: inherit; font-size: 14px;
}
.note-pop .row { display: flex; gap: 8px; margin-top: 8px; }
.note-pop .row .btn { flex: 1; padding: 6px; font-size: 13px; }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); z-index: 200;
  background: var(--text); color: var(--bg); border-radius: 20px; padding: 8px 18px; font-size: 13.5px;
  opacity: 0; pointer-events: none; transition: opacity .25s;
}
.toast.show { opacity: .92; }

@media (max-width: 600px) {
  .shelf { padding: 12px; }
  .book-grid { grid-template-columns: 1fr; }
  .reader-main { padding: 22px 16px 80px; }
}
