:root {
  --bg: #24211e;
  --bar: #1b1917;
  --line: rgba(255, 255, 255, .09);
  --ink: #f2ece1;
  --muted: #a9a092;
  --accent: #c8463d;
  --accent-deep: #a72126;
  --paper: #f6efe0;
  --bar-h: 52px;
  --foot-h: 56px;
  --font: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  --serif: "Songti TC", "Noto Serif TC", "PMingLiU", serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink); font: 15px/1.5 var(--font);
  overflow: hidden; -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- bars ---------- */
.bar {
  position: fixed; left: 0; right: 0; z-index: 20; display: flex; align-items: center; gap: 6px;
  background: var(--bar); padding: 0 max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left));
}
.bar.top { top: 0; height: calc(var(--bar-h) + env(safe-area-inset-top)); padding-top: env(safe-area-inset-top); border-bottom: 1px solid var(--line); }
.bar.bottom { bottom: 0; height: calc(var(--foot-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); border-top: 1px solid var(--line); }
.title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 12px; padding: 0 6px; }
.title strong { font-family: var(--serif); font-weight: 700; font-size: 17px; letter-spacing: .04em; white-space: nowrap; }
.title span { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tools { display: flex; gap: 2px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 38px; padding: 0 11px; border: 0; border-radius: 8px;
  background: transparent; cursor: pointer; text-decoration: none; color: var(--ink); font-size: 14px; white-space: nowrap;
}
.btn:hover { background: rgba(255, 255, 255, .07); }
.btn:active { background: rgba(255, 255, 255, .12); }
.btn:focus-visible, .nav:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.sm { padding: 0 8px; }
#slider { flex: 1; min-width: 60px; accent-color: var(--accent); height: 28px; }
#pageLabel { min-width: 92px; text-align: center; font-variant-numeric: tabular-nums; font-size: 14px; color: var(--ink); }
#pageLabel small { color: var(--muted); }

/* ---------- stage ---------- */
#stage {
  position: fixed; left: 0; right: 0; top: calc(var(--bar-h) + env(safe-area-inset-top));
  bottom: calc(var(--foot-h) + env(safe-area-inset-bottom));
  display: flex; align-items: center; justify-content: center; touch-action: pan-y pinch-zoom; user-select: none;
}
#view { display: flex; justify-content: center; align-items: center; }
#view.anim-next { animation: inR .24s ease-out; }
#view.anim-prev { animation: inL .24s ease-out; }
@keyframes inR { from { transform: translateX(28px); opacity: .25; } }
@keyframes inL { from { transform: translateX(-28px); opacity: .25; } }
.pg { position: relative; background: var(--paper); overflow: hidden; box-shadow: 0 10px 34px rgba(0, 0, 0, .45); }
.spread .pg:first-child:not(:only-child) { box-shadow: -6px 10px 34px rgba(0, 0, 0, .45); }
.spread .pg:first-child:not(:only-child)::after {
  content: ""; position: absolute; inset: 0 0 0 auto; width: 7%;
  background: linear-gradient(to left, rgba(0, 0, 0, .14), transparent); pointer-events: none;
}
.spread .pg:last-child:not(:only-child)::after {
  content: ""; position: absolute; inset: 0 auto 0 0; width: 7%;
  background: linear-gradient(to right, rgba(0, 0, 0, .12), transparent); pointer-events: none;
}
.pg img { display: block; width: 100%; height: 100%; -webkit-user-drag: none; }
.lk { position: absolute; z-index: 2; border-radius: 3px; }
.lk:hover { background: rgba(167, 33, 38, .12); outline: 1.5px solid rgba(167, 33, 38, .55); }
.lk.qr { outline: 2px solid rgba(167, 33, 38, .0); animation: qrpulse 2.4s ease-in-out 2; }
@keyframes qrpulse { 50% { outline-color: rgba(167, 33, 38, .8); } }
.nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 5; width: 48px; height: 96px; border: 0; border-radius: 10px;
  background: rgba(0, 0, 0, .25); color: var(--ink); cursor: pointer; display: grid; place-items: center;
}
.nav:hover { background: rgba(0, 0, 0, .45); }
.nav svg { width: 28px; height: 28px; }
.nav.prev { left: 10px; } .nav.next { right: 10px; }
.nav:disabled { opacity: 0; pointer-events: none; }
#hint {
  position: absolute; bottom: 14px; left: 50%; transform: translateX(-50%); background: rgba(0, 0, 0, .7); color: #fff;
  padding: 7px 14px; border-radius: 20px; font-size: 13px; pointer-events: none; transition: opacity .6s; white-space: nowrap;
}
#hint.gone { opacity: 0; }

/* ---------- panels ---------- */
.scrim { position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .45); }
.panel {
  position: fixed; top: 0; bottom: 0; z-index: 40; width: min(400px, 92vw); background: #2a2622; display: flex; flex-direction: column;
  box-shadow: 0 0 40px rgba(0, 0, 0, .5); padding-top: env(safe-area-inset-top);
}
.panel.left { left: 0; animation: slideL .22s ease-out; }
.panel.right { right: 0; animation: slideR .22s ease-out; }
@keyframes slideL { from { transform: translateX(-100%); } }
@keyframes slideR { from { transform: translateX(100%); } }
.panel-head, .overlay-head {
  display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--line); flex: none;
}
.panel-head h2, .overlay-head h2 { margin: 0; flex: 1; font: 700 18px/1.3 var(--serif); letter-spacing: .06em; }
.close { margin-left: auto; }
#tocList, #results { overflow-y: auto; flex: 1; padding: 6px 0 24px; overscroll-behavior: contain; }
.toc-sec {
  display: flex; width: 100%; align-items: baseline; gap: 10px; padding: 12px 18px 6px; border: 0; background: none; cursor: pointer; text-align: left;
  font: 700 15px/1.4 var(--serif); color: var(--ink); letter-spacing: .04em;
}
.toc-sec::before { content: ""; width: 4px; height: 14px; background: var(--accent-deep); border-radius: 2px; align-self: center; flex: none; }
.toc-item {
  display: flex; width: 100%; gap: 10px; padding: 8px 18px 8px 32px; border: 0; background: none; cursor: pointer; text-align: left; color: var(--ink);
}
.toc-sec:hover, .toc-item:hover { background: rgba(255, 255, 255, .05); }
.toc-item.on, .toc-sec.on { background: rgba(200, 70, 61, .16); }
.toc-item .tt { flex: 1; min-width: 0; font-size: 14.5px; line-height: 1.45; }
.toc-item .src, .toc-item .by { display: block; color: var(--muted); font-size: 12.5px; }
.pn { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; margin-left: auto; padding-left: 8px; }
.note { margin: 0; padding: 12px 18px calc(14px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 12.5px; border-top: 1px solid var(--line); }
#q {
  flex: 1; height: 40px; border: 1px solid rgba(255, 255, 255, .16); border-radius: 9px; background: #1f1c19; padding: 0 12px; font-size: 16px;
}
.empty { color: var(--muted); padding: 10px 18px; margin: 0; }
.hit { display: block; width: 100%; text-align: left; border: 0; background: none; color: var(--ink); padding: 10px 18px; cursor: pointer; border-bottom: 1px solid var(--line); }
.hit:hover { background: rgba(255, 255, 255, .05); }
.hit b { color: #f0b9b3; font-weight: 600; font-size: 13px; }
.hit b small { color: var(--muted); font-weight: 400; margin-left: 6px; }
.hit p { margin: 3px 0 0; font-size: 14px; color: #d9d1c4; line-height: 1.55; }
.hit mark { background: rgba(200, 70, 61, .45); color: #fff; padding: 0 1px; border-radius: 2px; }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; z-index: 50; background: #1c1a17; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
#gridList {
  flex: 1; overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 18px 14px; padding: 18px 18px 40px;
}
.gcell { border: 0; background: none; padding: 0; cursor: pointer; color: var(--muted); font-size: 12.5px; text-align: center; }
.gcell img { display: block; width: 100%; aspect-ratio: var(--ratio); background: #3a3530; border-radius: 2px; margin-bottom: 6px; box-shadow: 0 4px 12px rgba(0, 0, 0, .4); }
.gcell.on img { outline: 3px solid var(--accent); outline-offset: 2px; }
.gcell.on { color: var(--ink); }
.ghead { grid-column: 1 / -1; margin: 10px 0 -6px; font: 700 15px/1.3 var(--serif); color: var(--ink); letter-spacing: .05em; }
.zoom-tools { display: flex; gap: 2px; margin-left: auto; }
#zoomLabel { font-size: 14px; color: var(--muted); }
#zoomScroll { flex: 1; overflow: auto; overscroll-behavior: contain; touch-action: pan-x pan-y pinch-zoom; cursor: grab; }
#zoomScroll.drag { cursor: grabbing; }
#zoomPage { position: relative; margin: 16px auto 40px; background: var(--paper); }
#zoomPage img { display: block; width: 100%; height: auto; -webkit-user-drag: none; user-select: none; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--foot-h) + 20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 60; background: #f2ece1; color: #24211e; padding: 9px 16px; border-radius: 22px; font-size: 14px; box-shadow: 0 6px 20px rgba(0, 0, 0, .4);
}
.nojs { position: fixed; inset: 40% 10% auto; text-align: center; }
.nojs a { color: #f0b9b3; }

/* ---------- small screens ---------- */
@media (max-width: 720px) {
  .title strong { font-size: 15px; }
  .title span { display: none; }
  .btn span { display: none; }
  .btn { padding: 0 9px; }
  #btnToc span { display: inline; }
  .nav { display: none; }
  #pageLabel { min-width: 74px; font-size: 13px; }
  #btnPrev2, #btnNext2 { display: none; }
}
@media (max-width: 380px) {
  #btnShare { display: none; }
}
@media (min-width: 721px) {
  #btnPrev2, #btnNext2 { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  #view.anim-next, #view.anim-prev, .panel { animation: none; }
}
