:root {
  --theme: #2f6bff;
  --theme-soft: rgba(47, 107, 255, 0.12);
  --bg: #f4f6fb;
  --panel: #ffffff;
  --text: #1f2430;
  --muted: #8b93a7;
  --border: #e5e8f0;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "PingFang SC", "Microsoft YaHei", "Helvetica Neue", Arial, system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

body.embedded { background: transparent; }

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* 移动端动态视口，避免地址栏/键盘导致布局溢出 */
  max-width: 900px;
  margin: 0 auto;
  background: var(--panel);
  box-shadow: 0 4px 24px rgba(20, 30, 60, 0.06);
}

body.embedded .app { box-shadow: none; }

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  background: var(--theme);
  color: #fff;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
/* 站点 LOGO：支持长方形，高度固定、宽度最多为高度的 6 倍，保持比例不拉伸 */
.logo {
  height: 46px;
  width: auto;
  max-width: 276px;
  border-radius: 4px;
  object-fit: contain;
  background: transparent;
}
.site-name { font-size: 16px; font-weight: 600; line-height: 1.2; }
.site-status { font-size: 12px; opacity: 0.85; margin-top: 2px; }

.link-btn {
  background: rgba(255, 255, 255, 0.18);
  border: 0;
  color: #fff;
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}

.welcome {
  padding: 10px 12px;
  background: var(--theme-soft);
  color: #35405c;
  font-size: 13px;
  line-height: 1.6;
  border-radius: 10px;
  margin: 0;
}

.messages {
  flex: 1;
  min-height: 0; /* 关键：让 flex 子项可滚动，避免内容撑破布局导致重叠、点不到 */
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg { display: flex; flex-direction: column; gap: 6px; max-width: 100%; }
.msg.agent { align-items: flex-start; }
.msg.visitor { align-items: flex-end; }
.msg.system { align-items: center; }
.msg-main { display: flex; flex-direction: column; align-items: flex-start; max-width: 78%; }
.msg.visitor .msg-main { align-items: flex-end; }
.msg.agent .msg-main { max-width: 82%; }
.msg-avatar { width: 32px; height: 32px; flex: 0 0 32px; border-radius: 50%; object-fit: cover; background: #eef1f8; }
.msg-name { font-size: 12px; color: var(--muted); margin: 0 0 4px 2px; }
.msg-time { font-size: 11px; color: var(--muted); margin-top: 4px; }
.bubble {
  max-width: min(76%, 520px);
  padding: 10px 12px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.6;
  background: #f1f3f8;
  word-break: break-word;
  white-space: pre-wrap;
}
.msg.visitor .bubble { background: var(--theme); color: #fff; }
.msg.system { justify-content: center; }
.msg.system .bubble {
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 2px 8px;
}
.meta { font-size: 11px; color: var(--muted); margin-top: 4px; }
.msg.visitor .meta { text-align: right; color: rgba(255, 255, 255, 0.85); }

.bubble img.thumb {
  display: block;
  max-width: min(260px, 100%); /* 不超出气泡宽度 */
  width: auto;
  height: auto;
  border-radius: 8px;
  cursor: zoom-in;
}
.bubble { max-width: min(78%, 560px); overflow: hidden; }
.msg-main { min-width: 0; }
.bubble .file-card { max-width: 100%; }
.bubble .file-card {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: inherit;
  font-size: 13px;
}
.bubble .file-card .fname { word-break: break-all; }
.bubble .file-card .fsize { color: var(--muted); font-size: 11px; }
.msg.visitor .bubble .file-card .fsize { color: rgba(255, 255, 255, 0.8); }

.notice {
  margin: 0 16px 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: #fff7e6;
  color: #a26b00;
  font-size: 12px;
  pointer-events: none; /* 提示条只做展示，不拦截点击（避免挡住帮助中心） */
}

.notice.notice-blocked {
  background: #fdecec;
  color: #c02c2c;
  font-weight: 600;
}

/* 被拉黑访客的遮罩：覆盖输入区，避免继续排队/发送 */
.blocked-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(244, 246, 251, 0.92);
}
.blocked-card {
  max-width: 320px;
  padding: 24px 20px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 10px 32px rgba(20, 30, 60, 0.12);
  text-align: center;
}
.blocked-card .blocked-icon { font-size: 34px; line-height: 1; }
.blocked-card .blocked-title { margin: 10px 0 6px; font-size: 16px; font-weight: 700; color: #c02c2c; }
.blocked-card .blocked-text { font-size: 13px; color: var(--muted); line-height: 1.7; }
.tool-btn.disabled,
.send-btn.disabled { opacity: 0.5; cursor: not-allowed; }

.rating {
  margin: 0 16px 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
}
.rating .stars { display: flex; gap: 6px; margin: 8px 0; }
.rating-tags { display: flex; flex-wrap: wrap; gap: 8px; margin: 6px 0; font-size: 12px; color: #5a6480; }
.rating-tags label { display: inline-flex; align-items: center; gap: 4px; }
.rating-comment { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px; font-size: 13px; font-family: inherit; margin-bottom: 8px; resize: vertical; }
.rating .star { font-size: 22px; cursor: pointer; color: #d8dbe4; }
.rating .star.on { color: #ffb400; }

.quick-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.chip {
  border: 1px solid var(--border);
  background: #fff;
  color: #4a5570;
  border-radius: 14px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: #fff;
}
.input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  resize: none;
  font-size: 14px;
  font-family: inherit;
  outline: none;
}
.input:focus { border-color: var(--theme); }
.tools { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.tools-left { display: flex; gap: 8px; }
.tool-btn {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  color: #4a5570;
}
.send-btn {
  border: 0;
  background: var(--theme);
  color: #fff;
  border-radius: 8px;
  padding: 8px 22px;
  font-size: 14px;
  cursor: pointer;
}
.send-btn:disabled, .tool-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.help-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: min(420px, 92vw);
  height: 100%;
  background: #fff;
  box-shadow: -4px 0 24px rgba(20, 30, 60, 0.12);
  display: flex;
  flex-direction: column;
  z-index: 30;
}
.help-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.help-head .link-btn { background: #eef1f8; color: #4a5570; }
.help-search { display: flex; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.help-search input { flex: 1; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.help-search button { border: 0; background: var(--theme); color: #fff; border-radius: 8px; padding: 8px 14px; cursor: pointer; font-size: 13px; }
.help-body { flex: 1; overflow-y: auto; padding: 12px 16px 24px; font-size: 14px; }
.help-cat { font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
.help-item {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
  cursor: pointer;
}
.help-item:hover { border-color: var(--theme); color: var(--theme); }
.article-title { font-size: 16px; font-weight: 600; margin: 4px 0 10px; }
.article-body { line-height: 1.8; white-space: pre-wrap; }
.bubble.revoked { background: #f1f3f8 !important; color: var(--muted) !important; font-style: italic; font-size: 12px; }
.article-img { display: block; max-width: 100%; border-radius: 8px; margin: 10px 0; }
.bubble iframe { width: 100%; max-width: none; border: 0; background: transparent; border-radius: 8px; display: block; }
/* HTML5 消息与聊天窗口两边平齐 */
.msg-main.html-msg-main { max-width: 100%; width: 100%; }
.bubble.html-bubble { max-width: 100%; width: 100%; padding: 6px; }
.agent-line { display: flex; align-items: center; gap: 6px; }
.agent-line img { width: 20px; height: 20px; border-radius: 50%; object-fit: cover; border: 1px solid rgba(255, 255, 255, 0.6); }
.emoji-panel {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 150px;
  width: min(420px, 94vw);
  max-height: 260px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(20, 30, 60, 0.16);
  padding: 10px;
  z-index: 35;
}

/* ---------- v0.9 访客端：内嵌帮助 / 历史记录 ---------- */
.header-note { font-size: 12px; opacity: 0.85; }
.welcome iframe,
.history-box iframe,
.help-inline iframe { width: 100%; border: 0; background: transparent; display: block; }
.history-box {
  margin: 0;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: #fafbff;
  font-size: 13px;
}
.history-box .his-head {
  padding: 8px 12px;
  cursor: pointer;
  color: #5a6480;
  display: flex;
  justify-content: space-between;
}
.history-box .his-body { padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 12px; }

/* 历史记录"加载更早的消息"：位于标题下方、列表上方（点一次往上插 20 条） */
.history-box .his-more {
  display: block;
  width: calc(100% - 24px);
  margin: 10px 12px 2px;
  padding: 7px 10px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: #f7f9ff;
  color: #4a5570;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
}
.history-box .his-more:hover { border-color: var(--theme); color: var(--theme); background: #eef3ff; }
.history-box .his-more:disabled { opacity: 0.7; cursor: default; }

/* 折叠历史时，列表与"加载更早"按钮一起收起 */
.history-box.collapsed .his-body,
.history-box.collapsed .his-more { display: none; }
.history-item { padding: 4px 0; border-bottom: 1px dashed #eef0f6; font-size: 12px; color: #5a6480; }
.history-item .hi-who { color: var(--muted); margin-right: 6px; }
.help-inline {
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
  max-height: 62vh;
  min-height: 72px;
  overflow: auto;
  resize: vertical; /* 可拖拽右下角放大，完整查看内容 */
  position: relative;
  z-index: 6; /* 保证不被消息里的 H5 iframe 覆盖 */
}
.help-inline.floating {
  position: fixed;
  left: 50%;
  top: 80px;
  margin: 0;
  width: min(420px, 92vw);
  height: 60vh;
  max-height: none;
  resize: both;
  overflow: auto;
  box-shadow: 0 18px 48px rgba(20, 30, 60, 0.28);
  z-index: 95;
}
.help-inline-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; cursor: default; }
.help-inline.floating .help-inline-title { cursor: move; }
.help-float-btn {
  border: 1px solid var(--border);
  background: #fff;
  color: #5a6480;
  border-radius: 6px;
  font-size: 11px;
  padding: 2px 8px;
  cursor: pointer;
}
.help-float-btn:hover { border-color: var(--theme); color: var(--theme); }
.msg iframe { position: relative; z-index: 1; }
.help-inline-title { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.help-cats { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.help-cat-chip {
  border: 1px solid var(--border);
  background: #fff;
  color: #4a5570;
  border-radius: 14px;
  padding: 4px 12px;
  font-size: 12px;
  cursor: pointer;
}
.help-cat-chip.active { background: var(--theme); color: #fff; border-color: var(--theme); }
.help-list .help-item {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 6px;
  cursor: pointer;
  font-size: 13px;
}
.help-list .help-item:hover { border-color: var(--theme); color: var(--theme); }
.help-hint { font-size: 12px; color: var(--muted); padding: 4px 0; }
.help-article .article-title { font-size: 15px; font-weight: 600; margin-bottom: 8px; }
.help-article .article-actions { margin-top: 10px; display: flex; gap: 8px; }
.mute-btn { font-size: 14px; }

/* 消息区底部留白，确保最后一条消息不被输入框遮挡 */
.messages { padding-bottom: 24px; }
.emoji-group-title { font-size: 11px; color: var(--muted); margin: 8px 0 6px; }
.emoji-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 2px; }
.emoji-item { font-size: 20px; text-align: center; cursor: pointer; border-radius: 6px; padding: 2px; }
.emoji-item:hover { background: var(--theme-soft); }

/* ---------- 留言/反馈表单 ---------- */
.fb-modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 35, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  padding: 16px;
}
.fb-card {
  width: min(420px, 94vw);
  max-height: 88vh;
  overflow-y: auto;
  background: #fff;
  border-radius: 14px;
  padding: 18px 18px 14px;
  box-shadow: 0 18px 48px rgba(20, 30, 60, 0.28);
}
.fb-title { font-size: 16px; font-weight: 600; margin-bottom: 12px; line-height: 1.5; }
.fb-label { display: block; font-size: 13px; color: #5a6480; margin-bottom: 12px; }
.fb-input {
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: vertical;
}
.fb-input:focus { border-color: var(--theme); }
.fb-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.fb-thanks { font-size: 14px; line-height: 1.7; color: #1f2430; }
.fb-thanks iframe { width: 100%; border: 0; display: block; }
.article-actions { margin-top: 16px; display: flex; gap: 8px; }

.viewer {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  cursor: zoom-out;
}
.viewer img { max-width: 94vw; max-height: 94vh; }
.hidden { display: none !important; }

/* 老访客二次回来时显示"上次留言 + 客服回复" */
.last-leave {
  margin: 8px 10px 12px;
  padding: 10px 12px;
  border: 1px dashed #d7deee;
  border-radius: 10px;
  background: #f7f9ff;
  font-size: 13px;
  color: #3d465c;
}
.last-leave .ll-head { font-size: 11px; color: #8b93a7; margin-bottom: 6px; }
.last-leave .ll-row { display: flex; gap: 6px; margin-top: 4px; align-items: flex-start; }
.last-leave .ll-tag {
  flex: 0 0 auto;
  padding: 0 6px;
  border-radius: 6px;
  font-size: 11px;
  line-height: 18px;
  background: #eef1f8;
  color: #5a6480;
}
.last-leave .ll-tag.agent { background: #e8f7ee; color: #1a9c53; }
.last-leave .ll-text { flex: 1; word-break: break-word; line-height: 1.6; }

/* 右下角版本号（只做展示，不拦截点击） */
.ver-badge {
  position: fixed;
  right: 10px;
  bottom: 3px;
  z-index: 40;
  font-size: 10px;
  line-height: 1.5;
  color: rgba(120, 130, 150, 0.75);
  pointer-events: none;
  user-select: none;
}

@media (max-width: 640px) {
  .bubble { max-width: 84%; }
  .messages { padding: 8px; }
  .composer { padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); }
  .welcome { padding: 8px; }
  .history-box, .help-inline { margin: 0; }
  .bubble.html-bubble { padding: 2px; border-radius: 8px; }
  .msg-main { max-width: 88%; }
  .msg.agent .msg-main { max-width: 88%; }
  .msg-main.html-msg-main { max-width: 100%; }
  .msg-avatar { width: 30px; height: 30px; flex: 0 0 30px; }
  .logo { height: 40px; max-width: 240px; }
}

/* 移动端点击手感与层级（避免帮助中心无法点选） */
.help-cats, .help-list, .help-inline-title { position: relative; z-index: 2; }
.help-cat-chip, .help-list .help-item {
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(47, 107, 255, 0.15);
  min-height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
}
.help-list .help-item { display: flex; justify-content: flex-start; width: 100%; }
.history-box .his-head { touch-action: manipulation; -webkit-tap-highlight-color: rgba(47, 107, 255, 0.12); -webkit-user-select: none; user-select: none; }

/* HTML5 消息与聊天窗口两边平齐（覆盖 .msg.agent .msg-main 的宽度限制，移动端同样生效） */
.msg.agent .msg-main.html-msg-main,
.msg.visitor .msg-main.html-msg-main {
  max-width: 100%;
  width: 100%;
  align-items: stretch;
}
.bubble.html-bubble { width: 100%; max-width: 100%; }
.msg.agent .bubble.html-bubble,
.msg.visitor .bubble.html-bubble { width: 100%; }
