/* ==========================================================================
   Panchatantra Storybooks
   ========================================================================== */

:root {
  --ink: #3b2a20;
  --ink-soft: #6a5444;
  --paper: #fffaf0;
  --paper-2: #fbf1dc;
  --bg: #f7ead2;
  --wood: #8b5a3c;
  --wood-dark: #5e3a24;
  --wood-light: #b07a52;
  --book: #c0392b;
  --font-display: 'Fredoka', 'Baloo 2', 'Trebuchet MS', system-ui, sans-serif;
  --font-read: 'Andika', 'Nunito', 'Segoe UI', system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-read);
  background-color: var(--bg);
  background-image:
    radial-gradient(circle at 20% 10%, rgba(255, 255, 255, .55), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(214, 160, 90, .18), transparent 45%),
    radial-gradient(rgba(139, 90, 60, .07) 1.2px, transparent 1.4px);
  background-size: auto, auto, 22px 22px;
}

a { color: inherit; }

img { max-width: 100%; }

:focus-visible { outline: 3px solid #2f80ed; outline-offset: 3px; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: .2em;
  padding: .5em 1.05em;
  border-radius: 999px;
  border: 2px solid currentColor;
  font: 600 1rem/1.1 var(--font-display);
  text-decoration: none;
  cursor: pointer;
  background: transparent;
  color: var(--ink);
  transition: transform .15s ease, background-color .15s ease, box-shadow .15s ease;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn-ghost { border-color: rgba(59, 42, 32, .25); background: rgba(255, 255, 255, .6); }
.btn-ghost:hover { background: #fff; }
.btn-solid { background: var(--book); border-color: var(--book); color: #fff; box-shadow: 0 3px 0 rgba(0, 0, 0, .2); }
.btn[aria-pressed="true"] { background: var(--book); border-color: var(--book); color: #fff; }

/* ==========================================================================
   Bookshelf
   ========================================================================== */

.site-header {
  text-align: center;
  padding: clamp(1.5rem, 4vw, 3rem) 1rem 1.25rem;
}
.site-header h1 {
  margin: 0;
  font: 700 clamp(2.1rem, 6vw, 3.6rem)/1.05 var(--font-display);
  color: #7a3e12;
  letter-spacing: .01em;
  text-shadow: 0 3px 0 #f3c97a, 0 6px 14px rgba(122, 62, 18, .25);
}
.site-header p {
  margin: .7rem auto 0;
  max-width: 34rem;
  font-size: clamp(1.05rem, 2.4vw, 1.25rem);
  color: var(--ink-soft);
}

.bookcase {
  max-width: 1240px;
  margin: 1rem auto 2rem;
  padding: 18px;
  border-radius: 18px;
  background: linear-gradient(180deg, #9b6542, #7d4c2e);
  box-shadow: 0 18px 40px rgba(77, 43, 18, .35), inset 0 2px 0 rgba(255, 255, 255, .2);
}

.shelf {
  list-style: none;
  margin: 0;
  padding: 0 0 0;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  border-radius: 8px;
  overflow: hidden;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, .25), transparent 6%, transparent 94%, rgba(0, 0, 0, .25)),
    repeating-linear-gradient(90deg, #4e2f1c 0 2px, #5a3822 2px 90px),
    #563520;
  box-shadow: inset 0 10px 24px rgba(0, 0, 0, .45);
}

.shelf-item {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 34px 22px 46px;
  perspective: 1000px;
}

/* the wooden plank each row of books stands on */
.shelf-item::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 38px;
  background: linear-gradient(180deg, #c48c5f 0, #a8714a 22%, #8b5a3c 70%, #6e4329 100%);
  box-shadow: 0 6px 10px rgba(0, 0, 0, .35);
  z-index: 0;
}

.book3d {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  max-width: 230px;
  text-decoration: none;
  border-radius: 4px 10px 10px 4px;
  transform-origin: 0 100%;
  transform: rotateY(-6deg);
  box-shadow:
    3px 3px 0 #fbf4e2, 4px 4px 0 #d9c7a2,
    6px 6px 0 #fbf4e2, 7px 7px 0 #cdb88f,
    9px 9px 0 #fbf4e2, 10px 10px 0 #c2ab80,
    14px 16px 22px rgba(0, 0, 0, .45);
  transition: transform .35s cubic-bezier(.2, .8, .2, 1.2), box-shadow .35s ease;
  animation: book-in .6s backwards cubic-bezier(.2, .8, .2, 1.1);
  animation-delay: calc(var(--i) * 60ms);
}
.book3d:hover, .book3d:focus-visible {
  transform: rotateY(-18deg) translateY(-10px) scale(1.03);
  box-shadow:
    3px 3px 0 #fbf4e2, 4px 4px 0 #d9c7a2,
    6px 6px 0 #fbf4e2, 7px 7px 0 #cdb88f,
    9px 9px 0 #fbf4e2, 10px 10px 0 #c2ab80,
    22px 26px 30px rgba(0, 0, 0, .5);
}
@keyframes book-in {
  from { opacity: 0; transform: translateY(30px) rotateY(-6deg); }
}

.book-meta {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 9px;
  z-index: 1;
  margin: 0;
  text-align: center;
  font: 600 .85rem/1 var(--font-display);
  color: #fff3dc;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .4);
}

.site-footer {
  max-width: 46rem;
  margin: 0 auto;
  padding: 0 1.25rem 2.5rem;
  text-align: center;
  font-size: .95rem;
  color: var(--ink-soft);
}

/* ==========================================================================
   Book cover (shared)
   ========================================================================== */

.cover {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 4px 10px 10px 4px;
  background: var(--book);
  container-type: inline-size;
  user-select: none;
}
.cover-art {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
/* binding crease near the spine */
.cover::before {
  content: '';
  position: absolute;
  inset: 0 auto 0 0;
  width: 8%;
  z-index: 2;
  background: linear-gradient(90deg, rgba(0, 0, 0, .38), rgba(255, 255, 255, .22) 38%, rgba(0, 0, 0, .14) 62%, transparent);
}
.cover::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 3;
  border-radius: inherit;
  pointer-events: none;
  background: linear-gradient(135deg, rgba(255, 255, 255, .22), transparent 35%);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .2);
}
.cover-banner {
  position: absolute;
  z-index: 1;
  top: 4%;
  left: 11%;
  right: 4%;
  padding: 2.6cqi 4cqi 3.2cqi;
  border-radius: 3.5cqi;
  text-align: center;
  color: #fff;
  background: var(--book);
  background: color-mix(in srgb, var(--book) 92%, transparent);
  box-shadow: 0 1cqi 0 rgba(0, 0, 0, .18), 0 2cqi 4cqi rgba(0, 0, 0, .25);
  outline: .7cqi dashed rgba(255, 255, 255, .55);
  outline-offset: -2.2cqi;
}
.cover-kicker {
  display: block;
  font: 600 3.6cqi/1 var(--font-display);
  letter-spacing: .14em;
  text-transform: uppercase;
  opacity: .92;
}
.cover-title {
  margin: 1.2cqi 0 0;
  font: 700 7.4cqi/1.06 var(--font-display);
  text-wrap: balance;
  text-shadow: 0 .5cqi 0 rgba(0, 0, 0, .22);
}
.cover-title.is-long { font-size: 6.3cqi; }

/* ==========================================================================
   Reader
   ========================================================================== */

.reader-page {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  overflow: hidden;
  background-color: #ead6b0;
  background-image:
    radial-gradient(ellipse at 50% 45%, rgba(255, 248, 230, .9), rgba(255, 248, 230, 0) 65%),
    repeating-linear-gradient(0deg, rgba(120, 72, 34, .05) 0 3px, transparent 3px 46px);
}

.reader-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .7rem clamp(.75rem, 2vw, 1.5rem);
}
.reader-title {
  margin: 0;
  flex: 1;
  text-align: center;
  font: 700 clamp(1.05rem, 2.4vw, 1.5rem)/1.15 var(--font-display);
  color: #7a3e12;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage {
  --page: min(calc((100vw - 14rem) / 2), calc(100vh - 9.5rem), 620px);
  --page: min(calc((100vw - 14rem) / 2), calc(100dvh - 9.5rem), 620px);
  position: relative;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(.5rem, 2vw, 1.5rem);
  padding: .25rem 1rem;
}

.nav {
  flex: none;
  width: 3.6rem;
  height: 3.6rem;
  border-radius: 50%;
  border: none;
  background: #fff;
  color: var(--book);
  font: 700 2.4rem/1 var(--font-display);
  padding-bottom: .2rem;
  cursor: pointer;
  box-shadow: 0 4px 0 rgba(0, 0, 0, .12), 0 8px 18px rgba(77, 43, 18, .2);
  transition: transform .15s ease, opacity .2s ease;
  z-index: 5;
}
.nav:hover:not(:disabled) { transform: scale(1.08); }
.nav:disabled { opacity: .3; cursor: default; }
.nav.is-hint { animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.12); box-shadow: 0 0 0 10px color-mix(in srgb, var(--book) 20%, transparent), 0 8px 18px rgba(77, 43, 18, .2); } }

/* ----- the open book ----- */

.book {
  position: relative;
  flex: none;
  width: calc(var(--page) * 2);
  height: var(--page);
  perspective: calc(var(--page) * 5);
  transition: transform .9s cubic-bezier(.45, .05, .3, 1);
  user-select: none;
  -webkit-user-select: none;
}
.book.is-closed { transform: translateX(-25%); }
.book.is-booting, .book.is-booting::before { transition: none; }

/* hard cover board visible around the open pages */
.book::before {
  content: '';
  position: absolute;
  inset: -1.6% -1.2% -2.6%;
  border-radius: 16px;
  background: linear-gradient(90deg, color-mix(in srgb, var(--book) 80%, black), var(--book) 6%, var(--book) 47%, color-mix(in srgb, var(--book) 60%, black) 50%, var(--book) 53%, var(--book) 94%, color-mix(in srgb, var(--book) 80%, black));
  box-shadow: 0 22px 40px rgba(60, 30, 10, .4);
  transition: opacity .5s ease .2s;
}
.book.is-closed::before { opacity: 0; transition-delay: 0s; }

.page, .face {
  position: absolute;
  top: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
}
.page { width: 50%; }
.page-left { left: 0; }
.page-right { left: 50%; }

.page-left, .face[data-kind="art"], .face[data-kind="end"] {
  border-radius: 10px 2px 2px 10px;
  box-shadow: -3px 0 0 #efe3c8, -5px 2px 0 #d8c7a3;
}
.page-right, .face[data-kind="text"], .face[data-kind="moral"] {
  border-radius: 2px 10px 10px 2px;
  box-shadow: 3px 0 0 #efe3c8, 5px 2px 0 #d8c7a3;
}

/* gutter shading where the pages meet the spine */
.page-left::after, .page-right::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: 12%;
  pointer-events: none;
  z-index: 2;
}
.page-left::after { right: 0; background: linear-gradient(270deg, rgba(60, 30, 10, .28), rgba(60, 30, 10, .08) 40%, transparent); }
.page-right::after { left: 0; background: linear-gradient(90deg, rgba(60, 30, 10, .28), rgba(60, 30, 10, .08) 40%, transparent); }

.page.is-empty, .page[data-kind="cover"], .face[data-kind="cover"] {
  background: transparent;
  box-shadow: none;
}
.page.is-empty::after, .page[data-kind="cover"]::after { display: none; }

.page[data-kind="cover"], .face[data-kind="cover"] { border-radius: 4px 14px 14px 4px; }
.page[data-kind="cover"] .cover, .face[data-kind="cover"] .cover {
  height: 100%;
  border-radius: 4px 14px 14px 4px;
}
.page[data-kind="cover"] { overflow: visible; cursor: pointer; }
.page[data-kind="cover"] .cover {
  box-shadow:
    4px 3px 0 #fbf4e2, 6px 5px 0 #d9c7a2, 9px 7px 0 #fbf4e2, 11px 9px 0 #c2ab80,
    20px 26px 40px rgba(60, 30, 10, .45);
}

.page-right:not(.is-empty):not([data-kind="cover"]) { cursor: e-resize; }
.page-left:not(.is-empty) { cursor: w-resize; }

/* ----- page turning ----- */

.flipper {
  display: none;
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  z-index: 10;
  transform-style: preserve-3d;
}
.flipper.flip-next, .flipper.flip-prev { display: block; }
.flipper.flip-next { left: 50%; transform-origin: 0 50%; }
.flipper.flip-prev { left: 0; transform-origin: 100% 50%; }
.flipper.go { transition: transform .9s cubic-bezier(.45, .05, .3, 1); }
.flipper.flip-next.go { transform: rotateY(-180deg); }
.flipper.flip-prev.go { transform: rotateY(180deg); }

.face {
  inset: 0;
  width: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.face-back { transform: rotateY(180deg); }
.face::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  z-index: 4;
  border-radius: inherit;
}
.flip-next .face-front::after, .flip-prev .face-back::after { background: linear-gradient(90deg, rgba(40, 20, 5, .35), transparent 70%); }
.flip-next .face-back::after, .flip-prev .face-front::after { background: linear-gradient(270deg, rgba(40, 20, 5, .35), transparent 70%); }
.flipper.go .face::after { animation: flip-shade .9s ease; }
@keyframes flip-shade { 45%, 55% { opacity: 1; } }

/* ----- illustration page ----- */

.art { margin: 0; width: 100%; height: 100%; }
.art img { display: block; width: 100%; height: 100%; object-fit: cover; }

/* ----- text page ----- */

.text-page {
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 11% 11% 14%;
  background:
    radial-gradient(circle at 100% 0, color-mix(in srgb, var(--book) 10%, transparent), transparent 35%),
    radial-gradient(circle at 0 100%, color-mix(in srgb, var(--book) 8%, transparent), transparent 35%),
    linear-gradient(180deg, var(--paper), var(--paper-2));
}
.text-page::before {
  content: '';
  position: absolute;
  inset: 4.5%;
  border: 2px dashed color-mix(in srgb, var(--book) 32%, transparent);
  border-radius: 18px;
  pointer-events: none;
}
.story-text {
  margin: 0;
  font-size: calc(var(--page) * var(--fs, .047));
  line-height: 1.6;
  text-wrap: pretty;
}
.is-first .story-text::first-letter {
  float: left;
  margin: .06em .1em 0 0;
  font: 700 3.1em/.85 var(--font-display);
  color: var(--book);
}
.folio {
  position: absolute;
  left: 50%;
  bottom: 6.5%;
  transform: translateX(-50%);
  min-width: 2.1em;
  height: 2.1em;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--book);
  color: #fff;
  font: 700 calc(var(--page) * .032)/1 var(--font-display);
}
.w { border-radius: .25em; transition: background-color .12s ease, box-shadow .12s ease; }
.w.is-spoken { background: #ffe58a; box-shadow: 0 0 0 .12em #ffe58a; }

/* ----- the end + moral ----- */

.end-page, .moral-page {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 8%;
  background: linear-gradient(180deg, var(--paper), var(--paper-2));
}
.end-frame {
  width: 62%;
  aspect-ratio: 1;
  border-radius: 50%;
  overflow: hidden;
  border: calc(var(--page) * .016) solid var(--book);
  box-shadow: 0 0 0 calc(var(--page) * .012) #fff, 0 10px 24px rgba(60, 30, 10, .25);
}
.end-frame img { display: block; width: 100%; height: 100%; object-fit: cover; }
.the-end {
  margin: .5em 0 0;
  font: 700 calc(var(--page) * .1)/1 var(--font-display);
  color: var(--book);
  text-shadow: 0 3px 0 rgba(0, 0, 0, .12);
}
.moral-kicker {
  margin: 0 0 .6em;
  font: 600 calc(var(--page) * .034)/1.2 var(--font-display);
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--book);
}
.moral-kicker::before, .moral-kicker::after { content: ' ✦ '; opacity: .7; }
.moral {
  margin: 0 0 1.2em;
  font-size: calc(var(--page) * .05);
  line-height: 1.5;
  font-weight: 700;
  text-wrap: balance;
}
.end-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: .6rem; }
.end-actions .btn { font-size: calc(var(--page) * .03); }
.next-title { margin: 1.1em 0 0; font-size: calc(var(--page) * .028); color: var(--ink-soft); }

/* ----- progress dots ----- */

.progress {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: .45rem;
  padding: .6rem 1rem 1rem;
}
.dot {
  width: .8rem;
  height: .8rem;
  padding: 0;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--book) 70%, transparent);
  background: #fff;
  cursor: pointer;
  transition: transform .2s ease, background-color .2s ease;
}
.dot-end { border-radius: 3px; }
.dot.is-current { background: var(--book); transform: scale(1.35); }

.noscript { text-align: center; padding: 2rem; }

/* ----- one page at a time (phones, portrait tablets) ----- */

.book.is-single {
  width: min(100%, 560px);
  height: auto;
  perspective: none;
  transform: none;
}
.book.is-single::before { display: none; }
.book.is-single .page-left { display: none; }
.book.is-single .page-right {
  position: relative;
  left: 0;
  width: 100%;
  height: auto;
  border-radius: 14px;
  box-shadow: 0 10px 26px rgba(60, 30, 10, .25);
}
.book.is-single .page-right::after { display: none; }
.book.is-single .page-right[data-kind="cover"] { box-shadow: none; }
.book.is-single .page-right[data-kind="cover"] .cover { aspect-ratio: 1; height: auto; }

.single-page { --page: min(100vw - 1.5rem, 560px); }
.single-page .art { height: auto; aspect-ratio: 1; }
.single-page .text-page { height: auto; padding: 1.6rem 1.5rem 3.6rem; }
.single-page .text-page::before { inset: .6rem; }
.single-page .story-text { font-size: clamp(1.1rem, 4.6vw, 1.4rem); }
.single-page .folio { font-size: 1rem; bottom: 1.1rem; }
.single-end .end-page { padding: 2rem 1.5rem 0; }
.single-end .end-frame { width: 55%; }
.single-end .moral-page { padding: 1.5rem 1.5rem 2rem; }
.single-end .the-end { font-size: 2.6rem; }
.single-end .moral-kicker { font-size: .95rem; }
.single-end .moral { font-size: 1.3rem; }
.single-end .end-actions .btn, .single-end .next-title { font-size: 1rem; }

.slide-from-right { animation: slide-r .45s cubic-bezier(.2, .8, .2, 1); }
.slide-from-left { animation: slide-l .45s cubic-bezier(.2, .8, .2, 1); }
@keyframes slide-r { from { opacity: 0; transform: translateX(40px); } }
@keyframes slide-l { from { opacity: 0; transform: translateX(-40px); } }

@media (max-width: 820px), (orientation: portrait) and (max-width: 1100px) {
  .reader-page { overflow: auto; height: auto; min-height: 100dvh; }
  .stage {
    align-items: flex-start;
    padding: .25rem .75rem 5.5rem;
  }
  .nav {
    position: fixed;
    bottom: 3.4rem;
    width: 3.4rem;
    height: 3.4rem;
  }
  .nav-prev { left: .9rem; }
  .nav-next { right: .9rem; }
  .progress {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    padding: .7rem 4.5rem;
    background: linear-gradient(0deg, rgba(234, 214, 176, .95), rgba(234, 214, 176, 0));
  }
}

@media (max-width: 900px) {
  .shelf { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .btn-label { display: none; }
  .shelf { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .shelf-item { padding: 26px 14px 40px; }
  .bookcase { margin-inline: .6rem; padding: 10px; }
}

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