/* Chat Widget Styles */
.chat-widget {
  position: fixed;
  top: 50%;
  right: -400px;
  width: 380px;
  height: 500px;
  background: linear-gradient(135deg, #0a0e14 0%, #0e1218 50%, #121820 100%);
  border: 1px solid rgba(93, 173, 226, 0.2);
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(93, 173, 226, 0.1);
  z-index: 10000;
  transform: translateY(-50%);
  transition: right 0.3s ease-in-out;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(10px);
}

html.light .chat-widget {
  background: linear-gradient(135deg, #ffffff 0%, #f5f9fc 50%, #eef5fb 100%);
  border: 1px solid rgba(52, 152, 219, 0.25);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12), 0 0 0 1px rgba(52, 152, 219, 0.15);
}
  
  .chat-widget.show {
    right: 20px;
  }
  
  .chat-header {
    background: linear-gradient(135deg, #5dade2 0%, #3498db 100%);
    color: white;
    padding: 16px 20px;
    border-radius: 12px 12px 0 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }
  
  .chat-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
  }
  
  .chat-close-btn {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background-color 0.2s;
  }
  
  .chat-close-btn:hover {
    background-color: rgba(255, 255, 255, 0.1);
  }
  
  .chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: transparent;
    color: var(--text-color);
    scrollbar-width: thin;
    scrollbar-color: rgba(93, 173, 226, 0.3) transparent;
    /* Prevent touch events from bubbling to parent */
    touch-action: pan-y;
  }
  
  .chat-messages::-webkit-scrollbar {
    width: 6px;
  }
  
  .chat-messages::-webkit-scrollbar-track {
    background: transparent;
  }
  
  .chat-messages::-webkit-scrollbar-thumb {
    background: rgba(93, 173, 226, 0.3);
    border-radius: 3px;
  }
  
  .chat-messages::-webkit-scrollbar-thumb:hover {
    background: rgba(93, 173, 226, 0.5);
  }
  
  .chat-message {
    max-width: 85%;
    margin-bottom: 8px;
  }
  
  .chat-message.user-message {
    align-self: flex-end;
  }
  
  .chat-message.ai-message {
    align-self: flex-start;
  }
  
  /* Typing indicator animation */
  .typing-indicator {
    display: inline-block;
    color: var(--accent-color, #5dade2);
    font-size: 20px;
    animation: typing-pulse 1.4s infinite;
  }
  
  @keyframes typing-pulse {
    0%, 100% {
      opacity: 0.3;
      transform: scale(0.8);
    }
    50% {
      opacity: 1;
      transform: scale(1.2);
    }
  }
  
  .message-content {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
  }
  
  /* Formatting styles for markdown content */
  .message-content strong {
    font-weight: 600;
    color: inherit;
  }
  
  .message-content em {
    font-style: italic;
  }
  
  .message-content ol,
  .message-content ul {
    margin: 8px 0;
    padding-left: 24px;
  }
  
  .message-content li {
    margin: 4px 0;
    line-height: 1.5;
  }
  
  .message-content ol {
    list-style-type: decimal;
  }
  
  .message-content ul {
    list-style-type: disc;
  }
  
  .message-content code {
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Monaco', 'Menlo', 'Courier New', monospace;
    font-size: 13px;
  }
  
  .message-content br {
    display: block;
    content: "";
    margin: 4px 0;
  }
  
  .user-message .message-content {
    background: linear-gradient(135deg, #5dade2 0%, #3498db 100%);
    color: white;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 8px rgba(93, 173, 226, 0.3);
  }
  
  .ai-message .message-content {
    background: linear-gradient(135deg, rgba(93, 173, 226, 0.08) 0%, rgba(52, 152, 219, 0.05) 100%);
    color: var(--text-color, #ffffff);
    border-bottom-left-radius: 4px;
    border: 1px solid rgba(93, 173, 226, 0.25);
    box-shadow: 0 2px 8px rgba(93, 173, 226, 0.1);
    backdrop-filter: blur(10px);
  }

  html.light .ai-message .message-content {
    background: linear-gradient(135deg, rgba(93, 173, 226, 0.12) 0%, rgba(52, 152, 219, 0.08) 100%);
    color: var(--text-color, #000000);
    border: 1px solid rgba(52, 152, 219, 0.3);
    box-shadow: 0 2px 8px rgba(52, 152, 219, 0.15);
  }
  
  
  .ai-message.error .message-content {
    background: #fef2f2;
    color: #dc2626;
    border-color: #fca5a5;
  }
  
  .chat-input-container {
    padding: 12px 16px;
    border-top: 1px solid rgba(93, 173, 226, 0.2);
    background: linear-gradient(180deg, rgba(10, 14, 20, 0.8) 0%, rgba(14, 18, 24, 0.95) 100%);
    backdrop-filter: blur(10px);
    border-radius: 0 0 12px 12px;
    display: flex;
    gap: 12px;
    align-items: center;
  }
  
  html.light .chat-input-container {
    background: linear-gradient(180deg, rgba(255, 255, 255, 0.9) 0%, rgba(245, 249, 252, 0.95) 100%);
    border-top: 1px solid rgba(52, 152, 219, 0.2);
  }
  
  .chat-input {
    flex: 1;
    border: 2px solid rgba(93, 173, 226, 0.3);
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 16px;
    resize: none;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
    font-family: inherit;
    height: 44px;
    background: rgba(14, 18, 24, 0.6);
    color: var(--text-color);
    box-sizing: border-box;
    line-height: 1.4;
    /* Prevent iOS zoom and accessory bar */
    -webkit-appearance: none;
    appearance: none;
  }
  
  html.light .chat-input {
    background: rgba(255, 255, 255, 0.8);
    border-color: rgba(52, 152, 219, 0.3);
  }
  
  .chat-input:focus {
    border-color: #5dade2;
    background: rgba(14, 18, 24, 0.8);
    box-shadow: 0 0 0 3px rgba(93, 173, 226, 0.15);
  }
  
  html.light .chat-input:focus {
    background: rgba(255, 255, 255, 1);
    border-color: #3498db;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.15);
  }

  /* Placeholder text styling for chat input */
  .chat-input::placeholder {
    font-size: 14px;
    color: var(--secondary-text);
    opacity: 0.6;
  }

  /* Light theme placeholder */
  html.light .chat-input::placeholder {
    color: var(--secondary-text);
    opacity: 0.5;
  }
  
  .chat-send-btn {
    background: linear-gradient(135deg, #5dade2 0%, #3498db 100%);
    color: white;
    border: none;
    border-radius: 12px;
    padding: 0;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    min-width: 44px;
    box-shadow: 0 2px 8px rgba(93, 173, 226, 0.3);
    /* Improve touch handling on mobile */
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
  }

  .chat-send-btn svg {
    width: 20px;
    height: 20px;
    stroke-width: 2;
  }
  
  .chat-send-btn:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(93, 173, 226, 0.4);
  }
  
  .chat-send-btn:active {
    transform: translateY(0);
  }
  
  .chat-send-btn:disabled {
    background: var(--disabled-bg, #9ca3af);
    cursor: not-allowed;
  }
  
  /* Loading animation */
  .typing-dots {
    display: inline-flex;
    align-items: center;
    gap: 4px;
  }
  

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-color);
  opacity: 0.4;
  animation: typing 1.4s infinite;
}
  
  .typing-dots span:nth-child(1) {
    animation-delay: 0s;
  }
  
  .typing-dots span:nth-child(2) {
    animation-delay: 0.2s;
  }
  
  .typing-dots span:nth-child(3) {
    animation-delay: 0.4s;
  }
  
  @keyframes typing {
    0%, 60%, 100% {
      transform: scale(1);
      opacity: 0.4;
    }
    30% {
      transform: scale(1.2);
      opacity: 1;
    }
  }
  
  /* Ensure chat widget is above other elements */
  .chat-widget {
    z-index: 10001;
  }
  
  /* Adjust sources widget z-index to be below chat */
  .sources-widget {
    z-index: 10000;
  }
  
  /* Animation for smooth transitions */
  .chat-widget * {
    transition: all 0.2s ease;
  }
  
  /* Accessibility improvements */
  .chat-widget:focus-within {
    outline: 2px solid var(--primary-color, #2563eb);
    outline-offset: 2px;
  }
  
  /* Ensure proper text wrapping in messages */
  .message-content {
    word-break: break-word;
    overflow-wrap: break-word;
  }
  
  /* Style for code blocks if AI returns code */
  .message-content code {
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 4px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 13px;
  }
  
  .message-content pre {
    background: rgba(0, 0, 0, 0.1);
    padding: 8px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 8px 0;
  }
  
  .message-content pre code {
    background: none;
    padding: 0;
  }


/* Enhanced action buttons */
.action-buttons {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  /* Force horizontal layout even on mobile */
  flex-direction: row !important;
}
  
.sources-btn,
.ask-ai-btn {
  flex: 1;
  padding: 8px 12px; /* Reduced padding for compact size */
  border: 1px solid var(--border-color, #e0e0e0);
  border-radius: 6px; /* Slightly smaller border radius */
  background: var(--card-bg, #ffffff);
  color: var(--text-color, #334155);
  font-size: 13px; /* Smaller font size */
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px; /* Reduced gap */
  min-height: 36px; /* Smaller minimum height */
}
  
  .sources-btn:hover,
  .ask-ai-btn:hover {
    background: var(--primary-color, #2563eb);
    color: white;
    border-color: var(--primary-color, #2563eb);
    transform: translateY(-1px);
  }
  
  .ask-ai-btn svg {
    transition: transform 0.2s ease;
  }
  
  .ask-ai-btn:hover svg {
    transform: scale(1.1);
  }
  
  /* Active state when chat is open */
  .ask-ai-btn.active {
    background: var(--primary-color, #2563eb);
    color: white;
    border-color: var(--primary-color, #2563eb);
  }
  
/* Backdrop blur overlay for mobile */
.chat-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 10000;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

.chat-backdrop.show {
  display: block;
  opacity: 1;
}

/* Mobile responsive adjustments */
@media (max-width: 992px) {
  .action-buttons {
    flex-direction: row !important; /* Force horizontal even on mobile */
    gap: 6px;
    margin-top: 12px;
  }
  
  .sources-btn,
  .ask-ai-btn {
    padding: 10px 8px; /* Adjusted padding for mobile */
    font-size: 12px;
    min-height: 40px;
  }

  .chat-widget {
    position: fixed;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 85vh;
    height: 85dvh; /* Use dynamic viewport height for mobile */
    max-width: 100vw;
    max-height: 70dvh;
    border-radius: 16px 16px 0 0;
    transform: translateY(100%);
    transition: transform 0.3s ease-in-out;
    margin: 0;
    box-sizing: border-box;
    overflow: hidden;
    z-index: 10001;
    /* Handle iOS safe areas */
    padding-top: env(safe-area-inset-top);
    padding-bottom: 0; /* Remove bottom padding, will be handled by input container */
    display: flex;
    flex-direction: column;
  }
  
  .chat-widget.show {
    transform: translateY(0);
  }
  
  /* When keyboard is open on iOS, adjust positioning */
  .chat-widget.keyboard-open {
    position: fixed;
    bottom: auto;
    transition: none;
    /* Height and top will be set dynamically via JS */
    display: flex;
    flex-direction: column;
    border-radius: 0;
  }
  
  /* Ensure messages container shrinks properly when keyboard is open */
  .chat-widget.keyboard-open .chat-messages {
    flex: 1;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
  }
  
  /* Keep header and input container at fixed heights */
  .chat-widget.keyboard-open .chat-header {
    flex-shrink: 0;
  }
  
  .chat-widget.keyboard-open .chat-input-container {
    flex-shrink: 0;
  }

  body.chat-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
  }
  
  .chat-header {
    flex-shrink: 0;
    position: sticky;
    top: 0;
    z-index: 10;
    padding-top: max(16px, env(safe-area-inset-top));
  }
  
  .chat-messages {
    flex: 1;
    padding: 16px 20px;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
    overflow-y: auto;
    min-height: 0; /* Allow flex shrinking */
  }
  
  .chat-input-container {
    flex-shrink: 0;
    position: sticky;
    bottom: 0;
    z-index: 10;
    padding-bottom: 12px;
    /* Add safe area padding only when keyboard is not open */
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  
  /* When keyboard is open, remove extra bottom padding */
  .chat-widget.keyboard-open .chat-input-container {
    padding-bottom: 12px;
  }
  
  .chat-input {
    font-size: 16px; /* Prevent iOS zoom on focus */
    -webkit-appearance: none;
    appearance: none;
    /* Disable iOS accessory bar */
    -webkit-user-select: text;
    user-select: text;
  }
  
  /* Prevent iOS from showing the accessory bar */
  .chat-input:focus {
    -webkit-tap-highlight-color: transparent;
  }
  
  .chat-widget,
  .chat-widget * {
    box-sizing: border-box;
    max-width: 100%;
  }
  
  /* Ensure send button is always tappable */
  .chat-send-btn {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    pointer-events: auto;
  }
}