/* ================================================================
   styles.css — Sistema de Garantías
   Mobile-first. Vanilla. Sin frameworks.
   ================================================================ */

:root {
  --color-primary: #2563eb;
  --color-primary-dark: #1d4ed8;
  --color-primary-light: #eff6ff;
  --color-success: #16a34a;
  --color-success-light: #f0fdf4;
  --color-success-border: #bbf7d0;
  --color-success-text: #166534;
  --color-danger: #dc2626;
  --color-danger-light: #fef2f2;
  --color-danger-border: #fecaca;
  --color-danger-text: #991b1b;
  --color-warning: #d97706;
  --color-whatsapp: #25d366;
  --color-purple: #8b5cf6;

  --color-bg: #f5f7fb;
  --color-surface: #ffffff;
  --color-text: #111827;
  --color-text-muted: #6b7280;
  --color-text-subtle: #9ca3af;
  --color-border: #e5e7eb;
  --color-border-strong: #d1d5db;
  --color-border-dashed: #cbd5e1;

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 14px;

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.06), 0 2px 6px rgba(15, 23, 42, 0.04);

  --tap-min: 48px; /* tamaño mínimo cómodo en móvil */

  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

/* ================================================================
   Reset
   ================================================================ */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
               'Helvetica Neue', Arial, sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh; /* dvh > vh en móviles modernos: descuenta barra del navegador */
  overflow-x: hidden;
}

input, textarea, button, select {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--color-primary); }

/* ================================================================
   Layout
   ================================================================ */

.page {
  width: 100%;
  padding: calc(12px + var(--safe-top)) 12px calc(20px + var(--safe-bottom));
}

.card {
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  padding: 18px 16px;
  box-shadow: var(--shadow);
}

/* ================================================================
   Encabezado
   ================================================================ */

.header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}

.header .icon-badge {
  width: 40px;
  height: 40px;
  border-radius: var(--radius);
  background: var(--color-primary-light);
  color: var(--color-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
}

.header.success .icon-badge {
  background: var(--color-success-light);
  color: var(--color-success);
}

.header h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
  font-weight: 700;
  color: var(--color-text);
  letter-spacing: -0.01em;
}

.header .subtitle {
  margin: 4px 0 0;
  font-size: 13px;
  color: var(--color-text-muted);
  line-height: 1.4;
}

.intro {
  margin: 12px 0 18px;
  color: var(--color-text-muted);
  line-height: 1.5;
  font-size: 15px;
}

/* ================================================================
   Secciones y campos
   ================================================================ */

.section {
  margin-top: 18px;
}

.section + .section { margin-top: 18px; }

.section-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--color-text-subtle);
  margin: 0 0 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
}

.field label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 7px;
}

.field label i { color: var(--color-text-subtle); width: 16px; text-align: center; }

.field label .optional {
  font-weight: 400;
  font-size: 12px;
  color: var(--color-text-subtle);
  margin-left: 4px;
}

.hint {
  margin-top: 6px;
  font-size: 12px;
  color: var(--color-text-muted);
  line-height: 1.4;
}

input[type="text"],
input[type="email"],
input[type="tel"],
input[type="date"],
input[type="time"],
input[type="number"],
textarea {
  width: 100%;
  padding: 14px 14px;
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius);
  font-size: 16px; /* 16px evita zoom automático en iOS */
  background: var(--color-surface);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  min-height: var(--tap-min);
  -webkit-appearance: none;
  appearance: none;
}

textarea {
  min-height: 110px;
  resize: vertical;
  line-height: 1.5;
}

input:focus,
textarea:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
}

input::placeholder,
textarea::placeholder {
  color: var(--color-text-subtle);
}

input:disabled,
textarea:disabled {
  background: #f3f4f6;
  color: var(--color-text-muted);
}

/* ================================================================
   Ítems
   ================================================================ */

.items-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 10px;
  counter-reset: item-counter;
}

.item-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
  counter-increment: item-counter;
}

.item-row::before {
  content: counter(item-counter) ".";
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  font-weight: 700;
  font-size: 14px;
  color: var(--color-text-muted);
  flex-shrink: 0;
}

.item-row input {
  flex: 1;
  min-width: 0;
}

.btn-delete {
  width: var(--tap-min);
  height: auto;
  min-width: var(--tap-min);
  border: 1px solid var(--color-danger-border);
  background: var(--color-danger-light);
  color: var(--color-danger);
  border-radius: var(--radius);
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, transform 0.05s ease;
}

.btn-delete:active {
  background: #fecaca;
  transform: scale(0.96);
}

/* ================================================================
   Botones
   ================================================================ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: var(--tap-min);
  padding: 12px 16px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 16px;
  font-weight: 600;
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease,
              transform 0.05s ease, opacity 0.15s ease;
}

.btn:active { transform: scale(0.985); }
.btn:disabled { opacity: 0.6; pointer-events: none; }
.btn i { font-size: 14px; }

.btn-primary {
  background: var(--color-primary);
  color: #fff;
}
.btn-primary:active { background: var(--color-primary-dark); }

.btn-success {
  background: var(--color-success);
  color: #fff;
}
.btn-success:active { background: #15803d; }

.btn-secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
}
.btn-secondary:active { background: #f3f4f6; }

.btn-whatsapp { background: var(--color-whatsapp); color: #fff; }
.btn-whatsapp:active { background: #1ebe5d; }

.btn-share { background: var(--color-purple); color: #fff; }
.btn-share:active { background: #7c3aed; }

.btn-add {
  background: var(--color-primary-light);
  color: var(--color-primary);
  border: 1px dashed #93c5fd;
  font-weight: 600;
}
.btn-add:active { background: #dbeafe; }

.btn-ghost {
  background: transparent;
  color: var(--color-text-muted);
  border-color: var(--color-border);
}
.btn-ghost:active { background: #f3f4f6; }

.actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 18px;
}

.actions.row {
  flex-direction: row;
  flex-wrap: wrap;
}

.actions.row .btn { flex: 1 1 calc(50% - 5px); width: auto; }

/* ================================================================
   Resultados / mensajes
   ================================================================ */

.result {
  margin-top: 18px;
  padding: 14px 14px;
  background: var(--color-primary-light);
  border: 1px solid #bfdbfe;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
  overflow-wrap: anywhere;
  display: none;
}

.result.success {
  background: var(--color-success-light);
  border-color: var(--color-success-border);
}

.result.error {
  background: var(--color-danger-light);
  border-color: var(--color-danger-border);
  color: var(--color-danger-text);
}

.result-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  margin-bottom: 8px;
  font-size: 15px;
}

.result-title.success { color: var(--color-success-text); }
.result-title.error { color: var(--color-danger-text); }

.result-id {
  display: inline-block;
  padding: 2px 8px;
  background: rgba(0, 0, 0, 0.04);
  border-radius: 6px;
  font-family: 'SF Mono', Menlo, Consolas, monospace;
  font-size: 12px;
  margin-bottom: 8px;
}

.result-link {
  margin-top: 6px;
  padding: 10px 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  word-break: break-all;
  overflow-wrap: anywhere;
  line-height: 1.45;
}

.result-link a { color: var(--color-primary); text-decoration: none; }
.result-link a:hover { text-decoration: underline; }

.small-note {
  margin-top: 10px;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--color-success-text);
  background: var(--color-success-light);
  border: 1px solid var(--color-success-border);
  border-radius: var(--radius-sm);
  display: none;
}

.small-note i { margin-right: 6px; }

.msg {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.45;
  display: none;
  align-items: flex-start;
  gap: 8px;
}

.msg i { margin-top: 2px; flex-shrink: 0; }
.msg.success {
  display: flex;
  background: var(--color-success-light);
  color: var(--color-success-text);
  border: 1px solid var(--color-success-border);
}
.msg.error {
  display: flex;
  background: var(--color-danger-light);
  color: var(--color-danger-text);
  border: 1px solid var(--color-danger-border);
}

/* ================================================================
   Pantalla de firma — bloques de datos
   ================================================================ */

.box {
  background: #fafbfc;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}

.box-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--color-text-subtle);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.meta-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.meta-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-size: 15px;
  line-height: 1.4;
  word-break: break-word;
}

.meta-line .meta-label {
  color: var(--color-text-muted);
  font-weight: 500;
  font-size: 13px;
  flex-shrink: 0;
  min-width: 86px;
}

.meta-line .meta-value {
  color: var(--color-text);
  font-weight: 500;
}

.meta-line.full {
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
}

/* ================================================================
   Checklist de la firma
   ================================================================ */

.check-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  margin-bottom: 8px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: border-color 0.15s ease, background 0.15s ease;
  min-height: var(--tap-min);
}

.check-item:active { background: #fafbfc; }

.check-item.checked {
  border-color: var(--color-success);
  background: var(--color-success-light);
}

.check-item input[type="checkbox"] {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.custom-check {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border: 2px solid var(--color-border-strong);
  border-radius: 6px;
  margin-top: 1px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, border-color 0.15s ease;
  background: var(--color-surface);
}

.check-item.checked .custom-check,
.check-item input[type="checkbox"]:checked + .custom-check {
  background: var(--color-success);
  border-color: var(--color-success);
}

.check-item.checked .custom-check::after,
.check-item input[type="checkbox"]:checked + .custom-check::after {
  content: '';
  width: 6px;
  height: 11px;
  border-right: 2.5px solid #fff;
  border-bottom: 2.5px solid #fff;
  transform: rotate(45deg) translate(-1px, -1px);
}

.check-text {
  font-size: 15px;
  line-height: 1.4;
  flex: 1;
  color: var(--color-text);
}

.check-text .check-number {
  display: inline-block;
  min-width: 22px;
  font-weight: 700;
  color: var(--color-text-muted);
  margin-right: 2px;
}

.check-item.checked .check-text .check-number {
  color: var(--color-success);
}

/* ================================================================
   Canvas de firma
   ================================================================ */

.signature-wrap {
  position: relative;
  background: var(--color-surface);
  border: 2px dashed var(--color-border-dashed);
  border-radius: var(--radius);
  margin-top: 8px;
  overflow: hidden;
  touch-action: none;
}

.signature-wrap.has-signature {
  border-style: solid;
  border-color: var(--color-success);
  background: var(--color-surface);
}

#firmaCanvas {
  display: block;
  width: 100%;
  height: 220px;
  cursor: crosshair;
  touch-action: none;
  background: transparent;
}

.signature-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  color: var(--color-text-subtle);
  font-size: 14px;
  font-weight: 500;
  text-align: center;
  padding: 12px;
}

.signature-placeholder i {
  margin-right: 6px;
  font-size: 16px;
}

.signature-wrap.has-signature .signature-placeholder { display: none; }

.signature-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.signature-actions .btn { flex: 1; }

/* ================================================================
   Estado de carga
   ================================================================ */

.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 44px 20px;
  background: #fafbfc;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  color: var(--color-text-muted);
  font-size: 14px;
}

.loading-state .loading-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--color-text);
  margin-bottom: 6px;
  margin-top: 4px;
}

.loading-state .loading-subtitle {
  font-size: 13px;
  color: var(--color-text-muted);
  line-height: 1.45;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--color-border);
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 14px;
}

/* Spinner pequeño para usar dentro de botones o inline */
.spinner-sm {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-top-color: #ffffff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -3px;
  margin-right: 6px;
  flex-shrink: 0;
}

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

/* ================================================================
   Botón deshabilitado (estado "no listo")
   ================================================================ */

/* Aplicamos esta clase explícitamente en lugar de confiar solo en
   :disabled, para tener control total del look y compatibilidad. */
.btn.is-disabled,
.btn.is-disabled:active {
  background: #e5e7eb !important;
  color: #9ca3af !important;
  cursor: not-allowed;
  pointer-events: none;
  opacity: 1; /* anula el opacity del :disabled genérico */
  transform: none;
}

.btn.is-disabled i { opacity: 0.7; }

/* Texto de ayuda debajo del botón cuando no está listo */
.help-text {
  margin: 10px 0 0;
  text-align: center;
  font-size: 13px;
  line-height: 1.45;
  color: var(--color-text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.help-text.ready {
  color: var(--color-success-text);
}

.help-text i {
  font-size: 13px;
  flex-shrink: 0;
}

/* ================================================================
   Estado de procesamiento (envío en curso)
   ================================================================ */

.processing-state {
  margin-top: 16px;
  padding: 18px 16px;
  background: var(--color-primary-light);
  border: 1px solid #bfdbfe;
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  gap: 14px;
}

.processing-state .processing-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid #bfdbfe;
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

.processing-state .processing-text { flex: 1; }

.processing-state .processing-title {
  font-size: 14px;
  font-weight: 700;
  color: #1e3a8a;
  margin-bottom: 3px;
  line-height: 1.35;
}

.processing-state .processing-subtitle {
  font-size: 13px;
  color: #1e40af;
  line-height: 1.45;
  opacity: 0.85;
}

/* Bloqueo visual del formulario durante envío */
.form-locked input,
.form-locked textarea,
.form-locked .check-item,
.form-locked .signature-wrap,
.form-locked .btn-ghost {
  pointer-events: none;
  opacity: 0.6;
  user-select: none;
}

/* ================================================================
   Tarjeta de éxito post-firma
   ================================================================ */

.success-card {
  margin-top: 18px;
  padding: 22px 18px;
  background: var(--color-success-light);
  border: 1px solid var(--color-success-border);
  border-radius: var(--radius);
  text-align: center;
}

.success-card .success-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--color-success);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  margin-bottom: 12px;
}

.success-card h3 {
  margin: 0 0 6px;
  font-size: 17px;
  font-weight: 700;
  color: var(--color-success-text);
}

.success-card .success-text {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  color: var(--color-success-text);
  opacity: 0.85;
}

.success-card .success-actions {
  margin-top: 16px;
}

/* ================================================================
   Footer
   ================================================================ */

.footer-note {
  margin-top: 22px;
  text-align: center;
  font-size: 12px;
  color: var(--color-text-subtle);
  line-height: 1.4;
}

.footer-note a {
  color: var(--color-primary);
  text-decoration: none;
  font-weight: 600;
}

/* ================================================================
   Estado "ya firmado"
   ================================================================ */

.signed-state {
  text-align: center;
  padding: 20px 16px 12px;
}

.signed-state .signed-icon {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--color-success-light);
  color: var(--color-success);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  margin-bottom: 14px;
}

.signed-state h2 {
  margin: 0 0 6px;
  font-size: 18px;
  font-weight: 700;
  color: var(--color-text);
}

.signed-state p {
  margin: 0 0 16px;
  color: var(--color-text-muted);
  font-size: 14px;
  line-height: 1.5;
}

/* ================================================================
   Tablet / desktop
   ================================================================ */

@media (min-width: 640px) {
  .page {
    padding: 32px 20px calc(32px + var(--safe-bottom));
  }

  .card {
    max-width: 640px;
    padding: 32px 32px;
  }

  .header h1 { font-size: 22px; }

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

  .meta-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px 18px;
  }

  .meta-line.full { grid-column: 1 / -1; }
}

@media (min-width: 768px) {
  .actions.row .btn {
    flex: 0 0 auto;
    width: auto;
    min-width: 160px;
    padding: 12px 20px;
  }
}

/* ================================================================
   Modo standalone (PWA instalada)
   ================================================================ */

@media (display-mode: standalone) {
  body { background: var(--color-bg); }
  .footer-note { padding-bottom: 8px; }
}

/* ================================================================
   Reduce motion
   ================================================================ */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
