/* ============================================================
   知识问答前端样式（自建，无框架）
   原则：不写 !important。布局用 flex 应用壳（body 100vh，
   头/底固定、中间唯一滚动），吸顶吸底是布局天然结果。
   移动端只用媒体查询，不做 JS 打类。
   ============================================================ */
:root {
  --accent: #4da8b0;
  --accent-weak: #eef2ff;
  --text: #1f2937;
  --text-2: #6b7280;
  --text-3: #9ca3af;
  --line: #e5e7eb;
  --bg: #f6f7fb;
  --card: #ffffff;
  --warn: #b45309;
  --danger: #dc2626;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 8px 24px rgba(16, 24, 40, .06);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font: 14px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Microsoft YaHei", "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ===== 应用壳：头 / 滚动区 / 输入区 ===== */
.app {
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.brand-logo {
  width: 30px; height: 30px; flex: 0 0 auto;
  border-radius: 9px;
  background: linear-gradient(135deg, #6366f1, #4338ca);
  color: #fff;
  font-weight: 700;
  display: grid;
  place-items: center;
  font-size: 15px;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.brand-text strong { font-size: 15px; letter-spacing: .2px; }
.brand-text em { font-style: normal; font-size: 12px; color: var(--text-3); }

/* ===== 主导航（分段控件） ===== */
.main-nav {
  display: flex;
  gap: 2px;
  padding: 3px;
  background: #f1f2f6;
  border-radius: 10px;
  flex: 0 0 auto;
}
.main-nav button {
  border: none;
  background: transparent;
  padding: 6px 14px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--text-2);
}
.main-nav button:hover { color: var(--text); }
.main-nav button.active {
  background: #fff;
  color: var(--accent);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(16, 24, 40, .08);
}

.topbar-right { display: flex; align-items: center; gap: 10px; flex: 0 0 auto; }
.app[data-page="lab"] #chat_head_controls { display: none; }

/* ===== 页面容器：一次只显示一页，页面内部各自管理滚动 ===== */
.pages { flex: 1 1 auto; min-height: 0; display: flex; }
.page { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.page[hidden] { display: none; }

.field { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-2); }

select, textarea, button { font: inherit; color: inherit; }

select {
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
  cursor: pointer;
}

button {
  border: 1px solid var(--line);
  background: #fff;
  padding: 7px 14px;
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
  white-space: nowrap;
}
button:hover { border-color: #c7cbd4; }
button:disabled { opacity: .55; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #4338ca; }
button.danger { background: #fee2e2; border-color: #fecaca; color: var(--danger); }
button.ghost { background: transparent; }

/* ===== 对话滚动区 ===== */
.chat-scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  /* 流式期间每轮重绘都会 scrollToBottom()：smooth 会让每次赋值都启动一段
     平滑滚动动画，动画不断被下一轮打断、目标又在变，视觉上就是上下抖动
     （2026-11-20 客户反馈）。改为瞬时跟随，用户手动滚动仍照常流畅。 */
  scroll-behavior: auto;
}

.chat-list {
  max-width: 920px;
  margin: 0 auto;
  padding: 22px 20px 28px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

/* 空会话欢迎语 */
.welcome { text-align: center; padding: 8vh 8px 0; color: var(--text-2); }
.welcome h1 { font-size: 26px; margin: 0 0 10px; color: var(--text); font-weight: 650; }
.welcome p { margin: 0; font-size: 13px; line-height: 1.8; }

/* ===== 消息 ===== */
.msg { display: flex; flex-direction: column; gap: 6px; }

.msg.user { align-items: flex-end; }

.msg.user .bubble {
  max-width: min(78%, 640px);
  background: #eef1f4;          /* 2026-11-13 需求：用户气泡浅灰底 */
  color: #1f2937;
  padding: 10px 14px;
  border-radius: 14px 14px 4px 14px;
  white-space: pre-wrap;

  word-break: break-word;
}

.msg.assistant { align-items: stretch; }

.msg.assistant .answer {
  background: none;             /* 2026-11-13 需求：回答卡去背景，直接落在页面底色上 */
  border: none;
  box-shadow: none;
  border-radius: var(--radius);
  padding: 16px 18px;
  word-break: break-word;
}

.answer p { margin: 0 0 10px; }
.answer p:last-child { margin-bottom: 0; }

.answer h1, .answer h2, .answer h3, .answer h4 {
  margin: 18px 0 8px;
  line-height: 1.35;
  font-weight: 650;
}
.answer h1 { font-size: 19px; }
.answer h2 { font-size: 17px; }
.answer h3 { font-size: 15px; }
.answer h4 { font-size: 14px; }
.answer > :first-child { margin-top: 0; }

.answer ul, .answer ol { margin: 8px 0 12px; padding-left: 22px; }
.answer li { margin: 3px 0; }

.answer strong { font-weight: 650; }

.answer code {
  background: #f3f4f6;
  border-radius: 5px;
  padding: 1px 5px;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.answer pre {
  background: #0f172a;
  color: #e2e8f0;
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
}
.answer pre code { background: none; color: inherit; padding: 0; }

.table-scroll { overflow-x: auto; margin: 10px 0; }

.answer table { border-collapse: collapse; width: 100%; font-size: 13px; }
.answer th, .answer td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; }
.answer th { background: #f9fafb; font-weight: 600; white-space: nowrap; }

.answer blockquote {
  margin: 12px 0 0;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  background: var(--accent-weak);
  border-radius: 0 8px 8px 0;
  color: #4338ca;
  font-size: 13px;
}
.answer blockquote p { margin: 0; }

.answer img {
  max-width: 100%;
  /* width/height 属性由 stabilizeImage 按图片固有尺寸写回，用于预留比例空间；
     height:auto 保证按容器宽度等比缩放而不是被属性高度拉变形 */
  height: auto;
  border-radius: 10px;
  border: 1px solid var(--line);
  margin: 8px 0;
  cursor: zoom-in;
}

.answer a { color: var(--accent); text-decoration: none; border-bottom: 1px dashed currentColor; }
.answer a:hover { border-bottom-style: solid; }

/* 等待态 */
.msg.assistant .pending {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-2);
  font-size: 13px;
}
.spinner {
  width: 15px; height: 15px; flex: 0 0 auto;
  border: 2px solid #dbe0ea;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* 回答底部工具条 */
.answer-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-3);
}
.answer-tools .spacer { flex: 1 1 auto; }
.answer-tools button {
  padding: 4px 10px;
  font-size: 12px;
  border-radius: 8px;
  color: var(--text-2);
}
.answer-tools .meta { color: var(--text-3); }
.summary-line {
  margin-top: 6px;
  font-size: 13px;
  color: #4338ca;
  background: var(--accent-weak);
  border-radius: 8px;
  padding: 8px 12px;
}

/* ===== 输入区 ===== */
.composer {
  flex: 0 0 auto;
  padding: 10px 20px calc(10px + env(safe-area-inset-bottom)) 20px;
  background: linear-gradient(to bottom, rgba(246, 247, 251, .2), var(--bg) 40%);
  border-top: 1px solid var(--line);
}

.composer-box {
  max-width: 920px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px;
  box-shadow: var(--shadow);
}

#msg_input {
  flex: 1 1 auto;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  padding: 8px 10px;
  max-height: 160px;
  line-height: 1.6;
}

.composer-foot {
  max-width: 920px;
  margin: 6px auto 0;
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-3);
  padding: 0 4px;
}

.conn { color: var(--text-3); }
.conn.err { color: var(--danger); }

/* ===== toast ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  background: #111827;
  color: #fff;
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 99;
  max-width: min(86vw, 480px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .2);
}

/* ===== 移动端（纯媒体查询） ===== */
@media (max-width: 768px) {
  .topbar { padding: 8px 12px; gap: 8px; }
  .brand-text em { display: none; }
  .brand-text strong { font-size: 13px; white-space: nowrap; }
  .main-nav button { padding: 5px 9px; font-size: 12px; }
  .field > span { display: none; }
  select { padding: 6px 8px; font-size: 13px; }
  .chat-list { padding: 14px 12px 20px; gap: 14px; }
  .welcome { padding-top: 6vh; }
  .welcome h1 { font-size: 21px; }
  .msg.user .bubble { max-width: 86%; }
  .msg.assistant .answer { padding: 13px 14px; border-radius: 12px; }
  .composer { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)) 10px; }
  .answer table { font-size: 12px; }
  #new_topic_btn { padding: 6px 10px; }
}
