/* ============================================
   Global AI Chat Widget
   ============================================ */

/* ---- Floating Trigger Button ---- */
.chat-fab {
  position: fixed;
  bottom: 80px;
  right: 24px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--ai-accent), #7c6dd8);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 16px rgba(94, 106, 210, 0.4);
  transition: transform 0.2s, box-shadow 0.2s;
  z-index: 1000;
}

.chat-fab:hover {
  transform: scale(1.08);
  box-shadow: 0 6px 24px rgba(94, 106, 210, 0.55);
}

.chat-fab:active { transform: scale(0.96); }

.chat-fab svg { width: 24px; height: 24px; }

.chat-fab .chat-fab-badge {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid var(--bg-primary);
  display: none;
}

/* ---- Chat Panel ---- */
.chat-panel {
  position: fixed;
  bottom: 80px;
  right: 24px;
  width: 400px;
  height: 560px;
  max-height: calc(100vh - 120px);
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  z-index: 1001;
  opacity: 0;
  visibility: hidden;
  transform: translateY(16px) scale(0.96);
  transition: opacity 0.25s, transform 0.25s, visibility 0.25s;
  overflow: hidden;
}

.chat-panel.open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0) scale(1);
}

/* ---- Chat Header ---- */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

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

.chat-header-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--ai-accent), #7c6dd8);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: #fff;
}

.chat-header-info h4 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.chat-header-info span {
  font-size: 11px;
  color: var(--green);
}

.chat-header-actions {
  display: flex;
  gap: 4px;
}

.chat-header-btn {
  background: none;
  border: none;
  color: var(--text-tertiary);
  cursor: pointer;
  padding: 4px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
}

.chat-header-btn:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

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

.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 4px; }

/* Welcome message */
.chat-welcome {
  text-align: center;
  padding: 24px 16px;
}

.chat-welcome-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--ai-accent-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 12px;
  font-size: 28px;
}

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

.chat-welcome p {
  font-size: 13px;
  color: var(--text-tertiary);
  margin: 0;
  line-height: 1.5;
}

.chat-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
  justify-content: center;
}

.chat-suggestion-btn {
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.chat-suggestion-btn:hover {
  border-color: var(--ai-accent);
  color: var(--ai-accent);
  background: var(--ai-accent-dim);
}

/* Message bubbles */
.chat-msg {
  display: flex;
  gap: 8px;
  max-width: 88%;
  animation: chatMsgIn 0.25s ease;
}

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

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

.chat-msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
}

.chat-msg.assistant .chat-msg-avatar {
  background: linear-gradient(135deg, var(--ai-accent), #7c6dd8);
  color: #fff;
}

.chat-msg.user .chat-msg-avatar {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.chat-msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 13px;
  line-height: 1.65;
  word-break: break-word;
}

.chat-msg.assistant .chat-msg-bubble {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-bottom-left-radius: 4px;
}

.chat-msg.user .chat-msg-bubble {
  background: var(--ai-accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

/* Markdown in chat */
.chat-msg-bubble p { margin: 0 0 8px; }
.chat-msg-bubble p:last-child { margin-bottom: 0; }
.chat-msg-bubble code {
  background: rgba(0,0,0,0.3);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: var(--font-mono);
  font-size: 12px;
}
.chat-msg-bubble pre {
  background: rgba(0,0,0,0.3);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 8px 0;
}
.chat-msg-bubble pre code {
  background: none;
  padding: 0;
}
.chat-msg-bubble strong { font-weight: 600; }
.chat-msg-bubble ul, .chat-msg-bubble ol {
  padding-left: 18px;
  margin: 6px 0;
}
.chat-msg-bubble li { margin: 2px 0; }

/* Streaming cursor */
.chat-msg-bubble .typing-cursor {
  display: inline-block;
  width: 2px;
  height: 14px;
  background: var(--ai-accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: cursorBlink 0.8s infinite;
}

@keyframes cursorBlink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

/* ---- Chat Input ---- */
.chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-elevated);
  flex-shrink: 0;
}

.chat-input-wrap {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 22px;
  padding: 4px 4px 4px 16px;
  transition: border-color 0.15s;
}

.chat-input-wrap:focus-within {
  border-color: var(--ai-accent);
}

.chat-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 100px;
  padding: 6px 0;
  font-family: var(--font-ui);
}

.chat-input::placeholder { color: var(--text-tertiary); }

.chat-send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--ai-accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}

.chat-send-btn:hover { background: #6d5fd0; }
.chat-send-btn:active { transform: scale(0.92); }
.chat-send-btn:disabled {
  background: var(--bg-hover);
  color: var(--text-tertiary);
  cursor: not-allowed;
}

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

/* ---- Loading dots ---- */
.chat-loading-dots {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

.chat-loading-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-tertiary);
  animation: dotPulse 1.2s infinite;
}

.chat-loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.chat-loading-dots span:nth-child(3) { animation-delay: 0.4s; }

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

/* ---- Responsive ---- */
@media (max-width: 480px) {
  .chat-panel {
    width: calc(100vw - 16px);
    height: calc(100vh - 100px);
    bottom: 8px;
    right: 8px;
    border-radius: var(--radius-md);
  }

  .chat-fab {
    bottom: 64px;
    right: 16px;
    width: 46px;
    height: 46px;
  }
}
