:root {
  color-scheme: dark;
  --gold: #d8ae58;
  --gold-bright: #f1d38d;
  --ink: #0b0b0d;
  --panel: #161619;
  --line: rgba(255,255,255,.14);
  --muted: #aaa9ad;
  --page-ratio: 507.75 / 684;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 45%, rgba(216,174,88,.10), transparent 42%),
    linear-gradient(145deg, #121216 0%, #080809 72%);
  color: #f7f5f0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, a, input { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.topbar {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 24px;
  padding: 10px clamp(14px, 2vw, 28px);
  position: fixed;
  z-index: 30;
  inset: 0 0 auto;
  pointer-events: none;
}

.title-group { display: none; }
.eyebrow {
  display: block;
  margin-bottom: 2px;
  color: var(--gold-bright);
  font-size: .72rem;
  font-weight: 750;
  letter-spacing: .14em;
  text-transform: uppercase;
}
h1 {
  margin: 0;
  overflow: hidden;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1rem, 2vw, 1.35rem);
  font-weight: 500;
  line-height: 1.15;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions { display: flex; gap: 8px; pointer-events: auto; }
.icon-button, .control-button, .pages-button {
  border: 1px solid var(--line);
  color: #f7f5f0;
  background: rgba(255,255,255,.055);
  cursor: pointer;
  transition: background .2s ease, border-color .2s ease, color .2s ease, transform .2s ease;
}
.icon-button:hover, .control-button:hover, .pages-button:hover {
  background: rgba(216,174,88,.12);
  border-color: rgba(216,174,88,.6);
  color: var(--gold-bright);
}
.icon-button:focus-visible, .control-button:focus-visible, .pages-button:focus-visible, .edge-button:focus-visible,
.tray-header button:focus-visible, .thumbnail:focus-visible {
  outline: 2px solid var(--gold-bright);
  outline-offset: 3px;
}
.icon-button {
  min-width: 44px;
  height: 44px;
  border-radius: 99px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 13px;
  text-decoration: none;
}
.icon-button svg, .control-button svg, .pages-button svg, .edge-button svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.download-link span { font-size: .78rem; font-weight: 750; letter-spacing: .04em; }

.stage {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 8px;
  padding: max(10px, env(safe-area-inset-top)) clamp(10px, 1.5vw, 22px) max(10px, env(safe-area-inset-bottom));
  position: relative;
}

.book-shell {
  min-height: 0;
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  align-items: center;
  justify-items: center;
  gap: clamp(8px, 1.5vw, 20px);
}

.book {
  --page-h: min(calc(100dvh - 36px), calc((100vw - 146px) / 2 / (507.75 / 684)));
  width: calc(var(--page-h) * (507.75 / 684) * 2);
  height: var(--page-h);
  max-width: 100%;
  position: relative;
  perspective: 2600px;
  filter: drop-shadow(0 24px 28px rgba(0,0,0,.56));
  transform: translateZ(0);
  contain: layout paint;
}

.book::after {
  content: "";
  position: absolute;
  z-index: 12;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 2px;
  transform: translateX(-1px);
  background: linear-gradient(90deg, rgba(0,0,0,.45), rgba(255,255,255,.18), rgba(0,0,0,.4));
  box-shadow: 0 0 14px rgba(0,0,0,.6);
  pointer-events: none;
}

.page-slot, .flip-sheet {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  background: #fff;
  overflow: hidden;
}
.page-slot img, .sheet-face img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  user-select: none;
  -webkit-user-drag: none;
}
.page-slot.empty { opacity: 0; }
.left-slot { left: 0; border-radius: 3px 0 0 3px; }
.right-slot { right: 0; border-radius: 0 3px 3px 0; }

.flip-sheet {
  z-index: 10;
  transform-style: preserve-3d;
  overflow: visible;
  pointer-events: none;
  visibility: hidden;
  will-change: transform, filter;
}
.flip-sheet.from-right { left: 50%; transform-origin: left center; }
.flip-sheet.from-left { left: 0; transform-origin: right center; }
.sheet-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #fff;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.sheet-front::after, .sheet-back::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.sheet-front::after { background: linear-gradient(90deg, transparent 55%, rgba(0,0,0,.23)); }
.sheet-back { transform: rotateY(180deg); }
.sheet-back::after { background: linear-gradient(90deg, rgba(0,0,0,.25), transparent 48%); }
.flip-sheet.turn-next {
  visibility: visible;
  animation: turnNext .58s cubic-bezier(.46,.03,.32,1) forwards;
}
.flip-sheet.turn-prev {
  visibility: visible;
  animation: turnPrev .58s cubic-bezier(.46,.03,.32,1) forwards;
}
@keyframes turnNext {
  0% { transform: rotateY(0); filter: brightness(1); }
  48% { filter: brightness(.68); }
  100% { transform: rotateY(-180deg); filter: brightness(1); }
}
@keyframes turnPrev {
  0% { transform: rotateY(0); filter: brightness(1); }
  48% { filter: brightness(.68); }
  100% { transform: rotateY(180deg); filter: brightness(1); }
}

.edge-button {
  width: 48px;
  height: 64px;
  border: 1px solid var(--line);
  border-radius: 99px;
  display: grid;
  place-items: center;
  background: rgba(255,255,255,.04);
  color: #e7e4dc;
  cursor: pointer;
  transition: border-color .2s, background .2s, color .2s, opacity .2s;
}
.edge-button:hover:not(:disabled) { border-color: var(--gold); background: rgba(216,174,88,.12); color: var(--gold-bright); }
.edge-button:disabled, .control-button:disabled { opacity: .25; cursor: default; }

.controls {
  min-height: 50px;
  width: min(720px, 100%);
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(18,18,21,.88);
  box-shadow: 0 8px 30px rgba(0,0,0,.28);
  backdrop-filter: blur(14px);
  position: absolute;
  z-index: 24;
  left: 50%;
  bottom: max(10px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
}
.control-button {
  flex: 0 0 38px;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
}
.pages-button {
  height: 38px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 0 13px;
  border-radius: 99px;
  font-size: .8rem;
  font-weight: 700;
}
.pages-button svg { width: 17px; }
.page-status {
  flex: 0 0 116px;
  text-align: center;
  color: #eeece6;
  font-size: .82rem;
  font-variant-numeric: tabular-nums;
}
.progress-label { flex: 1; display: flex; }
input[type="range"] {
  width: 100%;
  height: 24px;
  appearance: none;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track { height: 3px; border-radius: 4px; background: linear-gradient(90deg, var(--gold) var(--progress, 0%), rgba(255,255,255,.2) var(--progress, 0%)); }
input[type="range"]::-moz-range-track { height: 3px; border-radius: 4px; background: rgba(255,255,255,.2); }
input[type="range"]::-moz-range-progress { height: 3px; border-radius: 4px; background: var(--gold); }
input[type="range"]::-webkit-slider-thumb { appearance: none; width: 15px; height: 15px; margin-top: -6px; border: 2px solid #171719; border-radius: 50%; background: var(--gold-bright); box-shadow: 0 0 0 1px var(--gold); }
input[type="range"]::-moz-range-thumb { width: 13px; height: 13px; border: 2px solid #171719; border-radius: 50%; background: var(--gold-bright); }

.loading {
  position: absolute;
  z-index: 30;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: #0b0b0d;
  color: #d8d5cc;
  transition: opacity .35s, visibility .35s;
}
.loading.hidden { opacity: 0; visibility: hidden; }
.loading span { width: 18px; height: 18px; border: 2px solid rgba(216,174,88,.22); border-top-color: var(--gold); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.thumbnail-tray {
  position: fixed;
  z-index: 50;
  top: 0;
  right: 0;
  width: min(420px, 92vw);
  height: 100%;
  padding: 20px;
  background: #141416;
  border-left: 1px solid var(--line);
  box-shadow: -20px 0 60px rgba(0,0,0,.46);
  transform: translateX(102%);
  transition: transform .28s ease;
}
.thumbnail-tray.open { transform: translateX(0); }
.tray-header { height: 48px; display: flex; align-items: center; justify-content: space-between; }
.tray-header strong { font-family: Georgia, "Times New Roman", serif; font-size: 1.15rem; font-weight: 500; }
.tray-header button { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; color: #eee; font-size: 1.75rem; cursor: pointer; }
.thumbnails {
  height: calc(100% - 48px);
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-content: start;
  gap: 18px 12px;
  padding: 12px 5px 30px;
  scrollbar-color: #5b513d transparent;
}
.thumbnail { border: 0; padding: 0; background: transparent; color: #bfbcc1; cursor: pointer; text-align: center; }
.thumbnail img { width: 100%; aspect-ratio: var(--page-ratio); object-fit: cover; display: block; border: 2px solid transparent; box-shadow: 0 5px 12px rgba(0,0,0,.36); transition: border-color .2s, transform .2s; }
.thumbnail:hover img { transform: translateY(-2px); border-color: rgba(216,174,88,.55); }
.thumbnail.current img { border-color: var(--gold-bright); }
.thumbnail span { display: block; margin-top: 6px; font-size: .72rem; }
.tray-backdrop { position: fixed; z-index: 45; inset: 0; background: rgba(0,0,0,.62); opacity: 0; visibility: hidden; transition: opacity .28s, visibility .28s; }
.tray-backdrop.open { opacity: 1; visibility: visible; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

@media (max-width: 720px) {
  .topbar { height: 56px; padding: 7px 10px; }
  .eyebrow { font-size: .62rem; }
  h1 { font-size: .98rem; max-width: 58vw; }
  .title-group { display: none; }
  .top-actions { margin-left: auto; }
  .download-link span { display: inline; }
  .icon-button { width: 42px; min-width: 42px; height: 42px; padding: 0; }
  .stage { height: 100vh; height: 100dvh; padding: 8px 6px max(8px, env(safe-area-inset-bottom)); gap: 8px; }
  .book-shell { display: block; position: relative; }
  .book {
    --page-h: min(calc(100dvh - 20px), calc((100vw - 12px) / (507.75 / 684)));
    width: calc(var(--page-h) * (507.75 / 684));
    height: var(--page-h);
    margin: 0 auto;
  }
  .book::after, .left-slot { display: none; }
  .page-slot, .flip-sheet { width: 100%; }
  .right-slot, .flip-sheet.from-right, .flip-sheet.from-left { left: 0; right: auto; border-radius: 3px; }
  .flip-sheet.from-right { transform-origin: left center; }
  .flip-sheet.from-left { transform-origin: right center; }
  .edge-button { position: absolute; z-index: 18; top: 50%; width: 42px; height: 52px; transform: translateY(-50%); background: rgba(9,9,11,.72); backdrop-filter: blur(8px); }
  .edge-prev { left: 4px; }
  .edge-next { right: 4px; }
  .controls { width: calc(100% - 16px); gap: 6px; min-height: 48px; padding: 5px; bottom: max(8px, env(safe-area-inset-bottom)); }
  .pages-button { width: 38px; padding: 0; justify-content: center; }
  .pages-button span { display: none; }
  .pages-button { font-size: 0; gap: 0; }
  .page-status { flex-basis: 94px; font-size: .76rem; }
  .progress-label { min-width: 52px; }
  .thumbnails { grid-template-columns: repeat(3, 1fr); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
