/* ==========================================================================
   CHATWHATS - MÁSCARA EMBED A PANTALLA COMPLETA PARA KOMMO CRM
   Encabezado Superior + Chat Nativo Kommo 100% Pantalla + Pie Legal Meta Ads
   ========================================================================== */

:root {
  --color-primary: #2563EB;
  --color-primary-hover: #1D4ED8;
  --surface-header: #FFFFFF;
  --surface-border: #E2E8F0;
  --text-primary: #0F172A;
  --text-secondary: #475569;
  --text-muted: #94A3B8;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --header-height: 56px;
}

/* 1. RESET Y PANTALLA COMPLETA DINÁMICA (100dvh) */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: -webkit-fill-available;
  font-family: var(--font-family);
  background-color: #FFFFFF;
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
  overflow-x: hidden;
}

/* Evitar bloqueos de scroll/pull-to-refresh inducidos por scripts de terceros */
html[class*="amo-livechat"],
body[class*="amo-livechat"] {
  overflow: visible !important;
  overflow-x: hidden !important;
}

/* Contenedor principal: columna flex que llena exactamente la pantalla visible */
.chat-app-wrapper {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: -webkit-fill-available;
  position: relative;
  overflow: hidden;
  background-color: #FFFFFF;
}

/* ==========================================================================
   2. ENCABEZADO SUPERIOR INSTITUCIONAL (FLEX HIJO 1: NO SE SOBREPONE)
   ========================================================================== */
.chat-header {
  flex: 0 0 auto;
  width: 100%;
  height: var(--header-height);
  background-color: var(--surface-header);
  padding: 6px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--surface-border);
  z-index: 50;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.brand-support-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.avatar-support-wrap {
  position: relative;
  flex-shrink: 0;
}

.avatar-support-img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background-color: #FFFFFF;
  border: 1.5px solid var(--surface-border);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.08);
  display: block;
}

.online-indicator-dot {
  position: absolute;
  bottom: 0px;
  right: 0px;
  width: 10px;
  height: 10px;
  background-color: #10B981;
  border: 2px solid #FFFFFF;
  border-radius: 50%;
}

.brand-text-col {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-title-row {
  display: flex;
  align-items: center;
  gap: 5px;
}

.brand-name {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.verified-icon {
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
}

.brand-status {
  font-size: 0.72rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-right-badges {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.safe-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background-color: #ECFDF5;
  color: #065F46;
  border: 1px solid #A7F3D0;
  padding: 3px 8px;
  border-radius: 9999px;
  font-size: 0.72rem;
  font-weight: 600;
}

.header-reload-btn {
  background: #F1F5F9;
  border: 1px solid #E2E8F0;
  border-radius: 50%;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #475569;
  transition: all 0.2s ease;
  padding: 0;
}

.header-reload-btn:hover {
  background: #E2E8F0;
  color: #0F172A;
  transform: rotate(45deg);
}

.header-reload-btn:active {
  transform: rotate(180deg);
}

/* ==========================================================================
   3. ÁREA CENTRAL: ESCENARIO PARA EL CHAT EMBED DE KOMMO (FLEX HIJO 2)
   Toma el 100% del espacio disponible entre cabecera y pie
   ========================================================================== */
.kommo-embed-stage {
  flex: 1 1 auto;
  min-height: 0; /* CRUCIAL para que no sobrepase el viewport en flex column */
  width: 100%;
  position: relative;
  background-color: #FFFFFF;
  overflow: hidden;
  margin: 0 !important;
  padding: 0 !important;
}

/* Forzar que el iframe ocupe el 100% exacto de este espacio central */
#kommoEmbedStage iframe,
#kommoEmbedStage #amo-livechat,
#kommoEmbedStage .amo-livechat_frame,
.amo-livechat {
  position: absolute !important;
  top: 0 !important;
  left: 0 !important;
  width: 100% !important;
  height: 100% !important;
  min-height: 100% !important;
  max-height: 100% !important;
  border: none !important;
  border-radius: 0 !important;
  box-shadow: none !important;
  visibility: visible !important;
  opacity: 1 !important;
  display: block !important;
}

/* Ocultar botones flotantes redundantes o cabeceras sueltas */
.crm-button-widget,
.crm-button-widget__button,
.amo-button-holder,
.amo-button,
.amo-button-close {
  display: none !important;
  visibility: hidden !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

/* Pantalla de carga suave mientras el iframe se monta */
.kommo-loader-wrap {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background-color: #FFFFFF;
  z-index: 5;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.kommo-loader-wrap.loaded {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.kommo-loader-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: 2px solid #2563EB;
  animation: pulse-avatar 1.8s infinite ease-in-out;
}

@keyframes pulse-avatar {
  0% { transform: scale(0.96); box-shadow: 0 0 0 0 rgba(37, 99, 235, 0.4); }
  70% { transform: scale(1.04); box-shadow: 0 0 0 14px rgba(37, 99, 235, 0); }
  100% { transform: scale(0.96); box-shadow: 0 0 0 0 rgba(37, 99, 235, 0); }
}

.kommo-spinner {
  width: 28px;
  height: 28px;
  border: 3px solid #E2E8F0;
  border-top-color: var(--color-primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.kommo-loader-text {
  font-size: 0.85rem;
  color: var(--text-secondary);
  font-weight: 500;
  text-align: center;
  padding: 0 20px;
}

/* ==========================================================================
   GUÍA RÁPIDA DE INICIO (BOTONES CHIPS FLOTANTES TIPO WHATSAPP / META ADS)
   Permite al usuario iniciar la conversación con un solo toque
   ========================================================================== */
.chat-starter-guide {
  position: absolute !important;
  bottom: 76px !important; /* Justo arriba de la barra de escribir de Kommo */
  left: 50% !important;
  transform: translateX(-50%) !important;
  width: calc(100% - 24px) !important;
  max-width: 440px !important;
  background: rgba(255, 255, 255, 0.98) !important;
  backdrop-filter: blur(12px) !important;
  -webkit-backdrop-filter: blur(12px) !important;
  border: 1.5px solid #BFDBFE !important;
  box-shadow: 0 12px 30px -5px rgba(0, 0, 0, 0.18), 0 4px 12px rgba(37, 99, 235, 0.12) !important;
  border-radius: 18px !important;
  padding: 10px 14px !important;
  z-index: 2147483645 !important; /* SUPERA EL Z-INDEX DE 2147483000 DE KOMMO */
  display: flex !important;
  flex-direction: column !important;
  gap: 8px !important;
  transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s ease !important;
  animation: slide-up-starter 0.35s ease-out !important;
}

.chat-starter-guide.hidden {
  opacity: 0 !important;
  transform: translate(-50%, 15px) !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

@keyframes slide-up-starter {
  from { opacity: 0; transform: translate(-50%, 20px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

.starter-guide-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 0.74rem;
  font-weight: 600;
  color: #475569;
}

.starter-guide-header-left {
  display: flex;
  align-items: center;
  gap: 6px;
}

.starter-guide-dismiss {
  background: none;
  border: none;
  color: #94A3B8;
  font-size: 1.15rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
  transition: color 0.15s ease;
}

.starter-guide-dismiss:hover {
  color: #0F172A;
}

.starter-chips-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.starter-chip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: linear-gradient(135deg, #EFF6FF 0%, #DBEAFE 100%);
  border: 1.5px solid #93C5FD;
  color: #1D4ED8;
  border-radius: 12px;
  padding: 9px 14px;
  font-size: 0.86rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  box-shadow: 0 2px 4px rgba(37, 99, 235, 0.08);
  text-align: left;
}

.starter-chip-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.starter-chip-send-icon {
  display: flex;
  align-items: center;
  color: #2563EB;
  transition: transform 0.2s ease;
}

.starter-chip:hover {
  background: linear-gradient(135deg, #2563EB 0%, #1D4ED8 100%);
  color: #FFFFFF;
  border-color: #1D4ED8;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(37, 99, 235, 0.25);
}

.starter-chip:hover .starter-chip-send-icon {
  color: #FFFFFF;
  transform: translateX(3px);
}

.starter-chip:active {
  transform: scale(0.98);
}

.starter-chip.copied {
  background: #ECFDF5 !important;
  border-color: #6EE7B7 !important;
  color: #065F46 !important;
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.2) !important;
}

.starter-chip-secondary {
  background: #F8FAFC;
  border: 1px solid #E2E8F0;
  color: #334155;
  font-weight: 500;
}

.starter-chip-secondary:hover {
  background: #F1F5F9;
  color: #0F172A;
  border-color: #CBD5E1;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.06);
}

.starter-chip-secondary .starter-chip-send-icon {
  color: #64748B;
}

.starter-chip-secondary:hover .starter-chip-send-icon {
  color: #0F172A;
}

/* ==========================================================================
   4. PIE DE PÁGINA LEGAL OBLIGATORIO (FLEX HIJO 3: SIEMPRE DEBAJO DEL CHAT)
   NUNCA tapa la barra de escribir porque se ubica naturalmente al final
   ========================================================================== */
.legal-footer {
  flex: 0 0 auto;
  width: 100%;
  background-color: #0F172A;
  color: #94A3B8;
  padding: 4px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-top: 1px solid #1E293B;
  z-index: 50;
  text-align: center;
}

.legal-links {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.68rem;
  line-height: 1.2;
}

.legal-links a {
  color: #CBD5E1;
  text-decoration: none;
  font-weight: 500;
  padding: 2px 4px;
}

.legal-links a:hover,
.legal-links a:active {
  color: #60A5FA;
  text-decoration: underline;
}

.legal-sep {
  color: #475569;
  font-size: 0.6rem;
}

.legal-disclaimer {
  font-size: 0.58rem;
  line-height: 1.15;
  color: #64748B;
  max-width: 900px;
}

/* ==========================================================================
   5. MODAL LEGAL (POLÍTICAS Y TÉRMINOS)
   ========================================================================== */
.legal-modal-backdrop {
  position: fixed !important;
  top: 0 !important;
  left: 0 !important;
  width: 100vw !important;
  height: 100vh !important;
  background-color: rgba(15, 23, 42, 0.8) !important;
  backdrop-filter: blur(8px) !important;
  -webkit-backdrop-filter: blur(8px) !important;
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2147483647 !important;
  padding: 16px !important;
}

.legal-modal-backdrop.active {
  display: flex !important;
}

body.legal-modal-is-open iframe,
body.legal-modal-is-open [class*="crm-"],
body.legal-modal-is-open [id*="crm_"],
body.legal-modal-is-open [class*="amo-"],
body.legal-modal-is-open [id*="amo-"] {
  visibility: hidden !important;
  opacity: 0 !important;
  pointer-events: none !important;
}

.legal-modal-card {
  position: relative !important;
  z-index: 2147483647 !important;
  background-color: #FFFFFF !important;
  border-radius: 16px !important;
  max-width: 520px !important;
  width: 100% !important;
  max-height: 85vh !important;
  overflow-y: auto !important;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5) !important;
  border: 1px solid #E2E8F0 !important;
  animation: modal-appear 0.2s ease-out;
}

@keyframes modal-appear {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--surface-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  font-size: 1.1rem !important;
  font-weight: 700 !important;
  color: var(--text-primary) !important;
}

.modal-close-btn {
  background: none;
  border: none;
  font-size: 1.6rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 4px;
}

.modal-close-btn:hover {
  color: var(--text-primary);
}

.modal-body {
  padding: 18px;
  font-size: 0.88rem !important;
  color: #334155 !important;
  line-height: 1.55;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ==========================================================================
   6. RESPONSIVE
   ========================================================================== */
@media (max-width: 640px) {
  :root {
    --header-height: 52px;
  }

  .chat-header {
    padding: 6px 10px;
  }

  .safe-badge {
    display: none;
  }

  .brand-name {
    font-size: 0.88rem;
  }

  .brand-status {
    font-size: 0.68rem;
  }

  .avatar-support-img {
    width: 36px;
    height: 36px;
  }

  .legal-footer {
    padding: 3px 8px;
  }

  .legal-links {
    font-size: 0.64rem;
  }

  .legal-disclaimer {
    font-size: 0.52rem;
    line-height: 1.1;
  }

  .chat-starter-guide {
    bottom: 70px !important;
    width: calc(100% - 16px) !important;
    padding: 8px 12px !important;
    border-radius: 14px !important;
    gap: 6px !important;
  }

  .starter-guide-header {
    font-size: 0.68rem;
  }

  .starter-chip {
    padding: 8px 12px;
    font-size: 0.8rem;
    border-radius: 10px;
  }
}
