:root {
  --bg: #f5ecd7; --fg: #3b2f22; --muted: #8f7f66; --panel: #fbf4e4; --accent: #9a5b2e;
  --line: rgba(0,0,0,.09);
  --fs: 20px; --lh: 1.9; --ls: .04em; --ps: .6em; --indent: 2em; --mx: 24px; --maxw: 40em;
  --font: "Noto Serif TC","Source Han Serif TC","Songti TC","PMingLiU","MingLiU",serif;
  --ui: system-ui,-apple-system,"PingFang TC","Noto Sans TC","Microsoft JhengHei",sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--fg); font-family: var(--ui);
  -webkit-tap-highlight-color: transparent; overflow: hidden;
  transition: background-color .2s, color .2s;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }

.view { position: fixed; inset: 0; display: none; flex-direction: column; }
.view.on { display: flex; }

/* ---------- 書櫃 ---------- */
.lib-head {
  width: 100%; max-width: 720px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: calc(env(safe-area-inset-top) + 24px) 16px 16px;
}
.lib-head h1 { font-family: var(--font); font-size: 28px; font-weight: 600; letter-spacing: .12em; }
.lib-actions { display: flex; gap: 8px; }
.lib-list { flex: 1; overflow: auto; padding: 0 16px calc(env(safe-area-inset-bottom) + 32px); }
.lib-inner { max-width: 720px; margin: 0 auto; display: grid; gap: 10px; }
.card {
  display: flex; gap: 14px; align-items: center; padding: 14px; cursor: pointer;
  border-radius: 14px; background: var(--panel); border: 1px solid var(--line);
}
.card:active { transform: scale(.99); }
.cover {
  width: 46px; height: 62px; flex: none; border-radius: 3px 6px 6px 3px;
  background: var(--accent); color: var(--panel);
  display: grid; place-items: center; font-family: var(--font); font-size: 20px;
  box-shadow: inset 4px 0 0 rgba(0,0,0,.15);
}
.meta { flex: 1; min-width: 0; }
.meta b { display: block; font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meta small { display: block; color: var(--muted); font-size: 13px; margin-top: 2px; }
.prog { height: 3px; background: var(--line); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.prog i { display: block; height: 100%; background: var(--accent); }
.del { font-size: 13px; color: var(--muted); padding: 8px; border-radius: 8px; flex: none; }
.del.armed { color: #c0392b; font-weight: 600; }
.empty { text-align: center; color: var(--muted); padding: 60px 20px; line-height: 2; }

.btn {
  padding: 8px 16px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--panel); font-size: 14px;
}
.btn.primary { background: var(--accent); color: var(--panel); border-color: var(--accent); }

/* ---------- 閱讀 ---------- */
#reader { background: var(--bg); }
#page {
  flex: 1; overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: calc(env(safe-area-inset-top) + 28px) var(--mx) 20px;
  font-family: var(--font); font-size: var(--fs); line-height: var(--lh); letter-spacing: var(--ls);
  font-kerning: none;
}
#text { max-inline-size: var(--maxw); margin-inline: auto; text-align: justify; line-break: strict; }
#text p { margin-block-end: var(--ps); text-indent: var(--indent); }
#text p.cont { text-indent: 0; }
#text h2 {
  font-size: 1.3em; font-weight: 600; letter-spacing: .08em; line-height: 1.5;
  margin-block: .4em 1.4em; text-indent: 0;
}
#reader.vertical #page { writing-mode: vertical-rl; padding: calc(env(safe-area-inset-top) + 24px) var(--mx) 24px; }
#reader.vertical #text { text-align: start; }

.foot {
  display: flex; justify-content: space-between; gap: 16px;
  padding: 6px max(20px, var(--mx)) calc(env(safe-area-inset-bottom) + 8px);
  font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.foot span:first-child { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.foot span:last-child { flex: none; }

.bar {
  position: absolute; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  background: var(--panel); transition: transform .22s ease;
}
.bar.top { top: 0; padding-top: calc(env(safe-area-inset-top) + 8px); border-bottom: 1px solid var(--line); transform: translateY(-105%); }
.bar.bottom {
  bottom: 0; padding-bottom: calc(env(safe-area-inset-bottom) + 8px); border-top: 1px solid var(--line);
  transform: translateY(105%); flex-direction: column; align-items: stretch; gap: 4px;
}
#reader.ui .bar { transform: none; }
.bar .title { flex: 1; text-align: center; font-weight: 600; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.row { display: flex; align-items: center; gap: 6px; }
.row.between { justify-content: space-between; padding-left: 10px; }
.slider-label { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.icon { min-width: 44px; height: 40px; padding: 0 10px; border-radius: 10px; font-size: 15px; white-space: nowrap; }
.icon:active { background: var(--line); }
.icon.strong { color: var(--accent); font-weight: 600; }
input[type=range] { flex: 1; min-width: 0; accent-color: var(--accent); height: 28px; }

/* ---------- 底部面板 ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 10; background: rgba(0,0,0,.28);
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
.backdrop.on { opacity: 1; pointer-events: auto; }
.backdrop.light { background: rgba(0,0,0,.04); }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11;
  max-width: 560px; margin: 0 auto; max-height: 78dvh; overflow: auto;
  background: var(--panel); color: var(--fg);
  border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.14);
  padding: 6px 20px calc(env(safe-area-inset-bottom) + 20px);
  transform: translateY(103%); transition: transform .26s cubic-bezier(.2,.8,.2,1);
  visibility: hidden;
}
.sheet.on { transform: none; visibility: visible; }
.grip { width: 36px; height: 4px; border-radius: 2px; background: var(--line); margin: 6px auto 12px; }
.sheet-title { font-size: 16px; margin-bottom: 8px; }
.sheet-foot { display: flex; justify-content: flex-end; gap: 8px; padding-top: 16px; }

.set {
  display: grid; grid-template-columns: 4.6em 1fr 3.2em; align-items: center; gap: 10px;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.set > label { font-size: 14px; color: var(--muted); }
.set output { font-size: 13px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.seg { grid-column: 2 / 4; display: flex; gap: 6px; }
.seg button { flex: 1; padding: 8px 4px; border-radius: 10px; border: 1px solid var(--line); font-size: 14px; }
.seg button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.swatches { grid-column: 2 / 4; display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.sw { height: 42px; border-radius: 10px; font-size: 12px; border: 1px solid rgba(128,128,128,.25); outline: 2px solid transparent; outline-offset: 2px; }
.sw.on { outline-color: var(--accent); }

#tocList { display: flex; flex-direction: column; }
#tocList button {
  text-align: left; padding: 12px 4px; border-bottom: 1px solid var(--line);
  font-family: var(--font); font-size: 16px; display: flex; justify-content: space-between; gap: 12px;
}
#tocList button small { color: var(--muted); font-family: var(--ui); font-size: 12px; flex: none; }
#tocList button.on { color: var(--accent); font-weight: 600; }
.toc-empty { color: var(--muted); padding: 20px 0; line-height: 1.8; }

.field {
  display: block; width: 100%; margin-top: 10px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); font-size: 15px; line-height: 1.6; resize: vertical;
}
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; color: var(--muted); margin-top: 10px; line-height: 1.5; }
.check input { margin-top: 3px; accent-color: var(--accent); }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 70px); z-index: 20;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--fg); color: var(--bg); padding: 8px 16px; border-radius: 999px; font-size: 14px;
  transition: opacity .2s, transform .2s; white-space: nowrap;
}
.toast.on { opacity: .92; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
