/* ============================================================
   AGENTE DENTAL - Chat flotante
   Estilos aislados con prefijo .ad- para no chocar con la web
   ============================================================ */

.ad-root {
  --ad-primary: #0A4D68;
  --ad-primary-hover: #088395;
  --ad-accent: #05BFDB;
  --ad-bg: #FFFFFF;
  --ad-bg-soft: #F0F4F8;
  --ad-text: #102A43;
  --ad-text-soft: #334E68;
  --ad-border: #D9E2EC;
  --ad-user-bubble: #0A4D68;
  --ad-user-text: #FFFFFF;
  --ad-bot-bubble: #F0F4F8;
  --ad-bot-text: #102A43;
  --ad-shadow: 0 16px 48px rgba(16, 42, 67, 0.18);

  font-family: 'DM Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--ad-text);
  line-height: 1.55;
  box-sizing: border-box;
}

.ad-root *,
.ad-root *::before,
.ad-root *::after {
  box-sizing: border-box;
}

/* ---------- Burbuja flotante (cerrada) ---------- */
.ad-bubble {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: var(--ad-primary);
  color: #fff;
  border: none;
  cursor: pointer;
  padding: 0;
  box-shadow: 0 10px 30px rgba(10, 77, 104, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483000;
  animation: ad-welcome-bounce 8s ease-in-out infinite;
}
.ad-bubble:hover {
  animation-play-state: paused;
  transform: scale(1.06);
  box-shadow: 0 14px 36px rgba(10, 77, 104, 0.55);
}
.ad-bubble svg {
  width: 28px;
  height: 28px;
  fill: currentColor;
}
.ad-bubble-img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  display: block;
  object-fit: cover;
  user-select: none;
  -webkit-user-drag: none;
}
.ad-bubble.ad-hidden { display: none; }

/* Móvil: tras los primeros 6 s la burbuja deja de saltar (el anillo sigue) */
.ad-bubble.ad-settled {
  animation: none;
}

/* ---------- Etiqueta "Agente dental" ---------- */
.ad-bubble-label {
  position: absolute;
  right: calc(100% + 14px);
  top: 50%;
  transform: translateY(-50%) translateX(6px);
  z-index: 1;
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--ad-primary);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  line-height: 1;
  letter-spacing: .01em;
  white-space: nowrap;
  box-shadow: 0 8px 20px rgba(10, 77, 104, 0.28);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}
.ad-bubble-label::after {
  content: "";
  position: absolute;
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
  border: 6px solid transparent;
  border-left-color: var(--ad-primary);
}

/* Escritorio: aparece al pasar el cursor o al enfocar con teclado */
@media (hover: hover) and (pointer: fine) {
  .ad-bubble:hover .ad-bubble-label,
  .ad-bubble:focus-visible .ad-bubble-label {
    opacity: 1;
    transform: translateY(-50%) translateX(0);
  }
}

/* Táctil: la muestra el JS durante 3 s al cargar */
.ad-bubble-label.is-visible {
  opacity: 1;
  transform: translateY(-50%) translateX(0);
}

@media (prefers-reduced-motion: reduce) {
  .ad-bubble-label { transition: none; }
}

.ad-bubble::after {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: 50%;
  border: 2px solid var(--ad-primary);
  opacity: .5;
  animation: ad-pulse 2.2s ease-out infinite;
}
@keyframes ad-pulse {
  0%   { transform: scale(0.9);  opacity: .6; }
  100% { transform: scale(1.5);  opacity: 0;  }
}

@keyframes ad-welcome-bounce {
  0%, 100% { transform: translateY(0); }
  37.5%    { transform: translateY(-25vh); }
  75%      { transform: translateY(0); }
}

/* ---------- Ventana del chat ---------- */
.ad-window {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: 380px;
  height: 600px;
  max-height: calc(100vh - 40px);
  background: var(--ad-bg);
  border-radius: 18px;
  box-shadow: var(--ad-shadow);
  display: none;
  flex-direction: column;
  overflow: hidden;
  z-index: 2147483000;
  border: 1px solid var(--ad-border);
  animation: ad-slide-up .25s ease-out;
}
.ad-window.ad-open { display: flex; }

@keyframes ad-slide-up {
  from { opacity: 0; transform: translateY(20px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- Header ---------- */
.ad-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--ad-primary);
  color: #fff;
}
.ad-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}
.ad-header-info { flex: 1; min-width: 0; }
.ad-header-title {
  font-weight: 600;
  font-size: 15px;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ad-header-status {
  font-size: 12px;
  opacity: 0.85;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 2px;
}
.ad-status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #27C86B;
}
.ad-status-dot.ad-off { background: #F0A020; }

.ad-header-actions {
  display: flex;
  gap: 4px;
}
.ad-icon-btn {
  background: transparent;
  border: none;
  color: #fff;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s ease;
}
.ad-icon-btn:hover { background: rgba(255,255,255,0.18); }
.ad-icon-btn svg { width: 18px; height: 18px; fill: currentColor; }

/* ---------- Área de mensajes ---------- */
.ad-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  background: var(--ad-bg-soft);
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}
.ad-messages::-webkit-scrollbar { width: 6px; }
.ad-messages::-webkit-scrollbar-thumb {
  background: var(--ad-border);
  border-radius: 3px;
}

.ad-msg {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: 16px;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: ad-msg-in .2s ease-out;
  font-size: 14.5px;
}
@keyframes ad-msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.ad-msg-user {
  align-self: flex-end;
  background: var(--ad-user-bubble);
  color: var(--ad-user-text);
  border-bottom-right-radius: 4px;
}
.ad-msg-bot {
  align-self: flex-start;
  background: var(--ad-bot-bubble);
  color: var(--ad-bot-text);
  border-bottom-left-radius: 4px;
}
.ad-msg-bot a {
  color: var(--ad-primary);
  font-weight: 500;
}

/* ---------- Typing indicator ---------- */
.ad-typing {
  align-self: flex-start;
  background: var(--ad-bot-bubble);
  padding: 12px 16px;
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  display: flex;
  gap: 4px;
  animation: ad-msg-in .2s ease-out;
}
.ad-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ad-text-soft);
  opacity: 0.6;
  animation: ad-blink 1.2s infinite ease-in-out;
}
.ad-typing span:nth-child(2) { animation-delay: .2s; }
.ad-typing span:nth-child(3) { animation-delay: .4s; }
@keyframes ad-blink {
  0%, 80%, 100% { transform: scale(0.7); opacity: 0.4; }
  40%           { transform: scale(1);   opacity: 1; }
}

/* ---------- Quick actions (botones bajo header) ---------- */
.ad-quick {
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  background: var(--ad-bg);
  border-bottom: 1px solid var(--ad-border);
  overflow-x: auto;
  scrollbar-width: none;
}
.ad-quick::-webkit-scrollbar { display: none; }
.ad-quick button {
  flex-shrink: 0;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--ad-border);
  background: var(--ad-bg);
  color: var(--ad-text);
  font-size: 12.5px;
  cursor: pointer;
  transition: all .15s ease;
  white-space: nowrap;
}
.ad-quick button:hover {
  background: var(--ad-primary);
  color: #fff;
  border-color: var(--ad-primary);
}

/* ---------- Input ---------- */
.ad-input-wrap {
  padding: 10px 12px;
  background: var(--ad-bg);
  border-top: 1px solid var(--ad-border);
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.ad-input {
  flex: 1;
  border: 1px solid var(--ad-border);
  border-radius: 20px;
  padding: 10px 14px;
  font-size: 14px;
  font-family: inherit;
  background: var(--ad-bg-soft);
  color: var(--ad-text);
  resize: none;
  max-height: 100px;
  min-height: 40px;
  outline: none;
  transition: border-color .15s ease;
  line-height: 1.35;
}
.ad-input:focus { border-color: var(--ad-primary); }
.ad-input::placeholder { color: var(--ad-text-soft); }

.ad-send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--ad-primary);
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s ease, transform .1s ease;
}
.ad-send:hover:not(:disabled) { background: var(--ad-primary-hover); }
.ad-send:active:not(:disabled) { transform: scale(0.94); }
.ad-send:disabled { opacity: 0.4; cursor: not-allowed; }
.ad-send svg { width: 18px; height: 18px; fill: currentColor; }

/* ---------- Footer (WhatsApp + legal) ---------- */
.ad-footer {
  padding: 6px 12px 10px;
  background: var(--ad-bg);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  color: var(--ad-text-soft);
}
.ad-footer a {
  color: var(--ad-text-soft);
  text-decoration: none;
}
.ad-wa {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: #25D366;
  color: #fff !important;
  font-size: 12px;
  font-weight: 500;
  text-decoration: none;
  transition: background .15s ease;
}
.ad-wa:hover { background: #1EB954; }
.ad-wa svg { width: 14px; height: 14px; fill: currentColor; }

/* ---------- Tarjeta de resumen de cita ---------- */
.ad-card {
  align-self: stretch;
  background: var(--ad-bg);
  border: 1px solid var(--ad-border);
  border-radius: 12px;
  padding: 14px;
  animation: ad-msg-in .3s ease-out;
}
.ad-card h4 {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--ad-primary);
  display: flex;
  align-items: center;
  gap: 6px;
}
.ad-card-row {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  border-bottom: 1px dashed var(--ad-border);
  font-size: 13.5px;
}
.ad-card-row:last-of-type { border-bottom: none; }
.ad-card-row strong { color: var(--ad-text-soft); font-weight: 500; }
.ad-card-row span { color: var(--ad-text); font-weight: 500; text-align: right; }
.ad-card-actions {
  margin-top: 12px;
  display: flex;
  gap: 8px;
}
.ad-card-actions button {
  flex: 1;
  padding: 8px 12px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background .15s ease;
}
.ad-btn-primary {
  background: var(--ad-primary);
  color: #fff;
}
.ad-btn-primary:hover { background: var(--ad-primary-hover); }
.ad-btn-secondary {
  background: var(--ad-bg-soft);
  color: var(--ad-text);
  border: 1px solid var(--ad-border) !important;
}

/* ---------- Responsive: ventana y contenido ---------- */

/* El chat usa una columna flexible: solo los mensajes se encogen y desplazan. */
.ad-window {
  position: fixed;
  right: 24px;
  bottom: 24px;
  top: auto;
  left: auto;
  width: min(400px, calc(100vw - 32px));
  height: min(680px, calc(100dvh - 48px));
  max-height: calc(100dvh - 48px);
  min-height: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--ad-border);
  border-radius: 18px;
  background: var(--ad-bg);
  box-shadow: var(--ad-shadow);
  z-index: 2147483000;
}

.ad-window.ad-open { display: flex; }

.ad-window *,
.ad-window *::before,
.ad-window *::after { box-sizing: border-box; }

.ad-header {
  flex: 0 0 auto;
  min-width: 0;
  gap: 10px;
}

.ad-header-info { flex: 1 1 auto; min-width: 0; }

.ad-header-title {
  white-space: normal;
  overflow-wrap: anywhere;
}

.ad-header-actions {
  flex: 0 0 auto;
  align-items: center;
}

.ad-icon-btn {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
}

/* Los accesos rápidos se reparten en filas, sin desplazamiento horizontal. */
.ad-quick {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  min-width: 0;
  max-height: none;
  overflow: visible;
  padding: 9px 12px;
  scrollbar-width: auto;
}

.ad-quick button {
  flex: 0 1 auto;
  min-width: 0;
  max-width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
  line-height: 1.3;
}

/* El historial ocupa el espacio sobrante y tiene su propio scroll. */
.ad-messages {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* Entrada y pie no se comprimen cuando crece el historial. */
.ad-input-wrap {
  flex: 0 0 auto;
  min-width: 0;
  align-items: flex-end;
}

.ad-input {
  flex: 1 1 auto;
  width: 0;
  min-width: 0;
  max-height: 100px;
}

.ad-send {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
}

.ad-footer {
  flex: 0 0 auto;
  min-width: 0;
  flex-wrap: wrap;
  gap: 6px 10px;
}

.ad-footer > span {
  flex: 1 1 130px;
  min-width: 0;
  overflow-wrap: anywhere;
}

.ad-wa {
  flex: 0 0 auto;
  white-space: nowrap;
}

/* Móvil: ventana completa, respetando las áreas seguras del dispositivo. */
@media (max-width: 480px) {
  .ad-window {
    top: var(--ad-vv-top, env(safe-area-inset-top, 0px));
    right: max(0px, env(safe-area-inset-right));
    bottom: auto;
    left: max(0px, env(safe-area-inset-left));
    width: auto;
    height: var(--ad-vv-height, 100dvh);
    max-height: var(--ad-vv-height, 100dvh);
    border-radius: 0;
    border: 0;
  }

  .ad-header {
    padding-top: max(12px, env(safe-area-inset-top));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: 10px;
    padding-left: max(12px, env(safe-area-inset-left));
  }

  .ad-quick { padding: 8px 10px; gap: 6px; }

  .ad-quick button {
    font-size: 11.5px;
    padding: 6px 9px;
  }

  .ad-messages { padding: 12px; }

  .ad-input-wrap {
    padding-right: max(10px, env(safe-area-inset-right));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    padding-left: max(10px, env(safe-area-inset-left));
  }

  .ad-input { font-size: 16px; }

  .ad-footer {
    padding-right: max(10px, env(safe-area-inset-right));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    padding-left: max(10px, env(safe-area-inset-left));
  }

  .ad-footer > span { white-space: normal; }

  .ad-icon-btn {
    width: 40px;
    height: 40px;
    flex-basis: 40px;
  }
}

/* Pantallas estrechas. */
@media (max-width: 360px) {
  .ad-header { gap: 6px; padding-right: 8px; padding-left: 8px; }

  .ad-avatar {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }

  .ad-header-title { font-size: 13px; }
  .ad-header-status { font-size: 10px; }
  .ad-header-actions { gap: 1px; }

  .ad-icon-btn {
    width: 34px;
    height: 34px;
    flex-basis: 34px;
  }

  .ad-quick { gap: 5px; padding: 7px; }

  .ad-quick button {
    font-size: 10.5px;
    padding: 5px 7px;
  }

  .ad-msg {
    max-width: 90%;
    font-size: 13px;
  }

  .ad-footer { font-size: 10px; }
}

/* Móvil en horizontal o ventanas con poca altura. */
@media (max-height: 520px) {
  .ad-window {
    height: calc(100dvh - 12px);
    max-height: calc(100dvh - 12px);
    bottom: 6px;
  }

  .ad-header { padding-top: 6px; padding-bottom: 6px; }

  .ad-avatar {
    width: 32px;
    height: 32px;
    font-size: 16px;
  }

  .ad-quick {
    gap: 4px;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .ad-quick button {
    padding-top: 4px;
    padding-bottom: 4px;
  }

  .ad-footer { padding-top: 4px; padding-bottom: 4px; }
}

@media (max-width: 480px) and (max-height: 520px) {
  .ad-window {
    top: var(--ad-vv-top, 0px);
    right: max(6px, env(safe-area-inset-right));
    bottom: auto;
    left: max(6px, env(safe-area-inset-left));
    width: auto;
    height: calc(var(--ad-vv-height, 100dvh) - 12px);
    max-height: calc(var(--ad-vv-height, 100dvh) - 12px);
    border-radius: 12px;
  }

  .ad-footer > span { display: none; }
  .ad-wa { margin-left: auto; }
}

@media (prefers-reduced-motion: reduce) {
  .ad-bubble,
  .ad-bubble::after,
  .ad-msg,
  .ad-window {
    animation: none !important;
    transition: none !important;
  }
}

/* ---------- Modo oscuro ---------- */
.ad-root[data-theme="dark"] {
  --ad-primary: #05BFDB;
  --ad-primary-hover: #38D3E8;
  --ad-accent: #088395;
  --ad-bg: #0D1B26;
  --ad-bg-soft: #142834;
  --ad-text: #E8F0F5;
  --ad-text-soft: #8FA8B8;
  --ad-border: #1F3342;
  --ad-user-bubble: #05BFDB;
  --ad-user-text: #0D1B26;
  --ad-bot-bubble: #1A2F3E;
  --ad-bot-text: #E8F0F5;
  --ad-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}

@media (max-width: 480px) {
  .ad-root[data-theme="dark"] .ad-header { background: #0A4D68; }
}

/* ---------- Corrección final: cabecera visible en todas las pantallas ---------- */
.ad-window.ad-open {
  display: flex;
}

.ad-header {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: nowrap;
  align-items: center;
  min-width: 0;
  min-height: 64px;
  overflow: visible;
  gap: 8px;
}

.ad-header .ad-avatar {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
}

.ad-header-info {
  flex: 1 1 0%;
  min-width: 0;
  overflow: hidden;
}

.ad-header-title {
  display: block;
  min-width: 0;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow-wrap: normal;
}

.ad-header-status {
  display: flex;
  align-items: center;
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
}

.ad-header-status .ad-status-dot {
  flex: 0 0 8px;
}

#ad-status-text {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ad-header-actions {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: flex-end;
  gap: 3px;
  margin-left: auto;
}

.ad-header-actions .ad-icon-btn {
  display: inline-flex;
  flex: 0 0 36px;
  align-items: center;
  justify-content: center;
  width: 36px;
  min-width: 36px;
  height: 36px;
  padding: 8px;
  margin: 0;
  box-sizing: border-box;
}

.ad-header-actions .ad-icon-btn svg {
  display: block;
  flex: 0 0 20px;
  width: 20px;
  height: 20px;
  min-width: 20px;
}

/* Móviles estrechos: reducir controles sin ocultar acciones. */
@media (max-width: 360px) {
  .ad-header {
    min-height: 56px;
    gap: 6px;
    padding-left: 8px;
    padding-right: 8px;
  }

  .ad-header .ad-avatar {
    flex-basis: 30px;
    width: 30px;
    height: 30px;
    font-size: 16px;
  }

  .ad-header-title { font-size: 12px; }
  .ad-header-status { font-size: 10px; gap: 4px; }
  .ad-header-actions { gap: 2px; }

  .ad-header-actions .ad-icon-btn {
    flex-basis: 32px;
    width: 32px;
    min-width: 32px;
    height: 32px;
    padding: 6px;
  }

  .ad-header-actions .ad-icon-btn svg {
    flex-basis: 18px;
    width: 18px;
    height: 18px;
    min-width: 18px;
  }
}

/* Móviles en horizontal: cabecera compacta, siempre fuera del scroll de mensajes. */
@media (max-height: 520px) {
  .ad-header {
    min-height: 48px;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .ad-header .ad-avatar {
    flex-basis: 30px;
    width: 30px;
    height: 30px;
    font-size: 16px;
  }

  .ad-header-actions .ad-icon-btn {
    flex-basis: 32px;
    width: 32px;
    min-width: 32px;
    height: 32px;
  }
}
