/* book — Scrapbook stage, BookCover, BookSpread, Page, PageTurn */

.stage {
  position: relative;
  z-index: 3;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.1rem;
  perspective: 2600px;
  perspective-origin: 50% 42%;
  padding: 1rem 0.6rem 0.4rem;
}

.stage[hidden] {
  display: none;
}

.stage.is-in .book {
  animation: bookIn 1.1s cubic-bezier(0.2, 0.7, 0.2, 1) both;
}

@keyframes bookIn {
  from {
    opacity: 0;
    transform: rotateX(16deg) translateY(40px) scale(0.94);
  }
}

/* ── the book ───────────────────────────────── */
.book {
  position: relative;
  width: min(94vw, 440px);
  height: min(86vh, 720px);
  transform-style: preserve-3d;
  transform: rotateX(4deg);
  transition: transform 1s cubic-bezier(0.2, 0.7, 0.2, 1);
}

.book__back {
  position: absolute;
  inset: 0;
  border-radius: 3px 6px 6px 3px;
  background: linear-gradient(145deg, #241c16, #120e0b);
  box-shadow: 0 40px 90px rgba(0, 0, 0, 0.65);
  transform: translateZ(-14px);
}

.book__endpaper,
.book__shadow {
  display: none;
  opacity: 0;
  transition: opacity 0.6s ease 0.35s;
  pointer-events: none;
}

.book__endpaper {
  position: absolute;
  top: 0;
  left: 0;
  width: 50%;
  height: 100%;
  border-radius: 3px 0 0 3px;
  background: radial-gradient(
      ellipse at 90% 50%,
      rgba(0, 0, 0, 0.22),
      transparent 62%
    ),
    linear-gradient(120deg, #e6d7bd, #d9c7a8);
}

.book__shadow {
  position: absolute;
  left: 50%;
  top: 2%;
  width: 42px;
  height: 96%;
  transform: translateX(-50%);
  background: linear-gradient(90deg, transparent, rgba(0, 0, 0, 0.3), transparent);
}

.leaves {
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
}

/* ── PageTurn ───────────────────────────────── */
.leaf {
  position: absolute;
  inset: 0;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform 1.15s cubic-bezier(0.35, 0.05, 0.2, 1);
  will-change: transform;
}

.leaf.is-flipped {
  transform: rotateY(-179.9deg);
}

/* the weight of a sheet: slow to leave the stack, lifts as it passes
   vertical, then settles rather than snapping */
.leaf.turn-fwd {
  animation: turnFwd 1.15s cubic-bezier(0.42, 0.02, 0.22, 1) forwards;
}

.leaf.turn-bwd {
  animation: turnBwd 1.15s cubic-bezier(0.42, 0.02, 0.22, 1) forwards;
}

@keyframes turnFwd {
  0% {
    transform: rotateY(0deg) translateZ(0);
  }
  48% {
    transform: rotateY(-88deg) translateZ(26px);
  }
  76% {
    transform: rotateY(-152deg) translateZ(12px);
  }
  100% {
    transform: rotateY(-179.9deg) translateZ(0);
  }
}

@keyframes turnBwd {
  0% {
    transform: rotateY(-179.9deg) translateZ(0);
  }
  48% {
    transform: rotateY(-92deg) translateZ(26px);
  }
  76% {
    transform: rotateY(-28deg) translateZ(12px);
  }
  100% {
    transform: rotateY(0deg) translateZ(0);
  }
}

/* shading that reads as the paper bending along the spine */
.face::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.5s ease;
  background: linear-gradient(
    to left,
    rgba(48, 34, 18, 0.26),
    rgba(48, 34, 18, 0.06) 42%,
    transparent 70%
  );
}

.face--back::before {
  background: linear-gradient(
    to right,
    rgba(48, 34, 18, 0.26),
    rgba(48, 34, 18, 0.06) 42%,
    transparent 70%
  );
}

.leaf.is-turning .face::before {
  opacity: 1;
}

/* ── Page ───────────────────────────────────── */
.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  border-radius: 2px 5px 5px 2px;
  background: var(--paper);
}

.face--back {
  transform: rotateY(180deg);
  border-radius: 5px 2px 2px 5px;
}

.pg {
  padding: calc(var(--pw) * 0.075) calc(var(--pw) * 0.09) calc(var(--pw) * 0.12);
  color: var(--ink-soft);
  overflow: hidden;
  background: radial-gradient(
      ellipse at 0% 50%,
      rgba(90, 66, 38, 0.16),
      transparent 22%
    ),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 26px,
      rgba(140, 104, 60, 0.045) 27px
    ),
    linear-gradient(160deg, var(--paper), var(--paper-2) 60%, var(--paper-3));
}

.pg::-webkit-scrollbar {
  display: none;
}

.pg__inner {
  height: 100%;
  max-height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: calc(var(--pw) * 0.022);
  overflow: hidden;
  box-sizing: border-box;
}

/* short pages can breathe in the middle — never center tall stacks (that clips top + bottom) */
.pg__inner--sparse {
  justify-content: center;
  padding-top: 0;
}

.face--back.pg {
  background: radial-gradient(
      ellipse at 100% 50%,
      rgba(90, 66, 38, 0.16),
      transparent 22%
    ),
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 26px,
      rgba(140, 104, 60, 0.045) 27px
    ),
    linear-gradient(200deg, var(--paper), var(--paper-2) 60%, var(--paper-3));
}

/* page moods — must outrank the .face--back.pg paper background */
.pg.pg--night {
  background: radial-gradient(ellipse at 50% 8%, #223047 0%, transparent 62%),
    linear-gradient(170deg, #172038, #0d1220);
  color: rgba(238, 232, 220, 0.85);
}

.pg.pg--madina {
  background: radial-gradient(
      ellipse at 50% 76%,
      rgba(200, 169, 120, 0.16),
      transparent 60%
    ),
    linear-gradient(170deg, #1d3a30, var(--forest) 55%, #0c1c16);
  color: rgba(238, 232, 220, 0.85);
}

.pg.pg--sacred {
  background: radial-gradient(
      ellipse at 50% 34%,
      rgba(200, 169, 120, 0.16),
      transparent 62%
    ),
    linear-gradient(170deg, #1b2740, var(--midnight) 55%, #0f1729);
  color: rgba(238, 232, 220, 0.86);
}

.pg.pg--dua {
  background: radial-gradient(
      ellipse at 50% 0%,
      rgba(207, 169, 108, 0.28),
      transparent 58%
    ),
    linear-gradient(170deg, #faf3e5, #eee0c6);
}

.pg.pg--last {
  background: radial-gradient(
      ellipse at 50% 100%,
      rgba(207, 169, 108, 0.22),
      transparent 55%
    ),
    linear-gradient(200deg, var(--paper), var(--paper-3));
}

.pg--note .pg__inner {
  justify-content: flex-start;
  padding-top: calc(var(--pw) * 0.03);
}

/* ── BookCover ──────────────────────────────── */
.cover {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--gold-2);
  background: radial-gradient(ellipse at 30% 20%, #3a2b1e 0%, transparent 60%),
    linear-gradient(150deg, #241a13 0%, #150f0b 55%, #241a13 100%);
  box-shadow: inset 0 0 80px rgba(0, 0, 0, 0.7);
}

.cover::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(
    108deg,
    transparent 38%,
    rgba(255, 230, 180, 0.11) 48%,
    transparent 58%
  );
  background-size: 260% 100%;
  animation: sheen 7s ease-in-out infinite;
}

@keyframes sheen {
  0%,
  62% {
    background-position: 160% 0;
  }
  100% {
    background-position: -60% 0;
  }
}

.cover__frame {
  width: 82%;
  text-align: center;
  padding: calc(var(--pw) * 0.09);
  border: 1px solid rgba(207, 169, 108, 0.32);
  outline: 3px solid rgba(207, 169, 108, 0.12);
  outline-offset: -9px;
}

.cover__moon {
  width: calc(var(--pw) * 0.22);
  height: auto;
  display: block;
  margin: 0 auto calc(var(--pw) * 0.035);
  mix-blend-mode: screen;
  -webkit-mask-image: radial-gradient(circle at 50% 50%, #000 45%, transparent 72%);
  mask-image: radial-gradient(circle at 50% 50%, #000 45%, transparent 72%);
}

.cover__name {
  font-family: "Cormorant Garamond", serif;
  font-style: italic;
  font-weight: 400;
  font-size: calc(var(--pw) * 0.195);
  line-height: 0.98;
  color: #f0dcb6;
  text-shadow: 0 1px 0 rgba(255, 240, 210, 0.35), 0 10px 30px rgba(0, 0, 0, 0.5);
}

.cover__rule {
  display: block;
  width: 46%;
  height: 1px;
  margin: calc(var(--pw) * 0.045) auto;
  background: linear-gradient(90deg, transparent, var(--gold-2), transparent);
}

.cover__sub {
  font-family: "Cormorant Garamond", serif;
  font-size: calc(var(--pw) * 0.05);
  letter-spacing: 0.34em;
  text-transform: uppercase;
  color: rgba(207, 169, 108, 0.75);
}

.cover__hand {
  font-family: "Caveat", cursive;
  font-size: calc(var(--pw) * 0.058);
  color: rgba(226, 205, 170, 0.6);
  margin-top: calc(var(--pw) * 0.05);
}

.cover__corner {
  position: absolute;
  width: calc(var(--pw) * 0.07);
  height: calc(var(--pw) * 0.07);
  border: 1px solid rgba(207, 169, 108, 0.4);
}

.cover__corner--tl {
  top: 12px;
  left: 12px;
  border-right: 0;
  border-bottom: 0;
}
.cover__corner--tr {
  top: 12px;
  right: 12px;
  border-left: 0;
  border-bottom: 0;
}
.cover__corner--bl {
  bottom: 12px;
  left: 12px;
  border-right: 0;
  border-top: 0;
}
.cover__corner--br {
  bottom: 12px;
  right: 12px;
  border-left: 0;
  border-top: 0;
}

/* ── BookSpread (desktop) ───────────────────── */
@media (min-width: 900px) and (min-height: 620px) {
  .book {
    width: min(94vw, 1080px);
    height: min(86vh, 780px);
  }

  .book__endpaper,
  .book__shadow {
    display: block;
  }

  .leaves {
    width: 50%;
  }

  .book.is-closed {
    transform: rotateX(4deg) translateX(-25%);
  }

  .book:not(.is-closed) .book__endpaper,
  .book:not(.is-closed) .book__shadow {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .leaf {
    transition-duration: 0.35s;
  }
  .cover::after {
    animation: none;
  }
}
