/* =========================================================
   B-SiteAgent AI · 前端模拟 AI 聊天窗口（客服 / 导购 通用）
   右下角浮动按钮 + 聊天面板，支持客户端直连 DeepSeek 流式回答，
   失败时回退到前端模拟应答。使用 var(--x, fallback) 以兼容父级 styles.css。
   ========================================================= */

#ai-chat-widget { display: none; } /* 仅作挂载标记，本体由 JS 注入到 body */

/* ---------- 浮动按钮 ---------- */
.cw-fab {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 9999;
  width: 60px;
  height: 60px;
  border: none;
  border-radius: 50%;
  background: var(--grad, linear-gradient(135deg, #5b6bff 0%, #3b8cff 100%));
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: var(--shadow-lg, 0 26px 80px rgba(24,39,75,.16));
  transition: transform .25s var(--ease, cubic-bezier(.16,1,.3,1)), box-shadow .25s ease;
}
.cw-fab:hover { transform: translateY(-3px) scale(1.04); }
.cw-fab:active { transform: translateY(0) scale(.98); }
.cw-fab svg { width: 28px; height: 28px; }
.cw-fab::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: 0 0 0 0 color-mix(in srgb, var(--brand, #4f5dff) 55%, transparent);
  animation: cw-pulse 2.6s var(--ease, ease) infinite;
}
@keyframes cw-pulse {
  0%   { box-shadow: 0 0 0 0 color-mix(in srgb, var(--brand, #4f5dff) 50%, transparent); }
  70%  { box-shadow: 0 0 0 16px color-mix(in srgb, var(--brand, #4f5dff) 0%, transparent); }
  100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--brand, #4f5dff) 0%, transparent); }
}
.cw-fab .cw-fab-tip {
  position: absolute;
  right: 72px;
  bottom: 14px;
  white-space: nowrap;
  background: var(--ink, #0f1b33);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 7px 11px;
  border-radius: 10px;
  opacity: 0;
  transform: translateX(6px);
  pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.cw-fab:hover .cw-fab-tip { opacity: 1; transform: translateX(0); }

/* ---------- 聊天面板 ---------- */
.cw-panel {
  position: fixed;
  right: 24px;
  bottom: 96px;
  z-index: 9999;
  width: 384px;
  height: 600px;
  max-height: calc(100vh - 120px);
  display: flex;
  flex-direction: column;
  background: var(--surface, #fff);
  border: 1px solid var(--line, #e7ebf2);
  border-radius: var(--radius, 16px);
  box-shadow: var(--shadow-lg, 0 26px 80px rgba(24,39,75,.16));
  overflow: hidden;
  opacity: 0;
  visibility: hidden;
  transform: translateY(16px) scale(.98);
  transform-origin: bottom right;
  transition: opacity .26s var(--ease, ease), transform .26s var(--ease, ease), visibility .26s;
  font-family: var(--font-ui, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif);
}
.cw-panel.cw-open { opacity: 1; visibility: visible; transform: translateY(0) scale(1); }

/* 头部 */
.cw-head {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 14px 16px;
  background: var(--grad, linear-gradient(135deg, #5b6bff 0%, #3b8cff 100%));
  color: #fff;
}
.cw-avatar {
  width: 38px; height: 38px;
  border-radius: 11px;
  background: rgba(255,255,255,.18);
  display: grid; place-items: center;
  font-weight: 800; font-size: 16px;
  flex: 0 0 auto;
}
.cw-head-text { flex: 1 1 auto; min-width: 0; }
.cw-title { font-size: 15px; font-weight: 700; line-height: 1.2; }
.cw-sub { font-size: 11.5px; opacity: .85; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cw-head-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.cw-icon-btn {
  width: 32px; height: 32px;
  border: none; border-radius: 9px;
  background: rgba(255,255,255,.14);
  color: #fff; cursor: pointer;
  display: grid; place-items: center;
  transition: background .18s ease;
}
.cw-icon-btn:hover { background: rgba(255,255,255,.28); }
.cw-icon-btn svg { width: 18px; height: 18px; }

/* 消息区 */
.cw-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  background: var(--surface-soft, #f8fafd);
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}
.cw-body::-webkit-scrollbar { width: 8px; }
.cw-body::-webkit-scrollbar-thumb { background: var(--line, #e7ebf2); border-radius: 8px; }

.cw-msg { display: flex; gap: 9px; align-items: flex-end; max-width: 100%; }
.cw-msg.cw-ai { align-self: flex-start; }
.cw-msg.cw-user { align-self: flex-end; flex-direction: row-reverse; }
.cw-msg-avatar {
  width: 30px; height: 30px; border-radius: 9px; flex: 0 0 auto;
  background: var(--grad, linear-gradient(135deg, #5b6bff, #3b8cff));
  color: #fff; display: grid; place-items: center; font-size: 13px; font-weight: 700;
}
.cw-bubble {
  max-width: 80%;
  padding: 10px 13px;
  border-radius: 14px;
  font-size: 13.5px;
  line-height: 1.65;
  word-wrap: break-word;
  white-space: normal;
}
.cw-ai .cw-bubble {
  background: var(--surface, #fff);
  border: 1px solid var(--line, #e7ebf2);
  color: var(--ink, #0f1b33);
  border-bottom-left-radius: 5px;
}
.cw-user .cw-bubble {
  background: var(--brand, #4f5dff);
  color: #fff;
  border-bottom-right-radius: 5px;
}
/* AI 气泡内的 Markdown 排版（加粗 / 列表 / 标题 / 引用 / 代码） */
.cw-bubble p { margin: 0 0 7px; }
.cw-bubble > *:first-child { margin-top: 0; }
.cw-bubble > *:last-child { margin-bottom: 0; }
.cw-bubble b,
.cw-bubble strong { font-weight: 700; color: var(--ink, #0f1b33); }
.cw-bubble em { font-style: normal; font-weight: 600; color: var(--brand, #4f5dff); }
.cw-bubble ul,
.cw-bubble ol { margin: 4px 0 7px; padding-left: 19px; }
.cw-bubble ul { list-style: none; padding-left: 15px; }
.cw-bubble ul > li { position: relative; padding-left: 13px; }
.cw-bubble ul > li::before {
  content: ''; position: absolute; left: 0; top: .68em;
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--brand, #4f5dff); opacity: .65;
}
.cw-bubble ol { list-style: decimal; }
.cw-bubble ol > li::marker { color: var(--brand, #4f5dff); font-weight: 700; }
.cw-bubble li { margin: 3px 0; }
.cw-bubble li > ul,
.cw-bubble li > ol { margin: 3px 0 0; }
.cw-bubble .cw-h {
  margin: 9px 0 5px; font-size: 14px; font-weight: 800;
  line-height: 1.45; color: var(--ink, #0f1b33);
}
.cw-bubble blockquote {
  margin: 7px 0; padding: 6px 10px;
  border-left: 3px solid var(--brand, #4f5dff);
  background: var(--surface-soft, #f8fafd);
  border-radius: 0 8px 8px 0;
  color: var(--ink-soft, #4a5568);
}
.cw-bubble hr {
  margin: 9px 0; border: 0; height: 1px;
  background: var(--line, #e7ebf2);
}
.cw-bubble a { color: var(--brand, #4f5dff); font-weight: 600; text-decoration: underline; }
.cw-bubble code {
  font-family: var(--font-code, ui-monospace, Menlo, Consolas, monospace);
  background: var(--line-soft, #eef1f6);
  padding: 1px 5px; border-radius: 5px; font-size: 12.5px;
}
.cw-user .cw-bubble a { color: #fff; }
.cw-user .cw-bubble code { background: rgba(255, 255, 255, .2); color: #fff; }

/* 正在输入 */
.cw-typing { display: inline-flex; gap: 4px; align-items: center; padding: 4px 2px; }
.cw-typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--muted, #8a93a3);
  animation: cw-blink 1.3s infinite ease-in-out;
}
.cw-typing span:nth-child(2) { animation-delay: .2s; }
.cw-typing span:nth-child(3) { animation-delay: .4s; }
@keyframes cw-blink { 0%, 60%, 100% { opacity: .25; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

/* 快捷回复 */
.cw-quick {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 10px 16px 4px;
  background: var(--surface-soft, #f8fafd);
  border-top: 1px solid var(--line-soft, #eef1f6);
}
.cw-chip {
  border: 1px solid color-mix(in srgb, var(--brand, #4f5dff) 35%, var(--line, #e7ebf2));
  background: var(--surface, #fff);
  color: var(--brand-dark, #3346d6);
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 12px;
  border-radius: 999px;
  cursor: pointer;
  transition: background .16s ease, transform .16s ease;
}
.cw-chip:hover { background: var(--grad-soft, rgba(91,107,255,.12)); transform: translateY(-1px); }
.cw-chip:active { transform: translateY(0); }

/* 底部输入 */
.cw-foot {
  border-top: 1px solid var(--line, #e7ebf2);
  background: var(--surface, #fff);
  padding: 10px 12px 12px;
}
.cw-input-row { display: flex; align-items: flex-end; gap: 8px; }
.cw-input {
  flex: 1 1 auto;
  resize: none;
  border: 1px solid var(--line, #e7ebf2);
  border-radius: 12px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 13.5px;
  line-height: 1.5;
  color: var(--ink, #0f1b33);
  background: var(--surface-soft, #f8fafd);
  max-height: 96px;
  outline: none;
  transition: border-color .16s ease, background .16s ease;
}
.cw-input:focus { border-color: var(--brand, #4f5dff); background: var(--surface, #fff); }
.cw-send {
  flex: 0 0 auto;
  width: 40px; height: 40px;
  border: none; border-radius: 12px;
  background: var(--brand, #4f5dff);
  color: #fff; cursor: pointer;
  display: grid; place-items: center;
  transition: background .16s ease, transform .16s ease, opacity .16s ease;
}
.cw-send:hover { background: var(--brand-dark, #3346d6); }
.cw-send:disabled { opacity: .5; cursor: not-allowed; }
.cw-send svg { width: 20px; height: 20px; }
.cw-foot-note {
  font-size: 10.5px;
  color: var(--muted, #8a93a3);
  margin: 8px 2px 0;
  display: flex; align-items: center; gap: 5px;
}
.cw-foot-note a { color: var(--brand-dark, #3346d6); font-weight: 600; cursor: pointer; }

/* ---------- 移动端 ---------- */
@media (max-width: 560px) {
  .cw-fab { right: 14px; bottom: 14px; width: 54px; height: 54px; }
  .cw-panel {
    right: 10px; left: 10px; bottom: 80px;
    width: auto; height: min(78vh, 620px);
    max-height: calc(100vh - 96px);
  }
  .cw-bubble { max-width: 86%; }
}
