/* ============================================
   AI 客服悬浮聊天（全站）
   R4「星蓝」：跟随主题 token，深蓝主色、白色面板。
   ============================================ */
.ai-chat-fab {
  position: fixed; right: 22px; bottom: 24px; z-index: 998;
  width: 54px; height: 54px; border-radius: 14px; border: 0; cursor: pointer;
  background: var(--accent, #3D6FE0);
  box-shadow: 0 2px 8px rgba(22, 35, 60, .22);
  display: flex; align-items: center; justify-content: center;
  transition: background-color .15s ease, box-shadow .15s ease, transform .15s ease;
  touch-action: none;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.ai-chat-fab:hover { background: var(--accent-strong, #2A54BE); box-shadow: 0 4px 14px rgba(22, 35, 60, .28); transform: translateY(-1px); }
.ai-chat-fab svg { width: 25px; height: 25px; stroke: var(--on-accent, #FFFFFF); }
.ai-chat-fab .fab-pulse { display: none; }
.ai-chat-fab .fab-badge {
  position: absolute; top: -3px; right: -3px; width: 14px; height: 14px; border-radius: 50%;
  background: var(--danger, #D6504C); border: 2px solid var(--bg, #EEF2F9);
}
.ai-chat-fab.ai-open .fab-pulse { display: none; }

/* 面板 */
.ai-chat-panel {
  position: fixed; right: 22px; bottom: 90px; z-index: 999;
  width: 380px; max-width: calc(100vw - 32px);
  height: 560px; max-height: calc(100vh - 120px);
  border-radius: 14px; overflow: hidden;
  display: flex; flex-direction: column;
  background: var(--surface, #FFFFFF);
  border: 1px solid var(--border-2, #C2CEE4);
  box-shadow: var(--shadow-lg, 0 18px 44px -14px rgba(22, 35, 60, .20));
  transform-origin: bottom right;
  opacity: 0; transform: scale(.96) translateY(8px); pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
}
.ai-chat-panel.ai-open { opacity: 1; transform: none; pointer-events: auto; }

.ai-chat-head {
  display: flex; align-items: center; gap: 10px; padding: 13px 15px;
  background: var(--surface, #FFFFFF);
  border-bottom: 1px solid var(--border-2, #C2CEE4);
  flex-shrink: 0;
}
.ai-chat-head .ch-ava {
  width: 34px; height: 34px; border-radius: 9px; flex-shrink: 0;
  background: var(--accent, #3D6FE0);
  display: flex; align-items: center; justify-content: center; color: var(--on-accent, #FFFFFF);
}
.ai-chat-head .ch-ava svg { width: 19px; height: 19px; stroke: var(--on-accent, #FFFFFF); }
.ai-chat-head .ch-title { flex: 1; min-width: 0; }
.ai-chat-head .ch-title b { font-size: 14.5px; font-weight: 800; display: block; line-height: 1.3; color: var(--ink, #16233C); }
.ai-chat-head .ch-title small { font-size: 11px; color: var(--muted, #7C8AA8); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-chat-head select {
  max-width: 158px; font-size: 11.5px; padding: 4px 8px; flex-shrink: 1; min-width: 0; border-radius: 7px;
  background: var(--surface-2, #F3F6FC); color: var(--ink-2, #46557A);
  border: 1px solid var(--border, #DEE6F1); outline: none;
}
.ai-chat-head .ch-ic {
  width: 28px; height: 28px; border-radius: 7px; border: 0; cursor: pointer;
  background: transparent; color: var(--muted, #7C8AA8);
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.ai-chat-head .ch-ic:hover { background: var(--accent-soft, rgba(61, 111, 224, .08)); color: var(--accent, #3D6FE0); }
.ai-chat-head .ch-ic svg { width: 16px; height: 16px; }

.ai-chat-body {
  flex: 1; overflow-y: auto; padding: 14px 15px;
  display: flex; flex-direction: column; gap: 12px;
  scrollbar-width: thin;
  background: var(--bg, #EEF2F9);
}
.ai-chat-body::-webkit-scrollbar { width: 5px; }
.ai-chat-body::-webkit-scrollbar-thumb { background: var(--border-2, #C2CEE4); border-radius: 3px; }

.ch-msg { display: flex; gap: 9px; max-width: 86%; }
.ch-msg.ch-user { align-self: flex-end; flex-direction: row-reverse; }
.ch-msg .ch-bubble {
  padding: 10px 13px; border-radius: 9px; font-size: 13.5px; line-height: 1.62;
  word-break: break-word; white-space: pre-wrap;
}
.ch-msg.ch-ai .ch-bubble {
  background: var(--surface, #FFFFFF); border: 1px solid var(--border, #DEE6F1);
  border-top-left-radius: 3px; color: var(--ink, #16233C);
}
.ch-msg.ch-user .ch-bubble {
  background: var(--accent, #3D6FE0); color: var(--on-accent, #FFFFFF);
  border-top-right-radius: 3px;
}
.ch-msg .ch-ava {
  width: 26px; height: 26px; border-radius: 8px; flex-shrink: 0;
  background: var(--accent, #3D6FE0);
  display: flex; align-items: center; justify-content: center; color: var(--on-accent, #FFFFFF);
}
.ch-msg .ch-ava svg { width: 14px; height: 14px; stroke: var(--on-accent, #FFFFFF); }
.ch-msg.ch-user .ch-ava { background: var(--seal, #1C3D93); }
.ch-msg.ch-typing .ch-bubble { color: var(--muted, #7C8AA8); }
.ch-msg .ch-typing-dot { display: inline-flex; gap: 4px; align-items: center; }
.ch-msg .ch-typing-dot i {
  width: 5px; height: 5px; border-radius: 50%; background: currentColor;
  animation: chBlink 1.2s infinite;
}
.ch-msg .ch-typing-dot i:nth-child(2) { animation-delay: .2s; }
.ch-msg .ch-typing-dot i:nth-child(3) { animation-delay: .4s; }
@keyframes chBlink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.ai-chat-foot {
  flex-shrink: 0; padding: 11px 13px;
  border-top: 1px solid var(--border, #DEE6F1);
  display: flex; align-items: flex-end; gap: 9px;
  background: var(--surface, #FFFFFF);
}
.ai-chat-foot textarea {
  flex: 1; resize: none; height: 40px; max-height: 96px;
  padding: 9px 12px; border-radius: 9px; font-size: 13.5px; line-height: 1.5;
  background: var(--surface-2, #F3F6FC); color: var(--ink, #16233C);
  border: 1px solid var(--border, #DEE6F1); outline: none;
  font-family: inherit;
}
.ai-chat-foot textarea:focus { border-color: var(--accent, #3D6FE0); box-shadow: 0 0 0 3px var(--accent-soft-2, rgba(61, 111, 224, .16)); }
.ai-chat-foot .ch-send {
  width: 42px; height: 40px; border-radius: 9px; border: 0; cursor: pointer;
  background: var(--accent, #3D6FE0); color: var(--on-accent, #FFFFFF);
  display: flex; align-items: center; justify-content: center;
  transition: background-color .15s;
}
.ai-chat-foot .ch-send:hover { background: var(--accent-strong, #2A54BE); }
.ai-chat-foot .ch-send:disabled { opacity: .5; cursor: not-allowed; }
.ai-chat-foot .ch-send svg { width: 17px; height: 17px; stroke: var(--on-accent, #FFFFFF); }

.ch-err { font-size: 12px; color: var(--danger, #D6504C); padding: 4px 2px; }

/* 移动端：面板全屏 + fab 上移到底栏上方安全区（避开 bottom-nav） */
@media (max-width: 600px) {
  .ai-chat-panel {
    right: 12px; bottom: 12px; left: 12px;
    width: auto; height: calc(100dvh - 140px); max-height: none;
    border-radius: 14px;
  }
  /* fab 上移到底栏上方 + 安全区 */
  .ai-chat-fab { right: 16px; bottom: calc(86px + env(safe-area-inset-bottom)); }
  .ai-chat-head { flex-wrap: wrap; gap: 8px; padding: 11px 13px; }
  .ai-chat-head .ch-ava { width: 30px; height: 30px; border-radius: 8px; }
  .ai-chat-head .ch-title { flex: 1 1 auto; }
  .ai-chat-head .ch-title b { font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .ai-chat-head .ch-ic { width: 26px; height: 26px; }
  .ai-chat-head select { order: 5; flex: 1 1 100%; max-width: none; width: 100%; font-size: 12px; padding: 6px 9px; }
}

/* 连接状态与 Markdown 渲染样式 */
#ai-chat-status { font-style: normal; }
#ai-chat-status.st-ok { color: var(--ok, #2E9E6B); }
#ai-chat-status.st-err { color: var(--danger, #D6504C); }
.ch-md-list { margin: 4px 0 4px 18px; padding: 0; list-style: disc; }
.ch-md-list li { margin: 2px 0; }
.ch-md-code { background: var(--bg-2, #E4EAF5); border-radius: 4px; padding: 0 5px; font-family: var(--mono, ui-monospace, monospace); font-size: 12.5px; color: var(--ink, #16233C); }

.ch-md-h { font-weight: 700; margin: 6px 0 2px; color: var(--ink, #16233C); }

