* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(circle at 50% 40%, #4a4f57 0%, #22252a 70%);
  font-family: system-ui, -apple-system, sans-serif;
  color: #eee;
  overflow: hidden;
}

#stage {
  position: absolute;
  inset: 0 0 64px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

#book { visibility: hidden; }
#book.ready { visibility: visible; }

.page {
  background: #fff;
  overflow: hidden;
}

.page img,
.page canvas {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
}

/* ---------- Hardcover book ---------- */

#book-wrap { position: relative; }
#book { position: relative; z-index: 2; }

/* Cloth-covered boards, visible around the page block */
.case {
  position: absolute;
  z-index: 0;
  border-radius: 3px 5px 5px 3px;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,.025) 0 2px, rgba(0,0,0,.03) 2px 4px),
    linear-gradient(180deg, #3a3d44, #24262b);
  box-shadow:
    0 30px 60px -10px rgba(0,0,0,.65),
    0 10px 20px rgba(0,0,0,.35),
    inset 0 0 0 1px rgba(255,255,255,.06),
    inset 0 2px 3px rgba(255,255,255,.08);
  transition: left .7s ease, width .7s ease, top .7s ease, height .7s ease;
}
/* Rounded spine bump down the middle of the open book */
#book-wrap[data-state="open"][data-spread="true"] .case::after {
  content: "";
  position: absolute;
  left: 50%; top: -3px; bottom: -3px;
  width: 22px;
  transform: translateX(-50%);
  border-radius: 6px;
  background: linear-gradient(90deg, #1d1f23, #3c3f46 50%, #1d1f23);
  z-index: -1;
}

/* Edges of the page block, getting thinner/thicker as you read */
.stack {
  position: absolute;
  z-index: 1;
  top: 3px;
  bottom: 3px;
  background:
    repeating-linear-gradient(90deg, #f7f5ef 0 2px, #c9c4b9 2px 3px);
  transition: left .7s ease, width .7s ease;
}
.stack-l { border-radius: 2px 0 0 2px; box-shadow: inset 3px 0 4px -2px rgba(0,0,0,.35); }
.stack-r { border-radius: 0 2px 2px 0; box-shadow: inset -3px 0 4px -2px rgba(0,0,0,.35); }
#book-wrap.no-anim .case,
#book-wrap.no-anim .stack { transition: none; }

/* Gutter: a thin line where the pages meet */
.page::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}
/* thin crisp gutter line: drawn by the right-hand page only, so it stays 1px */
.page.--right::after {
  box-shadow: inset 1px 0 0 rgba(0,0,0,.22);
}

/* Outer covers: glossy board with a hinge groove near the spine */
.page.cover-front::after,
.page.cover-back::after {
  background:
    linear-gradient(115deg, rgba(255,255,255,.18) 0%, rgba(255,255,255,0) 35%, rgba(255,255,255,0) 70%, rgba(255,255,255,.07) 100%),
    linear-gradient(90deg, rgba(0,0,0,.18) 0, rgba(0,0,0,.06) 2.2%, rgba(255,255,255,.18) 2.8%, rgba(0,0,0,.08) 3.6%, rgba(0,0,0,0) 5%);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.25);
}
.page.cover-back::after {
  background:
    linear-gradient(245deg, rgba(255,255,255,.12) 0%, rgba(255,255,255,0) 35%),
    linear-gradient(270deg, rgba(0,0,0,.18) 0, rgba(0,0,0,.06) 2.2%, rgba(255,255,255,.18) 2.8%, rgba(0,0,0,.08) 3.6%, rgba(0,0,0,0) 5%);
}

#loader {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
#loader.hidden { display: none; }

.spinner {
  width: 40px; height: 40px;
  border: 4px solid rgba(255,255,255,.2);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#toolbar {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 10px;
  background: rgba(0,0,0,.55);
  backdrop-filter: blur(6px);
  border-radius: 999px;
}

#toolbar button,
#toolbar a {
  all: unset;
  cursor: pointer;
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border-radius: 50%;
  font-size: 15px;
  color: #eee;
}
#toolbar button:hover,
#toolbar a:hover { background: rgba(255,255,255,.15); }
#toolbar button:disabled { opacity: .3; cursor: default; background: none; }

#counter {
  min-width: 80px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}

#toolbar[hidden] { display: none; }
#loader p { text-align: center; max-width: 90vw; }

/* ---------- Zoom ---------- */
#stage { touch-action: none; }            /* pinch/pan are handled by app.js */
#book-wrap { transform-origin: 0 0; will-change: transform; }
#book-wrap.zoom-anim { transition: transform .25s ease; }
#stage.zoomed { cursor: grab; }
#stage.zoomed.panning { cursor: grabbing; }
#toolbar #zoom-out, #toolbar #zoom-in { font-size: 20px; font-weight: 600; }

/* Narrow phones: compact toolbar so all buttons fit */
@media (max-width: 480px) {
  #toolbar { gap: 0; padding: 4px 6px; max-width: calc(100vw - 16px); }
  #toolbar button, #toolbar a { width: 32px; height: 32px; font-size: 14px; }
  #toolbar #zoom-out, #toolbar #zoom-in { font-size: 18px; }
  #counter { min-width: 56px; font-size: 13px; }
}

/* ---------- Rotate-your-phone prompt ---------- */
#rotate {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px;
  text-align: center;
  background: #1a1c20;
  color: #eee;
}
#rotate[hidden] { display: none; }
#rotate p { margin: 0; font-size: 17px; line-height: 1.45; }
#rotate .phone { animation: rotate-hint 2.4s ease-in-out infinite; }
@keyframes rotate-hint {
  0%, 25%   { transform: rotate(0deg); }
  50%, 80%  { transform: rotate(-90deg); }
  100%      { transform: rotate(0deg); }
}
#rotate-skip {
  all: unset;
  cursor: pointer;
  margin-top: 8px;
  padding: 8px 14px;
  font-size: 14px;
  color: #aaa;
  text-decoration: underline;
}

/* Short screens (phone in landscape): small toolbar, more room for the book */
@media (max-height: 500px) {
  #stage { inset: 0 0 44px 0; padding: 6px; }
  #toolbar { bottom: 4px; gap: 0; padding: 2px 6px; }
  #toolbar button, #toolbar a { width: 32px; height: 32px; font-size: 14px; }
  #toolbar #zoom-out, #toolbar #zoom-in { font-size: 18px; }
  #counter { min-width: 64px; font-size: 13px; }
}
