:root {
  --night: #0f1f3d;
  --night-deep: #081228;
  --paper: #fbf4e4;
  --paper-edge: #efe2c6;
  --ink: #2c2a35;
  --ink-soft: #6b6470;
  --berry: #6d3a8c;
  --berry-dark: #4c2666;
  --pine: #1f5a41;
  --gold: #d9a441;
  --serif: "Fraunces", "Iowan Old Style", Georgia, serif;
  --display: "Berkshire Swash", "Fraunces", Georgia, serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--serif);
  color: var(--ink);
  background:
    radial-gradient(ellipse at 50% 0%, #3a2f6b 0%, var(--night) 50%, var(--night-deep) 100%) fixed;
  overflow-x: hidden;
}

#sky {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.stage {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: clamp(12px, 3vw, 40px);
}

/* ---------- the book ---------- */
.book {
  --size: min(46vw, 82vh, 600px);
  display: grid;
  grid-template-columns: var(--size) var(--size);
  min-height: var(--size);
  background: var(--paper);
  border-radius: 10px 16px 16px 10px;
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.25),
    6px 6px 0 -1px var(--paper-edge),
    7px 7px 0 -1px rgba(0, 0, 0, 0.2),
    12px 12px 0 -2px var(--paper-edge),
    0 30px 60px rgba(0, 0, 0, 0.55);
  position: relative;
  z-index: 2;
}

.picture {
  position: relative;
  overflow: hidden;
  border-radius: 10px 0 0 10px;
  background: #cfd8e6;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.picture img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
}

/* the shadow in the gutter where the pages meet */
.picture::after {
  content: "";
  position: absolute;
  inset: 0 0 0 auto;
  width: 40px;
  background: linear-gradient(to right, transparent, rgba(40, 25, 10, 0.28));
  pointer-events: none;
}

.page {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: clamp(20px, 3.4vw, 46px) clamp(20px, 3.4vw, 46px) clamp(14px, 2vw, 24px);
  border-radius: 0 16px 16px 0;
  background:
    linear-gradient(to right, rgba(40, 25, 10, 0.16), transparent 34px),
    var(--paper);
}

.page-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: clamp(1.02rem, 1.55vw, 1.32rem);
  line-height: 1.55;
}

.page-body p { margin: 0 0 0.75em; text-wrap: pretty; }

.page-body > p:first-child::first-letter {
  font-family: var(--display);
  font-size: 3.1em;
  line-height: 0.8;
  float: left;
  padding: 0.08em 0.1em 0 0;
  color: var(--berry);
}

/* ---------- cover ---------- */
.is-cover .page-body { align-items: center; text-align: center; }

.title {
  font-family: var(--display);
  font-size: clamp(2.2rem, 4.6vw, 3.6rem);
  line-height: 1.05;
  margin: 0.1em 0 0.05em;
  color: var(--berry);
}

.subtitle {
  font-family: var(--display);
  font-size: clamp(1.15rem, 2vw, 1.6rem);
  color: var(--pine);
}

.blurb { font-style: italic; color: var(--ink-soft); margin-top: 0.6em; }

.is-cover .page-foot { justify-content: center; }
.is-cover .next { font-size: 1.1rem; padding: 0.8em 1.6em; }

.the-end {
  font-family: var(--display);
  font-size: 2em;
  color: var(--berry);
  text-align: center;
  margin-top: 0.2em !important;
}

/* ---------- footer / nav ---------- */
.page-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 12px;
  min-height: 56px;
}

.folio { color: var(--ink-soft); font-size: 0.85rem; font-style: italic; }

.nav {
  font: inherit;
  font-size: 0.98rem;
  font-weight: 600;
  border: 0;
  border-radius: 999px;
  padding: 0.6em 1.2em;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
}

.next { background: var(--berry); color: #fff; box-shadow: 0 3px 0 var(--berry-dark); }
.next:hover { background: #7f48a1; transform: translateY(-1px); }
.next:active { transform: translateY(2px); box-shadow: none; }

.back { background: transparent; color: var(--ink-soft); padding-left: 0.4em; }
.back:hover { color: var(--ink); }

.nav[hidden] { display: block; visibility: hidden; }
.is-cover .nav[hidden], .is-cover .folio { display: none; }

button:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }

/* ---------- page turn ---------- */
.picture img, .page-body, .page-foot { transition: opacity 0.28s ease, transform 0.28s ease; }

.turning-out .picture img { opacity: 0; }
.turning-out .page-body, .turning-out .page-foot { opacity: 0; transform: translateX(-18px); }
.turning-out[data-dir="back"] .page-body,
.turning-out[data-dir="back"] .page-foot { transform: translateX(18px); }

.turning-in .picture img { animation: fade-in 0.42s ease both; }
.turning-in .page-body, .turning-in .page-foot { animation: slide-in 0.42s ease both; }
.turning-in[data-dir="back"] .page-body,
.turning-in[data-dir="back"] .page-foot { animation-name: slide-in-back; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes slide-in { from { opacity: 0; transform: translateX(18px); } }
@keyframes slide-in-back { from { opacity: 0; transform: translateX(-18px); } }

/* ---------- picture extras ---------- */
.tap-hint {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  padding: 0.35em 0.9em;
  border-radius: 999px;
  background: rgba(15, 31, 61, 0.72);
  color: #fff;
  font-size: 0.85rem;
  white-space: nowrap;
  pointer-events: none;
  animation: bob 1.6s ease-in-out infinite;
}

@keyframes bob { 50% { transform: translate(-50%, -5px); } }

.sparkle {
  position: absolute;
  color: #ffe9a8;
  text-shadow: 0 0 8px #fff3c4;
  pointer-events: none;
  transform: translate(-50%, -50%);
  animation: sparkle 0.9s ease-out forwards;
}

@keyframes sparkle {
  to {
    transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(0.2) rotate(120deg);
    opacity: 0;
  }
}

.sound {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 4;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.12);
  font-size: 1.2rem;
  cursor: pointer;
}

.sound:hover { background: rgba(255, 255, 255, 0.22); }

/* ---------- small screens: picture on top, words below ---------- */
@media (max-width: 760px), (orientation: portrait) {
  .stage { place-items: start center; }

  .book {
    --size: min(94vw, 560px);
    grid-template-columns: var(--size);
    border-radius: 14px;
  }

  .picture { aspect-ratio: 1; border-radius: 14px 14px 0 0; }
  .picture::after { display: none; }

  .page {
    border-radius: 0 0 14px 14px;
    background: var(--paper);
    padding: 22px 22px 14px;
  }

  .page-body { font-size: 1.1rem; justify-content: flex-start; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
