* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #1a252f;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  gap: 15px;
  overflow: hidden;
}

#loading {
  color: white;
  font-size: 1.2rem;
  font-weight: bold;
}

/* Panel Lateral del Índice (TOC) */
.toc-menu {
  position: fixed;
  top: 0;
  left: -320px;
  width: 300px;
  height: 100vh;
  background: rgba(26, 37, 47, 0.95);
  backdrop-filter: blur(15px);
  box-shadow: 5px 0 25px rgba(0, 0, 0, 0.5);
  z-index: 200;
  transition: left 0.4s cubic-bezier(0.1, 0.9, 0.2, 1);
  padding: 25px 20px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  color: white;
}

.toc-menu.active {
  left: 0;
}

.toc-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  padding-bottom: 12px;
}

.toc-header h3 {
  font-size: 1.1rem;
  color: #3498db;
}

#toc-close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1.8rem;
  cursor: pointer;
  padding: 0;
  line-height: 1;
}

.toc-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}

.toc-link {
  color: #ecf0f1;
  text-decoration: none;
  font-size: 0.95rem;
  padding: 10px 12px;
  border-radius: 8px;
  display: block;
  background: rgba(255, 255, 255, 0.05);
  transition: background 0.2s, color 0.2s, transform 0.1s;
}

.toc-link:hover {
  background: rgba(52, 152, 219, 0.3);
  color: #3498db;
  transform: translateX(4px);
}

/* Buscador de Palabras en Barra de Controles */
.search-box {
  display: flex;
  align-items: center;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 20px;
  padding: 2px 6px 2px 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.search-box input {
  background: transparent;
  border: none;
  outline: none;
  color: white;
  font-size: 13px;
  width: 120px;
}

.search-box input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.search-box button {
  padding: 5px 10px;
  font-size: 12px;
}

/* Panel Desplegable de Resultados de Búsqueda */
.search-panel {
  position: fixed;
  top: 70px;
  right: 20px;
  width: 320px;
  max-height: 400px;
  background: rgba(26, 37, 47, 0.95);
  backdrop-filter: blur(15px);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  z-index: 150;
  padding: 15px;
  color: white;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.search-panel.hidden {
  display: none;
}

.search-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.85rem;
  color: #3498db;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding-bottom: 8px;
}

#close-search-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1.4rem;
  cursor: pointer;
  line-height: 1;
}

.search-results-list {
  list-style: none;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.search-result-item {
  background: rgba(255, 255, 255, 0.05);
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.search-result-item:hover {
  background: rgba(52, 152, 219, 0.3);
  transform: translateX(3px);
}

.search-result-page {
  font-weight: bold;
  font-size: 0.8rem;
  color: #e74c3c;
  margin-bottom: 3px;
}

.search-result-snippet {
  font-size: 0.85rem;
  color: #ecf0f1;
  line-height: 1.3;
}

.search-result-snippet mark {
  background-color: #f1c40f;
  color: #2c3e50;
  border-radius: 2px;
  padding: 0 2px;
}

/* Barra de Controles Superior */
.controls {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(255, 255, 255, 0.12);
  padding: 10px 20px;
  border-radius: 30px;
  backdrop-filter: blur(10px);
  z-index: 100;
  box-shadow: 0 4px 15px rgba(0,0,0,0.3);
}

.separator {
  width: 1px;
  height: 20px;
  background-color: rgba(255, 255, 255, 0.3);
  margin: 0 2px;
}

button {
  border: none;
  background-color: #e74c3c;
  color: white;
  padding: 8px 14px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: background 0.3s, transform 0.1s;
}

button:hover:not(:disabled) {
  background-color: #c0392b;
  transform: translateY(-1px);
}

button:disabled {
  background-color: #7f8c8d;
  cursor: not-allowed;
  opacity: 0.5;
}

#page-indicator, #zoom-indicator {
  color: white;
  font-size: 0.85rem;
  font-weight: 500;
}

/* Área de Visualización y Botones Laterales */
.viewport {
  position: relative;
  width: 100%;
  height: calc(100vh - 100px);
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 25px;
  overflow: auto;
  padding: 20px;
}

/* Botones Flotantes Laterales */
.side-btn {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  font-size: 1.3rem;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4);
  z-index: 80;
  flex-shrink: 0;
}

.side-btn:hover:not(:disabled) {
  background: #e74c3c;
  border-color: #e74c3c;
  transform: scale(1.15);
  box-shadow: 0 10px 25px rgba(231, 76, 60, 0.5);
}

.side-btn:disabled {
  opacity: 0.15;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* Contenedor del Libro Abierto (Perspectiva 3D) */
.book {
  position: relative;
  width: 700px;
  height: 500px;
  perspective: 2000px;
  transition: transform 0.4s ease-out, box-shadow 0.4s ease;
  transform-origin: center center;
  user-select: none;
  touch-action: pan-y;
  box-shadow: 
    0 25px 50px -12px rgba(0, 0, 0, 0.8),
    0 10px 20px -5px rgba(0, 0, 0, 0.5);
  flex-shrink: 0;
}

/* 📖 CONFIGURACIÓN DE PORTADA / CONTRAPORTADA ÚNICA */
.book.closed-cover {
  box-shadow: none;
}

.book.closed-cover::before,
.book.closed-cover::after {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.book.closed-cover .paper:not(.flipped) .front,
.book.closed-cover .paper.flipped .back {
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.7);
  border-radius: 4px;
}

.book::before,
.book::after {
  transition: opacity 0.4s ease, visibility 0.4s ease;
}

/* Sombra Realista del Lomo Central */
.book::after {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  width: 24px;
  height: 100%;
  transform: translateX(-50%);
  background: linear-gradient(to right, 
    rgba(0, 0, 0, 0.35) 0%, 
    rgba(0, 0, 0, 0.05) 45%, 
    rgba(0, 0, 0, 0.05) 55%, 
    rgba(0, 0, 0, 0.35) 100%
  );
  z-index: 50;
  pointer-events: none;
}

/* Estructura 3D de cada Hoja */
.paper {
  position: absolute;
  width: 50%;
  height: 100%;
  top: 0;
  left: 50%;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform 0.5s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.front,
.back {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background-color: #fcfaf7;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)' opacity='0.05'/%3E%3C/svg%3E");
  border: 1px solid #dcdcdc;
  backface-visibility: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

/* Reflejo de Luz Dinámico en las Hojas */
.front::before,
.back::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
  transition: opacity 0.5s ease-in-out, background 0.5s ease-in-out;
}

.front {
  z-index: 2;
  border-radius: 0 4px 4px 0;
  box-shadow: 4px 2px 12px rgba(0, 0, 0, 0.25);
}

.front::before {
  background: linear-gradient(
    135deg, 
    rgba(255, 255, 255, 0.35) 0%, 
    rgba(255, 255, 255, 0.05) 45%, 
    rgba(0, 0, 0, 0.08) 100%
  );
  opacity: 0.3;
}

.paper.flipped .front::before {
  opacity: 0.85;
  background: linear-gradient(
    90deg, 
    rgba(255, 255, 255, 0.7) 0%, 
    rgba(255, 255, 255, 0.1) 50%, 
    rgba(0, 0, 0, 0.3) 100%
  );
}

.back {
  z-index: 1;
  transform: rotateY(180deg);
  border-radius: 4px 0 0 4px;
  box-shadow: -4px 2px 12px rgba(0, 0, 0, 0.25);
}

.back::before {
  background: linear-gradient(
    225deg, 
    rgba(255, 255, 255, 0.7) 0%, 
    rgba(255, 255, 255, 0.1) 50%, 
    rgba(0, 0, 0, 0.3) 100%
  );
  opacity: 0.85;
}

.paper.flipped .back::before {
  opacity: 0.3;
  background: linear-gradient(
    225deg, 
    rgba(255, 255, 255, 0.35) 0%, 
    rgba(255, 255, 255, 0.05) 45%, 
    rgba(0, 0, 0, 0.08) 100%
  );
}

.paper.flipped {
  transform: rotateY(-180deg);
}

canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* Adaptación Móvil */
@media (max-width: 740px) {
  .book {
    width: 560px;
    height: 400px;
  }
}

@media (max-width: 580px) {
  .book {
    width: 360px;
    height: 260px;
  }
  .side-btn {
    width: 42px;
    height: 42px;
    font-size: 1rem;
  }
}
