@import url("https://fonts.googleapis.com/css2?family=Baloo+2:wght@500;600;700;800&family=Andika:wght@400;700&display=swap");

:root {
  --cream: #fbf1de;
  --panel: #fff9ec;
  --panel-line: rgba(143, 50, 8, 0.14);
  --terracotta: #c1440e;
  --terracotta-deep: #8f3208;
  --gold: #e8a93b;
  --gold-deep: #d4881b;
  --savanna: #6e7f3f;
  --ink: #3b2a1e;
  --ink-soft: rgba(59, 42, 30, 0.72);
  --dusk: #241a38;
  --star: #f4d584;
  --page-back: #f3e3c2;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
}

html {
  height: 100vh;
  height: 100dvh;
  /* html itself never scrolls — body is the one definitive scroll
     container below, so the scrollbar is wired to something that
     actually has fixed bounds instead of a box that just grows to
     fit its content (which made the earlier scrollbar non-functional). */
  overflow: hidden;
}

body {
  background: linear-gradient(180deg, #2b2140 0%, #1c1530 100%);
  color: var(--ink);
  font-family: "Andika", "Trebuchet MS", sans-serif;
  height: 100%;
  /* Block layout + margin:auto centers content WITHOUT the flex-centering
     bug where overflow gets clipped above the fold. If the book is taller
     than the screen, the page just scrolls to reach it — no content is
     ever hidden. */
  overflow-y: scroll;
  overflow-x: auto; /* safety net: reveal, not clip, anything that runs wide */
  scrollbar-width: thin;
  scrollbar-color: var(--gold-deep) rgba(255, 255, 255, 0.08);
}

body::-webkit-scrollbar {
  width: 12px;
}

body::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.06);
}

body::-webkit-scrollbar-thumb {
  background: var(--gold-deep);
  border-radius: 8px;
  border: 3px solid transparent;
  background-clip: padding-box;
}

body::-webkit-scrollbar-thumb:hover {
  background: var(--gold);
  background-clip: padding-box;
}

/* ---------- wrap / orientation hint ---------- */
.sb-wrap {
  max-width: 1000px;
  margin: 20px auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
}

.sb-orientation-hint {
  display: none;
  margin: 0;
  font-family: "Baloo 2", sans-serif;
  font-size: 0.72rem;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: rgba(244, 213, 132, 0.85);
  text-align: center;
}

@media (max-width: 820px) and (orientation: portrait) {
  .sb-orientation-hint {
    display: block;
    animation: sbHintPulse 2.4s ease-in-out infinite;
  }
}

@keyframes sbHintPulse {
  0%,
  100% {
    opacity: 0.6;
  }
  50% {
    opacity: 1;
  }
}

/* ---------- stage ---------- */
/* Width-driven sizing with a fixed aspect ratio, capped by a soft
   max-height. This never depends on exact viewport-height math (which
   mobile browser toolbars make unreliable) — if a device is short, the
   page just scrolls to show the rest instead of the book fighting to
   cram itself into the visible area. */
.sb-stage {
  position: relative;
  width: min(94vw, 900px);
  aspect-ratio: 3 / 4;
  max-height: 80vh;
  max-height: 80dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sb-book {
  position: relative;
  width: 100%;
  height: 100%;
  perspective: 2600px;
  cursor: pointer;
}

.sb-leaves {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
}

/* ---------- leaf / flip mechanics ---------- */
.sb-leaf {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transform-origin: left center;
  transition: transform 900ms cubic-bezier(0.45, 0.05, 0.15, 1);
  box-shadow: 0 20px 60px -20px rgba(0, 0, 0, 0.55);
  border-radius: 10px;
}

.sb-leaf.sb-flipped {
  transform: rotateY(-180deg);
}

.sb-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 10px;
  overflow: hidden;
  background: var(--page-back);
}

.sb-face.sb-back {
  transform: rotateY(180deg);
}

/* subtle page-edge shading during flip */
.sb-leaf::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.18), transparent 8%, transparent 92%, rgba(0, 0, 0, 0.12));
  border-radius: 10px;
}

/* ---------- cover ---------- */
.sb-cover {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 8%;
  color: #fff8ea;
}

.sb-sun {
  width: 84px;
  height: 84px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #f6c868 0%, var(--gold-deep) 70%);
  box-shadow: 0 0 0 6px rgba(255, 255, 255, 0.14), 0 0 0 12px rgba(255, 255, 255, 0.08);
  margin-bottom: 18px;
}

.sb-kicker {
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  font-size: 0.78rem;
  letter-spacing: 4px;
  text-transform: uppercase;
  opacity: 0.85;
  margin: 0 0 10px;
}

.sb-title {
  font-family: "Baloo 2", sans-serif;
  font-weight: 800;
  font-size: clamp(2rem, 6vw, 3.4rem);
  margin: 0 0 12px;
  text-wrap: balance;
}

.sb-subtitle {
  font-style: italic;
  font-size: 1rem;
  opacity: 0.9;
  max-width: 32ch;
  margin: 0 0 30px;
}

.sb-tap-hint {
  font-family: "Baloo 2", sans-serif;
  font-size: 0.72rem;
  letter-spacing: 2px;
  text-transform: uppercase;
  opacity: 0.65;
  position: absolute;
  bottom: 8%;
}

.sb-cover-inside {
  background: var(--cream);
}

.sb-the-end {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  letter-spacing: 6px;
  font-size: 1.4rem;
  color: #fff8ea;
}

/* ---------- story page ---------- */
.sb-page {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: var(--cream);
}

.sb-art-wrap {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
}

.sb-art {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 55%;
}

.sb-textpanel {
  position: relative;
  flex: 0 0 auto;
  max-height: 42%;
  overflow-y: auto;
  padding: 18px 26px 20px;
  background: var(--panel);
  box-shadow: 0 -8px 16px -12px rgba(59, 42, 30, 0.25);
}

.sb-pagenum {
  font-family: "Baloo 2", sans-serif;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--gold-deep);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
}

.sb-lines {
  /* scrolling is handled by the parent .sb-textpanel */
}

.sb-line {
  margin: 0 0 0.35em;
  font-size: clamp(0.95rem, 1.6vw, 1.14rem);
  line-height: 1.55;
  color: var(--ink);
}

.sb-page-ending .sb-textpanel {
  background: var(--dusk);
}

.sb-page-ending .sb-line {
  color: #f4ead8;
}

.sb-page-back {
  background: repeating-linear-gradient(135deg, var(--page-back) 0 18px, #efdcb2 18px 36px);
}

/* ---------- nav ---------- */
.sb-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 249, 236, 0.92);
  color: var(--terracotta-deep);
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
  transition: opacity 200ms, transform 200ms;
}

.sb-nav:hover {
  transform: translateY(-50%) scale(1.06);
}

.sb-nav:disabled {
  opacity: 0.25;
  cursor: default;
  transform: translateY(-50%);
}

.sb-prev {
  left: 10px;
}

.sb-next {
  right: 10px;
}

/* ---------- progress ---------- */
.sb-progress {
  position: absolute;
  bottom: -22px;
  left: 0;
  right: 0;
  height: 4px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  overflow: hidden;
}

.sb-progress-bar {
  height: 100%;
  background: var(--gold);
  transition: width 500ms ease;
}

@media (prefers-reduced-motion: reduce) {
  .sb-leaf {
    transition: none !important;
  }
}

/* Phones held sideways: images are landscape-shaped, so give them the room */
@media (orientation: landscape) and (max-height: 520px) {
  .sb-stage {
    width: min(90vw, 800px);
    aspect-ratio: 4 / 3;
    max-height: 92vh;
    max-height: 92dvh;
  }
  .sb-wrap {
    margin: 8px auto;
    gap: 6px;
  }
}

@media (max-width: 640px) and (orientation: portrait) {
  .sb-stage {
    width: 94vw;
    aspect-ratio: 3 / 4;
    max-height: 78vh;
    max-height: 78dvh;
  }
  .sb-nav {
    width: 38px;
    height: 38px;
  }
  .sb-textpanel {
    max-height: 50%;
    padding: 16px 18px 18px;
  }
  .sb-line {
    margin: 0 0 0.25em;
  }
}
