:root {
  --bar: 44px;
  --bar-bg: #000;
  --icon: #ecf5fb;
  --icon-dim: #c6c6c6;
  --accent: #8e1f17;
  --pw: 400px;
  --ph: 566px;
  --flip-ms: 700ms;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: #222;
  background: #e2e2e2 url("../files/mobile-ext/background.jpg") center / cover no-repeat;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button { font: inherit; cursor: pointer; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- loading ---------- */
#loading {
  position: fixed; inset: 0; z-index: 100; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 12px;
  background: #323232; color: #ddd; transition: opacity .4s;
}
#loading svg { width: 64px; height: 64px; stroke-width: 1.2; }
#loading p { margin: 0; font-size: 18px; letter-spacing: .02em; }
#loading.hide { opacity: 0; pointer-events: none; }

/* ---------- bars ---------- */
#topbar, #toolbar {
  position: fixed; left: 0; right: 0; height: var(--bar); z-index: 20;
  background: var(--bar-bg); color: var(--icon);
  display: flex; align-items: center;
}
#topbar { top: 0; justify-content: space-between; padding: 0 10px 0 16px; }
#topbar .title { font-size: 14px; color: var(--icon-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#topbar .title b { color: var(--icon); }
#searchForm { display: flex; align-items: center; background: #2b2b2b; border-radius: 3px; width: min(260px, 55vw); }
#searchInput { flex: 1; min-width: 0; border: 0; background: transparent; color: #fff; padding: 7px 10px; outline: none; }
#searchInput::placeholder { color: #8a8a8a; }
#searchForm button { background: none; border: 0; color: var(--icon-dim); padding: 4px 8px; display: flex; }

#toolbar { bottom: 0; justify-content: space-between; padding: 0 6px; padding-bottom: env(safe-area-inset-bottom); height: calc(var(--bar) + env(safe-area-inset-bottom)); }
#toolbar .group { display: flex; align-items: center; gap: 2px; flex: 1; }
#toolbar .group.center { justify-content: center; flex: 0 0 auto; }
#toolbar .group.right { justify-content: flex-end; }
#toolbar button {
  background: none; border: 0; color: var(--icon); width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center; border-radius: 4px; opacity: .9;
}
#toolbar button:hover { background: #222; opacity: 1; }
#toolbar button.on { color: #fff; background: #333; }
#toolbar button:disabled { opacity: .3; cursor: default; background: none; }
#pageInput {
  width: 74px; height: 28px; margin: 0 6px; text-align: center; border: 0; border-radius: 2px;
  background: #fff; color: #000; font-size: 13px;
}
.ic-pause, .ic-off { display: none; }
#btnPlay.on .ic-play { display: none; }
#btnPlay.on .ic-pause { display: inline; }
#btnSound.muted .ic-on { display: none; }
#btnSound.muted .ic-off { display: inline; }

/* ---------- viewport / book ---------- */
#viewport {
  position: fixed; top: var(--bar); bottom: calc(var(--bar) + env(safe-area-inset-bottom)); left: 0; right: 0;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  user-select: none; -webkit-user-select: none;
}
#zoomwrap { transition: transform .35s ease; transform-origin: center center; }
#viewport.zoomed { cursor: grab; }
#viewport.zoomed.dragging { cursor: grabbing; }
#viewport.zoomed #zoomwrap { transition: none; }

#book {
  position: relative; width: calc(var(--pw) * 2); height: var(--ph);
  perspective: 2600px; transition: transform var(--flip-ms) ease;
}
body.single #book { width: var(--pw); }
.slot {
  position: absolute; top: 0; width: var(--pw); height: var(--ph); background: #fff;
  box-shadow: 0 6px 22px rgba(0,0,0,.35);
}
.slot.left { left: 0; }
.slot.right { left: var(--pw); }
body.single .slot.right { display: none; }
.slot.empty { visibility: hidden; }
.slot img, .face img { display: block; width: 100%; height: 100%; object-fit: fill; pointer-events: none; -webkit-user-drag: none; }
/* spine shading */
body:not(.single) .slot.left::after, body:not(.single) .slot.right::after,
.face::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 9%; pointer-events: none;
}
body:not(.single) .slot.left::after { right: 0; background: linear-gradient(to left, rgba(0,0,0,.18), transparent); }
body:not(.single) .slot.right::after { left: 0; background: linear-gradient(to right, rgba(0,0,0,.20), transparent); }

/* ---------- flipping page ---------- */
.flipper {
  position: absolute; top: 0; width: var(--pw); height: var(--ph); z-index: 5;
  transform-style: preserve-3d; transition: transform var(--flip-ms) cubic-bezier(.3,.1,.25,1);
}
.face { position: absolute; inset: 0; background: #fff; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.face.back { transform: rotateY(180deg); }
.flipper.fwd { left: var(--pw); transform-origin: left center; }
.flipper.fwd .face.front::after { left: 0; background: linear-gradient(to right, rgba(0,0,0,.2), transparent); }
.flipper.fwd .face.back::after { right: 0; background: linear-gradient(to left, rgba(0,0,0,.18), transparent); }
.flipper.fwd.go { transform: rotateY(-180deg); }
.flipper.bwd { left: 0; transform-origin: right center; }
.flipper.bwd .face.front::after { right: 0; background: linear-gradient(to left, rgba(0,0,0,.18), transparent); }
.flipper.bwd .face.back::after { left: 0; background: linear-gradient(to right, rgba(0,0,0,.2), transparent); }
.flipper.bwd.go { transform: rotateY(180deg); }
/* single-page mode: page swings out (forward) or in (backward) around the left edge */
.flipper.one { left: 0; transform-origin: left center; box-shadow: 0 6px 22px rgba(0,0,0,.35); }
.flipper.one.fwd.go { transform: rotateY(-100deg); }
.flipper.one.bwd { transform: rotateY(-100deg); }
.flipper.one.bwd.go { transform: rotateY(0deg); }

/* ---------- side arrows ---------- */
.nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 10;
  background: none; border: 0; color: rgba(255,255,255,.95); padding: 18px 8px;
  filter: drop-shadow(0 1px 3px rgba(0,0,0,.6));
}
.nav svg { width: 40px; height: 40px; stroke-width: 1.4; }
.nav.prev { left: 8px; }
.nav.next { right: 8px; }
.nav:disabled { opacity: 0; pointer-events: none; }
#viewport.zoomed .nav { display: none; }

/* ---------- panels ---------- */
.panel { position: fixed; z-index: 30; background: rgba(20,20,20,.96); color: #eee; transition: transform .3s ease, opacity .3s; }
.panel.side { top: var(--bar); bottom: calc(var(--bar) + env(safe-area-inset-bottom)); left: 0; width: min(360px, 88vw); transform: translateX(-105%); display: flex; flex-direction: column; }
.panel.side.right { left: auto; right: 0; transform: translateX(105%); }
.panel.side.open { transform: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid #333; font-size: 15px; }
.panel .close { background: none; border: 0; color: #aaa; font-size: 26px; line-height: 1; padding: 0 4px; }
#tocList, #searchList { list-style: none; margin: 0; padding: 6px 0; overflow-y: auto; }
#tocList li button, #searchList li button {
  display: flex; gap: 12px; width: 100%; text-align: left; background: none; border: 0; color: #eee; padding: 9px 16px;
}
#tocList li button:hover, #searchList li button:hover { background: #2d2d2d; }
#tocList .pg, #searchList .pg { flex: 0 0 34px; color: #e0a39c; font-variant-numeric: tabular-nums; }
#tocList .t { font-weight: 600; }
#tocList .s { display: block; color: #aaa; font-size: 13px; font-weight: 400; }
#tocList li.sub button { padding-left: 62px; color: #ccc; font-size: 13px; }
#tocList li.sub .pg { display: none; }
#tocList li.sub button::before { content: attr(data-p); color: #e0a39c; margin-left: -46px; width: 34px; flex: 0 0 34px; }
#searchList .snip { color: #bbb; font-size: 13px; }
#searchList mark { background: #ffb000; color: #000; border-radius: 2px; }
#searchList .none { padding: 14px 16px; color: #aaa; }
#textBody { padding: 12px 16px 20px; overflow-y: auto; user-select: text; -webkit-user-select: text; font-size: 14px; color: #ddd; }
#textBody h3 { color: #e0a39c; font-size: 13px; margin: 14px 0 6px; text-transform: uppercase; letter-spacing: .05em; }

#thumbs {
  left: 0; right: 0; bottom: calc(var(--bar) + env(safe-area-inset-bottom)); height: 150px;
  transform: translateY(110%); opacity: 0; pointer-events: none;
}
#thumbs.open { transform: none; opacity: 1; pointer-events: auto; }
#thumbStrip { display: flex; gap: 10px; height: 100%; overflow-x: auto; padding: 12px 14px; scroll-behavior: smooth; }
#thumbStrip button { flex: 0 0 auto; background: none; border: 2px solid transparent; padding: 0; color: #bbb; display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 11px; }
#thumbStrip img { height: 100px; width: auto; display: block; background: #444; }
#thumbStrip button.cur { border-color: #e0a39c; color: #fff; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--bar) + 24px); transform: translateX(-50%) translateY(20px);
  background: rgba(0,0,0,.85); color: #fff; padding: 9px 16px; border-radius: 4px; z-index: 50;
  opacity: 0; pointer-events: none; transition: .25s;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- small screens ---------- */
@media (max-width: 640px) {
  #topbar .title { display: none; }
  #searchForm { width: 100%; }
  #toolbar .opt { display: none; }
  #toolbar button { width: 36px; }
  .nav { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  :root { --flip-ms: 1ms; }
}
