:root {
  --paper: #fbf8f0;
  --ink: #2f2a25;
  --blue: #2f4f8f;
  --gold: #b5652a;
  --red: #d2452f;
  --muted: #a08a70;
  --page-w: 480px;
  --page-h: 680px;
  --flip: 0.85s;
  --controls: 72px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(ellipse at 50% 40%, #f3e6cf 0%, #e3cfae 55%, #c9ad85 100%);
  color: var(--ink);
  font-family: "Andika", "Trebuchet MS", sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- stage & scaling ---------- */
.stage {
  position: fixed;
  inset: 0 0 var(--controls) 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
.scaler { transform-origin: center center; }
.book {
  position: relative;
  width: calc(var(--page-w) * 2);
  height: var(--page-h);
  perspective: 2600px;
}
.book.single { width: var(--page-w); }
.slide {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform var(--flip) cubic-bezier(.45,.05,.35,1);
}

/* ---------- leaves ---------- */
.leaf {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform var(--flip) cubic-bezier(.45,.05,.35,1);
  cursor: pointer;
}
.book.single .leaf { left: 0; width: 100%; }
.leaf.flipped { transform: rotateY(-180deg); }
.book.single .leaf.gone { visibility: hidden; }
.instant .leaf, .instant .slide { transition: none !important; }

.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  box-shadow: 0 3px 8px rgba(60, 40, 20, .045);
}
.face.back { transform: rotateY(180deg); }

/* spine shading */
.face::after {
  content: "";
  position: absolute;
  top: 0; bottom: 0;
  width: 46px;
  pointer-events: none;
}
.face.front::after { left: 0; background: linear-gradient(to right, rgba(80,55,30,.18), rgba(80,55,30,0)); }
.face.back::after  { right: 0; background: linear-gradient(to left,  rgba(80,55,30,.18), rgba(80,55,30,0)); }
.book.single .face.front::after { width: 18px; }
.leaf.turning .face.front::before {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(to left, rgba(0,0,0,.12), rgba(0,0,0,0) 60%);
  z-index: 2;
  pointer-events: none;
}

/* ---------- pages ---------- */
.page {
  position: relative;
  width: var(--page-w);
  height: var(--page-h);
  background: var(--paper);
  padding: 46px 44px 62px;
}
.page .content {
  height: 100%;
  overflow: hidden;
  font-size: 19px;
  line-height: 1.55;
}
.folio {
  position: absolute;
  left: 0; right: 0; bottom: 22px;
  text-align: center;
  font-family: "Grandstander", cursive;
  font-weight: 600;
  font-size: 14px;
  color: var(--muted);
}
.content p { margin: 0 0 .62em; }
.content p.split { margin-bottom: 0; }
.content em { font-style: italic; }
.content strong { font-weight: 700; }

.ch-head { text-align: center; margin: 26px 0 22px; }
.ch-num {
  font-family: "Grandstander", cursive;
  font-weight: 600;
  font-size: 15px;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: var(--gold);
}
.ch-title {
  font-family: "Grandstander", cursive;
  font-weight: 800;
  font-size: 30px;
  line-height: 1.15;
  color: var(--blue);
  margin-top: 6px;
}
.ch-rule {
  margin: 14px auto 0;
  width: 110px;
  height: 14px;
  background:
    radial-gradient(circle, var(--gold) 3px, transparent 3.5px) 0 50% / 22px 14px repeat-x;
  opacity: .55;
}
p.dropcap::first-letter {
  float: left;
  font-family: "Grandstander", cursive;
  font-weight: 800;
  font-size: 3.2em;
  line-height: .82;
  padding: 6px 8px 0 0;
  color: var(--gold);
}

.sfx {
  font-family: "Grandstander", cursive;
  font-weight: 800;
  font-size: 36px;
  text-align: center;
  color: var(--red);
  letter-spacing: .03em;
  margin: .15em 0 .45em;
  transform: rotate(-3deg);
  text-shadow: 2px 2px 0 #f7d36b;
}

figure.ill { margin: 6px -16px 16px; }
figure.ill img {
  display: block;
  width: 100%;
  aspect-ratio: 900 / 640;
  object-fit: cover;
  border-radius: 6px;
}

ol.code { margin: 0 0 .7em; padding-left: 1.5em; }
ol.code li { margin-bottom: .25em; padding-left: .2em; }
ol.code li::marker { font-family: "Grandstander", cursive; color: var(--gold); font-weight: 800; }

.note {
  margin: 6px 10px 18px;
  padding: 14px 18px 12px 30px;
  font-family: "Patrick Hand", cursive;
  font-size: 22px;
  line-height: 31px;
  background:
    linear-gradient(to right, transparent 20px, #e9a0a0 20px, #e9a0a0 22px, transparent 22px),
    repeating-linear-gradient(to bottom, #fffbe6 0, #fffbe6 30px, #b9d3ea 30px, #b9d3ea 31px);
  box-shadow: 0 4px 12px rgba(0,0,0,.14);
  transform: rotate(-1.2deg);
  border-radius: 2px;
}
.note h4 { margin: 0; font-weight: 400; font-size: 24px; text-decoration: underline; }
.note ol { margin: 0; padding-left: 1.3em; }

.the-end {
  text-align: center;
  font-family: "Grandstander", cursive;
  font-weight: 800;
  font-size: 34px;
  color: var(--blue);
  margin-top: 24px;
  letter-spacing: .08em;
}

/* ---------- cover, title page, back cover ---------- */
.page.cover, .page.backcover {
  background: var(--blue);
  color: #fdf3df;
  padding: 26px;
}
.cover .frame, .backcover .frame {
  height: 100%;
  border: 3px solid rgba(253, 243, 223, .75);
  border-radius: 10px;
  padding: 26px 22px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}
.cover .kicker {
  font-family: "Grandstander", cursive;
  font-weight: 600;
  font-size: 13px;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: #f7d36b;
}
.cover h1 {
  font-family: "Grandstander", cursive;
  font-weight: 800;
  font-size: 34px;
  line-height: 1.08;
  margin: 12px 0 4px;
}
.cover h2 {
  font-family: "Grandstander", cursive;
  font-weight: 600;
  font-size: 21px;
  line-height: 1.2;
  margin: 0 0 18px;
  color: #f7d36b;
}
.cover img {
  width: 100%;
  aspect-ratio: 900 / 640;
  object-fit: cover;
  border-radius: 8px;
  border: 4px solid #fdf3df;
  box-shadow: 0 6px 18px rgba(0,0,0,.3);
  margin: auto 0;
}
.cover .tag { font-size: 15px; line-height: 1.4; opacity: .9; }

.page.titlepage .content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  text-align: center;
}
.titlepage .t1 { font-family: "Grandstander", cursive; font-weight: 800; font-size: 30px; line-height: 1.12; color: var(--blue); }
.titlepage .t2 { font-family: "Grandstander", cursive; font-weight: 600; font-size: 21px; color: var(--gold); margin: 8px 0 34px; }
.titlepage .cast { font-size: 17px; line-height: 1.6; }
.titlepage .cast b { font-family: "Grandstander", cursive; color: var(--blue); }

.backcover .blurb { font-size: 18px; line-height: 1.5; margin: auto 0; }
.backcover .blurb p { margin: 0 0 1em; }
.backcover .stamp {
  font-family: "Grandstander", cursive;
  font-weight: 800;
  font-size: 26px;
  color: #f7d36b;
  border: 3px solid #f7d36b;
  border-radius: 8px;
  padding: 4px 16px;
  transform: rotate(-6deg);
  margin-bottom: 18px;
}
.restart {
  font: inherit;
  font-family: "Grandstander", cursive;
  font-weight: 600;
  font-size: 17px;
  background: #f7d36b;
  color: var(--blue);
  border: 0;
  border-radius: 999px;
  padding: 10px 22px;
  cursor: pointer;
}

.page.blank .content {
  display: flex; align-items: center; justify-content: center;
  font-size: 60px; color: #e7dccb;
}

/* ---------- controls ---------- */
.controls {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: var(--controls);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 0 16px env(safe-area-inset-bottom);
}
.btn {
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 0;
  background: var(--blue);
  color: #fdf3df;
  font-size: 22px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,.2);
}
.btn:disabled { opacity: .35; cursor: default; }
.where { text-align: center; min-width: 210px; max-width: 60vw; }
.where span {
  display: block;
  font-family: "Grandstander", cursive;
  font-weight: 600;
  font-size: 16px;
  color: var(--blue);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.where small { font-size: 12px; color: #7d6650; }

.loading {
  position: absolute;
  font-family: "Grandstander", cursive;
  font-size: 20px;
  color: var(--blue);
}

/* measuring box for pagination (off-screen, unscaled) */
.measure {
  position: absolute;
  left: -10000px;
  top: 0;
  visibility: hidden;
}

@media (prefers-reduced-motion: reduce) {
  :root { --flip: 0.01s; }
}
