/* ===========================================================================
   The Quiet Library — a minimal, modern book reader
   Calm paper tones, soft shadows, a single page at a time.
   =========================================================================== */

:root {
  --paper: #fdfcf7;
  --paper-edge: #f1ece0;
  --ink: #2a2622;
  --ink-soft: #8a7f70;
  --backdrop-top: #efeae0;
  --backdrop-bottom: #e3dccd;
  --accent: #b08a4f;
  --shadow: rgba(60, 48, 30, 0.22);
  --radius: 4px;
  /* Hebrew-first stacks; Latin glyphs fall through gracefully. */
  --serif: "Frank Ruhl Libre", "Fraunces", Georgia, "Times New Roman", serif;
  --sans: "Assistant", "Inter", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

/* The [hidden] attribute must win over our display:flex blocks below. */
[hidden] {
  display: none !important;
}

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

body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: radial-gradient(
    120% 120% at 50% 0%,
    var(--backdrop-top) 0%,
    var(--backdrop-bottom) 100%
  );
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ---- Masthead ---- */
.masthead {
  text-align: center;
  padding: clamp(14px, 3vh, 30px) 16px 4px;
  flex: 0 0 auto;
}
.masthead__title {
  font-family: var(--serif);
  font-weight: 600;
  font-size: clamp(20px, 3.2vw, 30px);
  letter-spacing: 0.01em;
  margin: 0;
}
.masthead__author {
  margin: 4px 0 0;
  font-size: 13px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

/* ---- Reader stage ---- */
.reader {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(8px, 2vh, 20px) clamp(8px, 4vw, 48px);
  min-height: 0;
  position: relative;
}

/* ---- Loading / empty / error states ---- */
.state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  color: var(--ink-soft);
  text-align: center;
  max-width: 520px;
}
.state__spinner {
  width: 30px;
  height: 30px;
  border: 2px solid rgba(138, 127, 112, 0.3);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.state--message .state__spinner {
  display: none;
}
.state__msg {
  font-size: 15px;
  line-height: 1.6;
  margin: 0;
}
.state__msg code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  background: rgba(176, 138, 79, 0.14);
  color: #6f5630;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 13px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---- The book ---- */
.book {
  display: flex;
  align-items: center;
  gap: clamp(6px, 2vw, 24px);
  max-width: 100%;
  max-height: 100%;
}

.stage {
  position: relative;
  /* Sizing is driven by the canvas's intrinsic aspect ratio. */
  filter: drop-shadow(0 18px 34px var(--shadow));
  border-radius: var(--radius);
  /* Subtle spine on the left edge, like a bound page. */
  background: linear-gradient(
    90deg,
    var(--paper-edge) 0%,
    var(--paper) 2.5%,
    var(--paper) 100%
  );
  perspective: 2200px;
}

.page {
  display: block;
  max-width: min(78vw, calc((100dvh - 220px) * 0.72));
  max-height: calc(100dvh - 220px);
  width: auto;
  height: auto;
  border-radius: var(--radius);
  background: var(--paper);
}

/* The flipping page sits on top of the current page during a turn. */
.page--flip {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform-origin: left center;
  backface-visibility: hidden;
  z-index: 3;
  box-shadow: 0 0 26px rgba(60, 48, 30, 0.18);
  opacity: 0;
  pointer-events: none;
}

/* Turn forward: page lifts from the right and rotates toward the spine. */
.stage.flip-next .page--flip {
  opacity: 1;
  animation: flipNext 0.55s cubic-bezier(0.4, 0, 0.2, 1) forwards;
  transform-origin: left center;
}
@keyframes flipNext {
  0% {
    transform: rotateY(0deg);
  }
  100% {
    transform: rotateY(-168deg);
  }
}

/* Turn backward: page comes back from the spine outward. */
.stage.flip-prev .page--flip {
  opacity: 1;
  transform-origin: left center;
  animation: flipPrev 0.55s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}
@keyframes flipPrev {
  0% {
    transform: rotateY(-168deg);
  }
  100% {
    transform: rotateY(0deg);
  }
}

/* ---- Click edges (invisible hot-zones over the page sides) ---- */
.edge {
  flex: 0 0 auto;
  width: clamp(34px, 6vw, 70px);
  align-self: stretch;
  border: none;
  background: transparent;
  cursor: pointer;
  border-radius: 10px;
  transition: background 0.2s;
  position: relative;
}
.edge::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 10px;
  height: 10px;
  border-top: 2px solid var(--ink-soft);
  opacity: 0;
  transition: opacity 0.2s;
}
.edge--prev::after {
  border-left: 2px solid var(--ink-soft);
  transform: translate(-30%, -50%) rotate(-45deg);
}
.edge--next::after {
  border-right: 2px solid var(--ink-soft);
  transform: translate(-70%, -50%) rotate(45deg);
}
.edge:hover {
  background: rgba(176, 138, 79, 0.07);
}
.edge:hover::after {
  opacity: 0.7;
}
.edge:disabled {
  cursor: default;
  opacity: 0;
  pointer-events: none;
}

/* ---- Bottom controls ---- */
.controls {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 10px 16px clamp(14px, 3vh, 26px);
}
.btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid rgba(138, 127, 112, 0.35);
  background: rgba(253, 252, 247, 0.7);
  color: var(--ink);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.15s, background 0.15s, box-shadow 0.15s;
}
.btn:hover {
  background: var(--paper);
  box-shadow: 0 4px 12px var(--shadow);
}
.btn:active {
  transform: scale(0.92);
}
.btn:disabled {
  opacity: 0.3;
  cursor: default;
  box-shadow: none;
}
.counter {
  direction: ltr; /* keep "1 / 6" in reading order even in RTL mode */
  font-variant-numeric: tabular-nums;
  font-size: 14px;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  min-width: 64px;
  text-align: center;
}

@media (max-width: 640px) {
  .edge {
    display: none; /* tap left/right halves instead (handled in JS) */
  }
  .page {
    max-width: 92vw;
    max-height: calc(100dvh - 200px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .stage.flip-next .page--flip,
  .stage.flip-prev .page--flip {
    animation-duration: 0.01s;
  }
}

/* ===========================================================================
   Right-to-left (Hebrew) mode — set <html dir="rtl">.
   The binding moves to the right and pages turn the other way.
   (Flex containers auto-mirror the edge buttons & controls; we only need to
   flip the spine, the page-turn pivot, and the little chevrons.)
   =========================================================================== */

/* Spine on the right edge instead of the left. */
[dir="rtl"] .stage {
  background: linear-gradient(
    270deg,
    var(--paper-edge) 0%,
    var(--paper) 2.5%,
    var(--paper) 100%
  );
}

/* Pages pivot on the right edge. */
[dir="rtl"] .page--flip {
  transform-origin: right center;
}
[dir="rtl"] .stage.flip-next .page--flip {
  transform-origin: right center;
  animation-name: flipNextRtl;
}
[dir="rtl"] .stage.flip-prev .page--flip {
  transform-origin: right center;
  animation-name: flipPrevRtl;
}
@keyframes flipNextRtl {
  0% {
    transform: rotateY(0deg);
  }
  100% {
    transform: rotateY(168deg);
  }
}
@keyframes flipPrevRtl {
  0% {
    transform: rotateY(168deg);
  }
  100% {
    transform: rotateY(0deg);
  }
}

/* Flip both edge chevrons horizontally in RTL. */
[dir="rtl"] .edge--prev::after {
  border-left: 0;
  border-right: 2px solid var(--ink-soft);
  transform: translate(-30%, -50%) rotate(-45deg);
}
[dir="rtl"] .edge--next::after {
  border-right: 0;
  border-left: 2px solid var(--ink-soft);
  transform: translate(-70%, -50%) rotate(45deg);
}
