/*
 | mailbox_ai — WIDGET CHATBOT GLOBAL (bouton flottant sur toute page admin)
 | -------------------------------------------------------------------------
 | ⚠️ ZERO REGRESSION : ce fichier est charge sur TOUTES les pages admin du CRM.
 | Regles STRICTES :
 |   - TOUTES les classes sont prefixees .mbxw-  (aucun selecteur generique :
 |     pas de *, body, a, p, ul... -> impossible de "deborder" sur Perfex).
 |   - toutes les proprietes sont scopees sous #mbxw-root / .mbxw-*.
 |   - box-sizing remis a border-box UNIQUEMENT sous #mbxw-root (jamais global).
 |   - z-index eleve mais SOUS les modales Bootstrap de Perfex (backdrop 1040 /
 |     modal 1050) -> une modale critique passe toujours DEVANT le widget.
 |   - le widget est INERTE tant qu'il n'est pas ouvert (seul le bouton s'affiche ;
 |     le panneau est display:none -> n'intercepte aucun clic de la page).
 */

#mbxw-root,
#mbxw-root * {
  box-sizing: border-box;
}

#mbxw-root {
  /* Variables locales : aucune fuite hors du widget. */
  --mbxw-accent: #2f8de4;
  --mbxw-accent-d: #1f6fbf;
  --mbxw-bg: #ffffff;
  --mbxw-bg-alt: #f4f6f9;
  --mbxw-text: #37404a;
  --mbxw-muted: #7d8794;
  --mbxw-border: #e2e7ee;
  --mbxw-user: #2f8de4;
  --mbxw-z: 1035; /* < 1040 (modal-backdrop Bootstrap) : les modales passent devant */

  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: var(--mbxw-z);
  font-family: "Roboto", -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  color: var(--mbxw-text);
}

/* Barre d'actions FIXE des formulaires de transaction Perfex (factures/devis/
   avoirs/depenses/abonnements) : .btn-bottom-toolbar (position:fixed;bottom:0).
   Elle recouvrirait le caret « Enregistrer » (menu deroulant) + la pagination.
   -> quand le JS detecte une telle barre fixe, il pose .mbxw-raise sur le root
   pour REMONTER le bouton flottant au-dessus. Sur les pages SANS barre fixe,
   la classe n'est pas posee -> position habituelle inchangee. */
#mbxw-root.mbxw-raise {
  bottom: 76px;
}

/* ------------------------------------------------------------------ Bouton */
.mbxw-launch {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--mbxw-accent);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(24, 60, 100, 0.28);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
  padding: 0;
  margin: 0;
  outline: none;
}
.mbxw-launch:hover {
  background: var(--mbxw-accent-d);
  transform: translateY(-2px);
  box-shadow: 0 10px 24px rgba(24, 60, 100, 0.34);
}
.mbxw-launch:focus-visible {
  box-shadow: 0 0 0 3px rgba(47, 141, 228, 0.45);
}
.mbxw-launch svg {
  width: 26px;
  height: 26px;
  display: block;
  fill: currentColor;
}
/* Bouton masque quand le panneau est ouvert (evite le double element). */
#mbxw-root.mbxw-open .mbxw-launch {
  display: none;
}

/* ------------------------------------------------------------------ Panneau */
.mbxw-panel {
  display: none;
  flex-direction: column;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: 560px;
  max-height: calc(100vh - 48px);
  background: var(--mbxw-bg);
  border: 1px solid var(--mbxw-border);
  border-radius: 14px;
  box-shadow: 0 18px 50px rgba(20, 45, 80, 0.28);
  overflow: hidden;
}
#mbxw-root.mbxw-open .mbxw-panel {
  display: flex;
}

/* En-tete */
.mbxw-head {
  flex: 0 0 auto;
  background: var(--mbxw-accent);
  color: #fff;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.mbxw-head-ic {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}
.mbxw-head-ic svg {
  width: 18px;
  height: 18px;
  fill: #fff;
  display: block;
}
.mbxw-head-txt {
  flex: 1 1 auto;
  min-width: 0;
}
.mbxw-head-title {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mbxw-head-sub {
  font-size: 11px;
  opacity: 0.85;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mbxw-close {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  color: #fff;
  cursor: pointer;
  border-radius: 8px;
  font-size: 20px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.12s ease;
}
.mbxw-close:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Zone messages */
.mbxw-msgs {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px;
  background: var(--mbxw-bg-alt);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.mbxw-msg {
  max-width: 86%;
  padding: 9px 12px;
  border-radius: 12px;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: normal;
}
.mbxw-msg.mbxw-bot {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--mbxw-border);
  border-bottom-left-radius: 4px;
}
.mbxw-msg.mbxw-user {
  align-self: flex-end;
  background: var(--mbxw-user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.mbxw-msg p {
  margin: 0 0 6px 0;
}
.mbxw-msg p:last-child {
  margin-bottom: 0;
}
.mbxw-msg ul {
  margin: 4px 0;
  padding-left: 18px;
}
.mbxw-msg li {
  margin: 2px 0;
}
.mbxw-msg code {
  background: rgba(120, 130, 145, 0.16);
  border-radius: 4px;
  padding: 1px 4px;
  font-size: 12.5px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
.mbxw-msg.mbxw-user code {
  background: rgba(255, 255, 255, 0.22);
}

/* Citations */
.mbxw-cite {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--mbxw-border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.mbxw-cite-lbl {
  font-size: 11px;
  font-weight: 600;
  color: var(--mbxw-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.mbxw-cite a,
.mbxw-cite span {
  font-size: 12.5px;
  color: var(--mbxw-accent-d);
  text-decoration: none;
  padding: 3px 6px;
  border-radius: 6px;
  background: var(--mbxw-bg-alt);
  display: inline-block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.mbxw-cite a:hover {
  background: #e7f1fb;
  text-decoration: underline;
}
.mbxw-cite span {
  color: var(--mbxw-muted);
}

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

/* Suggestions (chips) */
.mbxw-sugg {
  flex: 0 0 auto;
  padding: 8px 12px 0 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  background: var(--mbxw-bg-alt);
}
.mbxw-chip {
  border: 1px solid var(--mbxw-border);
  background: #fff;
  color: var(--mbxw-text);
  border-radius: 16px;
  padding: 5px 11px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
  max-width: 100%;
  text-align: left;
  line-height: 1.3;
}
.mbxw-chip:hover {
  background: #e7f1fb;
  border-color: var(--mbxw-accent);
}

/* Barre de saisie */
.mbxw-input {
  flex: 0 0 auto;
  border-top: 1px solid var(--mbxw-border);
  background: #fff;
  padding: 10px;
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
.mbxw-ta {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--mbxw-border);
  border-radius: 10px;
  padding: 9px 11px;
  font-family: inherit;
  font-size: 13.5px;
  line-height: 1.4;
  max-height: 120px;
  min-height: 40px;
  color: var(--mbxw-text);
  background: #fff;
  outline: none;
}
.mbxw-ta:focus {
  border-color: var(--mbxw-accent);
  box-shadow: 0 0 0 2px rgba(47, 141, 228, 0.16);
}
.mbxw-send {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: var(--mbxw-accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.12s ease;
}
.mbxw-send:hover:not(:disabled) {
  background: var(--mbxw-accent-d);
}
.mbxw-send:disabled {
  opacity: 0.5;
  cursor: default;
}
.mbxw-send svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
  display: block;
}

/* --------------------------------------------------- Responsive (mobile) */
@media (max-width: 900px) {
  #mbxw-root {
    right: 16px;
    bottom: 16px;
  }
  #mbxw-root.mbxw-open {
    right: 0;
    bottom: 0;
    left: 0;
    top: 0;
  }
  #mbxw-root.mbxw-open .mbxw-panel {
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    border: none;
  }
}

/* Confort clavier tres petits ecrans : la textarea ne mange pas tout l'ecran. */
@media (max-height: 480px) {
  .mbxw-panel { height: calc(100vh - 12px); }
}
