:root {
  --bg: #2b2622;
  --bg-2: #3a332d;
  --ink: #f4ede4;
  --muted: #bfb2a3;
  --accent: #c9a77c;
  --panel: rgba(30, 26, 23, .97);
  --bar-h: 56px;
  --top-h: 52px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 40%, var(--bg-2), var(--bg) 70%);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button, a.tb { font: inherit; color: inherit; }

/* top bar */
.topbar {
  height: calc(var(--top-h) + env(safe-area-inset-top)); display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: env(safe-area-inset-top) 16px 0;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand-mark { letter-spacing: .28em; font-weight: 600; color: var(--accent); white-space: nowrap; }
.brand-sub { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.langs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.langs::-webkit-scrollbar { display: none; }
.langs button {
  background: transparent; border: 1px solid rgba(201, 167, 124, .35); border-radius: 999px;
  padding: 4px 10px; cursor: pointer; white-space: nowrap; font-size: 13px;
}
.langs button:hover, .langs button.on { background: var(--accent); color: #241f1b; border-color: var(--accent); }

/* stage */
.stage {
  position: relative;
  /* A lone front/back cover shifts the two-page-wide book sideways; clip the empty half. */
  overflow: hidden;
  height: calc(100% - var(--top-h) - var(--bar-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  display: flex; align-items: center; justify-content: center; padding: 8px 56px;
}
.book-wrap { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
.book { position: relative; isolation: isolate; }
/* Drop shadow sits behind the canvas; with a lone cover it only covers the page half. */
.book::before {
  content: ""; position: absolute; inset: 0; z-index: -1; pointer-events: none;
  box-shadow: 0 18px 30px rgba(0, 0, 0, .45);
}
.book.solo-right::before { left: 50%; }
.book.solo-left::before { right: 50%; }

/* Pages (page-flip HTML mode). Paper colour shows while a page image is still loading. */
.page { background: #e8e1d3; overflow: hidden; }
.page img {
  display: block; width: 100%; height: 100%; object-fit: cover;
  user-select: none; -webkit-user-drag: none; pointer-events: none;
}
/* Fold shading at the spine travels with the page while it turns. */
.page .shade { position: absolute; top: 0; bottom: 0; width: 14%; pointer-events: none; }
.pg-right .shade {
  left: 0;
  background: linear-gradient(to right, rgba(0, 0, 0, .32) 0, rgba(0, 0, 0, .16) 6%, rgba(0, 0, 0, .06) 30%, rgba(255, 255, 255, .07) 62%, rgba(255, 255, 255, 0) 100%);
}
.pg-left .shade {
  right: 0;
  background: linear-gradient(to left, rgba(0, 0, 0, .32) 0, rgba(0, 0, 0, .16) 6%, rgba(0, 0, 0, .06) 30%, rgba(255, 255, 255, .07) 62%, rgba(255, 255, 255, 0) 100%);
}
/* Outer edge of each page: a faint darkening, like light falling off paper. */
.pg-right::after, .pg-left::after { content: ""; position: absolute; top: 0; bottom: 0; width: 3%; pointer-events: none; }
.pg-right::after { right: 0; background: linear-gradient(to left, rgba(0, 0, 0, .1), rgba(0, 0, 0, 0)); }
.pg-left::after { left: 0; background: linear-gradient(to right, rgba(0, 0, 0, .1), rgba(0, 0, 0, 0)); }
.book.portrait .page .shade, .book.portrait .page::after { display: none; }
/* Hard covers: a hinge line and soft crease where the cover bends at the spine. */
.pg-cover::before { content: ""; position: absolute; top: 0; bottom: 0; width: 4%; pointer-events: none; z-index: 1; }
.pg-cover.pg-right::before {
  left: 0;
  background: linear-gradient(to right, rgba(0, 0, 0, .28) 0, rgba(0, 0, 0, .1) 18%, rgba(255, 255, 255, .12) 34%, rgba(0, 0, 0, .06) 52%, rgba(0, 0, 0, 0) 100%);
}
.pg-cover.pg-left::before {
  right: 0;
  background: linear-gradient(to left, rgba(0, 0, 0, .28) 0, rgba(0, 0, 0, .1) 18%, rgba(255, 255, 255, .12) 34%, rgba(0, 0, 0, .06) 52%, rgba(0, 0, 0, 0) 100%);
}

/* Stacked paper edges beside the book (width set from the pages on each side). */
.edge { position: absolute; z-index: 0; pointer-events: none; }
.edge-r {
  border-radius: 0 3px 3px 0;
  background: repeating-linear-gradient(to right, #f1ebdf 0 1px, #c9bfae 1px 2px);
  box-shadow: 2px 0 3px rgba(0, 0, 0, .25);
}
.edge-l {
  border-radius: 3px 0 0 3px;
  background: repeating-linear-gradient(to left, #f1ebdf 0 1px, #c9bfae 1px 2px);
  box-shadow: -2px 0 3px rgba(0, 0, 0, .25);
}

.hot-layer { position: absolute; inset: 0; z-index: 11; pointer-events: none; }
.hot-layer .hot {
  position: absolute; cursor: pointer; border-radius: 4px; z-index: 2;
  background: transparent; border: 0; padding: 0; margin: 0; pointer-events: auto;
}
.nav-arrow {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 5;
  width: 44px; height: 72px; border: 0; border-radius: 8px; cursor: pointer;
  background: rgba(0, 0, 0, .18); color: var(--ink); font-size: 40px; line-height: 1;
}
.nav-arrow:hover { background: rgba(0, 0, 0, .35); }
.nav-arrow:disabled { opacity: .15; cursor: default; }
.nav-arrow.prev { left: 6px; }
.nav-arrow.next { right: 6px; }
.loading { position: absolute; z-index: 12; color: var(--muted); background: rgba(43, 38, 34, .72); padding: 8px 14px; border-radius: 999px; pointer-events: none; }

/* toolbar */
.toolbar {
  height: calc(var(--bar-h) + env(safe-area-inset-bottom)); display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 0 12px env(safe-area-inset-bottom); background: rgba(0, 0, 0, .25);
}
.tb-group { display: flex; gap: 2px; }
.tb {
  min-width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: 0; border-radius: 8px; cursor: pointer; font-size: 18px; text-decoration: none;
}
.tb:hover { background: rgba(255, 255, 255, .1); }
.tb:focus-visible, .langs button:focus-visible, .nav-arrow:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.tb-slider { display: flex; align-items: center; gap: 10px; flex: 0 1 360px; min-width: 120px; }
.tb-slider input { flex: 1; accent-color: var(--accent); }
.counter { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; min-width: 64px; text-align: center; }

/* panels */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(380px, 100%); z-index: 20;
  background: var(--panel); display: flex; flex-direction: column; padding: 12px 16px;
  box-shadow: -10px 0 30px rgba(0, 0, 0, .4);
}
.panel[hidden] { display: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.panel-head h2 { font-size: 16px; font-weight: 600; margin: 8px 0; }
.thumb-grid { overflow-y: auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 10px; padding: 4px 2px 20px; }
.thumb-grid button { background: none; border: 2px solid transparent; border-radius: 6px; padding: 0; cursor: pointer; color: var(--muted); font-size: 12px; }
.thumb-grid button.on { border-color: var(--accent); color: var(--ink); }
.thumb-grid img { width: 100%; aspect-ratio: var(--aspect, 1); object-fit: cover; display: block; border-radius: 4px; background: #ddd; }
#searchInput {
  width: 100%; padding: 10px 12px; border-radius: 8px; border: 1px solid rgba(255, 255, 255, .2);
  background: rgba(255, 255, 255, .06); color: var(--ink); font: inherit; font-size: 16px;
}
.results { list-style: none; margin: 10px 0 0; padding: 0; overflow-y: auto; }
.results li { padding: 10px 8px; border-bottom: 1px solid rgba(255, 255, 255, .08); cursor: pointer; border-radius: 6px; }
.results li:hover { background: rgba(255, 255, 255, .06); }
.results b { color: var(--accent); }
.results mark { background: rgba(201, 167, 124, .4); color: inherit; }
.results .empty { color: var(--muted); cursor: default; }

/* zoom */
.zoom { position: fixed; inset: 0; z-index: 30; background: #0f0d0b; display: flex; flex-direction: column; }
.zoom[hidden] { display: none; }
.zoom-bar {
  position: relative; z-index: 2; display: flex; align-items: center; justify-content: center; gap: 12px;
  padding-top: env(safe-area-inset-top); height: calc(52px + env(safe-area-inset-top));
  color: var(--muted); background: rgba(15, 13, 11, .92);
}
.zoom-bar .close { position: absolute; right: 12px; }
/* Gestures are handled in JS, so the browser must not pan/zoom the page itself here. */
.zoom-stage { position: relative; flex: 1; overflow: hidden; touch-action: none; cursor: zoom-in; user-select: none; }
.zoom-stage img {
  position: absolute; left: 0; top: 0; max-width: none; transform-origin: 0 0;
  background: #e8e1d3; box-shadow: 0 10px 40px rgba(0, 0, 0, .5); -webkit-user-drag: none;
}
.zoom-stage img.zoomed { cursor: grab; }
.zoom-stage:active img.zoomed { cursor: grabbing; }
.zoom-tools {
  position: absolute; left: 50%; bottom: calc(16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 2px; padding: 4px; border-radius: 999px;
  background: rgba(30, 26, 23, .88); box-shadow: 0 4px 16px rgba(0, 0, 0, .4); z-index: 2;
}
.zoom-tools .tb { font-size: 22px; }
/* Magnifier icons (inline SVG, follow the button colour). */
.tb .ico { width: 20px; height: 20px; display: block; }
.zoom-tools .tb .ico { width: 26px; height: 26px; }
.zoom-tools .tb:disabled { opacity: .3; cursor: default; }
.zoom-pct { min-width: 52px; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; }
.zoom-hint {
  position: absolute; left: 50%; bottom: calc(72px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100% - 32px); text-align: center; padding: 6px 12px; border-radius: 8px;
  background: rgba(0, 0, 0, .6); color: var(--ink); font-size: 12px; opacity: 0; transition: opacity .3s; pointer-events: none;
}
.zoom-hint.show { opacity: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--bar-h) + 16px); transform: translateX(-50%) translateY(20px);
  background: #111; color: #fff; padding: 8px 14px; border-radius: 8px; opacity: 0; pointer-events: none; transition: .2s; z-index: 40;
}
.toast.show { opacity: 1; transform: translateX(-50%); }

@media (max-width: 720px) {
  .topbar { padding-left: 10px; padding-right: 10px; gap: 8px; }
  .brand { flex: none; }
  .brand-sub { display: none; }
  .brand-mark { letter-spacing: .16em; font-size: 13px; }
  .langs { min-width: 0; flex: 1 1 auto; }
  .langs button { padding: 4px 8px; font-size: 12px; }
  .stage { padding: 6px 8px; }
  .nav-arrow { display: none; }
  #firstBtn, #lastBtn, .tb-slider input { display: none; }
  .toolbar { justify-content: space-between; gap: 2px; padding-left: 6px; padding-right: 6px; }
  .tb-slider { flex: 0 0 auto; min-width: 0; }
  .tb { min-width: 36px; }
  .counter { min-width: 56px; }
}
/* Fallback "full screen" (iPhone / blocked in an embed): drop the top bar. */
body.immersive { --top-h: 0px; }
body.immersive .topbar { display: none; }
.tb.on { background: rgba(201, 167, 124, .3); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
@media (max-width: 380px) {
  .tb { min-width: 30px; font-size: 16px; }
  .counter { min-width: 46px; font-size: 13px; }
  .langs button { padding: 3px 7px; }
}
