/* ai-assistant.css */
:root {
  --ai-primary: #10b981;
  --ai-primary-dark: #059669;
  --ai-bg: #1e1e2e;
  --ai-card-bg: #2d2d44;
  --ai-text: #e4e4f0;
  --ai-user-bubble: #4f46e5;
  --ai-assistant-bubble: #2d2d44;
  --ai-accent: #fbbf24;
}

/* Reset box-sizing scopé au widget uniquement (jamais au reste du site) —
   sans ça, le padding des bulles s'ajoute à leur max-width plutôt que d'être
   inclus dedans, contribuant silencieusement au débordement horizontal. */
#ai-chat-container,
#ai-chat-container *,
#ai-chat-container *::before,
#ai-chat-container *::after {
  box-sizing: border-box;
}

/* Bouton flottant I.A */
#ai-fab {
  position: fixed;
  bottom: 100px;
  right: 30px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--ai-primary), var(--ai-primary-dark));
  color: white;
  border: none;
  box-shadow: 0 8px 30px rgba(16, 185, 129, 0.4);
  font-size: 24px;
  font-weight: 700;
  cursor: pointer;
  z-index: 9999;
  transition: transform 0.3s, box-shadow 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

#ai-fab:hover {
  transform: scale(1.05);
  box-shadow: 0 12px 40px rgba(16, 185, 129, 0.5);
}

#ai-fab:active {
  transform: scale(0.95);
}

/* Conteneur du chat */
#ai-chat-container {
  position: fixed;
  /* La position (top, left) sera définie par JavaScript */
  width: 380px;
  max-height: 500px;
  background: var(--ai-bg);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  display: none;
  flex-direction: column;
  z-index: 99999;
  overflow: hidden;
  /* Seule l'opacité est animée à l'ouverture/fermeture */
  transition: opacity 0.3s ease;
  opacity: 0;
  pointer-events: none;
}

#ai-chat-container.open {
  display: flex;
  opacity: 1;
  pointer-events: all;
}

/* En-tête */
#ai-chat-header {
  padding: 16px 20px;
  background: white;
  color: green;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  cursor: move;          /* Indique que la fenêtre est déplaçable */
  user-select: none;     /* Empêche la sélection pendant le drag */
}

#ai-chat-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

#ai-chat-close {
  background: none;
  border: none;
  color: black;
  font-size: 20px;
  cursor: pointer;
  padding: 0 4px;
}

#ai-chat-close:hover {
  opacity: 0.8;
}

/* Zone de messages */
#ai-messages {
  flex: 1;
  padding: 12px 16px;
  overflow-y: auto;
  overflow-x: hidden;   /* filet de sécurité : aucun contenu ne s'échappe jamais horizontalement du widget */
  max-height: 360px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--ai-bg);
  
  scrollbar-width: none;
  -ms-overflow-style: none;
}

#ai-messages::-webkit-scrollbar {
    display: none;
}

.ai-message {
  max-width: 80%;
  min-width: 0;         /* CRITIQUE : sans ça, un flex-item column ne rétrécit
                            jamais sous la largeur intrinsèque de son contenu
                            (défaut CSS min-width:auto) — un <table> ou <pre>
                            large forçait la bulle entière à s'élargir et à
                            déborder du widget, coupée seulement par l'overflow
                            hidden du conteneur externe. C'est la cause racine
                            du débordement observé sur tableaux/blocs de code. */
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: anywhere;  /* plus robuste que break-word seul pour les
                                chaînes non-sécables (URLs, hash, tokens) */
  animation: aiMessageIn 0.3s ease;
}

.ai-message.user {
  align-self: flex-end;
  background: var(--ai-user-bubble);
  color: white;
  border-bottom-right-radius: 4px;
}

.ai-message.assistant {
  align-self: flex-start;
  background: var(--ai-assistant-bubble);
  color: var(--ai-text);
  border-bottom-left-radius: 4px;
}

/* Liens des produits dans les messages de l'IA */
.ai-message .product-link,
.ai-message a {
    color: #10b981;
    text-decoration: none;
    border-bottom: 1px solid rgba(16, 185, 129, 0.3);
    transition: border-color 0.2s, color 0.2s;
    overflow-wrap: anywhere;   /* une URL longue ne pousse plus la bulle en largeur */
}

.ai-message .product-link:hover,
.ai-message a:hover {
    color: #059669;
    border-bottom-color: #059669;
}

.ai-message a[href^="http"] {
    color: #10b981;
    text-decoration: none;
    border-bottom: 1px solid rgba(16, 185, 129, 0.3);
}

.ai-message a[href^="http"]:hover {
    color: #059669;
    border-bottom-color: #059669;
}

/* ── Contenu Markdown généré par marked.js — jusqu'ici totalement non stylé,
   livré aux styles par défaut du navigateur (fond blanc, pas de retour à la
   ligne pour <pre>, aucune gestion d'overflow pour <table>) : c'est la cause
   directe des débordements sur tableaux et blocs de code. ────────────────── */

.ai-message p {
  margin: 0 0 8px;
}
.ai-message p:last-child {
  margin-bottom: 0;
}

.ai-message ul,
.ai-message ol {
  margin: 8px 0;
  padding-left: 22px;
}
.ai-message li {
  margin-bottom: 4px;
  overflow-wrap: anywhere;
}

.ai-message blockquote {
  border-left: 3px solid var(--ai-primary);
  margin: 8px 0;
  padding: 2px 12px;
  color: #b8b8c8;
  font-style: italic;
}

.ai-message h1, .ai-message h2, .ai-message h3,
.ai-message h4, .ai-message h5, .ai-message h6 {
  margin: 14px 0 6px;
  line-height: 1.3;
  font-weight: 700;
  color: #fff;
}
.ai-message h1:first-child, .ai-message h2:first-child, .ai-message h3:first-child,
.ai-message h4:first-child, .ai-message h5:first-child, .ai-message h6:first-child {
  margin-top: 0;
}
.ai-message h1 { font-size: 1.25em; }
.ai-message h2 { font-size: 1.15em; }
.ai-message h3 { font-size: 1.05em; }

.ai-message hr {
  border: none;
  border-top: 1px solid #3d3d5c;
  margin: 12px 0;
}

.ai-message img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  margin: 8px 0;
  display: block;
}

/* Code inline : `comme ceci` */
.ai-message code {
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 0.88em;
  color: #fbbf24;
  overflow-wrap: anywhere;
}

/* Blocs de code ```...``` — LA cause principale du débordement signalé.
   <pre> a par défaut white-space:pre (aucun retour à la ligne) et aucune
   gestion d'overflow : tout contenu plus large que le widget s'échappait
   visuellement à droite. Solution : scroll horizontal contenu DANS la bulle,
   jamais de débordement visuel, avec scrollbar fine cohérente avec le thème. */
.ai-message pre {
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: #16161f;
  border: 1px solid #3d3d5c;
  border-radius: 8px;
  padding: 12px 14px;
  margin: 8px 0;
  position: relative;
}
.ai-message pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  color: #d4d4e0;
  font-size: 12.5px;
  line-height: 1.55;
  white-space: pre;      /* préserve l'indentation du code — le scroll gère la largeur */
  display: block;
  overflow-wrap: normal;  /* ne pas casser le code au milieu d'un mot/token */
}
.ai-message pre::-webkit-scrollbar {
  height: 6px;
}
.ai-message pre::-webkit-scrollbar-thumb {
  background: #4d4d6a;
  border-radius: 3px;
}
.ai-message pre {
  scrollbar-width: thin;
  scrollbar-color: #4d4d6a transparent;
}

/* Bouton copier injecté par JS sur chaque bloc de code */
.ai-copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(255, 255, 255, 0.1);
  color: #d4d4e0;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 6px;
  font-size: 11px;
  padding: 3px 8px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s;
}
.ai-message pre:hover .ai-copy-btn {
  opacity: 1;
}
.ai-copy-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

/* Tableaux Markdown — deuxième cause principale du débordement signalé.
   Technique standard "overflow table" : display:block sur <table> permet
   d'appliquer overflow-x SANS casser l'alignement interne des colonnes
   (<tbody>/<tr>/<td> gardent leur comportement table par défaut). */
.ai-message table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
}
.ai-message th,
.ai-message td {
  padding: 6px 12px;
  border: 1px solid #3d3d5c;
  text-align: left;
  white-space: nowrap;   /* la cellule ne se casse pas — c'est le tableau entier qui scrolle */
}
.ai-message th {
  background: rgba(255, 255, 255, 0.06);
  font-weight: 700;
  color: #fff;
}
.ai-message tr:nth-child(even) td {
  background: rgba(255, 255, 255, 0.02);
}
.ai-message table::-webkit-scrollbar {
  height: 6px;
}
.ai-message table::-webkit-scrollbar-thumb {
  background: #4d4d6a;
  border-radius: 3px;
}
.ai-message table {
  scrollbar-width: thin;
  scrollbar-color: #4d4d6a transparent;
}

@keyframes aiMessageIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Typing indicator */
.ai-typing {
  align-self: flex-start;
  background: var(--ai-assistant-bubble);
  padding: 10px 14px;
  border-radius: 16px;
  display: flex;
  gap: 4px;
  align-items: center;
}

.ai-typing span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #888;
  display: inline-block;
  animation: aiTyping 1.2s infinite;
}

.ai-typing span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes aiTyping {
  0%, 60%, 100% { transform: translateY(0); background: #888; }
  30% { transform: translateY(-6px); background: #ccc; }
}

/* Zone de saisie */
#ai-input-area {
  padding: 12px 16px;
  border-top: 1px solid #3d3d5c;
  display: flex;
  gap: 8px;
  background: var(--ai-bg);
  border-bottom-left-radius: 20px;
  border-bottom-right-radius: 20px;
}

#ai-input {
  flex: 1;
  resize: none;
  overflow-y: auto;      
  padding: 10px 14px;
  border: 1px solid #3d3d5c;
  border-radius: 24px;
  background: #2d2d44;
  color: var(--ai-text);
  font-size: 14px;
  outline: none;
  transition: border 0.2s;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

#ai-input:focus {
  border-color: var(--ai-primary);
}

#ai-input::-webkit-scrollbar {
    display: none;
}

#ai-send {
  background: var(--ai-primary);
  border: none;
  color: white;
  padding: 0 16px;
  border-radius: 24px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

#ai-send:hover {
  background: var(--ai-primary-dark);
}

#ai-upload-btn {
  background: #3d3d5c;
  border: none;
  color: white;
  padding: 0 12px;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  transition: background 0.2s;
}

#ai-upload-btn:hover {
  background: #55557a;
}

#ai-file-input {
  display: none;
}

/* Confettis (optionnel) */
.ai-confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 99999;
  overflow: hidden;
}

.ai-confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  animation: aiConfettiFall 3s linear forwards;
}

@keyframes aiConfettiFall {
  0% { transform: translateY(-100px) rotate(0deg); opacity: 1; }
  100% { transform: translateY(110vh) rotate(720deg); opacity: 0; }
}

/* Responsive */
@media (max-width: 600px) {
  #ai-chat-container {
    width: 90%;
    max-height: 70vh;
    /* La position est toujours gérée par JS */
  }
  #ai-fab {
    bottom: 80px;
    right: 20px;
    width: 50px;
    height: 50px;
    font-size: 20px;
  }
  .ai-message {
    max-width: 90%;
  }
  .ai-message pre {
    padding: 10px 12px;
  }
  .ai-message pre code {
    font-size: 11.5px;
  }
  .ai-message table {
    font-size: 12px;
  }
}