/* =====================================================================
   Widget de chat · Asistente de Aptia
   Aislado del resto del sitio: todas las clases usan el prefijo
   .aptia-chat-*. Reutiliza los tokens globales (--brand-*, --ink, --bg,
   --radius-*, --shadow-*, --font-*) definidos en styles.css, sin
   redefinirlos.
   ===================================================================== */

/* No mostrar el widget mientras el preloader está en pantalla (index.html).
   Misma técnica que ya usa el sitio para ocultar/revelar el header mientras
   carga el preloader: ver preloader.css. En páginas sin preloader, #preloader
   no existe y estas reglas simplemente no aplican. */
#preloader ~ .aptia-chat-root {
  display: none;
}

.preloader-skip #preloader ~ .aptia-chat-root {
  display: block;
}

.aptia-chat-bubble {
  position: fixed;
  right: clamp(1rem, 4vw, 1.5rem);
  bottom: clamp(1rem, 4vw, 1.5rem);
  z-index: 999999;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  border: none;
  border-radius: var(--radius-pill);
  background: var(--brand-gradient);
  color: var(--on-dark);
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: transform 0.2s ease;
  animation: aptia-chat-pulse 2.8s ease-in-out infinite;
}

.aptia-chat-bubble:hover {
  transform: scale(1.06);
}

.aptia-chat-bubble[aria-expanded="true"] {
  animation: none;
}

@keyframes aptia-chat-pulse {
  0%, 100% {
    box-shadow: var(--shadow-lg), 0 0 0 0 rgba(148, 111, 242, 0.35);
  }
  50% {
    box-shadow: var(--shadow-lg), 0 0 0 10px rgba(148, 111, 242, 0);
  }
}

@media (prefers-reduced-motion: reduce) {
  .aptia-chat-bubble {
    animation: none;
  }
}

/* Anillo de foco. Antes esta regla lo pintaba de BLANCO para los cinco controles,
   anulando por especificidad el :focus-visible global de styles.css. Sobre fondo
   claro eso daba 1.00:1 contra la página y 1.03:1 contra la fila del input: el
   foco del teclado era literalmente invisible.
   Ahora manda el anillo global (3px var(--brand-lila)), que cumple el mínimo de
   3:1 en los tres fondos donde aparece: página blanca 3.61:1, fila del input
   3.52:1 y footer oscuro 3.24:1.
   Abajo se corrige el único caso que ese anillo no cubre. */

/* .aptia-chat-close y .aptia-chat-contact-link viven dentro de .aptia-chat-header,
   cuyo fondo ES el degradado de marca. Ahí el lila del anillo queda 1:1 contra el
   propio degradado y desaparece; el blanco tampoco sirve (2.40:1). Un anillo
   oscuro da 4.91:1 en el peor punto del degradado. Solo se cambia el color: el
   grosor y el offset siguen viniendo de la regla global. */
.aptia-chat-close:focus-visible,
.aptia-chat-contact-link:focus-visible {
  outline-color: var(--on-brand);
}

.aptia-chat-bubble svg {
  width: 26px;
  height: 26px;
}

.aptia-chat-window {
  position: fixed;
  right: clamp(1rem, 4vw, 1.5rem);
  bottom: calc(clamp(1rem, 4vw, 1.5rem) + 60px + 0.75rem);
  z-index: 999999;
  width: min(380px, calc(100vw - 2rem));
  height: min(600px, calc(100vh - 8rem));
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  font-family: var(--font-body);
}

.aptia-chat-window[hidden] {
  display: none;
}

.aptia-chat-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4);
  background: var(--brand-gradient);
  color: var(--on-dark);
}

.aptia-chat-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--text-base);
}

.aptia-chat-contact-link {
  display: inline-block;
  margin-top: var(--space-1);
  font-size: var(--text-sm);
  color: var(--on-dark);
  opacity: 0.9;
  text-decoration: underline;
}

.aptia-chat-close {
  flex: none;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-pill);
  background: rgba(255, 255, 255, 0.16);
  color: var(--on-dark);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
}

.aptia-chat-close:hover {
  background: rgba(255, 255, 255, 0.28);
}

.aptia-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.aptia-chat-msg {
  max-width: 85%;
  padding: var(--space-3);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.aptia-chat-msg--bot {
  align-self: flex-start;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--border);
}

/* El texto va oscuro, no blanco: es lo que la persona acaba de escribir y tiene que
   poder releerlo. Sobre el degradado, el blanco da 2.40:1 y --on-brand da 4.91:1.
   Decisión deliberada de no aplicarlo al botón primario ni a .step__num, que se
   dejaron con blanco por criterio de diseño — ver A11Y-1 y §2 en Auditorias/. */
.aptia-chat-msg--user {
  align-self: flex-end;
  background: var(--brand-gradient);
  color: var(--on-brand);
}

.aptia-chat-note {
  align-self: center;
  max-width: 90%;
  text-align: center;
  font-size: var(--text-sm);
  line-height: 1.4;
  color: var(--ink-soft);
  font-style: italic;
}

.aptia-chat-typing {
  align-self: flex-start;
  display: flex;
  gap: 4px;
  padding: var(--space-3);
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border);
}

.aptia-chat-typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ink-soft);
  animation: aptia-chat-blink 1.2s infinite ease-in-out;
}

.aptia-chat-typing span:nth-child(2) {
  animation-delay: 0.2s;
}

.aptia-chat-typing span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes aptia-chat-blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

.aptia-chat-form {
  display: flex;
  align-items: flex-end;
  gap: var(--space-2);
  padding: var(--space-3);
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.aptia-chat-sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
}

.aptia-chat-input {
  flex: 1;
  resize: none;
  max-height: 100px;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  /* 16px fijos (no var(--text-sm)): con menos de 16px, iOS Safari hace
     zoom automático al enfocar el input y descuadra la ventana flotante. */
  font-size: 16px;
  color: var(--ink);
  background: var(--bg);
}

.aptia-chat-input:disabled {
  opacity: 0.6;
}

.aptia-chat-counter {
  flex: none;
  align-self: flex-end;
  padding-bottom: 0.6rem;
  font-size: var(--text-sm);
  line-height: 1;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.aptia-chat-counter--limit {
  color: #c0392b;
  font-weight: 600;
}

.aptia-chat-send {
  flex: none;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: var(--radius-pill);
  background: var(--brand-gradient);
  color: var(--on-dark);
  cursor: pointer;
}

.aptia-chat-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.aptia-chat-send svg {
  width: 18px;
  height: 18px;
}

@media (max-width: 640px) {
  .aptia-chat-window {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    border-radius: 0;
  }
}
