/* ==================== 主样式：布局、气泡、输入框、浮动按钮 ==================== */

/* ====== CSS Variables ====== */
:root {
  --primary: #2DB366;
  --primary-dark: #1A924D;
  --bg-body: #f0f2f5;
  --bg-card: #ffffff;
  --bg-input: #f5f6fa;
  --bg-header: linear-gradient(135deg, #2DB366 0%, #1A924D 100%);
  --text-primary: #333333;
  --text-secondary: #666666;
  --text-muted: #999999;
  --border-color: #e8e8e8;
  --shadow-sm: 0 1px 6px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.10);
  --bubble-user-bg: linear-gradient(135deg, #2DB366 0%, #1A924D 100%);
  --bubble-user-color: #fff;
  --bubble-ai-bg: #fff;
  --bubble-ai-color: #333;
  --bubble-ai-shadow: 0 1px 6px rgba(0,0,0,0.06);
  --header-title-color: #fff;
  --header-sub-color: rgba(255,255,255,0.75);
  --input-border: #ddd;
  --scrollbar-track: transparent;
  --welcome-text-color: #1A924D;
  --card-hover-shadow: 0 3px 12px rgba(0,0,0,0.10);
  --resize-handle-color: rgba(45,179,102,0.35);
}
/* 暗色模式仅作用于 .page 窗口内部，不影响全局 body */
.page[data-theme="dark"] {
  --bg-body: #1a1a1a;
  --bg-card: #262626;
  --bg-input: #333333;
  --bg-header: linear-gradient(135deg, #1a7a44 0%, #145232 100%);
  --text-primary: #e8e8e8;
  --text-secondary: #b0b0b0;
  --text-muted: #777777;
  --border-color: #3a3a3a;
  --shadow-sm: 0 1px 6px rgba(0,0,0,0.25);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.40);
  --bubble-ai-bg: #2d2d2d;
  --bubble-ai-color: #e0e0e0;
  --bubble-ai-shadow: 0 1px 6px rgba(0,0,0,0.30);
  --header-sub-color: rgba(255,255,255,0.7);
  --input-border: #444;
  --welcome-text-color: #4ade80;
  --card-hover-shadow: 0 3px 12px rgba(0,0,0,0.35);
  --resize-handle-color: rgba(74,222,128,0.25);
}

/* ====== CSS Reset & Base ====== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  height: 100vh;
  background-color: #f0f2f5; /* 固定浅色背景，不受暗色模式影响 */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  overflow: hidden;
  font-size: 14px;
}

/* ====== 浮动窗口容器 ====== */
.page {
  position: fixed;
  top: calc(50% - 290px);
  left: calc(50% - 260px);
  width: 520px;
  height: 580px;
  min-width: 560px;  /* ★ 最小宽度限制 */
  min-height: 420px;
  display: flex;
  flex-direction: column;
  border-radius: 14px;
  background: var(--bg-card);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  z-index: 10000;
  transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
.drag-region { cursor: move; user-select: none; -webkit-user-select: none; }
.resize-handle {
  position: absolute; bottom: 0; right: 0;
  width: 20px; height: 20px; cursor: nwse-resize; z-index: 200;
}
.resize-handle::before, .resize-handle::after {
  content: ''; position: absolute;
  background: var(--text-muted); border-radius: 1.5px; opacity: 0.5; transition: opacity 0.15s;
}
.resize-handle::before { bottom: 5px; right: 4px; width: 12px; height: 2.5px; transform: rotate(-45deg); }
.resize-handle::after { bottom: 8px; right: 7px; width: 7px; height: 2.5px; transform: rotate(-45deg); }
.resize-handle:hover::before, .resize-handle:hover::after { opacity: 1; }

/* ====== Header（Logo+标题 左 / 图标按钮 右）====== */
.header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 16px; background: var(--bg-header); flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 10px; }
.header-logo-img { width: 32px; height: 32px; flex-shrink: 0; filter: brightness(0) invert(1); }
.page[data-theme="dark"] .header-logo-img { filter: none; }
.header-info { display: flex; flex-direction: column; min-width: 0; }  /* ★ min-width:0 允许 flex 子项收缩 */
.header-title {
  font-size: 15px; font-weight: 600; color: var(--header-title-color); letter-spacing: 0.5px;
  white-space: nowrap;           /* ★ 禁止换行 */
  overflow: hidden;              /* ★ 溢出隐藏 */
  text-overflow: ellipsis;       /* ★ 显示省略号 */
}
.header-status-row { display: flex; align-items: center; gap: 5px; margin-top: 2px; }
.status-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: #7FE7A5; box-shadow: 0 0 4px rgba(127,231,165,0.6); flex-shrink: 0;
  transition: background 0.3s, box-shadow 0.3s;
}
.status-dot.offline { background: #ff4d4f; box-shadow: 0 0 4px rgba(255,77,79,0.6); }
.status-label { font-size: 11px; color: var(--header-sub-color); }

.header-right { display: flex; align-items: center; gap: 4px; flex-shrink: 0; }
.header-icon-btn {
  width: 32px; height: 32px; border-radius: 8px; border: none;
  background: rgba(255,255,255,0.15); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; transition: background 0.15s, transform 0.1s; padding: 0; line-height: 1;
}
.header-icon-btn:hover { background: rgba(255,255,255,0.28); }
.header-icon-btn:active { transform: scale(0.92); }
.header-icon-btn svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* 人工客服按钮（图标+文字，比普通按钮宽） */
.human-service-btn {
  width: auto !important;
  padding: 0 10px !important;
  gap: 4px;
  background: rgba(255,255,255,0.22) !important;
}
.human-service-btn:hover { background: rgba(255,255,255,0.36) !important; }
.human-service-text {
  font-size: 12px; font-weight: 500; letter-spacing: 0.5px;
  white-space: nowrap; line-height: 1;
}

/* ====== 聊天消息区域 ====== */
.msg-container {
  flex: 1; height: 0; padding: 12px 14px; padding-bottom: 8px;
  overflow-x: hidden; overflow-y: auto; scroll-behavior: smooth;
  background: var(--bg-body); transition: background-color 0.3s ease;
}
.msg-container::-webkit-scrollbar { width: 3px !important; }
.msg-container::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.15); border-radius: 3px; }
.page[data-theme="dark"] .msg-container::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); }

/* 欢迎区域 */
.welcome-section { margin-top: 20px; width: 100%; }
.welcome-card {
  background: var(--bg-card); border-radius: 12px; padding: 22px 18px;
  text-align: center; box-shadow: var(--shadow-sm); transition: background 0.3s, box-shadow 0.3s;
}
.welcome-text { display: block; font-size: 17px; font-weight: bold; color: var(--welcome-text-color); margin-bottom: 8px; }
.welcome-desc { display: block; font-size: 12px; color: var(--text-muted); line-height: 1.5; }

/* 引导卡片内部元素样式 */
.quick-cards { margin-top: 12px; width: 100%; }
.quick-card-num {
  width: 24px; height: 24px; border-radius: 6px; background: var(--bg-header); color: #fff;
  font-size: 11px; font-weight: bold; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; margin-right: 8px;
}
.quick-card-body { display: flex; flex-direction: column; overflow: hidden; }
.quick-card-title {
  font-size: 13px; font-weight: 600; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 1.4;
}
.quick-card-desc {
  font-size: 11px; color: var(--text-muted); margin-top: 2px;
  line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ====== 消息行（无头像，纯气泡对齐）====== */
.msg-row { display: flex; align-items: flex-start; margin-bottom: 14px; }
.msg-row.right { justify-content: flex-end; }
.msg-row.left { justify-content: flex-start; }

/* 气泡 */
.bubble {
  max-width: 82%; padding: 9px 14px; border-radius: 12px;
  word-break: break-all; line-height: 1.55; min-width: 0; position: relative;
}
.bubble.user {
  background: var(--bubble-user-bg); color: var(--bubble-user-color);
  border-bottom-right-radius: 3px; box-shadow: 0 2px 7px rgba(45,179,102,0.20);
}
.bubble.assistant {
  background: var(--bubble-ai-bg); color: var(--bubble-ai-color);
  box-shadow: var(--bubble-ai-shadow); border-bottom-left-radius: 3px;
  transition: background 0.3s, color 0.3s, box-shadow 0.3s;
}
.bubble.loading { min-width: 70px; }
.msg-text { font-size: 14px; white-space: pre-wrap; word-break: break-word; }

/* ==================== 🎨 Markdown 富文本样式 ==================== */
/* 以下样式限定在 .msg-text 作用域内，避免污染全局 */

/* 标题 */
.msg-text h1, .msg-text h2, .msg-text h3,
.msg-text h4, .msg-text h5, .msg-text h6 {
  margin-top: 0.7em;
  margin-bottom: 0.4em;
  font-weight: 600;
  line-height: 1.4;
  color: inherit;
}
.msg-text h1 { font-size: 1.25em; }
.msg-text h2 { font-size: 1.15em; }
.msg-text h3 { font-size: 1.08em; }

/* 段落（聊天场景：极紧凑，像微信/IM 一样的段落间距） */
.msg-text p { margin: 0; }
.msg-text p:empty,
.msg-text p:empty + p { display: none; }  /* 隐藏空段落（\n\n 产生的） */
/* 相邻段落之间只留一行间距 */
.msg-text p + p { margin-top: 0.6em; }

/* 文本格式 */
.msg-text strong, .msg-text b { font-weight: 600; color: inherit; }
.msg-text em, .msg-text i { font-style: italic; }
.msg-text u { text-decoration: underline; }
.msg-text s, .msg-text del, .msg-text strike { text-decoration: line-through; opacity: 0.75; }

/* 分隔线 */
.msg-text hr {
  border: none;
  border-top: 1px solid var(--border-color);
  margin: 0.6em 0;
}

/* 链接 */
.msg-text a {
  color: var(--primary);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s;
}
.msg-text a:hover { border-bottom-color: var(--primary); }

/* 图片 */
.msg-text img {
  max-width: 100%;
  border-radius: 6px;
  margin: 0.4em 0;
}

/* 列表 */
.msg-text ul, .msg-text ol {
  margin: 0.35em 0;
  padding-left: 1.4em;
}
.msg-text li {
  margin: 0.18em 0;
  line-height: 1.6;
}
.msg-text ul { list-style-type: disc; }
.msg-text ol { list-style-type: decimal; }
.msg-text li > ul, .msg-text li > ol { margin: 0.1em 0; } /* 嵌套列表紧凑 */

/* 引用块 */
.msg-text blockquote {
  margin: 0.5em 0;
  padding: 0.4em 0.8em;
  border-left: 3px solid var(--primary);
  background: rgba(45, 179, 102, 0.06);
  border-radius: 0 6px 6px 0;
  color: var(--text-secondary);
}
.page[data-theme="dark"] .msg-text blockquote {
  background: rgba(74, 222, 128, 0.06);
}
.msg-text blockquote p { margin: 0.2em 0; }

/* 代码 */
.msg-text code {
  font-family: 'SF Mono', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 0.88em;
  background: rgba(0, 0, 0, 0.055);
  padding: 0.15em 0.4em;
  border-radius: 4px;
  word-break: break-all;
  color: #d73a49;
}
.page[data-theme="dark"] .msg-text code {
  background: rgba(255, 255, 255, 0.1);
  color: #f97583;
}
/* 行内代码不在 pre 内时才用小圆角背景 */
.msg-text pre > code {
  display: block;
  background: transparent;
  padding: 0;
  border-radius: 0;
  font-size: 0.85em;
  line-height: 1.55;
  color: inherit;
}

/* 代码块 */
.msg-text pre {
  margin: 0.5em 0;
  padding: 0.65em 0.85em;
  background: #f6f8fa;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.page[data-theme="dark"] .msg-text pre {
  background: #161b22;
  border-color: #30363d;
}

/* 表格 */
.msg-text table {
  width: 100%;
  margin: 0.5em 0;
  border-collapse: collapse;
  font-size: 0.93em;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  overflow: hidden;
}
.msg-text thead {
  background: linear-gradient(135deg, rgba(45,179,102,0.08) 0%, rgba(26,146,77,0.04) 100%);
}
.page[data-theme="dark"] .msg-text thead {
  background: linear-gradient(135deg, rgba(74,222,128,0.08) 0%, rgba(20,82,50,0.04) 100%);
}
.msg-text th {
  padding: 0.45em 0.7em;
  font-weight: 600;
  text-align: left;
  border-bottom: 2px solid var(--primary);
  color: var(--text-primary);
  white-space: nowrap;
}
.msg-text td {
  padding: 0.4em 0.7em;
  border-bottom: 1px solid var(--border-color);
  vertical-align: top;
  word-break: break-word;
}
.msg-text tr:last-child td { border-bottom: none; }
.msg-text tbody tr:hover {
  background: rgba(45, 179, 102, 0.03);
}
.page[data-theme="dark"] .msg-text tbody tr:hover {
  background: rgba(255, 255, 255, 0.03);
}

/* 详情折叠（<details>/<summary>） */
.msg-text details {
  margin: 0.4em 0;
  padding: 0.4em 0.6em;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  background: rgba(0,0,0,0.01);
}
.msg-text summary {
  cursor: pointer;
  font-weight: 500;
  color: var(--primary);
  user-select: none;
  outline: none;
}
.msg-text summary:hover { opacity: 0.8; }

/* 加载中动画 */
.thinking-box { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 4px 0; }
.typing-dots { display: flex; gap: 4px; padding: 4px 0; }
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-muted); animation: typing 1.2s infinite ease-in-out;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
.thinking-text { font-size: 12px; color: var(--text-muted); }
@keyframes typing {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* 错误提示 */
.error-text { color: #e74c3c; font-size: 14px; }

/* ====== 底部输入区域 ====== */
.input-area {
  padding: 8px 14px; padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--bg-card); border-top: 1px solid var(--border-color);
  flex-shrink: 0; transition: background 0.3s, border-color 0.3s;
}
.input-wrapper {
  display: flex; align-items: end; gap: 8px;
  background: var(--bg-input); border: 1px solid var(--input-border); border-radius: 12px;
  padding: 9px 7px 8px 13px; transition: border-color 0.2s, box-shadow 0.2s, background 0.3s;
}
.input-wrapper:focus-within { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(45,179,102,0.12); }

.chat-input {
  flex: 1; max-height: 100px; min-height: 29px;
  background: transparent; border: none; outline: none;
  font-size: 14px; color: var(--text-primary); line-height: 1.6;
  resize: none; font-family: inherit; padding: 2px 0;
}
.chat-input::placeholder { color: var(--text-muted); }
.chat-input:disabled { opacity: 0.55; }

.send-btn {
  width: 28px; height: 28px; border-radius: 7px; border: none;
  background: var(--primary); color: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: opacity 0.15s, transform 0.1s, background 0.15s; padding: 0; line-height: 1; margin-bottom: 2px;
}
.send-btn:hover:not(.disabled) { background: var(--primary-dark); }
.send-btn:active:not(.disabled) { transform: scale(0.93); }
.send-btn.disabled { opacity: 0.38; cursor: not-allowed; }
.send-btn svg { width: 14px; height: 14px; fill: currentColor; stroke: currentColor; }

.disclaimer {
  margin-top: 6px; font-size: 11px; color: var(--text-muted);
  text-align: center; line-height: 1.4; opacity: 0.7;
}

/* ====== 浮动触发按钮 ====== */
.float-trigger {
  position: fixed; right: 28px; bottom: 32px;
  width: 54px; height: 54px; border-radius: 50%;
  background: linear-gradient(135deg, #2DB366 0%, #1A924D 100%);
  border: none; cursor: pointer; z-index: 9999;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 4px 16px rgba(45,179,102,0.35);
  transition: transform 0.2s ease, box-shadow 0.2s ease; padding: 0; line-height: 1;
}
.float-trigger:hover { transform: scale(1.08); box-shadow: 0 6px 22px rgba(45,179,102,0.45); }
.float-trigger:active { transform: scale(0.95); }
.float-trigger svg { width: 30px; height: 30px; }
/* 未读消息小红点 */
.float-trigger .badge-dot {
  position: absolute; top: 4px; right: 4px;
  width: 12px; height: 12px; border-radius: 50%;
  background: #ff4d4f; border: 2px solid #fff; display: none;
}
.float-trigger.has-msg .badge-dot { display: block; }
