/* Shtypi: the reading view, the loading state, and the book's pages. */

.reader {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-height: 0;
  background: var(--stage);
}

.stage {
  position: relative;
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 36px;
  overflow: hidden;
}

.stage > * {
  grid-area: 1 / 1;
}

/* Loading */

.loading {
  display: grid;
  gap: 16px;
  width: min(420px, 86vw);
  animation: rise 0.4s ease-out both;
}

.loading-title {
  margin: 0;
  font: 500 22px/1.3 var(--font-ui);
}

.loading-text {
  min-height: 1.5em;
  margin: 0;
  font-size: 15px;
  color: var(--text-body);
  overflow-wrap: anywhere;
}

.meter {
  height: 2px;
  overflow: hidden;
  background: var(--line);
}

.meter span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--ink);
  transition: width 0.25s ease-out;
}

/* ---------- The book ---------- */

.book-sizer {
  max-width: 100%;
  animation: rise 0.6s ease-out both;
}

.page {
  background: #fff;
  box-shadow: 0 24px 48px -24px rgba(0, 0, 0, 0.38), 0 2px 6px rgba(0, 0, 0, 0.08);
}

.page-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #fff;
}

/*
 * Paper: a scan of real textured paper, multiplied over the rendered page so
 * white takes on the paper's tooth and a soft cream tone while ink stays dark.
 * Texture: "Paper 001" from ambientCG (ambientcg.com), CC0 public domain.
 */
.page-face::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background: url("assets/paper.jpg") center / cover no-repeat, #f9f3e7;
  background-blend-mode: multiply;
  mix-blend-mode: multiply;
}

/* Mirror and shift the scan so facing pages don't show the same sheet */
.page--left .page-face::before {
  transform: scaleX(-1);
}

.page:nth-child(3n) .page-face::before {
  background-position: 30% 40%, 0 0;
}

.page:nth-child(3n + 1) .page-face::before {
  background-position: 70% 60%, 0 0;
}

.page-face img {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.35s ease-out;
}

.page.is-ready img {
  opacity: 1;
}

/* Shading toward the spine so a spread reads as one bound book */
.page-face::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

.page--left .page-face::after {
  background: linear-gradient(to left, rgba(40, 25, 10, 0.2), rgba(40, 25, 10, 0.05) 4%, transparent 9%);
}

.page--right .page-face::after {
  background: linear-gradient(to right, rgba(40, 25, 10, 0.2), rgba(40, 25, 10, 0.05) 4%, transparent 9%);
}

.is-portrait .page-face::after,
.page[data-density="hard"] .page-face::after {
  background: none;
}

.page-wait {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font: 14px var(--font-ui);
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  background: #fff;
  animation: breathe 1.4s ease-in-out infinite alternate;
}

.page.is-ready .page-wait {
  display: none;
}

.page.is-failed .page-wait {
  padding: 16px;
  text-align: center;
  animation: none;
}

@keyframes breathe {
  from {
    opacity: 0.35;
  }
}

/* ---------- Bottom controls ---------- */

.controls {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 4px 24px 22px;
}

.folio {
  min-width: 15ch;
  margin: 0;
  font-size: 15px;
  text-align: center;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.folio span {
  color: var(--muted);
}

input[type="range"] {
  --fill: 0%;
  width: 100%;
  height: 28px;
  margin: 0;
  background: transparent;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 1px;
  background: linear-gradient(to right, var(--ink) var(--fill), var(--line) var(--fill));
}

input[type="range"]::-webkit-slider-thumb {
  width: 12px;
  height: 12px;
  margin-top: -6px;
  background: var(--ink);
  border: 0;
  border-radius: 0;
  -webkit-appearance: none;
  appearance: none;
}

input[type="range"]::-moz-range-track {
  height: 1px;
  background: var(--line);
}

input[type="range"]::-moz-range-progress {
  height: 1px;
  background: var(--ink);
}

input[type="range"]::-moz-range-thumb {
  width: 12px;
  height: 12px;
  background: var(--ink);
  border: 0;
  border-radius: 0;
}

@media (max-width: 760px) {
  .stage {
    padding: 16px;
  }

  .controls {
    gap: 10px;
    padding: 4px 16px 16px;
  }

  .folio {
    min-width: 0;
    font-size: 13px;
  }
}
