:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --danger: #ef4444;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
}

html, body {
  width: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  position: fixed;
}

body {
  background-color: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

header {
  background-color: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
  width: 100%;
}

header h1 {
  font-size: 1.1rem;
  color: var(--primary);
  white-space: nowrap;
}

main {
  flex: 1;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.upload-zone {
  border: 2px dashed var(--primary);
  border-radius: 12px;
  padding: 1.5rem 1rem;
  text-align: center;
  background-color: rgba(37, 99, 235, 0.02);
  cursor: pointer;
  transition: background-color 0.2s;
  width: 100%;
}

.upload-zone:hover {
  background-color: rgba(37, 99, 235, 0.06);
}

.upload-zone input[type="file"] {
  display: none;
}

.upload-icon {
  font-size: 2rem;
}

.upload-zone h3 {
  font-size: 1rem;
  margin-top: 0.5rem;
  word-wrap: break-word;
}

.upload-text {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-top: 0.25rem;
}

.btn-upload {
  display: inline-block;
  margin-top: 0.75rem;
  background-color: var(--primary);
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-weight: 500;
  font-size: 0.85rem;
  cursor: pointer;
}

.section-title {
  font-size: 1rem;
  margin-bottom: 0.75rem;
}

.pdf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.75rem;
  width: 100%;
}

.pdf-card {
  position: relative;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.85rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.1s;
  min-width: 0;
}

.pdf-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

.pdf-icon {
  font-size: 1.5rem;
  flex-shrink: 0;
}

.pdf-info {
  overflow: hidden;
  flex: 1;
  min-width: 0;
  padding-right: 1.5rem;
}

.pdf-title {
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.pdf-date {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.menu-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

.menu-dropdown {
  display: none;
  position: absolute;
  top: 2rem;
  right: 0.5rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  z-index: 10;
  overflow: hidden;
}

.menu-dropdown.active {
  display: block;
}

.menu-dropdown button {
  display: block;
  width: 100%;
  padding: 0.5rem 1rem;
  background: none;
  border: none;
  text-align: left;
  font-size: 0.85rem;
  color: var(--danger);
  cursor: pointer;
}

.empty-msg {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.reader-header {
  background-color: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-shrink: 0;
  width: 100%;
  min-width: 0;
}

.btn-back {
  text-decoration: none;
  color: var(--primary);
  font-size: 1.2rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  padding: 0.2rem 0.4rem;
}

.btn-back-text {
  display: inline;
  font-size: 0.85rem;
  margin-left: 0.2rem;
}

.reader-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
  text-align: right;
}

.reader-container {
  flex: 1;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pdf-embed {
  width: 100%;
  height: 100%;
  flex: 1;
  border: none;
  display: block;
}

footer {
  background-color: var(--card-bg);
  border-top: 1px solid var(--border);
  padding: 0.75rem 1rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
  width: 100%;
}

@media (max-width: 600px) {
  .btn-back-text {
    display: none;
  }

  .pdf-grid {
    grid-template-columns: 1fr;
  }

  .reader-title {
    font-size: 0.8rem;
  }
}

.reader-body {
  height: 100dvh;
  width: 100vw;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  touch-action: none;
}

.reader-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 50px;
  background-color: var(--card-bg);
  border-bottom: 1px solid var(--border);
  padding: 0 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  z-index: 100;
  touch-action: none;
}

.reader-main {
  position: fixed;
  top: 50px;
  bottom: 40px;
  left: 0;
  right: 0;
  width: 100% !important;
  max-width: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
  background-color: #525659;
  overflow: hidden;
  z-index: 1;
}

.pdf-embed {
  width: 100%;
  height: 100%;
  border: none;
  outline: none;
  display: block;
}

.reader-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40px;
  background-color: var(--card-bg);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  color: var(--text-muted);
  z-index: 100;
  touch-action: none;
}

/* ==========================================================================
   ESTILOS COMPLETOS DE LA BARRA LATERAL (TOOLBAR) Y LAYOUT DEL EDITOR
   ========================================================================== */

/* Layout general del editor */
.editor-layout {
  position: fixed;
  top: 50px;
  bottom: 40px;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: row;
  overflow: hidden;
  background-color: #484b4e; /* Fondo oscuro que resalta las páginas del PDF */
}

/* Menú colapsado (replegado al pulsar el botón de 3 barras) */
.toolbar.collapsed {
  transform: translateX(-100%);
  width: 0;
  padding: 0;
  overflow: hidden;
  border-right: none;
}

/* Botones de Herramientas */
.tool-btn {
  position: relative;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  width: 44px;
  height: 44px;
  font-size: 1.2rem;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #475569;
}

.tool-btn:hover {
  background-color: #f1f5f9;
  color: #0f172a;
  transform: translateY(-1px);
}

.tool-btn:active {
  transform: scale(0.94);
}

/* Estado Activo de Herramienta Seleccionada */
.tool-btn.active {
  background-color: var(--primary);
  color: #ffffff;
  border-color: var(--primary);
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.35);
}

/* Separadores de sección */
.tool-separator {
  width: 32px;
  height: 1px;
  background-color: #e2e8f0;
  margin: 0.2rem 0;
}

/* Selector de Color Personalizado */
.color-picker-wrapper {
  position: relative;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #ffffff;
  box-shadow: 0 0 0 2px #cbd5e1, 0 2px 6px rgba(0, 0, 0, 0.1);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  overflow: hidden;
}

.color-picker-wrapper:hover {
  transform: scale(1.08);
  box-shadow: 0 0 0 2px var(--primary), 0 4px 10px rgba(0, 0, 0, 0.15);
}

.color-picker-wrapper input[type="color"] {
  position: absolute;
  opacity: 0;
  width: 200%;
  height: 200%;
  cursor: pointer;
}

.color-preview-circle {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: var(--danger);
  transition: background-color 0.15s ease;
}

/* Controles de Zoom integrados (Tipo Pill / Píldora) */
.zoom-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  background: #f8fafc;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  padding: 0.35rem 0.25rem;
  width: 44px;
}

.tool-btn-sm {
  background: #ffffff;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  width: 28px;
  height: 28px;
  font-size: 0.85rem;
  font-weight: 700;
  color: #334155;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s ease;
}

.tool-btn-sm:hover {
  background-color: var(--primary);
  color: #ffffff;
  border-color: var(--primary);
}

#zoomVal {
  font-size: 0.65rem;
  font-weight: 700;
  color: #64748b;
  letter-spacing: -0.02em;
  padding: 0.1rem 0;
}

/* Tooltips emergentes elegantes al pasar el ratón */
.toolbar [title] {
  position: relative;
}

.toolbar [title]::after {
  content: attr(title);
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%) translateX(10px);
  background: #0f172a;
  color: #ffffff;
  padding: 0.35rem 0.65rem;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  z-index: 100;
}

.toolbar [title]:hover::after {
  opacity: 1;
  transform: translateY(-50%) translateX(14px);
}

.tool-btn, .tool-btn-sm {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.5rem;
  font-size: 1.1rem;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
}

.tool-btn.active {
  background-color: var(--primary);
  color: white;
  border-color: var(--primary);
}

.color-picker-wrapper {
  cursor: pointer;
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.color-picker-wrapper input[type="color"] {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.tool-separator {
  width: 80%;
  height: 1px;
  background-color: var(--border);
  margin: 0.2rem 0;
}

.zoom-controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.75rem;
  font-weight: 600;
}

.btn-export {
  background-color: #10b981;
  color: white;
  border: none;
  font-weight: 600;
  width: auto;
  padding: 0.5rem 0.75rem;
  height: auto;
}

/* Área de Lectura Multipágina */
.reader-main {
  flex: 1;
  width: 100%;
  height: 100%;
  background-color: #525659;
  overflow: auto;
  padding: 1.5rem 1rem;
  -webkit-overflow-scrolling: touch;
}

.pages-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  margin: 0 auto;
}

.page-wrapper {
  position: relative;
  box-shadow: 0 4px 15px rgba(0,0,0,0.3);
  background-color: #fff;
}

.pdf-page-canvas {
  display: block;
}

.draw-page-canvas {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 5;
}

/* Cajas de texto interactivas (Móviles/Editables) */
.draggable-text {
  position: absolute;
  z-index: 10;
  cursor: move;
  border: 1px dashed var(--primary);
  background: rgba(255, 255, 255, 0.8);
  border-radius: 4px;
  padding: 2px 6px;
  outline: none;
  user-select: none;
  min-width: 30px;
  font-family: sans-serif;
  font-size: 16px;
  word-break: break-word;
  touch-action: none;
  -webkit-user-select: text;
  user-select: text;
}

.draggable-text:focus {
  border-style: solid;
  background: #ffffff;
}

@media (min-width: 768px) {
  .toolbar.collapsed {
    transform: translateY(0);
    margin-left: -60px;
  }
}

/* Botones toolbar más táctiles y con feedback */
.tool-btn, .tool-btn-sm {
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

.tool-btn:active, .tool-btn-sm:active {
  transform: scale(0.93);
}

.reader-header, .reader-footer {
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
}

.reader-body {
  padding-top: env(safe-area-inset-top);
}

/* Barra flotante de edición de texto */
.text-edit-bar {
  position: fixed;
  bottom: 50px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.15);
  padding: 0.5rem 0.6rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  z-index: 200;
  max-width: 94vw;
  overflow-x: auto;
}

.text-edit-bar.hidden {
  display: none;
}

.text-edit-bar select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.4rem;
  font-size: 0.8rem;
  background: var(--bg);
  color: var(--text);
}

.btn-danger-sm {
  color: var(--danger) !important;
  border-color: var(--danger) !important;
}

/* Texto arrastrable: asa de redimensionar */
.draggable-text {
  min-width: 24px;
  min-height: 1.2em;
  line-height: 1.2;
}

.draggable-text.selected {
  border-style: solid;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37,99,235,0.15);
}

.resize-handle {
  position: absolute;
  width: 14px;
  height: 14px;
  background: var(--primary);
  border: 2px solid #fff;
  border-radius: 50%;
  right: -8px;
  bottom: -8px;
  cursor: se-resize;
  z-index: 15;
  touch-action: none;
}

/* ============ MODAL DE FIRMAS ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
  padding: 1rem;
}

.modal-overlay.hidden {
  display: none;
}

.modal-box {
  background: var(--card-bg);
  border-radius: 14px;
  width: 100%;
  max-width: 480px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h3 {
  font-size: 1rem;
}

.btn-close {
  background: none;
  border: none;
  font-size: 1.1rem;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.3rem 0.5rem;
}

.modal-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.modal-tab {
  flex: 1;
  padding: 0.7rem;
  background: none;
  border: none;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
}

.modal-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.modal-panel {
  padding: 1rem;
  overflow-y: auto;
}

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

.signatures-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
}

.signature-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.5rem;
  text-align: center;
  cursor: pointer;
  background: #fff;
  position: relative;
}

.signature-card img {
  width: 100%;
  height: 60px;
  object-fit: contain;
}

.signature-card span {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 0.3rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.signature-card .delete-sig {
  position: absolute;
  top: 2px;
  right: 2px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 0.7rem;
  color: var(--danger);
  cursor: pointer;
  line-height: 1;
}

.signature-pad {
  width: 100%;
  height: 180px;
  background: #fff;
  border: 1px dashed var(--border);
  border-radius: 8px;
  touch-action: none;
  display: block;
}

.signature-pad-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.signature-pad-actions input[type="text"] {
  flex: 1;
  min-width: 120px;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.85rem;
}

/* Firma insertada en la página (imagen arrastrable/redimensionable) */
.draggable-signature {
  position: absolute;
  z-index: 10;
  cursor: move;
  border: 1px dashed transparent;
}

.draggable-signature.selected {
  border-color: var(--primary);
}

.draggable-signature img {
  width: 100%;
  height: 100%;
  pointer-events: none;
  user-select: none;
}

/* --- ESTILO CORREGIDO DEL BOTÓN HAMBURGUESA --- */
.btn-menu {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text);
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.btn-menu:hover {
  background: var(--border);
  color: var(--primary);
}

/* --- ESTILO CORREGIDO PARA EVITAR QUE EXPORTAR SE SALGA DE PANTALLA --- */
.toolbar {
  width: 60px;
  background: var(--card-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem 0.25rem;
  gap: 0.4rem;
  transition: transform 0.2s ease, width 0.2s ease, padding 0.2s ease;
  z-index: 10;
  flex-shrink: 0;
  overflow-y: auto; /* Permite scroll vertical en la barra si no cabe todo */
  max-height: 100%;
}

/* --- BOTÓN EXPORTAR VERDE, COMPACTO Y CON EMOJI CENTRADO --- */
.btn-export {
  background: #15803d !important; /* Verde oscuro elegante */
  color: #ffffff !important;
  border: none !important;
  border-radius: 8px !important;
  
  /* Tamaños compactos alineados con los demás botones */
  width: 38px !important;
  height: 38px !important;
  min-height: 38px !important;
  font-size: 1.1rem !important;
  padding: 0 !important;
  
  /* Centrado perfecto del emoji */
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  
  /* Posicionamiento arriba con el resto de herramientas */
  margin-top: 0 !important;
  margin-bottom: 0 !important;
  flex-shrink: 0 !important;
  
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(21, 128, 61, 0.25);
  transition: all 0.2s ease;
}

.btn-export:hover {
  background: #166534 !important; /* Verde aún más oscuro al pasar el ratón */
  transform: translateY(-1px);
  box-shadow: 0 4px 6px rgba(21, 128, 61, 0.35);
}

.btn-export:active {
  transform: translateY(0);
}

@media (max-width: 400px) {
  .text-edit-bar {
    gap: 0.3rem;
    padding: 0.4rem;
  }
  .text-edit-bar select {
    font-size: 0.72rem;
    padding: 0.25rem;
  }
}

.toolbar {
  width: 60px;
  background: var(--card-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.6rem 0.25rem;
  gap: 0.5rem;
  transition: transform 0.2s ease, width 0.2s ease, padding 0.2s ease;
  z-index: 10;
  flex-shrink: 0;
  /* Garantiza que si la pantalla es bajita, puedas hacer scroll sin cortar nada */
  overflow-y: auto;
  overflow-x: hidden;
  height: 100%;
}

/* Previene el scroll al tocar en el contenedor del PDF cuando esté en modo dibujo/texto */
.reader-main {
  touch-action: pan-x pan-y;
}