/* BEM Lead AI — widget de chat. Design moderne, scopé sous .bem-* et piloté
   par des variables CSS (toutes surchargées en ligne selon les réglages). */

.bem-widget {
  --bem-primary: #0b3d91;
  --bem-primary-dark: #082a66;
  --bem-accent: #e6b800;
  --bem-user: #0b3d91;
  --bem-radius: 20px;
  --bem-conv-bg: #f6f7fb;
  --bem-bot: #ffffff;
  --bem-text: #1f2430;
  --bem-muted: #8a93a6;
  --bem-input-bg: #ffffff;
  --bem-input-border: #e2e6ef;
  --bem-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  position: fixed;
  bottom: 22px;
  right: 22px;
  /* z-index très élevé : passe AU-DESSUS des barres de navigation fixes de
     certains thèmes mobiles qui masquaient la zone de saisie du chat. */
  z-index: 2147483000;
  font-family: var(--bem-font);
}

/* ---- Accroche animée (teaser) ------------------------------------- */
.bem-teaser {
  position: absolute;
  bottom: 76px;
  width: 220px;
  box-sizing: border-box;
  display: flex;
  align-items: flex-start;
  gap: 6px;
  background: var(--bem-bot, #fff);
  color: var(--bem-text, #1f2430);
  border: 1px solid rgba(120, 130, 150, 0.16);
  border-radius: 16px;
  padding: 11px 12px 11px 14px;
  box-shadow: 0 12px 30px rgba(15, 23, 42, 0.18);
  font-size: 13.5px;
  line-height: 1.4;
  opacity: 0;
  transform: translateY(8px) scale(0.94);
  transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.34, 1.4, 0.5, 1);
  pointer-events: none;
}
.bem-pos-right .bem-teaser { right: 6px; transform-origin: bottom right; }
.bem-pos-left .bem-teaser { left: 6px; transform-origin: bottom left; }
.bem-teaser.bem-teaser-in { opacity: 1; transform: none; pointer-events: auto; animation: bem-teaser-bob 3.2s ease-in-out 0.4s infinite; }
.bem-teaser-text { cursor: pointer; }
.bem-teaser::after {
  content: "";
  position: absolute;
  bottom: -6px;
  width: 14px;
  height: 14px;
  background: inherit;
  border-right: inherit;
  border-bottom: inherit;
  transform: rotate(45deg);
}
.bem-pos-right .bem-teaser::after { right: 22px; }
.bem-pos-left .bem-teaser::after { left: 22px; }
.bem-teaser-close {
  flex: 0 0 auto;
  background: transparent;
  border: none;
  color: var(--bem-muted, #8a93a6);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  padding: 0 2px;
}
.bem-teaser-close:hover { color: var(--bem-text, #1f2430); }
@keyframes bem-teaser-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

/* ---- Lanceur ------------------------------------------------------- */
.bem-launcher {
  position: relative;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--bem-primary), var(--bem-primary-dark));
  color: #fff;
  font-size: 27px;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 10px 28px rgba(11, 61, 145, 0.4);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}
.bem-launcher::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid var(--bem-primary);
  opacity: 0.5;
  animation: bem-pulse 2.4s ease-out infinite;
}
.bem-launcher:hover { transform: translateY(-2px) scale(1.05); box-shadow: 0 14px 34px rgba(11, 61, 145, 0.48); }
.bem-launcher.bem-open::after { display: none; }
.bem-launcher .bem-launcher-close { display: none; font-size: 26px; }
.bem-launcher.bem-open .bem-launcher-icon { display: none; }
.bem-launcher.bem-open .bem-launcher-close { display: block; }
/* Lanceur avec image (logo) au lieu d'un emoji */
.bem-launcher-icon img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; display: block; }
.bem-launcher.bem-has-image { padding: 0; overflow: hidden; }
.bem-launcher.bem-has-image .bem-launcher-icon { width: 100%; height: 100%; }
@keyframes bem-pulse {
  0% { transform: scale(1); opacity: 0.5; }
  70% { transform: scale(1.5); opacity: 0; }
  100% { opacity: 0; }
}

/* ---- Panneau ------------------------------------------------------- */
.bem-panel {
  position: absolute;
  bottom: 78px;
  right: 0;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: 580px;
  max-height: calc(100vh - 130px);
  background: var(--bem-bot);
  border-radius: var(--bem-radius);
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.28), 0 2px 8px rgba(15, 23, 42, 0.12);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform-origin: bottom right;
  animation: bem-in 0.26s cubic-bezier(0.34, 1.4, 0.5, 1);
}
/* Le sélecteur de classe ci-dessus l'emporte sur la règle navigateur
   [hidden]{display:none}. Sans ce correctif, le bouton « fermer » masque
   l'attribut mais le panneau reste affiché. */
.bem-panel[hidden] { display: none !important; }
@keyframes bem-in {
  from { opacity: 0; transform: translateY(16px) scale(0.96); }
  to { opacity: 1; transform: none; }
}

/* ---- En-tête ------------------------------------------------------- */
.bem-header {
  position: relative;
  flex: 0 0 auto;
  background: linear-gradient(135deg, var(--bem-primary), var(--bem-primary-dark));
  color: #fff;
  padding: 16px 18px;
  display: flex;
  gap: 12px;
  align-items: center;
}
.bem-header-avatar {
  position: relative;
  width: 42px;
  height: 42px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.16);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  overflow: hidden;
}
.bem-header-avatar img { width: 100%; height: 100%; object-fit: cover; }
.bem-header-avatar::after {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #34d399;
  border: 2px solid var(--bem-primary-dark);
}
.bem-header-text { flex: 1; min-width: 0; }
.bem-header-title { font-weight: 700; font-size: 15px; line-height: 1.2; }
.bem-header-subtitle { font-size: 12px; opacity: 0.82; margin-top: 2px; }
.bem-close {
  background: rgba(255, 255, 255, 0.12);
  border: none;
  color: #fff;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease;
  flex: 0 0 auto;
}
.bem-close:hover { background: rgba(255, 255, 255, 0.26); }

/* ---- Messages ------------------------------------------------------ */
.bem-messages {
  flex: 1 1 auto;
  min-height: 0; /* indispensable : sans ça, l'enfant flex ne défile pas et pousse l'en-tête hors écran */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--bem-conv-bg);
  scroll-behavior: smooth;
}
.bem-messages::-webkit-scrollbar { width: 6px; }
.bem-messages::-webkit-scrollbar-thumb { background: rgba(120, 130, 150, 0.35); border-radius: 3px; }

.bem-row { display: flex; gap: 8px; align-items: flex-end; max-width: 100%; animation: bem-pop 0.28s ease; }
.bem-row-user { flex-direction: row-reverse; }
@keyframes bem-pop {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
.bem-row-avatar {
  width: 28px;
  height: 28px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--bem-primary), var(--bem-primary-dark));
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  overflow: hidden;
}
.bem-row-avatar img { width: 100%; height: 100%; object-fit: cover; }

.bem-msg {
  max-width: 78%;
  padding: 11px 15px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.bem-msg-plain { white-space: pre-wrap; }
/* Rendu Markdown léger */
.bem-msg p { margin: 0 0 8px; }
.bem-msg p:last-child { margin-bottom: 0; }
.bem-msg strong { font-weight: 700; }
.bem-msg em { font-style: italic; }
.bem-msg code { background: rgba(120,130,150,0.15); padding: 1px 5px; border-radius: 5px; font-size: 0.92em; }
.bem-msg ul, .bem-msg ol { margin: 6px 0; padding-left: 20px; }
.bem-msg li { margin: 3px 0; }
.bem-msg a { color: inherit; text-decoration: underline; }
.bem-row-user .bem-msg a { color: #fff; }
.bem-row-user .bem-msg {
  background: var(--bem-user);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.bem-row-bot .bem-msg {
  background: var(--bem-bot);
  color: var(--bem-text);
  border: 1px solid rgba(120, 130, 150, 0.12);
  border-bottom-left-radius: 5px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
}

/* Indicateur de saisie */
.bem-typing { display: flex; gap: 4px; align-items: center; padding: 13px 16px; }
.bem-typing span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--bem-muted);
  animation: bem-blink 1.2s infinite both;
}
.bem-typing span:nth-child(2) { animation-delay: 0.2s; }
.bem-typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bem-blink { 0%, 80%, 100% { opacity: 0.3; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }

.bem-handoff-note {
  flex: 0 0 auto;
  padding: 9px 16px;
  background: #fff8e1;
  color: #7a5c00;
  font-size: 13px;
  text-align: center;
  border-top: 1px solid #f0e4b8;
}

/* ---- Bouton WhatsApp ---------------------------------------------- */
.bem-wa {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: calc(100% - 28px);
  margin: 12px 14px 0;
  padding: 11px 14px;
  border: none;
  border-radius: 26px;
  background: linear-gradient(135deg, #25d366, #1da851);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(37, 211, 102, 0.32);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.bem-wa:hover { transform: translateY(-1px); box-shadow: 0 9px 20px rgba(37, 211, 102, 0.4); }
.bem-wa:disabled { opacity: 0.6; cursor: default; transform: none; }
.bem-wa-icon { font-size: 17px; }

/* ---- Choix des numéros WhatsApp (plusieurs contacts) --------------- */
.bem-wa-sheet { position: absolute; inset: 0; z-index: 5; display: flex; align-items: flex-end; }
.bem-wa-sheet-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.45); animation: bem-fade 0.2s ease; }
.bem-wa-sheet-card {
  position: relative;
  width: 100%;
  background: var(--bem-bot, #fff);
  color: var(--bem-text, #1f2430);
  border-radius: 18px 18px var(--bem-radius, 20px) var(--bem-radius, 20px);
  padding: 16px 16px 18px;
  box-shadow: 0 -8px 30px rgba(15, 23, 42, 0.25);
  animation: bem-sheet-up 0.28s cubic-bezier(0.34, 1.4, 0.5, 1);
}
.bem-wa-sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; font-size: 14.5px; }
.bem-wa-sheet-close { background: transparent; border: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--bem-muted, #8a93a6); }
.bem-wa-choices { display: flex; flex-direction: column; gap: 8px; max-height: 60vh; overflow-y: auto; }
.bem-wa-choice {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid rgba(120, 130, 150, 0.18);
  background: var(--bem-conv-bg, #f6f7fb);
  text-decoration: none;
  color: inherit;
  transition: transform 0.12s ease, border-color 0.12s ease;
}
.bem-wa-choice:hover { transform: translateY(-1px); border-color: #25d366; }
.bem-wa-choice-icon {
  flex: 0 0 auto; width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #25d366, #1da851); color: #fff; font-size: 18px;
}
.bem-wa-choice-text { display: flex; flex-direction: column; line-height: 1.3; }
.bem-wa-choice-text strong { font-size: 14px; }
.bem-wa-choice-text em { font-style: normal; font-size: 12.5px; color: var(--bem-muted, #8a93a6); }
@keyframes bem-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes bem-sheet-up { from { transform: translateY(30px); opacity: 0.4; } to { transform: none; opacity: 1; } }

/* ---- Saisie -------------------------------------------------------- */
.bem-input {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 12px 14px;
  /* Respecte l'encoche/la barre système (iOS) pour ne jamais masquer la saisie. */
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  background: var(--bem-bot);
  border-top: 1px solid var(--bem-input-border);
}
.bem-input input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--bem-input-border);
  background: var(--bem-input-bg);
  color: var(--bem-text);
  border-radius: 24px;
  padding: 11px 16px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.bem-input input::placeholder { color: var(--bem-muted); }
.bem-input input:focus {
  border-color: var(--bem-primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--bem-primary) 18%, transparent);
}
.bem-input button {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--bem-primary), var(--bem-primary-dark));
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: transform 0.15s ease, filter 0.15s ease;
}
.bem-input button:hover { transform: scale(1.06); filter: brightness(1.06); }
.bem-input button:active { transform: scale(0.96); }

/* ---- Bandeau de consentement -------------------------------------- */
.bem-consent {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 99999;
  background: #10233f;
  color: #eef2f9;
  padding: 14px 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: center;
  font-size: 13.5px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.bem-consent a { color: var(--bem-accent, #e6b800); text-decoration: underline; }
.bem-consent-actions { display: flex; gap: 8px; }
.bem-btn { border: none; border-radius: 22px; padding: 9px 20px; font-size: 13px; cursor: pointer; font-weight: 600; }
.bem-btn-primary { background: #0b3d91; color: #fff; }
.bem-btn-primary:hover { background: #082a66; }
.bem-btn-ghost { background: transparent; color: #cdd6e6; border: 1px solid #3a4d6d; }
.bem-btn-ghost:hover { background: rgba(255, 255, 255, 0.08); }

/* ---- Mobile -------------------------------------------------------- */
@media (max-width: 480px) {
  .bem-widget { bottom: 14px; right: 14px; }
  /* Plein écran, au tout premier plan : couvre l'en-tête, le pied de page et
     la barre de navigation fixes du thème (qui masquaient la saisie). */
  .bem-widget.bem-panel-open {
    position: fixed !important;
    inset: 0 !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    z-index: 2147483000 !important;
  }
  .bem-panel {
    position: fixed !important;
    inset: 0 !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    max-height: none !important;
    border-radius: 0 !important;
    z-index: 2147483000 !important;
  }
  /* En plein écran mobile, le lanceur flottant chevauche le panneau : on le masque quand le chat est ouvert. */
  .bem-widget.bem-panel-open .bem-launcher { display: none; }
  .bem-widget.bem-panel-open .bem-teaser { display: none; }
  .bem-teaser { max-width: calc(100vw - 40px); }
}

@media (prefers-reduced-motion: reduce) {
  .bem-launcher::after, .bem-panel, .bem-row, .bem-typing span { animation: none; }
}
