:root {
  --paper: #f7f0e3;
  --ink: #3b3226;
  --muted: #8a7d6b;
  --accent: #e2574c;
  --flip-ms: 900ms;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Trebuchet MS", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background:
    radial-gradient(ellipse at top, #fffaf1 0%, transparent 60%),
    var(--paper);
  min-height: 100vh;
}

a { color: inherit; }

/* ---------- Library (pick a book) ---------- */

.library-head {
  text-align: center;
  padding: 56px 20px 8px;
}

.library-head h1 {
  font-size: clamp(2.4rem, 6vw, 4rem);
  margin: 0;
  letter-spacing: 0.01em;
}

.library-head p {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 1.15rem;
}

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 220px));
  justify-content: center;
  gap: 48px 40px;
  padding: 40px 24px 80px;
  max-width: 1100px;
  margin: 0 auto;
}

.shelf-book {
  text-decoration: none;
  text-align: center;
  display: block;
}

.shelf-cover {
  position: relative;
  background: #fff;
  border-radius: 3px 8px 8px 3px;
  overflow: hidden;
  box-shadow:
    inset 6px 0 8px -4px rgba(0, 0, 0, 0.25),
    0 2px 4px rgba(0, 0, 0, 0.08),
    0 14px 28px rgba(80, 55, 20, 0.18);
  transform-origin: left center;
  transition: transform 0.35s ease, box-shadow 0.35s ease;
}

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

.shelf-book:focus-visible .shelf-cover,
.shelf-book:active .shelf-cover {
  transform: perspective(900px) rotateY(-14deg) translateY(-4px);
  box-shadow:
    inset 6px 0 8px -4px rgba(0, 0, 0, 0.25),
    0 4px 8px rgba(0, 0, 0, 0.08),
    0 24px 40px rgba(80, 55, 20, 0.25);
}

.shelf-title {
  margin-top: 16px;
  font-weight: 700;
  font-size: 1.2rem;
}

.shelf-authors {
  color: var(--muted);
  margin-top: 2px;
}

/* ---------- Reader ---------- */

body.reader {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.reader-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
}

.reader-head h1 {
  margin: 0;
  font-size: 1.25rem;
  text-align: center;
}

.back {
  text-decoration: none;
  color: var(--muted);
  font-weight: 600;
  justify-self: start;
}

.back:hover { color: var(--ink); }

.head-right {
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 10px;
}

.fs {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 6px;
  touch-action: manipulation;
}
.fs:hover { color: var(--ink); background: rgba(0, 0, 0, 0.05); }
.fs[hidden] { display: none; }
.fs svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.fs .exit, .is-fullscreen .fs .enter { display: none; }
.is-fullscreen .fs .exit { display: inline; }

.counter {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: pinch-zoom;
}

.nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 5000;
  width: 52px;
  height: 52px;
  border: none;
  border-radius: 50%;
  background: #fff;
  color: var(--ink);
  font-size: 2.2rem;
  line-height: 1;
  padding: 0 0 4px;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(80, 55, 20, 0.2);
  transition: opacity 0.2s, transform 0.15s;
  touch-action: manipulation;
}

@media (hover: hover) {
  .shelf-book:hover .shelf-cover {
    transform: perspective(900px) rotateY(-14deg) translateY(-4px);
  }
  .nav:hover:not(:disabled) { transform: translateY(-50%) scale(1.08); }
}
.nav:disabled { opacity: 0; pointer-events: none; }
.nav.prev { left: 12px; }
.nav.next { right: 12px; }

/* The open book is two pages wide; leaves hinge on the spine in the middle. */
.book {
  position: relative;
  width: calc(var(--pw, 300px) * 2);
  height: var(--ph, 424px);
  flex-shrink: 0;
  perspective: calc(var(--pw, 300px) * 5);
  transform: translateX(var(--shift, 0));
  transition: transform var(--flip-ms) cubic-bezier(0.45, 0, 0.25, 1);
  cursor: pointer;
}

.leaf {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform var(--flip-ms) cubic-bezier(0.45, 0, 0.25, 1);
}

.leaf.flipped { transform: rotateY(-180deg); }


.no-anim, .no-anim .leaf { transition: none !important; }

.face {
  position: absolute;
  inset: 0;
  background: #fff;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(80, 55, 20, 0.18), 0 1px 3px rgba(0, 0, 0, 0.1);
}

.face.front { border-radius: 0 4px 4px 0; }
.face.back { transform: rotateY(180deg); border-radius: 4px 0 0 4px; }

.face img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

/* Soft shadow in the gutter, like the paper curving into the spine. */
.face::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
}
.face.front::before {
  background: linear-gradient(to right, rgba(0, 0, 0, 0.16), rgba(0, 0, 0, 0.04) 4%, transparent 10%);
}
.face.back::before {
  background: linear-gradient(to left, rgba(0, 0, 0, 0.16), rgba(0, 0, 0, 0.04) 4%, transparent 10%);
}
.face.cover::before { background: linear-gradient(to right, rgba(0, 0, 0, 0.08), transparent 3%); }
.face.back.cover::before { background: linear-gradient(to left, rgba(0, 0, 0, 0.08), transparent 3%); }

/* Shading that sweeps over a page while it turns. */
.face::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
}
.face.front::after { background: linear-gradient(to left, rgba(0, 0, 0, 0.28), rgba(0, 0, 0, 0.05)); }
.face.back::after { background: linear-gradient(to right, rgba(0, 0, 0, 0.28), rgba(0, 0, 0, 0.05)); }
.leaf.turning .face::after { animation: turn-shade var(--flip-ms) ease-in-out; }
.leaf.dragging .face::after,
.leaf.settling .face::after { opacity: var(--shade, 0); }
.leaf.settling .face::after { transition: opacity 0.3s; }
.book.dragging { transition: none; }

@keyframes turn-shade {
  0% { opacity: 0; }
  50% { opacity: 1; }
  100% { opacity: 0; }
}

.reader-foot {
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px max(8px, env(safe-area-inset-bottom));
}

.end-actions {
  display: flex;
  gap: 18px;
  align-items: center;
  z-index: 6000;
  animation: rise 0.5s ease both;
  animation-delay: 0.6s;
}

.end-actions[hidden] { display: none; }

.end-actions button {
  font: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  border: none;
  border-radius: 999px;
  padding: 10px 22px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(226, 87, 76, 0.35);
}

.end-actions a { color: var(--muted); font-weight: 600; }

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 700px) {
  .reader-head { padding: 8px 10px; }
  .reader-foot { min-height: 56px; }
  .counter { font-size: 0.85rem; }
  .reader-head h1 { font-size: 1.05rem; }
  .nav { width: 40px; height: 40px; font-size: 1.7rem; top: auto; bottom: 8px; transform: none; }
}


@media (prefers-reduced-motion: reduce) {
  :root { --flip-ms: 1ms; }
}
