/* ============================================================
   Book Reader Overlay — ChanzoPlay
   Physical open-book layout with smooth page flip animation
   ============================================================ */

/* ─── Overlay ─────────────────────────────────────────────── */
#bookReaderModal {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 99999;
  background: radial-gradient(ellipse at 60% 40%, #3b1e0a 0%, #1c0d04 60%, #0d0702 100%);
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: calc(52px + env(safe-area-inset-top, 0px)) 12px calc(12px + env(safe-area-inset-bottom, 0px));
  gap: 0;
  height: 100dvh;
  box-sizing: border-box;
  overflow: hidden;
}

/* ─── Close Button ────────────────────────────────────────── */
.br-close {
  position: absolute;
  top: 12px;
  right: 16px;
  background: rgba(255, 255, 255, 0.1);
  border: 1.5px solid rgba(255, 255, 255, 0.22);
  color: #fff;
  font-size: 1.4rem;
  line-height: 1;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.15s;
  z-index: 100;
}
.br-close:hover {
  background: rgba(255, 255, 255, 0.22);
  transform: scale(1.1);
}

/* ─── Book Title ──────────────────────────────────────────── */
.br-title {
  color: #f0d9b5;
  font-family: 'Fredoka', 'Comic Neue', sans-serif;
  font-size: 1rem;
  font-weight: 600;
  text-align: center;
  margin-bottom: 10px;
  flex-shrink: 0;
  opacity: 0.85;
  letter-spacing: 0.04em;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.7);
}

/* ─── Stage ───────────────────────────────────────────────── */
.br-stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  flex: 1;
  min-height: 0;
}

/* ─── Open Book Spread ────────────────────────────────────── */
.br-book {
  position: relative;
  display: flex;
  height: min(82dvh, 64vw, 700px);
  aspect-ratio: 3 / 2;
  max-width: 98vw;
  max-height: calc(100dvh - 150px);
  cursor: grab;
  filter:
    drop-shadow(0 30px 60px rgba(0, 0, 0, 0.75))
    drop-shadow(0 6px 18px rgba(0, 0, 0, 0.5));
  transform-style: preserve-3d;
}
.br-book:active { cursor: grabbing; }

/* ─── Left Page (cream paper) ─────────────────────────────── */
.br-left-page {
  position: relative;
  width: 50%;
  height: 100%;
  background: linear-gradient(160deg, #f7edd6 0%, #f0e2c4 50%, #e8d5af 100%);
  border-radius: 3px 0 0 3px;
  overflow: hidden;
  box-shadow: inset 0 0 40px rgba(160, 110, 50, 0.08);
}

/* Gutter shadow at right edge of left page */
.br-gutter-shadow {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 28px;
  background: linear-gradient(to right,
    transparent 0%,
    rgba(0, 0, 0, 0.08) 50%,
    rgba(0, 0, 0, 0.18) 100%
  );
  pointer-events: none;
  z-index: 1;
}

/* Left page interior layout */
.br-left-decor {
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  padding: 22px 20px 36px;
  gap: 6px;
  text-align: center;
  position: relative;
  z-index: 2;
  overflow: hidden;
}

.br-left-title {
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: clamp(0.68rem, 1.5vw, 0.98rem);
  color: #6b4422;
  font-style: italic;
  line-height: 1.4;
  opacity: 0.75;
  display: block;
  flex-shrink: 0;
}

.br-left-ornament {
  font-size: clamp(1.12rem, 2.6vw, 1.8rem);
  color: #9b6b38;
  display: block;
  opacity: 0.45;
  line-height: 1;
  flex-shrink: 0;
}

.br-left-pagenum {
  font-family: 'Georgia', serif;
  font-size: clamp(0.5rem, 1.1vw, 0.75rem);
  color: #9b6b38;
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  opacity: 0.6;
  letter-spacing: 0.1em;
  white-space: nowrap;
}

/* ─── Right Page (destination — shown after leaf flips) ───── */
.br-right-page {
  position: relative;
  width: 50%;
  height: 100%;
  background-size: cover;
  background-position: center;
  background-color: #e8d5af;
  border-radius: 0 3px 3px 0;
  overflow: hidden;
}

/* ─── The Leaf (pivots at spine = left edge) ──────────────── */
.br-leaf {
  position: absolute;
  right: 0;
  top: 0;
  width: 50%;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  z-index: 10;
  cursor: grab;
  will-change: transform;
}
.br-leaf:active { cursor: grabbing; }
.br-leaf.br-leaf-hidden { opacity: 0; }

.br-flip-canvas {
  display: none;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 14;
  pointer-events: none;
}

/* ─── Leaf Faces ──────────────────────────────────────────── */
.br-leaf-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background-size: cover;
  background-position: center;
  overflow: hidden;
  border-radius: 0 3px 3px 0;
}

/* Front face: the current page illustration */
.br-leaf-front {
  box-shadow: inset -8px 0 20px rgba(0, 0, 0, 0.12);
}

/* Gradient overlay on front to simulate page curl shadow */
.br-leaf-front::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(to left,
    transparent 85%,
    rgba(0, 0, 0, 0.08) 100%
  );
  pointer-events: none;
  z-index: 1;
}

/* Back face: plain cream (back of a book page) */
.br-leaf-back {
  transform: rotateY(180deg);
  background: linear-gradient(160deg, #f0e2c4 0%, #e6d4a8 100%);
  border-radius: 3px 0 0 3px;
  box-shadow: inset 8px 0 20px rgba(0, 0, 0, 0.1);
}

/* Corner curl hint (bottom-right of front) */
.br-leaf-curl-hint {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 48px;
  height: 48px;
  background: radial-gradient(ellipse at bottom right,
    rgba(0, 0, 0, 0.22) 0%,
    transparent 65%
  );
  pointer-events: none;
  z-index: 5;
  border-radius: 0 0 3px 0;
}

/* ─── Centre Spine Overlay ────────────────────────────────── */
.br-book-spine {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 8px;
  transform: translateX(-50%);
  background: linear-gradient(to right,
    rgba(0, 0, 0, 0.35) 0%,
    rgba(0, 0, 0, 0.12) 30%,
    rgba(255, 255, 255, 0.06) 50%,
    rgba(0, 0, 0, 0.12) 70%,
    rgba(0, 0, 0, 0.30) 100%
  );
  z-index: 20;
  pointer-events: none;
}

/* ─── Page Text — printed on the left cream page ─────────── */
.br-page-text {
  flex: 1;
  overflow-y: auto;
  width: 100%;
  padding: 6px 6px 0;
  color: #3a2010;
  font-family: 'Georgia', 'Times New Roman', serif;
  font-size: clamp(0.78rem, 1.62vw, 1.06rem);
  font-weight: 400;
  line-height: 1.75;
  text-align: justify;
  scrollbar-width: thin;
  scrollbar-color: rgba(107, 68, 34, 0.3) transparent;
  pointer-events: none;
  -webkit-overflow-scrolling: touch;
}
.br-page-text::-webkit-scrollbar { width: 3px; }
.br-page-text::-webkit-scrollbar-thumb {
  background: rgba(107, 68, 34, 0.3);
  border-radius: 2px;
}

/* ─── Audio Button — sits on left page ───────────────────── */
.br-audio-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(107, 68, 34, 0.15);
  color: #6b4422;
  border: 1.5px solid rgba(107, 68, 34, 0.3);
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  cursor: pointer;
  z-index: 15;
  transition: background 0.2s, transform 0.15s;
  pointer-events: all;
}
.br-audio-btn:hover {
  background: rgba(107, 68, 34, 0.28);
  transform: scale(1.1);
}

@keyframes brPulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}
.br-audio-btn.playing {
  border-color: #2a7a50;
  color: #2a7a50;
  animation: brPulse 1.4s ease-in-out infinite;
}

/* ─── Reading Head — character image that tracks the active word ── */
#brReadingHead {
  position: absolute;
  width: 36px;
  height: auto;
  pointer-events: none;
  z-index: 30;
  display: none;
  transform: translateX(-50%);
  transition: left 0.14s cubic-bezier(0.25, 0.46, 0.45, 0.94),
              top  0.14s cubic-bezier(0.25, 0.46, 0.45, 0.94);
  filter: drop-shadow(0 3px 8px rgba(0, 0, 0, 0.4));
}

/* ─── Word Highlighting ───────────────────────────────────── */
.br-word {
  display: inline;
  border-radius: 3px;
  padding: 1px 2px;
  transition: background-color 0.08s ease, color 0.08s ease;
}
.br-word.active {
  background-color: rgba(200, 140, 30, 0.28);
  color: #7a3a00;
  text-shadow: none;
}

/* ─── Navigation Controls ────────────────────────────────── */
.br-controls {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 12px;
  flex-shrink: 0;
}

.br-nav-btn {
  background: rgba(240, 217, 181, 0.12);
  border: 1.5px solid rgba(240, 217, 181, 0.28);
  color: #f0d9b5;
  padding: 8px 20px;
  border-radius: 30px;
  font-size: 0.9rem;
  font-family: 'Fredoka', sans-serif;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 7px;
  transition: background 0.2s, border-color 0.2s, transform 0.15s;
  white-space: nowrap;
}
.br-nav-btn:hover:not(:disabled) {
  background: rgba(240, 217, 181, 0.22);
  border-color: rgba(240, 217, 181, 0.55);
  transform: scale(1.04);
}
.br-nav-btn:disabled {
  opacity: 0.25;
  cursor: not-allowed;
  transform: none;
}

/* ─── Page Counter ────────────────────────────────────────── */
.br-page-counter {
  color: rgba(240, 217, 181, 0.7);
  font-family: 'Fredoka', sans-serif;
  font-size: 0.95rem;
  min-width: 54px;
  text-align: center;
}

/* ─── Page Dots ───────────────────────────────────────────── */
.br-dots {
  display: flex;
  gap: 7px;
  margin-top: 8px;
  justify-content: center;
  flex-wrap: wrap;
  max-width: min(420px, 90vw);
  flex-shrink: 0;
}
.br-page-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(240, 217, 181, 0.25);
  cursor: pointer;
  transition: background 0.2s, transform 0.2s;
  border: 1px solid rgba(240, 217, 181, 0.15);
}
.br-page-dot:hover  { background: rgba(240, 217, 181, 0.5); }
.br-page-dot.active {
  background: #f0d9b5;
  transform: scale(1.4);
  border-color: transparent;
}

/* ─── Keyboard Hint ───────────────────────────────────────── */
.br-hint {
  color: rgba(240, 217, 181, 0.3);
  font-size: 0.7rem;
  margin-top: 8px;
  text-align: center;
  letter-spacing: 0.04em;
}

/* ─── Responsive ──────────────────────────────────────────── */

@media (max-width: 700px) and (orientation: portrait) {
  #bookReaderModal {
    justify-content: center;
    padding: calc(66px + env(safe-area-inset-top, 0px)) 8px calc(10px + env(safe-area-inset-bottom, 0px));
  }

  #bookReaderModal::after {
    content: "Rotate your phone for the best reading view";
    position: absolute;
    top: calc(42px + env(safe-area-inset-top, 0px));
    left: 50%;
    transform: translateX(-50%);
    width: max-content;
    max-width: calc(100vw - 88px);
    color: rgba(240, 217, 181, 0.88);
    background: rgba(0, 0, 0, 0.34);
    border: 1px solid rgba(240, 217, 181, 0.18);
    border-radius: 999px;
    padding: 5px 11px;
    font-family: 'Fredoka', sans-serif;
    font-size: 0.72rem;
    line-height: 1.15;
    text-align: center;
    z-index: 95;
    pointer-events: none;
  }

  .br-close {
    top: calc(8px + env(safe-area-inset-top, 0px));
    right: 10px;
    width: 36px;
    height: 36px;
    font-size: 1.2rem;
  }

  .br-title {
    width: min(88vw, 420px);
    margin-bottom: 10px;
    font-size: 0.92rem;
    line-height: 1.2;
  }

  .br-stage {
    flex: 1 1 auto;
    width: 100%;
    min-height: 0;
  }

  .br-book {
    flex-direction: row;
    aspect-ratio: 3 / 2;
    width: min(96vw, 560px);
    height: auto;
    max-height: min(58dvh, 66vw);
    filter:
      drop-shadow(0 18px 34px rgba(0, 0, 0, 0.68))
      drop-shadow(0 4px 12px rgba(0, 0, 0, 0.42));
  }

  .br-left-page {
    width: 50%;
    height: 100%;
    min-height: 0;
    border-radius: 3px 0 0 3px;
  }

  .br-left-decor {
    padding: 10px 10px 22px;
    gap: 2px;
  }

  .br-left-title {
    font-size: clamp(0.48rem, 1.7vw, 0.7rem);
    line-height: 1.2;
  }

  .br-left-ornament {
    display: block;
    font-size: clamp(0.78rem, 3vw, 1rem);
  }

  .br-left-pagenum {
    bottom: 7px;
    font-size: 0.5rem;
  }

  .br-page-text {
    padding: 2px 2px 0;
    font-size: clamp(0.5rem, 2vw, 0.72rem);
    line-height: 1.36;
    text-align: justify;
    pointer-events: none;
  }

  .br-right-page {
    width: 50%;
    height: 100%;
    background-size: cover;
    border-radius: 0 3px 3px 0;
  }

  .br-leaf {
    display: block;
    position: absolute;
    right: 0;
    top: 0;
    width: 50%;
    height: 100%;
    transform-origin: left center;
  }

  .br-book-spine { display: block; }
  .br-gutter-shadow { display: block; }

  .br-audio-btn {
    top: 6px;
    right: 6px;
    width: 24px;
    height: 24px;
    font-size: 0.66rem;
  }

  #brReadingHead {
    width: 22px;
  }

  .br-controls {
    width: min(96vw, 560px);
    justify-content: center;
    gap: 8px;
    margin-top: 9px;
  }

  .br-nav-btn  {
    min-width: 82px;
    justify-content: center;
    padding: 7px 10px;
    font-size: 0.76rem;
  }

  .br-page-counter {
    min-width: 48px;
    font-size: 0.8rem;
  }

  .br-dots {
    max-width: min(86vw, 360px);
    gap: 6px;
    margin-top: 6px;
  }

  .br-page-dot {
    width: 7px;
    height: 7px;
  }

  .br-hint     { display: none; }
}

@media (orientation: landscape) and (max-height: 500px) {
  /* Full-screen book reader on mobile landscape */
  #bookReaderModal {
    padding:
      calc(env(safe-area-inset-top, 0px) + 4px)
      calc(env(safe-area-inset-right, 0px) + 6px)
      calc(env(safe-area-inset-bottom, 0px) + 4px)
      calc(env(safe-area-inset-left, 0px) + 6px);
    gap: 0;
  }

  /* Hide chrome to maximise reading area */
  .br-title { display: none; }
  .br-dots  { display: none; }
  .br-hint  { display: none; }

  .br-close {
    top:   calc(4px + env(safe-area-inset-top, 0px));
    right: calc(6px + env(safe-area-inset-right, 0px));
    width: 30px;
    height: 30px;
    font-size: 1rem;
  }

  .br-stage {
    flex: 1;
    min-height: 0;
  }

  /* Book fills almost the entire viewport height */
  .br-book {
    height: calc(100dvh
      - env(safe-area-inset-top, 0px)
      - env(safe-area-inset-bottom, 0px)
      - 44px);   /* 44px reserved for nav controls row */
    max-height: none;
    max-width: calc(100dvw
      - env(safe-area-inset-left, 0px)
      - env(safe-area-inset-right, 0px)
      - 12px);
    aspect-ratio: unset; /* let height drive size */
  }

  .br-left-decor {
    padding: 8px 10px 18px;
    gap: 2px;
  }

  .br-page-text {
    font-size: clamp(0.62rem, 1.5vw, 0.82rem);
    line-height: 1.4;
  }

  .br-left-title {
    font-size: 0.68rem;
    line-height: 1.2;
  }

  .br-left-ornament { font-size: 0.9rem; }
  .br-left-pagenum  { bottom: 6px; }

  .br-audio-btn {
    width: 26px;
    height: 26px;
    top: 6px;
    right: 6px;
    font-size: 0.62rem;
  }

  #brReadingHead { width: 20px; }

  .br-controls {
    margin-top: 4px;
    gap: 8px;
    flex-shrink: 0;
  }

  .br-nav-btn {
    padding: 5px 12px;
    font-size: 0.74rem;
    min-width: 68px;
  }

  .br-page-counter {
    font-size: 0.76rem;
    min-width: 44px;
  }
}

@media (max-height: 600px) {
  .br-title    { margin-bottom: 6px; font-size: 0.88rem; }
  .br-dots     { margin-top: 6px; }
  .br-controls { margin-top: 8px; }
}
