/*
 * 在线聊天样式：访客浮窗（cw-*）+ 客服工作台（ac-*）
 * 主色取 var(--brand)，断点 768px。
 *
 * 外部依赖（改名前先同步修改这些文件）：
 *   public/css/site.css 依赖 .cw-bar、body.cw-has-bar（页面自带 .mobile-bar 时隐藏 .cw-bar、修正底部内边距）；
 *   public/css/site.css 的 [data-open-chat].has-unread 红点由 chat-widget.js 的 setDot() 同步；
 *   public/css/admin.css 的 .admin-chat-page 设置 --ac-offset，并覆盖窄屏下 .ac-app 的边框与圆角。
 */

/* ================= 访客浮窗 cw- ================= */
.cw-root {
  --cw-brand: var(--brand, #1f5eff);
  --cw-text: #1d2433;
  --cw-muted: #8a93a6;
  --cw-line: #e6e9f0;
  --cw-bg: #f5f7fb;
  --cw-danger: #e5484d;
  --cw-ok: #20b26b;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--cw-text);
  -webkit-font-smoothing: antialiased;
}
.cw-root *,
.cw-root *::before,
.cw-root *::after { box-sizing: border-box; }
.cw-root [hidden] { display: none !important; }
.cw-root button { font: inherit; cursor: pointer; }
.cw-root button:disabled { cursor: not-allowed; opacity: .55; }

/* 圆形按钮（PC） */
.cw-launcher {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 2147483000;
  width: 56px;
  height: 56px;
  border: 0;
  border-radius: 50%;
  background: var(--cw-brand);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 8px 24px rgba(31, 94, 255, .35), 0 2px 6px rgba(0, 0, 0, .12);
  transition: transform .2s ease, box-shadow .2s ease;
}
.cw-launcher:hover { transform: translateY(-2px); }
.cw-launcher:focus-visible { outline: 3px solid rgba(31, 94, 255, .35); outline-offset: 3px; }

/* 未读红点 */
.cw-dot {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--cw-danger);
  border: 2px solid #fff;
}

/* 底部"在线咨询"条（H5） */
.cw-bar { display: none; }

/* 面板（PC 360×520） */
.cw-panel {
  position: fixed;
  right: 24px;
  bottom: 92px;
  z-index: 2147483001;
  width: 360px;
  height: 520px;
  max-height: calc(100vh - 116px);
  background: #fff;
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(20, 30, 60, .22), 0 2px 8px rgba(20, 30, 60, .08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: cw-pop .18s ease-out;
}
@keyframes cw-pop {
  from { opacity: 0; transform: translateY(12px) scale(.98); }
  to { opacity: 1; transform: none; }
}

.cw-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 10px 12px 16px;
  background: var(--cw-brand);
  color: #fff;
}
.cw-head-text { flex: 1; min-width: 0; }
.cw-title { font-size: 16px; font-weight: 600; }
.cw-status { display: flex; align-items: center; gap: 6px; font-size: 12px; opacity: .92; }
.cw-status-dot { width: 8px; height: 8px; border-radius: 50%; background: #c4cad6; }
.cw-is-online .cw-status-dot { background: #4ade80; box-shadow: 0 0 0 3px rgba(74, 222, 128, .25); }
.cw-is-offline .cw-status-dot { background: #c4cad6; }
.cw-close {
  flex: none;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.cw-close:hover { background: rgba(255, 255, 255, .16); }

.cw-notice {
  flex: none;
  padding: 8px 14px;
  font-size: 13px;
  color: #8a5a00;
  background: #fff7e6;
  border-bottom: 1px solid #ffe7ba;
}

.cw-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 14px 12px;
  background: var(--cw-bg);
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.cw-msg { display: flex; flex-direction: column; margin-bottom: 12px; }
.cw-msg-visitor { align-items: flex-end; }
.cw-msg-admin { align-items: flex-start; }
.cw-msg-system { align-items: center; }
.cw-bubble {
  max-width: 78%;
  padding: 8px 12px;
  border-radius: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.cw-msg-admin .cw-bubble { background: #fff; border: 1px solid var(--cw-line); border-top-left-radius: 4px; }
.cw-msg-visitor .cw-bubble { background: var(--cw-brand); color: #fff; border-top-right-radius: 4px; }
.cw-bubble-img { padding: 4px; background: #fff !important; border: 1px solid var(--cw-line); }
.cw-img { display: block; max-width: 200px; max-height: 220px; border-radius: 8px; min-width: 60px; min-height: 40px; background: #eef1f6; }
.cw-msg-pending, .ac-msg-pending { opacity: .55; }
.cw-time { margin-top: 3px; font-size: 11px; color: var(--cw-muted); }
.cw-sys {
  max-width: 90%;
  padding: 3px 10px;
  font-size: 12px;
  color: var(--cw-muted);
  background: rgba(0, 0, 0, .04);
  border-radius: 10px;
  text-align: center;
}

/* 留联系方式表单 */
.cw-contact {
  flex: none;
  display: grid;
  grid-template-columns: 1fr 1fr;
  column-gap: 8px;
  max-height: 60%;
  overflow-y: auto;
  padding: 10px 14px 12px;
  background: #fff;
  border-top: 1px solid var(--cw-line);
}
.cw-contact-title { grid-column: 1 / -1; font-weight: 600; margin-bottom: 6px; }
.cw-contact-error, .cw-contact-actions, .cw-field-wide { grid-column: 1 / -1; }
.cw-field { display: block; min-width: 0; margin-bottom: 8px; }
.cw-field-label { display: block; font-size: 12px; color: var(--cw-muted); margin-bottom: 2px; }
.cw-field-input {
  display: block;
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--cw-line);
  border-radius: 8px;
  font: inherit;
  font-size: 16px; /* 避免 iOS 聚焦放大 */
  color: inherit;
  background: #fff;
  resize: none;
}
@media (min-width: 768px) { .cw-field-input { font-size: 14px; } }
.cw-field-input:focus { outline: none; border-color: var(--cw-brand); box-shadow: 0 0 0 3px rgba(31, 94, 255, .12); }
.cw-contact-error { color: var(--cw-danger); font-size: 12px; margin-bottom: 6px; }
.cw-contact-actions { display: flex; justify-content: flex-end; gap: 8px; }
.cw-btn {
  padding: 6px 16px;
  border-radius: 8px;
  border: 1px solid var(--cw-line);
  background: #fff;
  color: var(--cw-text);
}
.cw-btn-primary { background: var(--cw-brand); border-color: var(--cw-brand); color: #fff; }

.cw-error {
  flex: none;
  padding: 6px 14px;
  font-size: 12px;
  color: var(--cw-danger);
  background: #fff1f1;
  border-top: 1px solid #ffd6d6;
}

.cw-composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px;
  border-top: 1px solid var(--cw-line);
  background: #fff;
}
.cw-icon-btn {
  flex: none;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--cw-muted);
  display: flex;
  align-items: center;
  justify-content: center;
}
.cw-icon-btn:hover:not(:disabled) { background: var(--cw-bg); color: var(--cw-brand); }
.cw-input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  max-height: 96px;
  padding: 7px 10px;
  border: 1px solid var(--cw-line);
  border-radius: 8px;
  font: inherit;
  font-size: 16px;
  line-height: 1.4;
  color: inherit;
  resize: none;
}
@media (min-width: 768px) { .cw-input { font-size: 14px; } }
.cw-input:focus { outline: none; border-color: var(--cw-brand); }
.cw-send {
  flex: none;
  height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: var(--cw-brand);
  color: #fff;
}

.cw-foot {
  flex: none;
  padding: 0 12px 8px;
  background: #fff;
  text-align: center;
}
.cw-foot:empty { display: none; }
.cw-link {
  border: 0;
  background: none;
  padding: 2px 6px;
  font-size: 12px;
  color: var(--cw-brand);
}
.cw-link:hover { text-decoration: underline; }

/* PC 打开面板时圆形按钮变成"关闭"语义，仍可见 */
.cw-open .cw-launcher { box-shadow: 0 4px 14px rgba(31, 94, 255, .28); }

/* ---------- H5（<768px）：底部固定条 + 全屏面板 ---------- */
@media (max-width: 767.98px) {
  body.cw-has-bar { padding-bottom: calc(52px + env(safe-area-inset-bottom, 0px)); }
  html.cw-lock, html.cw-lock body { overflow: hidden; }

  .cw-launcher { display: none; }
  .cw-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2147483000;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: calc(52px + env(safe-area-inset-bottom, 0px));
    padding: 0 16px env(safe-area-inset-bottom, 0px);
    border: 0;
    background: var(--cw-brand);
    color: #fff;
    font-size: 16px;
    font-weight: 600;
    box-shadow: 0 -4px 16px rgba(20, 30, 60, .12);
  }
  .cw-bar svg { width: 22px; height: 22px; }
  .cw-bar .cw-dot { position: static; margin-left: -2px; }
  .cw-open .cw-bar { display: none; }

  .cw-panel {
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
    animation: cw-slide .2s ease-out;
  }
  .cw-head { padding-top: calc(10px + env(safe-area-inset-top, 0px)); }
  .cw-composer { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
  .cw-bubble { max-width: 82%; }
  .cw-img { max-width: 64vw; }
  @keyframes cw-slide {
    from { transform: translateY(24px); opacity: 0; }
    to { transform: none; opacity: 1; }
  }
}

@media (prefers-reduced-motion: reduce) {
  .cw-panel, .cw-launcher { animation: none; transition: none; }
}

/* ================= 客服工作台 ac- ================= */
.ac-app {
  --ac-brand: var(--brand, #1f5eff);
  --ac-text: #1d2433;
  --ac-muted: #8a93a6;
  --ac-line: #e6e9f0;
  --ac-bg: #f5f7fb;
  --ac-danger: #e5484d;
  --ac-ok: #20b26b;
  position: relative;
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--ac-offset, 0px));
  height: calc(100dvh - var(--ac-offset, 0px));
  min-height: 420px;
  background: #fff;
  border: 1px solid var(--ac-line);
  border-radius: 10px;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--ac-text);
}
.ac-app *, .ac-app *::before, .ac-app *::after { box-sizing: border-box; }
.ac-app [hidden] { display: none !important; }
.ac-app button { font: inherit; cursor: pointer; }
.ac-app button:disabled { cursor: not-allowed; opacity: .55; }
.ac-fatal { padding: 24px; color: #e5484d; }

.ac-top {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--ac-line);
}
.ac-heading { margin: 0; font-size: 16px; font-weight: 600; }
.ac-conn { flex: 1; font-size: 12px; color: var(--ac-muted); }
.ac-conn-ok { color: var(--ac-ok); }
.ac-conn-warn { color: #c27c00; }
.ac-conn-err { color: var(--ac-danger); }

/* 在线开关 */
.ac-switch { position: relative; display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.ac-switch-input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.ac-switch-track {
  position: relative;
  width: 40px;
  height: 22px;
  border-radius: 11px;
  background: #c4cad6;
  transition: background .2s;
}
.ac-switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .2);
  transition: transform .2s;
}
.ac-switch-on .ac-switch-track { background: var(--ac-ok); }
.ac-switch-on .ac-switch-track::after { transform: translateX(18px); }
.ac-switch-input:focus-visible + .ac-switch-track { outline: 3px solid rgba(31, 94, 255, .3); outline-offset: 2px; }
.ac-switch-text { font-size: 13px; min-width: 2em; }

.ac-body { flex: 1; min-height: 0; display: flex; }

/* 会话列表 */
.ac-list {
  flex: none;
  width: 300px;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--ac-line);
  background: #fafbfd;
  min-height: 0;
}
.ac-list-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 14px;
  font-weight: 600;
  border-bottom: 1px solid var(--ac-line);
}
.ac-badge, .ac-unread {
  display: inline-block;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--ac-danger);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  line-height: 18px;
  text-align: center;
}
.ac-items { flex: 1; min-height: 0; margin: 0; padding: 0; list-style: none; overflow-y: auto; }
.ac-item {
  padding: 10px 14px;
  border-bottom: 1px solid #eef0f5;
  cursor: pointer;
  outline: none;
}
.ac-item:hover, .ac-item:focus-visible { background: #f0f3fa; }
.ac-item-active, .ac-item-active:hover { background: #e8efff; box-shadow: inset 3px 0 0 var(--ac-brand); }
.ac-item-row { display: flex; align-items: center; gap: 8px; }
.ac-item-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-item-time { flex: none; font-size: 12px; color: var(--ac-muted); }
.ac-item-preview { flex: 1; min-width: 0; font-size: 13px; color: #5b6478; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-item-unread .ac-item-preview { color: var(--ac-text); }
.ac-item-meta { font-size: 12px; color: var(--ac-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ac-empty { padding: 32px 16px; text-align: center; color: var(--ac-muted); font-size: 13px; }

/* 聊天窗口 */
.ac-detail { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }
.ac-placeholder { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--ac-muted); background: var(--ac-bg); }
.ac-chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ac-chat-head {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--ac-line);
}
.ac-back { display: none; border: 0; background: none; color: var(--ac-brand); padding: 4px 6px 4px 0; font-size: 15px; }
.ac-chat-info { flex: 1; min-width: 0; }
.ac-chat-name { font-weight: 600; }
.ac-chat-meta { font-size: 12px; color: var(--ac-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.ac-btn {
  flex: none;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--ac-line);
  border-radius: 8px;
  background: #fff;
  color: var(--ac-text);
  white-space: nowrap;
}
.ac-btn-primary { background: var(--ac-brand); border-color: var(--ac-brand); color: #fff; }
.ac-btn-outline { border-color: var(--ac-brand); color: var(--ac-brand); }
.ac-btn-outline:hover { background: #eef3ff; }

.ac-msgs { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; background: var(--ac-bg); overscroll-behavior: contain; }
.ac-loading { text-align: center; color: var(--ac-muted); font-size: 13px; padding: 20px 0; }
.ac-msg { display: flex; flex-direction: column; margin-bottom: 12px; }
.ac-msg-visitor { align-items: flex-start; }
.ac-msg-admin { align-items: flex-end; }
.ac-msg-system { align-items: center; }
.ac-bubble {
  max-width: 70%;
  padding: 8px 12px;
  border-radius: 12px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.ac-msg-visitor .ac-bubble { background: #fff; border: 1px solid var(--ac-line); border-top-left-radius: 4px; }
.ac-msg-admin .ac-bubble { background: var(--ac-brand); color: #fff; border-top-right-radius: 4px; }
.ac-bubble-img { padding: 4px; background: #fff !important; border: 1px solid var(--ac-line); }
.ac-img { display: block; max-width: 240px; max-height: 260px; border-radius: 8px; min-width: 60px; min-height: 40px; background: #eef1f6; }
.ac-time { margin-top: 3px; font-size: 11px; color: var(--ac-muted); }
.ac-sys { padding: 3px 10px; font-size: 12px; color: var(--ac-muted); background: rgba(0, 0, 0, .04); border-radius: 10px; }

.ac-error { flex: none; padding: 6px 16px; font-size: 12px; color: var(--ac-danger); background: #fff1f1; }
.ac-dialog .ac-error { margin: 0 0 10px; border-radius: 6px; }

.ac-composer {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--ac-line);
}
.ac-icon-btn {
  flex: none;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ac-muted);
  display: flex;
  align-items: center;
  justify-content: center;
}
.ac-icon-btn:hover:not(:disabled) { background: var(--ac-bg); color: var(--ac-brand); }
.ac-input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  max-height: 140px;
  padding: 7px 10px;
  border: 1px solid var(--ac-line);
  border-radius: 8px;
  font: inherit;
  color: inherit;
  resize: vertical;
}
.ac-input:focus { outline: none; border-color: var(--ac-brand); }
.ac-send { height: 36px; }

/* 转为线索弹窗 */
.ac-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(15, 20, 35, .45);
}
.ac-dialog {
  width: 100%;
  max-width: 420px;
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 20px;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
.ac-dialog-title { margin: 0 0 14px; font-size: 17px; }
.ac-field { display: block; margin-bottom: 12px; }
.ac-field-label { display: block; font-size: 13px; color: #5b6478; margin-bottom: 4px; }
.ac-field-input {
  display: block;
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--ac-line);
  border-radius: 8px;
  font: inherit;
  color: inherit;
  resize: vertical;
}
.ac-field-input:focus { outline: none; border-color: var(--ac-brand); box-shadow: 0 0 0 3px rgba(31, 94, 255, .12); }
.ac-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.ac-toast {
  position: fixed;
  left: 50%;
  bottom: 40px;
  z-index: 1001;
  transform: translateX(-50%);
  padding: 8px 18px;
  border-radius: 20px;
  background: rgba(20, 25, 40, .88);
  color: #fff;
  font-size: 14px;
}

/* 窄屏：列表 / 聊天窗口切换 */
@media (max-width: 767.98px) {
  .ac-app { border-radius: 0; border-left: 0; border-right: 0; min-height: 360px; }
  .ac-top { padding: 8px 12px; }
  .ac-list { width: 100%; border-right: 0; }
  .ac-detail { display: none; }
  .ac-show-detail .ac-list { display: none; }
  .ac-show-detail .ac-detail { display: flex; }
  .ac-back { display: inline-block; }
  .ac-chat-head { padding: 8px 12px; }
  .ac-msgs { padding: 12px; }
  .ac-bubble { max-width: 82%; }
  .ac-img { max-width: 60vw; }
  .ac-input { font-size: 16px; resize: none; }
  .ac-field-input { font-size: 16px; }
  .ac-composer { padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
}
