:root {
  --app-bg: #f4f6fa;
  --card-bg: rgba(255, 255, 255, 0.85);
  --text-main: #1e293b;
  --text-muted: #64748b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --secondary: #e2e8f0;

  --color1: #75AADB;
  --color2: #ffffff;
  --color3: #75AADB;
}

/* Print styles must remain strict */
@page {
  size: A4 landscape;
  margin: 0;
}

@media print {
  body {
    margin: 0;
    padding: 0;
  }

  .a4 {
    margin: 0;
    padding: 0.5cm;
    box-shadow: none !important;
    transform: scale(1) !important;
    /* Forza evitar escalar en impresión directa */
  }

  /* Ocultar UI en impresión nativa */
  .app-header,
  .controls-grid,
  .card:not(.print-card),
  .app-footer {
    display: none !important;
  }

  .a4-scaler {
    padding: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
    height: auto !important;
  }

  .header-with-action {
    display: none !important;
  }
}

/* Base */
body {
  font-family: 'Outfit', sans-serif;
  color: var(--text-main);
  margin: 0;
  padding: 0;
  min-height: 100vh;
}

body::before {
  content: "";
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: -1;
  background: var(--app-bg);
  background-image:
    radial-gradient(at 0% 0%, hsla(217, 100%, 76%, 0.15) 0, transparent 50%),
    radial-gradient(at 100% 0%, hsla(280, 100%, 76%, 0.15) 0, transparent 50%);
}

/* App Header */
.app-header {
  text-align: center;
  padding: 3rem 1rem 2rem;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  margin-bottom: 2rem;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.03);
  will-change: transform;
}

.app-header h1 {
  margin: 0 0 0.5rem 0;
  font-size: 2.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, #1e293b, var(--primary));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: -0.5px;
}

.app-header p {
  margin: 0;
  color: var(--text-muted);
  font-size: 1.1rem;
}

/* Layout Principal */
.app-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px 4rem;
  display: flex;
  flex-direction: column;
  gap: 2rem;
}

.controls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2rem;
  align-items: start;
}

@media (max-width: 768px) {
  .controls-grid {
    grid-template-columns: 1fr;
  }

  .card {
    padding: 24px;
  }

  .a4-scaler {
    display: none !important;
  }

  .mobile-warning-box {
    display: flex !important;
  }
}

/* Cards */
.card {
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 24px;
  padding: 30px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.6);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  will-change: transform;
}

.card:hover {
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.08);
  transform: translateY(-2px);
}

.card h2 {
  margin-top: 0;
  font-size: 1.5rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 8px;
}

.subtitle {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin-bottom: 1.5rem;
  margin-top: 0;
  line-height: 1.5;
}

/* Botones */
button,
.btn-primary,
.btn-secondary,
label.btn-primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-family: 'Outfit', sans-serif;
  font-weight: 600;
  border: none;
  cursor: pointer;
  padding: 12px 24px;
  border-radius: 14px;
  transition: all 0.2s ease;
  font-size: 1rem;
}

.btn-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
}

.btn-primary:active {
  transform: translateY(1px);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.4);
}

.btn-primary:hover {
  background: var(--primary-hover);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(59, 130, 246, 0.5);
}

.btn-secondary {
  background: #e0e7ff;
  color: var(--primary-hover);
}

.btn-secondary:hover {
  background: #c7d2fe;
  transform: translateY(-2px);
}

.btn-large {
  padding: 16px 32px;
  font-size: 1.1rem;
}

/* Encabezado con acción (Download PDF) */
.header-with-action {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 1.5rem;
  margin-bottom: 2rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.header-with-action .titles {
  margin: 0;
}

.header-with-action .titles h2 {
  margin-bottom: 0.3rem;
}

/* ========== GUIA IA EN TAPA ========== */
.ai-guide-details {
  background: rgba(59, 130, 246, 0.04);
  border: 1px solid rgba(59, 130, 246, 0.15);
  border-radius: 12px;
  margin-bottom: 1.5rem;
  transition: all 0.3s ease;
}

.ai-guide-details[open] {
  background: white;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.08);
  border-color: rgba(59, 130, 246, 0.3);
}

.ai-guide-details summary {
  padding: 1rem;
  font-weight: 600;
  color: var(--primary);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.ai-guide-details summary::after {
  content: "";
  display: inline-block;
  width: 20px;
  height: 20px;
  margin-left: auto;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%233b82f6' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  transition: transform 0.3s ease;
}

.ai-guide-details[open] summary::after {
  transform: rotate(180deg);
}

.ai-guide-details summary::-webkit-details-marker {
  display: none;
}

.guide-content {
  padding: 0 1.25rem 1.25rem;
  color: var(--text-muted);
  font-size: 0.95rem;
  line-height: 1.5;
}

.prompt-box {
  background: #f8fafc;
  border-left: 4px solid var(--primary);
  padding: 1rem;
  border-radius: 4px 8px 8px 4px;
  color: var(--text-main);
  margin: 1rem 0;
  user-select: all;
  /* Facilita copiar el texto completo */
  font-size: 0.9rem;
}

.prompt-box strong {
  color: var(--primary-hover);
}

/* AI Formula Styles */
.ai-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 1rem;
}

@media (max-width: 480px) {
  .ai-form-grid {
    grid-template-columns: 1fr;
  }
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-group label {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-main);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.field-group input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--secondary);
  font-family: inherit;
  font-size: 0.9rem;
  background: white;
  transition: all 0.2s;
  width: 100%;
  box-sizing: border-box;
}

.field-group input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.ai-guide-actions {
  display: flex;
  gap: 10px;
  margin-top: 1.5rem;
}

.ai-guide-actions .btn-primary,
.ai-guide-actions .btn-secondary {
  flex: 1;
  font-size: 0.9rem;
  padding: 10px 15px;
  text-decoration: none;
}

/* ========== TAPA CANVAS (Totalmente Responsivo) ========== */
.tapa-preview-card {
  width: 100%;
  max-width: 250px;
  margin: 1.5rem auto;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  border: 4px solid white;
  aspect-ratio: 717 / 957;
  background: white;
  position: relative;
}

/* Sobreescritura a FabricJS para que se ajuste 100% al div fluido de la tapa */
.tapa-preview-card .canvas-container {
  width: 100% !important;
  height: 100% !important;
  pointer-events: none;
  /* Permite hacer scroll al tocar el canvas en móviles */
}

.tapa-preview-card .canvas-container canvas {
  width: 100% !important;
  height: 100% !important;
  object-fit: contain;
}

/* Upload Input Hide */
input[type="file"] {
  display: none;
}

/* Controles de Color */
.color-controls {
  display: flex;
  gap: 20px;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  justify-content: center;
}

.color-picker-wrapper {
  position: relative;
  width: 65px;
  height: 65px;
  border-radius: 50%;
  overflow: hidden;
  border: 4px solid white;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.color-picker-wrapper:hover {
  transform: scale(1.15);
}

.color-picker-wrapper input[type="color"] {
  position: absolute;
  top: -20px;
  left: -20px;
  width: 100px;
  height: 100px;
  border: none;
  cursor: pointer;
  padding: 0;
}

/* Grilla Hojas */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  /* Permitir columnas reducidas en móviles */
  gap: 20px;
}

.frame {
  aspect-ratio: 3/4;
  position: relative;
  overflow: hidden;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border: 2px solid white;
  background: #fff;
  transition: transform 0.3s;
}

.frame:hover {
  transform: translateY(-5px);
}

.frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(135deg, var(--color1), var(--color2), var(--color3));
  opacity: 0.6;
  mix-blend-mode: multiply;
  pointer-events: none;
  transition: background 0.3s ease;
}

/* ========== A4 Impresión y Escala ========== */
.a4-scaler {
  width: 100%;
  background: #f1f5f9;
  border-radius: 16px;
  padding: 1.5rem 0;
  /* Configurado para escalar sin forzar desplazamiento */
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: hidden;
  box-shadow: inset 0 2px 15px rgba(0, 0, 0, 0.05);
  position: relative;
  transition: height 0.2s;
  /* Ayuda visual al redimensionar la ventana */
}

.a4 {
  box-sizing: border-box;
  width: 29.7cm;
  height: 21cm;
  padding: 0.5cm;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(2, 1fr);
  background-color: white;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  /* Sombra de documento */
  transform-origin: top center;
  /* Central para la animacion de responsive */
}

.page {
  position: relative;
  overflow: hidden;
}

.page img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

#tapaPreview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  /* Adaptable visual igual a las demás páginas */
  background: #fff;
}

.rotated {
  transform: rotate(180deg);
}

/* Utils */
@keyframes spin {
  100% {
    transform: rotate(360deg);
  }
}

.spin {
  animation: spin 1s linear infinite;
}

/* Clases de Limpieza HTML */
.text-center { text-align: center; }
.guide-text-top { margin-top: 0; }
.guide-text-optional { margin-top: 1rem; font-size: 0.85rem; }
.guide-text-action { margin-bottom: 0; font-weight: 600; color: var(--primary); }
.grid-steps { margin-top: 2rem; margin-bottom: 2rem; grid-template-columns: repeat(2, 1fr); gap: 12px; }
.design-tip-box { margin-top: 1rem; padding: 1.5rem; background: rgba(59, 130, 246, 0.05); border-radius: 12px; border: 1px solid rgba(59, 130, 246, 0.1); }
.design-tip-title { margin-top: 0; color: var(--primary); font-size: 1.1rem; display: flex; align-items: center; gap: 6px; }
.design-tip-desc { margin: 0; font-size: 0.95rem; line-height: 1.5; color: var(--text-muted); }
.subtitle-compact { margin-bottom: 0.5rem; }
.mobile-warning-box { display: none; align-items: center; gap: 10px; color: #b45309; font-size: 0.95rem; background: #fffbeb; padding: 12px 16px; border-radius: 8px; border: 1px solid #fde68a; margin-bottom: 1.5rem; line-height: 1.4; }
.mobile-warning-icon { display: block; flex-shrink: 0; }
.actions-row { display: flex; gap: 1rem; flex-wrap: wrap; }
.ai-guide-details-spaced { margin-bottom: 2rem; }
.guide-content-centered { display: flex; flex-direction: column; gap: 1rem; align-items: center; text-align: center; }
.tutorial-text { margin: 0; font-size: 1rem; color: var(--text-main); }
.tutorial-img { max-width: 100%; border-radius: 8px; box-shadow: 0 4px 15px rgba(0,0,0,0.1); border: 2px solid white; }
.share-modal-emoji { font-size: 3rem; margin-bottom: 1rem; }


/* ========== FOOTER ========== */
.app-footer {
  text-align: center;
  padding: 3rem 1rem 2rem;
  color: var(--text-muted);
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  margin-top: 2rem;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: center;
}

.footer-content p {
  margin: 0;
  font-size: 1rem;
}

.footer-content p strong {
  color: var(--text-main);
  font-weight: 600;
}

.footer-links {
  display: flex;
  gap: 1.5rem;
}

.footer-links a {
  color: var(--text-muted);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
  transition: color 0.2s;
}

.footer-links a:hover {
  color: var(--primary);
}

/* ========== SHARE MODAL ========== */
.share-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.share-modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.share-modal {
  background: white;
  border-radius: 16px;
  padding: 2rem;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  transform: translateY(20px) scale(0.95);
  transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  position: relative;
  text-align: center;
}

.share-modal-overlay.active .share-modal {
  transform: translateY(0) scale(1);
}

.share-modal-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.share-modal-close:hover {
  background: #f1f5f9;
  color: var(--text-main);
}

.share-modal-title {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  color: var(--text-main);
}

.share-modal-desc {
  color: var(--text-muted);
  margin-bottom: 1.5rem;
  line-height: 1.5;
}

.share-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.share-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  font-weight: 600;
  text-decoration: none;
  color: white;
  transition: transform 0.2s, opacity 0.2s;
}

.share-btn:hover {
  transform: translateY(-2px);
  opacity: 0.9;
}

.share-wa {
  background: #25D366;
}

.share-tw {
  background: #000000;
}

.share-fb {
  background: #1877F2;
}

/* Accessibility improvements */
*:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}