/* The library cover and animated cover share theme artwork and typography. */
.module-book {
  position: relative;
  width: min(100%, 340px);
  min-height: 440px;
  margin: 8px auto 24px;
  padding: 20px 20px 32px 30px;
  color: #f8edc9;
  background: #315c4b;
  border-radius: 4px 15px 15px 4px;
  box-shadow:
    7px 6px 0 #eee6ce,
    10px 9px 0 #cbbf9c,
    13px 12px 0 #315c4b,
    14px 22px 35px #263e392e;
  transform: perspective(900px) rotateY(-9deg) rotateZ(-2deg);
  text-align: center;
  transition:
    transform 0.3s,
    box-shadow 0.3s;
}

.module-book:hover,
.module-book:focus-visible {
  transform: perspective(900px) rotateY(0) rotateZ(0) translateY(-7px);
  box-shadow:
    7px 6px 0 #eee6ce,
    10px 9px 0 #cbbf9c,
    13px 12px 0 #315c4b,
    14px 30px 40px #263e3940;
}

.book-spine {
  position: absolute;
  inset: 0 auto 0 0;
  width: 19px;
  border-right: 1px solid #ffffff24;
  background: linear-gradient(90deg, #0e302b80, #ffffff15, #102b2340);
}

.cover-border {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 22px 12px;
  border: 1px solid #cdb46b9c;
  outline: 1px solid #cdb46b40;
  outline-offset: 5px;
}

.cover-series,
.cover-footer {
  font-size: 8px;
  letter-spacing: 2px;
  line-height: 1.6;
}

.cover-title {
  font: 29px/1.2 var(--serif);
  color: #fff2d1;
}

.cover-border img {
  width: 170px;
  height: 170px;
  object-fit: cover;
  object-position: 70%;
  border-radius: 50% 50% 40% 40%;
  border: 3px double #d7c68d;
}

.cover-ornament {
  font-size: 20px;
  color: #e4c97e;
}

.cover-subtitle {
  font: italic 12px/1.6 var(--serif);
}

.book-open-label {
  position: absolute;
  bottom: -30px;
  left: 0;
  right: 0;
  color: var(--green);
  font-size: 12px;
}

.reading-book {
  overflow: hidden;
  background: #29453c;
}

.book-stage {
  position: fixed;
  inset: 0;
  max-width: none;
  margin: 0;
  padding: 12px 20px 20px;
  background: radial-gradient(ellipse at top, #6d8973, #253f36);
  overflow: auto;
  perspective: 1800px;
}

.book-frame {
  --page-stack-width: clamp(10px, 1.5vw, 20px);
  --page-stack-depth: 10px;

  position: relative;
  width: 100%;
  height: calc(100dvh - 32px);
  min-height: 600px;
  display: flex;
  flex-direction: column;
  transform-origin: center;
}

.book-toolbar {
  height: 48px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 10px;
  color: #f2ecd8;
}

.book-toolbar .text-button,
.book-toolbar .icon-button,
.book-toolbar .gem-count,
.book-toolbar .gem-count span,
.book-toolbar .gem-count .icon {
  color: #f2ecd8;
}

.book-toolbar .icon-button {
  border-color: #f2ecd84a;
}

.book-name {
  font: italic 14px var(--serif);
  opacity: 0.8;
}

.book-toolbar .header-actions {
  gap: 12px;
}

.book-toolbar .gem-count.collecting {
  color: #397460;
}

.book-spread {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  flex: 1;
  min-height: 0;
  isolation: isolate;
  padding: 0 var(--page-stack-width) var(--page-stack-depth);
  border: 7px solid #7c6543;
  border-bottom-width: 12px;
  border-radius: 12px 20px 18px 12px;
  background:
    linear-gradient(
      90deg,
      #6f563e40,
      transparent 4%,
      transparent 96%,
      #6f563e50
    ),
    repeating-linear-gradient(
      90deg,
      #f5ebd2 0 2px,
      #c2af89 2px 3px,
      #fff7e4 3px 4px
    );
  box-shadow:
    0 2px 0 #b49a68,
    0 6px 0 #4e3b29,
    0 10px 0 #816744,
    0 16px 16px #10251f55,
    0 30px 55px #10251f99;
  perspective: 2000px;
}

/* Exposed paper edges stay in place beneath the sheets as they turn. */
.book-spread::before {
  content: '';
  position: absolute;
  inset: auto 0 0;
  height: var(--page-stack-depth);
  border-radius: 0 0 8px 5px;
  background:
    linear-gradient(90deg, transparent 43%, #76604166 50%, transparent 57%),
    repeating-linear-gradient(
      0deg,
      #f5ebd2 0 2px,
      #baa581 2px 3px,
      #fff7e4 3px 4px
    );
  box-shadow: inset 0 3px 4px #6f563e33;
  pointer-events: none;
}

.book-left,
.book-right {
  position: relative;
  min-width: 0;
  min-height: 0;
  padding: clamp(20px, 3vw, 48px);
  overflow: auto;
  scrollbar-width: thin;
  scrollbar-color: #c2b694 transparent;
  background: #faf4e4;
  background-image: repeating-linear-gradient(
    0deg,
    #bba77a05 0 1px,
    transparent 1px 4px
  );
}

.book-left {
  border-radius: 5px 0 0 5px;
  background-image: linear-gradient(
    90deg,
    #c8b58c18,
    transparent 12%,
    #fffdf440 75%,
    #8e704322
  );
  box-shadow:
    inset -26px 0 28px -22px #6f563e99,
    -3px 2px 5px #6f563e40;
}

.book-right {
  border-bottom: 80px solid #faf4e4;
  border-radius: 0 12px 10px 0;
  background-image: linear-gradient(
    90deg,
    #8e704322,
    #fffdf440 25%,
    transparent 88%,
    #c8b58c18
  );
  box-shadow:
    inset 26px 0 28px -22px #6f563e99,
    3px 2px 5px #6f563e40;
  padding-bottom: 20px;
}

.book-spread::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: var(--page-stack-depth);
  left: calc(50% - 8px);
  width: 16px;
  background: linear-gradient(
    90deg,
    transparent,
    #6f563e38,
    #fff7e460,
    transparent
  );
  pointer-events: none;
  z-index: 1;
}

.book-footer {
  width: 100%;
  max-width: none;
  margin: 0;
  display: flex;
  justify-content: space-between;
  padding: 20px 12px 0;
  color: #d4d5ba;
  font-size: 8px;
  letter-spacing: 1.5px;
  flex-shrink: 0;
}

.story-art,
.ending-art {
  height: 100%;
  min-height: 420px;
  position: relative;
  border: 1px solid #d2c59c;
  padding: 8px;
  overflow: hidden;
}

.story-art img,
.ending-art > img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: right;
  border-radius: 40% 40% 2px 2px;
}

.art-caption {
  position: absolute;
  inset: auto 8px 8px;
  padding: 60px 24px 28px;
  text-align: center;
  background: linear-gradient(transparent, #f4edda 55%);
}

.art-caption span {
  font-size: 9px;
  letter-spacing: 2px;
  color: #738064;
}

.art-caption h2 {
  margin-top: 12px;
  font-size: clamp(20px, 2.1vw, 32px);
  line-height: 1.4;
}

.story-copy {
  max-width: 500px;
  margin: auto;
  padding: 16px 10px 45px;
}

.story-copy h1 {
  font: 500 clamp(28px, 3.2vw, 49px)/1.18 var(--serif);
  color: #3c5844;
  margin: 8px 0 18px;
  letter-spacing: -1px;
}

.story-copy p {
  color: #6b725b;
  font-size: clamp(13px, 1.15vw, 16px);
  margin-bottom: 14px;
}

.story-lead::first-letter {
  float: left;
  font: 52px/1 var(--serif);
  padding: 4px 7px 0 0;
  color: #967e40;
}

.story-flourish {
  font-size: 28px;
  color: #bba05e;
  margin: 14px 0;
}

.quest-instructions {
  padding-left: 23px;
  color: #56694c;
  font-size: 13px;
  line-height: 2.2;
  margin: 18px 0;
}

.story-copy .story-note {
  font-size: 12px;
  font-style: italic;
  padding-top: 20px;
  border-top: 1px solid #ded5ba;
}

.story-copy .turn-invitation {
  font: italic 13px/1.7 var(--serif);
  color: #a18443;
  margin-top: 20px;
}

.corner-slot {
  position: absolute;
  right: calc(7px + var(--page-stack-width));
  bottom: calc(38px + var(--page-stack-depth));
  z-index: 4;
}

.page-corner {
  position: relative;
  width: 100px;
  height: 100px;
  padding: 0;
  background: transparent;
  color: #705d31;
  border-radius: 0 0 12px;
  animation: corner-glow 3s ease-in-out infinite;
}

.page-corner::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(
    135deg,
    transparent 47%,
    #9d8352 49%,
    #fff6dc 52%,
    #d8c18c 85%
  );
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
  filter: drop-shadow(-3px -3px 4px #66533340);
  transition: transform 0.2s;
}

.page-corner:hover::before,
.page-corner:focus-visible::before {
  transform: scale(1.08);
}

@keyframes corner-glow {
  0%,
  100% {
    filter: drop-shadow(0 0 3px #e5bd6226);
  }

  50% {
    filter: drop-shadow(0 0 12px #e5bd6273);
  }
}

.quest-page-heading h1 {
  font-size: clamp(22px, 2.1vw, 32px);
  margin: 14px 0 10px;
}

.quest-page-heading p {
  color: #7c816c;
  font-size: 12px;
  max-width: 390px;
}

.book-left .equation {
  font-size: clamp(32px, 4vw, 54px);
  margin: 25px 0 16px;
}

.book-left .river-scene {
  margin: 0 -12px 20px;
  width: calc(100% + 24px);
  border-radius: 25px;
}

.book-left .countup-note {
  text-align: center;
}

.book-progress {
  display: flex;
  justify-content: center;
  gap: 16px;
  color: #c7baa0;
  margin-top: 24px;
}

.book-progress .complete {
  color: var(--green);
}

.book-right .question-area {
  max-width: 500px;
  margin: auto;
  padding-top: 10px;
}

.page-question-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.book-right .step-labels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 24px 0 30px;
  padding: 0;
  border: 0;
  background: transparent;
}

.book-right .question-area h2 {
  font-size: clamp(24px, 2.7vw, 40px);
  line-height: 1.3;
  margin: 28px 0;
}

.book-right #answer-form {
  flex-wrap: wrap;
  gap: 12px;
}

.book-right #answer {
  background: #fffdf5;
  width: 90px;
}

.book-right .hint-box {
  background: #eee9d5;
  font-size: 13px;
}

.book-reassurance {
  font-size: 11px;
  color: #9a9c82;
  margin-top: 32px;
}

.book-reassurance .icon {
  width: 15px;
}

.ending-seal {
  position: absolute;
  bottom: 35px;
  left: 50%;
  transform: translateX(-50%);
  display: grid;
  place-items: center;
  gap: 12px;
  text-align: center;
  width: 180px;
  height: 180px;
  padding: 24px;
  border: 6px double #ddc58a;
  border-radius: 50%;
  background: #fcf0ccf2;
  color: #9a7e3c;
  font: 17px/1.4 var(--serif);
  box-shadow: 0 10px 20px #32432c33;
}

.ending-seal .icon {
  width: 45px;
  height: 45px;
}

.end-copy {
  text-align: center;
}

.end-copy h1 {
  font-size: clamp(48px, 5vw, 76px);
  margin: 20px 0 10px;
}

.adventure-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 28px 0;
}

.adventure-summary b {
  display: block;
  font: 32px var(--serif);
  color: #8f773f;
}

.adventure-summary span {
  font-size: 10px;
  color: #81816b;
}

.solved-problems {
  list-style: none;
  padding: 0;
  margin: 20px auto;
  max-width: 250px;
}

.solved-problems li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid #e8dec5;
  font-size: 13px;
}

.solved-problems .icon {
  color: #65976b;
  width: 16px;
}

/* Real front/back faces rotate around the binding, revealing the next spread. */
.page-turn-layer {
  position: absolute;
  inset: 0 var(--page-stack-width) var(--page-stack-depth);
  z-index: 5;
  pointer-events: none;
  perspective: 2000px;
}

.turn-static-left {
  position: absolute;
  inset: 0 50% 0 0;
}

.turning-leaf,
.animated-cover {
  position: absolute;
  inset: 0 0 0 50%;
  transform-origin: left center;
  transform-style: preserve-3d;
  z-index: 6;
}

.turning-leaf .leaf-front,
.turning-leaf .leaf-back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  box-shadow: 0 5px 30px #45362240;
}

.turning-leaf .leaf-back {
  transform: rotateY(180deg);
}

.animated-cover {
  right: var(--page-stack-width);
  bottom: var(--page-stack-depth);
  padding: 25px;
  background: #315c4b;
  color: #f8edc9;
  backface-visibility: hidden;
  border: 3px solid #ddc58a;
  border-radius: 0 12px 12px 0;
  overflow: hidden;
}

.animated-cover .cover-border {
  justify-content: center;
}

@media (width <= 1000px) {
  .book-name {
    display: none;
  }

  .book-left,
  .book-right {
    padding: 24px;
  }

  .book-right {
    padding-bottom: 20px;
  }

  .book-right .step-labels {
    gap: 8px;
  }

  .book-right .step-labels span {
    font-size: 9px;
  }
}

@media (width <= 700px) {
  .book-stage {
    padding: 6px 8px 16px;
  }

  .book-frame {
    --page-stack-width: 7px;
    --page-stack-depth: 7px;

    height: auto;
    min-height: calc(100dvh - 22px);
  }

  .book-toolbar {
    position: sticky;
    top: -6px;
    z-index: 8;
    background: #355447;
    padding: 4px 6px 8px;
    height: 52px;
  }

  .book-spread {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto;
    border-width: 4px;
  }

  .book-left,
  .book-right {
    padding: 20px;
    overflow: visible;
  }

  .book-left {
    box-shadow: inset 0 -22px 22px -22px #6f563e80;
  }

  .book-right {
    box-shadow: inset 0 22px 22px -22px #6f563e80;
    padding-bottom: 20px;
  }

  .book-spread::after {
    display: none;
  }

  .story-art,
  .ending-art {
    height: 240px;
    min-height: 0;
  }

  .art-caption {
    padding: 24px 10px 12px;
  }

  .art-caption h2 {
    font-size: 19px;
  }

  .story-art img,
  .ending-art > img {
    object-position: 70% 45%;
    border-radius: 4px;
  }

  .story-copy {
    padding: 10px 4px;
  }

  .story-copy h1 {
    font-size: 34px;
  }

  .story-flourish {
    margin: 8px 0;
  }

  .quest-page-heading h1 {
    font-size: 22px;
    margin: 8px 0;
  }

  .quest-page-heading p {
    display: none;
  }

  .book-left .equation {
    font-size: 32px;
    margin: 14px 0;
  }

  .book-left .river-scene {
    margin-bottom: 10px;
  }

  .book-progress {
    margin-top: 12px;
  }

  .book-right .question-area {
    padding: 0;
  }

  .book-right .step-labels {
    margin: 12px 0;
  }

  .book-right .question-area h2 {
    margin: 18px 0;
    font-size: 26px;
  }

  .book-reassurance {
    margin-top: 20px;
  }

  .book-footer {
    padding: 18px 6px 0;
    font-size: 6px;
    letter-spacing: 0.6px;
  }

  .corner-slot {
    bottom: calc(32px + var(--page-stack-depth));
    right: calc(4px + var(--page-stack-width));
  }

  .ending-seal {
    width: 140px;
    height: 140px;
    bottom: 20px;
    font-size: 13px;
  }

  .ending-seal .icon {
    width: 30px;
    height: 30px;
  }

  .end-copy h1 {
    font-size: 56px;
  }

  .turn-static-left {
    inset: 0 0 auto;
    height: var(--book-fold, 50%);
  }

  .turning-leaf,
  .animated-cover {
    inset: var(--book-fold, 50%) 0 0;
    transform-origin: center top;
  }

  .turning-leaf .leaf-back {
    transform: rotateX(180deg);
  }

  .animated-cover {
    left: var(--page-stack-width);
    right: var(--page-stack-width);
    bottom: var(--page-stack-depth);
    padding: 14px;
  }

  .animated-cover .cover-border img {
    height: 100px;
    width: 100px;
  }

  .animated-cover .cover-border {
    gap: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .page-corner {
    animation: none;
    filter: drop-shadow(0 0 6px #e5bd6240);
  }

  .module-book,
  .page-corner::before {
    transition: none;
  }
}

/* Background visitors remain decorative and never intercept the number path. */
.scene-decorations {
  pointer-events: none;
  opacity: 0.9;
}

.ambient-detail {
  animation: ambient-breathe 5s ease-in-out infinite;
  animation-delay: var(--ambient-delay);
}

@keyframes ambient-breathe {
  0%,
  100% {
    opacity: 0.75;
  }

  50% {
    opacity: 1;
  }
}

.scene-garden .stone-ripple,
.scene-clouds .stone-ripple,
.scene-castle .stone-ripple,
.scene-forest .stone-ripple {
  opacity: 0.25;
}

.scene-soft-landing {
  animation: soft-landing 600ms ease-out both;
}

@keyframes soft-landing {
  from {
    opacity: 0;
    scale: 0.7;
  }

  to {
    opacity: 1;
    scale: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ambient-detail,
  .scene-soft-landing {
    animation: none;
  }
}

.quest-page-heading {
  /* Keep text on its own paint layer when a 3D page-turn copy is removed. */
  transform: translateZ(0);
}
