
:root {
  --bg: #161412;
  --panel: rgba(25, 22, 19, .96);
  --line: rgba(255,255,255,.12);
  --text: #f0ebe6;
  --muted: #b8aea4;
  --paper: #f4efe7;
  --paper-edge: #e5dccf;
  --accent: #a56442;
  --shadow: 0 24px 60px rgba(0,0,0,.33);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background:
    radial-gradient(circle at 50% 0%, rgba(255,255,255,.035), transparent 36%),
    linear-gradient(180deg, #1e1a16 0%, #14110e 100%);
  color: var(--text);
  overflow: hidden;
}
button, input, a { font: inherit; }
a { color: inherit; }
.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
}
.toolbar {
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: max(10px, env(safe-area-inset-top)) clamp(14px, 2.5vw, 24px) 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(12,10,8,.82);
  backdrop-filter: blur(18px);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(180deg, #c08564, #8f5536);
  box-shadow: 0 0 0 3px rgba(165,100,66,.16);
}
.brand-title {
  font-size: 13px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: #e2d8cf;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.toolbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 7px;
}
.tool-button,
.icon-button {
  min-height: 36px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.14);
  background: rgba(255,255,255,.05);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  text-decoration: none;
  transition: background .2s ease, border-color .2s ease, transform .2s ease;
}
.tool-button { padding: 0 14px; font-size: 12px; letter-spacing: .035em; }
.tool-link { line-height: 1; }
.icon-button { width: 38px; padding: 0; }
.icon-button svg, .nav-button svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tool-button:hover, .icon-button:hover, .tool-button:focus-visible, .icon-button:focus-visible { background: rgba(255,255,255,.10); border-color: rgba(255,255,255,.24); outline: none; }
.tool-button:active, .icon-button:active { transform: translateY(1px); }

.viewer {
  min-height: 0;
  padding: 22px clamp(10px, 3vw, 40px) 12px;
  display: grid;
  grid-template-columns: 46px minmax(0, auto) 46px;
  align-items: center;
  justify-content: center;
  gap: clamp(8px, 2vw, 24px);
  perspective: 3300px;
}

.book {
  width: min(92vw, calc((100dvh - 180px) * 16 / 9), 1480px);
  aspect-ratio: 16 / 9;
  position: relative;
  background: var(--paper);
  box-shadow: var(--shadow);
  transform-style: preserve-3d;
  touch-action: pan-y;
  user-select: none;
  isolation: isolate;
  overflow: hidden;
  border-radius: 4px;
}
.paper-edge {
  position: absolute;
  inset: 4px -7px -8px -7px;
  z-index: -3;
  background: linear-gradient(180deg, #ece3d8 0%, #dfd3c4 100%);
  border-radius: 4px 6px 8px 6px;
  box-shadow: 0 10px 24px rgba(0,0,0,.25);
}
.page {
  position: absolute;
  inset: 0;
  background: var(--paper);
}
.page > img {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  -webkit-user-drag: none;
}
.blank-page {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, rgba(255,255,255,.30), rgba(0,0,0,.025)),
    linear-gradient(90deg, rgba(255,255,255,.12), rgba(0,0,0,.035)),
    var(--paper);
}
.half-face .blank-page {
  background:
    radial-gradient(ellipse at 22% 18%, rgba(255,255,255,.22), transparent 36%),
    linear-gradient(180deg, rgba(255,255,255,.22), rgba(0,0,0,.028)),
    linear-gradient(90deg, rgba(255,255,255,.18), transparent 24%, rgba(0,0,0,.045)),
    #f5f0e8;
}
.blank-page[hidden] { display: none; }
.page-sheen {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(180deg, rgba(255,255,255,.10), transparent 11%, transparent 90%, rgba(0,0,0,.03)),
    radial-gradient(circle at 14% 18%, rgba(255,255,255,.10), transparent 32%);
}

.under-page {
  opacity: 0;
  visibility: hidden;
  z-index: 12;
  will-change: clip-path, opacity;
  pointer-events: none;
}
.under-page.visible {
  opacity: 1;
  visibility: visible;
}
.under-page.reveal-forward {
  animation: revealForward 940ms cubic-bezier(.20,.70,.18,1) both;
}
.under-page.reveal-back {
  animation: revealBack 940ms cubic-bezier(.20,.70,.18,1) both;
}
.current-page {
  z-index: 8;
}
.current-page.hidden-during-turn {
  opacity: 1;
  visibility: visible;
}

.link-layer { position: absolute; inset: 0; z-index: 18; }
.hotspot {
  position: absolute;
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  cursor: pointer;
  border-radius: 2px;
  text-decoration: none;
}
.hotspot:hover { background: rgba(165,100,66,.08); box-shadow: inset 0 -1px 0 rgba(165,100,66,.65); }
.hotspot:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; background: rgba(165,100,66,.10); }

.turn-shadow {
  position: absolute;
  top: -2%;
  bottom: -2%;
  width: 52%;
  opacity: 0;
  pointer-events: none;
  z-index: 18;
  filter: blur(13px);
  mix-blend-mode: multiply;
}
.turn-shadow.visible.forward {
  left: 47%;
  transform-origin: left center;
  background: radial-gradient(ellipse at 4% 50%, rgba(31,25,20,.26) 0%, rgba(31,25,20,.16) 21%, rgba(31,25,20,.07) 43%, rgba(31,25,20,0) 73%);
  animation: shadowForward 940ms cubic-bezier(.22,.68,.18,1) both;
}
.turn-shadow.visible.back {
  right: 47%;
  transform-origin: right center;
  background: radial-gradient(ellipse at 96% 50%, rgba(31,25,20,.26) 0%, rgba(31,25,20,.16) 21%, rgba(31,25,20,.07) 43%, rgba(31,25,20,0) 73%);
  animation: shadowBack 940ms cubic-bezier(.22,.68,.18,1) both;
}

.half {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 50%;
  overflow: visible;
  background: transparent;
  transform-style: preserve-3d;
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}
.half.visible {
  visibility: visible;
  opacity: 1;
}
.half.left { left: 0; }
.half.right { left: 50%; }
.half > img,
.half-face > img {
  position: absolute;
  top: 0;
  width: 200%;
  height: 100%;
  object-fit: fill;
  display: block;
  pointer-events: none;
  -webkit-user-drag: none;
}
.slice-left { left: 0; }
.slice-right { left: -100%; }

/* The moving leaf is intentionally blank, like the FlipHTML5 reference. */
.flip-half {
  z-index: 24;
  will-change: transform, filter, opacity;
  transform-style: preserve-3d;
}
.flip-half.right { left: 50%; transform-origin: 0% 50%; }
.flip-half.left { left: 0; transform-origin: 100% 50%; }

.half-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: #fbfaf6;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform-style: preserve-3d;
}
.half-front {
  transform: rotateY(0deg) translateZ(.4px);
  box-shadow:
    inset 0 0 0 1px rgba(101,88,73,.05),
    inset 0 1px 0 rgba(255,255,255,.95),
    0 4px 13px rgba(0,0,0,.08);
}
/* Ambas caras de la hoja móvil son neutras: nunca aparece contenido impreso en el giro. */
.half-back {
  display: block;
  transform: rotateY(180deg) translateZ(.4px);
  box-shadow:
    inset 0 0 0 1px rgba(101,88,73,.04),
    inset 0 1px 0 rgba(255,255,255,.90),
    0 4px 13px rgba(0,0,0,.07);
}

.half-face .blank-page {
  display: block !important;
  background:
    radial-gradient(ellipse at 18% 14%, rgba(255,255,255,.86), rgba(255,255,255,0) 40%),
    linear-gradient(180deg, #fffefa 0%, #faf8f1 58%, #f4f0e6 100%);
}
.half-face > img { display: none !important; }

.half-face::before {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: .72;
}
.flip-half.right .half-face::before {
  background:
    linear-gradient(90deg, rgba(122,106,86,.10) 0%, rgba(255,255,255,.20) 7%, rgba(255,255,255,0) 32%, rgba(0,0,0,.025) 100%);
}
.flip-half.left .half-face::before {
  background:
    linear-gradient(270deg, rgba(122,106,86,.10) 0%, rgba(255,255,255,.20) 7%, rgba(255,255,255,0) 32%, rgba(0,0,0,.025) 100%);
}
.half-face::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
  background:
    linear-gradient(180deg, rgba(255,255,255,.24), rgba(255,255,255,0) 13%, rgba(0,0,0,0) 83%, rgba(82,67,52,.045));
}

.face-shade,
.edge-highlight {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}
.face-shade { opacity: 0; }
.edge-highlight { opacity: 0; }
.flip-half.right .edge-highlight {
  left: auto;
  width: 13%;
  background: linear-gradient(90deg, rgba(255,255,255,0), rgba(255,255,255,.54));
}
.flip-half.left .edge-highlight {
  right: auto;
  width: 13%;
  background: linear-gradient(270deg, rgba(255,255,255,0), rgba(255,255,255,.54));
}

.flip-half.turn-forward {
  animation: turnForward 940ms cubic-bezier(.20,.70,.18,1) both;
}
.flip-half.turn-back {
  animation: turnBack 940ms cubic-bezier(.20,.70,.18,1) both;
}
.turn-forward .front-shade {
  background: linear-gradient(90deg, rgba(45,38,31,.03), rgba(45,38,31,.31));
  animation: shadeForward 940ms ease both;
}
.turn-back .front-shade {
  background: linear-gradient(270deg, rgba(45,38,31,.03), rgba(45,38,31,.31));
  animation: shadeForward 940ms ease both;
}
.turn-forward .edge-highlight,
.turn-back .edge-highlight {
  animation: edgeLight 940ms ease both;
}

@keyframes revealForward {
  0%   { clip-path: polygon(100% 0, 100% 0, 100% 100%, 100% 100%); }
  14%  { clip-path: polygon(94% 0, 100% 0, 100% 100%, 96% 100%); }
  32%  { clip-path: polygon(76% 0, 100% 0, 100% 100%, 80% 100%); }
  52%  { clip-path: polygon(50% 0, 100% 0, 100% 100%, 56% 100%); }
  72%  { clip-path: polygon(25% 0, 100% 0, 100% 100%, 31% 100%); }
  88%  { clip-path: polygon(7% 0, 100% 0, 100% 100%, 10% 100%); }
  100% { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
}
@keyframes revealBack {
  0%   { clip-path: polygon(0 0, 0 0, 0 100%, 0 100%); }
  14%  { clip-path: polygon(0 0, 6% 0, 4% 100%, 0 100%); }
  32%  { clip-path: polygon(0 0, 24% 0, 20% 100%, 0 100%); }
  52%  { clip-path: polygon(0 0, 50% 0, 44% 100%, 0 100%); }
  72%  { clip-path: polygon(0 0, 75% 0, 69% 100%, 0 100%); }
  88%  { clip-path: polygon(0 0, 93% 0, 90% 100%, 0 100%); }
  100% { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
}
@keyframes turnForward {
  0%   { transform: perspective(2500px) rotateY(0deg) translateZ(0) skewY(0deg) scaleX(1); opacity: 1; border-radius: 0; }
  16%  { transform: perspective(2500px) rotateY(-18deg) translateZ(5px) skewY(-.25deg) scaleX(.998); opacity: 1; border-radius: 0 8px 8px 0; }
  36%  { transform: perspective(2500px) rotateY(-50deg) translateZ(12px) skewY(-.75deg) scaleX(.988); opacity: 1; border-radius: 0 18px 18px 0; }
  56%  { transform: perspective(2500px) rotateY(-86deg) translateZ(18px) skewY(-1.25deg) scaleX(.970); opacity: 1; border-radius: 0 28px 28px 0; }
  72%  { transform: perspective(2500px) rotateY(-119deg) translateZ(14px) skewY(-1.00deg) scaleX(.976); opacity: 1; border-radius: 18px 0 0 18px; }
  88%  { transform: perspective(2500px) rotateY(-154deg) translateZ(6px) skewY(-.35deg) scaleX(.992); opacity: .78; border-radius: 8px 0 0 8px; }
  100% { transform: perspective(2500px) rotateY(-180deg) translateZ(0) skewY(0deg) scaleX(1); opacity: 0; border-radius: 0; }
}
@keyframes turnBack {
  0%   { transform: perspective(2500px) rotateY(0deg) translateZ(0) skewY(0deg) scaleX(1); opacity: 1; border-radius: 0; }
  16%  { transform: perspective(2500px) rotateY(18deg) translateZ(5px) skewY(.25deg) scaleX(.998); opacity: 1; border-radius: 8px 0 0 8px; }
  36%  { transform: perspective(2500px) rotateY(50deg) translateZ(12px) skewY(.75deg) scaleX(.988); opacity: 1; border-radius: 18px 0 0 18px; }
  56%  { transform: perspective(2500px) rotateY(86deg) translateZ(18px) skewY(1.25deg) scaleX(.970); opacity: 1; border-radius: 28px 0 0 28px; }
  72%  { transform: perspective(2500px) rotateY(119deg) translateZ(14px) skewY(1.00deg) scaleX(.976); opacity: 1; border-radius: 0 18px 18px 0; }
  88%  { transform: perspective(2500px) rotateY(154deg) translateZ(6px) skewY(.35deg) scaleX(.992); opacity: .78; border-radius: 0 8px 8px 0; }
  100% { transform: perspective(2500px) rotateY(180deg) translateZ(0) skewY(0deg) scaleX(1); opacity: 0; border-radius: 0; }
}
@keyframes shadeForward {
  0%   { opacity: .01; }
  30%  { opacity: .16; }
  54%  { opacity: .50; }
  72%  { opacity: .38; }
  90%,100% { opacity: 0; }
}
@keyframes edgeLight {
  0%   { opacity: .08; }
  30%  { opacity: .32; }
  55%  { opacity: .86; }
  78%  { opacity: .36; }
  94%,100% { opacity: 0; }
}
@keyframes shadowForward {
  0%   { opacity: 0; transform: translateX(-5%) scaleX(.16); }
  24%  { opacity: .15; transform: translateX(-2%) scaleX(.42); }
  52%  { opacity: .34; transform: translateX(0%) scaleX(.92); }
  74%  { opacity: .19; transform: translateX(3%) scaleX(.64); }
  100% { opacity: 0; transform: translateX(7%) scaleX(.18); }
}
@keyframes shadowBack {
  0%   { opacity: 0; transform: translateX(5%) scaleX(.16); }
  24%  { opacity: .15; transform: translateX(2%) scaleX(.42); }
  52%  { opacity: .34; transform: translateX(0%) scaleX(.92); }
  74%  { opacity: .19; transform: translateX(-3%) scaleX(.64); }
  100% { opacity: 0; transform: translateX(-7%) scaleX(.18); }
}

.nav-button {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.05);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background .2s ease, opacity .2s ease, transform .2s ease;
}
.nav-button:hover:not(:disabled), .nav-button:focus-visible:not(:disabled) { background: rgba(255,255,255,.12); outline: none; transform: scale(1.04); }
.nav-button:disabled { opacity: .2; cursor: default; }

.controls {
  min-height: 58px;
  display: grid;
  grid-template-columns: auto minmax(150px, 560px) auto;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 8px 24px max(14px, env(safe-area-inset-bottom));
  color: var(--muted);
}
.counter { min-width: 72px; font-size: 12px; font-variant-numeric: tabular-nums; letter-spacing: .05em; }
#pageCurrent { color: var(--text); font-weight: 650; }
.counter-sep { margin: 0 5px; opacity: .55; }
.page-slider { width: 100%; accent-color: var(--accent); cursor: pointer; }
.hint { font-size: 11px; white-space: nowrap; }
kbd { border: 1px solid var(--line); background: rgba(255,255,255,.04); border-radius: 4px; padding: 1px 5px; font-family: inherit; color: #d6cfc7; }

.thumb-panel {
  position: fixed;
  z-index: 50;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(430px, 92vw);
  background: var(--panel);
  backdrop-filter: blur(22px);
  border-left: 1px solid var(--line);
  box-shadow: -20px 0 50px rgba(0,0,0,.30);
  transform: translateX(102%);
  transition: transform .28s ease;
  display: grid;
  grid-template-rows: auto 1fr;
}
.thumb-panel.open { transform: translateX(0); }
.thumb-panel-head { height: 66px; padding: 0 18px 0 22px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); }
.thumb-panel-head strong { font-size: 13px; letter-spacing: .04em; }
.thumb-grid { padding: 16px; overflow: auto; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-content: start; }
.thumb {
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(255,255,255,.035);
  border-radius: 6px;
  padding: 6px;
  color: var(--text);
  cursor: pointer;
  transition: border-color .2s ease, background .2s ease, transform .2s ease;
}
.thumb:hover { background: rgba(255,255,255,.08); transform: translateY(-1px); }
.thumb.active { border-color: var(--accent); box-shadow: 0 0 0 1px rgba(165,100,66,.35); background: rgba(165,100,66,.10); }
.thumb img { display: block; width: 100%; aspect-ratio: 16/9; object-fit: cover; background: #eee; }
.thumb span { display: block; padding-top: 6px; font-size: 10px; color: var(--muted); text-align: center; }
.scrim { position: fixed; z-index: 40; inset: 0; background: rgba(0,0,0,.42); opacity: 0; visibility: hidden; transition: opacity .28s ease, visibility .28s ease; }
.scrim.show { opacity: 1; visibility: visible; }

body.is-fullscreen .toolbar { background: rgba(10,9,8,.78); }

@media (max-width: 980px) {
  .toolbar { min-height: 58px; padding-left: 12px; padding-right: 12px; }
  .brand-title { max-width: 150px; }
  .toolbar-actions { gap: 5px; }
  .tool-button { padding: 0 10px; min-height: 34px; }
  .icon-button { width: 34px; min-height: 34px; }
  .tool-button:nth-of-type(2) { display: none; }
  .viewer { grid-template-columns: 1fr; padding: 14px 8px 8px; }
  .book { width: min(98vw, calc((100dvh - 165px) * 16 / 9)); }
  .nav-button { position: fixed; z-index: 18; top: 50%; width: 40px; height: 52px; border-radius: 10px; background: rgba(20,18,16,.62); backdrop-filter: blur(8px); }
  .nav-prev { left: 8px; }
  .nav-next { right: 8px; }
  .controls { grid-template-columns: auto 1fr; gap: 12px; padding-left: 16px; padding-right: 16px; }
  .hint { display: none; }
}

@media (max-width: 560px) {
  .brand-title { display: none; }
  .toolbar { gap: 8px; }
  .tool-button { font-size: 11px; padding: 0 9px; }
  .toolbar-actions { width: 100%; justify-content: flex-end; }
  .book { width: 99vw; }
  .viewer { padding-left: 2px; padding-right: 2px; }
  .thumb-grid { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .flip-half.turn-forward, .flip-half.turn-back, .under-page.reveal-forward, .under-page.reveal-back, .turn-forward .face-shade, .turn-back .face-shade, .turn-shadow.visible.forward, .turn-shadow.visible.back { animation-duration: 1ms; }
  .thumb-panel, .scrim, .tool-button, .icon-button, .nav-button { transition-duration: 1ms; }
}
