/* 🎨 PALETA DE COLORES CORPORATIVOS */
:root {
  --color-primario: #37ab9c;       /* Teal Principal */
  --color-secundario: #80cc28;     /* Verde Lima de Acento */
  --color-primario-hover: #2c8a7e; /* Hover botones principales */
  --color-secundario-hover: #6eb320;/* Hover secundario */
  --color-fondo-app: #1a252f;      /* Fondo oscuro */
  --color-barra: rgba(26, 37, 47, 0.92); /* Fondo flotante controles */
  --color-texto: #ffffff;           /* Texto principal */
}

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

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

/* 🌟 PANTALLA DE CARGA ATRACTIVA */
#loading {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(15, 23, 42, 0.94);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 15px;
  z-index: 999;
  transition: opacity 0.6s ease, visibility 0.6s ease;
}

#loading.hidden-loader {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.spinner {
  width: 65px;
  height: 65px;
  border: 5px solid rgba(255, 255, 255, 0.08);
  border-top: 5px solid var(--color-primario);
  border-right: 5px solid var(--color-secundario);
  border-radius: 50%;
  animation: spin 0.9s cubic-bezier(0.68, -0.55, 0.265, 1.55) infinite;
  box-shadow: 0 0 25px rgba(55, 171, 156, 0.35);
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  color: var(--color-texto);
  font-size: 1.2rem;
  font-weight: 600;
  letter-spacing: 0.5px;
  animation: pulse 1.6s ease-in-out infinite;
}

.loading-subtext {
  color: var(--color-secundario);
  font-size: 0.85rem;
  font-weight: 500;
  opacity: 0.8;
}

@keyframes pulse {
  0%, 100% { opacity: 0.5; transform: scale(0.98); }
  50% { opacity: 1; transform: scale(1); }
}

/* 📊 BARRA DE AVANCE DE LECTURA INTERACTIVA (INFERIOR) */
.progress-bar-container {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 10px;
  background: rgba(255, 255, 255, 0.1);
  z-index: 120;
  cursor: pointer;
  transition: height 0.2s ease;
}

.progress-bar-container:hover {
  height: 14px;
}

.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--color-primario), var(--color-secundario));
  transition: width 0.4s ease;
  box-shadow: 0 0 10px rgba(128, 204, 40, 0.6);
  pointer-events: none;
}

.progress-bar-text {
  position: absolute;
  right: 15px;
  bottom: 14px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--color-secundario);
  background: rgba(0, 0, 0, 0.6);
  padding: 2px 8px;
  border-radius: 10px;
  backdrop-filter: blur(4px);
  pointer-events: none;
}

/* Panel Lateral del Índice (TOC) */
.toc-menu {
  position: fixed;
  top: 0;
  left: -320px;
  width: 300px;
  height: 100vh;
  background: var(--color-barra);
  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: var(--color-texto);
}

.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: var(--color-secundario);
}

#toc-close-btn {
  background: none;
  border: none;
  color: var(--color-texto);
  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(128, 204, 40, 0.25);
  color: var(--color-secundario);
  transform: translateX(4px);
}

/* 🔍 VENTANA FLOTANTE DE BÚSQUEDA INTUITIVA */
.search-panel {
  position: fixed;
  top: 75px;
  left: 50%;
  transform: translateX(-50%);
  width: 90%;
  max-width: 450px;
  max-height: 500px;
  background: var(--color-barra);
  backdrop-filter: blur(16px);
  border-radius: 16px;
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.6);
  z-index: 180;
  padding: 20px;
  color: var(--color-texto);
  display: flex;
  flex-direction: column;
  gap: 12px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

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

.search-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-panel-header h3 {
  font-size: 1rem;
  color: var(--color-secundario);
}

#close-search-btn {
  background: none;
  border: none;
  color: var(--color-texto);
  font-size: 1.5rem;
  cursor: pointer;
}

.search-box-modal {
  display: flex;
  gap: 8px;
}

.search-box-modal input {
  flex: 1;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 8px 16px;
  color: white;
  outline: none;
  font-size: 0.9rem;
}

.search-box-modal input:focus {
  border-color: var(--color-secundario);
}

.search-status {
  font-size: 0.8rem;
  color: rgba(255, 255, 255, 0.7);
}

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

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

.search-result-item:hover {
  background: rgba(55, 171, 156, 0.3);
  transform: translateX(3px);
}

.search-result-page {
  font-weight: bold;
  font-size: 0.8rem;
  color: var(--color-secundario);
  margin-bottom: 3px;
}

.search-result-snippet mark {
  background-color: var(--color-secundario);
  color: #1a252f;
  border-radius: 2px;
  padding: 0 3px;
  font-weight: bold;
}

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

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

button {
  border: none;
  background-color: var(--color-primario);
  color: var(--color-texto);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.3s, transform 0.1s, box-shadow 0.2s;
  flex-shrink: 0;
}

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

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

#page-indicator, #zoom-indicator {
  color: var(--color-secundario);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0 4px;
}

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

/* Botones Flotantes Laterales */
.side-btn {
  background: rgba(255, 255, 255, 0.12);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: var(--color-texto);
  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: var(--color-secundario);
  border-color: var(--color-secundario);
  color: #1a252f;
  transform: scale(1.15);
  box-shadow: 0 10px 25px rgba(128, 204, 40, 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: manipulation;
  cursor: pointer;
  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;
}

/* Portada y Contraportada Cerradas */
.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: 20px;
  height: 100%;
  transform: translateX(-50%);
  background: linear-gradient(to right, 
    rgba(0, 0, 0, 0.25) 0%, 
    rgba(0, 0, 0, 0.05) 45%, 
    rgba(0, 0, 0, 0.05) 55%, 
    rgba(0, 0, 0, 0.25) 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;
  -webkit-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;
  -webkit-backface-visibility: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

/* 📖 SEPARACIÓN DEL CONTENIDO RESPECTO AL LOMO CENTRAL */
.front {
  transform: rotateY(0deg);
  border-radius: 0 4px 4px 0;
  box-shadow: 4px 2px 12px rgba(0, 0, 0, 0.25);
  padding: 6px 6px 6px 16px; /* 👈 Espaciado izquierdo para separar del lomo */
}

.back {
  transform: rotateY(180deg);
  border-radius: 4px 0 0 4px;
  box-shadow: -4px 2px 12px rgba(0, 0, 0, 0.25);
  padding: 6px 16px 6px 6px; /* 👈 Espaciado derecho para separar del lomo */
}

/* Reflejos de Luz Dinámicos */
.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::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::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;
}

/* 📱 OPTIMIZACIÓN AVANZADA PARA DISPOSITIVOS MÓVILES */
@media (max-width: 740px) {
  .book {
    width: 560px;
    height: 400px;
  }
}

@media (max-width: 600px) {
  .controls {
    padding: 6px 12px;
    gap: 5px;
    max-width: 96vw;
    flex-wrap: wrap;
    justify-content: center;
  }

  .separator {
    height: 14px;
    margin: 0 1px;
  }

  button {
    width: 32px;
    height: 32px;
    font-size: 12px;
  }

  .book {
    width: 320px;
    height: 230px;
  }

  .side-btn {
    width: 38px;
    height: 38px;
    font-size: 0.9rem;
  }

  .progress-bar-container {
    height: 12px;
  }

  .front {
    padding: 3px 3px 3px 10px;
  }

  .back {
    padding: 3px 10px 3px 3px;
  }
}

@media (max-width: 580px) {
  .book {
    width: 360px;
    height: 260px;
  }
}