/**
 * AUP ASSISTANT V2 - Modern Clean Interface
 * Light mode, minimal, conversational
 */

/* ============================================
   CSS VARIABLES
   ============================================ */
:root {
  --ai-popup-width: 400px;
  --ai-popup-height: 560px;
  --ai-popup-max-height: min(700px, calc(100vh - 120px));
  --ai-popup-min-width: 340px;
  --ai-popup-min-height: 400px;
  --ai-popup-max-width: 520px;
  --ai-popup-z: 9999;
  
  /* Site blue color palette */
  --ai-accent: #2563eb;
  --ai-accent-light: #eff6ff;
  --ai-accent-dark: #1d4ed8;
  --ai-accent-rgb: 37, 99, 235;
  
  /* Backgrounds */
  --ai-bg: #FAFAFA;
  --ai-bg-chat: #FFFFFF;
  --ai-bg-input: #F7F7F8;
  
  /* Borders */
  --ai-border: #E5E5E5;
  --ai-border-light: #EFEFEF;
  
  /* Text colors */
  --ai-text: #1A1A1A;
  --ai-text-secondary: #555555;
  --ai-text-muted: #888888;
  
  /* Message backgrounds */
  --ai-user-bg: #F3F4F6;
  
  /* Shadow - prominent for visibility */
  --ai-shadow: 
    0 0 0 1px rgba(0, 0, 0, 0.1),
    0 8px 30px rgba(0, 0, 0, 0.12),
    0 20px 60px rgba(0, 0, 0, 0.15);
  
  --ai-radius: 16px;
  --ai-radius-sm: 12px;
  --ai-transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================================
   FLOATING TOGGLE BUTTON
   ============================================ */
.ai-floating-btn {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--ai-accent);
  border: none;
  cursor: pointer;
  z-index: var(--ai-popup-z);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(var(--ai-accent-rgb), 0.35);
  transition: transform var(--ai-transition), box-shadow var(--ai-transition);
}

.ai-floating-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 24px rgba(var(--ai-accent-rgb), 0.45);
}

.ai-floating-btn:active {
  transform: scale(0.95);
}

.ai-floating-btn svg {
  width: 26px;
  height: 26px;
  color: white;
  transition: transform 0.2s ease;
}

.ai-floating-btn.open svg.icon-chat { display: none; }
.ai-floating-btn svg.icon-close { display: none; }
.ai-floating-btn.open svg.icon-close { display: block; }

/* Notification dot */
.ai-notification-dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 12px;
  height: 12px;
  background: #EF4444;
  border-radius: 50%;
  border: 2px solid white;
}
.ai-notification-dot.hidden { display: none; }

/* ============================================
   POPUP CHAT WINDOW
   ============================================ */
.ai-popup-chat {
  position: fixed;
  bottom: 96px;
  right: 24px;
  width: var(--ai-popup-width);
  height: var(--ai-popup-height);
  max-height: var(--ai-popup-max-height);
  min-width: var(--ai-popup-min-width);
  min-height: var(--ai-popup-min-height);
  max-width: var(--ai-popup-max-width);
  background: var(--ai-bg-chat);
  border-radius: var(--ai-radius);
  box-shadow: var(--ai-shadow);
  z-index: calc(var(--ai-popup-z) - 1);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.96);
  pointer-events: none;
  transition: opacity var(--ai-transition), transform var(--ai-transition);
}

.ai-popup-chat.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* ============================================
   RESIZE HANDLES
   ============================================ */
.ai-resize-handle {
  position: absolute;
  background: transparent;
  z-index: 10;
}

.ai-resize-handle-tl { top: 0; left: 0; width: 16px; height: 16px; cursor: nwse-resize; }
.ai-resize-handle-t { top: 0; left: 16px; right: 16px; height: 8px; cursor: ns-resize; }
.ai-resize-handle-tr { top: 0; right: 0; width: 16px; height: 16px; cursor: nesw-resize; }
.ai-resize-handle-l { top: 16px; left: 0; bottom: 16px; width: 8px; cursor: ew-resize; }

.ai-popup-chat.resizing { transition: none; user-select: none; }

/* ============================================
   HEADER
   ============================================ */
.ai-popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--ai-bg-chat);
  border-bottom: 1px solid var(--ai-border-light);
  flex-shrink: 0;
}

.ai-popup-header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.ai-popup-avatar {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ai-accent);
  flex-shrink: 0;
}

.ai-popup-avatar svg {
  width: 24px;
  height: 24px;
}

.ai-popup-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--ai-text);
  margin: 0;
  letter-spacing: -0.01em;
}

.ai-popup-header-right {
  display: flex;
  align-items: center;
  gap: 4px;
}

.ai-popup-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--ai-text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.ai-popup-icon-btn svg { width: 18px; height: 18px; }

.ai-popup-icon-btn:hover {
  background: var(--ai-bg-input);
  color: var(--ai-text-secondary);
}

/* ============================================
   MESSAGES CONTAINER
   ============================================ */
.ai-popup-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--ai-bg-chat);
  /* CRITICAL: Constrain width for text wrapping */
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  min-width: 0; /* Allow flex children to shrink */
}

/* Custom scrollbar */
.ai-popup-messages::-webkit-scrollbar { width: 6px; }
.ai-popup-messages::-webkit-scrollbar-track { background: transparent; }
.ai-popup-messages::-webkit-scrollbar-thumb { 
  background: var(--ai-border); 
  border-radius: 3px; 
}
.ai-popup-messages::-webkit-scrollbar-thumb:hover { background: #CCC; }

/* ============================================
   MESSAGES
   ============================================ */
.ai-message {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
  overflow: hidden;
  animation: message-fade-in 0.25s ease;
}

@keyframes message-fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Message header with label and copy button */
.ai-message-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}

/* Message label */
.ai-message-label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--ai-text-muted);
}

.ai-message-assistant .ai-message-label { color: var(--ai-accent); }

/* Copy button */
.ai-copy-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: var(--ai-text-muted);
  cursor: pointer;
  border-radius: 4px;
  opacity: 0;
  transition: all 0.15s ease;
}

.ai-message:hover .ai-copy-btn {
  opacity: 1;
}

.ai-copy-btn:hover {
  background: var(--ai-bg-input);
  color: var(--ai-text-secondary);
}

.ai-copy-btn.copied {
  color: #10B981;
  opacity: 1;
}

.ai-copy-btn svg {
  width: 14px;
  height: 14px;
}

/* Message content - BULLETPROOF TEXT HANDLING */
.ai-message-content {
  display: block;
  font-size: 15px;
  line-height: 1.5;
  color: var(--ai-text);
  
  /* CRITICAL: Strict width constraints to match bubble */
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
  
  /* Text wrapping - comprehensive multi-browser support */
  word-wrap: break-word;          /* Legacy browsers */
  overflow-wrap: break-word;      /* Modern standard */
  word-break: break-word;         /* Webkit/Blink */
  -webkit-hyphens: auto;          /* Safari hyphenation */
  -ms-hyphens: auto;              /* IE hyphenation */
  hyphens: auto;                  /* Standard hyphenation */
  white-space: pre-wrap;          /* Preserve whitespace, allow wrapping */
  
  /* Prevent overflow completely */
  overflow: hidden;
  overflow-x: hidden;
  text-overflow: ellipsis;        /* Show ellipsis if still overflows */
  
  /* Handle long URLs/strings without spaces */
  -ms-word-break: break-all;      /* IE edge case */
}

/* User messages */
.ai-message-user {
  background: var(--ai-user-bg);
  border-radius: var(--ai-radius-sm);
  padding: 12px 14px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  overflow: hidden;
}

.ai-message-user .ai-message-label { 
  color: var(--ai-text-secondary);
}

.ai-message-user .ai-message-content {
  /* Reduce content width to account for padding */
  width: calc(100%);
  max-width: calc(100%);
}

/* Assistant messages */
.ai-message-assistant {
  padding: 4px 0;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  overflow: hidden;
}

.ai-message-assistant .ai-message-content {
  /* Constrain assistant response text width */
  width: calc(100% - 4px);
  max-width: calc(100% - 4px);
}

/* Message content formatting */
.ai-message-content p { 
  margin: 0 0 10px 0;
  max-width: 100%;
  overflow-wrap: break-word;
  word-break: break-word;
  /* Ensure paragraphs don't create horizontal overflow */
  display: block;
  width: 100%;
}
.ai-message-content p:last-child { 
  margin-bottom: 0; 
}

/* Handle very long words/URLs */
.ai-message-content a {
  word-break: break-all;
  overflow-wrap: anywhere;
}

.ai-message-content ul, 
.ai-message-content ol {
  margin: 10px 0;
  padding-left: 20px;
  max-width: 100%;
  box-sizing: border-box;
  /* Ensure lists don't overflow */
  list-style-position: inside;
}

.ai-message-content li {
  margin: 4px 0;
  color: var(--ai-text);
  max-width: 100%;
  overflow-wrap: break-word;
  word-break: break-word;
  /* Handle nested content */
  display: list-item;
}

/* Handle tables in responses */
.ai-message-content table {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  display: block;
  border-collapse: collapse;
  font-size: 13px;
}

.ai-message-content th,
.ai-message-content td {
  padding: 4px 8px;
  border: 1px solid var(--ai-border);
  word-break: break-word;
}

/* Handle blockquotes */
.ai-message-content blockquote {
  margin: 8px 0;
  padding-left: 12px;
  border-left: 3px solid var(--ai-accent);
  color: var(--ai-text-secondary);
  max-width: 100%;
  overflow-wrap: break-word;
}

/* Handle images in responses */
.ai-message-content img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
}

/* Handle horizontal rules */
.ai-message-content hr {
  border: none;
  border-top: 1px solid var(--ai-border);
  margin: 12px 0;
}

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

.ai-message-content code {
  font-family: 'SF Mono', Monaco, 'Cascadia Code', Consolas, monospace;
  font-size: 13px;
  background: var(--ai-bg-input);
  padding: 2px 6px;
  border-radius: 4px;
  word-break: break-all;
  /* Prevent inline code from causing overflow */
  display: inline-block;
  max-width: 100%;
  overflow-wrap: anywhere;
  vertical-align: bottom;
}

.ai-message-content pre {
  background: var(--ai-bg-input);
  padding: 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 10px 0;
  font-size: 13px;
  white-space: pre-wrap;
  word-wrap: break-word;
  max-width: 100%;
  box-sizing: border-box;
  /* Handle code blocks with long lines */
  overflow-wrap: break-word;
  -webkit-overflow-scrolling: touch;
}

.ai-message-content pre code {
  background: none;
  padding: 0;
  word-break: normal;
}

/* Line breaks from API responses */
.ai-message-content br {
  display: block;
  content: "";
  margin-top: 6px;
}

/* Error message */
.ai-message-error {
  background: #FEF2F2;
  border-radius: var(--ai-radius-sm);
  padding: 12px 14px;
}

.ai-message-error .ai-message-label { color: #B91C1C; }
.ai-message-error .ai-message-content { color: #991B1B; }

.ai-retry-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-top: 10px;
  padding: 7px 14px;
  font-size: 12px;
  font-weight: 500;
  color: #DC2626;
  background: white;
  border: 1px solid #FECACA;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.ai-retry-btn:hover { background: #FEF2F2; border-color: #DC2626; }
.ai-retry-btn svg { width: 13px; height: 13px; }

/* ============================================
   TYPING INDICATOR - Non-bouncing fade dots with 'Thinking' text
   ============================================ */
.ai-typing-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 8px 0;
}

.ai-typing-indicator span {
  width: 8px;
  height: 8px;
  background: var(--ai-accent);
  border-radius: 50%;
  opacity: 0.3;
  animation: typing-fade 1.4s ease-in-out infinite;
}

.ai-typing-indicator span:nth-child(1) { animation-delay: 0s; }
.ai-typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

/* 'Thinking' text label */
.ai-typing-status {
  font-size: 13px;
  color: var(--ai-text-muted);
  margin-left: 6px;
}

/* Fade animation - NO bouncing, just opacity change with slight scale */
@keyframes typing-fade {
  0%, 100% { 
    opacity: 0.3;
    transform: scale(1);
  }
  50% { 
    opacity: 1;
    transform: scale(1.1);
  }
}

/* ============================================
   WELCOME STATE
   ============================================ */
.ai-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 32px 20px;
  text-align: center;
}

.ai-welcome-icon {
  width: 48px;
  height: 48px;
  color: var(--ai-accent);
  margin-bottom: 16px;
}

.ai-welcome-icon svg {
  width: 100%;
  height: 100%;
}

.ai-welcome h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--ai-text);
  margin: 0 0 6px 0;
  letter-spacing: -0.01em;
}

.ai-welcome p {
  font-size: 15px;
  color: var(--ai-text-secondary);
  margin: 0;
  line-height: 1.5;
}

/* ============================================
   QUICK ACTIONS
   ============================================ */
.ai-popup-quick-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  padding: 12px 16px 8px;
  background: var(--ai-bg-chat);
}

.ai-quick-action {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 500;
  color: var(--ai-text-secondary);
  background: var(--ai-bg-input);
  border: none;
  border-radius: 18px;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.ai-quick-action:hover {
  background: var(--ai-accent-light);
  color: var(--ai-accent);
}

.ai-quick-action svg { width: 13px; height: 13px; }

/* ============================================
   INPUT AREA
   ============================================ */
.ai-popup-input-area {
  padding: 12px 16px 16px;
  background: var(--ai-bg-chat);
  border-top: 1px solid var(--ai-border-light);
  flex-shrink: 0;
}

.ai-popup-input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 8px 10px;
  background: var(--ai-bg-input);
  border-radius: 24px;
  border: 1px solid var(--ai-border);
  transition: all 0.15s ease;
}

.ai-popup-input-wrapper:focus-within {
  background: var(--ai-bg-chat);
  border-color: #D1D5DB;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
}

/* Input field - clean, no outlines */
.ai-popup-input {
  flex: 1;
  border: none;
  outline: none !important;
  font-size: 14px;
  line-height: 1.4;
  resize: none;
  max-height: 100px;
  min-height: 20px;
  background: transparent;
  color: var(--ai-text);
  font-family: inherit;
  padding: 4px 2px;
}

.ai-popup-input:focus {
  outline: none !important;
  box-shadow: none !important;
}

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

/* Action buttons */
.ai-input-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

/* Attachment & voice buttons */
.ai-attach-btn,
.ai-voice-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--ai-text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.ai-attach-btn:hover,
.ai-voice-btn:hover {
  background: var(--ai-border-light);
  color: var(--ai-text-secondary);
}

.ai-attach-btn svg,
.ai-voice-btn svg { width: 18px; height: 18px; }

/* Voice recording state */
.ai-voice-btn.recording {
  background: var(--ai-accent);
  color: white;
  animation: pulse-recording 1.5s ease-in-out infinite;
}

@keyframes pulse-recording {
  0%, 100% { box-shadow: 0 0 0 0 rgba(var(--ai-accent-rgb), 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(var(--ai-accent-rgb), 0); }
}

/* Send button - perfect circle with upward arrow */
.ai-popup-send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  min-width: 32px;
  min-height: 32px;
  max-width: 32px;
  max-height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--ai-accent);
  color: white;
  cursor: pointer;
  transition: all 0.15s ease;
  flex-shrink: 0;
  aspect-ratio: 1 / 1;
  box-sizing: border-box;
  padding: 0;
}

.ai-popup-send-btn:hover:not(:disabled) {
  background: var(--ai-accent-dark);
  transform: scale(1.05);
}

.ai-popup-send-btn:disabled {
  background: var(--ai-border);
  cursor: not-allowed;
}

.ai-popup-send-btn svg { 
  width: 16px; 
  height: 16px;
  flex-shrink: 0;
}

/* Hidden file input */
.ai-file-input { display: none; }

/* File preview */
.ai-file-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.ai-file-preview:empty { display: none; margin: 0; }

.ai-file-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--ai-bg-input);
  border: 1px solid var(--ai-border);
  border-radius: 8px;
  font-size: 12px;
  color: var(--ai-text-secondary);
}

.ai-file-item img {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  object-fit: cover;
}

.ai-file-remove {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: none;
  background: var(--ai-border);
  color: var(--ai-text-secondary);
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  margin-left: 2px;
}

.ai-file-remove:hover { background: #EF4444; color: white; }

/* Hint text */
.ai-popup-hint {
  margin: 8px 0 0 0;
  font-size: 11px;
  color: var(--ai-text-muted);
  text-align: center;
}

/* ============================================
   RESPONSIVE - Mobile First
   ============================================ */

/* Small phones */
@media (max-width: 380px) {
  :root {
    --ai-popup-width: calc(100vw - 16px);
    --ai-popup-height: calc(100vh - 100px);
    --ai-popup-max-height: calc(100vh - 100px);
  }
  
  .ai-floating-btn {
    bottom: 12px;
    right: 8px;
    width: 48px;
    height: 48px;
  }
  
  .ai-popup-chat {
    bottom: 70px;
    right: 8px;
    left: 8px;
    width: auto;
    border-radius: 14px;
  }
  
  .ai-popup-header { padding: 12px 14px; }
  .ai-popup-messages { padding: 12px; gap: 12px; }
  .ai-popup-input-area { padding: 10px 12px 14px; }
  .ai-popup-quick-actions { padding: 10px 12px 6px; gap: 4px; }
  .ai-quick-action { padding: 6px 10px; font-size: 11px; }
  .ai-message-content { font-size: 13px; }
}

/* Regular phones */
@media (min-width: 381px) and (max-width: 480px) {
  :root {
    --ai-popup-width: calc(100vw - 20px);
    --ai-popup-height: calc(100vh - 110px);
    --ai-popup-max-height: calc(100vh - 110px);
  }
  
  .ai-floating-btn {
    bottom: 14px;
    right: 10px;
    width: 52px;
    height: 52px;
  }
  
  .ai-popup-chat {
    bottom: 76px;
    right: 10px;
    border-radius: 14px;
  }
  
  .ai-popup-header { padding: 12px 14px; }
  .ai-popup-messages { padding: 14px; }
  .ai-popup-input-area { padding: 10px 14px 14px; }
}

/* Tablets */
@media (min-width: 481px) and (max-width: 768px) {
  :root {
    --ai-popup-width: 380px;
    --ai-popup-height: 520px;
  }
}

/* Desktop */
@media (min-width: 769px) {
  :root {
    --ai-popup-width: 400px;
    --ai-popup-height: 560px;
  }
}

/* Large Desktop */
@media (min-width: 1200px) {
  :root {
    --ai-popup-width: 420px;
    --ai-popup-height: 600px;
  }
}

/* ============================================
   PRINT - Hide
   ============================================ */
@media print {
  .ai-floating-btn,
  .ai-popup-chat { display: none !important; }
}

/* ============================================
   REDUCED MOTION
   ============================================ */
@media (prefers-reduced-motion: reduce) {
  .ai-popup-chat,
  .ai-message,
  .ai-floating-btn { transition-duration: 0.01ms; }
  .ai-typing-indicator span { animation: none; opacity: 1; }
}

/* ============================================
   LEGACY CLEANUP
   ============================================ */
#ai-toggle-btn.ai-toggle-btn { display: none !important; }
