{% load static %}

<style>
/* ============================================
   ARTÉMIS WIDGET - NEURAL UPLINK INTERFACE
   CYBERPUNK 2077 ENHANCED EDITION
   ============================================ */

:root {
  --artemis-cyan: #00f3ff;
  --artemis-violet: #b026ff;
  --artemis-yellow: #fcee0a;
  --artemis-red: #ff003c;
  --artemis-dark: #0a0a0f;
  --artemis-darker: #050508;
  --artemis-electric: #00ffff;
}

/* FLOATING TRIGGER BUTTON */
.artemis-trigger {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 70px;
  height: 70px;
  background: linear-gradient(135deg, var(--artemis-cyan), var(--artemis-violet));
  border: 3px solid var(--artemis-cyan);
  border-radius: 50%;
  cursor: pointer;
  z-index: 9998;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 
    0 0 30px rgba(0, 243, 255, 0.6),
    0 0 60px rgba(176, 38, 255, 0.4),
    inset 0 0 20px rgba(0, 243, 255, 0.3);
  transition: all 0.3s ease;
  animation: breathe 3s ease-in-out infinite;
  overflow: visible;
}

.artemis-trigger::before {
  content: '';
  position: absolute;
  inset: -3px;
  background: conic-gradient(
    from 0deg,
    var(--artemis-cyan),
    var(--artemis-violet),
    var(--artemis-cyan)
  );
  animation: rotateBorder 4s linear infinite;
  opacity: 0.6;
  z-index: -1;
  border-radius: 50%;
}

@keyframes breathe {
  0%, 100% { 
    transform: scale(1); 
    box-shadow: 
      0 0 30px rgba(0, 243, 255, 0.6),
      0 0 60px rgba(176, 38, 255, 0.4),
      inset 0 0 20px rgba(0, 243, 255, 0.3);
  }
  50% { 
    transform: scale(1.05); 
    box-shadow: 
      0 0 40px rgba(0, 243, 255, 0.8),
      0 0 80px rgba(176, 38, 255, 0.6),
      inset 0 0 25px rgba(0, 243, 255, 0.5);
  }
}

@keyframes rotateBorder {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.artemis-trigger:hover {
  transform: scale(1.1);
  box-shadow: 
    0 0 50px rgba(0, 243, 255, 0.9),
    0 0 100px rgba(176, 38, 255, 0.7),
    inset 0 0 30px rgba(0, 243, 255, 0.5);
}

.artemis-trigger-icon {
  font-size: 32px;
  color: #ffffff;
  text-shadow: 0 0 10px var(--artemis-cyan);
  position: relative;
  z-index: 1;
  animation: iconPulse 2s ease-in-out infinite;
}

@keyframes iconPulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

/* ENHANCED TOOLTIP - CYBERPUNK 2077 STYLE */
.artemis-tooltip {
  position: absolute;
  bottom: 50%;
  right: calc(100% + 20px);
  transform: translateY(50%) translateX(10px);
  background: var(--artemis-darker);
  border: 2px solid var(--artemis-cyan);
  padding: 0.8rem 1.2rem;
  border-radius: 0;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 0.8rem;
  color: var(--artemis-cyan);
  letter-spacing: 1.5px;
  text-transform: uppercase;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: all 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  box-shadow: 
    0 0 30px rgba(0, 243, 255, 0.5),
    inset 0 0 20px rgba(0, 243, 255, 0.1);
  clip-path: polygon(
    0 0, calc(100% - 12px) 0, 
    100% 12px, 100% 100%, 
    12px 100%, 0 calc(100% - 12px)
  );
  z-index: 9999;
  min-width: 240px;
}

/* Holographic scan effect */
.artemis-tooltip::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, 
    transparent, 
    var(--artemis-electric), 
    transparent
  );
  animation: tooltipScan 3s linear infinite;
  opacity: 0.8;
}

@keyframes tooltipScan {
  0% { transform: translateY(0); }
  100% { transform: translateY(50px); }
}

/* Arrow pointing to button */
.artemis-tooltip::after {
  content: '';
  position: absolute;
  top: 50%;
  right: -12px;
  transform: translateY(-50%);
  width: 0;
  height: 0;
  border-top: 10px solid transparent;
  border-bottom: 10px solid transparent;
  border-left: 12px solid var(--artemis-cyan);
  filter: drop-shadow(0 0 8px var(--artemis-cyan));
}

/* Glitch effect on tooltip text */
.artemis-tooltip-text {
  position: relative;
  display: block;
  text-shadow: 
    0 0 10px var(--artemis-cyan),
    2px 2px 0 rgba(255, 0, 60, 0.3),
    -2px -2px 0 rgba(0, 243, 255, 0.3);
}

.artemis-tooltip-subtitle {
  display: block;
  font-size: 0.65rem;
  color: rgba(0, 243, 255, 0.6);
  margin-top: 0.3rem;
  letter-spacing: 2px;
}

.artemis-trigger:hover .artemis-tooltip {
  opacity: 1;
  transform: translateY(50%) translateX(0);
}

/* Glitch animation on hover */
@keyframes glitchText {
  0%, 100% {
    text-shadow: 
      0 0 10px var(--artemis-cyan),
      2px 2px 0 rgba(255, 0, 60, 0.3),
      -2px -2px 0 rgba(0, 243, 255, 0.3);
  }
  25% {
    text-shadow: 
      0 0 10px var(--artemis-cyan),
      -2px 2px 0 rgba(255, 0, 60, 0.5),
      2px -2px 0 rgba(0, 243, 255, 0.5);
  }
  50% {
    text-shadow: 
      0 0 10px var(--artemis-cyan),
      2px -2px 0 rgba(255, 0, 60, 0.4),
      -2px 2px 0 rgba(0, 243, 255, 0.4);
  }
  75% {
    text-shadow: 
      0 0 10px var(--artemis-cyan),
      -2px -2px 0 rgba(255, 0, 60, 0.3),
      2px 2px 0 rgba(0, 243, 255, 0.3);
  }
}

.artemis-trigger:hover .artemis-tooltip-text {
  animation: glitchText 0.3s infinite;
}

/* CHAT WINDOW - ENHANCED CYBERPUNK */
.artemis-window {
  position: fixed;
  bottom: 120px;
  right: 30px;
  width: 420px;
  max-width: calc(100vw - 60px);
  height: 600px;
  max-height: calc(100vh - 160px);
  background: var(--artemis-darker);
  border: 3px solid var(--artemis-cyan);
  border-radius: 0;
  z-index: 9999;
  display: none;
  flex-direction: column;
  box-shadow: 
    0 0 40px rgba(0, 243, 255, 0.5),
    0 20px 60px rgba(0, 0, 0, 0.8),
    inset 0 0 30px rgba(0, 243, 255, 0.05);
  clip-path: polygon(
    15px 0, calc(100% - 15px) 0,
    100% 15px, 100% calc(100% - 15px),
    calc(100% - 15px) 100%, 15px 100%,
    0 calc(100% - 15px), 0 15px
  );
  animation: slideUpGlitch 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}

.artemis-window.active {
  display: flex;
}

@keyframes slideUpGlitch {
  0% {
    transform: translateY(100px);
    opacity: 0;
    filter: hue-rotate(0deg);
  }
  20% {
    transform: translateY(-10px);
    opacity: 0.5;
    filter: hue-rotate(30deg);
  }
  40% {
    transform: translateY(5px);
    opacity: 0.8;
    filter: hue-rotate(-15deg);
  }
  60% {
    transform: translateY(-3px);
    opacity: 1;
    filter: hue-rotate(10deg);
  }
  80% {
    transform: translateY(2px);
    filter: hue-rotate(-5deg);
  }
  100% {
    transform: translateY(0);
    opacity: 1;
    filter: hue-rotate(0deg);
  }
}

/* HEADER - ENHANCED */
.artemis-header {
  background: linear-gradient(135deg, rgba(0, 243, 255, 0.1), rgba(176, 38, 255, 0.1));
  border-bottom: 2px solid var(--artemis-cyan);
  padding: 1rem 1.2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.artemis-header::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, 
    transparent, 
    var(--artemis-cyan), 
    var(--artemis-electric),
    transparent
  );
  animation: scanLine 3s linear infinite;
}

@keyframes scanLine {
  0% { left: -100%; }
  100% { left: 100%; }
}

.artemis-title-wrapper {
  display: flex;
  align-items: center;
  gap: 0.8rem;
}

.artemis-status-led {
  width: 10px;
  height: 10px;
  background: #00ff00;
  border-radius: 50%;
  box-shadow: 
    0 0 10px #00ff00,
    0 0 20px rgba(0, 255, 0, 0.5);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { 
    opacity: 1;
    transform: scale(1);
  }
  50% { 
    opacity: 0.3;
    transform: scale(0.8);
  }
}

.artemis-title {
  font-family: 'Orbitron', sans-serif;
  font-size: 1rem;
  font-weight: 700;
  color: var(--artemis-cyan);
  text-transform: uppercase;
  letter-spacing: 2px;
  text-shadow: 
    0 0 10px var(--artemis-cyan),
    0 0 20px rgba(0, 243, 255, 0.5);
}

.artemis-version {
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 0.65rem;
  color: rgba(0, 243, 255, 0.6);
  letter-spacing: 1px;
}

.artemis-close {
  background: transparent;
  border: 2px solid var(--artemis-red);
  color: var(--artemis-red);
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  clip-path: polygon(20% 0%, 80% 0%, 100% 20%, 100% 80%, 80% 100%, 20% 100%, 0% 80%, 0% 20%);
  font-size: 1.2rem;
  font-weight: bold;
}

.artemis-close:hover {
  background: var(--artemis-red);
  color: var(--artemis-darker);
  box-shadow: 
    0 0 20px var(--artemis-red),
    inset 0 0 10px rgba(255, 0, 60, 0.3);
  transform: rotate(90deg);
}

/* MESSAGES AREA - ENHANCED */
.artemis-messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  background: 
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(0, 243, 255, 0.02) 2px,
      rgba(0, 243, 255, 0.02) 4px
    ),
    var(--artemis-dark);
  display: flex;
  flex-direction: column;
  gap: 1rem;
  position: relative;
}

/* Ambient glow effect */
.artemis-messages::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(
    ellipse at center,
    rgba(0, 243, 255, 0.03) 0%,
    transparent 70%
  );
  pointer-events: none;
}

.artemis-messages::-webkit-scrollbar {
  width: 8px;
}

.artemis-messages::-webkit-scrollbar-track {
  background: var(--artemis-darker);
  border-left: 1px solid var(--artemis-cyan);
}

.artemis-messages::-webkit-scrollbar-thumb {
  background: var(--artemis-cyan);
  box-shadow: 0 0 10px var(--artemis-cyan);
}

/* MESSAGE BUBBLE - ENHANCED */
.artemis-message {
  display: flex;
  gap: 0.8rem;
  animation: messageAppear 0.4s ease-out;
}

@keyframes messageAppear {
  0% {
    transform: translateY(20px) scale(0.95);
    opacity: 0;
  }
  100% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

.artemis-message.user {
  flex-direction: row-reverse;
}

.artemis-avatar {
  width: 40px;
  height: 40px;
  min-width: 40px;
  border: 2px solid var(--artemis-cyan);
  display: flex;
  align-items: center;
  justify-content: center;
  clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
  font-size: 1.2rem;
  position: relative;
}

.artemis-message.ai .artemis-avatar {
  background: linear-gradient(135deg, var(--artemis-cyan), var(--artemis-violet));
  color: #ffffff;
  box-shadow: 
    0 0 15px var(--artemis-cyan),
    inset 0 0 10px rgba(0, 243, 255, 0.3);
  animation: avatarGlow 2s ease-in-out infinite;
}

@keyframes avatarGlow {
  0%, 100% {
    box-shadow: 
      0 0 15px var(--artemis-cyan),
      inset 0 0 10px rgba(0, 243, 255, 0.3);
  }
  50% {
    box-shadow: 
      0 0 25px var(--artemis-cyan),
      inset 0 0 15px rgba(0, 243, 255, 0.5);
  }
}

.artemis-message.user .artemis-avatar {
  background: var(--artemis-yellow);
  border-color: var(--artemis-yellow);
  color: var(--artemis-dark);
  box-shadow: 
    0 0 15px var(--artemis-yellow),
    inset 0 0 10px rgba(252, 238, 10, 0.3);
}

.artemis-bubble {
  background: rgba(0, 243, 255, 0.1);
  border: 1px solid var(--artemis-cyan);
  padding: 0.8rem 1rem;
  border-radius: 8px;
  max-width: 75%;
  position: relative;
  font-family: 'Rajdhani', sans-serif;
  font-size: 0.95rem;
  line-height: 1.5;
  word-wrap: break-word;
  backdrop-filter: blur(10px);
}

.artemis-message.ai .artemis-bubble {
  color: var(--artemis-cyan);
  border-color: var(--artemis-cyan);
  clip-path: polygon(0 0, calc(100% - 10px) 0, 100% 10px, 100% 100%, 10px 100%, 0 calc(100% - 10px));
  box-shadow: 
    0 0 20px rgba(0, 243, 255, 0.1),
    inset 0 0 30px rgba(0, 243, 255, 0.05);
}

.artemis-message.user .artemis-bubble {
  background: rgba(252, 238, 10, 0.1);
  border-color: var(--artemis-yellow);
  color: #ffffff;
  clip-path: polygon(10px 0, 100% 0, 100% calc(100% - 10px), calc(100% - 10px) 100%, 0 100%, 0 10px);
  box-shadow: 
    0 0 20px rgba(252, 238, 10, 0.1),
    inset 0 0 30px rgba(252, 238, 10, 0.05);
}

/* LOADING INDICATOR - ENHANCED */
.artemis-loading {
  display: flex;
  gap: 0.4rem;
  padding: 0.5rem;
}

.artemis-loading-dot {
  width: 8px;
  height: 8px;
  background: var(--artemis-cyan);
  border-radius: 50%;
  animation: loadingBounce 1.4s infinite ease-in-out both;
  box-shadow: 0 0 8px var(--artemis-cyan);
}

.artemis-loading-dot:nth-child(1) {
  animation-delay: -0.32s;
}

.artemis-loading-dot:nth-child(2) {
  animation-delay: -0.16s;
}

@keyframes loadingBounce {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1.2);
    opacity: 1;
    box-shadow: 0 0 15px var(--artemis-cyan);
  }
}

/* INPUT ZONE - ENHANCED */
.artemis-input-zone {
  background: var(--artemis-darker);
  border-top: 2px solid var(--artemis-cyan);
  padding: 1rem 1.2rem;
  display: flex;
  gap: 0.8rem;
  align-items: center;
  position: relative;
}

.artemis-input-zone::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, 
    transparent, 
    var(--artemis-cyan), 
    transparent
  );
}

.artemis-input {
  flex: 1;
  background: rgba(0, 243, 255, 0.05);
  border: 2px solid var(--artemis-cyan);
  color: #ffffff;
  padding: 0.8rem 1rem;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 0.9rem;
  outline: none;
  transition: all 0.3s ease;
  clip-path: polygon(8px 0, 100% 0, 100% calc(100% - 8px), calc(100% - 8px) 100%, 0 100%, 0 8px);
}

.artemis-input::placeholder {
  color: rgba(0, 243, 255, 0.4);
}

.artemis-input:focus {
  background: rgba(0, 243, 255, 0.1);
  box-shadow: 
    0 0 20px rgba(0, 243, 255, 0.3),
    inset 0 0 10px rgba(0, 243, 255, 0.1);
  border-color: var(--artemis-electric);
}

.artemis-send-btn {
  background: linear-gradient(135deg, var(--artemis-cyan), var(--artemis-violet));
  border: 2px solid var(--artemis-cyan);
  color: #ffffff;
  width: 45px;
  height: 45px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  clip-path: polygon(20% 0%, 80% 0%, 100% 20%, 100% 80%, 80% 100%, 20% 100%, 0% 80%, 0% 20%);
  font-size: 1.2rem;
  position: relative;
  overflow: hidden;
}

.artemis-send-btn::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transform: translateX(-100%);
  transition: transform 0.6s;
}

.artemis-send-btn:hover:not(:disabled)::before {
  transform: translateX(100%);
}

.artemis-send-btn:hover:not(:disabled) {
  transform: scale(1.1);
  box-shadow: 
    0 0 30px var(--artemis-cyan),
    inset 0 0 20px rgba(0, 243, 255, 0.3);
}

.artemis-send-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* MARKDOWN SUPPORT */
.artemis-bubble strong {
  color: var(--artemis-yellow);
  font-weight: 700;
  text-shadow: 0 0 5px var(--artemis-yellow);
}

.artemis-bubble code {
  background: rgba(0, 0, 0, 0.4);
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
  font-family: 'Share Tech Mono', 'Courier New', monospace;
  font-size: 0.85rem;
  border: 1px solid rgba(0, 243, 255, 0.3);
}

.artemis-bubble ul {
  margin: 0.5rem 0;
  padding-left: 1.5rem;
}

.artemis-bubble li {
  margin: 0.3rem 0;
}

/* MOBILE RESPONSIVE */
@media (max-width: 576px) {
  .artemis-trigger {
    width: 60px;
    height: 60px;
    bottom: 20px;
    right: 20px;
  }

  .artemis-trigger-icon {
    font-size: 28px;
  }

  .artemis-tooltip {
    bottom: auto;
    top: calc(100% + 15px);
    right: 50%;
    transform: translateX(50%) translateY(10px);
    min-width: 200px;
  }

  .artemis-tooltip::after {
    top: -10px;
    right: 50%;
    transform: translateX(50%);
    border-left: 10px solid transparent;
    border-right: 10px solid transparent;
    border-bottom: 10px solid var(--artemis-cyan);
    border-top: none;
  }

  .artemis-trigger:hover .artemis-tooltip {
    transform: translateX(50%) translateY(0);
  }

  .artemis-window {
    bottom: 95px;
    right: 20px;
    width: calc(100vw - 40px);
    height: calc(100vh - 140px);
  }

  .artemis-bubble {
    max-width: 85%;
  }

  .artemis-input {
    font-size: 0.85rem;
  }
}
</style>

<!-- WIDGET HTML -->
<div class="artemis-trigger" id="artemisTrigger">
  <i class="fas fa-brain artemis-trigger-icon"></i>
  <div class="artemis-tooltip">
    <span class="artemis-tooltip-text">
      DISCUTER AVEC ARTÉMIS
      <span class="artemis-tooltip-subtitle">// IA ASSISTANT</span>
    </span>
  </div>
</div>

<div class="artemis-window" id="artemisWindow">
  <!-- HEADER -->
  <div class="artemis-header">
    <div class="artemis-title-wrapper">
      <div class="artemis-status-led"></div>
      <div>
        <div class="artemis-title">ARTÉMIS</div>
        <div class="artemis-version">v1.0 // ONLINE</div>
      </div>
    </div>
    <button class="artemis-close" id="artemisClose">×</button>
  </div>

  <!-- MESSAGES AREA -->
  <div class="artemis-messages" id="artemisMessages">
    <div class="artemis-message ai">
      <div class="artemis-avatar">
        <i class="fas fa-brain"></i>
      </div>
      <div class="artemis-bubble">
        <strong>Connexion établie.</strong> Je suis Artémis, votre interface neurale. Comment puis-je vous assister ?
      </div>
    </div>
  </div>

  <!-- INPUT ZONE -->
  <div class="artemis-input-zone">
    <input 
      type="text" 
      class="artemis-input" 
      id="artemisInput" 
      placeholder="> Entrez votre requête..."
      autocomplete="off"
    />
    <button class="artemis-send-btn" id="artemisSend">
      <i class="fas fa-arrow-right"></i>
    </button>
  </div>
</div>

<script>
(function() {
  'use strict';

  // CSRF TOKEN RETRIEVAL
  function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
      const cookies = document.cookie.split(';');
      for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.substring(0, name.length + 1) === (name + '=')) {
          cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
          break;
        }
      }
    }
    return cookieValue;
  }

  const csrftoken = getCookie('csrftoken');

  // ELEMENTS
  const trigger = document.getElementById('artemisTrigger');
  const window = document.getElementById('artemisWindow');
  const closeBtn = document.getElementById('artemisClose');
  const messagesContainer = document.getElementById('artemisMessages');
  const input = document.getElementById('artemisInput');
  const sendBtn = document.getElementById('artemisSend');

  // CHAT HISTORY
  let chatHistory = [];

  // TOGGLE WINDOW
  function toggleWindow() {
    window.classList.toggle('active');
    if (window.classList.contains('active')) {
      input.focus();
    }
  }

  trigger.addEventListener('click', toggleWindow);
  closeBtn.addEventListener('click', toggleWindow);

  // RENDER MESSAGE
  function renderMessage(text, sender = 'ai') {
    const messageDiv = document.createElement('div');
    messageDiv.className = `artemis-message ${sender}`;

    const avatar = document.createElement('div');
    avatar.className = 'artemis-avatar';
    avatar.innerHTML = sender === 'ai' 
      ? '<i class="fas fa-brain"></i>' 
      : '<i class="fas fa-user"></i>';

    const bubble = document.createElement('div');
    bubble.className = 'artemis-bubble';
    
    // Simple Markdown to HTML conversion
    let formattedText = text
      .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
      .replace(/\n/g, '<br>');
    
    bubble.innerHTML = formattedText;

    messageDiv.appendChild(avatar);
    messageDiv.appendChild(bubble);
    messagesContainer.appendChild(messageDiv);

    // Auto-scroll
    messagesContainer.scrollTop = messagesContainer.scrollHeight;
  }

  // RENDER LOADING
  function renderLoading() {
    const loadingDiv = document.createElement('div');
    loadingDiv.className = 'artemis-message ai';
    loadingDiv.id = 'artemisLoading';

    const avatar = document.createElement('div');
    avatar.className = 'artemis-avatar';
    avatar.innerHTML = '<i class="fas fa-brain"></i>';

    const bubble = document.createElement('div');
    bubble.className = 'artemis-bubble';

    const loading = document.createElement('div');
    loading.className = 'artemis-loading';
    loading.innerHTML = `
      <div class="artemis-loading-dot"></div>
      <div class="artemis-loading-dot"></div>
      <div class="artemis-loading-dot"></div>
    `;

    bubble.appendChild(loading);
    loadingDiv.appendChild(avatar);
    loadingDiv.appendChild(bubble);
    messagesContainer.appendChild(loadingDiv);

    messagesContainer.scrollTop = messagesContainer.scrollHeight;
  }

  // REMOVE LOADING
  function removeLoading() {
    const loadingEl = document.getElementById('artemisLoading');
    if (loadingEl) {
      loadingEl.remove();
    }
  }

  // SEND MESSAGE
  async function sendMessage() {
    const message = input.value.trim();
    if (!message) return;

    // Disable input
    input.disabled = true;
    sendBtn.disabled = true;

    // Display user message
    renderMessage(message, 'user');
    chatHistory.push({ sender: 'user', text: message });

    // Clear input
    input.value = '';

    // Show loading
    renderLoading();

    try {
      const response = await fetch('{% url "bot:ask" %}', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'X-CSRFToken': csrftoken
        },
        body: JSON.stringify({
          message: message,
          history: chatHistory
        })
      });

      removeLoading();

      if (!response.ok) {
        throw new Error('Erreur réseau');
      }

      const data = await response.json();

      if (data.error) {
        renderMessage('⚠️ Erreur : ' + data.error, 'ai');
      } else {
        renderMessage(data.response, 'ai');
        chatHistory.push({ sender: 'ai', text: data.response });
      }

    } catch (error) {
      removeLoading();
      renderMessage('❌ Connexion neurale interrompue. Veuillez réessayer.', 'ai');
      console.error('Artémis Error:', error);
    }

    // Re-enable input
    input.disabled = false;
    sendBtn.disabled = false;
    input.focus();
  }

  // EVENT LISTENERS
  sendBtn.addEventListener('click', sendMessage);
  
  input.addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
      sendMessage();
    }
  });

  // GLITCH EFFECT ON TRIGGER (RANDOM)
  setInterval(() => {
    if (Math.random() > 0.98) {
      trigger.style.filter = 'hue-rotate(90deg)';
      setTimeout(() => {
        trigger.style.filter = 'none';
      }, 100);
    }
  }, 2000);

})();
</script>