<!-- SEMPA Chatbot Widget -->
<style>
  #sempa-chat-button {
    position: fixed;
    bottom: 20px;
    right: 20px;
    background: #003366;
    color: white;
    border: none;
    border-radius: 50%;
    width: 60px;
    height: 60px;
    font-size: 28px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    z-index: 1000;
    cursor: pointer;
  }

  #sempa-chat-container {
    display: none;
    position: fixed;
    bottom: 90px;
    right: 20px;
    width: 400px;
    height: 600px;
    z-index: 1001;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    background: #f0f0f0;
  }

  #sempa-chat-iframe {
    width: 100%;
    height: 100%;
    border: none;
    border-radius: 12px;
  }

  #sempa-chat-controls {
    position: absolute;
    top: -36px;
    right: 0;
    display: flex;
    gap: 5px;
  }

  .sempa-chat-control {
    background: #003366;
    color: white;
    font-size: 14px;
    border: none;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    cursor: pointer;
    text-align: center;
    line-height: 24px;
  }
</style>

<button id="sempa-chat-button">💬</button>

<div id="sempa-chat-container">
  <div id="sempa-chat-controls">
    <button class="sempa-chat-control" onclick="toggleChatSize()">↕</button>
    <button class="sempa-chat-control" onclick="closeSEMPAChat()">×</button>
  </div>
  <iframe id="sempa-chat-iframe" src="https://sempassistant.streamlit.app" title="SEMPA Chatbot"></iframe>
</div>

<script>
  const chatButton = document.getElementById("sempa-chat-button");
  const chatContainer = document.getElementById("sempa-chat-container");
  let autoCloseTimer = null;
  let isExpanded = false;

  chatButton.onclick = function () {
    chatContainer.style.display = "block";
    resetAutoClose();
  };

  function closeSEMPAChat() {
    chatContainer.style.display = "none";
    clearTimeout(autoCloseTimer);
  }

  function toggleChatSize() {
    if (!isExpanded) {
      chatContainer.style.width = "90vw";
      chatContainer.style.height = "80vh";
      isExpanded = true;
    } else {
      chatContainer.style.width = "400px";
      chatContainer.style.height = "600px";
      isExpanded = false;
    }
  }

  function resetAutoClose() {
    clearTimeout(autoCloseTimer);
    autoCloseTimer = setTimeout(() => {
      closeSEMPAChat();
    }, 5 * 60 * 1000); // 5 minutes of inactivity
  }

  // Make the chat draggable
  let isDragging = false, offsetX, offsetY;

  chatContainer.addEventListener("mousedown", function(e) {
    isDragging = true;
    offsetX = e.clientX - chatContainer.getBoundingClientRect().left;
    offsetY = e.clientY - chatContainer.getBoundingClientRect().top;
    chatContainer.style.transition = "none";
  });

  document.addEventListener("mousemove", function(e) {
    if (isDragging) {
      chatContainer.style.left = e.clientX - offsetX + "px";
      chatContainer.style.top = e.clientY - offsetY + "px";
      chatContainer.style.bottom = "auto";
      chatContainer.style.right = "auto";
    }
  });

  document.addEventListener("mouseup", function() {
    isDragging = false;
  });
</script>