* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  background: #0f172a;
  color: #0f172a;
  overscroll-behavior: none;
  touch-action: manipulation;
}
button { font-family: inherit; cursor: pointer; }
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

.view { display: none; height: 100%; flex-direction: column; }
.view.active { display: flex; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 18px;
  background: linear-gradient(135deg, #2563eb, #4f46e5);
  color: #fff;
  flex-shrink: 0;
}
.topbar h1 { font-size: 20px; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.editor-topbar { position: relative; }
.doc-title {
  font-size: 16px; font-weight: 600; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; max-width: 40vw;
}
.save-status { font-size: 13px; opacity: .85; margin-right: 6px; }

.btn {
  border: none; border-radius: 10px; padding: 10px 16px;
  font-size: 15px; font-weight: 600; min-height: 44px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-primary { background: #fff; color: #2563eb; }
.btn-primary:active { background: #e2e8f0; }
.btn-ghost { background: rgba(255,255,255,.15); color: #fff; }
.btn-icon {
  background: #fff; color: #1e293b; border-radius: 50%;
  width: 44px; height: 44px; padding: 0; font-size: 20px;
}
.hidden { display: none !important; }

/* ---------- Biblioteca ---------- */
.library-grid {
  flex: 1; overflow-y: auto; padding: 20px;
  display: grid; gap: 18px;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  background: #f1f5f9;
}
.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 40px; color: #475569; background: #f1f5f9; gap: 6px;
}
.pdf-card {
  background: #fff; border-radius: 14px; overflow: hidden;
  box-shadow: 0 1px 4px rgba(0,0,0,.12);
  display: flex; flex-direction: column;
  position: relative;
}
.pdf-card-thumb {
  width: 100%; aspect-ratio: 3/4; object-fit: cover; background: #e2e8f0;
  display: block;
}
.pdf-card-info { padding: 10px 12px; }
.pdf-card-name {
  font-size: 14px; font-weight: 600; margin: 0 0 4px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.pdf-card-meta { font-size: 12px; color: #64748b; margin: 0; }
.pdf-card-delete {
  position: absolute; top: 8px; right: 8px; width: 34px; height: 34px;
  border-radius: 50%; background: rgba(15,23,42,.6); color: #fff; border: none; font-size: 16px;
}

/* ---------- Editor ---------- */
.editor-body { flex: 1; display: flex; flex-direction: column; overflow: hidden; background: #334155; min-height: 0; }
.editor-main { flex: 1 1 auto; width: 100%; display: flex; overflow: hidden; min-height: 0; min-width: 0; }

.toolbar {
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 12px 8px; background: #1e293b; flex-shrink: 0;
  overflow-y: auto; width: 68px;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }
.tool-btn {
  width: 48px; height: 48px; border-radius: 12px; border: none;
  background: #334155; color: #fff; font-size: 20px;
}
.tool-btn.active { background: #2563eb; box-shadow: 0 0 0 2px #93c5fd inset; }
.toolbar-sep { width: 36px; height: 1px; background: #475569; margin: 4px 0; }

.color-swatches { display: flex; flex-direction: column; gap: 8px; }
.swatch {
  width: 30px; height: 30px; border-radius: 50%; border: 2px solid transparent;
}
.swatch.active { border-color: #fff; }

.stroke-range { display: flex; flex-direction: column; align-items: center; gap: 4px; color: #cbd5e1; font-size: 11px; }
.stroke-range input[type="range"] { writing-mode: vertical-lr; direction: rtl; width: 20px; height: 60px; }

.pages-scroll {
  flex: 1 1 auto; min-width: 0; overflow: auto; padding: 40px 12px;
  display: flex; justify-content: safe center;
  /* "safe center": si el contenido con zoom desborda, prioriza que se
     pueda desplazar hasta verlo completo en vez de recortarlo — sin esto,
     los navegadores basados en flexbox bloquean el scroll a mitad de
     camino cuando el contenido centrado es más ancho que el contenedor. */
  scrollbar-width: none; /* Firefox: oculta la barra mantiniendo el scroll */
}
.pages-scroll::-webkit-scrollbar { display: none; } /* Chrome/Edge/Safari */
#pages-container { display: flex; flex-direction: column; gap: 48px; align-items: safe center; will-change: transform; }
.page-wrap {
  position: relative; background: #fff; box-shadow: 0 4px 18px rgba(0,0,0,.5);
  touch-action: none; /* las herramientas de dibujo capturan el toque directamente */
}
.page-label {
  position: absolute; top: -30px; left: 0; right: 0;
  text-align: center; color: #94a3b8; font-size: 13px; font-weight: 600;
  letter-spacing: .02em; user-select: none; pointer-events: none;
}
/* con la herramienta "mover" activa, deja que el dedo mueva la hoja
   libremente (no solo vertical) */
#pages-container.tool-pan .page-wrap { touch-action: pan-x pan-y; }
/* con "seleccionar texto", deja que el toque/arrastre seleccione texto nativamente */
#pages-container.tool-select .page-wrap { touch-action: auto; }
.page-wrap canvas { display: block; }

/* Capa de texto de pdf.js: invisible pero seleccionable, para el resaltador
   por selección de texto (herramienta 🅰️). */
.text-layer {
  position: absolute; top: 0; left: 0; overflow: hidden;
  line-height: 1; pointer-events: none;
}
.text-layer span, .text-layer br {
  color: transparent; position: absolute; white-space: pre; cursor: text;
  transform-origin: 0% 0%; line-height: 1; margin: 0; padding: 0;
}
.text-layer ::selection { background: rgba(37, 99, 235, .35); }
/* El canvas del PDF queda en flujo normal (no absolute): así el wrap toma
   su tamaño real automáticamente, sin depender de un cálculo aparte que
   pudiera desincronizarse y hacer que las páginas se superpusieran. */
.page-canvas { display: block; }
.annotation-canvas { position: absolute; top: 0; left: 0; }

/* Franja fija, NO flotante: ocupa su propio espacio en el layout con
   flex-shrink:0, así nunca queda montada sobre el PDF sin importar el
   nivel de zoom (a diferencia de un overlay position:absolute). */
.page-nav {
  flex-shrink: 0;
  background: #1e293b; color: #fff;
  padding: 10px 14px; display: flex; align-items: center; justify-content: center; gap: 10px;
  font-size: 14px; border-top: 1px solid #334155;
}
.page-nav .btn-icon { width: 36px; height: 36px; font-size: 18px; }

.loading-overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.85); z-index: 999;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; color: #fff;
}
.spinner {
  width: 44px; height: 44px; border-radius: 50%;
  border: 4px solid rgba(255,255,255,.25); border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 640px) {
  .toolbar { width: 58px; }
  .tool-btn { width: 42px; height: 42px; font-size: 18px; }
  .doc-title { max-width: 30vw; }
}

/* Pantalla dividida en tablet (Android split-screen) o móvil angosto:
   la barra lateral se convierte en una barra horizontal arriba, para no
   robarle ancho a la hoja del PDF. */
@media (max-width: 520px) {
  .editor-main { flex-direction: column; }
  .toolbar {
    width: 100%; height: 60px; flex-direction: row; align-items: center;
    overflow-x: auto; overflow-y: hidden; padding: 6px 10px; gap: 8px;
    flex-shrink: 0;
  }
  .tool-btn { width: 40px; height: 40px; font-size: 17px; flex-shrink: 0; }
  .toolbar-sep { width: 1px; height: 28px; margin: 0 2px; flex-shrink: 0; }
  .color-swatches { flex-direction: row; gap: 6px; flex-shrink: 0; }
  .swatch { width: 24px; height: 24px; }
  .stroke-range { flex-direction: row; gap: 6px; flex-shrink: 0; }
  .stroke-range input[type="range"] { writing-mode: horizontal-tb; direction: ltr; width: 70px; height: 20px; }
  .doc-title { max-width: 22vw; }
  .topbar { padding: 10px 12px; }
  .btn { padding: 8px 12px; font-size: 14px; min-height: 38px; }
}
