/* ============================================================
   官网客服聊天窗（2026-10-04 重写）
   ------------------------------------------------------------
   风格：微信 / 企业微信经典款 —— 中性灰消息底 + 品牌蓝出站气泡。

   ★★★ 2026-10-04 修掉一个致命 bug：类名整体错配 ★★★
   旧版 CSS 写的是 .ch-avatar / .cm-text / .cf-send / .cf-btn 这一套，
   但 aio-v2.html 与 aio-v2.js 实际用的是
     .chat-head-main / #aioChatTitle / #aioChatStatus / .chat-icon-btn
     .chat-msg.from-* / .chat-role-label / .chat-bubble
     .chat-foot / .chat-input-row / #aioChatInput / .chat-send-btn
   两边对不上 → 消息气泡、发送键、输入行、角色名全是零样式裸元素，
   页面看起来像"一堆没上色的字"。本次把类名口径统一到下面这套，
   HTML 里也全部改成显式 class（不再依赖裸元素默认样式）。

   位置说明：右下角原本有 #stickyCta（免费试用/预约演示），
   聊天窗排在它【上面】（bottom 更大），两者不重叠。
   颜色令牌沿用 aio-v2.css 的 --aio-*，保证与整站一致。
   ============================================================ */

/* ---------- 悬浮气泡按钮 ---------- */
#aioChatFab {
  position: fixed;
  right: 24px;
  bottom: 148px;
  /* 高于 .aio-nav（1000），避免被导航遮挡 */
  z-index: 1080;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 18px 0 15px;
  border: 0;
  border-radius: 24px;
  background: var(--aio-primary);
  color: #fff;
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(51, 112, 255, 0.32);
  opacity: 0;
  transform: scale(0.7) translateY(8px);
  transform-origin: bottom right;
  pointer-events: none;
  transition: opacity .28s ease, transform .28s cubic-bezier(.34,1.3,.64,1), background .18s;
}
#aioChatFab .aio-chat-fab-label { line-height: 1; }
#aioChatFab.show {
  opacity: 1;
  transform: scale(1) translateY(0);
  pointer-events: auto;
}
/* 窗口打开时按钮让位：直接收起而不是 display:none（保留动画） */
#aioChatFab.hidden {
  opacity: 0;
  transform: scale(0.7) translateY(8px);
  pointer-events: none;
}
#aioChatFab:hover { background: var(--aio-primary-dark); transform: scale(1.04) translateY(0); }
#aioChatFab:active { transform: scale(0.98); }

/* 未读小圆点（窗口没打开时只亮一个点，不堆数字） */
#aioChatFab .fab-dot {
  position: absolute;
  top: 4px;
  right: 6px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #E24B4A;
  border: 2px solid #fff;
  display: none;
}
#aioChatFab.has-unread .fab-dot { display: block; }

/* ---------- 聊天窗口 ---------- */
#aioChatWin {
  position: fixed;
  right: 24px;
  bottom: 148px;
  /* ★ 2026-10-06 必须高于 .aio-nav（z-index:1000）：移动端全屏时导航栏
     会盖住客服窗头部右上角的关闭按钮，用户点 × 被导航的汉堡菜单吞掉。 */
  z-index: 1100;
  width: 380px;
  max-width: calc(100vw - 32px);
  height: 560px;
  max-height: calc(100vh - 190px);
  background: #EDEDED;              /* 微信经典浅灰底 */
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(20, 22, 28, 0.20), 0 2px 8px rgba(20, 22, 28, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.07);
  display: none;
  flex-direction: column;
  overflow: hidden;
}
#aioChatWin.open {
  display: flex;
  animation: chatWinIn .24s cubic-bezier(.34,1.2,.64,1);
}
@keyframes chatWinIn {
  from { opacity: 0; transform: translateY(14px) scale(.97); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* ---------- 头部（微信风：深色实底，不花哨渐变） ---------- */
#aioChatWin .chat-head {
  flex: 0 0 auto;
  padding: 0 12px 0 16px;
  height: 54px;
  box-sizing: border-box;
  background: #2E2E2E;              /* 微信深灰头部 */
  color: #fff;
  display: flex;
  align-items: center;
  gap: 10px;
}
#aioChatWin .chat-head-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 1px;
}
#aioChatWin .chat-head #aioChatTitle {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#aioChatWin .chat-head #aioChatStatus {
  font-size: 11.5px;
  line-height: 1.3;
  color: rgba(255, 255, 255, 0.62);
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* 状态前的小圆点：在线=绿 / 断线=灰 */
#aioChatWin .chat-head #aioChatStatus::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #4CD964;
  flex: 0 0 auto;
}
#aioChatWin .chat-head #aioChatStatus.is-offline::before { background: rgba(255,255,255,.35); }
#aioChatWin .chat-head #aioChatStatus.is-busy::before { background: #FFCC00; }

#aioChatWin .chat-head .chat-icon-btn {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: rgba(255, 255, 255, 0.82);
  font-size: 19px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, color .15s;
  font-family: inherit;
}
#aioChatWin .chat-head .chat-icon-btn:hover { background: rgba(255, 255, 255, 0.12); color: #fff; }

/* ---------- 消息区 ---------- */
#aioChatWin .chat-body {
  flex: 1 1 auto;
  /* ★ 关键：flex 子项默认 min-height:auto，消息一多它不肯收缩，
     就不会产生内部滚动条 → 触摸手势穿透到页面，整个窗口跟着一起跑
     （用户 2026-10-04 手机版反馈）。必须显式 min-height:0 让它可收缩。 */
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px 14px 8px;
  background: #EDEDED;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
#aioChatWin .chat-body::-webkit-scrollbar { width: 6px; }
#aioChatWin .chat-body::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.18);
  border-radius: 3px;
}
#aioChatWin .chat-body::-webkit-scrollbar-track { background: transparent; }

/* 时间分隔线 */
#aioChatWin .chat-time-sep {
  align-self: center;
  font-size: 11px;
  color: #8A8A8A;
  background: rgba(0, 0, 0, 0.06);
  padding: 3px 9px;
  border-radius: 4px;
  line-height: 1.5;
}

/* 系统提示（如转人工成功） */
#aioChatWin .chat-sys {
  align-self: center;
  max-width: 82%;
  font-size: 11.5px;
  color: #7A7A7A;
  text-align: center;
  line-height: 1.6;
  padding: 2px 4px;
}

/* 消息行 */
#aioChatWin .chat-msg {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  max-width: 84%;
  animation: chatMsgIn .22s ease;
}
@keyframes chatMsgIn {
  from { opacity: 0; transform: translateY(7px); }
  to   { opacity: 1; transform: translateY(0); }
}
#aioChatWin .chat-msg.from-customer { align-self: flex-end; flex-direction: row-reverse; }
#aioChatWin .chat-msg.from-customer { margin-left: auto; }
#aioChatWin .chat-msg.from-agent,
#aioChatWin .chat-msg.from-ai { align-self: flex-start; margin-right: auto; }

/* 头像：方形圆角，微信风不用圆形 */
#aioChatWin .chat-msg .cm-avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  user-select: none;
}
#aioChatWin .chat-msg.from-customer .cm-avatar { background: #F5A623; }
#aioChatWin .chat-msg.from-agent .cm-avatar { background: #07C160; }   /* 微信绿 = 在线客服 */
#aioChatWin .chat-msg.from-ai .cm-avatar { background: #5B7CB8; }     /* 蓝灰 = AI */

#aioChatWin .chat-msg .cm-col {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
#aioChatWin .chat-msg.from-customer .cm-col { align-items: flex-end; }

#aioChatWin .chat-msg .chat-role-label {
  font-size: 11px;
  color: #8A8A8A;
  line-height: 1.3;
  padding: 0 2px;
  white-space: nowrap;
}
#aioChatWin .chat-msg .chat-role-label .rl-workflow {
  color: #5B7CB8;
  font-size: 10px;
  margin-left: 3px;
}

/* 气泡本体 */
#aioChatWin .chat-msg .chat-bubble {
  padding: 9px 12px;
  border-radius: 5px;
  font-size: 14px;
  line-height: 1.65;
  word-break: break-word;
  white-space: pre-wrap;
  max-width: 100%;
  box-sizing: border-box;
}
/* 入站（客服 / AI）：白底 */
#aioChatWin .chat-msg.from-agent .chat-bubble,
#aioChatWin .chat-msg.from-ai .chat-bubble {
  background: #FFFFFF;
  color: #1F2329;
}
/* 连续同角色的第二条起：不画头像、贴紧上一条（微信就这么省空间） */
#aioChatWin .chat-msg.same-role .cm-avatar { display: none; }
#aioChatWin .chat-msg.from-customer.same-role { margin-top: -8px; }
#aioChatWin .chat-msg.from-agent.same-role,
#aioChatWin .chat-msg.from-ai.same-role { margin-top: -8px; }
/* 同角色续接时把气泡小尖角去掉，只第一颗带角 */
#aioChatWin .chat-msg.same-role .chat-bubble::before { display: none; }
#aioChatWin .chat-msg.same-role .chat-bubble {
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
}

/* 出站（客户自己）：品牌蓝 */
#aioChatWin .chat-msg.from-customer .chat-bubble {
  background: var(--aio-primary);
  color: #fff;
}
/* 气泡小尖角 */
#aioChatWin .chat-msg .chat-bubble::before {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  border: 5px solid transparent;
}
#aioChatWin .chat-msg { position: relative; }
#aioChatWin .chat-msg .cm-col { position: relative; }
/* 入站尖角朝左 */
#aioChatWin .chat-msg.from-agent .chat-bubble::before,
#aioChatWin .chat-msg.from-ai .chat-bubble::before {
  left: -9px;
  top: 11px;
  border-right-color: #FFFFFF;
}
/* 出站尖角朝右 */
#aioChatWin .chat-msg.from-customer .chat-bubble::before {
  right: -9px;
  top: 11px;
  border-left-color: var(--aio-primary);
}

/* 发送中 / 出错提示 */
#aioChatWin .chat-msg.is-pending .chat-bubble { opacity: .62; }
#aioChatWin .chat-msg.is-failed .chat-bubble {
  background: #FFF1F0;
  color: #C62828;
  border: 1px solid #FFCCC7;
}

/* "正在输入"等待动画（2026-10-07）：随机短语 + 三点 */
#aioChatWin .chat-typing {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 12px 12px !important;
}
#aioChatWin .chat-typing-text {
  font-size: 13px;
  color: #8a94a6;
  white-space: nowrap;
  animation: aioChatPhraseFade .25s ease-out;
}
#aioChatWin .chat-typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #B4B4B4;
  animation: chatDot 1.2s infinite ease-in-out;
}
/* 注意：i 前面多了短语 span，nth-child 从 3 起算 */
#aioChatWin .chat-typing i:nth-child(3) { animation-delay: .18s; }
#aioChatWin .chat-typing i:nth-child(4) { animation-delay: .36s; }
@keyframes aioChatPhraseFade {
  from { opacity: .2; transform: translateY(2px); }
  to   { opacity: 1; transform: translateY(0); }
}
@keyframes chatDot {
  0%, 60%, 100% { transform: translateY(0); opacity: .45; }
  30%           { transform: translateY(-4px); opacity: 1; }
}

/* ---------- 输入区 ---------- */
#aioChatWin .chat-foot {
  flex: 0 0 auto;
  padding: 10px 12px 12px;
  background: #F7F7F7;
  border-top: 1px solid #DEDEDE;
}
#aioChatWin .chat-foot .cf-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 9px;
  flex-wrap: wrap;
}
/* 快捷话术按钮 */
#aioChatWin .chat-foot .cf-btn {
  padding: 5px 12px;
  border-radius: 14px;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  border: 1px solid #D5D5D5;
  background: #FFFFFF;
  color: #555;
  cursor: pointer;
  transition: all .16s;
  line-height: 1.5;
}
#aioChatWin .chat-foot .cf-btn:hover {
  border-color: var(--aio-primary);
  color: var(--aio-primary);
  background: #F2F6FF;
}
/* 转人工 */
#aioChatWin .chat-foot .cf-btn.human-btn {
  border-color: #07C160;
  color: #07A050;
  background: #F0FBF3;
}
#aioChatWin .chat-foot .cf-btn.human-btn:hover {
  background: #07C160;
  border-color: #07C160;
  color: #fff;
}
#aioChatWin .chat-foot .cf-btn:disabled { opacity: .5; cursor: not-allowed; }
#aioChatWin .chat-foot .cf-btn:disabled:hover {
  border-color: #D5D5D5; background: #fff; color: #555;
}

/* ---------- 客户资料（称呼/公司/电话，可收起） ---------- */
#aioChatWin .chat-foot .chat-profile {
  background: #FFFFFF;
  border: 1px solid #E4E8EF;
  border-radius: 6px;
  padding: 8px 10px 6px;
  margin-bottom: 9px;
}
#aioChatWin .chat-foot .chat-profile .cp-row {
  display: flex;
  gap: 6px;
  margin-bottom: 6px;
}
#aioChatWin .chat-foot .chat-profile .cp-input {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid #DCDCDC;
  border-radius: 4px;
  padding: 6px 9px;
  font-size: 13px;
  font-family: inherit;
  color: #1F2329;
  background: #fff;
}
#aioChatWin .chat-foot .chat-profile .cp-input.cp-wide {
  flex: 1 1 100%;
  width: 100%;
}
#aioChatWin .chat-foot .chat-profile .cp-input:focus {
  outline: none;
  border-color: var(--aio-primary);
  box-shadow: 0 0 0 2px rgba(51, 112, 255, .10);
}
#aioChatWin .chat-foot .chat-profile .cp-save {
  flex: 0 0 auto;
  white-space: nowrap;
}
#aioChatWin .chat-foot .chat-profile .cp-hint {
  font-size: 11px;
  color: #999;
  line-height: 1.5;
}
#aioChatWin .chat-foot .cf-btn.cp-toggle { color: #555; }

#aioChatWin .chat-foot .chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
#aioChatWin .chat-foot #aioChatInput {
  flex: 1 1 auto;
  min-width: 0;
  border: 1px solid #DCDCDC;
  border-radius: 5px;
  padding: 9px 11px;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  color: #1F2329;
  background: #FFFFFF;
  resize: none;
  height: 38px;
  max-height: 96px;
  overflow-y: auto;
  transition: border-color .16s, box-shadow .16s;
}
#aioChatWin .chat-foot #aioChatInput:focus {
  outline: none;
  border-color: var(--aio-primary);
  box-shadow: 0 0 0 2px rgba(51, 112, 255, .12);
}
#aioChatWin .chat-foot #aioChatInput::placeholder { color: #B2B2B2; }

#aioChatWin .chat-foot .chat-send-btn {
  flex: 0 0 auto;
  width: 68px;
  height: 38px;
  border: 0;
  border-radius: 5px;
  background: var(--aio-primary);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  transition: background .16s, opacity .16s;
}
#aioChatWin .chat-foot .chat-send-btn:hover { background: var(--aio-primary-dark); }
#aioChatWin .chat-foot .chat-send-btn:active { background: var(--aio-primary-darker); }
#aioChatWin .chat-foot .chat-send-btn:disabled {
  background: #C8C8C8;
  cursor: not-allowed;
}

/* ---------- 移动端：聊天窗全屏 ---------- */
/* 2026-10-06：断点从 560px 放宽到 720px —— 手机（含大屏/折叠机竖屏）一律全屏，
   只有桌面宽屏保留右下角浮窗。 */
@media (max-width: 720px) {
  #aioChatFab { right: 16px; bottom: 130px; height: 44px; padding: 0 15px 0 13px; }
  /* 全屏时锁住背后页面滚动：iOS 的滚动容器是 html、Android 是 body，
     只锁 body 在 iOS 上拦不住。两个都 overflow:hidden + overscroll-behavior:none，
     才能两边都罩住、也不产生「手指一滑整页跟着跑」的穿透。 */
  html.aio-chat-open,
  body.aio-chat-open {
    overflow: hidden;
    overscroll-behavior: none;
  }
  #aioChatWin {
    position: fixed;
    top: 0;
    right: 0;
    left: 0;
    width: 100%;
    max-width: 100%;
    /* ★ iOS 关键：不能用 height:100% —— fixed 元素的 100% 按「布局视口」算，
       比可见区高（URL 栏/底部工具条占位），底部输入区会被顶出屏幕、
       消息区也显得"滑不动"。改用动态视口单位，URL 栏收起/展开、键盘弹出时
       窗口高度跟着变，输入框始终贴底可见。100vh 是旧 iOS 的兜底。 */
    height: 100vh;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
    border: 0;
  }
  #aioChatWin .chat-head { height: 50px; padding-left: 14px; }
  /* 全屏时给消息区留足空间，滚动条粗一点好摸；pan-y 明确只允许竖向滑动 */
  #aioChatWin .chat-body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }
  #aioChatWin .chat-body::-webkit-scrollbar { width: 4px; }
}
/* ============================================================
   客服消息 Markdown / 流程图（2026-10-04 移植自平台前端 ai-markdown.js）
   ```mermaid 代码块 → mermaid.run() 渲染成 SVG 流程图
   样式跟白底气泡配套：边框+圆角+浅背景，不引第三方主题
   ============================================================ */
#aioChatWin .chat-bubble .mermaid {
  margin: 8px 0 2px;
  padding: 10px;
  background: #F7F8FA;
  border: 1px solid #E5E7EB;
  border-radius: 6px;
  overflow-x: auto;
  text-align: center;
}
#aioChatWin .chat-bubble .mermaid svg {
  max-width: 100%;
  height: auto;
}
/* 渲染失败兜底：把转义后的 mermaid 源码当等宽字显示，不黑屏 */
#aioChatWin .chat-bubble .mermaid:not(.mermaid-rendered) {
  font-family: Consolas, Menlo, monospace;
  font-size: 12px;
  color: #8A8F99;
  white-space: pre-wrap;
  text-align: left;
}
/* 客服回复里的 markdown 元素 */
#aioChatWin .chat-bubble .chat-inline-code {
  background: #F0F1F3;
  border-radius: 3px;
  padding: 1px 4px;
  font-family: Consolas, Menlo, monospace;
  font-size: 13px;
}
#aioChatWin .chat-bubble .chat-code {
  background: #1F2329;
  color: #E6E8EB;
  border-radius: 6px;
  padding: 8px 10px;
  overflow-x: auto;
  margin: 6px 0;
}
#aioChatWin .chat-bubble .chat-code code {
  font-family: Consolas, Menlo, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}
#aioChatWin .chat-bubble .chat-h {
  font-weight: 600;
  font-size: 15px;
  margin: 6px 0 2px;
}
#aioChatWin .chat-bubble .chat-li {
  padding-left: 12px;
  line-height: 1.7;
}
#aioChatWin .chat-bubble a {
  color: #5B7CB8;
  text-decoration: underline;
  word-break: break-all;
}
