/*──────────────────────────────────────────────────────────────
  Ai-Bot Human · Lab
  Diseño inspirado en Apple: minimal, oscuro, con luz radial.
──────────────────────────────────────────────────────────────*/

:root {
  --bg-deep: #070b13;
  --bg-near: #0d1422;
  --fg: #edf1f8;
  --fg-soft: #c7cedb;
  --fg-muted: #7a8494;
  --fg-faint: #4a5262;
  --accent: #ff4d4d;
  --accent-soft: rgba(255, 77, 77, 0.55);
  --accent-ghost: rgba(255, 77, 77, 0.12);
  --halo-cool: rgba(120, 150, 220, 0.13);
  --halo-warm: rgba(210, 130, 170, 0.08);
  --glass: rgba(255, 255, 255, 0.045);
  --glass-strong: rgba(255, 255, 255, 0.07);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --mic-size: 44px;
  --radius-lg: 24px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100vh;
  height: 100dvh;
  width: 100vw;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  letter-spacing: 0.005em;
}

body {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  position: relative;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  background: none;
  border: none;
  /* iOS: sin flash gris al tocar, sin selección/lupa en long-press y sin
     el retardo de 350 ms del doble-tap-zoom. */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}
input { font: inherit; color: inherit; }
svg { display: block; }

/*──────────────── FONDO ────────────────*/
/* Capa base: gradiente radial oscuro. */
body {
  background:
    radial-gradient(ellipse 90% 70% at 50% 45%, #121a2e 0%, #0a101c 55%, var(--bg-deep) 100%);
}

/* Dos blobs de luz que flotan lentamente. Sutileza total. */
#bgLayer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
}
#bgLayer::before,
#bgLayer::after {
  content: "";
  position: absolute;
  inset: -20%;
  pointer-events: none;
  filter: blur(60px);
}
#bgLayer::before {
  background: radial-gradient(ellipse 40% 30% at 30% 35%, var(--halo-cool), transparent 65%);
  animation: drift1 26s ease-in-out infinite;
}
#bgLayer::after {
  background: radial-gradient(ellipse 35% 30% at 72% 68%, var(--halo-warm), transparent 65%);
  animation: drift2 32s ease-in-out infinite;
}
@keyframes drift1 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(6vw, -4vh) scale(1.08); }
}
@keyframes drift2 {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(-5vw, 5vh) scale(0.94); }
}

/*──────────────── TOP BAR ────────────────*/
#topBar {
  position: relative;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px 22px;
  flex: 0 0 auto;
}

/*──────────────── BRAND HEADER (assistant + brand) ────────────────
 * Se muestra cuando JS detecta nombre de asistente o de marca por
 * query string (?assistant=…&brand=…) o por defaults del tema.
 * Cada tema lo restila vía CSS sobre #brandHeader / #brandAssistant /
 * #brandTagline. Aquí solo el layout neutro.
 */
#brandHeader {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 8px 18px;
  background: var(--glass);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--border);
  border-radius: 999px;
  max-width: min(92vw, 560px);
}
#brandHeader[hidden] { display: none; }
#brandLogo {
  height: 22px;
  width: auto;
  flex: 0 0 auto;
  object-fit: contain;
}
#brandText {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  line-height: 1.15;
  min-width: 0;
}
#brandAssistant {
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}
#brandTagline {
  font-size: 11.5px;
  font-weight: 400;
  letter-spacing: 0.04em;
  color: var(--fg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60vw;
}
#brandTagline:empty { display: none; }

#brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px 6px 10px;
  background: var(--glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12.5px;
  color: var(--fg-soft);
  letter-spacing: 0.02em;
}
.brand-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px rgba(255, 77, 77, 0.6);
  animation: dotPulse 2.4s ease-in-out infinite;
}
@keyframes dotPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.55; }
}

#settingsBtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  color: var(--fg-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, color 0.15s, transform 0.1s;
}
#settingsBtn svg { width: 17px; height: 17px; }
#settingsBtn:hover {
  background: var(--glass-strong);
  color: var(--fg);
}
#settingsBtn:active { transform: scale(0.95); }

/*──────────────── STAGE (avatar + status) ────────────────*/
#stage {
  position: relative;
  z-index: 3;
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0 16px;
  gap: 14px;
}

/*──────────────── AVATAR 9:16 + halo ────────────────*/
/*
  IMPORTANTE: el glow del avatar NO va en box-shadow del #avatarBox porque
  animar box-shadow con blur grande provoca repintados constantes que
  frenan la reproducción del <video>. En su lugar, tenemos una capa
  #avatarGlow hermana (detrás) y animamos sólo opacity + transform
  (GPU-acelerado, no afecta al vídeo).
*/
#avatarWrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 9 / 16;
  max-height: min(68vh, calc(100dvh - 280px));
  min-height: 0;
  height: 100%;
}

#avatarGlow {
  position: absolute;
  inset: -12%;
  z-index: 0;
  pointer-events: none;
  border-radius: 50%;
  background: radial-gradient(
    ellipse at center,
    rgba(150, 175, 235, 0.25) 0%,
    rgba(150, 175, 235, 0.1) 35%,
    transparent 65%
  );
  filter: blur(32px);
  opacity: 0.45;
  will-change: opacity, transform;
  animation: glowBreathe 6s ease-in-out infinite;
  transition: background 0.5s ease;
}
@keyframes glowBreathe {
  0%, 100% { opacity: 0.4; transform: scale(1); }
  50%      { opacity: 0.7; transform: scale(1.05); }
}

/* Estados del glow — SÓLO cambia el color de fondo + velocidad de respiro. */
body[data-state="listening"] #avatarGlow {
  background: radial-gradient(
    ellipse at center,
    rgba(255, 77, 77, 0.35) 0%,
    rgba(255, 77, 77, 0.12) 35%,
    transparent 65%
  );
  animation: glowBreatheFast 1.8s ease-in-out infinite;
}
body[data-state="speaking"] #avatarGlow {
  background: radial-gradient(
    ellipse at center,
    rgba(100, 160, 255, 0.32) 0%,
    rgba(100, 160, 255, 0.1) 35%,
    transparent 65%
  );
  animation: glowBreatheMed 3.2s ease-in-out infinite;
}
body[data-state="thinking"] #avatarGlow {
  background: radial-gradient(
    ellipse at center,
    rgba(235, 200, 140, 0.24) 0%,
    rgba(235, 200, 140, 0.08) 35%,
    transparent 65%
  );
  animation: glowBreatheMed 2.2s ease-in-out infinite;
}
@keyframes glowBreatheFast {
  0%, 100% { opacity: 0.55; transform: scale(1); }
  50%      { opacity: 0.9;  transform: scale(1.08); }
}
@keyframes glowBreatheMed {
  0%, 100% { opacity: 0.5; transform: scale(1); }
  50%      { opacity: 0.85; transform: scale(1.06); }
}

#avatarBox {
  position: relative;
  z-index: 1;
  aspect-ratio: 9 / 16;
  height: 100%;
  background: #000;
  border-radius: var(--radius-lg);
  overflow: hidden;
  /* box-shadow ESTÁTICO (no se anima). Barato. */
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.07),
    0 30px 90px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(255, 255, 255, 0.04);
}

#idleVideo, #clipVideo {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 12%;
}
#idleVideo {
  z-index: 1;
  /* Oculto hasta que reproduce DE VERDAD (JS añade .is-playing en el evento
     "playing"). Mientras no reproduzca —p.ej. autoplay bloqueado por el Modo
     de Bajo Consumo de iOS— se queda transparente, y con él desaparece el
     botón "play" nativo que iOS pinta sobre los <video> parados (era el
     segundo icono de play). El clipVideo y el welcomeVideo ya nacen ocultos
     (visibility/hidden), así que tampoco muestran botón nativo. */
  opacity: 0;
  transition: opacity 0.35s ease;
}
#idleVideo.is-playing { opacity: 1; }
#clipVideo { z-index: 3; visibility: hidden; }

/* Refuerzo por si alguna versión de iOS pintara el botón nativo pese al
   opacity:0. Sólo el botón concreto de arranque/overlay — NO el contenedor
   ::-webkit-media-controls completo, que en algunas versiones interfiere con
   el render del propio vídeo. */
#idleVideo::-webkit-media-controls-start-playback-button,
#clipVideo::-webkit-media-controls-start-playback-button,
#welcomeVideo::-webkit-media-controls-start-playback-button,
#idleVideo::-webkit-media-controls-overlay-play-button,
#clipVideo::-webkit-media-controls-overlay-play-button,
#welcomeVideo::-webkit-media-controls-overlay-play-button {
  display: none !important;
  -webkit-appearance: none;
  appearance: none;
}

/*──────────────── BOTÓN PLAY INICIAL ────────────────
 * Cápsula circular abajo del avatar. NO oscurece ni desenfoca el idle:
 * el <video id="idleVideo"> está en autoplay+muted desde antes y debe
 * verse limpio. Al pulsar, JS hace .hidden=true y desaparece.
 */
#initialPlayButton {
  position: absolute;
  bottom: 4%;
  left: 50%;
  transform: translateX(-50%);
  width: 84px;
  height: 84px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(12px) saturate(1.2);
  -webkit-backdrop-filter: blur(12px) saturate(1.2);
  border: 1px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  color: #fff;
  transition: background 0.2s, transform 0.15s;
}
#initialPlayButton:hover {
  background: rgba(255, 255, 255, 0.22);
  transform: translateX(-50%) scale(1.06);
}
#initialPlayButton svg {
  width: 28px;
  height: 28px;
  margin-left: 3px;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.3));
}
/* JS hace .hidden=true al pulsar — sin esta regla, display:flex del id
   gana sobre el [hidden] por defecto del UA y el botón sigue visible. */
#initialPlayButton[hidden] { display: none; }

/*──────────────── PANEL DE CARGA INICIAL ────────────────
 * Aparece encima del avatar mientras se descargan los recursos.
 * Texto genérico — el usuario no debe enterarse de que son "clips".
 * Se oculta y el play button aparece cuando blobCache está completo.
 */
#loadingPanel {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(10, 14, 24, 0.78);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
  padding: 22px 28px;
  z-index: 25;
  min-width: 240px;
  max-width: 80%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: loadingPanelIn 0.3s ease-out;
}
#loadingPanel[hidden] { display: none; }
@keyframes loadingPanelIn {
  from { opacity: 0; transform: translate(-50%, -45%) scale(0.96); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
#loadingMsg {
  font-size: 14px;
  font-weight: 500;
  color: rgba(255, 255, 255, 0.92);
  letter-spacing: 0.01em;
  margin-bottom: 14px;
}
#loadingProgressTrack {
  width: 100%;
  height: 5px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 3px;
  overflow: hidden;
}
#loadingProgressBar {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, rgba(110, 170, 255, 0.95), rgba(220, 140, 200, 0.95));
  box-shadow: 0 0 10px rgba(140, 170, 240, 0.5);
  border-radius: 3px;
  transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
#loadingPct {
  font-size: 11.5px;
  color: rgba(255, 255, 255, 0.55);
  margin-top: 8px;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}

/*──────────────── INDICADOR "AI" ────────────────*/
#aiIndicator {
  position: absolute;
  top: 14px;
  right: 14px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(12px);
  color: #fff;
  font-weight: 600;
  font-size: 10.5px;
  letter-spacing: 0.18em;
  padding: 5px 11px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 7px;
  z-index: 20;
  display: none;
  animation: aiBlink 1.3s ease-in-out infinite;
}
@keyframes aiBlink { 50% { opacity: 0.5; } }

/*──────────────── SUBTÍTULO (avatar) ────────────────*/
#subtitle {
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: 16px;
  z-index: 15;
  padding: 12px 18px;
  background: rgba(10, 14, 24, 0.68);
  backdrop-filter: blur(18px) saturate(1.2);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
  color: #fff;
  font-size: clamp(14px, 1.5vw, 17px);
  line-height: 1.42;
  font-weight: 450;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  text-align: center;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  pointer-events: none;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
#subtitle.visible { opacity: 1; transform: translateY(0); }

/*──────────────── STATUS ZONE (visualizer + label) ────────────────*/
#statusZone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  flex: 0 0 auto;
  min-height: 56px;
}

/* Visualizer de 20 barras. JS escribe --h (0–1) por barra. */
#audioViz {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  height: 32px;
  width: 220px;
  opacity: 0.75;
  transition: opacity 0.3s;
}
.viz-bar {
  width: 3px;
  height: calc(var(--h, 0.08) * 100%);
  min-height: 10%;
  background: linear-gradient(180deg, var(--fg-soft), var(--fg-muted));
  border-radius: 2px;
  transition: height 0.07s cubic-bezier(0.4, 0, 0.2, 1);
}
#audioViz.mode-listening .viz-bar { background: linear-gradient(180deg, var(--accent), var(--accent-soft)); }
#audioViz.mode-speaking .viz-bar { background: linear-gradient(180deg, #86a4e2, #4d73a8); }
#audioViz.idle { opacity: 0.3; }

#statusLabel {
  font-size: 12.5px;
  color: var(--fg-muted);
  letter-spacing: 0.05em;
  min-height: 16px;
  transition: color 0.2s;
}
#statusLabel.accent { color: var(--fg-soft); }

/*──────────────── BARRA INFERIOR ────────────────*/
#bottomBar {
  position: relative;
  z-index: 5;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 16px 22px 22px;
  max-width: 920px;
  margin: 0 auto;
  width: 100%;
}

/*──────────────── INPUT PILL ────────────────*/
#inputRow {
  flex: 1 1 auto;
  max-width: 580px;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--glass);
  backdrop-filter: blur(18px) saturate(1.1);
  -webkit-backdrop-filter: blur(18px) saturate(1.1);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 5px 5px 20px;
  transition: border-color 0.2s, background 0.2s;
  /* iOS: que un long-press cerca del mic no seleccione el placeholder. */
  -webkit-user-select: none;
  user-select: none;
}
#inputRow:focus-within { border-color: var(--border-strong); background: var(--glass-strong); }
#inputRow.listening { border-color: var(--accent-soft); background: rgba(255, 77, 77, 0.05); }

#chatInput {
  flex: 1 1 auto;
  height: var(--mic-size);
  background: transparent;
  border: none;
  outline: none;
  /* 16px mínimo: con menos, iOS hace zoom automático al enfocar el input
     y descoloca todo el layout. */
  font-size: 16px;
  font-weight: 400;
  color: var(--fg);
  letter-spacing: 0.01em;
  /* El input SÍ debe permitir seleccionar/editar su texto. */
  -webkit-user-select: text;
  user-select: text;
}
#chatInput::placeholder { color: var(--fg-muted); }
#chatInput:disabled { opacity: 0.75; cursor: default; }

/*──────────────── BOTÓN MIC ────────────────*/
#micButton {
  flex: 0 0 var(--mic-size);
  width: var(--mic-size);
  height: var(--mic-size);
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(255, 77, 77, 0.3);
  transition: transform 0.1s, background 0.15s, box-shadow 0.15s, opacity 0.2s;
  position: relative;
  /* PTT en iOS: mantener pulsado NO debe iniciar selección de texto, lupa,
     callout ni scroll. touch-action none = ningún gesto del sistema sobre
     el botón (el micro-movimiento del dedo ya no dispara pointercancel). */
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#micButton:hover:not(:disabled) { background: #ff5d5d; }
#micButton.pressing { transform: scale(0.92); }
#micButton:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }
#micButton svg { width: 52%; height: 52%; pointer-events: none; }

#micButton.state-listening { animation: micPulse 1.4s ease-in-out infinite; }
@keyframes micPulse {
  0%   { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0.5); }
  70%  { box-shadow: 0 0 0 14px rgba(255, 77, 77, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0); }
}

#micButton.state-speaking {
  background: rgba(255, 255, 255, 0.14);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.18);
}
#micButton.state-speaking:hover:not(:disabled) { background: rgba(255, 255, 255, 0.22); }

/*──────────────── HISTORIAL LINK ────────────────*/
#historyToggle {
  flex: 0 0 auto;
  padding: 6px 10px;
  color: var(--fg-muted);
  font-size: 12.5px;
  letter-spacing: 0.03em;
  transition: color 0.15s, background 0.15s;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border-radius: 999px;
}
#historyToggle svg { width: 14px; height: 14px; }
#historyToggle:hover { color: var(--fg-soft); background: var(--glass); }

/*──────────────── Acciones del footer + switch manos libres ────────────────*/
#footerActions {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}
#handsFreeSwitch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  color: var(--fg-muted);
  font-size: 12.5px;
  letter-spacing: 0.03em;
  white-space: nowrap;
  transition: color 0.15s;
}
#handsFreeSwitch:hover { color: var(--fg-soft); }
#handsFreeCheckbox { position: absolute; opacity: 0; width: 1px; height: 1px; }
#handsFreeSwitch .switch-track {
  position: relative;
  flex: 0 0 auto;
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid var(--border);
  transition: background 0.18s, border-color 0.18s;
}
#handsFreeSwitch .switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--fg-soft);
  transition: transform 0.18s, background 0.18s;
}
#handsFreeCheckbox:checked + .switch-track {
  background: var(--accent-ghost);
  border-color: var(--accent-soft);
}
#handsFreeCheckbox:checked + .switch-track .switch-thumb {
  transform: translateX(14px);
  background: var(--accent);
}
#handsFreeCheckbox:focus-visible + .switch-track {
  outline: 2px solid var(--accent-soft);
  outline-offset: 2px;
}
@media (max-width: 720px) {
  #footerActions {
    flex-direction: row;
    align-self: center;
    align-items: center;
    justify-content: center;
    gap: 18px;
  }
}

/*──────────────── ERROR / WARMUP ────────────────*/
#errorBox {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 90%;
  padding: 10px 16px;
  background: rgba(200, 60, 60, 0.92);
  backdrop-filter: blur(10px);
  color: #fff;
  border-radius: 10px;
  font-size: 13.5px;
  z-index: 100;
  display: none;
  border: 1px solid rgba(255, 100, 100, 0.4);
}

/* Barra fina de carga tipo iOS. Se posiciona justo debajo del topBar. */
#loadLine {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  z-index: 60;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s;
}
#loadLine.visible { opacity: 1; }
#loadLineBar {
  height: 100%;
  width: 0%;
  background: linear-gradient(
    90deg,
    rgba(110, 170, 255, 0.9),
    rgba(220, 140, 200, 0.9)
  );
  box-shadow: 0 0 10px rgba(140, 170, 240, 0.5);
  transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 0 2px 2px 0;
}

/*──────────────── PANEL HISTORIAL ────────────────*/
#historyBackdrop {
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 14, 0.6);
  backdrop-filter: blur(6px);
  z-index: 190;
  opacity: 0;
  transition: opacity 0.22s;
}
#historyBackdrop.open { opacity: 1; }

#historyPanel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(420px, 92vw);
  height: 100dvh;
  background: rgba(16, 22, 36, 0.92);
  backdrop-filter: blur(22px) saturate(1.3);
  -webkit-backdrop-filter: blur(22px) saturate(1.3);
  border-left: 1px solid var(--border);
  box-shadow: -20px 0 60px rgba(0, 0, 0, 0.5);
  z-index: 200;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
}
#historyPanel.open { transform: translateX(0); }

#historyHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  font-size: 11.5px;
  font-weight: 600;
  color: var(--fg-muted);
  letter-spacing: 0.16em;
  text-transform: uppercase;
}
#historyClose {
  width: 32px;
  height: 32px;
  color: var(--fg-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.15s, color 0.15s;
}
#historyClose:hover { background: var(--glass); color: var(--fg); }
#historyClose svg { width: 16px; height: 16px; }

#historyContent {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 18px 22px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--fg-faint) transparent;
}
#historyContent::-webkit-scrollbar { width: 6px; }
#historyContent::-webkit-scrollbar-thumb { background: var(--fg-faint); border-radius: 3px; }

.msg {
  max-width: 85%;
  padding: 11px 15px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.42;
  word-wrap: break-word;
  color: var(--fg);
}
.msg.user {
  align-self: flex-end;
  background: linear-gradient(135deg, #2d4666, #2a3b56);
  border-bottom-right-radius: 5px;
}
.msg.assistant {
  align-self: flex-start;
  background: rgba(255, 255, 255, 0.055);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}
.msg time {
  display: block;
  font-size: 10.5px;
  color: var(--fg-muted);
  margin-top: 5px;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.03em;
}

#historyContent .empty {
  color: var(--fg-muted);
  text-align: center;
  font-style: italic;
  font-size: 13.5px;
  padding: 48px 0;
}

/* Input al pie del panel para escribir sin salir del chat. */
#historyInputRow {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 18px 16px;
  border-top: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.25);
}
#historyInput {
  flex: 1 1 auto;
  height: 40px;
  padding: 0 16px;
  background: var(--glass);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 16px; /* <16px provoca zoom automático de iOS al enfocar */
  color: var(--fg);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}
#historyInput::placeholder { color: var(--fg-muted); }
#historyInput:focus { border-color: var(--border-strong); background: var(--glass-strong); }
#historyInput:disabled { opacity: 0.55; cursor: not-allowed; }

#historySend {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.1s, opacity 0.2s;
  box-shadow: 0 3px 10px rgba(255, 77, 77, 0.3);
}
#historySend:hover:not(:disabled) { background: #ff5d5d; }
#historySend:active { transform: scale(0.93); }
#historySend:disabled { opacity: 0.4; cursor: not-allowed; box-shadow: none; }
#historySend svg { width: 16px; height: 16px; margin-left: -2px; }

/*──────────────── MÓVIL (portrait) ────────────────*/
@media (max-width: 640px) {
  :root { --mic-size: 46px; }

  #topBar { padding: 14px 16px; }
  #brand { font-size: 11.5px; padding: 5px 12px 5px 9px; }
  #settingsBtn { width: 34px; height: 34px; }

  #stage { padding: 0 18px; gap: 14px; }

  #avatarWrap { max-height: calc(100dvh - 260px); }
  #avatarBox { border-radius: 20px; }

  #subtitle {
    font-size: 13.5px;
    padding: 10px 14px;
    left: 10px;
    right: 10px;
    bottom: 12px;
  }

  #audioViz { width: 200px; height: 30px; }
  #statusLabel { font-size: 12px; }

  /* BottomBar en COLUMNA: input ancho + "Historial" centrado debajo. */
  #bottomBar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: 10px 18px 22px;
  }
  #inputRow {
    max-width: none;
    width: 100%;
    padding: 5px 5px 5px 18px;
  }
  #chatInput { font-size: 15px; }

  #historyToggle {
    align-self: center;
    font-size: 12.5px;
  }

  /* Panel de historial full-screen en móvil. */
  #historyPanel {
    width: 100vw;
    border-left: none;
  }
  #historyHeader { padding: 16px 18px; }
  #historyContent { padding: 16px 18px; }
}

/* Alturas bajas (móvil landscape, pantallas pequeñas). */
@media (max-height: 640px) {
  #avatarWrap { max-height: calc(100dvh - 220px); }
  #statusZone { min-height: 40px; gap: 4px; }
  #audioViz { height: 24px; }
}

/* Móviles estrechos (iPhone SE y similares). */
@media (max-width: 380px) {
  :root { --mic-size: 44px; }
  #stage { padding: 0 14px; gap: 10px; }
  #bottomBar { padding: 8px 14px 18px; }
  #inputRow { padding: 4px 4px 4px 15px; }
  #audioViz { width: 170px; height: 26px; }
  #brand { font-size: 11px; padding: 5px 10px 5px 8px; }
  .brand-dot { width: 6px; height: 6px; }
}

/*──────────────── VÍDEO DE BIENVENIDA (overlay) ────────────────
 * Capa por encima del idle/clip durante el calentamiento de cache.
 * Misma geometría que #idleVideo (inset:0 + border-radius del avatarBox).
 * Fade out al terminar el vídeo o al primer segmento hablado.
 */
.welcome-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center 12%;
  z-index: 4; /* sobre clip (z:3), bajo botón play (z:10) */
  opacity: 1;
  transition: opacity 0.6s ease-out;
  pointer-events: none;
}
.welcome-video[hidden] { display: none; }
.welcome-video.fading { opacity: 0; }

/*──────────────── MODAL RGPD ────────────────
 * Centrado, opaco, con backdrop. Estilo neutro — los temas redefinen
 * fondo/colores/tipografía para alinearse con la marca.
 */
#rgpdModal {
  position: fixed;
  inset: 0;
  z-index: 250;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
#rgpdModal[hidden] { display: none; }

#rgpdBackdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 5, 11, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: rgpdBackdropIn 0.25s ease-out;
}
@keyframes rgpdBackdropIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

#rgpdCard {
  position: relative;
  width: min(460px, 100%);
  background: linear-gradient(180deg, #131b2c, #0d1422);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 26px 26px 22px;
  color: var(--fg);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
  animation: rgpdCardIn 0.32s cubic-bezier(0.18, 0.7, 0.2, 1);
}
@keyframes rgpdCardIn {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

#rgpdTitle {
  font-size: 19px;
  font-weight: 600;
  margin-bottom: 12px;
  letter-spacing: 0.005em;
}

#rgpdBody {
  font-size: 14px;
  line-height: 1.55;
  color: var(--fg-soft);
  margin-bottom: 18px;
}
#rgpdBody a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

#rgpdCheckLabel {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  font-size: 13.5px;
  line-height: 1.45;
  color: var(--fg-soft);
  cursor: pointer;
  user-select: none;
  margin-bottom: 18px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: background 0.15s, border-color 0.15s;
}
#rgpdCheckLabel:hover {
  background: rgba(255, 255, 255, 0.05);
  border-color: var(--border-strong);
}
#rgpdCheckbox {
  flex: 0 0 18px;
  width: 18px;
  height: 18px;
  margin-top: 2px;
  accent-color: var(--accent);
  cursor: pointer;
}

#rgpdAcceptBtn {
  width: 100%;
  padding: 12px 18px;
  font-size: 14.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(255, 77, 77, 0.32);
  transition: background 0.15s, transform 0.08s, box-shadow 0.15s, opacity 0.2s;
}
#rgpdAcceptBtn:hover:not(:disabled) {
  background: #ff5d5d;
  box-shadow: 0 8px 24px rgba(255, 77, 77, 0.4);
}
#rgpdAcceptBtn:active:not(:disabled) { transform: scale(0.985); }
#rgpdAcceptBtn:disabled {
  background: rgba(255, 255, 255, 0.08);
  color: var(--fg-muted);
  box-shadow: none;
  cursor: not-allowed;
}

@media (max-width: 480px) {
  #rgpdCard { padding: 22px 20px 18px; border-radius: 16px; }
  #rgpdTitle { font-size: 17.5px; }
  #rgpdBody  { font-size: 13.5px; }
}

/*──────────────── Modo widget (iframe) ────────────────
 * Paridad con el widget flotante del player antiguo: el padre encoge el
 * iframe a una burbuja y manda postMessage {type:'set_view', view:'minimized'}.
 * En minimizado SOLO se ve el avatar llenando el viewport del iframe (el
 * recorte redondo lo hace el padre); todo lo demás se oculta. La conversación
 * y el audio SIGUEN: solo cambia la presentación.
 */
body.view-minimized #topBar,
body.view-minimized #bottomBar,
body.view-minimized #statusZone,
body.view-minimized #subtitle,
body.view-minimized #aiIndicator,
body.view-minimized #initialPlayButton,
body.view-minimized #loadingPanel,
body.view-minimized #loadLine,
body.view-minimized #historyPanel,
body.view-minimized #historyBackdrop,
body.view-minimized #errorBox,
body.view-minimized #avatarGlow {
  display: none !important;
}
body.view-minimized #stage { padding: 0; gap: 0; }
body.view-minimized #avatarWrap {
  position: fixed;
  inset: 0;
  aspect-ratio: auto;
  max-height: none;
  height: 100%;
  width: 100%;
}
body.view-minimized #avatarBox {
  aspect-ratio: auto;
  width: 100%;
  height: 100%;
  border-radius: 0;
  box-shadow: none;
}

/* Dentro de un iframe el avatar es clicable (pliega/despliega el widget). */
body.in-iframe #avatarBox { cursor: pointer; }

/*──────────────── Modal de agendado (Cal.com) ────────────────*/
#schedulingModal {
  position: fixed;
  inset: 0;
  z-index: 260;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
#schedulingModal[hidden] { display: none; }

#schedulingBackdrop {
  position: absolute;
  inset: 0;
  background: rgba(2, 5, 11, 0.72);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: rgpdBackdropIn 0.25s ease-out;
}

#schedulingCard {
  position: relative;
  display: flex;
  flex-direction: column;
  width: min(760px, 100%);
  height: min(720px, 92vh);
  background: linear-gradient(180deg, #131b2c, #0d1422);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.55);
  animation: rgpdCardIn 0.32s cubic-bezier(0.18, 0.7, 0.2, 1);
}

#schedulingHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
#schedulingModalTitle {
  font-size: 16px;
  font-weight: 600;
  color: var(--fg);
  letter-spacing: 0.005em;
}
#closeSchedulingModal {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 9px;
  color: var(--fg-soft);
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
#closeSchedulingModal:hover {
  background: rgba(255, 255, 255, 0.06);
  border-color: var(--border-strong);
  color: var(--fg);
}
#closeSchedulingModal svg { width: 18px; height: 18px; }

#schedulingBody {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  /* Fondo blanco: Cal.com se carga en claro y evita un flash oscuro al montar. */
  background: #fff;
}
#calFrame {
  width: 100%;
  height: 100%;
  border: none;
}

@media (max-width: 480px) {
  #schedulingModal { padding: 0; }
  #schedulingCard {
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    border: none;
  }
}
