/* =======================================
   CHAT SYSTEM — MOBILE + DESKTOP MODES
======================================= */

/* Enhanced Chat Launcher with pulse animation */
#chatButton {
  transition: opacity .2s ease, transform .2s ease;
  cursor: pointer;
  user-select: none;
}

#chatButton:hover .relative > div:first-child {
  transform: scale(1.1);
}

#chatButton:active .relative > div:first-child {
  transform: scale(0.95);
}

/* Overlay */
#chatOverlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.35);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
#chatOverlay.active {
  opacity: 1;
  pointer-events: auto;
}

/* Chat Box */
#chatBox {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  opacity: 0;
  pointer-events: none;
  transform: translateY(100%);
  transition:
    opacity .25s ease,
    transform .25s ease;
  touch-action: pan-y;
  /* Allow height to adjust for mobile keyboard */
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height for mobile browsers */
}
.dark #chatBox {
  background: #131313;
}

/* Desktop floating mode */
@media (min-width: 768px) {
  #chatBox {
    width: 380px;
    height: 520px;
    max-height: 520px;
    left: auto;
    right: 20px;
    bottom: 20px;
    top: auto;
    border-radius: 12px;
    border: 1px solid rgba(0,0,0,0.12);
    box-shadow: 0 0 24px rgba(0,0,0,0.18);
    transform: scale(0.95);
  }
  .dark #chatBox {
    border-color: rgba(255,255,255,0.15);
  }
}

/* Chat active toggle */
#chatBox.active {
  opacity: 1;
  pointer-events: auto;
}
@media (min-width: 768px) {
  #chatBox.active { transform: scale(1); }
}
@media (max-width: 767px) {
  #chatBox.active { transform: translateY(0); }
}

/* Header */
.chat-header {
  flex: 0 0 auto;
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: inherit;
  border-bottom: 1px solid rgba(0,0,0,0.12);
  user-select: none;
}
.dark .chat-header {
  border-color: rgba(255,255,255,0.15);
}

/* Swipe indicator */
.swipe-indicator {
  width: 36px;
  height: 4px;
  border-radius: 999px;
  background: rgba(0,0,0,0.25);
  transform: translateY(-4px);
}
.dark .swipe-indicator {
  background: rgba(255,255,255,0.3);
}
@media (min-width: 768px) {
  .swipe-indicator { display: none; }
}

/* Messages scroll area */
#ai-chat-log {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

/* Scrollbars */
#ai-chat-log::-webkit-scrollbar { width: 6px; }
#ai-chat-log::-webkit-scrollbar-thumb {
  background: rgba(120,120,120,0.4);
  border-radius: 4px;
}
.dark #ai-chat-log::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.2);
}

/* Input area */
.input-area {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px 12px;
  border-top: 1px solid rgba(0,0,0,0.12);
  background: inherit;
  position: sticky;
  bottom: 0;
  z-index: 10;
}
.dark .input-area {
  border-color: rgba(255,255,255,0.15);
}

/* Input field */
#ai-chat-input { 
  flex: 1;
  max-height: 120px;
  resize: none;
  overflow-y: auto;
}

/* Handle notches and safe areas */
@supports(padding: env(safe-area-inset-bottom)) {
  .input-area {
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  
  /* For mobile devices, ensure input stays above keyboard */
  @media (max-width: 767px) {
    #chatBox {
      padding-bottom: env(safe-area-inset-bottom);
    }
  }
}

/* ================= CHAT BUTTON SOFT PULSE ================= */

/* Softer, slower pulse animation for chat button */
@keyframes chatPulse {
  0%, 100% {
    opacity: 0.4;
    transform: scale(1);
  }
  50% {
    opacity: 0;
    transform: scale(1.3);
  }
}

.chat-pulse {
  animation: chatPulse 3s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}