/* ═══════════════════════════════════════════════════════════════════════════
   AI-CHAT-UI.CSS — Rev Intel AI Chat Drawer Styles
   Modern chatbot UI with proper scroll behavior and polished visuals.
   Uses existing CSS variables from app-v3.html (--accent, --bg, --text, etc.)
   ═══════════════════════════════════════════════════════════════════════════ */

/* ── Floating Trigger Button ─────────────────────────────────────────────── */

.ai-chat-fab {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 900;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 22px;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 50%, #7c3aed 100%);
  color: #fff;
  border: none;
  border-radius: 50px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(99, 102, 241, 0.35), 0 2px 8px rgba(0, 0, 0, 0.25);
  transition: all 0.3s cubic-bezier(0.23, 1, 0.32, 1);
}
.ai-chat-fab:hover {
  transform: translateY(-3px) scale(1.02);
  box-shadow: 0 8px 32px rgba(99, 102, 241, 0.45), 0 4px 12px rgba(0, 0, 0, 0.3);
}
.ai-chat-fab:active {
  transform: translateY(0) scale(0.97);
}
.ai-chat-fab.hidden {
  display: none;
}
.ai-chat-fab-icon {
  font-size: 18px;
  line-height: 1;
}

/* ── Backdrop Overlay ─────────────────────────────────────────────────────── */

.ai-chat-backdrop {
  position: fixed;
  inset: 0;
  z-index: 940;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}
.ai-chat-backdrop.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ── Chat Drawer Container ───────────────────────────────────────────────── */

.ai-chat-drawer {
  position: fixed;
  top: 0;
  right: 0;
  width: 440px;
  min-width: 360px;
  max-width: 90vw;
  height: 100vh;
  height: 100dvh;
  z-index: 950;
  display: flex;
  flex-direction: column;
  background: #0a0f1e;
  border-left: 1px solid rgba(99, 102, 241, 0.12);
  box-shadow: -12px 0 60px rgba(0, 0, 0, 0.6);
  transform: translateX(100%);
  transition: transform 0.35s cubic-bezier(0.23, 1, 0.32, 1);
}
.ai-chat-drawer.open {
  transform: translateX(0);
}
.ai-chat-drawer.wide {
  width: 680px;
}

/* ── Resize Handle (left edge drag) ──────────────────────────────────────── */

.ai-chat-resize-handle {
  position: absolute;
  top: 0;
  left: -4px;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 10;
  background: transparent;
  transition: background 0.2s;
}
.ai-chat-resize-handle:hover,
.ai-chat-resize-handle.active {
  background: rgba(99, 102, 241, 0.3);
}
.ai-chat-resize-handle::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 2px;
  width: 4px;
  height: 40px;
  transform: translateY(-50%);
  border-radius: 2px;
  background: rgba(99, 102, 241, 0.15);
  transition: background 0.2s;
}
.ai-chat-resize-handle:hover::after {
  background: rgba(99, 102, 241, 0.5);
}

/* ── Header ──────────────────────────────────────────────────────────────── */

.ai-chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: linear-gradient(180deg, rgba(20, 24, 44, 0.98) 0%, rgba(15, 18, 35, 0.95) 100%);
  border-bottom: 1px solid rgba(99, 102, 241, 0.1);
  flex-shrink: 0;
}
.ai-chat-header-info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.ai-chat-header-avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.3);
}
.ai-chat-header-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.ai-chat-header-title {
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  display: flex;
  align-items: center;
  gap: 6px;
  line-height: 1.2;
}
.ai-chat-header-title .ai-icon {
  display: none;
}
.ai-chat-header-context {
  font-size: 11px;
  color: rgba(148, 163, 184, 0.7);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.ai-chat-status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
  box-shadow: 0 0 6px rgba(34, 197, 94, 0.5);
}
.ai-chat-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}
.ai-chat-header-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: rgba(148, 163, 184, 0.6);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.15s;
}
.ai-chat-header-btn:hover {
  background: rgba(99, 102, 241, 0.1);
  color: #fff;
}

/* ── Message Area ────────────────────────────────────────────────────────── */

.ai-chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(99, 102, 241, 0.15) transparent;
}
.ai-chat-messages::-webkit-scrollbar {
  width: 5px;
}
.ai-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}
.ai-chat-messages::-webkit-scrollbar-thumb {
  background: rgba(99, 102, 241, 0.15);
  border-radius: 3px;
}
.ai-chat-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(99, 102, 241, 0.3);
}

/* ── Welcome State ───────────────────────────────────────────────────────── */

.ai-chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  padding: 40px 24px;
  color: var(--muted, #94a3b8);
}
.ai-chat-welcome-icon {
  width: 64px;
  height: 64px;
  border-radius: 20px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.15), rgba(139, 92, 246, 0.1));
  border: 1px solid rgba(99, 102, 241, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  margin-bottom: 20px;
}
.ai-chat-welcome h4 {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  margin-bottom: 8px;
}
.ai-chat-welcome p {
  font-size: 13px;
  line-height: 1.6;
  max-width: 300px;
  margin: 0 auto;
  color: rgba(148, 163, 184, 0.7);
}

/* ── Message Groups ──────────────────────────────────────────────────────── */

.ai-chat-msg {
  display: flex;
  gap: 10px;
  max-width: 100%;
  animation: aiChatFadeIn 0.3s ease-out;
  padding: 6px 0;
}
.ai-chat-msg.user {
  flex-direction: row-reverse;
}
.ai-chat-msg.ai {
  flex-direction: row;
}

.ai-chat-msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  flex-shrink: 0;
  margin-top: 2px;
}
.ai-chat-msg.ai .ai-chat-msg-avatar {
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  color: #fff;
  font-size: 12px;
}
.ai-chat-msg.user .ai-chat-msg-avatar {
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent2, #818cf8);
  font-size: 12px;
}

.ai-chat-msg-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  max-width: 85%;
}

.ai-chat-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 13px;
  line-height: 1.6;
  word-break: break-word;
}
.ai-chat-msg.user .ai-chat-bubble {
  background: linear-gradient(135deg, #6366f1, #7c3aed);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 1px 4px rgba(99, 102, 241, 0.2);
}
.ai-chat-msg.ai .ai-chat-bubble {
  background: rgba(30, 33, 58, 0.6);
  color: var(--text, #e2e8f0);
  border: 1px solid rgba(99, 102, 241, 0.08);
  border-bottom-left-radius: 4px;
}

.ai-chat-msg-label {
  font-size: 10px;
  color: rgba(148, 163, 184, 0.5);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 0 2px;
}
.ai-chat-msg.user .ai-chat-msg-label {
  text-align: right;
}

/* ── Markdown Rendering in AI Bubbles ────────────────────────────────────── */

.ai-chat-msg.ai .ai-chat-bubble h2 {
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  margin: 12px 0 6px 0;
}
.ai-chat-msg.ai .ai-chat-bubble h2:first-child {
  margin-top: 0;
}
.ai-chat-msg.ai .ai-chat-bubble h3 {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent2, #818cf8);
  margin: 10px 0 4px 0;
}
.ai-chat-msg.ai .ai-chat-bubble p {
  margin: 0 0 8px 0;
}
.ai-chat-msg.ai .ai-chat-bubble p:last-child {
  margin-bottom: 0;
}
.ai-chat-msg.ai .ai-chat-bubble strong {
  color: #fff;
  font-weight: 600;
}
.ai-chat-msg.ai .ai-chat-bubble em {
  color: var(--accent2, #818cf8);
  font-style: italic;
}
.ai-chat-msg.ai .ai-chat-bubble ul,
.ai-chat-msg.ai .ai-chat-bubble ol {
  margin: 6px 0;
  padding-left: 18px;
}
.ai-chat-msg.ai .ai-chat-bubble li {
  margin-bottom: 4px;
}
.ai-chat-msg.ai .ai-chat-bubble li::marker {
  color: rgba(99, 102, 241, 0.5);
}
.ai-chat-msg.ai .ai-chat-bubble blockquote {
  margin: 8px 0;
  padding: 8px 12px;
  border-left: 3px solid rgba(99, 102, 241, 0.3);
  background: rgba(99, 102, 241, 0.04);
  border-radius: 0 8px 8px 0;
  color: rgba(226, 232, 240, 0.85);
  font-size: 12.5px;
}
.ai-chat-msg.ai .ai-chat-bubble code {
  background: rgba(99, 102, 241, 0.1);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12px;
  font-family: 'SF Mono', Menlo, monospace;
}
.ai-chat-msg.ai .ai-chat-bubble .ai-chat-table-wrap {
  overflow-x: auto;
  margin: 10px 0;
  border-radius: 8px;
  border: 1px solid rgba(99, 102, 241, 0.08);
  scrollbar-width: thin;
  scrollbar-color: rgba(99, 102, 241, 0.2) transparent;
}
.ai-chat-msg.ai .ai-chat-bubble table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11.5px;
  white-space: nowrap;
}
.ai-chat-msg.ai .ai-chat-bubble th {
  text-align: left;
  padding: 7px 10px;
  background: rgba(99, 102, 241, 0.06);
  border-bottom: 1px solid rgba(99, 102, 241, 0.12);
  color: var(--accent2, #818cf8);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 600;
}
.ai-chat-msg.ai .ai-chat-bubble td {
  padding: 6px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
}
.ai-chat-msg.ai .ai-chat-bubble tr:hover td {
  background: rgba(99, 102, 241, 0.03);
}
.ai-chat-msg.ai .ai-chat-bubble hr {
  border: none;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  margin: 12px 0;
}

/* ── Typing / Loading Indicator ────────────────────────────────────────── */

.ai-chat-loading {
  display: flex;
  gap: 10px;
  padding: 6px 0;
  animation: aiChatFadeIn 0.3s ease-out;
}
.ai-chat-loading-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  flex-shrink: 0;
}
.ai-chat-loading-bubble {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 12px 18px;
  background: rgba(30, 33, 58, 0.6);
  border: 1px solid rgba(99, 102, 241, 0.08);
  border-radius: 16px;
  border-bottom-left-radius: 4px;
}
.ai-chat-loading-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(99, 102, 241, 0.6);
  animation: aiChatTyping 1.4s infinite ease-in-out both;
}
.ai-chat-loading-dot:nth-child(1) { animation-delay: 0s; }
.ai-chat-loading-dot:nth-child(2) { animation-delay: 0.2s; }
.ai-chat-loading-dot:nth-child(3) { animation-delay: 0.4s; }

/* ── FAQ Chips ────────────────────────────────────────────────────────────── */

.ai-chat-chips-area {
  flex-shrink: 0;
  border-top: 1px solid rgba(99, 102, 241, 0.06);
  padding: 10px 16px;
  background: rgba(15, 18, 35, 0.8);
}
.ai-chat-chips-label {
  font-size: 10px;
  color: rgba(148, 163, 184, 0.5);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  font-weight: 500;
  margin-bottom: 8px;
}
.ai-chat-chips {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.ai-chat-chips::-webkit-scrollbar {
  display: none;
}
.ai-chat-chip {
  flex-shrink: 0;
  padding: 7px 14px;
  background: rgba(99, 102, 241, 0.06);
  border: 1px solid rgba(99, 102, 241, 0.12);
  border-radius: 20px;
  color: var(--accent2, #818cf8);
  font-size: 11.5px;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.ai-chat-chip:hover {
  background: rgba(99, 102, 241, 0.15);
  border-color: rgba(99, 102, 241, 0.3);
  color: #fff;
  transform: translateY(-1px);
}
.ai-chat-chip:active {
  transform: scale(0.96);
}
.ai-chat-chip.more {
  background: rgba(255, 255, 255, 0.03);
  border-color: rgba(255, 255, 255, 0.08);
  color: rgba(148, 163, 184, 0.6);
}
.ai-chat-chip.more:hover {
  background: rgba(255, 255, 255, 0.06);
  color: var(--text, #e2e8f0);
}

/* ── Input Area ──────────────────────────────────────────────────────────── */

.ai-chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 14px 16px;
  border-top: 1px solid rgba(99, 102, 241, 0.08);
  background: linear-gradient(180deg, rgba(15, 18, 35, 0.95) 0%, rgba(10, 15, 30, 0.98) 100%);
  flex-shrink: 0;
}
.ai-chat-input {
  flex: 1;
  background: rgba(20, 25, 50, 0.8);
  border: 1px solid rgba(99, 102, 241, 0.12);
  color: var(--text, #e2e8f0);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 13px;
  font-family: inherit;
  resize: none;
  min-height: 40px;
  max-height: 120px;
  line-height: 1.45;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.ai-chat-input:focus {
  border-color: rgba(99, 102, 241, 0.35);
  outline: none;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.08);
}
.ai-chat-input::placeholder {
  color: rgba(148, 163, 184, 0.35);
}
.ai-chat-send {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #6366f1, #7c3aed);
  border: none;
  border-radius: 12px;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}
.ai-chat-send:hover {
  filter: brightness(1.15);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}
.ai-chat-send:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  transform: none;
  filter: none;
  box-shadow: none;
}

/* ── Error Message ───────────────────────────────────────────────────────── */

.ai-chat-error {
  display: flex;
  gap: 10px;
  padding: 6px 0;
}
.ai-chat-error-bubble {
  padding: 10px 14px;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.15);
  border-radius: 12px;
  color: #fca5a5;
  font-size: 12.5px;
  line-height: 1.5;
}

/* ── Warmup Indicator ────────────────────────────────────────────────────── */

.ai-chat-warmup {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: rgba(99, 102, 241, 0.04);
  border: 1px solid rgba(99, 102, 241, 0.08);
  border-radius: 10px;
  font-size: 12px;
  color: rgba(129, 140, 248, 0.8);
  align-self: center;
  margin: 4px 0;
}
.ai-chat-warmup-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid rgba(99, 102, 241, 0.15);
  border-top-color: var(--accent, #6366f1);
  border-radius: 50%;
  animation: aiChatSpin 0.8s linear infinite;
}

/* ── Usage Badge ─────────────────────────────────────────────────────────── */

.ai-chat-usage {
  font-size: 10px;
  color: rgba(148, 163, 184, 0.4);
  text-align: left;
  margin-top: 2px;
  padding: 0 2px;
}

/* ── Scroll Anchor ───────────────────────────────────────────────────────── */

.ai-chat-scroll-anchor {
  height: 1px;
  flex-shrink: 0;
}

/* ── Animations ──────────────────────────────────────────────────────────── */

@keyframes aiChatFadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes aiChatTyping {
  0%, 80%, 100% { transform: scale(0.4); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}
@keyframes aiChatSpin {
  to { transform: rotate(360deg); }
}

/* ── Admin Usage Stats ──────────────────────────────────────────────────── */

.ai-usage-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}
.ai-usage-stat {
  background: rgba(99, 102, 241, 0.06);
  border: 1px solid rgba(99, 102, 241, 0.1);
  border-radius: 8px;
  padding: 12px;
  text-align: center;
}
.ai-usage-stat-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent2, #818cf8);
}
.ai-usage-stat-label {
  font-size: 10px;
  color: var(--muted, #94a3b8);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  margin-top: 2px;
}
.ai-usage-recent {
  margin-top: 12px;
  max-height: 240px;
  overflow-y: auto;
}
.ai-usage-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
  font-size: 11.5px;
  color: var(--muted, #94a3b8);
}
.ai-usage-row-query {
  color: var(--text, #e2e8f0);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-right: 8px;
}
.ai-usage-row-meta {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
  font-size: 10px;
}

/* ── Responsive: Full Width on Small Screens ─────────────────────────────── */

@media (max-width: 768px) {
  .ai-chat-drawer,
  .ai-chat-drawer.wide {
    width: 100vw;
  }
  .ai-chat-resize-handle {
    display: none;
  }
  .ai-chat-fab {
    bottom: 16px;
    right: 16px;
    padding: 10px 16px;
    font-size: 12px;
  }
}
