/* --- Bookshelf viewer: warm, child-friendly theme --- */

:root {
  --shelf-bg: radial-gradient(ellipse at 50% 100%, #8b6914 0%, #5c3d0a 40%, #3d2806 100%);
  --shelf-ledge: #4a3520;
  --shelf-ledge-shadow: rgba(0, 0, 0, 0.4);
  --book-bg: #2c1810;
  --book-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  --book-shadow-hover: 0 16px 40px rgba(0, 0, 0, 0.5);
  --paper: #f5f0e6;
  --paper-texture: rgba(0, 0, 0, 0.02);
  --text-dark: #1a1a1a;
  --back-link: rgba(255, 255, 255, 0.85);
  --back-link-hover: #fff;
  --reader-backdrop: rgba(0, 0, 0, 0.88);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: 'Lora', Georgia, serif;
  background: var(--shelf-bg);
  background-attachment: fixed;
  color: var(--text-dark);
  overflow-x: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden {
  display: none !important;
}

/* Back to Dashboard */
.back-link {
  position: fixed;
  top: max(1rem, env(safe-area-inset-top));
  left: max(1rem, env(safe-area-inset-left));
  z-index: 50;
  color: var(--back-link);
  text-decoration: none;
  font-size: 1rem;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  transition: color 0.2s, background 0.2s;
}
.back-link:hover {
  color: var(--back-link-hover);
  background: rgba(255, 255, 255, 0.1);
}
body.reader-focus .back-link {
  display: none;
}

/* --- Bookshelf --- */
.bookshelf {
  min-height: 100vh;
  padding: max(4rem, env(safe-area-inset-top)) max(1rem, env(safe-area-inset-right)) max(3rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}

.shelf-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.shelf-row {
  display: flex;
  gap: 1.5rem;
  padding: 2rem 1rem 3rem;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
  -webkit-overflow-scrolling: touch;
}
.shelf-row::after {
  content: '';
  flex: 0 0 1px;
  scroll-snap-align: end;
}

/* Wooden ledge below each row */
.shelf-row {
  position: relative;
}
.shelf-ledge {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 24px;
  background: linear-gradient(180deg, var(--shelf-ledge) 0%, #352614 100%);
  border-radius: 0 0 8px 8px;
  box-shadow:
    0 4px 8px var(--shelf-ledge-shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.08);
  pointer-events: none;
}

/* --- Book --- */
.book {
  flex: 0 0 140px;
  width: 140px;
  scroll-snap-align: start;
  cursor: pointer;
  position: relative;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  transform: perspective(800px) rotateY(-3deg);
}
.book:hover {
  transform: perspective(800px) rotateY(-5deg) translateY(-8px);
  box-shadow: var(--book-shadow-hover);
}
.book:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.8);
  outline-offset: 4px;
}

.book-inner {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  border-radius: 4px 8px 8px 4px;
  overflow: hidden;
  box-shadow: var(--book-shadow);
  transform-style: preserve-3d;
}

.book-spine {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 12px;
  background: var(--book-bg);
  border-radius: 4px 0 0 4px;
  transform: rotateY(30deg);
  transform-origin: right center;
  z-index: 0;
}

.book-cover-wrap {
  position: absolute;
  left: 12px;
  right: 0;
  top: 0;
  bottom: 0;
  border-radius: 0 8px 8px 0;
  overflow: hidden;
  z-index: 1;
}

.book-cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  background: var(--book-bg);
}

.book-title {
  position: absolute;
  left: 12px;
  right: 0;
  bottom: 0;
  padding: 0.6rem 0.5rem 0.5rem;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.75));
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Empty state */
.empty-state {
  text-align: center;
  padding: 4rem 2rem;
  color: rgba(255, 255, 255, 0.9);
}
.empty-state p {
  margin: 0.5rem 0;
  font-size: 1.25rem;
}
.empty-state-link {
  display: inline-block;
  margin-top: 1.5rem;
  padding: 0.75rem 1.5rem;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  text-decoration: none;
  border-radius: 8px;
  font-weight: 600;
  transition: background 0.2s;
}
.empty-state-link:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* --- Book reader overlay --- */
.book-reader {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(2rem, env(safe-area-inset-top)) max(2rem, env(safe-area-inset-right)) max(2rem, env(safe-area-inset-bottom)) max(2rem, env(safe-area-inset-left));
}
.book-reader:fullscreen,
.book-reader:-webkit-full-screen {
  padding: 0;
}

.book-reader-backdrop {
  position: absolute;
  inset: 0;
  background: var(--reader-backdrop);
  opacity: 0;
  transition: opacity 0.3s ease;
}
.book-reader:not(.hidden) .book-reader-backdrop {
  opacity: 1;
}

.book-reader-inner {
  position: relative;
  width: 100%;
  max-width: 1000px;
  max-height: 90vh;
  aspect-ratio: 2 / 1.4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transform: scale(0.9);
  opacity: 0;
  transition: transform 0.4s ease-out, opacity 0.4s ease-out;
}
.book-reader:fullscreen .book-reader-inner,
.book-reader:-webkit-full-screen .book-reader-inner {
  max-height: 100vh;
}
.book-reader.visible .book-reader-inner {
  transform: scale(1);
  opacity: 1;
}
.book-reader.closing .book-reader-inner {
  transform: scale(0.9);
  opacity: 0;
}
body.reader-focus .book-reader-inner {
  max-height: 100vh;
}

/* ── Fullscreen immersive layout ──
   Image fills viewport; text + controls form a unified bottom bar.
   ┌─────────────────────────────────────┐
   │         FULL-BLEED IMAGE            │
   │                                     │
   ├─────────────────────────────────────┤
   │      Story text (full width)        │
   │  × Read to me  Exit  3/9    Zoom   │
   └─────────────────────────────────────┘
*/
body.reader-fullscreen-active .book-reader {
  padding: 0;
}
body.reader-fullscreen-active .book-reader-backdrop {
  background: #000;
}
body.reader-fullscreen-active .book-reader-inner {
  max-width: none;
  max-height: none;
  width: 100%;
  height: 100%;
  aspect-ratio: auto;
  justify-content: flex-start;
  align-items: stretch;
}
body.reader-fullscreen-active .spread-container {
  flex: 1;
  min-height: 0;
  width: 100%;
  align-self: stretch;
  perspective: none;
}
body.reader-fullscreen-active .spread {
  max-height: none;
  flex: 1;
  min-height: 0;
  border-radius: 0;
  background: #000;
  box-shadow: none;
  background-image: none;
}

/* Image fills entire viewport */
body.reader-fullscreen-active .page-left {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  flex: none;
  z-index: 0;
}
body.reader-fullscreen-active .page-left .page-face {
  padding: 0;
  background: #000;
}
body.reader-fullscreen-active .page-media-wrap,
body.reader-fullscreen-active .page-media {
  width: 100%;
  height: 100%;
}
body.reader-fullscreen-active .page-media {
  object-fit: cover;
}

/* Text: full-width overlay above toolbar */
body.reader-fullscreen-active .page-right {
  position: absolute;
  bottom: 3.25rem;
  left: 0;
  right: 0;
  flex: none;
  z-index: 1;
  height: auto;
  max-height: 40%;
  overflow: hidden;
  transform-origin: bottom center;
}
body.reader-fullscreen-active .page-right .page-face-front {
  position: relative;
  background: linear-gradient(to bottom, rgba(0,0,0,0.0) 0%, rgba(0,0,0,0.85) 15%);
  color: #fff;
  justify-content: flex-end;
  padding: 2.5rem 2rem 1rem;
  font-size: clamp(16px, 2.2vw, 24px);
  line-height: 1.6;
  text-align: center;
}
body.reader-fullscreen-active .page-right .page-text-inner {
  color: #fff;
  max-width: 72ch;
  text-align: center;
}

/* Toolbar: bottom bar flush with text overlay */
body.reader-fullscreen-active .reader-toolbar {
  position: absolute;
  top: auto;
  bottom: 0;
  left: 0;
  right: 0;
  flex-direction: row;
  justify-content: center;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1.5rem;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.92);
  z-index: 10;
}

/* Hide page dots in fullscreen; show inline counter in toolbar */
body.reader-fullscreen-active .page-dots {
  display: none;
}
body.reader-fullscreen-active .toolbar-page-counter {
  display: inline;
}

/* Narration progress bar: sits at boundary between image and text */
body.reader-fullscreen-active .narration-progress-wrap {
  position: absolute;
  bottom: 3.25rem;
  left: 0;
  right: 0;
  max-width: none;
  margin: 0;
  z-index: 3;
  border-radius: 0;
}

.reader-toolbar {
  position: absolute;
  top: max(-2.5rem, calc(-2.5rem + env(safe-area-inset-top)));
  right: 0;
  z-index: 10;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  max-width: 100%;
  justify-content: flex-end;
}
.book-reader:fullscreen .reader-toolbar,
.book-reader:-webkit-full-screen .reader-toolbar {
  top: max(0.5rem, env(safe-area-inset-top));
  right: max(0.5rem, env(safe-area-inset-right));
}
/* Focus-mode fullscreen toolbar is handled in the immersive section below */

/* Zoomed mode: toolbar at bottom, text overlay above it */
body.reader-zoomed .reader-toolbar {
  top: auto;
  bottom: 0;
  left: 0;
  right: 0;
  min-height: 3rem;
  justify-content: center;
  padding: 0.5rem 1rem;
  padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  background: rgba(0, 0, 0, 0.92);
  border-radius: 0;
  flex-direction: row;
  gap: 0.75rem;
}
/* In zoomed mode, hide page dots (toolbar handles bottom) */
body.reader-zoomed .page-dots {
  display: none;
}

/* Unified toolbar button */
.reader-btn {
  flex: 0 0 auto;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.reader-btn:hover {
  background: rgba(255, 255, 255, 0.25);
}
.reader-btn svg {
  width: 1.25rem;
  height: 1.25rem;
  display: block;
}
.reader-btn.active {
  background: rgba(255, 200, 100, 0.4);
  color: #1a1a1a;
}
.reader-close-btn {
  order: 90;
}

/* Navigation arrows */
.reader-nav {
  flex: 0 0 auto;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.reader-nav svg {
  width: 1.25rem;
  height: 1.25rem;
  display: block;
}
.reader-nav:hover {
  background: rgba(255, 255, 255, 0.25);
}
.reader-nav:disabled {
  opacity: 0.25;
  pointer-events: none;
}
.reader-nav-prev {
  order: -1;
}
.reader-nav-next {
  order: 100;
}

.toolbar-page-counter {
  display: none;
  color: rgba(255, 255, 255, 0.7);
  font-size: 0.85rem;
  white-space: nowrap;
  padding: 0 0.5rem;
}
body.reader-zoomed .toolbar-page-counter {
  display: inline;
}

.spread-container {
  width: 100%;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.narration-progress-wrap {
  width: 100%;
  max-width: 400px;
  height: 4px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
  margin-top: 0.5rem;
  overflow: hidden;
}
.narration-progress {
  height: 100%;
  width: 0%;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 2px;
  transition: width 0.1s linear;
}

.page-dots {
  margin-top: 0.75rem;
  margin-bottom: 0.5rem;
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.9rem;
}

/* Two-page spread layout with fade transition */
.spread {
  display: flex;
  width: 100%;
  height: 100%;
  max-height: 70vh;
  min-height: 0;
  background: var(--paper);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  background-image: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    var(--paper-texture) 2px,
    var(--paper-texture) 4px
  );
  transition: opacity 0.3s ease;
}
.spread.fade-out {
  opacity: 0;
}

.spread-inner {
  display: flex;
  width: 100%;
  height: 100%;
  position: relative;
}

.page {
  flex: 1;
  min-width: 0;
  position: relative;
}

.page-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  overflow: hidden;
}

.page-image .page-face {
  background: #e8e2d8;
}
.page-text .page-face {
  background: var(--paper);
  font-size: clamp(16px, 2.5vw, 24px);
  line-height: 1.7;
  text-align: center;
  justify-content: center;
  align-items: center;
}
.page-text-inner {
  overflow: auto;
  padding: 1rem;
  max-width: 56ch;
  max-height: 100%;
}

/* Zoomed (immersive) mode: media full-bleed, text overlay */
body.reader-zoomed .spread-inner {
  position: relative;
  width: 100%;
  height: 100%;
}
body.reader-zoomed .page-left {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  flex: none;
  z-index: 0;
}
body.reader-zoomed .page-left .page-face {
  padding: 0;
}
body.reader-zoomed .page-left .page-media-wrap,
body.reader-zoomed .page-left .page-media {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
body.reader-zoomed .page-left .page-media {
  object-fit: cover;
}
body.reader-zoomed .page-right {
  position: absolute;
  bottom: 3.5rem;
  left: 0;
  right: 0;
  flex: none;
  z-index: 1;
  max-height: 35%;
  border-radius: 12px 12px 0 0;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.3);
}
body.reader-zoomed .page-right .page-face {
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  justify-content: flex-start;
  padding: 1.25rem 1.5rem;
  padding-bottom: max(1.25rem, env(safe-area-inset-bottom));
  border-radius: 12px 12px 0 0;
}
body.reader-zoomed .page-right .page-text-inner {
  color: #fff;
  max-width: none;
  text-align: center;
}

.page-media-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.page-media {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}
.unmute-btn {
  position: absolute;
  bottom: 0.5rem;
  right: 0.5rem;
  width: 2rem;
  height: 2rem;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.unmute-btn:hover {
  background: rgba(0, 0, 0, 0.8);
}
.unmute-btn.hidden {
  display: none;
}

/* Reduced motion: instant transitions */
@media (prefers-reduced-motion: reduce) {
  .spread {
    transition: none;
  }
}

/* --- Responsive: single-page stack on mobile --- */
@media (max-width: 768px) {
  .book {
    flex: 0 0 110px;
    width: 110px;
  }
  .shelf-row {
    padding: 1.5rem 0.75rem 2.5rem;
    gap: 1rem;
  }
  .book-reader-inner {
    max-height: 85vh;
  }
  .spread {
    flex-direction: column;
    max-height: 75vh;
  }
  .spread-inner {
    flex-direction: column;
  }
  .page-left {
    flex: 0 0 58%;
    min-height: 0;
  }
  .page-right {
    flex: 0 0 42%;
    min-height: 0;
  }
}

/* Image loading placeholder */
.page-image .page-face {
  background: #e0dcd4;
}
.page-media.loading {
  opacity: 0;
}
.page-media.loaded {
  opacity: 1;
  transition: opacity 0.2s ease;
}

@media (prefers-reduced-motion: reduce) {
  .book,
  .book-reader-backdrop,
  .book-reader-inner {
    transition: none;
  }
  .book:hover {
    transform: perspective(800px) rotateY(-5deg) translateY(-4px);
  }
  .book-reader-inner {
    transform: scale(1);
    opacity: 1;
  }
  .book-reader.closing .book-reader-inner {
    opacity: 0;
  }
}
