/* ------------------------------------------------------------------
   Flipbook — a quiet reading desk. The pages are the only bright thing.
   ------------------------------------------------------------------ */

:root {
  --desk: #264045;
  --desk-edge: #121f25;
  --ink: #eaf0f1;
  --ink-dim: #9fb2b8;
  --bar: rgba(12, 21, 26, 0.86);
  --line: rgba(255, 255, 255, 0.13);
  --hover: rgba(255, 255, 255, 0.09);
  --accent: #f0be4d;
  --accent-ink: #231a02;
  --paper: #fcfbf8;
  --dur: 850ms;
  --ease: cubic-bezier(0.45, 0.05, 0.25, 1);
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
}

*, *::before, *::after { box-sizing: border-box; }

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

body {
  background: var(--desk-edge);
  color: var(--ink);
  font: 15px/1.4 var(--ui);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

[hidden] { display: none !important; }

.app {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: radial-gradient(ellipse 90% 80% at 50% 45%, var(--desk) 0%, var(--desk-edge) 100%);
}

/* ---------- bars ---------- */

.bar {
  position: relative;
  z-index: 20;
  display: flex;
  flex: none;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  background: var(--bar);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.bar.top { justify-content: space-between; border-bottom: 1px solid var(--line); }
.bar.bottom {
  border-top: 1px solid var(--line);
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  min-width: 0;
}

.heading { display: flex; align-items: center; gap: 12px; min-width: 0; }
h1 {
  margin: 0;
  font: 600 1.15rem/1.2 var(--serif);
  letter-spacing: 0.005em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

select {
  max-width: 220px;
  padding: 6px 28px 6px 10px;
  color: var(--ink);
  font: inherit;
  background: var(--hover) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23eaf0f1' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 6px center / 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  appearance: none;
  -webkit-appearance: none;
}
select option { color: #111; background: #fff; }

.tools { display: flex; align-items: center; gap: 4px; flex: none; }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 40px;
  height: 40px;
  padding: 0 10px;
  color: var(--ink);
  font: 500 0.9rem/1 var(--ui);
  text-decoration: none;
  background: transparent;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: 0.32; cursor: default; background: transparent; }
.btn svg,
.edge svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
.btn.text svg { width: 19px; height: 19px; }
.btn.primary {
  padding: 0 18px;
  height: 44px;
  color: var(--accent-ink);
  background: var(--accent);
}
.btn.primary:hover { background: #f6cc6d; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- footer controls ---------- */

.page-label {
  min-width: 7.5em;
  text-align: center;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.divider { width: 1px; height: 22px; margin: 0 6px; background: var(--line); }
.zoom { display: flex; align-items: center; flex: none; }
.display-mode { display: flex; align-items: center; gap: 2px; flex: none; padding: 3px; border: 1px solid var(--line); border-radius: 10px; }
.mode-btn { min-width: 58px; height: 32px; padding: 0 9px; font-size: 0.82rem; color: var(--ink-dim); }
.mode-btn.active { color: var(--accent-ink); background: var(--accent); }
.zoom-value { min-width: 3.4em; text-align: center; color: var(--ink-dim); font-variant-numeric: tabular-nums; }

input[type="range"] {
  flex: 1 1 auto;
  min-width: 80px;
  height: 40px;
  margin: 0 6px;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
  --fill: 0%;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(to right, var(--accent) var(--fill), rgba(255, 255, 255, 0.2) var(--fill));
}
input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.2);
}
input[type="range"]::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--accent); }
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -6px;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.25);
}
input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.25);
}
input[type="range"]:disabled { opacity: 0.4; cursor: default; }

/* ---------- the reading area ---------- */

.viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  isolation: isolate;
}

.stage {
  position: absolute;
  z-index: 0;
  inset: 0;
  min-width: 0;
  display: flex;
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: none;
}
.stage.zoomed { cursor: grab; }

.book-wrap {
  margin: auto;
  padding: 28px;
  flex: none;
}

.book {
  position: relative;
  perspective: 4200px;
  transition: transform var(--dur) var(--ease);
  user-select: none;
  -webkit-user-select: none;
}
.book.instant, .book.instant * { transition: none !important; animation: none !important; }

/* the shadow the book casts on the desk */
.book::before {
  content: "";
  position: absolute;
  left: 3%;
  right: 3%;
  bottom: -14px;
  height: 26px;
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.55), transparent 70%);
  filter: blur(7px);
  pointer-events: none;
}

.leaf {
  position: absolute;
  top: 0;
  left: 50%;
  width: var(--pw);
  height: var(--ph);
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform var(--dur) var(--ease), visibility 0s;
}
.leaf.flipped { transform: rotateY(-180deg); }

.book.single .leaf { left: 0; }
.book.single .leaf.flipped {
  visibility: hidden;
  transition: transform var(--dur) var(--ease), visibility 0s linear var(--dur);
}

.face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--paper);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.face.back { transform: rotateY(180deg); }

.face canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.face canvas.ready { opacity: 1; }

/* shading on the paper: a soft fold at the spine, and a shadow that passes over the page while it turns */
.face::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.05) 3.5%, transparent 9%);
}
.face.back::after { background: linear-gradient(to left, rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.05) 3.5%, transparent 9%); }

.face::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  opacity: 0;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.42), rgba(0, 0, 0, 0) 70%);
}
.face.back::before { background: linear-gradient(to left, rgba(0, 0, 0, 0.42), rgba(0, 0, 0, 0) 70%); }
.leaf.turning .face::before { animation: shade var(--dur) ease-in-out; }
@keyframes shade { 0% { opacity: 0; } 50% { opacity: 1; } 100% { opacity: 0; } }

/* the outer edges of the pages cast a faint line so single pages don't melt into the desk */
.leaf .face { box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08); }

/* ---------- side arrows ---------- */

.edge {
  position: absolute;
  top: 50%;
  z-index: 3;
  width: 44px;
  height: 64px;
  margin-top: -32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  background: rgba(12, 21, 26, 0.55);
  border: 0;
  border-radius: 12px;
  opacity: 0.55;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.2s ease, background 0.2s ease;
}
.edge:hover { opacity: 1; background: rgba(12, 21, 26, 0.8); }
.edge.prev { left: max(8px, env(safe-area-inset-left)); }
.edge.next { right: max(8px, env(safe-area-inset-right)); }
.edge:disabled { opacity: 0; pointer-events: none; }

/* ---------- loading, empty and error states ---------- */

.loader {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: rgba(18, 31, 37, 0.6);
}
.loader p { margin: 0; color: var(--ink-dim); }
.loader-bar { width: min(260px, 60vw); height: 4px; overflow: hidden; border-radius: 2px; background: rgba(255, 255, 255, 0.16); }
.loader-bar span { display: block; width: 0; height: 100%; background: var(--accent); transition: width 0.2s linear; }
.loader-bar.busy span { width: 35%; animation: slide 1.1s ease-in-out infinite alternate; }
@keyframes slide { from { transform: translateX(0); } to { transform: translateX(185%); } }

.notice {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 4;
  width: min(440px, calc(100% - 32px));
  padding: 28px;
  transform: translate(-50%, -50%);
  background: rgba(12, 21, 26, 0.88);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.notice h2 { margin: 0 0 8px; font: 600 1.25rem/1.25 var(--serif); }
.notice p { margin: 0 0 18px; color: var(--ink-dim); line-height: 1.55; }
.notice p:last-child { margin-bottom: 0; }

.drop-hint {
  position: absolute;
  inset: 12px;
  z-index: 6;
  display: none;
  align-items: center;
  justify-content: center;
  font: 600 1.2rem var(--serif);
  background: rgba(18, 31, 37, 0.82);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  pointer-events: none;
}
.viewport.dropping .drop-hint { display: flex; }

/* ---------- full screen ---------- */



/* ---------- small screens ---------- */

@media (max-width: 720px) {
  .bar { padding: 6px 8px; gap: 4px; }
  h1 { font-size: 1.02rem; }
  .btn.text span { display: none; }
  .btn.text { padding: 0; width: 40px; }
  .page-label { min-width: 5.2em; font-size: 0.85rem; }
  .divider, .zoom-value, .display-mode, .mode-divider { display: none; }
  .book-wrap { padding: 10px; }
  .edge { width: 36px; height: 56px; margin-top: -28px; }
  select { max-width: 130px; }

  /* Keep every footer control inside narrow phone viewports.
     The slider is allowed to shrink; zoom controls keep a fixed touch target. */
  .bar.bottom {
    display: grid;
    grid-template-columns: 36px minmax(28px, 1fr) auto 36px 76px;
    align-items: center;
    column-gap: 4px;
    padding-left: max(6px, env(safe-area-inset-left));
    padding-right: max(6px, env(safe-area-inset-right));
  }
  .bar.bottom > #prev,
  .bar.bottom > #next { width: 36px; min-width: 36px; padding: 0; }
  .bar.bottom > #slider {
    width: 100%;
    min-width: 0;
    margin: 0;
  }
  .bar.bottom > #pageLabel {
    min-width: 0;
    max-width: 5.5em;
    font-size: 0.78rem;
    white-space: nowrap;
  }
  .bar.bottom > .zoom {
    width: 76px;
    justify-content: space-between;
  }
  .bar.bottom > .zoom .btn {
    width: 36px;
    min-width: 36px;
    padding: 0;
  }
}

@media (max-width: 460px) {
  .edge { display: none; }
}

/* In fullscreen, only auto-hide toolbars on devices with a real hover pointer.
   Touch devices otherwise lose the footer as soon as a page is tapped. */
@media (hover: hover) and (pointer: fine) {
  .app:fullscreen .bar,
  .app:-webkit-full-screen .bar {
    opacity: 0;
    transition: opacity 0.3s ease;
  }
  .app:fullscreen .bar:hover,
  .app:fullscreen .bar:focus-within,
  .app:-webkit-full-screen .bar:hover,
  .app:-webkit-full-screen .bar:focus-within {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .book, .leaf, .face canvas { transition-duration: 0.01ms !important; }
  .leaf.turning .face::before { animation: none; }
  .loader-bar.busy span { animation: none; }
}
