/* =====================================================================
   Asistente interno del panel · botón redondo y chat
   Mismo vidrio que el resto del panel. Azul = acción (lo que escribes tú);
   marca = identidad (el asistente).
   ===================================================================== */

.ia-fab {
  position: fixed; right: 24px; bottom: 24px; z-index: 80;
  width: 58px; height: 58px; border-radius: 50%; border: 0; padding: 0; cursor: pointer;
  display: grid; place-items: center; color: #fff;
  background:
    radial-gradient(120% 120% at 25% 15%, rgba(255, 255, 255, .38) 0%, rgba(255, 255, 255, 0) 45%),
    linear-gradient(145deg, var(--marca) 0%, color-mix(in oklab, var(--marca) 55%, var(--accion)) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .45), inset 0 0 0 1px rgba(255, 255, 255, .18),
              0 18px 34px -14px color-mix(in oklab, var(--marca) 70%, #000), 0 4px 12px -4px rgba(12, 52, 54, .3);
  transition: transform .18s ease, box-shadow .18s ease;
  -webkit-tap-highlight-color: transparent;
}
.ia-fab:hover { transform: translateY(-2px) scale(1.03); }
.ia-fab:active { transform: scale(.96); }
.ia-fab:focus-visible { outline: none; box-shadow: var(--focus), 0 18px 34px -14px rgba(12, 52, 54, .5); }
/* Un halo suave que respira, para que se vea que está ahí sin molestar. */
.ia-fab::before {
  content: ""; position: absolute; inset: -5px; border-radius: 50%; pointer-events: none;
  border: 2px solid color-mix(in oklab, var(--marca) 45%, transparent); opacity: 0;
  animation: ia-halo 3.6s ease-out 1.2s infinite;
}
.ia-fab.activo::before { animation: none; }
@keyframes ia-halo { 0% { transform: scale(.92); opacity: .75; } 70%, 100% { transform: scale(1.28); opacity: 0; } }

.ia-fab-icono { grid-area: 1 / 1; display: grid; place-items: center; transition: transform .22s ease, opacity .18s ease; }
.ia-fab-icono svg { width: 26px; height: 26px; }
.ia-fab-cerrar svg { width: 22px; height: 22px; }
.ia-fab-cerrar { opacity: 0; transform: rotate(-90deg) scale(.6); }
.ia-fab.activo .ia-fab-chispa { opacity: 0; transform: rotate(90deg) scale(.6); }
.ia-fab.activo .ia-fab-cerrar { opacity: 1; transform: none; }

/* En WhatsApp, abajo a la derecha está el cuadro de escribir y «Tomar el control»: el asistente sube. */
body:has(#view-conversaciones.active) .ia-fab { bottom: 150px; }

/* Con un cajón abierto (ficha, cita, edición) el botón se aparta: tapaba «Guardar». */
body:has(#cajon-host .cajon) .ia-fab { display: none; }

/* ---------------------------------------------------------------- la caja */
.ia-caja {
  position: fixed; right: 24px; bottom: 96px; z-index: 81;
  width: min(410px, calc(100vw - 48px)); height: min(640px, calc(100dvh - 130px));
  display: flex; flex-direction: column; overflow: hidden;
  border-radius: var(--r-xl);
  background: var(--vidrio-reflejo), var(--vidrio-fuerte);
  -webkit-backdrop-filter: var(--desenfoque); backdrop-filter: var(--desenfoque);
  border: 1px solid var(--vidrio-borde);
  box-shadow: var(--sombra-vidrio), var(--sombra-flota);
  transform-origin: calc(100% - 29px) calc(100% + 40px);
  opacity: 0; transform: translateY(12px) scale(.96);
  transition: opacity .2s ease, transform .22s cubic-bezier(.2, .8, .2, 1);
}
.ia-caja.abierto { opacity: 1; transform: none; }
.ia-caja[hidden] { display: none; }
body:has(#view-conversaciones.active) .ia-caja { bottom: 222px; height: min(560px, calc(100dvh - 250px)); }

.ia-cabecera {
  display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 12px;
  padding: 14px 12px 14px 16px; border-bottom: 1px solid var(--divider);
  background: linear-gradient(180deg, color-mix(in oklab, var(--marca) 9%, transparent), transparent);
}
.ia-titulo { display: grid; gap: 1px; min-width: 0; }
.ia-titulo strong { font-size: 15.5px; font-weight: 700; letter-spacing: -.01em; color: var(--text-dark); }
.ia-titulo span { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--text-muted); }
.ia-punto { width: 7px; height: 7px; border-radius: 50%; background: #22a06b; box-shadow: 0 0 0 3px rgba(34, 160, 107, .16); }
.ia-punto.apagado { background: var(--text-muted); box-shadow: none; }
.ia-icono {
  width: 36px; height: 36px; border-radius: 12px; border: 0; background: transparent; color: var(--text-muted);
  display: grid; place-items: center; cursor: pointer; transition: background .15s ease, color .15s ease;
}
.ia-icono:hover { background: var(--dash-bg); color: var(--text-dark); }
.ia-icono:focus-visible { outline: none; box-shadow: var(--focus); }
.ia-icono svg { width: 18px; height: 18px; }

.ia-aviso {
  margin: 10px 12px 0; padding: 9px 12px; border-radius: 12px; font-size: 13px; line-height: 1.4;
  background: var(--aviso-claro); color: var(--aviso);
}

/* ---------------------------------------------------------------- mensajes */
.ia-lista {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 16px 14px 8px; display: flex; flex-direction: column; gap: 12px;
  scroll-behavior: smooth;
}
.ia-fila { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; }
.ia-yo { justify-content: flex-end; }

.ia-avatar {
  flex: 0 0 auto; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  font-size: 11.5px; font-weight: 700; letter-spacing: .02em;
}
.ia-avatar svg { width: 16px; height: 16px; }
.ia-avatar-bot {
  color: #fff;
  background: linear-gradient(145deg, var(--marca) 0%, color-mix(in oklab, var(--marca) 55%, var(--accion)) 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .4), 0 6px 14px -8px color-mix(in oklab, var(--marca) 80%, #000);
}
.ia-avatar-grande { width: 40px; height: 40px; }
.ia-avatar-grande svg { width: 21px; height: 21px; }
.ia-avatar-yo { background: var(--accion-clara); color: var(--accion-oscura); }
html[data-theme="dark"] .ia-avatar-yo { color: #cfe0ff; }

.ia-burbuja {
  max-width: min(82%, 320px); padding: 10px 14px; border-radius: 18px; font-size: 14.5px; line-height: 1.5;
  overflow-wrap: anywhere;
}
.ia-bot .ia-burbuja {
  border-bottom-left-radius: 6px; color: var(--text-dark);
  background: color-mix(in oklab, var(--card-bg) 88%, var(--marca) 4%);
  box-shadow: inset 0 0 0 1px var(--divider), 0 6px 16px -12px rgba(12, 52, 54, .35);
}
.ia-yo .ia-burbuja {
  border-bottom-right-radius: 6px; color: #fff;
  background: linear-gradient(160deg, color-mix(in oklab, var(--accion) 88%, #fff), var(--accion));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .22), 0 8px 18px -12px rgba(37, 99, 235, .7);
}
.ia-burbuja p { margin: 0; }
.ia-burbuja p + p, .ia-burbuja ul + p, .ia-burbuja p + ul { margin-top: 7px; }
.ia-burbuja ul { margin: 0; padding-left: 18px; }
.ia-burbuja li + li { margin-top: 3px; }
.ia-burbuja strong { font-weight: 700; }
.ia-burbuja em { color: var(--text-muted); font-style: normal; font-size: 13px; }
.ia-burbuja.ia-error { background: var(--peligro-claro); color: var(--peligro); box-shadow: none; }
.ia-reintentar {
  margin-top: 8px; padding: 6px 12px; border-radius: 999px; border: 1px solid currentColor;
  background: transparent; color: inherit; font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}

.ia-escribiendo { display: inline-flex; gap: 5px; padding: 14px 16px; }
.ia-escribiendo i {
  width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); opacity: .35;
  animation: ia-punto 1.2s ease-in-out infinite;
}
.ia-escribiendo i:nth-child(2) { animation-delay: .15s; }
.ia-escribiendo i:nth-child(3) { animation-delay: .3s; }
@keyframes ia-punto { 0%, 60%, 100% { transform: translateY(0); opacity: .35; } 30% { transform: translateY(-4px); opacity: .9; } }

.ia-sugerencias { display: flex; flex-wrap: wrap; gap: 7px; padding-left: 38px; }
.ia-chip {
  padding: 7px 12px; border-radius: 999px; font: inherit; font-size: 13px; font-weight: 500; cursor: pointer;
  color: var(--marca-oscura); background: var(--marca-clara);
  border: 1px solid color-mix(in oklab, var(--marca) 22%, transparent);
  transition: background .15s ease, transform .15s ease;
  text-align: left;
}
.ia-chip:hover { background: color-mix(in oklab, var(--marca) 22%, var(--card-bg)); transform: translateY(-1px); }
.ia-chip:focus-visible { outline: none; box-shadow: var(--focus); }

/* ---------------------------------------------------------------- escribir */
.ia-pie {
  display: flex; align-items: flex-end; gap: 8px; margin: 8px 12px 4px; padding: 6px 6px 6px 16px;
  border-radius: 24px; background: var(--card-bg);
  box-shadow: inset 0 0 0 1px var(--divider), 0 6px 16px -12px rgba(12, 52, 54, .3);
  transition: box-shadow .15s ease;
}
.ia-pie:focus-within { box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--accion) 55%, transparent), var(--focus); }
.ia-entrada {
  flex: 1; min-width: 0; resize: none; border: 0; outline: 0; background: transparent; color: var(--text-dark);
  font: inherit; font-size: 14.5px; line-height: 1.45; padding: 8px 0; max-height: 120px; min-height: 0;
}
.ia-pie .ia-entrada:focus, .ia-pie .ia-entrada:focus-visible { outline: none; box-shadow: none; border: 0; border-radius: 0; }
.ia-entrada::placeholder { color: var(--text-muted); }
.ia-entrada:disabled { cursor: not-allowed; }
.ia-enviar {
  flex: 0 0 auto; width: 38px; height: 38px; border-radius: 50%; border: 0; cursor: pointer;
  display: grid; place-items: center; color: #fff; background: var(--accion);
  transition: background .15s ease, transform .15s ease, opacity .15s ease;
}
.ia-enviar:hover:not(:disabled) { background: var(--accion-oscura); transform: scale(1.05); }
.ia-enviar:disabled { opacity: .35; cursor: default; }
.ia-enviar svg { width: 18px; height: 18px; }
/* Panel 271 · Micrófono para dictar. */
.ia-micro {
  flex: 0 0 auto; width: 38px; height: 38px; border-radius: 50%; border: 0; cursor: pointer;
  display: grid; place-items: center; color: var(--accion); background: color-mix(in oklab, var(--accion) 14%, transparent);
  transition: background .15s ease, transform .15s ease;
}
.ia-micro svg { width: 19px; height: 19px; }
.ia-micro.grabando { color: #fff; background: #d33a3a; animation: ia-latido 1.1s ease-in-out infinite; }
@keyframes ia-latido { 50% { transform: scale(1.12); } }
.ia-nota { margin: 2px 16px 12px; font-size: 11.5px; line-height: 1.35; color: var(--text-muted); text-align: center; }

/* ---------------------------------------------------------------- móvil */
@media (max-width: 860px) {
  /* Encima de la barra de abajo, sin tapar las pestañas. */
  .ia-fab { right: 16px; bottom: calc(96px + env(safe-area-inset-bottom, 0px)); width: 54px; height: 54px; }
  body:has(#view-conversaciones.active) .ia-fab { bottom: calc(96px + env(safe-area-inset-bottom, 0px)); }
  /* Con una conversación abierta, abajo está el cuadro de escribir: se aparta. */
  /* Panel 436 · Silvia (26/10, iPhone): «las estrellitas se han ido a dormir». La conversación se quedaba marcada como
     abierta al irse a otra sección y el botón desaparecía en TODO el panel. Solo se aparta dentro de WhatsApp. */
  body:has(#view-conversaciones.active .msg.abierta) .ia-fab { display: none; }
  /* La caja ocupa la pantalla, como una hoja. */
  .ia-caja, body:has(#view-conversaciones.active) .ia-caja {
    inset: 0; width: auto; height: auto; bottom: 0; border-radius: 0; border: 0; z-index: 95;
    padding-top: env(safe-area-inset-top, 0px); padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--vidrio-reflejo), color-mix(in oklab, var(--fondo) 94%, #fff);
    transform-origin: 50% 100%; transform: translateY(24px);
  }
  html[data-theme="dark"] .ia-caja { background: var(--vidrio-reflejo), var(--fondo); }
  .ia-caja.abierto { transform: none; }
  html.ia-abierto .ia-fab { display: none; }
  html.ia-abierto, html.ia-abierto body { overflow: hidden; }
  .ia-burbuja { max-width: 84%; font-size: 15.5px; }
  .ia-entrada { font-size: 16px; }   /* 16 px: el iPhone no hace zoom al escribir */
  .ia-chip { font-size: 14px; padding: 9px 13px; }
  .ia-icono { width: 44px; height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  .ia-fab::before, .ia-escribiendo i { animation: none; }
  .ia-caja, .ia-fab, .ia-fab-icono { transition: none; }
}
