/* ==================== 主题变量 ==================== */
:root {
  --bg: #f4efe6;
  --surface: #ffffff;
  --surface-2: #faf7f1;
  --sidebar-bg: #efe8da;
  --panel-border: #e3d9c6;
  --text: #34302a;
  --text-muted: #8a8073;
  --text-faint: #b0a695;
  --primary: #9a6f3e;
  --primary-hover: #835a2e;
  --primary-soft: #f0e3d0;
  --accent: #c49356;
  --danger: #c0533a;
  --danger-soft: #f6e4dd;
  --user-bubble: #9a6f3e;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #ffffff;
  --code-bg: #f3eee3;
  --shadow-sm: 0 1px 2px rgba(74, 60, 35, 0.06);
  --shadow-md: 0 4px 16px rgba(74, 60, 35, 0.10);
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
             "Microsoft YaHei", "Helvetica Neue", sans-serif;
  --font-serif: Georgia, "Songti SC", "SimSun", "Noto Serif SC", serif;
  --font-mono: "SF Mono", "JetBrains Mono", "Fira Code", Consolas, monospace;
  /* 对话正文字号（可由 A−/A+ 按钮动态调节，并持久化到 localStorage） */
  --chat-font-size: 14.5px;
}

[data-theme="dark"] {
  --bg: #1c1916;
  --surface: #2a2520;
  --surface-2: #242019;
  --sidebar-bg: #221e18;
  --panel-border: #3a342b;
  --text: #e8e0d2;
  --text-muted: #a89e8c;
  --text-faint: #6e6557;
  --primary: #c49356;
  --primary-hover: #d4a567;
  --primary-soft: #3a2f20;
  --accent: #d4a567;
  --danger: #d96b50;
  --danger-soft: #3a241c;
  --user-bubble: #9a6f3e;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #2a2520;
  --code-bg: #1a1712;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
}

/* ==================== 基础重置 ==================== */
* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; font-size: 14px; color: inherit; }
input::placeholder, textarea::placeholder { color: var(--text-faint); }

/* 滚动条 */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-faint); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ==================== 布局骨架 ==================== */
.app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 18px; height: 58px; flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--panel-border);
  box-shadow: var(--shadow-sm);
  z-index: 20;
}
.topbar-left { display: flex; align-items: center; gap: 14px; }
.topbar-right { display: flex; align-items: center; gap: 8px; }

.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { font-size: 26px; }
.brand-text h1 { font-size: 17px; font-weight: 700; letter-spacing: 0.2px; line-height: 1.2; }
.brand-text p { font-size: 12px; color: var(--text-muted); font-family: var(--font-serif); }

.layout {
  flex: 1; display: flex; min-height: 0;
  position: relative;
}

/* ==================== 侧栏 / 文档面板 ==================== */
.sidebar, .doc-panel {
  flex-shrink: 0;
  background: var(--sidebar-bg);
  display: flex; flex-direction: column;
  transition: margin-left 0.28s ease;
}
.sidebar { width: 360px; border-right: 1px solid var(--panel-border); overflow-y: auto; }
.doc-panel { width: 440px; border-left: 1px solid var(--panel-border); }

.sidebar.collapsed { margin-left: -360px; }
.doc-panel.collapsed { margin-right: -440px; border-left: none; }

/* 面板块 */
.panel {
  border-bottom: 1px solid var(--panel-border);
  background: var(--sidebar-bg);
}
.panel:last-child { border-bottom: none; }
.panel-grow { flex: 1; display: flex; flex-direction: column; min-height: 0; }

/* 对话列表 */
.conv-list { display: flex; flex-direction: column; gap: 4px; }
.conv-item {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 10px; border-radius: 7px;
  cursor: pointer; border: 1px solid transparent;
  transition: background 0.13s, border-color 0.13s;
  font-size: 13px; line-height: 1.4;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface); border-color: var(--panel-border); }
.conv-item-title {
  flex: 1; overflow: hidden; white-space: nowrap;
  text-overflow: ellipsis; color: var(--text);
}
.conv-item.active .conv-item-title { color: var(--primary); font-weight: 600; }
.conv-item-time { font-size: 10.5px; color: var(--text-faint); flex-shrink: 0; }
.conv-item-del {
  flex-shrink: 0; opacity: 0; color: var(--text-faint);
  padding: 2px 4px; border-radius: 4px; font-size: 12px;
  transition: opacity 0.12s, background 0.12s, color 0.12s;
}
.conv-item:hover .conv-item-del { opacity: 1; }
.conv-item-del:hover { background: var(--danger-soft); color: var(--danger); }

.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px 6px;
}
.panel-actions { display: flex; gap: 6px; align-items: center; }
.panel-title { font-size: 13px; font-weight: 700; letter-spacing: 0.3px; }
.panel-body { padding: 4px 18px 18px; display: flex; flex-direction: column; gap: 12px; }

/* ==================== 表单控件 ==================== */
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 12px; color: var(--text-muted); font-weight: 500; }

input[type="text"], input[type="password"], textarea {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
input[type="text"]:focus, input[type="password"]:focus, textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
textarea { resize: vertical; line-height: 1.6; }

.secret-input { position: relative; display: flex; align-items: center; }
.secret-input input { padding-right: 38px; }
.reveal-btn {
  position: absolute; right: 6px; padding: 6px;
  color: var(--text-faint); border-radius: 6px; display: flex;
}
.reveal-btn:hover { color: var(--text-muted); background: var(--surface-2); }

/* Skill 标签页 */
.skill-tabs { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.skill-tab {
  display: flex; flex-direction: column; gap: 3px; align-items: flex-start;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  transition: all 0.15s;
  text-align: left;
}
.skill-tab:hover { border-color: var(--accent); transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.skill-tab.active { background: var(--primary); border-color: var(--primary); color: var(--user-bubble-text); box-shadow: var(--shadow-sm); }
.skill-tab-idx { font-size: 10px; font-weight: 700; opacity: 0.75; text-transform: uppercase; letter-spacing: 0.5px; }
.skill-tab-name { font-size: 13px; font-weight: 600; }

.skill-desc { font-size: 12px; color: var(--text-muted); line-height: 1.5; padding: 0 2px; min-height: 18px; }
.skill-desc:empty { display: none; }

.skill-prompt {
  background: var(--code-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
  color: var(--text-muted);
}

/* 预设模型选择 */
.preset-tabs { display: flex; flex-direction: column; gap: 6px; }
.preset-tab {
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  transition: all 0.15s;
  text-align: left;
}
.preset-tab:hover:not(.active) { border-color: var(--accent); transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.preset-tab.active { background: var(--primary); border-color: var(--primary); color: var(--user-bubble-text); box-shadow: var(--shadow-sm); }
.preset-tab-name { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.preset-tab-model { font-size: 10.5px; opacity: 0.65; font-family: var(--font-mono); }
.preset-tab.not-ready { opacity: 0.6; }
.preset-tab.not-ready.active { opacity: 1; }
.preset-tab .warn-dot { font-size: 11px; }

/* 上传区 */
.upload-zone {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 12px;
  background: var(--surface);
  border: 1.5px dashed var(--panel-border);
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.15s;
}
.upload-zone:hover { border-color: var(--accent); color: var(--primary); background: var(--primary-soft); }
.upload-zone.dragover { border-color: var(--primary); background: var(--primary-soft); color: var(--primary); }

/* 按钮 */
.primary-btn {
  padding: 11px 16px;
  background: var(--primary);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.primary-btn:hover:not(:disabled) { background: var(--primary-hover); }
.primary-btn:active:not(:disabled) { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.apply-row { display: flex; gap: 8px; align-items: stretch; }
.apply-row .primary-btn { flex: 1; }

.mini-btn {
  padding: 5px 10px; font-size: 12px; border-radius: 6px;
  color: var(--text-muted); border: 1px solid var(--panel-border);
  background: var(--surface); transition: all 0.15s;
}
.mini-btn:hover:not(:disabled) { color: var(--text); border-color: var(--accent); }
.mini-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.mini-btn.danger { color: var(--danger); }
.mini-btn.danger:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }

/* 字号调节按钮：用字号差异直观体现功能 */
.font-btn { font-weight: 700; line-height: 1; min-width: 30px; }
#fontDec { font-size: 11px; }
#fontReset { font-size: 13px; }
#fontInc { font-size: 15px; }

.ghost-btn {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 13px; font-weight: 500;
  transition: all 0.15s;
}
.ghost-btn:hover { background: var(--surface-2); color: var(--text); }

.icon-btn {
  padding: 8px; border-radius: var(--radius-sm); color: var(--text-muted);
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ==================== 对话区 ==================== */
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); position: relative; }

.chat-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 20px; flex-shrink: 0;
  border-bottom: 1px solid var(--panel-border);
  min-height: 40px;
}
.chat-status { font-size: 12px; color: var(--text-muted); }
.chat-status.streaming { color: var(--primary); }
.chat-status.error { color: var(--danger); }
.chat-actions { display: flex; gap: 8px; }

.chat-scroll {
  flex: 1; overflow-y: auto; padding: 24px 0;
  scroll-behavior: smooth;
}

/* 「回到底部」悬浮按钮：用户上滚查看历史时显示 */
.scroll-down-btn {
  position: absolute;
  bottom: 96px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  padding: 7px 16px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.15);
  transition: background 0.15s ease, transform 0.15s ease;
}
.scroll-down-btn:hover { background: var(--surface-2); transform: translateX(-50%) translateY(-2px); }

/* 空状态 */
.empty-state {
  height: 100%; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px;
  color: var(--text-muted); text-align: center; padding: 40px;
}
.empty-icon { font-size: 52px; opacity: 0.6; }
.empty-state h2 { font-size: 19px; color: var(--text); font-weight: 600; font-family: var(--font-serif); }
.empty-state p { font-size: 13px; line-height: 1.8; }

/* 消息气泡 */
.msg {
  max-width: 80%; width: 80%; margin: 0 auto 20px; padding: 0 24px;
  display: flex; flex-direction: column; gap: 6px;
  animation: fadeUp 0.25s ease;
}
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.msg-meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-muted); padding: 0 2px; }
.msg-avatar { width: 22px; height: 22px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 12px; flex-shrink: 0; }
.msg-avatar.user { background: var(--primary); color: #fff; }
.msg-avatar.bot { background: var(--primary-soft); color: var(--primary); }
.msg-role { font-weight: 600; color: var(--text); }

.msg-bubble {
  padding: 14px 18px; border-radius: var(--radius);
  font-size: var(--chat-font-size); line-height: 1.75; word-break: break-word;
  position: relative;
}
.msg.user .msg-bubble {
  background: var(--user-bubble); color: var(--user-bubble-text);
  margin-left: 30px; border-bottom-left-radius: 4px;
}
.msg.bot .msg-bubble {
  background: var(--assistant-bubble); border: 1px solid var(--panel-border);
  margin-left: 30px; border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}
.msg.user .msg-bubble a { color: #ffe9c7; }
.msg.user .msg-bubble code { background: rgba(255,255,255,0.18); }

/* markdown 内容样式 */
.msg-bubble p { margin: 0 0 10px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble h1, .msg-bubble h2, .msg-bubble h3, .msg-bubble h4,
.msg-bubble h5, .msg-bubble h6 {
  margin: 16px 0 8px; font-family: var(--font-serif); font-weight: 700; line-height: 1.35;
}
/* 标题用 em 相对单位，跟随 --chat-font-size 等比缩放（A−/A+ 一同放大缩小） */
.msg-bubble h1 { font-size: 1.38em; }
.msg-bubble h2 { font-size: 1.24em; }
.msg-bubble h3 { font-size: 1.16em; }
.msg-bubble h4 { font-size: 1.05em; }
.msg-bubble h5 { font-size: 0.95em; }
.msg-bubble h6 { font-size: 0.85em; }
.msg-bubble ul, .msg-bubble ol { margin: 0 0 10px; padding-left: 22px; }
.msg-bubble li { margin: 3px 0; }
.msg-bubble code {
  font-family: var(--font-mono); font-size: calc(var(--chat-font-size) - 1.5px);
  background: var(--code-bg); padding: 2px 6px; border-radius: 5px;
}
.msg-bubble pre {
  background: var(--code-bg); border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm); padding: 14px; overflow-x: auto; margin: 0 0 10px;
}
.msg-bubble pre code { background: none; padding: 0; font-size: calc(var(--chat-font-size) - 1.5px); line-height: 1.6; }
.msg-bubble blockquote {
  border-left: 3px solid var(--accent); padding: 2px 14px;
  margin: 0 0 10px; color: var(--text-muted); background: var(--surface-2);
  border-radius: 0 6px 6px 0;
}
.msg-bubble table { border-collapse: collapse; margin: 0 0 10px; width: 100%; font-size: 0.92em; }
.msg-bubble th, .msg-bubble td { border: 1px solid var(--panel-border); padding: 7px 10px; text-align: left; }
.msg-bubble th { background: var(--surface-2); font-weight: 600; }
.msg-bubble hr { border: none; border-top: 1px solid var(--panel-border); margin: 14px 0; }
.msg-bubble a { color: var(--primary); }
.msg-bubble strong { font-weight: 700; }

/* 光标 */
.cursor::after {
  content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; margin-left: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

/* 复制按钮 */
.msg-actions { display: flex; gap: 6px; margin-left: 30px; opacity: 0; transition: opacity 0.15s; }
.msg.bot:hover .msg-actions { opacity: 1; }
.copy-btn {
  padding: 3px 9px; font-size: 11px; border-radius: 5px;
  color: var(--text-faint); border: 1px solid var(--panel-border);
  background: var(--surface);
}
.copy-btn:hover { color: var(--primary); border-color: var(--accent); }

/* 错误气泡 */
.msg.error .msg-bubble { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }

/* 输入栏 */
.chat-input-bar {
  display: flex; align-items: flex-end; gap: 10px;
  padding: 14px 24px 18px; flex-shrink: 0;
  background: var(--bg); border-top: 1px solid var(--panel-border);
}
#msgInput {
  flex: 1; resize: none; max-height: 160px; min-height: 44px;
  padding: 12px 14px; line-height: 1.5;
  background: var(--surface); border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}
#msgInput:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }

.send-btn {
  width: 44px; height: 44px; flex-shrink: 0;
  border-radius: 50%; background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
}
.send-btn:hover:not(:disabled) { background: var(--primary-hover); }
.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ==================== 文档面板 ==================== */
.doc-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; border-bottom: 1px solid var(--panel-border);
  background: var(--sidebar-bg); flex-shrink: 0;
}
.doc-content {
  flex: 1; overflow-y: auto; padding: 22px 24px;
  font-family: var(--font-serif); font-size: var(--chat-font-size); line-height: 1.8; color: var(--text);
}
.doc-content h1, .doc-content h2, .doc-content h3, .doc-content h4,
.doc-content h5, .doc-content h6 { margin: 22px 0 10px; font-weight: 700; line-height: 1.35; }
/* 标题用 em 相对单位，跟随 --chat-font-size 等比缩放（A−/A+ 一同放大缩小） */
.doc-content h1 { font-size: 1.52em; }
.doc-content h2 { font-size: 1.24em; }
.doc-content h3 { font-size: 1.16em; }
.doc-content h4 { font-size: 1.05em; }
.doc-content h5 { font-size: 0.95em; }
.doc-content h6 { font-size: 0.85em; }
.doc-content p { margin: 0 0 12px; }
.doc-content ul, .doc-content ol { margin: 0 0 12px; padding-left: 22px; }
.doc-content li { margin: 4px 0; }
.doc-content code { font-family: var(--font-mono); background: var(--code-bg); padding: 2px 6px; border-radius: 5px; font-size: 13px; }
.doc-content pre { background: var(--code-bg); border: 1px solid var(--panel-border); border-radius: var(--radius-sm); padding: 14px; overflow-x: auto; margin: 0 0 14px; }
.doc-content pre code { background: none; padding: 0; white-space: pre-wrap; word-break: break-word; }
.doc-content blockquote { border-left: 3px solid var(--accent); padding: 2px 14px; margin: 0 0 12px; color: var(--text-muted); }
.doc-content hr { border: none; border-top: 1px solid var(--panel-border); margin: 18px 0; }
.doc-content a { color: var(--primary); }

/* 遮罩（移动端，由 JS 控制显隐） */
.overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,0.35); z-index: 15; backdrop-filter: blur(2px);
}
.overlay.show { display: block; }

/* ==================== 响应式 ==================== */
@media (max-width: 1100px) {
  .sidebar { position: fixed; top: 58px; bottom: 0; left: 0; z-index: 16; box-shadow: var(--shadow-md); }
  .sidebar.collapsed { margin-left: -360px; }
  .doc-panel { position: fixed; top: 58px; bottom: 0; right: 0; z-index: 16; box-shadow: var(--shadow-md); }
  .doc-panel.collapsed { margin-right: -440px; }
}
@media (max-width: 640px) {
  .sidebar, .doc-panel { width: 100%; }
  .sidebar.collapsed { margin-left: -100%; }
  .doc-panel.collapsed { margin-right: -100%; }
  .brand-text h1 { font-size: 15px; }
  .brand-text p { display: none; }
  .msg { padding: 0 14px; }
  .chat-input-bar { padding: 12px 14px 14px; }
}
