/* Change these tokens to re-theme the viewer. */
:root {
  --stage: #ffffff;
  --paper: #ffffff;
  --ink: #2a2723;
  --muted: #6f695f;
  --bar: #ffffff;
  --line: #d9d4ca;
  --hover: #efece6;
  --accent: #3d5a4c;
  --page-shadow: 0 6px 24px rgba(40, 32, 20, .2);
  --zoom-bg: #1e1c19;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--stage);
  color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}
[hidden] { display: none !important; }

#app { position: relative; display: flex; flex-direction: column; height: 100%; }

/* Stage and book */
#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
#book {
  position: relative;
  flex: none;
  transition: transform .5s cubic-bezier(.4, 0, .2, 1);
  isolation: isolate;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
}
#book.dragging { cursor: grabbing; }

.leaf {
  position: absolute;
  top: 0;
  height: 100%;
  transform-origin: left center;
  background: var(--paper);
}

/* The book's outer shadow, held beneath the pages so it never lands on one. */
.under {
  position: absolute;
  top: 0;
  z-index: -1;
  width: 50%;
  height: 100%;
  box-shadow: var(--page-shadow);
  transition: opacity .3s;
}
#underLeft { left: 0; }
#underRight { left: 50%; }
.under.off { opacity: 0; }
#book[data-mode="single"] #underLeft { display: none; }
#book[data-mode="single"] #underRight { left: 0; width: 100%; }
#book[data-mode="spread"] .leaf { left: 50%; width: 50%; }
#book[data-mode="single"] .leaf { left: 0; width: 100%; }

.face { position: absolute; inset: 0; overflow: hidden; background: var(--paper); }
.face img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}

/* Spine gradient. Every inside page carries a matching one at its spine edge and keeps
   it through a turn, so the fold looks the same on both sides. The covers have none. */
#book[data-mode="spread"] .leaf:not(.cover-leaf) .face.front::after,
#book[data-mode="spread"] .leaf:not(.back-cover-leaf) .face.back::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 7%;
  pointer-events: none;
}
#book[data-mode="spread"] .leaf:not(.cover-leaf) .face.front::after {
  left: 0;
  background: linear-gradient(to right, rgba(0, 0, 0, .05), rgba(0, 0, 0, 0));
}
#book[data-mode="spread"] .leaf:not(.back-cover-leaf) .face.back::after {
  right: 0;
  background: linear-gradient(to left, rgba(0, 0, 0, .05), rgba(0, 0, 0, 0));
}

.shade {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: 0;
}
/* Darkest at the spine and fading toward the free edge, like light on a bent page. */
.front .shade { background: linear-gradient(to right, #000, rgba(0, 0, 0, .15)); }
.back .shade { background: linear-gradient(to left, #000, rgba(0, 0, 0, .15)); }

.links { position: absolute; inset: 0; z-index: 1; }
.links-off .links { display: none; }

/* A brief pulse as links become tappable in the zoom view. */
.links-flash .links a::after, .links-flash .links button::after {
  animation: link-flash 1.8s ease-in-out both;
}
@keyframes link-flash {
  0% { opacity: 0; }
  15% { opacity: .7; }
  40% { opacity: .21; }
  60% { opacity: .7; }
  100% { opacity: 0; }
}
@media (prefers-reduced-motion: reduce) {
  .links-flash .links a::after, .links-flash .links button::after {
    animation: link-flash-still 1.8s linear both;
  }
  @keyframes link-flash-still {
    0%, 80% { opacity: .7; }
    100% { opacity: 0; }
  }
}
.links a, .links button {
  position: absolute;
  display: block;
  padding: 0;
  border: 0;
  border-radius: 2px;
  background: transparent;
  cursor: pointer;
  transition: background-color .15s, box-shadow .15s;
}
/* Hover and the zoom pulse are separate layers, and each only ever changes opacity.
   Switching styles on hover made the effect flicker as the mouse left. */

/* Hover layer. Older browsers without the contrast effect get a light wash instead. */
.links a::before, .links button::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, .15);
  opacity: 0;
  transition: opacity .15s;
  pointer-events: none;
}
.links a:hover::before, .links button:hover::before { opacity: 1; }

/* The photo under the link gains contrast: highlights lift and darks deepen. A mask
   feathers the edges, since blurring here would also blur the photo. */
@supports ((backdrop-filter: contrast(1)) or (-webkit-backdrop-filter: contrast(1))) {
  .links a::before, .links button::before {
    --feather: calc(6px / var(--zoom, 1));
    background: transparent;
    -webkit-backdrop-filter: contrast(1.2) brightness(.977) saturate(1.06);
    backdrop-filter: contrast(1.2) brightness(.977) saturate(1.06);
    -webkit-mask-image:
      linear-gradient(to right, transparent, #000 var(--feather), #000 calc(100% - var(--feather)), transparent),
      linear-gradient(to bottom, transparent, #000 var(--feather), #000 calc(100% - var(--feather)), transparent);
    mask-image:
      linear-gradient(to right, transparent, #000 var(--feather), #000 calc(100% - var(--feather)), transparent),
      linear-gradient(to bottom, transparent, #000 var(--feather), #000 calc(100% - var(--feather)), transparent);
    -webkit-mask-composite: source-in;
    mask-composite: intersect;
  }
}

/* Pulse layer: a soft patch of light. Blur divides by the zoom level to look the same
   when magnified. */
.links a::after, .links button::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, .18);
  filter: blur(calc(3px / var(--zoom, 1)));
  opacity: 0;
  pointer-events: none;
}

/* Keyboard focus stays crisp so it is easy to find. */
.links a:focus-visible, .links button:focus-visible {
  box-shadow: inset 0 0 0 calc(2px / var(--zoom, 1)) var(--accent);
  outline: none;
}

/* Tap zones */
#book.zone-edge { cursor: pointer; }
#book.zone-mid { cursor: zoom-in; }

/* Control bar */
#bar {
  flex: none;
  display: flex;
  align-items: center;
  align-self: center;
  gap: 2px;
  margin: 0 0 12px;
  padding: 4px;
  background: var(--bar);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(40, 32, 20, .1);
  z-index: 20;
}
#bar button, #zoomBar button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  height: 40px;
  padding: 0 8px;
  color: var(--ink);
  background: transparent;
  border: 0;
  border-radius: 7px;
  font: inherit;
  cursor: pointer;
}
#bar button:hover:not(:disabled), #zoomBar button:hover { background: var(--hover); }
#bar button:focus-visible, #zoomBar button:focus-visible, .thumb:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
#bar button:disabled { opacity: .3; cursor: default; }
#label { min-width: 92px !important; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sep { width: 1px; height: 22px; margin: 0 4px; background: var(--line); }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Thumbnails */
#thumbs {
  position: absolute;
  inset: 0 0 68px 0;
  z-index: 15;
  overflow-y: auto;
  padding: 20px 16px;
  background: var(--stage);
}
#thumbGrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(112px, 1fr));
  gap: 16px;
  max-width: 1100px;
  margin: 0 auto;
}
.thumb {
  display: block;
  width: 100%;
  padding: 0;
  color: var(--muted);
  background: none;
  border: 0;
  font: inherit;
  font-size: 12px;
  text-align: center;
  cursor: pointer;
}
.thumb img {
  display: block;
  width: 100%;
  height: auto;
  margin-bottom: 5px;
  background: var(--paper);
  box-shadow: 0 1px 5px rgba(40, 32, 20, .25);
}
.thumb:hover img { box-shadow: 0 2px 10px rgba(40, 32, 20, .4); }
.thumb.current { color: var(--ink); font-weight: 600; }
.thumb.current img { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Zoom */
#zoom {
  position: absolute;
  inset: 0;
  z-index: 30;
  overflow: hidden;
  background: var(--zoom-bg);
  cursor: grab;
  touch-action: none;
}
#zoom.panning { cursor: grabbing; }
#zoomContent {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  transform-origin: 0 0;
  will-change: transform;
}
.zoom-page { position: relative; flex: 1 1 0; min-width: 0; height: 100%; background: var(--paper); }
.zoom-page img { display: block; width: 100%; height: 100%; object-fit: contain; pointer-events: none; -webkit-user-drag: none; }
#zoomBar {
  position: absolute;
  right: 12px;
  top: 12px;
  display: flex;
  gap: 2px;
  padding: 4px;
  background: var(--bar);
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .35);
}

#message { max-width: 420px; padding: 24px; text-align: center; color: var(--muted); }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: pre-wrap;
}

@media (prefers-reduced-motion: reduce) {
  #book { transition: none; }
}
