/* ============ 主题变量 ============ */
:root, [data-theme="dark"] {
  --bg: #16171c;
  --bg-side: #1e1f24;
  --bg-input: #2b2c33;
  --bg-hover: #34353c;
  --bg-active: #3a3b42;
  --bg-bubble-user: #4d6bfe22;
  --bg-card: #1e1f24;
  --bg-code: #0d0e12;
  --text: #e8e8ed;
  --text-2: #a0a0ab;
  --text-3: #6b6c77;
  --primary: #4d6bfe;
  --primary-hover: #6180ff;
  --border: #34353c;
  --scrollbar: #3a3b42;
  --shadow: 0 4px 24px rgba(0, 0, 0, .4);
  --danger: #f56c6c;
}

[data-theme="light"] {
  --bg: #f9f9f9;
  --bg-side: #f3f4f6;
  --bg-input: #ffffff;
  --bg-hover: #e9eaee;
  --bg-active: #dde0e6;
  --bg-bubble-user: #4d6bfe14;
  --bg-card: #ffffff;
  --bg-code: #f5f6f8;
  --text: #282a30;
  --text-2: #6b6f76;
  --text-3: #9b9ea6;
  --primary: #4d6bfe;
  --primary-hover: #3a58ee;
  --border: #e3e5e8;
  --scrollbar: #cfd1d6;
  --shadow: 0 4px 24px rgba(0, 0, 0, .08);
  --danger: #e5484d;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

/* hidden 属性优先级高于组件自身的 display 声明，防止浮动工具条等元素重叠显示 */
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: "Helvetica Neue", "PingFang SC", "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  overflow: hidden;
  transition: background .25s ease, color .25s ease;
}

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 3px; }
::-webkit-scrollbar-track { background: transparent; }

button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, textarea { font-family: inherit; color: inherit; }

/* ============ 侧边栏 ============ */
.sidebar {
  width: 260px;
  min-width: 260px;
  height: 100%;
  background: var(--bg-side);
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  gap: 10px;
  transition: margin-left .25s ease, background .25s ease;
  z-index: 20;
}
body.side-collapsed .sidebar { margin-left: -260px; }

.sidebar-header { display: flex; align-items: center; justify-content: space-between; padding: 0 6px; }

.logo { display: flex; align-items: center; gap: 6px; text-decoration: none; color: var(--primary); }
.logo-icon { width: 30px; height: 30px; display: block; }
.logo-text { font-size: 21px; font-weight: 700; letter-spacing: .5px; }

.sidebar-actions { display: flex; gap: 2px; }

.icon-btn {
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 8px; color: var(--text-2);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn svg { width: 19px; height: 19px; }
.icon-btn.small { width: 28px; height: 28px; }
.icon-btn.small svg { width: 16px; height: 16px; }

/* 搜索框 */
.search-bar {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-input); border-radius: 10px; padding: 4px 6px 4px 12px;
}
.search-bar svg { width: 16px; height: 16px; color: var(--text-3); flex: none; }
.search-bar input {
  flex: 1; background: none; border: none; outline: none;
  font-size: 14px; color: var(--text); padding: 6px 0;
}
.search-bar input::placeholder { color: var(--text-3); }

.new-chat-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 11px 0;
  background: var(--primary); color: #fff;
  border-radius: 10px; font-size: 15px; font-weight: 500;
  transition: background .15s;
}
.new-chat-btn:hover { background: var(--primary-hover); }
.new-chat-btn svg { width: 18px; height: 18px; }

.history-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px 2px; font-size: 12px; color: var(--text-3);
}

.history-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; padding-right: 2px; }

.history-group-label { font-size: 12px; color: var(--text-3); padding: 12px 10px 4px; }

.history-item {
  position: relative;
  display: flex; align-items: center;
  padding: 9px 10px; border-radius: 8px;
  font-size: 14px; color: var(--text);
  cursor: pointer; transition: background .12s;
}
.history-item span.title {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.history-item:hover { background: var(--bg-hover); }
.history-item.active { background: var(--bg-active); }
.history-item .item-more {
  width: 24px; height: 24px; flex: none;
  display: none; align-items: center; justify-content: center;
  border-radius: 6px; color: var(--text-2);
}
.history-item:hover .item-more { display: inline-flex; }
.history-item .item-more:hover { background: var(--bg-hover); color: var(--text); }
.history-item .item-more svg { width: 15px; height: 15px; }
/* 内联重命名：就地高亮当前项，标题变为可编辑输入框 */
.history-item.editing { background: var(--bg-active); }
.history-item.editing:hover { background: var(--bg-active); }
.rename-input {
  flex: 1; min-width: 0; font: inherit; color: var(--primary);
  background: transparent; border: none; outline: none; caret-color: var(--primary);
  border-bottom: 1.5px solid var(--primary); padding: 0 1px 1px;
}
.history-item .pin-mark { flex: none; width: 13px; height: 13px; margin-right: 5px; color: var(--primary); display: inline-flex; }
.history-item .pin-mark svg { width: 100%; height: 100%; }

.history-empty { text-align: center; color: var(--text-3); font-size: 13px; padding: 30px 0; }

.sidebar-footer {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 6px 2px; border-top: 1px solid var(--border);
}
.user-info { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--bg-active); color: var(--text-2);
  display: inline-flex; align-items: center; justify-content: center;
}
.avatar svg { width: 18px; height: 18px; }
.username { font-size: 14px; color: var(--text); }
.usage-badge {
  font-size: 11px; color: var(--text-3); flex-shrink: 0;
  background: var(--bg-active); border-radius: 10px; padding: 2px 8px; white-space: nowrap;
}

/* ============ 折叠浮动条 ============ */
.collapsed-area {
  position: fixed; top: 26px; left: 14px; z-index: 30;
  transform: translateY(-50%);
  display: flex; align-items: center; gap: 10px;
}
.collapsed-area .collapsed-logo { display: flex; align-items: center; color: var(--primary); }
.collapsed-area .collapsed-logo .logo-icon { width: 28px; height: 28px; }
.collapsed-bar {
  display: flex; align-items: center; gap: 2px;
  background: var(--bg-side); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 6px;
  box-shadow: var(--shadow);
}
/* 主题按钮统一放到左侧；收起侧栏时隐藏顶栏右侧的重复主题按钮 */
body.side-collapsed .top-bar .theme-toggle-main { display: none; }

/* ============ 主区域 ============ */
.main {
  flex: 1; height: 100%;
  display: flex; flex-direction: column;
  position: relative; min-width: 0;
}

.top-bar {
  height: 52px; flex: none;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px;
}
/* 问题1：侧栏收起后，顶栏标题左移让位，避免与左上角 logo+工具条重叠 */
body.side-collapsed .top-bar { padding-left: 196px; }
.top-title { font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 欢迎页 */
.welcome {
  flex: 1; display: flex; align-items: center; justify-content: center;
  padding-bottom: 12vh;
}
/* 新对话（欢迎态）：欢迎语 + 输入框作为一组在页面正中，输入框落在欢迎文字下方 */
body:not(.has-chat) .main { justify-content: center; }
body:not(.has-chat) .welcome {
  flex: none; padding-bottom: 0;
}
body:not(.has-chat) .composer-wrap {
  flex: none; margin-top: 28px;
}
.welcome-title {
  display: flex; align-items: center; gap: 14px;
  font-size: 30px; font-weight: 700;
  animation: fadeUp .4s ease;
}
.welcome-logo { width: 40px; height: 40px; display: block; }

/* 聊天区 */
.chat-view { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.chat-list {
  max-width: 760px; margin: 0 auto;
  padding: 20px 16px 40px;
  display: flex; flex-direction: column; gap: 26px;
}

.msg { display: flex; flex-direction: column; gap: 8px; animation: fadeUp .25s ease; }
.msg.user { align-items: flex-end; }
.msg.user .msg-body {
  background: var(--bg-bubble-user);
  border-radius: 14px 14px 4px 14px;
  padding: 12px 16px; max-width: 85%;
  white-space: pre-wrap; word-break: break-word; font-size: 15px; line-height: 1.7;
}
.msg.ai { align-items: flex-start; }
.msg.ai .msg-body { max-width: 100%; width: 100%; font-size: 15px; line-height: 1.75; }

.msg-files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.msg-file {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-2);
  background: var(--bg-input); border: 1px solid var(--border);
  padding: 4px 10px; border-radius: 999px;
}
.msg-file svg { width: 13px; height: 13px; }

/* 思考过程 */
.thinking-box {
  border-left: 2px solid var(--border);
  margin: 2px 0 8px; padding: 2px 0 2px 14px;
  color: var(--text-2); font-size: 13.5px;
}
.thinking-head {
  display: flex; align-items: center; gap: 6px;
  cursor: pointer; user-select: none; font-size: 13.5px; color: var(--text-2);
}
.thinking-head:hover { color: var(--text); }
.thinking-head svg { width: 14px; height: 14px; transition: transform .2s; }
.thinking-box.open .thinking-head svg.arrow { transform: rotate(90deg); }
.thinking-content { display: none; margin-top: 8px; line-height: 1.7; white-space: pre-wrap; }
.thinking-box.open .thinking-content { display: block; }

/* 搜索结果引用 */
.search-status {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--text-2); margin-bottom: 8px;
}
.refs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.ref-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--text-2);
  background: var(--bg-input); border: 1px solid var(--border);
  padding: 4px 10px; border-radius: 999px; cursor: default;
}
.ref-chip b { color: var(--primary); font-weight: 600; }

/* Markdown 渲染 */
.md p { margin: 0 0 10px; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0 0 10px; padding-left: 22px; }
.md li { margin: 3px 0; }
.md h1, .md h2, .md h3 { margin: 14px 0 8px; line-height: 1.4; }
.md h1 { font-size: 20px; } .md h2 { font-size: 18px; } .md h3 { font-size: 16px; }
.md code {
  background: var(--bg-code); border: 1px solid var(--border);
  padding: 1px 6px; border-radius: 5px; font-size: 13px;
  font-family: Consolas, "JetBrains Mono", monospace;
}
.md pre {
  background: var(--bg-code); border: 1px solid var(--border);
  border-radius: 10px; padding: 14px 16px; margin: 0 0 12px;
  overflow-x: auto; position: relative;
}
.md pre code { background: none; border: none; padding: 0; font-size: 13px; line-height: 1.6; display: block; }
.md pre .copy-code {
  position: absolute; top: 8px; right: 8px;
  font-size: 11px; color: var(--text-3);
  background: var(--bg-hover); border-radius: 6px; padding: 3px 8px;
}
.md pre .copy-code:hover { color: var(--text); }
.md blockquote { border-left: 3px solid var(--primary); padding: 2px 12px; margin: 0 0 10px; color: var(--text-2); background: var(--bg-bubble-user); border-radius: 0 8px 8px 0; }
.md table { border-collapse: collapse; margin: 0 0 12px; font-size: 14px; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 12px; }
.md th { background: var(--bg-input); }

/* 消息操作 */
.msg-actions { display: flex; gap: 2px; opacity: 0; transition: opacity .15s; margin-top: 2px; }
.msg:hover .msg-actions { opacity: 1; }
.msg-time { font-size: 11px; color: var(--text-3); opacity: .7; margin-top: 4px; padding: 0 2px; }

/* 打字光标 */
.cursor::after {
  content: "▍"; color: var(--primary);
  animation: blink 1s infinite; margin-left: 1px;
}

/* 停止按钮 */
.stop-row { display: flex; justify-content: center; margin-top: 4px; }
.stop-btn {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--text-2);
  border: 1px solid var(--border); border-radius: 999px; padding: 6px 16px;
}
.stop-btn:hover { background: var(--bg-hover); color: var(--text); }
.stop-btn svg { width: 13px; height: 13px; }

/* ============ 输入区 ============ */
.composer-wrap {
  flex: none; width: 100%;
  padding: 0 16px 14px;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.welcome ~ .composer-wrap .composer { max-width: 900px; }
.composer {
  width: 100%; max-width: 760px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 18px 18px 12px;
  transition: border-color .15s, background .25s ease;
}
body.has-chat .composer { max-width: 800px; }
.composer:focus-within { border-color: var(--primary); }

#chatInput {
  width: 100%; background: none; border: none; outline: none; resize: none;
  font-size: 15.5px; line-height: 1.6; min-height: 56px; max-height: 220px; overflow-y: auto;
  color: var(--text);
}
#chatInput::placeholder { color: var(--text-3); }

.composer-bottom { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.composer-tools { display: flex; gap: 8px; }

.tool-chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13.5px; color: var(--text-2);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 6px 14px; transition: all .15s;
}
.tool-chip svg { width: 15px; height: 15px; }
.tool-chip:hover { color: var(--text); background: var(--bg-hover); }
.tool-chip.on { color: var(--primary); border-color: var(--primary); background: var(--bg-bubble-user); }

.composer-right { display: flex; align-items: center; gap: 6px; }

.send-btn {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  transition: background .15s, opacity .15s;
}
.send-btn svg { width: 17px; height: 17px; }
.send-btn:disabled { opacity: .35; cursor: not-allowed; }
.send-btn:not(:disabled):hover { background: var(--primary-hover); }

.composer-hint { font-size: 11.5px; color: var(--text-3); text-align: center; }

.attach-preview {
  display: flex; flex-wrap: wrap; gap: 8px; width: 100%; max-width: 760px;
}
.attach-item {
  display: inline-flex; align-items: center; gap: 8px;
  font-size: 12.5px; color: var(--text-2);
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 999px; padding: 5px 12px;
}
.attach-item svg { width: 14px; height: 14px; }
.attach-item button { color: var(--text-3); display: inline-flex; }
.attach-item button:hover { color: var(--danger); }
.attach-item button svg { width: 12px; height: 12px; }

/* ============ 弹出菜单 / Toast ============ */
.context-menu {
  position: fixed; z-index: 100;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 12px; box-shadow: var(--shadow);
  padding: 6px; min-width: 150px;
  display: flex; flex-direction: column;
}
.context-menu button {
  display: flex; align-items: center; gap: 11px;
  width: 100%; text-align: left; font-size: 14px; color: var(--text);
  padding: 9px 10px; border-radius: 8px;
}
.context-menu button svg { width: 17px; height: 17px; flex: none; }
.context-menu button:hover { background: var(--bg-hover); }
.context-menu button.danger {
  color: var(--danger); margin-top: 4px;
  background: rgba(245, 108, 108, .10);
}
.context-menu button.danger:hover { background: rgba(245, 108, 108, .18); }

.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  background: var(--bg-active); color: var(--text);
  font-size: 13.5px; padding: 10px 20px; border-radius: 999px;
  box-shadow: var(--shadow); z-index: 200;
  animation: fadeUp .2s ease;
}

@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes blink { 50% { opacity: 0; } }

/* ============ 设置弹窗 ============ */
.modal-mask {
  position: fixed; inset: 0; z-index: 300;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .45); animation: fadeUp .15s ease;
}
.modal-mask[hidden] { display: none; }
.modal {
  width: min(440px, 92vw); background: var(--bg-side);
  border: 1px solid var(--border); border-radius: 16px;
  box-shadow: var(--shadow); overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 18px; font-size: 16px; font-weight: 600;
  border-bottom: 1px solid var(--border);
}
.modal-body { padding: 18px; display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 13px; color: var(--text-2); }
.field input, .field select {
  font: inherit; font-size: 14px; color: var(--text);
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 10px; padding: 9px 12px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--primary); }
.field-tip { margin: 0; font-size: 12px; line-height: 1.6; color: var(--text-3); }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 14px 18px; border-top: 1px solid var(--border);
}
.btn-primary, .btn-ghost {
  font: inherit; font-size: 14px; cursor: pointer;
  border-radius: 10px; padding: 8px 18px; transition: all .15s;
}
.btn-primary { background: var(--primary); color: #fff; border: 1px solid var(--primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text-2); border: 1px solid var(--border); }
.btn-ghost:hover { color: #e5484d; border-color: #e5484d; }

/* ===== 登录 / 注册弹窗 ===== */
.auth-modal { width: min(400px, 92vw); }
.auth-tabs { display: flex; gap: 6px; padding: 14px 18px 0; }
.auth-tab {
  flex: 1; font: inherit; font-size: 14px; cursor: pointer;
  padding: 8px 0; border-radius: 10px; border: 1px solid var(--border);
  background: transparent; color: var(--text-2); transition: all .15s;
}
.auth-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.auth-error {
  margin: 0; font-size: 13px; color: #e5484d;
  background: rgba(229,72,77,.08); border: 1px solid rgba(229,72,77,.25);
  border-radius: 10px; padding: 8px 12px;
}
.auth-hint { margin: 0; padding: 12px 18px 16px; font-size: 12px; line-height: 1.6; color: var(--text-3); text-align: center; }

/* ============ 响应式 ============ */
@media (max-width: 760px) {
  .sidebar { position: fixed; left: 0; top: 0; box-shadow: var(--shadow); }
  body.side-collapsed .sidebar { margin-left: -260px; box-shadow: none; }
  .welcome-title { font-size: 22px; }
  .welcome-logo { width: 30px; height: 30px; }
}
