:root {
  --bg: #111720;
  --panel: #1b2430;
  --ink: #f5f7f8;
  --muted: #9eabb8;
  --accent: #dfff1f;
  --page-ratio: 0.7926829268;
  --shadow: 0 24px 70px rgba(0,0,0,.42);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  background: radial-gradient(circle at 50% 12%, #283441 0, var(--bg) 55%, #090d12 100%);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, a { font: inherit; }
button { color: inherit; }
.app-shell { height: 100dvh; display: grid; grid-template-rows: auto minmax(0,1fr) auto; }
.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 18px;
  background: rgba(13,18,25,.88);
  border-bottom: 1px solid rgba(255,255,255,.08);
  backdrop-filter: blur(16px);
  z-index: 20;
}
.brand { display: flex; align-items: center; min-width: 0; gap: 13px; }
.brand img { width: 108px; max-height: 43px; object-fit: contain; }
.brand-copy { display: grid; min-width: 0; }
.brand-copy strong { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-copy span { color: var(--muted); font-size: 12px; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.icon-button, .text-button, .toolbar button, .drawer-head button {
  border: 1px solid rgba(255,255,255,.13);
  background: rgba(255,255,255,.06);
  color: var(--ink);
  min-width: 38px;
  height: 38px;
  border-radius: 10px;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  text-decoration: none;
  transition: background .2s ease, transform .2s ease, border-color .2s ease;
}
.icon-button:hover, .text-button:hover, .toolbar button:hover, .drawer-head button:hover {
  background: rgba(255,255,255,.12);
  border-color: rgba(255,255,255,.22);
}
.icon-button:active, .toolbar button:active { transform: scale(.96); }
.text-button { padding: 0 13px; font-size: 13px; font-weight: 700; }
.viewer {
  position: relative;
  min-height: 0;
  display: grid;
  grid-template-columns: 56px minmax(0,1fr) 56px;
  align-items: center;
  padding: 16px 10px 10px;
}
.book-stage { position: relative; width: 100%; height: 100%; min-height: 0; display: grid; place-items: center; perspective: 2400px; }
.book {
  --book-h: min(calc(100dvh - 162px), calc((100vw - 150px) / (2 * var(--page-ratio))));
  width: calc(var(--book-h) * var(--page-ratio) * 2);
  height: var(--book-h);
  position: relative;
  /* Avoid filter here: filters flatten nested 3D page faces. */
  transform-style: preserve-3d;
}
.book::after {
  content: "";
  position: absolute;
  inset: 0 auto 0 50%;
  width: 1px;
  background: linear-gradient(transparent, rgba(0,0,0,.35) 8%, rgba(255,255,255,.15) 50%, rgba(0,0,0,.35) 92%, transparent);
  z-index: 7;
}
.page-slot, .flip-sheet {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  background: #f7f7f4;
  overflow: hidden;
}
/* Let the empty left-hand page show the viewer's gradient background. */
.page-slot.left {
  left: 0;
  border-radius: 8px 1px 1px 8px;
  background: transparent;
}
.page-slot.right {
  right: 0;
  border-radius: 1px 8px 8px 1px;
  box-shadow: 0 25px 34px rgba(0,0,0,.38);
}
/* Only give the left side a shadow when it contains an actual page image. */
.page-slot.left:has(img) { box-shadow: 0 25px 34px rgba(0,0,0,.38); }
.page-slot img, .mobile-page img, .flip-face img { width: 100%; height: 100%; object-fit: contain; display: block; background: #f7f7f4; }
.page-slot.left img { box-shadow: inset -16px 0 22px -25px rgba(0,0,0,.6); }
.page-slot.right img { box-shadow: inset 16px 0 22px -25px rgba(0,0,0,.6); }
.blank-page { width: 100%; height: 100%; background: transparent; }
.flip-sheet {
  left: 50%;
  z-index: 12;
  transform-origin: left center;
  transform-style: preserve-3d;
  border-radius: 0 8px 8px 0;
  will-change: transform;
  /* Do not clip or paint over the reverse face during a 3D turn. */
  overflow: visible;
  background: transparent;
}
.flip-sheet.backward { transform-origin: left center; }
/* Keep each side of the turning page from showing through the other. */
.flip-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #f7f7f4;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform-style: preserve-3d;
}
.flip-face.front { transform: rotateY(0deg) translateZ(1px); }
.flip-face.back { transform: rotateY(180deg) translateZ(1px); }
/* Use the 3D face orientation instead of separately animating visibility.
   Independent visibility animations can expose a blank face mid-turn. */
.flip-sheet.turn-next { animation: turnNext .74s cubic-bezier(.4,.02,.2,1) forwards; }
.flip-sheet.turn-prev { animation: turnPrev .74s cubic-bezier(.4,.02,.2,1) forwards; }
@keyframes turnNext { from { transform: rotateY(0deg); } to { transform: rotateY(-180deg); } }
@keyframes turnPrev { from { transform: rotateY(-180deg); } to { transform: rotateY(0deg); } }
.mobile-page { display: none; position: relative; height: 100%; max-width: 100%; aspect-ratio: var(--page-ratio); filter: drop-shadow(var(--shadow)); transform-style: preserve-3d; }
.mobile-page.zoomed { height: auto; width: min(96vw, 900px); max-width: none; align-self: start; }
.mobile-page.mobile-next { animation: mobileNext .38s ease; }
.mobile-page.mobile-prev { animation: mobilePrev .38s ease; }
@keyframes mobileNext { from { opacity:.45; transform: translateX(24px) rotateY(-8deg); } to { opacity:1; transform:none; } }
@keyframes mobilePrev { from { opacity:.45; transform: translateX(-24px) rotateY(8deg); } to { opacity:1; transform:none; } }
.nav-button {
  width: 46px; height: 80px; border: 0; border-radius: 14px;
  background: rgba(255,255,255,.07); color: white; font-size: 45px; font-weight: 200;
  cursor: pointer; transition: background .2s, opacity .2s, transform .2s; z-index: 4;
}
.nav-button:hover { background: rgba(255,255,255,.13); transform: scale(1.03); }
.nav-button:disabled { opacity: .18; cursor: default; transform: none; }
.toolbar {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 14px max(8px, env(safe-area-inset-bottom));
  background: rgba(13,18,25,.92);
  border-top: 1px solid rgba(255,255,255,.08);
  backdrop-filter: blur(16px);
  z-index: 20;
}
.toolbar button { min-width: 38px; height: 36px; border-radius: 9px; }
.toolbar #zoomButton { min-width: 58px; font-size: 12px; font-weight: 700; }
.page-status { width: min(340px, 38vw); display: grid; gap: 7px; text-align: center; font-size: 12px; color: var(--muted); }
.progress-track { height: 3px; overflow: hidden; border-radius: 999px; background: rgba(255,255,255,.12); }
#progressBar { height: 100%; width: 0; background: var(--accent); transition: width .25s ease; }
.drawer {
  position: fixed; z-index: 50; top: 0; bottom: 0; left: 0; width: min(390px, 88vw);
  background: #161e28; transform: translateX(-102%); transition: transform .28s ease;
  box-shadow: 24px 0 60px rgba(0,0,0,.45); display: grid; grid-template-rows: auto 1fr;
}
.drawer.open { transform: translateX(0); }
.drawer-head { height: 64px; padding: 10px 16px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid rgba(255,255,255,.09); }
.drawer-head button { font-size: 24px; border: 0; background: transparent; }
.thumb-grid { overflow: auto; padding: 14px; display: grid; grid-template-columns: repeat(2,1fr); gap: 14px; align-content: start; }
.thumb { border: 1px solid transparent; border-radius: 8px; background: rgba(255,255,255,.04); padding: 7px; cursor: pointer; color: var(--muted); text-align: center; font-size: 11px; }
.thumb img { width: 100%; display: block; border-radius: 4px; margin-bottom: 6px; background: white; }
.thumb:hover, .thumb.active { border-color: var(--accent); color: white; }
.scrim { position: fixed; z-index: 45; inset: 0; background: rgba(0,0,0,.52); opacity: 0; pointer-events: none; transition: opacity .28s ease; }
.scrim.open { opacity: 1; pointer-events: auto; }
.noscript { position: fixed; inset: 30% 15%; background: white; color: black; padding: 24px; z-index: 100; }
.viewer.zoom-mode { overflow: auto; align-items: start; }
.viewer.zoom-mode .book { --book-h: min(1100px, 94vw); margin-top: 18px; }
@media (max-width: 780px), (max-height: 650px) and (orientation: portrait) {
  .topbar { height: 56px; padding: 6px 10px; }
  .brand img { width: 84px; max-height: 38px; }
  .brand-copy strong { font-size: 12px; }
  .brand-copy span { display: none; }
  .text-button { display: none; }
  .viewer { grid-template-columns: 35px minmax(0,1fr) 35px; padding: 8px 2px; overflow: auto; }
  .book { display: none; }
  .mobile-page { display: block; height: min(calc(100dvh - 140px), calc((100vw - 76px) / var(--page-ratio))); }
  .nav-button { width: 32px; height: 64px; font-size: 36px; background: transparent; }
  .toolbar { min-height: 54px; gap: 5px; padding-left: 7px; padding-right: 7px; }
  .toolbar button { min-width: 34px; }
  .page-status { width: min(210px, 42vw); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
