/* ============================================================
   AI 问答小助手（可拖动悬浮球）
   - 悬浮球可自由拖动，位置自动记忆（localStorage）
   - 点击打开聊天面板，流式输出回答
   - ≤768px 移动端自动适配
   ============================================================ */

:root {
  --ai-primary: #1565C0;
  --ai-primary-dark: #0D47A1;
  --ai-bg: #ffffff;
  --ai-text: #1F2937;
  --ai-text-light: #6B7280;
  --ai-border: #E5E7EB;
  --ai-bubble-user: #1565C0;
  --ai-bubble-ai: #F1F5F9;
}

/* ---------- 悬浮球 ---------- */
.ai-fab {
  position: fixed;
  z-index: 9999;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  cursor: grab;
  background: linear-gradient(135deg, var(--ai-primary), var(--ai-primary-dark));
  color: #fff;
  box-shadow: 0 6px 20px rgba(21, 101, 192, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;           /* 拖动时禁止页面滚动 */
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}
.ai-fab:hover { transform: scale(1.08); }
.ai-fab:active { cursor: grabbing; }
.ai-fab svg { width: 26px; height: 26px; pointer-events: none; }
.ai-fab.dragging { transition: none; transform: scale(1.05); }

/* 拖动中禁用全局文本选中，避免误选页面内容 */
body.ai-dragging { user-select: none; -webkit-user-select: none; }

/* ---------- 聊天面板 ---------- */
.ai-panel {
  position: fixed;
  z-index: 9999;
  width: 360px;
  max-width: calc(100vw - 24px);
  height: 500px;
  max-height: calc(100vh - 100px);
  background: var(--ai-bg);
  border-radius: 16px;
  box-shadow: 0 12px 48px rgba(15, 40, 80, 0.25);
  border: 1px solid var(--ai-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.97);
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
}
.ai-panel.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

/* 面板头部 */
.ai-header {
  background: linear-gradient(135deg, var(--ai-primary), var(--ai-primary-dark));
  color: #fff;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.ai-header-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.ai-header-avatar svg { width: 20px; height: 20px; }
.ai-header-info { flex: 1; min-width: 0; }
.ai-header-info strong { display: block; font-size: 15px; }
.ai-header-info span { font-size: 12px; opacity: 0.85; }
.ai-header-btn {
  width: 30px; height: 30px;
  border: none; border-radius: 8px;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}
.ai-header-btn:hover { background: rgba(255, 255, 255, 0.3); }
.ai-header-btn svg { width: 16px; height: 16px; }

/* 消息区 */
.ai-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px;
  background: #F8FAFC;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.ai-msg { display: flex; gap: 8px; max-width: 100%; }
.ai-msg.user { flex-direction: row-reverse; }
.ai-msg-avatar {
  width: 28px; height: 28px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px;
  color: #fff;
  background: var(--ai-primary);
  margin-top: 2px;
}
.ai-msg.user .ai-msg-avatar { background: #64748B; }
.ai-msg-avatar svg { width: 15px; height: 15px; }
.ai-msg-bubble {
  max-width: 78%;
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.6;
  word-break: break-word;
  white-space: pre-wrap;
  color: var(--ai-text);
}
.ai-msg.ai .ai-msg-bubble { background: var(--ai-bubble-ai); border-top-left-radius: 4px; }
.ai-msg.user .ai-msg-bubble { background: var(--ai-bubble-user); color: #fff; border-top-right-radius: 4px; }
.ai-msg-bubble.error { background: #FEF2F2; color: #B91C1C; border: 1px solid #FECACA; }
.ai-msg-bubble .ai-cursor {
  display: inline-block;
  width: 7px; height: 14px;
  background: var(--ai-primary);
  vertical-align: -2px;
  margin-left: 2px;
  animation: ai-blink 0.8s infinite;
}
@keyframes ai-blink { 50% { opacity: 0; } }

/* 快捷提问 */
.ai-quick {
  padding: 8px 14px 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex-shrink: 0;
}
.ai-quick button {
  border: 1px solid var(--ai-primary);
  color: var(--ai-primary);
  background: #fff;
  border-radius: 14px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.ai-quick button:hover { background: var(--ai-primary); color: #fff; }

/* 输入区 */
.ai-input-row {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--ai-border);
  background: #fff;
  flex-shrink: 0;
}
.ai-input-row textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--ai-border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  height: 42px;
  max-height: 100px;
  outline: none;
  transition: border-color 0.15s;
}
.ai-input-row textarea:focus { border-color: var(--ai-primary); }
.ai-send-btn {
  width: 42px; height: 42px;
  border: none;
  border-radius: 10px;
  background: var(--ai-primary);
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}
.ai-send-btn:hover { background: var(--ai-primary-dark); }
.ai-send-btn:disabled { background: #B0BEC5; cursor: not-allowed; }
.ai-send-btn svg { width: 18px; height: 18px; }

.ai-footnote {
  text-align: center;
  font-size: 11px;
  color: #94A3B8;
  padding: 0 0 6px;
  background: #fff;
  flex-shrink: 0;
}

/* ---------- 移动端适配 ---------- */
@media (max-width: 768px) {
  .ai-panel {
    width: calc(100vw - 20px);
    height: min(480px, calc(100dvh - 90px));
    right: 10px !important;
    bottom: 86px !important;
    left: auto !important;
    top: auto !important;
  }
  .ai-fab { width: 52px; height: 52px; }
}
