/* ============================================================================
   Story Time — kid-friendly claymorphism UI
   Soft 3D, chunky, rounded. Responsive + iPad touch optimized.
   ========================================================================== */

:root {
  /* Palette — warm, playful, high-contrast text */
  --bg: #fff6e9;
  --bg-2: #ffeede;
  --surface: #ffffff;
  --ink: #3a2e4d;        /* body text — high contrast on cream/white */
  --ink-soft: #7a6b8c;
  --primary: #ff7eb6;    /* bubblegum */
  --primary-dark: #f4589b;
  --accent: #5ab2ff;     /* sky */
  --accent-dark: #2f93f0;
  --sun: #ffb443;        /* amber CTA */
  --sun-dark: #f59a1f;
  --clay-shadow: rgba(58, 46, 77, 0.16);
  --highlight: rgba(255, 255, 255, 0.7);

  /* Spacing rhythm (4/8) */
  --r-md: 18px;
  --r-lg: 26px;
  --r-pill: 999px;

  /* Safe areas (notch / home indicator) */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100dvh;
  font-family: "Baloo 2", "Comic Sans MS", system-ui, sans-serif;
  color: var(--ink);
  background: radial-gradient(120% 90% at 50% -10%, var(--bg-2), var(--bg));
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#app, .screen { min-height: 100dvh; }
.hidden { display: none !important; }
.icon { display: inline-flex; }
.icon svg { width: 1em; height: 1em; }

/* Chunky clay press feedback (color/shadow only — no layout shift) */
.book-card, .nav-btn, .pill-btn, .replay-btn, .add-book-link {
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 180ms ease-out, box-shadow 180ms ease-out, background 180ms ease-out;
}
.book-card:active, .nav-btn:active, .pill-btn:active,
.replay-btn:active, .add-book-link:active {
  transform: translateY(4px);
}
:focus-visible { outline: 4px solid var(--accent); outline-offset: 3px; border-radius: 8px; }

/* ===================== Library ===================== */
#library {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(var(--safe-t) + 2rem) calc(var(--safe-r) + 1.25rem)
           calc(var(--safe-b) + 2rem) calc(var(--safe-l) + 1.25rem);
}
.library-header { text-align: center; margin-bottom: 2rem; }
.app-title {
  font-size: clamp(2.2rem, 6vw, 3.4rem);
  font-weight: 800;
  margin: 0;
  color: var(--primary-dark);
  text-shadow: 0 3px 0 var(--highlight), 0 5px 0 var(--clay-shadow);
}
.app-subtitle { font-size: clamp(1rem, 2.5vw, 1.35rem); margin: 0.4rem 0 0; color: var(--ink-soft); }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: clamp(1rem, 3vw, 1.75rem);
  width: 100%;
  max-width: 1100px;
}
.book-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.85rem;
  padding: 0.9rem 0.9rem 1.1rem;
  background: var(--surface);
  border: none;
  border-radius: var(--r-lg);
  font: inherit;
  color: inherit;
  box-shadow: 0 9px 0 var(--clay-shadow), inset 0 2px 0 var(--highlight);
}
.book-card:hover { transform: translateY(-3px); box-shadow: 0 12px 0 var(--clay-shadow), inset 0 2px 0 var(--highlight); }
.book-card:active { box-shadow: 0 5px 0 var(--clay-shadow), inset 0 2px 0 var(--highlight); }

.book-cover {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  border-radius: var(--r-md);
  overflow: hidden;
  background: linear-gradient(160deg, var(--bg-2), #ffe3c6);
  display: grid;
  place-items: center;
}
.book-cover img {
  width: 100%; height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity 300ms ease-out;
}
.book-cover img.loaded { opacity: 1; }
.cover-fallback { font-size: 2.6rem; color: var(--primary); }
.book-cover img.loaded + .cover-fallback { display: none; }
.book-cover.cover-failed img { display: none; }

.book-title { font-size: 1.1rem; font-weight: 700; text-align: center; line-height: 1.25; }
.empty { grid-column: 1 / -1; text-align: center; font-size: 1.15rem; color: var(--ink-soft); }

.add-book-link {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin-top: 2.25rem;
  padding: 0.7rem 1.6rem;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--accent-dark);
  text-decoration: none;
  background: var(--surface);
  border-radius: var(--r-pill);
  box-shadow: 0 6px 0 var(--clay-shadow), inset 0 2px 0 var(--highlight);
}
.add-book-link .icon { font-size: 1.3rem; }

/* ===================== Reader ===================== */
#reader { display: flex; flex-direction: column; }
.reader-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: calc(var(--safe-t) + 0.6rem) calc(var(--safe-r) + 0.9rem)
           0.6rem calc(var(--safe-l) + 0.9rem);
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 0 var(--primary-dark);
}
.reader-title {
  flex: 1;
  margin: 0;
  font-size: clamp(1.1rem, 3vw, 1.5rem);
  font-weight: 700;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.page-indicator {
  font-size: 1.05rem;
  font-weight: 700;
  min-width: 4.5rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.pill-btn {
  display: grid;
  place-items: center;
  width: 48px; height: 48px;
  flex: 0 0 auto;
  font-size: 1.5rem;
  color: #fff;
  background: rgba(255, 255, 255, 0.22);
  border: none;
  border-radius: 16px;
}

.stage {
  flex: 1;
  display: flex;
  align-items: stretch;          /* let the page area fill full height */
  justify-content: center;
  gap: 0.5rem;
  padding: 1rem calc(var(--safe-r) + 0.5rem) 1rem calc(var(--safe-l) + 0.5rem);
  min-height: 0;
}
.canvas-wrap {
  flex: 1;
  display: flex;
  align-items: center;           /* center the page within the full-height area */
  justify-content: center;
  min-width: 0;
  min-height: 0;
}
.nav-btn { align-self: center; } /* keep arrows vertically centered, not stretched */
#pdf-canvas {
  border-radius: 14px;
  box-shadow: 0 10px 0 var(--clay-shadow), 0 4px 22px rgba(58, 46, 77, 0.2);
  background: #fff;
  max-width: 100%;
  max-height: 100%;
}

.nav-btn {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: clamp(3.25rem, 8vw, 4.5rem);
  height: clamp(3.25rem, 8vw, 4.5rem);
  font-size: clamp(1.6rem, 4vw, 2.2rem);
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  box-shadow: 0 7px 0 var(--accent-dark), inset 0 2px 0 var(--highlight);
}
.nav-btn:active { box-shadow: 0 3px 0 var(--accent-dark), inset 0 2px 0 var(--highlight); }
.nav-btn:disabled { opacity: 0.35; box-shadow: none; transform: none; cursor: default; }

.reader-footer {
  display: flex;
  justify-content: center;
  padding: 0.75rem 1rem calc(var(--safe-b) + 0.9rem);
}
.replay-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.55rem;
  font: inherit;
  font-size: 1.25rem;
  font-weight: 700;
  color: #fff;
  background: var(--sun);
  border: none;
  border-radius: var(--r-pill);
  padding: 0.7rem 2rem;
  box-shadow: 0 7px 0 var(--sun-dark), inset 0 2px 0 var(--highlight);
}
.replay-btn:active { box-shadow: 0 3px 0 var(--sun-dark), inset 0 2px 0 var(--highlight); }
.replay-btn .icon { font-size: 1.35rem; }

/* ===================== Upload page ===================== */
#upload-view {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: calc(var(--safe-t) + 2rem) calc(var(--safe-r) + 1.25rem)
           calc(var(--safe-b) + 2rem) calc(var(--safe-l) + 1.25rem);
}
.upload-card {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  width: 100%;
  max-width: 460px;
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: 0 9px 0 var(--clay-shadow), inset 0 2px 0 var(--highlight);
  padding: 1.75rem;
}
.field { display: flex; flex-direction: column; gap: 0.45rem; font-weight: 700; }
.field span { display: inline-flex; align-items: center; gap: 0.4rem; }
.field .icon { color: var(--primary); }
.field input {
  font: inherit;
  min-height: 48px;
  padding: 0.6rem 0.9rem;
  border: 2px solid #f0e2ef;
  border-radius: 14px;
  background: #fdfbff;
}
.field input:focus-visible { border-color: var(--accent); outline: none; }
.selection { min-height: 1.2rem; font-weight: 700; color: var(--primary-dark); margin: 0; }
.back-link { display: inline-block; margin-top: 0.75rem; text-align: center; color: var(--ink-soft); font-weight: 600; }

.progress { width: 100%; max-width: 460px; margin-top: 1.5rem; }
.bar { height: 16px; background: #f2e2ec; border-radius: var(--r-pill); overflow: hidden; box-shadow: inset 0 2px 4px var(--clay-shadow); }
.bar-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), var(--primary)); transition: width 220ms ease-out; }
.log { list-style: none; padding: 0; margin: 0.85rem 0 0; font-size: 0.95rem; max-height: 240px; overflow-y: auto; }
.log li { display: flex; align-items: center; gap: 0.4rem; padding: 0.15rem 0; }
.log .ok { color: #2f9e54; }
.log .err { color: #d1445a; }

/* ===================== Responsive ===================== */
@media (min-width: 768px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
}
@media (min-width: 1024px) {           /* iPad landscape / desktop */
  .stage { gap: 1.5rem; padding-inline: calc(var(--safe-l) + 1.5rem); }
  .book-grid { gap: 2rem; }
}
@media (max-width: 380px) {
  .nav-btn { width: 3rem; height: 3rem; }
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); }
}

/* Landscape phones/tablets: keep the page tall, shrink chrome */
@media (orientation: landscape) and (max-height: 560px) {
  .reader-bar { padding-top: calc(var(--safe-t) + 0.3rem); }
  .app-title { font-size: 2rem; }
}

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