:root {
  --accent: #e11d48;
  --accent-hover: #be123c;
  --bg: #0c0e14;
  --bg-radial: radial-gradient(circle at 50% 35%, #191d2b 0%, #0c0e14 75%);
  --surface: rgba(22, 26, 36, 0.78);
  --surface-solid: #151822;
  --surface-hover: rgba(255, 255, 255, 0.1);
  --border: rgba(255, 255, 255, 0.1);
  --text: #f3f4f6;
  --text-muted: #9ca3af;
  --shadow-book: 0 24px 64px rgba(0, 0, 0, 0.65), 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-ui: 0 10px 30px rgba(0, 0, 0, 0.35);
  --radius-pill: 999px;
  --radius-md: 12px;
  --topbar-h: 64px;
  --bottombar-h: 72px;
}

[data-theme="light"] {
  --bg: #eef0f4;
  --bg-radial: radial-gradient(circle at 50% 35%, #ffffff 0%, #e5e8ef 80%);
  --surface: rgba(255, 255, 255, 0.85);
  --surface-solid: #ffffff;
  --surface-hover: rgba(15, 23, 42, 0.07);
  --border: rgba(15, 23, 42, 0.1);
  --text: #0f172a;
  --text-muted: #64748b;
  --shadow-book: 0 20px 50px rgba(15, 23, 42, 0.18), 0 4px 12px rgba(15, 23, 42, 0.08);
  --shadow-ui: 0 8px 24px rgba(15, 23, 42, 0.1);
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  background-image: var(--bg-radial);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  user-select: none;
  touch-action: manipulation;
}

/* ==================== TOPBAR ==================== */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-h);
  padding: 0 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--surface);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo-link {
  display: inline-flex;
  align-items: center;
  height: 100%;
  text-decoration: none;
  flex-shrink: 0;
  transition: opacity 0.16s ease, transform 0.16s ease;
}

.brand-logo-link[href]:hover {
  opacity: 0.9;
  transform: scale(1.02);
}

.brand-logo-link:not([href]) {
  cursor: default;
}

.brand-logo-link[hidden] {
  display: none;
}

.brand-logo {
  height: 38px;
  width: auto;
  display: block;
  flex-shrink: 0;
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.25));
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 12px var(--accent);
  flex-shrink: 0;
}

.brand-dot[hidden] {
  display: none;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-text[hidden] {
  display: none;
}

.catalog-title {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.catalog-subtitle {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.18s ease, transform 0.15s ease, border-color 0.18s ease;
  flex-shrink: 0;
}

.icon-btn:hover {
  background: var(--surface-hover);
  border-color: var(--border);
}

.icon-btn:active {
  transform: scale(0.95);
}

.icon-btn svg {
  width: 19px;
  height: 19px;
}

[data-theme="dark"] .icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }

#btn-sound .icon-sound-off { display: none; }
#btn-sound.is-muted .icon-sound-on { display: none; }
#btn-sound.is-muted .icon-sound-off { display: block; opacity: 0.6; }

.download-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border-radius: var(--radius-pill);
  background: var(--accent);
  color: #ffffff;
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 40%, transparent);
  transition: background 0.18s ease, transform 0.15s ease, box-shadow 0.18s ease;
  flex-shrink: 0;
}

.download-btn:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.download-btn svg {
  width: 16px;
  height: 16px;
}

.download-size {
  font-size: 11px;
  font-weight: 500;
  opacity: 0.88;
  padding-left: 6px;
  border-left: 1px solid rgba(255, 255, 255, 0.3);
}

.download-size:empty {
  display: none;
}

/* Menú desplegable de herramientas (transparente en escritorio, selector en móviles) */
.tools-menu {
  position: relative;
  display: flex;
  align-items: center;
}

.tools-toggle {
  display: none;
}

.tools-panel {
  display: flex;
  align-items: center;
  gap: 10px;
}

.tool-label {
  display: none;
}

/* ==================== STAGE & BOOK (PANTALLA COMPLETA SIN MÁSCARA) ==================== */
.stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: visible;
}

.viewport {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.r3d-container {
  position: absolute !important;
  inset: 0 !important;
  width: 100% !important;
  height: 100% !important;
}

/* Mantener únicamente el lienzo 3D WebGL de Real3D a pantalla completa y ocultar sus controles antiguos */
.r3d-container .flipbook-main-wrapper {
  background: transparent !important;
  --flipbook-link-color: transparent !important;
  --flipbook-link-color-hover: transparent !important;
  --flipbook-link-opacity: 0 !important;
  top: 0 !important;
  bottom: 0 !important;
  left: 0 !important;
  right: 0 !important;
  width: 100% !important;
  height: 100% !important;
}

/* Sombra difuminada y suave por GPU (sin pixelado de shadowMap WebGL) */
.r3d-container canvas {
  filter: drop-shadow(0 14px 28px rgba(0, 0, 0, 0.24));
}

[data-theme="light"] .r3d-container canvas {
  filter: drop-shadow(0 12px 24px rgba(15, 23, 42, 0.16));
}

/* Zonas clicables 100% invisibles (solo cursor de mano, sin bloques de color sobre la revista) */
.r3d-container .htmlLayer .RInner,
.r3d-container .htmlLayer .LInner {
  position: relative;
  width: 100%;
  height: 100%;
}

.r3d-hotspot,
.r3d-hotspot:hover,
.r3d-hotspot:focus,
.r3d-container .flipbook-page-html a,
.r3d-container .flipbook-page-html a:hover {
  position: absolute;
  display: block;
  cursor: pointer !important;
  background: transparent !important;
  box-shadow: none !important;
  border: none !important;
  outline: none !important;
  -webkit-tap-highlight-color: transparent;
}

.r3d-container .flipbook-menuBottom,
.r3d-container .flipbook-menuTop,
.r3d-container .flipbook-currentPageHolder,
.r3d-container .flipbook-left-arrow,
.r3d-container .flipbook-right-arrow,
.r3d-container .flipbook-first-arrow,
.r3d-container .flipbook-last-arrow {
  display: none !important;
}

/* Flechas laterales */
.nav-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-ui);
  z-index: 20;
  transition: transform 0.18s ease, background 0.18s ease, opacity 0.18s ease;
}

.nav-arrow:hover:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
  transform: translateY(-50%) scale(1.06);
}

.nav-arrow:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

.nav-arrow svg {
  width: 22px;
  height: 22px;
}

.nav-prev { left: 16px; }
.nav-next { right: 16px; }

/* Loader */
.loader {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-muted);
  font-size: 13px;
  z-index: 15;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.loader.hidden {
  opacity: 0;
}

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ==================== BOTTOMBAR ==================== */
.bottombar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: var(--bottombar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  z-index: 30;
  pointer-events: none;
}

.bottom-pill {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  height: 48px;
  padding: 0 14px;
  border-radius: var(--radius-pill);
  background: var(--surface);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-ui);
  max-width: 720px;
  width: 100%;
}

.page-control, .zoom-control {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.pill-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.16s ease, color 0.16s ease;
}

.pill-btn:hover {
  background: var(--surface-hover);
}

.pill-btn.active {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
}

.pill-btn svg {
  width: 17px;
  height: 17px;
}

.page-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
}

#page-input {
  width: 40px;
  height: 28px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface-hover);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  outline: none;
  -moz-appearance: textfield;
}

#page-input::-webkit-inner-spin-button,
#page-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
}

#page-input:focus {
  border-color: var(--accent);
}

.scrubber-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  min-width: 60px;
}

#page-scrubber {
  width: 100%;
  height: 5px;
  appearance: none;
  -webkit-appearance: none;
  background: var(--border);
  border-radius: 99px;
  outline: none;
  cursor: pointer;
}

#page-scrubber::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
  cursor: pointer;
  transition: transform 0.15s ease;
}

#page-scrubber::-webkit-slider-thumb:hover {
  transform: scale(1.18);
}

.divider {
  width: 1px;
  height: 22px;
  background: var(--border);
  flex-shrink: 0;
}

.zoom-badge {
  min-width: 46px;
  height: 28px;
  padding: 0 6px;
  border-radius: 7px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.16s ease;
}

.zoom-badge:hover {
  background: var(--surface-hover);
}

/* ==================== DRAWER ==================== */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 40;
}

.drawer-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 340px;
  max-width: 88vw;
  background: var(--surface-solid);
  border-right: 1px solid var(--border);
  box-shadow: var(--shadow-ui);
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
  z-index: 50;
  display: flex;
  flex-direction: column;
}

.drawer.open {
  transform: translateX(0);
}

.drawer-header {
  height: var(--topbar-h);
  padding: 0 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.drawer-tabs {
  display: flex;
  gap: 6px;
  background: var(--surface-hover);
  padding: 4px;
  border-radius: 10px;
}

.tab-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.16s ease, color 0.16s ease;
}

.tab-btn.active {
  background: var(--accent);
  color: #ffffff;
}

.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.thumbs-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.thumb-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 6px;
  border-radius: 10px;
  border: 2px solid transparent;
  background: var(--surface-hover);
  cursor: pointer;
  transition: border-color 0.16s ease, transform 0.16s ease;
}

.thumb-card:hover {
  transform: translateY(-2px);
  border-color: var(--border);
}

.thumb-card.active {
  border-color: var(--accent);
}

.thumb-card img {
  width: 100%;
  aspect-ratio: 0.8;
  object-fit: cover;
  border-radius: 6px;
  background: #ffffff;
}

.thumb-num {
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}

.thumb-card.active .thumb-num {
  color: var(--accent);
}

.toc-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toc-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--surface-hover);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.16s ease, color 0.16s ease;
}

.toc-item:hover {
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  color: var(--accent);
}

.toc-page {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

/* ==================== TOAST ==================== */
.toast {
  position: fixed;
  bottom: calc(var(--bottombar-h) + 12px);
  left: 50%;
  transform: translateX(-50%) translateY(16px);
  padding: 10px 18px;
  border-radius: var(--radius-pill);
  background: var(--surface-solid);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-ui);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
  z-index: 60;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 768px) {
  :root {
    --topbar-h: 56px;
    --bottombar-h: 64px;
  }

  .hide-mobile { display: none !important; }

  .topbar { padding: 0 10px; }
  .topbar-left { height: 100%; }
  .topbar-right { gap: 8px; flex-shrink: 0; }
  .brand-info { height: 100%; gap: 8px; }
  .brand-logo {
    height: calc(var(--topbar-h) - 14px);
    max-width: 175px;
    object-fit: contain;
  }

  /* Selector desplegable de opciones en móviles */
  .tools-toggle {
    display: inline-flex;
    border: 1px solid var(--border);
    background: var(--surface-hover);
  }

  .tools-menu.open .tools-toggle {
    border-color: var(--accent);
    color: var(--accent);
  }

  .tools-panel {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    min-width: 185px;
    padding: 6px;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    border-radius: 14px;
    background: var(--surface-solid);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-ui);
    opacity: 0;
    transform: translateY(-6px) scale(0.96);
    transform-origin: top right;
    pointer-events: none;
    transition: opacity 0.18s ease, transform 0.18s ease;
    z-index: 45;
  }

  .tools-menu.open .tools-panel {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
  }

  .tools-panel .tool-item {
    width: 100%;
    height: 40px;
    padding: 0 12px;
    border-radius: 9px;
    justify-content: flex-start;
    gap: 10px;
    font-size: 13px;
    font-weight: 500;
  }

  .tools-panel .tool-label {
    display: inline;
  }

  .catalog-title { font-size: 13px; }
  .catalog-subtitle { display: none; }
  .download-label, .download-size { display: none !important; }
  .download-btn {
    width: 38px;
    height: 38px;
    padding: 0;
    border-radius: 50%;
    justify-content: center;
    gap: 0;
  }
  .download-btn svg {
    width: 18px;
    height: 18px;
  }

  .viewport { padding: 10px 10px; }

  .nav-arrow {
    width: 38px;
    height: 38px;
    opacity: 0.85;
  }
  .nav-prev { left: 8px; }
  .nav-next { right: 8px; }

  .bottom-pill {
    gap: 6px;
    padding: 0 10px;
    height: 44px;
  }

  .zoom-control, .divider { display: none; }
}
