/* ==========================================================================
   悬浮在线客服聊天框
   ========================================================================== */

.chat-fab {
  position: fixed; right: 12px; top: calc(50% + 144px); bottom: auto; z-index: 900;
  width: 58px; height: 58px; border-radius: 50%; border: 0; cursor: pointer;
  background: linear-gradient(135deg, var(--brand) 0%, var(--accent) 100%);
  box-shadow: 0 12px 30px rgba(11, 74, 111, .38);
  display: grid; place-items: center; color: #fff;
  transition: transform .25s var(--ease), box-shadow .25s;
}
.chat-fab:hover { transform: translateY(-4px) scale(1.05); box-shadow: 0 16px 38px rgba(11, 74, 111, .45); }
.chat-fab svg { width: 26px; height: 26px; }
.chat-fab .chat-dot {
  position: absolute; top: -3px; right: -3px; width: 20px; height: 20px;
  border-radius: 50%; background: #f43f5e; color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center; line-height: 1; padding: 0; border: 2px solid #fff;
}
.chat-fab.is-open .chat-dot { display: none; }

/* 10秒未互动的主动邀请气泡（参考百度商桥） */
.chat-invite {
  position: fixed; right: 12px; bottom: calc(50% - 125px); z-index: 899;
  display: flex; align-items: flex-end; gap: 10px;
  max-width: calc(100vw - 32px);
  opacity: 0; visibility: hidden; transform: translateY(14px);
  transition: opacity .3s var(--ease), transform .3s var(--ease), visibility .3s;
}
.chat-invite.show { opacity: 1; visibility: visible; transform: none; }
.chat-invite .bubble {
  position: relative; background: #fff; border: 1px solid var(--line);
  border-radius: 14px 14px 4px 14px; padding: 12px 14px; cursor: pointer;
  box-shadow: 0 12px 30px rgba(11, 27, 43, .18);
  font-size: 13.5px; line-height: 1.6; color: var(--ink);
}
.chat-invite .bubble b { color: var(--brand); }
.chat-invite .bubble .go {
  display: inline-block; margin-top: 6px; padding: 4px 12px; border-radius: 99px;
  background: var(--accent); color: #fff; font-size: 12.5px; font-weight: 600;
}
.chat-invite .bubble .x {
  position: absolute; top: -8px; right: -8px; width: 20px; height: 20px;
  border: 0; border-radius: 50%; background: #94a3b8; color: #fff;
  font-size: 12px; line-height: 1; cursor: pointer;
}
.chat-invite .bubble .x:hover { background: #64748b; }

.chat-panel {
  position: fixed; right: 78px; top: 50%; bottom: auto; z-index: 901;
  width: 640px; max-width: calc(100vw - 126px); height: calc(100vh - 110px); max-height: 780px;
  background: #fff; border-radius: 18px; overflow: hidden;
  box-shadow: 0 24px 70px rgba(11, 27, 43, .28);
  display: flex; flex-direction: column;
  opacity: 0; visibility: hidden; transform: translateY(calc(-50% + 16px)) scale(.98);
  transition: opacity .25s var(--ease), transform .25s var(--ease), visibility .25s;
}
.chat-panel.open { opacity: 1; visibility: visible; transform: translateY(-50%); }
.chat-panel.drag-over .chat-input { background: #eefaf5; box-shadow: inset 0 0 0 2px var(--accent); }

/* 头部 */
.chat-head {
  background: linear-gradient(135deg, var(--brand-dark), var(--brand) 60%, #0d5c7a);
  color: #fff; padding: 16px 18px; display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.chat-head .avatar {
  width: 42px; height: 42px; border-radius: 12px; background: rgba(255, 255, 255, .16);
  display: grid; place-items: center; font-weight: 800; font-size: 15px; flex-shrink: 0;
}
.chat-head .info { flex: 1; min-width: 0; }
.chat-head b { display: block; font-size: 15px; }
.chat-head small { display: flex; align-items: center; gap: 5px; font-size: 12px; opacity: .85; margin-top: 2px; }
.chat-head small i { width: 7px; height: 7px; border-radius: 50%; background: #4ade80; }
.chat-head .x {
  width: 30px; height: 30px; border: 0; border-radius: 9px; background: rgba(255, 255, 255, .14);
  color: #fff; font-size: 16px; cursor: pointer; line-height: 1; flex-shrink: 0;
}
.chat-head .x:hover { background: rgba(255, 255, 255, .26); }

/* 消息区 */
.chat-body { flex: 1 1 0; min-height: 0; overflow-y: auto; padding: 16px 14px 8px; background: #f6f9fa; }
.msg { display: flex; gap: 8px; margin-bottom: 12px; }
.msg .b-av {
  width: 30px; height: 30px; border-radius: 9px; flex-shrink: 0; margin-top: 2px;
  background: linear-gradient(135deg, var(--brand), var(--accent)); color: #fff;
  display: grid; place-items: center; font-size: 12px; font-weight: 700;
}
.msg .bubble {
  max-width: 78%; padding: 12px 15px; border-radius: 4px 14px 14px 14px;
  background: #fff; border: 1px solid var(--line); color: var(--ink);
  font-size: 14.5px; line-height: 1.65; box-shadow: 0 1px 2px rgba(11, 27, 43, .04);
}
.msg .bubble b { color: var(--brand); }
.msg.me { flex-direction: row-reverse; }
.msg.me .bubble {
  background: var(--brand); color: #fff; border: 0;
  border-radius: 14px 4px 14px 14px;
}
/* 自己消息下面的「已送达 / 已读」——贴着气泡右下角，不占左右宽度 */
.msg.me { position: relative; padding-bottom: 14px; margin-bottom: 2px; }
.msg.me .rd {
  position: absolute; right: 3px; bottom: 1px;
  font-size: 10.5px; line-height: 1; color: #a7b5bf;
  letter-spacing: .2px; user-select: none;
}
.msg.me .rd.on { color: #6f9c8b; }
.msg.me .rd.bad { color: #d9534f; font-weight: 600; }
.msg.me.no-rd { padding-bottom: 0; margin-bottom: 12px; }
.msg.me.no-rd .rd { display: none; }
.msg .bubble a { color: var(--accent); font-weight: 600; text-decoration: underline; }

/* 快捷问题：单行横滑（参考百度商桥样式），鼠标滚轮/触屏可左右滑动 */
.chat-chips {
  display: flex; flex-wrap: nowrap; gap: 7px; padding: 2px 14px 12px; background: #f6f9fa; flex-shrink: 0;
  overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.chat-chips::-webkit-scrollbar { display: none; }
.chat-chips { cursor: grab; }
.chat-chips.dragging { cursor: grabbing; user-select: none; }
.chat-chips.dragging button { pointer-events: none; }
.chat-chips button {
  border: 1px solid var(--line); background: #fff; border-radius: 99px;
  padding: 6px 12px; font-size: 12.5px; color: var(--brand); cursor: pointer;
  flex: 0 0 auto; white-space: nowrap;
  transition: all .2s;
}
.chat-chips button:hover { background: var(--brand); color: #fff; border-color: var(--brand); }

/* 底部工具条 + 输入 */
.chat-tools {
  display: flex; border-top: 1px solid var(--line); background: #fff; flex-shrink: 0;
}
.chat-tools button {
  flex: 1; border: 0; background: none; padding: 9px 0 8px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  font-size: 11.5px; color: var(--muted);
}
.chat-tools button + button { border-left: 1px solid var(--line); }
.chat-tools button:hover { color: var(--brand); background: var(--soft); }
.chat-tools svg { width: 18px; height: 18px; }
/* 输入区：上排工具（表情 / 表情包 / 照片 / 文件），下排输入框+发送 */
.chat-input {
  display: flex; flex-direction: column; gap: 8px;
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--line); background: #fff; flex-shrink: 0; position: relative;
}
.chat-input.drag-over { background: #eefaf5; box-shadow: inset 0 0 0 2px var(--accent); }
.ci-main { display: flex; gap: 8px; align-items: flex-end; }
.chat-input textarea {
  flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 18px; padding: 10px 16px;
  font: inherit; font-size: 14.5px; line-height: 1.5; background: #fbfcfd; color: var(--ink);
  resize: none; max-height: 110px; overflow-y: auto;
}
.chat-input textarea:focus { outline: none; border-color: var(--accent); background: #fff; }
.chat-input .ci-send {
  min-width: 60px; height: 38px; padding: 0 16px; border-radius: 19px; border: 0; cursor: pointer;
  background: var(--accent); color: #fff; display: grid; place-items: center; flex-shrink: 0;
  font: inherit; font-size: 14px; font-weight: 700; letter-spacing: .5px;
  transition: background .18s, transform .18s;
}
.chat-input .ci-send:hover { background: #00947c; }
.chat-input .ci-send:active { transform: scale(.96); }
.chat-input .ci-send svg { width: 18px; height: 18px; }

.ci-bar {
  display: flex; align-items: center; gap: 6px; padding-left: 2px;
}
.ci-bar button {
  width: 34px; height: 34px; border: 0; border-radius: 10px; background: none; cursor: pointer;
  color: var(--muted); display: grid; place-items: center; padding: 0;
  transition: background .18s, color .18s;
}
.ci-bar button:hover:not(:disabled) { background: var(--soft); color: var(--brand); }
.ci-bar button.on { background: var(--soft); color: var(--accent); }
.ci-bar button:disabled { opacity: .4; cursor: default; }
.ci-bar button svg { width: 22px; height: 22px; display: block; }
.ci-bar .ci-hint { font-size: 11.5px; color: #9aa8b5; margin-left: 4px; }

/* 表情 / 表情包面板 */
.ci-panel {
  position: absolute; left: 8px; right: 8px; bottom: calc(100% + 6px);
  background: #fff; border: 1px solid var(--line); border-radius: 14px;
  box-shadow: 0 14px 36px rgba(11, 27, 43, .18);
  padding: 6px; z-index: 4; max-height: 268px; display: flex; flex-direction: column;
}
.ci-panel[hidden] { display: none; }
.cp-tabs { display: flex; gap: 4px; padding: 2px 2px 6px; border-bottom: 1px solid var(--line); flex: 0 0 auto; }
.cp-tabs button {
  border: 0; background: none; cursor: pointer; font: inherit; font-size: 12.5px;
  color: var(--muted); padding: 5px 12px; border-radius: 99px;
}
.cp-tabs button.on { background: var(--soft); color: var(--brand); font-weight: 700; }
.cp-body { overflow-y: auto; padding: 6px 0 8px; }
.cp-body[hidden] { display: none !important; }
.cp-body[data-panel="emoji"]:not([hidden]) { display: grid; grid-template-columns: repeat(9, 1fr); gap: 2px; }
.cp-body[data-panel="stk"]:not([hidden]) { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; padding: 8px 4px 4px; }
.cp-body[data-panel="emoji"] button {
  border: 0; background: none; cursor: pointer; font-size: 21px; line-height: 1;
  padding: 5px 0; border-radius: 8px;
}
.cp-body[data-panel="emoji"] button:hover { background: var(--soft); }
.cp-stk {
  border: 0; background: none; padding: 0; cursor: pointer; border-radius: 11px;
  line-height: 0; transition: transform .15s;
}
.cp-stk:hover { transform: translateY(-2px) scale(1.05); }
.cp-stk img { width: 100%; height: auto; display: block; border-radius: 11px; }

/* 时间分隔条 */
.chat-stamp {
  text-align: center; font-size: 11.5px; color: #9aa8b5;
  margin: 2px 0 12px; letter-spacing: .2px;
}

/* 附件：图片气泡 / 文件卡片 */
.msg.att .bubble { padding: 6px; background: #fff; border: 1px solid var(--line); }
.msg.me.att .bubble { background: #eaf7f2; border: 1px solid #d3ece1; }
.att-img { display: block; line-height: 0; border-radius: 10px; overflow: hidden; }
.att-img img {
  max-width: 212px; max-height: 212px; width: auto; height: auto;
  border-radius: 10px; display: block; object-fit: cover; cursor: zoom-in;
}
/* 多图：九宫格 */
.att-grid { display: grid; gap: 4px; width: 216px; grid-template-columns: repeat(2, 1fr); }
.att-grid .att-img { position: relative; aspect-ratio: 1 / 1; overflow: hidden; }
.att-grid.n3 .att-img:first-child { grid-row: span 2; aspect-ratio: 1 / 2; }
.att-grid .att-img img { width: 100%; height: 100%; max-width: none; max-height: none; object-fit: cover; }
.att-more {
  position: absolute; inset: 0; display: grid; place-items: center;
  background: rgba(11, 27, 43, .55); color: #fff; font-size: 18px; font-style: normal; font-weight: 700;
}
.att-up { color: var(--muted); font-size: 13px; }
.att-up b { color: var(--brand); }
.msg.me .att-up { color: rgba(255, 255, 255, .82); }
.msg.me .att-up b { color: #fff; }

/* 表情包消息：去掉气泡底 */
.msg.stk .bubble { padding: 0; background: none; border: 0; box-shadow: none; }
.att-stk { display: block; }
.att-stk img { width: 116px; height: 116px; display: block; border-radius: 14px; }

.att-file {
  display: flex; align-items: center; gap: 10px; max-width: 240px;
  text-decoration: none !important; color: inherit;
}
.att-file .af-ic {
  width: 38px; height: 38px; border-radius: 9px; flex-shrink: 0;
  background: var(--accent-soft); color: var(--brand);
  display: grid; place-items: center; font-size: 10.5px; font-weight: 800; letter-spacing: .3px;
}
.msg.me .att-file .af-ic { background: rgba(255, 255, 255, .22); color: #fff; }
.att-file .af-tx { min-width: 0; flex: 1; }
.att-file .af-tx b {
  display: block; font-size: 13.5px; font-weight: 600; color: var(--ink);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px;
}
.att-file .af-tx small { font-size: 11.5px; color: var(--muted); }
.msg.me .att-file .af-tx b { color: #fff; }
.msg.me .att-file .af-tx small { color: rgba(255, 255, 255, .75); }
.att-file .af-dl { font-size: 15px; color: var(--accent); flex-shrink: 0; }
.msg.me .att-file .af-dl { color: #fff; }

/* 图片放大预览 */
.chat-viewer {
  position: fixed; inset: 0; z-index: 9999; background: rgba(8, 18, 26, .88);
  display: none; align-items: center; justify-content: center; padding: 20px;
}
.chat-viewer.show { display: flex; }
.chat-viewer img { max-width: 94vw; max-height: 88vh; border-radius: 10px; }
.chat-viewer .x {
  position: absolute; top: 14px; right: 14px; width: 40px; height: 40px; border-radius: 50%;
  border: 0; background: rgba(255, 255, 255, .18); color: #fff; font-size: 18px; cursor: pointer;
}

/* 视图切换：留言 / 二维码 */
.chat-view { display: none; flex-direction: column; flex: 1; background: #fff; }
.chat-view.on { display: flex; }
.chat-sub {
  display: flex; align-items: center; gap: 8px; padding: 12px 14px;
  border-bottom: 1px solid var(--line); font-weight: 700; color: var(--ink); font-size: 14px;
  background: #fff; flex-shrink: 0;
}
.chat-sub button { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 18px; line-height: 1; padding: 2px 4px; }
.chat-sub button:hover { color: var(--ink); }
.chat-form { padding: 14px; overflow-y: auto; }
.chat-form .field { margin-bottom: 12px; }
.chat-form label { display: block; font-size: 12.5px; font-weight: 600; color: var(--ink); margin-bottom: 5px; }
.chat-form label i { color: #d9534f; font-style: normal; }
.chat-form input, .chat-form textarea {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px;
  font-size: 14px; background: #fbfcfd;
}
.chat-form input:focus, .chat-form textarea:focus { outline: none; border-color: var(--accent); background: #fff; }
.chat-form textarea { min-height: 74px; resize: vertical; }
.chat-form .btn { width: 100%; height: 44px; }
.chat-form .ok {
  display: none; margin-top: 12px; padding: 10px 13px; border-radius: 10px;
  background: var(--accent-soft); color: #04715c; font-size: 13px;
}
.chat-form .ok.show { display: block; }

.chat-qr { padding: 20px; text-align: center; overflow-y: auto; }
.chat-qr img, .chat-qr svg {
  width: 190px; height: 190px; margin: 0 auto; border: 1px solid var(--line);
  border-radius: 12px; padding: 8px; background: #fff;
}
.chat-qr p { font-size: 13px; color: var(--muted); margin-top: 12px; line-height: 1.7; }

/* 打字中 */
.typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: chatblink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes chatblink { 0%, 60%, 100% { opacity: .25; } 30% { opacity: 1; } }

@media (max-width: 720px) {
  .chat-fab { right: 14px; bottom: 14px; top: auto; }
  /* max-width 必须一起覆盖：桌面那条 calc(100vw - 126px) 优先级更高，
     不重置的话手机上面板只占屏幕左侧 2/3，右边留一条白 */
  .chat-panel { right: 0; left: 0; bottom: 0; top: 0; width: 100%; max-width: 100%;
    height: 100dvh; max-height: none; border-radius: 0; transform: translateY(6px) scale(1); }
  .chat-panel.open { transform: none; }
  .chat-head { padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
  .chat-invite { right: 10px; bottom: 82px; max-width: calc(100vw - 90px); }
  .chat-invite .bubble { padding: 10px 12px; font-size: 12.5px; }
  .chat-fab { right: 14px; bottom: 14px; width: 54px; height: 54px; }
  .chat-head { padding: 10px 12px; gap: 9px; }
  .chat-head .avatar { width: 34px; height: 34px; font-size: 13px; }
  .chat-head b { font-size: 14px; }
  .chat-body { padding: 12px 10px 6px; }
  .msg .bubble { max-width: 86%; padding: 10px 12px; font-size: 13.8px; }
  .att-img img { max-width: 62vw; max-height: 46vh; }
  .att-grid { width: min(62vw, 216px); }
  .att-stk img { width: clamp(72px, 24vw, 96px); height: auto; aspect-ratio: 1 / 1; }
  .att-file { max-width: 62vw; }
  .att-grid { max-width: 100%; }
  .att-file .af-tx b { max-width: 34vw; }
  .cp-body[data-panel="emoji"] { grid-template-columns: repeat(auto-fill, minmax(30px, 1fr)); }
  .cp-body[data-panel="stk"]:not([hidden]) { grid-template-columns: repeat(auto-fill, minmax(56px, 1fr)); gap: 5px; }
  .ci-panel { max-height: min(252px, 40vh); }
  /* 底部工具栏在最外沿，安全区留给它；输入框不再重复留白 */
  .chat-input { padding-bottom: 8px; }
  .chat-tools, .chat-limit { padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)); }
  .chat-chips { padding-right: 14px; }
  .ci-bar button { width: 38px; height: 38px; }
  .chat-input .ci-send { min-width: clamp(52px, 15vw, 60px); height: clamp(36px, 10vw, 42px); }
}
@media (prefers-reduced-motion: reduce) {
  .chat-fab, .chat-panel { transition: none; }
}

/* ---------- 每日限额：强制电话联系 ---------- */
.chat-limit { padding: 14px 16px; text-align: center; border-top: 1px solid var(--line); background: #fff8ef; }
.chat-limit p { font-size: 13.5px; color: #8a5a1f; margin: 0 0 10px; line-height: 1.7; }
.chat-limit .btn { display: inline-flex; margin: 0 5px 6px; padding: 10px 20px; font-size: 15px; }
.chat-form .ts-slot { min-height: 0; }
