/* The interface stays out of the way. The book is the only thing that matters. */

:root {
  --cream: #f6e4d4;
  --cream-dim: rgba(246, 228, 212, 0.58);
  --ink: #05090f;
  --serif: Georgia, 'Iowan Old Style', 'Times New Roman', serif;
  --sans: ui-sans-serif, system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--ink);
  color: var(--cream);
  font-family: var(--sans);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}
#scene:active { cursor: grabbing; }

/* ---------- loading ---------- */
#loader {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 0.9rem;
  background: var(--ink);
  z-index: 40;
  transition: opacity 0.8s var(--ease), visibility 0.8s;
}
#loader h1 {
  font-family: var(--serif);
  font-weight: 400;
  font-size: clamp(1.5rem, 4vw, 2.4rem);
  margin: 0;
  letter-spacing: 0.01em;
}
.track {
  width: min(240px, 52vw);
  height: 2px;
  background: rgba(246, 228, 212, 0.14);
  overflow: hidden;
}
#bar { height: 100%; width: 0; background: var(--cream); transition: width 0.3s linear; }
.tiny {
  font-size: 0.72rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--cream-dim);
  margin: 0;
}
.err { font-family: var(--serif); text-align: center; line-height: 1.7; }

/* ---------- the invitation ---------- */
#enter {
  position: fixed;
  left: 0; right: 0;
  bottom: clamp(1.1rem, 4vh, 2.4rem);
  display: grid;
  justify-items: center;
  gap: 1rem;
  z-index: 30;
  transition: opacity 0.7s var(--ease), transform 0.7s var(--ease);
}
.open {
  position: relative;
  appearance: none;
  border: 0;
  background: none;
  color: var(--cream);
  font-family: var(--serif);
  font-size: clamp(1rem, 2.4vw, 1.25rem);
  letter-spacing: 0.04em;
  padding: 0.85rem 2.2rem;
  cursor: pointer;
}
.open .ring {
  position: absolute;
  inset: 0;
  border: 1px solid rgba(246, 228, 212, 0.35);
  border-radius: 999px;
  transition: border-color 0.4s var(--ease), transform 0.4s var(--ease);
}
.open::after {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: 999px;
  border: 1px solid rgba(246, 228, 212, 0.16);
  animation: breathe 3.6s ease-in-out infinite;
}
@keyframes breathe {
  0%, 100% { transform: scale(1);    opacity: 0.5; }
  50%      { transform: scale(1.045); opacity: 0.1; }
}
.open:hover .ring { border-color: var(--cream); transform: scale(1.02); }
.hintline { opacity: 0.75; }

/* ---------- controls ---------- */
#hud {
  position: fixed;
  top: max(1rem, env(safe-area-inset-top));
  right: max(1rem, env(safe-area-inset-right));
  display: flex;
  gap: 0.45rem;
  z-index: 25;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.6s var(--ease);
}
body.reading #hud { opacity: 1; pointer-events: auto; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 0.42rem;
  padding: 0.42rem 0.75rem;
  border-radius: 999px;
  border: 1px solid rgba(246, 228, 212, 0.18);
  background: rgba(8, 12, 20, 0.42);
  backdrop-filter: blur(9px);
  color: var(--cream-dim);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: color 0.3s, border-color 0.3s, background 0.3s;
}
.chip svg {
  width: 15px; height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.chip svg path:first-child { fill: currentColor; stroke-width: 1.2; }
.chip .w1, .chip .w2 { opacity: 0; transition: opacity 0.3s; fill: none !important; }
.chip:hover { color: var(--cream); border-color: rgba(246, 228, 212, 0.4); }
.chip.on {
  color: var(--cream);
  border-color: rgba(246, 228, 212, 0.65);
  background: rgba(246, 228, 212, 0.1);
}
.chip.on .w1, .chip.on .w2 { opacity: 1; }

.page-arrow {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: 44px; height: 44px;
  display: grid;
  place-content: center;
  border: 0;
  border-radius: 50%;
  background: rgba(8, 12, 20, 0.3);
  color: var(--cream-dim);
  cursor: pointer;
  z-index: 25;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s var(--ease), color 0.3s, background 0.3s;
}
.page-arrow svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.page-arrow.left  { left: max(0.6rem, env(safe-area-inset-left)); }
.page-arrow.right { right: max(0.6rem, env(safe-area-inset-right)); }
body.reading .page-arrow { opacity: 0.55; pointer-events: auto; }
.page-arrow:hover { opacity: 1; color: var(--cream); background: rgba(8, 12, 20, 0.6); }

#chapter {
  position: fixed;
  left: max(1.1rem, env(safe-area-inset-left));
  bottom: calc(max(1.1rem, env(safe-area-inset-bottom)) + 0.5rem);
  font-family: var(--serif);
  font-size: 0.82rem;
  letter-spacing: 0.03em;
  color: var(--cream-dim);
  z-index: 25;
  opacity: 0;
  max-width: 70vw;
  transition: opacity 0.6s var(--ease);
}
body.reading #chapter { opacity: 1; }

#progress {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: 2px;
  background: rgba(246, 228, 212, 0.08);
  z-index: 25;
  opacity: 0;
  transition: opacity 0.6s var(--ease);
}
body.reading #progress { opacity: 1; }
#progressFill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, rgba(246, 228, 212, 0.55), var(--cream));
  transition: width 0.35s var(--ease);
}

.hidden { opacity: 0; visibility: hidden; }
.gone { opacity: 0; visibility: hidden; transform: translateY(10px); pointer-events: none; }

@media (max-width: 560px) {
  .chip span { display: none; }
  .chip { padding: 0.5rem; }
  #chapter { font-size: 0.74rem; }
}

@media (prefers-reduced-motion: reduce) {
  .open::after { animation: none; }
}

/* ---------- fallback flipbook (no WebGL) ---------- */
#fallback {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  perspective: 2400px;
  background: radial-gradient(120% 90% at 50% 25%, #14243f 0%, #05090f 70%);
}
#fb-stage {
  position: relative;
  transform-style: preserve-3d;
  box-shadow: 0 40px 90px rgba(0, 0, 0, 0.65);
}
.fb-leaf {
  position: absolute;
  top: 0; left: 50%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform 0.95s var(--ease);
  backface-visibility: hidden;
}
.fb-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  background-size: cover;
  box-shadow: inset 14px 0 26px -18px rgba(0, 0, 0, 0.8);
}
.fb-face.back { transform: rotateY(180deg); box-shadow: inset -14px 0 26px -18px rgba(0, 0, 0, 0.8); }
.fb-note {
  position: fixed;
  bottom: 1rem; left: 0; right: 0;
  text-align: center;
  font-size: 0.72rem;
  color: var(--cream-dim);
}
