/*
 * BTOLabs Chat – Estilos del chat bubble
 *
 * Reglas:
 * - Todos los selectores van prefijados con .btolabs-chat- para evitar
 *   colisiones con el tema activo.
 * - Reset defensivo en el root para aislar el widget.
 * - Colores via CSS custom properties (sobreescribibles desde el admin).
 * - Solo se usa !important donde es estrictamente necesario (position, z-index).
 */

/* --------------------------------------------------------------------------
   Variables y reset del root
   -------------------------------------------------------------------------- */

:root {
  --btolabs-chat-primary:        #0a6cff;
  --btolabs-chat-primary-hover:  #0557d9;
  --btolabs-chat-primary-text:   #ffffff;
  --btolabs-chat-bg:             #ffffff;
  --btolabs-chat-surface:        #f5f7fa;
  --btolabs-chat-border:         #e2e8f0;
  --btolabs-chat-text:           #1a202c;
  --btolabs-chat-text-muted:     #718096;
  --btolabs-chat-user-bubble:    var(--btolabs-chat-primary);
  --btolabs-chat-bot-bubble:     #f0f4f8;
  --btolabs-chat-bot-text:       #1a202c;
  --btolabs-chat-shadow:         0 8px 32px rgba(0, 0, 0, 0.14);
  --btolabs-chat-radius:         16px;
  --btolabs-chat-radius-sm:      10px;
  --btolabs-chat-panel-width:    370px;
  --btolabs-chat-panel-height:   540px;
  --btolabs-chat-trigger-size:   58px;
  --btolabs-chat-z:              2147483000;
  --btolabs-chat-font:           -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  --btolabs-chat-transition:     0.22s ease;
}

/* Reset defensivo: aísla el widget del tema */
.btolabs-chat-root,
.btolabs-chat-root *,
.btolabs-chat-root *::before,
.btolabs-chat-root *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: var(--btolabs-chat-font);
  line-height: 1.5;
}

/* --------------------------------------------------------------------------
   Contenedor raíz
   -------------------------------------------------------------------------- */

.btolabs-chat-root {
  position: fixed !important;
  bottom: 24px;
  right: 24px;
  z-index: var(--btolabs-chat-z) !important;
  display: flex !important;
  flex-direction: column;
  align-items: flex-end;
  gap: 14px;
  pointer-events: none;
}

.btolabs-chat-root[data-position="bottom-left"] {
  right: auto;
  left: 24px;
  align-items: flex-start;
}

/* --------------------------------------------------------------------------
   Botón trigger flotante
   -------------------------------------------------------------------------- */

.btolabs-chat-trigger {
  width: var(--btolabs-chat-trigger-size);
  height: var(--btolabs-chat-trigger-size);
  border-radius: 50%;
  border: none;
  background: var(--btolabs-chat-primary);
  color: var(--btolabs-chat-primary-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.22);
  transition:
    background var(--btolabs-chat-transition),
    transform var(--btolabs-chat-transition),
    box-shadow var(--btolabs-chat-transition);
  flex-shrink: 0;
  position: relative;
  pointer-events: auto;
}

.btolabs-chat-trigger:hover,
.btolabs-chat-trigger:focus-visible {
  background: var(--btolabs-chat-primary-hover);
  transform: scale(1.07);
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.28);
  outline: none;
}

.btolabs-chat-trigger:active {
  transform: scale(0.97);
}

.btolabs-chat-trigger-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 0;
}

/* --- Botón con imagen personalizada --- */

.btolabs-chat-trigger--has-image {
  overflow: hidden;
  padding: 0;
  background: transparent;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.30);
}

.btolabs-chat-trigger--has-image:hover,
.btolabs-chat-trigger--has-image:focus-visible {
  background: transparent;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.38);
}

/* La imagen ocupa todo el círculo */
.btolabs-chat-trigger-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
  display: block;
  border-radius: 0; /* el padre ya tiene border-radius:50% + overflow:hidden */
  pointer-events: none;
  user-select: none;
}

/* Con imagen: el span que envuelve la img ocupa todo el botón y siempre es visible */
.btolabs-chat-trigger--has-image .btolabs-chat-trigger-icon--open {
  width: 100%;
  height: 100%;
  display: block !important;
  line-height: 0;
}

/* Con imagen: la X nunca se muestra (la imagen es el único indicador visual) */
.btolabs-chat-trigger--has-image .btolabs-chat-trigger-icon--close {
  display: none !important;
}

/* Badge de notificaciones */
.btolabs-chat-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: #e53e3e;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

/* --------------------------------------------------------------------------
   Panel de chat
   -------------------------------------------------------------------------- */

.btolabs-chat-panel {
  width: var(--btolabs-chat-panel-width);
  height: var(--btolabs-chat-panel-height);
  background: var(--btolabs-chat-bg);
  border-radius: var(--btolabs-chat-radius);
  box-shadow: var(--btolabs-chat-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Animación de aparición */
  transform-origin: bottom right;
  transform: scale(0.9) translateY(10px);
  opacity: 0;
  pointer-events: none;
  transition:
    transform var(--btolabs-chat-transition),
    opacity var(--btolabs-chat-transition);
}

.btolabs-chat-root[data-position="bottom-left"] .btolabs-chat-panel {
  transform-origin: bottom left;
}

.btolabs-chat-panel.is-open {
  transform: scale(1) translateY(0);
  opacity: 1;
  pointer-events: auto;
}

/* --------------------------------------------------------------------------
   Header
   -------------------------------------------------------------------------- */

.btolabs-chat-header {
  background: var(--btolabs-chat-primary);
  color: var(--btolabs-chat-primary-text);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.btolabs-chat-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btolabs-chat-close-btn {
  background: transparent;
  border: none;
  color: var(--btolabs-chat-primary-text);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  opacity: 0.85;
  transition: opacity var(--btolabs-chat-transition), background var(--btolabs-chat-transition);
  flex-shrink: 0;
  margin-left: 8px;
}

.btolabs-chat-close-btn:hover,
.btolabs-chat-close-btn:focus-visible {
  opacity: 1;
  background: rgba(255, 255, 255, 0.15);
  outline: none;
}

/* --------------------------------------------------------------------------
   Área de mensajes
   -------------------------------------------------------------------------- */

.btolabs-chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

/* Scrollbar delgada */
.btolabs-chat-messages::-webkit-scrollbar {
  width: 4px;
}
.btolabs-chat-messages::-webkit-scrollbar-thumb {
  background: var(--btolabs-chat-border);
  border-radius: 4px;
}

/* Mensaje individual */
.btolabs-chat-message {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--btolabs-chat-radius-sm);
  font-size: 14px;
  word-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.55;
}

.btolabs-chat-message--user {
  background: var(--btolabs-chat-user-bubble);
  color: var(--btolabs-chat-primary-text);
  align-self: flex-end;
  border-bottom-right-radius: 3px;
}

.btolabs-chat-message--bot {
  background: var(--btolabs-chat-bot-bubble);
  color: var(--btolabs-chat-bot-text);
  align-self: flex-start;
  border-bottom-left-radius: 3px;
}

.btolabs-chat-message--error {
  background: #fff5f5;
  color: #c53030;
  border: 1px solid #feb2b2;
  align-self: stretch;
  text-align: center;
  font-size: 13px;
}

/* --------------------------------------------------------------------------
   Indicador "escribiendo…"
   -------------------------------------------------------------------------- */

.btolabs-chat-typing {
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.btolabs-chat-typing-dot {
  width: 7px;
  height: 7px;
  background: var(--btolabs-chat-text-muted);
  border-radius: 50%;
  animation: btolabs-chat-bounce 1.2s infinite;
}
.btolabs-chat-typing-dot:nth-child(2) { animation-delay: 0.2s; }
.btolabs-chat-typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes btolabs-chat-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40%           { transform: translateY(-6px); opacity: 1; }
}

/* --------------------------------------------------------------------------
   Banner de límite alcanzado
   -------------------------------------------------------------------------- */

.btolabs-chat-limit-banner {
  margin: 0 12px 8px;
  background: #fffbeb;
  border: 1px solid #f6e05e;
  border-radius: var(--btolabs-chat-radius-sm);
  padding: 12px 14px;
  flex-shrink: 0;
}

.btolabs-chat-limit-text {
  font-size: 13px;
  color: #744210;
  /* Sin botón bajo el texto: antes margin-bottom: 10px */
  margin-bottom: 0;
  line-height: 1.5;
}

/*
.btolabs-chat-new-session-btn {
  background: var(--btolabs-chat-primary);
  color: var(--btolabs-chat-primary-text);
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--btolabs-chat-transition);
  width: 100%;
}
.btolabs-chat-new-session-btn:hover,
.btolabs-chat-new-session-btn:focus-visible {
  background: var(--btolabs-chat-primary-hover);
  outline: none;
}
*/

/* --------------------------------------------------------------------------
   Formulario de envío
   -------------------------------------------------------------------------- */

.btolabs-chat-form {
  padding: 10px 12px;
  border-top: 1px solid var(--btolabs-chat-border);
  display: flex;
  align-items: flex-end;
  gap: 8px;
  flex-shrink: 0;
  background: var(--btolabs-chat-bg);
}

.btolabs-chat-input {
  flex: 1;
  resize: none;
  border: 1px solid var(--btolabs-chat-border);
  border-radius: var(--btolabs-chat-radius-sm);
  padding: 9px 12px;
  font-size: 14px;
  color: var(--btolabs-chat-text);
  background: var(--btolabs-chat-surface);
  line-height: 1.5;
  max-height: 120px;
  overflow-y: auto;
  transition: border-color var(--btolabs-chat-transition), box-shadow var(--btolabs-chat-transition);
  outline: none;
}

.btolabs-chat-input:focus {
  border-color: var(--btolabs-chat-primary);
  box-shadow: 0 0 0 3px rgba(10, 108, 255, 0.12);
  background: var(--btolabs-chat-bg);
}

.btolabs-chat-input::placeholder {
  color: var(--btolabs-chat-text-muted);
}

.btolabs-chat-input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btolabs-chat-submit-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: var(--btolabs-chat-primary);
  color: var(--btolabs-chat-primary-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--btolabs-chat-transition), transform var(--btolabs-chat-transition);
}
.btolabs-chat-submit-btn:hover,
.btolabs-chat-submit-btn:focus-visible {
  background: var(--btolabs-chat-primary-hover);
  outline: none;
}
.btolabs-chat-submit-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

/* --------------------------------------------------------------------------
   Footer
   -------------------------------------------------------------------------- */

.btolabs-chat-footer {
  padding: 6px 14px 8px;
  text-align: center;
  border-top: 1px solid var(--btolabs-chat-border);
  flex-shrink: 0;
}

.btolabs-chat-powered {
  font-size: 11px;
  color: var(--btolabs-chat-text-muted);
}

/* --------------------------------------------------------------------------
   Accesibilidad: screen-reader only
   -------------------------------------------------------------------------- */

.btolabs-chat-root .screen-reader-text,
.btolabs-chat-root .btolabs-chat-sr-only {
  clip: rect(1px, 1px, 1px, 1px);
  clip-path: inset(50%);
  height: 1px;
  width: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  word-wrap: normal;
}

/* --------------------------------------------------------------------------
   Responsive: mobile (< 480px)
   -------------------------------------------------------------------------- */

@media screen and (max-width: 480px) {
  .btolabs-chat-root {
    bottom: 16px;
    right: 16px;
    left: 16px;
    align-items: flex-end;
  }

  .btolabs-chat-root[data-position="bottom-left"] {
    align-items: flex-start;
  }

  /* Solo cuando el panel está abierto, sube el root para no tapar el menú */
  .btolabs-chat-root:has(.btolabs-chat-panel.is-open) {
    bottom: 70px;
  }

  .btolabs-chat-panel {
    width: calc(100vw - 32px);
    height: calc(100dvh - 100px);
    max-height: 560px;
  }

  /* Compensa el bottom extra para que el panel no se salga de pantalla */
  .btolabs-chat-root:has(.btolabs-chat-panel.is-open) .btolabs-chat-panel {
    height: calc(100dvh - 155px);
  }
}
