/**
 * CoPilot Stock Chatbot Styles
 * Integrates with existing dark theme using CSS custom properties
 */

/* Use existing CSS variables from styles.css */
:root {
  /* Ensure chatbot-specific z-index layering */
  --chatbot-z-toggle: 10000;
  --chatbot-z-window: 10001;
  --chatbot-shadow-toggle: 0 4px 20px rgba(0, 0, 0, 0.3);
  --chatbot-shadow-window: 0 8px 32px rgba(0, 0, 0, 0.4);
  
  /* Animation timing */
  --chatbot-transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  --chatbot-bounce: cubic-bezier(0.68, -0.55, 0.265, 1.55);
  
  /* Chatbot specific colors (using existing dark theme) */
  --chatbot-bg-primary: var(--bg-card, #1a1a1a);
  --chatbot-bg-secondary: var(--bg-secondary, #2a2a2a);
  --chatbot-bg-input: var(--bg-primary, #141414);
  --chatbot-text-primary: var(--text-primary, #ffffff);
  --chatbot-text-secondary: var(--text-secondary, #b3b3b3);
  --chatbot-text-muted: var(--text-muted, #666666);
  --chatbot-accent: var(--accent-primary, #007bff);
  --chatbot-accent-hover: var(--accent-secondary, #0056b3);
  --chatbot-border: var(--border-color, #333333);
  --chatbot-success: #10b981;
  --chatbot-warning: #f59e0b;
  --chatbot-error: #ef4444;
}

/* Main container */
.copilot-chatbot-container {
  font-family: var(--font-family, -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif);
  font-size: 14px;
  line-height: 1.4;
  
  /* Ensure container doesn't interfere with page layout */
  pointer-events: none;
}

.copilot-chatbot-container * {
  pointer-events: auto;
  box-sizing: border-box;
}

/* Toggle Button */
.copilot-chatbot-toggle {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--chatbot-accent) 0%, var(--chatbot-accent-hover) 100%);
  border: none;
  box-shadow: var(--chatbot-shadow-toggle);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  z-index: var(--chatbot-z-toggle);
  transition: var(--chatbot-transition);
  position: relative;
  overflow: hidden;
}

.copilot-chatbot-toggle:hover {
  transform: scale(1.05);
  box-shadow: 0 6px 25px rgba(0, 123, 255, 0.4);
}

.copilot-chatbot-toggle:active {
  transform: scale(0.95);
}

.copilot-chatbot-toggle svg {
  width: 24px;
  height: 24px;
  transition: var(--chatbot-transition);
}

.copilot-chatbot-toggle .close-icon {
  position: absolute;
  opacity: 0;
  transform: rotate(90deg);
}

.copilot-chatbot-container.chatbot-visible .copilot-chatbot-toggle .chat-icon {
  opacity: 0;
  transform: rotate(-90deg);
}

.copilot-chatbot-container.chatbot-visible .copilot-chatbot-toggle .close-icon {
  opacity: 1;
  transform: rotate(0deg);
}

/* Chat Window */
.copilot-chatbot-window {
  position: absolute;
  bottom: 80px;
  right: 0;
  width: 380px;
  max-width: calc(100vw - 40px);
  height: 600px;
  max-height: calc(100vh - 120px);
  background: var(--chatbot-bg-primary);
  border-radius: var(--radius-lg, 12px);
  box-shadow: var(--chatbot-shadow-window);
  border: 1px solid var(--chatbot-border);
  display: flex;
  flex-direction: column;
  z-index: var(--chatbot-z-window);
  
  /* Initial hidden state */
  opacity: 0;
  visibility: hidden;
  transform: translateY(20px) scale(0.95);
  transition: var(--chatbot-transition);
}

.copilot-chatbot-window.chatbot-window-visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
}

/* Header */
.chatbot-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--chatbot-border);
  background: var(--chatbot-bg-secondary);
  border-radius: var(--radius-lg, 12px) var(--radius-lg, 12px) 0 0;
}

.chatbot-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chatbot-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--chatbot-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  flex-shrink: 0;
}

.chatbot-avatar svg {
  width: 20px;
  height: 20px;
}

.chatbot-info h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: var(--chatbot-text-primary);
}

.chatbot-status {
  margin: 0;
  font-size: 12px;
  color: var(--chatbot-success);
}

.chatbot-close {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm, 6px);
  cursor: pointer;
  color: var(--chatbot-text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--chatbot-transition);
}

.chatbot-close:hover {
  background: var(--chatbot-bg-input);
  color: var(--chatbot-text-primary);
}

.chatbot-close svg {
  width: 16px;
  height: 16px;
}

/* Messages Container */
.chatbot-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.chatbot-messages::-webkit-scrollbar {
  width: 4px;
}

.chatbot-messages::-webkit-scrollbar-track {
  background: transparent;
}

.chatbot-messages::-webkit-scrollbar-thumb {
  background: var(--chatbot-border);
  border-radius: 2px;
}

.chatbot-messages::-webkit-scrollbar-thumb:hover {
  background: var(--chatbot-text-muted);
}

/* Messages */
.message {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: messageSlide 0.3s ease-out;
}

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

.user-message {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.bot-message {
  align-self: flex-start;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 2px;
}

.user-message .message-avatar {
  background: var(--chatbot-text-secondary);
  color: var(--chatbot-bg-primary);
}

.bot-message .message-avatar {
  background: var(--chatbot-accent);
  color: white;
}

.message-avatar svg {
  width: 16px;
  height: 16px;
}

.message-content {
  background: var(--chatbot-bg-secondary);
  padding: 12px 14px;
  border-radius: var(--radius-md, 8px);
  position: relative;
  word-wrap: break-word;
}

.user-message .message-content {
  background: var(--chatbot-accent);
  color: white;
  border-bottom-right-radius: 4px;
}

.bot-message .message-content {
  border-bottom-left-radius: 4px;
}

.message-content p {
  margin: 0;
  color: inherit;
  line-height: 1.5;
}

.message-content strong {
  font-weight: 600;
}

.message-content em {
  font-style: italic;
}

.message-time {
  display: block;
  font-size: 11px;
  color: var(--chatbot-text-muted);
  margin-top: 4px;
  opacity: 0.7;
}

.user-message .message-time {
  color: rgba(255, 255, 255, 0.7);
  text-align: right;
}

.message-provider {
  display: inline-block;
  font-size: 10px;
  color: var(--chatbot-text-muted);
  background: var(--chatbot-bg-input);
  padding: 2px 6px;
  border-radius: 3px;
  margin-left: 8px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

/* System Messages */
.system-message {
  align-self: center;
  max-width: 100%;
}

.system-message .message-content {
  background: var(--chatbot-bg-input);
  color: var(--chatbot-text-secondary);
  text-align: center;
  font-size: 13px;
  border-radius: var(--radius-lg, 12px);
}

.system-message.error .message-content {
  background: rgba(239, 68, 68, 0.1);
  color: var(--chatbot-error);
  border: 1px solid rgba(239, 68, 68, 0.2);
}

.system-message.escalation .message-content {
  background: rgba(245, 158, 11, 0.1);
  color: var(--chatbot-warning);
  border: 1px solid rgba(245, 158, 11, 0.2);
}

/* Input Container */
.chatbot-input-container {
  border-top: 1px solid var(--chatbot-border);
  padding: 16px;
  background: var(--chatbot-bg-primary);
  border-radius: 0 0 var(--radius-lg, 12px) var(--radius-lg, 12px);
}

.chatbot-input-wrapper {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  background: var(--chatbot-bg-input);
  border-radius: var(--radius-lg, 12px);
  border: 1px solid var(--chatbot-border);
  padding: 8px;
  transition: border-color 0.2s ease;
}

.chatbot-input-wrapper:focus-within {
  border-color: var(--chatbot-accent);
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.1);
}

.chatbot-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--chatbot-text-primary);
  font-size: 14px;
  line-height: 1.4;
  padding: 6px 0;
  resize: none;
  min-height: 20px;
  max-height: 80px;
}

.chatbot-input::placeholder {
  color: var(--chatbot-text-muted);
}

.chatbot-send {
  width: 36px;
  height: 36px;
  border: none;
  background: var(--chatbot-accent);
  color: white;
  border-radius: var(--radius-md, 8px);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--chatbot-transition);
  flex-shrink: 0;
}

.chatbot-send:hover {
  background: var(--chatbot-accent-hover);
  transform: scale(1.05);
}

.chatbot-send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.chatbot-send svg {
  width: 18px;
  height: 18px;
}

/* Typing Indicator */
.chatbot-typing {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  color: var(--chatbot-text-muted);
  font-size: 13px;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-10px);
  transition: all 0.3s ease;
}

.chatbot-typing.visible {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.typing-indicator {
  display: flex;
  gap: 3px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  background: var(--chatbot-text-muted);
  border-radius: 50%;
  animation: typingBounce 1.4s ease-in-out infinite;
}

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

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

@keyframes typingBounce {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

/* Welcome Message */
.welcome-message .message {
  animation: none;
  opacity: 0.9;
}

.welcome-message .message-content {
  background: linear-gradient(135deg, var(--chatbot-accent), var(--chatbot-accent-hover));
  color: white;
}

/* Mobile Responsiveness */
/* Handle mobile browser address bar changes */
@supports (height: 100dvh) {
  @media (max-width: 768px) {
    .copilot-chatbot-window {
      /* Use dynamic viewport height for better mobile browser support */
      height: calc(100dvh - 140px) !important;
    }
  }
  
  @media (max-width: 480px) {
    .copilot-chatbot-window {
      height: calc(100dvh - 120px) !important;
    }
  }
}

@media (max-width: 768px) {
  .copilot-chatbot-window {
    position: fixed;
    bottom: 80px;
    left: 15px;
    right: 15px;
    width: auto;
    /* Fallback for older browsers */
    height: calc(100vh - 140px);
    /* Modern viewport units that account for mobile browser UI */
    height: calc(100dvh - 140px); /* Dynamic viewport height */
    max-height: calc(100svh - 140px); /* Small viewport height as safety */
    border-radius: var(--radius-lg, 12px) var(--radius-lg, 12px) 0 0;
    max-width: none; /* Override the desktop max-width */
    min-width: 320px; /* Ensure minimum readable width */
    z-index: var(--chatbot-z-window);
  }
  
  .copilot-chatbot-toggle {
    width: 56px;
    height: 56px;
    position: fixed;
    bottom: 15px;
    right: 15px;
    z-index: var(--chatbot-z-toggle);
  }
  
  .copilot-chatbot-toggle svg {
    width: 22px;
    height: 22px;
  }
  
  .message {
    max-width: 95%; /* Allow more width for messages on mobile */
  }
  
  .message-content {
    font-size: 14px; /* Ensure readable text size */
    line-height: 1.4;
  }
  
  .chatbot-header {
    padding: 12px 16px;
  }
  
  .chatbot-messages {
    padding: 12px;
  }
  
  .chatbot-input-container {
    padding: 12px;
  }
  
  .chatbot-input {
    font-size: 16px; /* Prevent zoom on iOS */
    min-height: 44px; /* Touch target compliance */
  }
  
  .chatbot-send {
    min-width: 44px;
    min-height: 44px;
  }
}

@media (max-width: 480px) {
  .copilot-chatbot-window {
    position: fixed;
    bottom: 70px;
    left: 10px;
    right: 10px;
    width: auto;
    /* Fallback for older browsers */
    height: calc(100vh - 120px);
    /* Modern viewport units that account for mobile browser UI */
    height: calc(100dvh - 120px); /* Dynamic viewport height */
    max-height: calc(100svh - 120px); /* Small viewport height as safety */
    min-width: 300px; /* Ensure readability on very small screens */
    z-index: var(--chatbot-z-window);
  }
  
  .message-content {
    padding: 10px 12px;
  }
  
  .chatbot-header-info h3 {
    font-size: 15px;
  }
  
  .chatbot-status {
    font-size: 11px;
  }
  
  .copilot-chatbot-toggle {
    position: fixed;
    bottom: 10px;
    right: 10px;
    z-index: var(--chatbot-z-toggle);
  }
  
  .message {
    max-width: 96%; /* Even more width on very small screens */
  }
  
  .chatbot-input {
    font-size: 16px; /* Prevent zoom on iOS */
  }
}

/* Animation for better UX */
.copilot-chatbot-container.chatbot-visible .copilot-chatbot-toggle {
  background: var(--chatbot-bg-secondary);
  color: var(--chatbot-text-primary);
}

.copilot-chatbot-container.chatbot-visible .copilot-chatbot-toggle:hover {
  background: var(--chatbot-bg-input);
  box-shadow: var(--chatbot-shadow-toggle);
  transform: scale(1.05);
}

/* Focus states for accessibility */
.copilot-chatbot-toggle:focus,
.chatbot-close:focus,
.chatbot-send:focus {
  outline: 2px solid var(--chatbot-accent);
  outline-offset: 2px;
}

.chatbot-input:focus {
  outline: none; /* Handled by wrapper */
}

/* High contrast mode support */
@media (prefers-contrast: high) {
  .copilot-chatbot-window {
    border: 2px solid var(--chatbot-text-primary);
  }
  
  .message-content {
    border: 1px solid var(--chatbot-border);
  }
  
  .user-message .message-content {
    border: 1px solid var(--chatbot-accent-hover);
  }
}

/* Markdown formatting styles for bot messages */
.message-content h1.message-h1,
.message-content h2.message-h2,
.message-content h3.message-h3 {
  margin: 12px 0 8px 0;
  font-weight: 600;
  line-height: 1.3;
  color: inherit;
}

.message-content h1.message-h1 {
  font-size: 18px;
  border-bottom: 1px solid var(--chatbot-border);
  padding-bottom: 4px;
}

.message-content h2.message-h2 {
  font-size: 16px;
}

.message-content h3.message-h3 {
  font-size: 14px;
}

.message-content code.inline-code {
  background: var(--chatbot-bg-input);
  color: var(--chatbot-text-primary);
  padding: 2px 4px;
  border-radius: 3px;
  font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 12px;
  border: 1px solid var(--chatbot-border);
}

.user-message .message-content code.inline-code {
  background: rgba(255, 255, 255, 0.15);
  color: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.message-content pre.code-block {
  background: var(--chatbot-bg-input);
  border: 1px solid var(--chatbot-border);
  border-radius: var(--radius-md, 8px);
  padding: 12px;
  margin: 8px 0;
  overflow-x: auto;
  font-family: 'Menlo', 'Monaco', 'Courier New', monospace;
  font-size: 12px;
  line-height: 1.4;
  position: relative;
}

.message-content pre.code-block[data-language]:not([data-language=""]):before {
  content: attr(data-language);
  position: absolute;
  top: 4px;
  right: 8px;
  font-size: 10px;
  color: var(--chatbot-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.user-message .message-content pre.code-block {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.message-content ul.message-ul,
.message-content ol.message-ol {
  margin: 8px 0;
  padding-left: 20px;
}

.message-content li.message-li,
.message-content li.message-li-ordered {
  margin: 4px 0;
  line-height: 1.4;
}

.message-content a.message-link {
  color: var(--chatbot-accent);
  text-decoration: underline;
  text-decoration-color: rgba(0, 123, 255, 0.5);
  transition: color 0.2s ease;
}

.message-content a.message-link:hover {
  color: var(--chatbot-accent-hover);
  text-decoration-color: var(--chatbot-accent-hover);
}

.user-message .message-content a.message-link {
  color: rgba(255, 255, 255, 0.9);
  text-decoration-color: rgba(255, 255, 255, 0.5);
}

.user-message .message-content a.message-link:hover {
  color: rgba(255, 255, 255, 1);
  text-decoration-color: rgba(255, 255, 255, 0.8);
}

.message-content blockquote.message-blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid var(--chatbot-accent);
  background: var(--chatbot-bg-input);
  border-radius: 0 var(--radius-sm, 6px) var(--radius-sm, 6px) 0;
  font-style: italic;
  color: var(--chatbot-text-secondary);
}

.user-message .message-content blockquote.message-blockquote {
  background: rgba(255, 255, 255, 0.1);
  border-left: 3px solid rgba(255, 255, 255, 0.6);
  color: rgba(255, 255, 255, 0.9);
}

.message-content hr.message-hr {
  margin: 12px 0;
  border: none;
  border-top: 1px solid var(--chatbot-border);
  opacity: 0.5;
}

.user-message .message-content hr.message-hr {
  border-top: 1px solid rgba(255, 255, 255, 0.3);
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
  .copilot-chatbot-window,
  .copilot-chatbot-toggle,
  .copilot-chatbot-toggle svg,
  .message,
  .chatbot-typing {
    animation: none;
    transition: none;
  }
  
  .typing-indicator span {
    animation: none;
    opacity: 0.7;
  }
}