/*! Lector Ambiental — Todos los derechos reservados. Ver LICENSE.md. */
/* Dos sistemas de color:
 *  - El "papel" (data-paper en <html>: oscuro, blanco o sepia): solo las páginas.
 *    Pensado para leer horas: contraste alto y estable, sin efectos.
 *  - El "escenario" (data-atm en <body>, ver escenarios.css): todo lo de alrededor
 *    (barra, paneles, biblioteca) toma los colores del ambiente elegido.
 * Las animaciones usan solo transform y opacity, y se apagan con
 * prefers-reduced-motion o con "Animaciones: no" (body.no-motion).
 */

:root {
  --reader-font-default: 'Iowan Old Style', 'Palatino Linotype', Palatino, 'Book Antiqua', Georgia, serif;
  --reader-font: var(--reader-font-default);
  --display-font: 'Iowan Old Style', 'Palatino Linotype', Palatino, Georgia, serif;
  --ui-font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --font-size: 20px;
  --panel-w: 320px;
  --topbar-h: 58px;
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);

  /* Papel oscuro (por defecto) */
  --paper: #1b1b20; --ink: #e6e3dc; --ink-muted: #9a958b; --paper-line: rgba(255, 255, 255, 0.1);
  --hl: rgba(224, 139, 90, 0.12); --link: #e08b5a;
}
/* Papeles claros: grano oscuro y sombra del lomo más suave */
:root[data-paper='light'] {
  --paper: #fbfaf7; --ink: #1d1d1f; --ink-muted: #6b6b70; --paper-line: rgba(0, 0, 0, 0.1);
  --hl: rgba(47, 95, 208, 0.08); --link: #2f5fd0;
  --paper-grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.3 0 0 0 0 0.22 0 0 0 0 0.12 0 0 0 0.09 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"); --gutter: rgba(0, 0, 0, 0.22);
}
:root[data-paper='sepia'] {
  --paper: #f1e6d0; --ink: #2b2620; --ink-muted: #7a6f60; --paper-line: rgba(80, 60, 30, 0.14);
  --hl: rgba(163, 84, 43, 0.1); --link: #a3542b;
  --paper-grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.3 0 0 0 0 0.22 0 0 0 0 0.12 0 0 0 0.09 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"); --gutter: rgba(60, 35, 10, 0.28);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh;
  background: var(--c-bg);
  color: var(--c-ink);
  font-family: var(--ui-font);
  -webkit-font-smoothing: antialiased;
}
button, select, input { font: inherit; color: inherit; }
a { color: var(--c-accent); }
:focus-visible { outline: 2px solid var(--c-accent); outline-offset: 2px; }
[hidden] { display: none !important; }
/* Todo el contenido va por encima del escenario. */
.topbar, main, .panel, .toc, .toc-backdrop, .sheet, .sheet-backdrop, .toasts, .focus-exit { position: relative; z-index: 2; }

/* --- Botones ------------------------------------------------------------------------- */

.btn {
  border: 1px solid var(--c-line);
  background: var(--c-surface-2);
  color: var(--c-ink);
  border-radius: 10px;
  padding: 8px 16px;
  cursor: pointer;
  transition: transform 0.22s var(--spring), background-color 0.2s, border-color 0.2s, box-shadow 0.25s;
}
.btn:hover { border-color: var(--c-accent); transform: translateY(-1px); box-shadow: 0 8px 20px -10px var(--c-glow); }
.btn:active { transform: translateY(0) scale(0.95); transition-duration: 0.06s; }
.btn:disabled { opacity: 0.5; cursor: default; transform: none; box-shadow: none; }
.btn.primary { background: var(--c-accent); color: var(--c-accent-ink); border-color: transparent; font-weight: 600; }
.btn.primary:hover { box-shadow: 0 10px 26px -10px var(--c-accent); }
.btn.icon { padding: 8px 11px; line-height: 1; }
.btn.small { padding: 6px 12px; font-size: 0.85rem; border-radius: 8px; }
.link-btn { background: none; border: 0; padding: 0; color: var(--c-muted); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
.link-btn:hover { color: var(--c-accent); }

/* Botón de sonido: barras de ecualizador que se mueven mientras suena. */
.sound { display: inline-flex; align-items: center; gap: 8px; }
.eq { display: inline-flex; align-items: flex-end; gap: 2px; height: 14px; }
.eq i { width: 3px; height: 100%; background: currentColor; border-radius: 1px; transform: scaleY(0.3); transform-origin: bottom; }
body.sound-on .eq i { animation: eq 0.9s ease-in-out infinite alternate; }
body.sound-on .eq i:nth-child(2) { animation-duration: 0.7s; animation-delay: -0.3s; }
body.sound-on .eq i:nth-child(3) { animation-duration: 1.1s; animation-delay: -0.6s; }
@keyframes eq { from { transform: scaleY(0.25); } to { transform: scaleY(1); } }

/* Interruptor (sí/no) */
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; cursor: pointer; font-size: 0.9rem; }
.switch input { appearance: none; width: 40px; height: 22px; border-radius: 11px; background: var(--c-line); position: relative; cursor: pointer; flex: none; margin: 0; transition: background-color 0.2s; }
.switch input::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: var(--c-ink); transition: transform 0.25s var(--spring); }
.switch input:checked { background: var(--c-accent); }
.switch input:checked::after { transform: translateX(18px); background: var(--c-accent-ink); }

/* Control segmentado (modo gráfico) */
.seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; padding: 4px; border-radius: 12px; background: var(--c-surface-2); border: 1px solid var(--c-line); }
.seg label { position: relative; text-align: center; font-size: 0.82rem; padding: 7px 4px; border-radius: 8px; cursor: pointer; color: var(--c-muted); transition: color 0.2s, background-color 0.2s; }
.seg input { position: absolute; opacity: 0; inset: 0; cursor: pointer; margin: 0; }
.seg label:has(input:checked) { background: var(--c-accent); color: var(--c-accent-ink); font-weight: 600; }
.seg label:has(input:focus-visible) { outline: 2px solid var(--c-accent); outline-offset: 1px; }

/* --- Barra superior ------------------------------------------------------------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  background: var(--c-chrome);
  border-bottom: 1px solid var(--c-line);
  transition: transform 0.35s var(--ease-out), opacity 0.35s;
}
/* El desenfoque detrás de la barra es caro en placas integradas: solo en modo inmersivo. */
body.gfx-inmersivo .topbar { backdrop-filter: blur(10px) saturate(1.2); }
.brand { background: none; border: 0; font-family: var(--display-font); font-weight: 600; font-size: 1.15rem; letter-spacing: 0.01em; cursor: pointer; padding: 0; white-space: nowrap; color: var(--c-ink); }
.brand:hover { color: var(--c-accent); }
.book-title { flex: 1; min-width: 0; color: var(--c-muted); font-size: 0.88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.book-title .chapter { color: var(--c-ink); }
.actions { display: flex; gap: 8px; margin-left: auto; align-items: center; }
.lang { border: 1px solid var(--c-line); background: var(--c-surface-2); color: var(--c-ink); border-radius: 10px; padding: 7px 8px; cursor: pointer; }
.lang option { color: #222; }

/* --- Biblioteca (pantalla principal) --------------------------------------------------- */

.library { max-width: 980px; margin: 0 auto; padding: 56px 24px 72px; }
.hero { display: grid; grid-template-columns: 1.1fr 1fr; gap: 40px; align-items: end; margin-bottom: 36px; }
.hero h1 {
  font-family: var(--display-font);
  font-weight: 600;
  font-size: clamp(2.2rem, 5vw, 3.6rem);
  line-height: 1.04;
  letter-spacing: -0.015em;
  margin: 0 0 16px;
  text-shadow: 0 2px 30px rgba(0, 0, 0, 0.25);
}
.lead { color: var(--c-muted); line-height: 1.65; font-size: 1.05rem; margin: 0; max-width: 34em; }
.hero-actions { display: grid; gap: 12px; }

.dropzone {
  position: relative;
  overflow: hidden;
  display: grid;
  place-items: center;
  gap: 6px;
  padding: 34px 20px;
  border: 1.5px dashed var(--c-line);
  border-radius: 14px;
  background: var(--c-surface);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.25s, transform 0.3s var(--spring), box-shadow 0.3s;
}
.dropzone strong { font-family: var(--display-font); font-size: 1.25rem; font-weight: 600; }
.dropzone .drop-icon { font-size: 1.8rem; transition: transform 0.35s var(--spring); }
.dropzone:hover { border-color: var(--c-accent); box-shadow: 0 18px 40px -24px var(--c-glow); }
.dropzone:hover .drop-icon { transform: translateY(-3px) rotate(-6deg); }
.dropzone.over { border-color: var(--c-accent); border-style: solid; transform: scale(1.02); box-shadow: 0 0 0 4px var(--c-glow); }
.dropzone small { color: var(--c-muted); }
/* Mientras se lee el archivo: un brillo que recorre la caja (solo transform). */
.dropzone.busy::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 30%, var(--c-glow) 50%, transparent 70%);
  transform: translateX(-100%);
  animation: shimmer 1.2s ease-in-out infinite;
}
@keyframes shimmer { to { transform: translateX(100%); } }
.sample-row { display: flex; justify-content: center; }
.status { color: var(--c-muted); min-height: 1.4em; margin: 0; text-align: center; font-size: 0.9rem; }
.status.error { color: #ff7b6b; }

.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 40px 0 14px; }
.section-title h2 { font-family: var(--display-font); font-weight: 600; font-size: 1.35rem; margin: 0; }
.section-title p { margin: 0; color: var(--c-muted); font-size: 0.88rem; }

/* Selector de escenarios */
.atm-strip { display: grid; grid-template-columns: repeat(6, minmax(130px, 1fr)); gap: 12px; overflow-x: auto; padding: 4px 2px 10px; scroll-snap-type: x mandatory; }
.atm-tile {
  position: relative;
  display: grid;
  gap: 8px;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
  scroll-snap-align: start;
  color: var(--c-ink);
}
.atm-tile .atm-preview {
  aspect-ratio: 4 / 5;
  border-radius: 12px;
  border: 1px solid var(--c-line);
  box-shadow: 0 10px 24px -16px rgba(0, 0, 0, 0.8);
  transition: transform 0.3s var(--spring), box-shadow 0.3s, border-color 0.2s;
}
.atm-tile:hover .atm-preview { transform: translateY(-4px) rotate(-0.6deg); box-shadow: 0 18px 30px -16px rgba(0, 0, 0, 0.85); }
.atm-tile:active .atm-preview { transform: scale(0.97); transition-duration: 0.08s; }
.atm-tile[aria-checked='true'] .atm-preview { border-color: var(--c-accent); box-shadow: 0 0 0 3px var(--c-glow), 0 18px 30px -16px rgba(0, 0, 0, 0.85); }
.atm-name { font-weight: 600; font-size: 0.92rem; }
.atm-mood { display: block; color: var(--c-muted); font-size: 0.78rem; line-height: 1.35; }
.atm-check {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--c-accent);
  color: var(--c-accent-ink);
  font-size: 0.8rem;
  font-weight: 700;
  transform: scale(0);
  transition: transform 0.35s var(--spring);
}
.atm-tile[aria-checked='true'] .atm-check { transform: scale(1); }

/* Biblioteca: cada libro con un lomo de color y su progreso */
.book-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.book-list li {
  display: flex;
  align-items: stretch;
  border-radius: 12px;
  background: var(--c-surface);
  border: 1px solid var(--c-line);
  overflow: hidden;
  transition: transform 0.25s var(--spring), border-color 0.2s, box-shadow 0.25s;
  animation: rise 0.45s var(--ease-out) both;
}
.book-list li:hover { transform: translateX(4px); border-color: var(--c-accent); box-shadow: 0 12px 28px -20px var(--c-glow); }
.book-list li::before { content: ''; width: 8px; flex: none; background: var(--spine, var(--c-accent)); }
.book-list button.open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 14px 16px; cursor: pointer; color: var(--c-ink); }
.book-list .book-name { display: block; font-family: var(--display-font); font-size: 1.08rem; font-weight: 600; }
.book-list .book-meta { display: block; color: var(--c-muted); font-size: 0.8rem; margin-top: 3px; }
.book-list .ai-badge { color: var(--c-accent); }
.book-list .outdated { display: block; color: var(--c-accent); font-size: 0.8rem; margin-top: 3px; }
.book-list .progress-row { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.book-list .progress-bar { flex: 1; height: 3px; border-radius: 2px; background: var(--c-line); overflow: hidden; }
.book-list .progress-bar::after { content: ''; display: block; height: 100%; background: var(--c-accent); transform-origin: left; transform: scaleX(var(--p, 0)); }
.book-list .progress { color: var(--c-muted); font-size: 0.78rem; font-variant-numeric: tabular-nums; }
.book-list button.remove { background: none; border: 0; color: var(--c-muted); cursor: pointer; padding: 0 16px; opacity: 0.5; transition: opacity 0.2s, color 0.2s; }
.book-list li:hover button.remove, .book-list button.remove:focus-visible { opacity: 1; }
.book-list button.remove:hover { color: #ff7b6b; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.site-footer { margin-top: 56px; padding-top: 20px; border-top: 1px solid var(--c-line); text-align: center; font-size: 0.85rem; }
.site-footer .links a { color: var(--c-muted); }
.site-footer .copyright { color: var(--c-muted); margin: 6px 0 0; }
.btn.donate { display: inline-block; text-decoration: none; margin-bottom: 12px; border-color: var(--c-accent); color: var(--c-accent); }

/* --- Lector: la columna de papel ------------------------------------------------------- */

main { transition: opacity 0.4s; }
.reader {
  max-width: 40rem;
  margin: 28px auto 0;
  padding: 56px 52px 70vh;
  background: var(--paper);
  color: var(--ink);
  border-radius: 4px 4px 0 0;
  box-shadow: 0 0 0 1px var(--paper-line), 0 30px 80px -30px rgba(0, 0, 0, 0.7);
  font-family: var(--reader-font);
  font-size: calc(var(--font-size) * var(--font-scale, 1));
  line-height: 1.75;
  transition: margin 0.35s var(--ease-out);
  animation: page-in 0.5s var(--ease-out) both;
}
@keyframes page-in { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.reader h1 { font-family: var(--book-heading, var(--display-font)); font-size: 1.9em; font-weight: 600; margin: 0 0 1.6em; line-height: 1.15; letter-spacing: -0.01em; }
.reader h2 { font-family: var(--book-heading, var(--display-font)); font-size: 1.3em; font-weight: 600; margin: 2.6em 0 1em; }
.reader p { margin: 0 0 1em; text-align: justify; hyphens: auto; position: relative; }
.reader hr { border: 0; text-align: center; margin: 2em 0; }
.reader hr::after { content: '· · ·'; color: var(--ink-muted); letter-spacing: 0.3em; }
.reader figure { margin: 1.5em 0; text-align: center; }
.reader figure img { max-width: 100%; max-height: 85vh; height: auto; border-radius: 3px; }
/* Muchas ilustraciones tienen fondo transparente y trazo negro: en oscuro se les pone fondo claro. */
.reader figure img { background: #f4f1ea; }
.reader a { color: var(--link); }
.reader [data-i] { transition: background-color 0.5s; border-radius: 4px; }
.reader .active { background: var(--hl); box-shadow: -12px 0 0 var(--hl), 12px 0 0 var(--hl); }

/* Opción que depende de la de arriba (se corre a la derecha y se apaga con ella) */
.check.sub { margin-left: 26px; }
.check.sub:has(input:disabled) { opacity: 0.45; }

/* Selector de tipografía (panel de lectura) */
.font-pick { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 10px; }
.font-pick button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 7px 2px 6px;
  border: 1px solid var(--c-line);
  border-radius: 10px;
  background: var(--c-surface-2);
  color: var(--c-ink);
  cursor: pointer;
  transition: border-color 0.2s, transform 0.2s var(--spring);
}
.font-pick button:hover { transform: translateY(-1px); }
.font-pick button[aria-checked='true'] { border-color: var(--c-accent); box-shadow: 0 0 0 2px var(--c-glow); }
.font-pick .aa { font-size: 1.35rem; line-height: 1.1; }
.font-pick .name { font: 0.66rem/1.2 var(--ui-font); color: var(--c-muted); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }

/* Selector de papel (panel de lectura) */
.paper-pick { display: flex; gap: 8px; margin: 10px 0 4px; }
.paper-pick button {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 1;
  padding: 6px 8px;
  border: 1px solid var(--c-line);
  border-radius: 999px;
  background: var(--c-surface-2);
  color: var(--c-ink);
  font: 0.8rem var(--ui-font);
  cursor: pointer;
  transition: border-color 0.2s, transform 0.2s var(--spring);
}
.paper-pick button:hover { transform: translateY(-1px); }
.paper-pick button[aria-checked='true'] { border-color: var(--c-accent); box-shadow: 0 0 0 2px var(--c-glow); }
.paper-pick .swatch { width: 16px; height: 16px; border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(127, 127, 127, 0.5); }
.paper-pick [data-paper='dark'] .swatch { background: #1b1b20; }
.paper-pick [data-paper='light'] .swatch { background: #fbfaf7; }
.paper-pick [data-paper='sepia'] .swatch { background: #f1e6d0; }
.reader { transition: margin 0.35s var(--ease-out), background-color 0.4s, color 0.4s; }
.book-page { transition: background-color 0.4s, color 0.4s; }

/* Marcas de cambio de sonido */
.mark {
  display: none;
  font-family: var(--ui-font);
  font-size: 0.72rem;
  line-height: 1.3;
  color: var(--ink-muted);
  border: 1px solid var(--paper-line);
  border-radius: 6px;
  padding: 2px 8px;
  margin-bottom: 4px;
  width: fit-content;
  text-align: left;
}
.mark.override { border-color: var(--link); color: var(--link); }
body.show-marks .mark { display: block; }

/* Línea en el texto que marca hasta dónde analizó la IA */
.ai-boundary { margin: 2em 0; font-family: var(--ui-font); text-align: center; color: var(--link); }
.ai-boundary .line { display: flex; align-items: center; gap: 12px; font-size: 0.85rem; font-weight: 600; }
.ai-boundary .line::before, .ai-boundary .line::after { content: ''; flex: 1; border-top: 2px dashed var(--link); opacity: 0.6; }
.ai-boundary small { display: block; margin-top: 4px; color: var(--ink-muted); font-size: 0.75rem; }

/* --- Vista de libro (js/libro.js) ----------------------------------------------------- */

body.book-view { overflow: hidden; }
body.book-view .topbar { z-index: 3; }
.book {
  /* Grano del papel: ruido SVG, sin descargar imágenes. */
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 0.95 0 0 0 0 0.85 0 0 0 0.1 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  --leather: color-mix(in srgb, var(--c-accent) 22%, #24160f);
  position: fixed;
  width: calc(var(--n) * var(--pw));
  height: var(--ph);
  /* Cerrado, el libro se corre para que la tapa quede centrada. */
  translate: var(--shift, 0px) 0;
  transition: translate 1s var(--ease-out);
  animation: book-in 0.5s var(--ease-out) both;
}
@keyframes book-in { from { opacity: 0; transform: translateY(16px) scale(0.985); } to { opacity: 1; transform: none; } }

/* Tapa de cuero con costura y lomo */
.book-cover {
  position: absolute;
  inset: -14px -18px;
  border-radius: 10px;
  background-color: var(--leather);
  background-image:
    var(--grain), var(--grain),
    radial-gradient(120% 90% at 30% 20%, rgba(255, 220, 180, 0.1), transparent 60%),
    radial-gradient(140% 120% at 50% 50%, transparent 55%, rgba(0, 0, 0, 0.45));
  background-size: 180px, 90px, auto, auto;
  box-shadow: 0 50px 90px -30px rgba(0, 0, 0, 0.9), 0 0 0 1px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 230, 200, 0.12);
  transition: left 1s var(--ease-out), right 1s var(--ease-out);
}
.book-cover::after {
  content: '';
  position: absolute;
  inset: 6px;
  border: 1px dashed rgba(255, 225, 190, 0.22);
  border-radius: 6px;
}
.book-cover::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: calc(50% - 14px);
  width: 28px;
  background: linear-gradient(90deg, rgba(0, 0, 0, 0.35), rgba(255, 220, 180, 0.06) 50%, rgba(0, 0, 0, 0.35));
}
.book.single .book-cover { inset: -8px -8px -8px -14px; }
.book.single .book-cover::before { left: 0; }

/* Cantos de las hojas: más gruesos del lado que más páginas tiene */
.book-stack {
  position: absolute;
  top: 3px;
  bottom: 3px;
  background: repeating-linear-gradient(90deg, color-mix(in srgb, var(--paper) 70%, #000) 0 1px, color-mix(in srgb, var(--paper) 88%, #fff) 1px 2px);
  transition: width 0.4s var(--ease-out), left 0.4s var(--ease-out);
}
.book-stack.left { left: calc(-1 * var(--sl, 4px)); width: var(--sl, 4px); border-radius: 3px 0 0 3px; }
.book-stack.right { left: calc(var(--n) * var(--pw)); width: var(--sr, 4px); border-radius: 0 3px 3px 0; }
.book.single .book-stack.left { display: none; }

/* Papel: grano, un poco de luz despareja y sombra hacia el lomo */
.book-page {
  position: absolute;
  top: 0;
  width: var(--pw);
  height: var(--ph);
  overflow: hidden;
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--gutter-dir, to left), var(--gutter, rgba(0, 0, 0, 0.5)), color-mix(in srgb, var(--gutter, rgba(0, 0, 0, 0.5)) 28%, transparent) 3%, transparent 11%),
    radial-gradient(130% 100% at 50% 40%, rgba(255, 240, 215, 0.035), transparent 70%),
    var(--paper-grain, var(--grain));
  background-size: auto, auto, 180px;
}
.book-page.left { left: 0; border-radius: 4px 0 0 4px; --gutter-dir: to left; }
.book-page.right { left: var(--pw); border-radius: 0 4px 4px 0; --gutter-dir: to right; }
.book.single .book-page.left { border-radius: 2px 4px 4px 2px; --gutter-dir: to right; }
.book.single .book-page.right { display: none; }
.book-head {
  position: absolute;
  top: calc(var(--pt) * 0.42);
  left: var(--px);
  right: var(--px);
  font: 600 0.66rem/1 var(--ui-font);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  text-align: center;
  color: var(--ink-muted);
  opacity: 0.75;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-folio {
  position: absolute;
  bottom: calc(var(--pb) * 0.36);
  left: var(--px);
  right: var(--px);
  font: 0.8rem/1 var(--reader-font);
  color: var(--ink-muted);
}
.book-page.left .book-folio { text-align: left; }
.book-page.right .book-folio, .book.single .book-folio { text-align: right; }

/* El texto: las páginas son columnas del #reader, corridas de a dos */
.book-text {
  position: absolute;
  left: calc(var(--px) - var(--hlpad));
  top: var(--pt);
  width: calc(var(--n) * var(--pw) - 2 * var(--px) + 2 * var(--hlpad));
  height: calc(var(--ph) - var(--pt) - var(--pb));
  overflow: hidden;
}
.book-text.copy { width: calc(var(--pw) - 2 * var(--px) + 2 * var(--hlpad)); }
.book .reader.paged {
  position: absolute;
  left: var(--hlpad);
  top: 0;
  width: calc(var(--pw) - 2 * var(--px));
  max-width: none;
  height: calc(var(--ph) - var(--pt) - var(--pb));
  margin: 0;
  padding: 0;
  background: none;
  box-shadow: none;
  border-radius: 0;
  animation: none;
  transition: none;
  column-count: 1;
  column-gap: calc(2 * var(--px));
  column-fill: auto;
}
.book .reader.paged h1 { break-after: column; margin: 0; padding-top: 30%; text-align: center; font-size: 2.1em; }
.book .reader.paged h1::after { content: '❦'; display: block; margin-top: 1.2em; font-size: 0.6em; color: var(--ink-muted); }
.book .reader.paged h2 { break-after: avoid; }
.book .reader.paged figure { break-inside: avoid; }
.book .reader.paged figure img { max-height: calc(var(--ph) - var(--pt) - var(--pb) - 3em); }
.book .reader.paged p { cursor: pointer; }

/* Zonas para pasar de página (los márgenes de afuera) */
.book-nav {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 4;
  width: var(--px);
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--ink-muted);
  font-size: 1.8rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s;
}
.book-nav.prev { left: 0; }
.book-nav.next { right: 0; }
.book-nav:hover, .book-nav:focus-visible { opacity: 0.85; }
@media (hover: none) { .book-nav { opacity: 0.3; } }

/* Esquina que se levanta al pasar el mouse por "siguiente" */
.book-page.right::after, .book.single .book-page.left::after {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  width: 0;
  height: 0;
  background: linear-gradient(315deg, var(--c-bg) 50%, color-mix(in srgb, var(--paper) 80%, #fff) 50%, var(--paper) 75%);
  box-shadow: -3px -3px 8px rgba(0, 0, 0, 0.35);
  border-top-left-radius: 6px;
  transition: width 0.25s var(--spring), height 0.25s var(--spring);
}
.book:has(.book-nav.next:hover) .book-page.right:not(.copy)::after,
.book.single:has(.book-nav.next:hover) .book-page.left:not(.copy)::after { width: 34px; height: 34px; }
.book-page.copy::after { display: none; }

/* Libro cerrado: se ve solo la mitad de la tapa (o de la contratapa) */
.book.hide-left > .book-page.left, .book.hide-left > .book-stack.left,
.book.hide-right > .book-page.right, .book.hide-right > .book-stack.right { visibility: hidden; }
.book.hide-left > .book-text { clip-path: inset(0 0 0 calc(var(--pw) - var(--px) + var(--hlpad))); }
.book.hide-right > .book-text { clip-path: inset(0 calc(var(--pw) - var(--px) + var(--hlpad)) 0 0); }
.book.leather-right > .book-cover { left: calc(var(--pw) - 8px); }
.book.leather-left > .book-cover { right: calc(var(--pw) - 8px); }
.book.leather-right > .book-cover::before, .book.leather-left > .book-cover::before { opacity: 0; }
.book.closed-front > .book-nav.prev, .book.closed-back > .book-nav.next { display: none; }

/* Tapas duras: la imagen del libro o cuero con el título en relieve dorado */
.book-art {
  --gold: #e3c27a;
  position: absolute;
  top: 0;
  z-index: 3;
  display: none;
  width: var(--pw);
  height: var(--ph);
  overflow: hidden;
  cursor: pointer;
  border-radius: 3px 7px 7px 3px;
  background-color: var(--leather);
  background-image:
    var(--grain), var(--grain),
    radial-gradient(110% 80% at 35% 25%, rgba(255, 225, 185, 0.12), transparent 60%),
    radial-gradient(140% 120% at 50% 50%, transparent 50%, rgba(0, 0, 0, 0.5));
  background-size: 180px, 90px, auto, auto;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 230, 200, 0.15);
  transform-origin: left center;
  transition: transform 0.35s var(--ease-out);
}
.book-art.front { left: var(--pw); }
.book-art.back { left: 0; border-radius: 7px 3px 3px 7px; transform-origin: right center; }
.book.single .book-art { left: 0; }
.book.closed-front > .book-art.front, .book.closed-back > .book-art.back, .face > .book-art { display: block; }
.face > .book-art { left: 0; }
/* Al pasar el mouse por la tapa cerrada, se entreabre un poco. */
.book.closed-front > .book-art.front:hover { transform: perspective(1400px) rotateY(-7deg); }
.book.closed-back > .book-art.back:hover { transform: perspective(1400px) rotateY(7deg); }
.book-art img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.book-art.back img { filter: blur(18px) brightness(0.4) saturate(1.2); transform: scale(1.2); }
.art-frame {
  position: absolute;
  inset: 7% 10%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5%;
  padding: 8%;
  text-align: center;
  border: 1.5px solid color-mix(in srgb, var(--gold) 55%, transparent);
  outline: 1px solid color-mix(in srgb, var(--gold) 25%, transparent);
  outline-offset: 6px;
  border-radius: 2px;
}
.art-title {
  font-family: var(--display-font);
  font-size: calc(var(--pw) * 0.085);
  font-weight: 600;
  line-height: 1.15;
  color: var(--gold);
  /* Relieve: sombra abajo y brillo arriba */
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.7), 0 -1px 0 rgba(255, 245, 210, 0.3);
}
.art-orn { font-size: calc(var(--pw) * 0.07); color: var(--gold); opacity: 0.85; text-shadow: 0 1px 0 rgba(0, 0, 0, 0.7); }
.art-author, .art-small {
  font: 600 calc(var(--pw) * 0.03) / 1.4 var(--ui-font);
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--gold);
  opacity: 0.8;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.7);
}
.art-hinge { position: absolute; top: 0; bottom: 0; left: 14px; width: 6px; background: linear-gradient(90deg, rgba(0, 0, 0, 0.45), rgba(255, 240, 220, 0.1), rgba(0, 0, 0, 0.3)); }
.book-art.back .art-hinge { left: auto; right: 14px; }
.art-gloss { position: absolute; inset: 0; pointer-events: none; background: linear-gradient(115deg, rgba(255, 255, 255, 0.13), transparent 32%, transparent 68%, rgba(0, 0, 0, 0.28)); }

/* La hoja que gira */
.book-flip { position: absolute; inset: 0; z-index: 3; perspective: 2400px; pointer-events: none; }
.leaf { position: absolute; top: 0; width: var(--pw); height: var(--ph); transform-style: preserve-3d; will-change: transform; }
.leaf.right { left: var(--pw); transform-origin: left center; }
.leaf.left { left: 0; transform-origin: right center; }
.leaf.single { transform-origin: left center; }
.face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.face.back { transform: rotateY(180deg); }
.face > .book-page { left: 0; }
.leaf.right .face.front, .leaf.left .face.back { box-shadow: 8px 0 24px -10px rgba(0, 0, 0, 0.6); }
.leaf.left .face.front, .leaf.right .face.back { box-shadow: -8px 0 24px -10px rgba(0, 0, 0, 0.6); }
.shade { position: absolute; inset: 0; opacity: 0; pointer-events: none; }
.leaf.right .front .shade, .leaf.left .back .shade, .leaf.single .shade { background: linear-gradient(to left, rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.2) 70%, rgba(255, 255, 255, 0.04)); }
.leaf.left .front .shade, .leaf.right .back .shade { background: linear-gradient(to right, rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.2) 70%, rgba(255, 255, 255, 0.04)); }
.cast { position: absolute; top: 0; width: var(--pw); height: var(--ph); pointer-events: none; }
.cast.right { left: var(--pw); background: linear-gradient(to right, rgba(0, 0, 0, 0.55), transparent 60%); }
.cast.left { left: 0; background: linear-gradient(to left, rgba(0, 0, 0, 0.55), transparent 60%); }
.book.single .cast { left: 0; }

/* Modo lectura: se esconden los controles. */
body.focus .topbar { transform: translateY(-100%); opacity: 0; pointer-events: none; }
body.focus.peek .topbar { transform: none; opacity: 1; pointer-events: auto; }
body.focus .panel, body.focus .toc, body.focus .toc-backdrop { display: none; }
body.focus .reader { margin-top: 0; }
body.focus.panel-open .reader { margin-right: auto; }
.focus-exit {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 11;
  opacity: 0.25;
  transition: opacity 0.25s, transform 0.25s var(--spring);
}
.focus-exit:hover, .focus-exit:focus-visible { opacity: 1; }

/* --- Paneles: índice, sonido y hojas ----------------------------------------------------- */

.panel, .toc, .sheet { background: var(--c-surface); color: var(--c-ink); border-color: var(--c-line); }
.panel a, .toc a { color: var(--c-accent); }

.toc-backdrop, .sheet-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 15; animation: fade-in 0.3s both; }
.toc {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  bottom: 0;
  width: min(340px, 88vw);
  overflow-y: auto;
  border-right: 1px solid var(--c-line);
  box-shadow: 18px 0 40px -20px rgba(0, 0, 0, 0.6);
  z-index: 20;
  padding: 8px 0 24px;
  animation: slide-left 0.38s var(--ease-out) both;
}
.toc-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 16px 8px; }
.toc-head h3, .panel h3, .sheet h3 { margin: 0 0 10px; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.12em; color: var(--c-muted); font-weight: 600; }
.toc-head h3 { margin: 0; }
.toc-list { list-style: none; margin: 0; padding: 0; }
.toc-list button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  border-left: 3px solid transparent;
  padding: 10px 16px 10px 13px;
  cursor: pointer;
  line-height: 1.35;
  font-size: 0.92rem;
  color: var(--c-ink);
  transition: background-color 0.2s, border-color 0.2s, padding 0.25s var(--ease-out);
}
.toc-list button:hover { background: var(--c-surface-2); padding-left: 17px; }
.toc-list button.current { border-left-color: var(--c-accent); background: var(--c-surface-2); font-weight: 600; }
.toc #toc-empty { padding: 0 16px; }

.panel {
  position: fixed;
  top: var(--topbar-h);
  right: 0;
  bottom: 0;
  width: var(--panel-w);
  overflow-y: auto;
  border-left: 1px solid var(--c-line);
  padding: 8px 18px 24px;
  z-index: 5;
  animation: slide-right 0.4s var(--ease-out) both;
}
.panel section { padding: 14px 0; border-bottom: 1px solid var(--c-line); }
.panel section:last-child { border-bottom: 0; }

.scene { margin: 0; display: grid; gap: 7px; }
.scene div { display: flex; justify-content: space-between; gap: 8px; }
.scene dt { color: var(--c-muted); }
.scene dd { margin: 0; font-weight: 600; text-align: right; }
.scene dd.changed { animation: value-pop 0.5s var(--spring); }
@keyframes value-pop { from { transform: translateY(4px); opacity: 0.2; } to { transform: none; opacity: 1; } }

.intensity { margin-top: 12px; display: flex; align-items: center; gap: 10px; color: var(--c-muted); font-size: 0.9rem; }
.meter { flex: 1; height: 6px; border-radius: 3px; background: var(--c-line); overflow: hidden; }
.meter div { height: 100%; width: 100%; background: linear-gradient(90deg, #6aa86a, #e0b84a, #d0503a); transform-origin: left; transform: scaleX(0); transition: transform 0.6s var(--ease-out); }

.slider { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 9px 0; font-size: 0.9rem; }
.slider input[type='range'] { width: 55%; accent-color: var(--c-accent); }

.hint { font-size: 0.85rem; color: var(--c-muted); margin: 0 0 10px; line-height: 1.45; }
.override { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.override label { display: flex; flex-direction: column; gap: 3px; font-size: 0.8rem; color: var(--c-muted); }
.panel select, .sheet select, .ai-setup input[type='password'] {
  width: 100%;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid var(--c-line);
  background: var(--c-surface-2);
  color: var(--c-ink);
}
.panel select option, .sheet select option { color: #222; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row select { width: auto; flex: 1; }
.check { display: flex; gap: 8px; align-items: center; margin-top: 12px; font-size: 0.9rem; }
.check input { accent-color: var(--c-accent); }

.mode { display: flex; gap: 16px; margin-bottom: 8px; font-size: 0.9rem; }
.mode label { display: flex; gap: 6px; align-items: center; }
.mode input { accent-color: var(--c-accent); }
.ai-setup { display: grid; gap: 8px; margin-bottom: 10px; }
.source { margin: 8px 0 0; font-size: 0.78rem; }
#btn-ai-goto { margin: 0 0 10px; }
.ai-help { font-size: 0.85rem; line-height: 1.45; }
.ai-help p { margin: 0 0 4px; }
.ai-help a { font-weight: 600; }
.ai-help summary { cursor: pointer; color: var(--c-muted); }
.ai-help ol { margin: 6px 0; padding-left: 20px; }
.ai-help li { margin: 4px 0; }
.ai-help details p { color: var(--c-muted); }
#btn-amb-save { margin-top: 8px; }
#amb-state { margin: 8px 0 0; }
.info-title { margin: 0 0 4px; font-weight: 600; }
.lookup-result { margin-top: 10px; font-size: 0.9rem; }
.lookup-result p { margin: 0 0 8px; }
.auto { margin-top: 14px; }
.auto .slider { flex-direction: column; align-items: stretch; gap: 6px; }
.auto .slider input { width: 100%; }
.auto .hint { margin-top: 6px; }

/* Hoja de ambientes (selector + modo gráfico) */
.sheet {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 30;
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  overflow-y: auto;
  border: 1px solid var(--c-line);
  border-radius: 16px;
  padding: 22px 22px 26px;
  box-shadow: 0 40px 100px -30px rgba(0, 0, 0, 0.85);
  transform: translate(-50%, -50%);
  animation: sheet-in 0.42s var(--spring) both;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.sheet-head h2 { font-family: var(--display-font); font-weight: 600; font-size: 1.4rem; margin: 0; }
.sheet .atm-strip { grid-template-columns: repeat(3, minmax(0, 1fr)); overflow: visible; }
.sheet .atm-tile .atm-preview { aspect-ratio: 16 / 10; }
.sheet-section { margin-top: 22px; display: grid; gap: 12px; }
.sheet-section h3 { margin: 0; }

/* Avisos */
.toasts {
  position: fixed;
  left: 50%;
  bottom: 20px;
  z-index: 40;
  display: grid;
  gap: 8px;
  justify-items: center;
  transform: translateX(-50%);
  pointer-events: none;
  width: max-content;
  max-width: calc(100vw - 32px);
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--c-surface);
  color: var(--c-ink);
  border: 1px solid var(--c-line);
  box-shadow: 0 18px 40px -18px rgba(0, 0, 0, 0.8);
  font-size: 0.9rem;
  animation: toast-in 0.4s var(--spring) both;
}
.toast.leaving { animation: toast-out 0.28s ease-in both; }
.toast-icon { font-size: 1.05rem; }

.load-error { position: fixed; left: 16px; right: 16px; bottom: 16px; z-index: 100; padding: 14px 16px; border-radius: 12px; background: #c0392b; color: #fff; font-weight: 600; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-right { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes slide-left { from { transform: translateX(-24px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes sheet-in { from { opacity: 0; transform: translate(-50%, -46%) scale(0.96); } to { opacity: 1; transform: translate(-50%, -50%); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(12px) scale(0.96); } to { opacity: 1; transform: none; } }
@keyframes toast-out { to { opacity: 0; transform: translateY(8px); } }

/* --- Pantallas anchas y chicas --------------------------------------------------------- */

/* Panel abierto en pantallas anchas: el papel se corre a la izquierda */
@media (min-width: 1000px) {
  body.panel-open .reader { margin-right: calc(var(--panel-w) + max(16px, (100vw - var(--panel-w) - 40rem) / 2)); }
}

@media (max-width: 999px) {
  .panel {
    top: auto;
    left: 0;
    width: auto;
    max-height: 60vh;
    border-left: 0;
    border-top: 1px solid var(--c-line);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -12px 40px -10px rgba(0, 0, 0, 0.6);
    animation-name: slide-up;
  }
  .brand span { display: none; }
  .topbar { gap: 6px; padding: 0 10px; }
  .actions { gap: 6px; }
  .sound .label { display: none; }
  .hero { grid-template-columns: 1fr; gap: 24px; }
  :root { --font-size: 18px; }
}
@keyframes slide-up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }

@media (max-width: 700px) {
  .library { padding: 32px 16px 56px; }
  .reader { margin-top: 0; padding: 32px 20px 70vh; border-radius: 0; box-shadow: none; }
  .sheet { top: auto; bottom: 0; left: 0; width: 100%; transform: none; border-radius: 16px 16px 0 0; max-height: 85vh; animation-name: slide-up; }
  .sheet .atm-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .atm-strip { grid-template-columns: repeat(6, 42%); }
  .seg { grid-template-columns: repeat(2, 1fr); }
}

/* --- Menos movimiento -------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
body.no-motion *, body.no-motion *::before, body.no-motion *::after {
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}
