:root {
  --paper: #f4ecd8;
  --paper-shadow: #d9cfb8;
  --ink: #2a2620;
  --ink-soft: #6b5f4d;
  --accent: #8a6d3b;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  background: #1a1410;
  font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) calc(96px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
}

.book {
  position: relative;
  width: min(92vw, 440px);
  height: min(82vh, 640px);
  height: min(82dvh, 640px);
  max-height: calc(100dvh - 120px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  perspective: 1800px;
  transform-style: preserve-3d;
}

.page {
  position: absolute;
  inset: 0;
  background: var(--paper);
  background-image:
    radial-gradient(ellipse at 30% 20%, rgba(255,255,255,0.4), transparent 60%),
    radial-gradient(ellipse at 70% 80%, rgba(0,0,0,0.06), transparent 60%);
  border-radius: 4px 14px 14px 4px;
  box-shadow:
    -2px 0 0 rgba(0,0,0,0.08) inset,
    0 18px 40px rgba(0,0,0,0.55),
    0 2px 0 var(--paper-shadow);
  transform-origin: left center;
  transition: transform 0.7s cubic-bezier(0.22, 0.61, 0.36, 1), box-shadow 0.7s ease;
  backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 36px 28px;
  cursor: pointer;
  user-select: none;
}

.page::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 14px;
  background: linear-gradient(to right, rgba(0,0,0,0.18), transparent);
  border-radius: 4px 0 0 4px;
  pointer-events: none;
}

.page.flipped {
  transform: rotateY(-172deg);
  box-shadow: 0 18px 40px rgba(0,0,0,0.3);
}

.page-inner {
  width: 100%;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
}

/* Cover */
.cover .title {
  font-size: clamp(2rem, 7.5vw, 2.8rem);
  line-height: 1.15;
  letter-spacing: 0.02em;
  color: var(--ink);
  font-weight: 500;
}

.cover .subtitle {
  font-size: clamp(1.1rem, 4.2vw, 1.35rem);
  color: var(--ink-soft);
  margin-top: 0.5rem;
}

.cover .tap-hint {
  position: absolute;
  bottom: 24px;
  left: 0; right: 0;
  font-size: 0.85rem;
  color: var(--ink-soft);
  letter-spacing: 0.2em;
  text-transform: uppercase;
  opacity: 0.6;
  animation: pulse 2.4s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50%      { opacity: 0.75; }
}

/* Quote pages */
.quote-page .quote-mark {
  font-family: Georgia, serif;
  font-size: 4rem;
  line-height: 0.6;
  color: var(--accent);
  opacity: 0.55;
}

.quote-page .quote {
  font-size: clamp(1.35rem, 5.4vw, 1.75rem);
  line-height: 1.4;
  font-style: italic;
  color: var(--ink);
  max-width: 22ch;
  text-wrap: balance;
}

.quote-page .divider {
  width: 60px;
  height: 1px;
  background: var(--accent);
  opacity: 0.5;
  margin: 0.5rem auto;
}

.quote-page .meaning {
  font-size: clamp(0.95rem, 3.8vw, 1.05rem);
  line-height: 1.55;
  color: var(--ink-soft);
  max-width: 28ch;
  text-wrap: pretty;
}

.quote-page .meaning-label {
  font-variant: small-caps;
  letter-spacing: 0.15em;
  font-size: 0.7rem;
  color: var(--accent);
  margin-bottom: 0.25rem;
  display: block;
}

.quote-page .page-num {
  position: absolute;
  bottom: 18px;
  left: 0; right: 0;
  font-size: 0.75rem;
  color: var(--ink-soft);
  opacity: 0.55;
  font-variant: small-caps;
  letter-spacing: 0.2em;
}

/* End page */
.end .end-text {
  font-size: 1.8rem;
  color: var(--ink);
}
.end .end-sub {
  color: var(--ink-soft);
  margin-top: 0.25rem;
}
.restart {
  margin-top: 1.5rem;
  background: transparent;
  border: 1px solid var(--accent);
  color: var(--accent);
  padding: 0.6rem 1.4rem;
  font-family: inherit;
  font-size: 0.9rem;
  letter-spacing: 0.1em;
  border-radius: 2px;
  cursor: pointer;
  text-transform: uppercase;
}
.restart:active { background: var(--accent); color: var(--paper); }

/* Nav */
.nav {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom));
  left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
  z-index: 10;
  pointer-events: none;
}

.nav-btn, .page-counter { pointer-events: auto; }

.nav-btn {
  background: rgba(244, 236, 216, 0.1);
  border: 1px solid rgba(244, 236, 216, 0.25);
  color: var(--paper);
  width: 52px;
  height: 52px;
  border-radius: 50%;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.nav-btn:active { background: rgba(244, 236, 216, 0.2); }
.nav-btn:disabled { opacity: 0.25; cursor: default; }

.page-counter {
  color: var(--paper);
  opacity: 0.65;
  font-size: 0.8rem;
  letter-spacing: 0.15em;
  font-variant: small-caps;
  min-width: 60px;
  text-align: center;
}

/* Landscape phones / short screens */
@media (max-height: 520px) {
  .book {
    width: min(80vw, 480px);
    height: calc(100dvh - 100px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
  .page { padding: 20px 24px; }
  .cover .title { font-size: clamp(1.5rem, 5vw, 2.2rem); }
  .cover .tap-hint { bottom: 12px; font-size: 0.75rem; }
  .quote-page .quote-mark { font-size: 2.5rem; }
  .quote-page .page-num { bottom: 10px; }
}

/* Very narrow phones */
@media (max-width: 360px) {
  .page { padding: 28px 20px; }
  .cover .title { font-size: 1.7rem; }
}

/* Respect reduced motion */
@media (prefers-reduced-motion: reduce) {
  .page { transition: transform 0.25s ease, box-shadow 0.25s ease; }
  .cover .tap-hint { animation: none; opacity: 0.6; }
}
