/* ───────────────────────────────────────────────────────────────────────────
   Palette is pulled from the illustration style: snowy pale blues for the
   ground, warm bear-brown and honey for the subjects, cream for paper.
   ─────────────────────────────────────────────────────────────────────────── */
:root {
  --snow-100: #f4fafe;
  --snow-200: #e3f1fb;
  --snow-300: #cbe4f6;
  --ice-400: #9ec9e8;

  --deep-700: #1d5081;
  --deep-800: #143a5e;
  --deep-900: #0e2942;

  --bear-500: #b5713f;
  --honey-400: #f0a04b;
  --honey-300: #f7c07a;

  --paper: #fdf9f2;
  --ink: #16334f;
  --ink-soft: #4d6b86;

  --radius-lg: 22px;
  --radius-md: 14px;
  --shadow-card: 0 18px 38px -18px rgba(20, 58, 94, 0.45);
  --shadow-book: 0 40px 90px -30px rgba(0, 0, 0, 0.75);

  --font-display: 'Baloo Bhaijaan 2', 'Tajawal', 'Geeza Pro', system-ui, sans-serif;
  --font-body: 'Tajawal', 'Geeza Pro', system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--snow-100);
}

/* ── Library ─────────────────────────────────────────────────────────────── */

.library {
  min-height: 100vh;
  padding: clamp(2rem, 5vw, 4.5rem) clamp(1rem, 5vw, 4rem) 3rem;
  background:
    radial-gradient(ellipse 70% 50% at 80% -10%, var(--snow-300), transparent 70%),
    radial-gradient(ellipse 60% 45% at 10% 5%, #fff3e2, transparent 70%),
    linear-gradient(180deg, var(--snow-100), #ffffff 55%);
}

.library__header {
  max-width: 62rem;
  margin: 0 auto clamp(2rem, 4vw, 3.5rem);
  text-align: center;
}

.library__eyebrow {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: var(--bear-500);
}

.library__title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(2.4rem, 6vw, 4rem);
  line-height: 1.15;
  color: var(--deep-800);
}

.library__tagline {
  margin: 0.75rem 0 0;
  font-size: clamp(1rem, 2vw, 1.15rem);
  color: var(--ink-soft);
}

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 19rem), 1fr));
  gap: clamp(1.25rem, 3vw, 2rem);
  max-width: 72rem;
  margin: 0 auto;
}

.story-card {
  display: flex;
  flex-direction: column;
  padding: 0;
  border: 1px solid rgba(29, 80, 129, 0.1);
  border-radius: var(--radius-lg);
  background: #fff;
  box-shadow: var(--shadow-card);
  text-align: right;
  font: inherit;
  color: inherit;
  cursor: pointer;
  overflow: hidden;
  transition: transform 0.25s ease, box-shadow 0.25s ease;
}

.story-card:hover,
.story-card:focus-visible {
  transform: translateY(-6px);
  box-shadow: 0 26px 48px -20px rgba(20, 58, 94, 0.55);
  outline: none;
}

.story-card:focus-visible {
  outline: 3px solid var(--honey-400);
  outline-offset: 3px;
}

.story-card__cover {
  position: relative;
  aspect-ratio: 1 / 1;
  background: var(--snow-200);
  overflow: hidden;
}

.story-card__cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.4s ease;
}

.story-card:hover .story-card__cover img {
  transform: scale(1.04);
}

.story-card__cover--empty {
  display: grid;
  place-items: center;
  font-size: 3rem;
  color: var(--ice-400);
  background: linear-gradient(160deg, var(--snow-200), #fff);
}

.story-card__body {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 1.25rem 1.35rem 1.5rem;
  flex: 1;
}

.story-card__cover-title {
  position: absolute;
  inset: 0 0 auto;
  padding: 1rem 1.25rem 2.5rem;
  background: linear-gradient(#fff9e9 15%, rgba(255, 249, 233, 0.85) 65%, transparent);
  color: #334f39;
  font-family: var(--font-display);
  font-size: clamp(1.4rem, 3vw, 2rem);
  line-height: 1.35;
  text-align: center;
}

.story-card__title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.45rem;
  line-height: 1.3;
  color: var(--deep-800);
}

.story-card__author {
  margin: 0;
  font-size: 0.92rem;
  color: var(--bear-500);
}

.story-card__summary {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.7;
  color: var(--ink-soft);
  flex: 1;
}

.story-card__meta {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-top: 0.35rem;
}

.chip {
  padding: 0.28rem 0.7rem;
  border-radius: 999px;
  background: var(--snow-200);
  color: var(--deep-700);
  font-size: 0.8rem;
  font-weight: 500;
}

.chip--accent {
  background: #fdeed9;
  color: #a2651f;
}

.story-card--soon {
  cursor: default;
  box-shadow: none;
  background: #fbfdff;
  border-style: dashed;
}

.story-card--soon:hover {
  transform: none;
  box-shadow: none;
}

.badge-soon {
  position: absolute;
  inset-block-start: 0.85rem;
  inset-inline-start: 0.85rem;
  padding: 0.3rem 0.8rem;
  border-radius: 999px;
  background: var(--deep-800);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 500;
}

.library__footer {
  margin: clamp(2.5rem, 6vw, 4rem) auto 0;
  text-align: center;
  font-size: 0.9rem;
  color: var(--ink-soft);
}

.library__footer p {
  margin: 0;
}

/* ── Reader ──────────────────────────────────────────────────────────────── */

.reader {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 0.5rem;
  padding: clamp(0.75rem, 2vw, 1.5rem);
  background:
    radial-gradient(ellipse 60% 60% at 50% 40%, #1c4a76, transparent 75%),
    linear-gradient(180deg, var(--deep-900), #071c30);
  color: #eaf4fb;
}

.reader[hidden] {
  display: none;
}

.reader__topbar,
.reader__bottombar {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.reader__topbar {
  justify-content: space-between;
  flex-wrap: wrap;
  padding-inline: clamp(0.25rem, 2vw, 1rem);
}

.reader__heading {
  min-width: 0;
}

.reader__title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(1.15rem, 3vw, 1.7rem);
  color: #fff;
}

.reader__author {
  margin: 0.15rem 0 0;
  font-size: 0.88rem;
  color: var(--ice-400);
}

.reader__actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font: inherit;
  text-decoration: none;
  border-radius: 999px;
  border: 1px solid transparent;
  padding: 0.55rem 1.1rem;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.btn[hidden] {
  display: none;
}

.btn--ghost {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.22);
  color: #eaf4fb;
}

.btn--ghost:hover {
  background: rgba(255, 255, 255, 0.18);
}

.reader__stage {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(0.5rem, 2vw, 1.75rem);
  min-height: 0;
}

/* Takes the width left over by the arrows, so .book has something definite to
   size itself against — without this the book collapses to its minimum. */
.book-frame {
  position: relative;
  display: grid;
  place-items: center;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
}

/* Default to the original square pages. Captioned stories supply their page
   ratio so both portrait pages and open spreads fit the available height. */
.book {
  --page-max: 433px;
  --page-ratio: 1;
  --stage-height: calc(100dvh - 14rem);
  width: min(100%, calc(var(--page-max) * 2), calc(var(--stage-height) * 2 / var(--page-ratio)));
  filter: drop-shadow(0 30px 60px rgba(0, 0, 0, 0.55));
}

.book.is-loading {
  visibility: hidden;
}

/* PageFlip sets width/max-width inline; retain the height cap for tall pages. */
.reader--captioned .book {
  max-width: min(100%, calc(var(--page-max) * 2), calc(var(--stage-height) * 2 / var(--page-ratio))) !important;
}

.book__loading {
  position: absolute;
  margin: 0;
  color: var(--ice-400);
  font-size: 0.95rem;
}

.book__loading[hidden] {
  display: none;
}

.page {
  position: relative;
  container-type: inline-size;
  background: var(--paper);
  overflow: hidden;
}

.page__watermark {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(5, 1fr);
  align-items: center;
  justify-items: center;
  pointer-events: none;
  user-select: none;
  opacity: 0.2;
  color: #26342b;
  text-shadow: 0 1px 1px #fff;
  font-family: var(--font-body);
  font-size: clamp(12px, 4.5cqw, 22px);
  font-weight: 700;
}

.page__watermark span {
  white-space: nowrap;
  transform: rotate(-30deg);
}

.page img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
}

.page--blank {
  background: var(--paper);
}

.page--captioned {
  container-type: inline-size;
  color: #334733;
  background: #fffaf0;
}

.page__content {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.page--captioned img {
  flex: none;
  height: auto;
  aspect-ratio: 1;
}

.page__caption {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  gap: 0.4em;
  padding: 4% 6% 8%;
  border-top: 1px solid #eadfc7;
  font-family: var(--font-body);
  font-size: clamp(14px, 4.6cqw, 20px);
  font-weight: 500;
  line-height: 1.55;
  text-align: center;
}

.page__caption p {
  margin: 0;
  white-space: pre-line;
}

.page__number {
  position: absolute;
  bottom: 1.3%;
  left: 0;
  right: 0;
  font-size: clamp(11px, 3cqw, 13px);
  text-align: center;
  color: #857550;
}

.page--cover .page__caption p:first-child,
.page--ending .page__caption {
  font-family: var(--font-display);
  font-size: 7.5cqw;
  font-weight: 700;
  color: #3f613b;
}

.page--lessons img {
  position: absolute;
  height: 100%;
  object-fit: cover;
  opacity: 0.3;
}

.page--lessons .page__caption {
  position: relative;
  gap: 3cqw;
  padding: 6% 6% 9%;
  border: 0;
  font-size: clamp(14px, 4.4cqw, 19px);
}

.page--lessons .page__caption p {
  padding: 3cqw 4cqw;
  border: 1px solid #dbdbc5;
  border-radius: 35% 25% 30% 20% / 22px 28px 24px 30px;
  background: rgba(255, 253, 246, 0.96);
  box-shadow: 0 3px 10px rgba(67, 89, 54, 0.08);
}

.nav-arrow {
  flex: none;
  display: grid;
  place-items: center;
  width: clamp(2.6rem, 6vw, 3.4rem);
  height: clamp(2.6rem, 6vw, 3.4rem);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  color: #eaf4fb;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.2s ease, opacity 0.2s ease;
}

.nav-arrow:hover:not(:disabled) {
  background: var(--honey-400);
  color: var(--deep-900);
  transform: scale(1.08);
}

.nav-arrow:disabled {
  opacity: 0.28;
  cursor: default;
}

.nav-arrow svg {
  width: 55%;
  height: 55%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.reader__bottombar {
  flex-direction: column;
  gap: 0.5rem;
  padding-block-end: 0.25rem;
}

.progress {
  width: min(100%, 26rem);
  height: 5px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  overflow: hidden;
}

.progress__fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--honey-300), var(--honey-400));
  transition: width 0.35s ease;
}

.reader__counter {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 500;
  color: #fff;
}

.reader__hint {
  margin: 0;
  font-size: 0.82rem;
  color: var(--ice-400);
}

body.is-reading {
  overflow: hidden;
}

@media (max-width: 640px), (max-height: 700px) {
  .reader--captioned {
    display: block;
    overflow-y: auto;
  }

  .reader--captioned .reader__topbar {
    margin-bottom: 1rem;
  }

  .reader--captioned .reader__stage {
    display: block;
    position: relative;
    padding-bottom: 3.5rem;
  }

  .reader--captioned .reader__bottombar {
    margin-top: 0.75rem;
  }

  .reader--captioned .book {
    max-width: min(100%, var(--page-max)) !important;
  }

  .reader--captioned .nav-arrow {
    position: absolute;
    bottom: 0;
  }

  .reader--captioned #btn-prev {
    right: calc(50% - 4rem);
  }

  .reader--captioned #btn-next {
    left: calc(50% - 4rem);
  }

}

@media (max-width: 640px) {
  .reader__hint {
    display: none;
  }
}

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