:root {
  --bg: #0f1117;
  --bg-side: #151821;
  --bg-main: #0f1117;
  --bg-card: #1b1f2b;
  --bg-input: #1e2230;
  --bg-hover: #222735;
  --border: #2a2f3d;
  --text: #e8eaf0;
  --text-dim: #9aa3b5;
  --text-faint: #6b7280;
  --accent: #4d6bfe;
  --accent-2: #6d8bff;
  --user-bubble: #2b3a6e;
  --ai-bubble: #1b1f2b;
  --danger: #ef4444;
  --ok: #34d399;
  --radius: 14px;
  --shadow: 0 8px 30px rgba(0,0,0,.35);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

a { color: inherit; text-decoration: none; }

.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

/* ---------- 侧边栏 ---------- */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg-side);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform .28s ease;
  z-index: 30;
}
.sidebar-header { padding: 16px 14px 12px; }
.logo {
  display: flex; align-items: center; gap: 8px;
  font-size: 17px; font-weight: 700; padding: 0 4px 14px;
}
.logo-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 12px var(--accent);
}
.new-chat-btn {
  width: 100%; display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg-card); color: var(--text); font-size: 14px; cursor: pointer;
  transition: background .15s, border-color .15s;
}
.new-chat-btn:hover { background: var(--bg-hover); border-color: var(--accent); }

.session-list { flex: 1; overflow-y: auto; padding: 8px; }
.session-item {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-radius: 10px; cursor: pointer;
  color: var(--text-dim); font-size: 14px; margin-bottom: 2px;
  transition: background .12s;
  position: relative;
}
.session-item:hover { background: var(--bg-hover); }
.session-item.active { background: var(--bg-hover); color: var(--text); }
.session-title {
  flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-tokens { font-size: 11px; color: var(--text-faint); flex-shrink: 0; }
.session-actions { display: none; gap: 4px; flex-shrink: 0; }
.session-item:hover .session-actions { display: flex; }
.session-actions button {
  background: none; border: none; color: var(--text-faint); cursor: pointer;
  padding: 3px; border-radius: 5px; display: flex;
}
.session-actions button:hover { color: var(--text); background: rgba(255,255,255,.08); }
.session-actions button.del:hover { color: var(--danger); }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }
.settings-link {
  display: flex; align-items: center; gap: 8px; padding: 9px 12px;
  border-radius: 9px; color: var(--text-dim); font-size: 14px;
}
.settings-link:hover { background: var(--bg-hover); color: var(--text); }

/* ---------- 主区域 ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg-main); }

.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: rgba(15,17,23,.85); backdrop-filter: blur(8px);
  flex-wrap: wrap;
}
.icon-btn {
  background: none; border: none; color: var(--text-dim); cursor: pointer;
  padding: 8px; border-radius: 8px; display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.menu-btn { display: none; }

.model-wrap { flex: 1; max-width: 260px; }
.model-select {
  width: 100%; padding: 8px 12px; border-radius: 9px;
  background: var(--bg-input); border: 1px solid var(--border); color: var(--text);
  font-size: 14px; cursor: pointer; outline: none;
}
.model-select:focus { border-color: var(--accent); }

.think-toggle {
  display: flex; align-items: center; gap: 6px; cursor: pointer;
  font-size: 13px; color: var(--text-dim); user-select: none;
  padding: 8px 12px; border-radius: 9px; border: 1px solid var(--border); background: var(--bg-input);
}
.think-toggle input { display: none; }
.think-toggle .think-label { display: flex; align-items: center; gap: 6px; }
.think-toggle .think-label::before {
  content: ""; width: 16px; height: 16px; border-radius: 5px;
  border: 2px solid var(--text-faint); transition: all .15s; flex-shrink: 0;
}
.think-toggle input:checked + .think-label::before {
  background: var(--accent); border-color: var(--accent);
  box-shadow: inset 0 0 0 3px var(--bg-input);
}

.token-info { display: flex; align-items: center; gap: 8px; min-width: 150px; }
.token-bar {
  flex: 1; height: 6px; border-radius: 3px; background: var(--bg-input);
  overflow: hidden; min-width: 70px;
}
.token-bar-fill {
  height: 100%; width: 0%; border-radius: 3px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: width .4s ease;
}
.token-text { font-size: 12px; color: var(--text-dim); font-family: var(--mono); white-space: nowrap; }

/* ---------- 聊天区 ---------- */
.chat-area { flex: 1; overflow-y: auto; padding: 20px 0; scroll-behavior: smooth; }

.welcome {
  max-width: 720px; margin: 8vh auto 0; text-align: center; padding: 0 20px;
}
.welcome-logo { font-size: 52px; margin-bottom: 16px; }
.welcome h1 { font-size: 26px; margin-bottom: 10px; font-weight: 700; }
.welcome p { color: var(--text-dim); margin-bottom: 24px; }
.welcome-tips { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.welcome-tips span {
  padding: 7px 14px; border-radius: 20px; font-size: 13px;
  background: var(--bg-card); border: 1px solid var(--border); color: var(--text-dim);
}

.msg-row {
  max-width: 860px; margin: 0 auto 18px; padding: 0 20px;
  display: flex; gap: 12px;
}
.msg-row.user { flex-direction: row-reverse; }
.msg-avatar {
  width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 16px;
  background: var(--bg-card); border: 1px solid var(--border);
}
.msg-row.user .msg-avatar { background: var(--accent); border-color: var(--accent); }
.msg-body { max-width: 78%; min-width: 0; }
.msg-content {
  padding: 12px 16px; border-radius: var(--radius); line-height: 1.65;
  font-size: 15px; word-break: break-word; overflow-wrap: break-word;
  background: var(--ai-bubble); border: 1px solid var(--border);
}
.msg-row.user .msg-content {
  background: var(--user-bubble); border-color: transparent; color: #fff;
}
.msg-meta { font-size: 11px; color: var(--text-faint); margin-top: 5px; display: flex; gap: 10px; align-items: center; }
.msg-row.user .msg-meta { justify-content: flex-end; }

/* 思考过程折叠 */
.reasoning-block {
  margin-bottom: 8px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg-card); overflow: hidden;
}
.reasoning-head {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px;
  font-size: 12px; color: var(--text-dim); cursor: pointer; user-select: none;
}
.reasoning-head .chev { transition: transform .2s; }
.reasoning-block.open .reasoning-head .chev { transform: rotate(90deg); }
.reasoning-body {
  display: none; padding: 4px 12px 10px; font-size: 13px; color: var(--text-dim);
  border-top: 1px dashed var(--border); white-space: pre-wrap; word-break: break-word;
}
.reasoning-block.open .reasoning-body { display: block; }

/* 流式光标 */
.streaming-cursor {
  display: inline-block; width: 2px; height: 1em; background: var(--accent);
  vertical-align: text-bottom; animation: blink 1s steps(2) infinite; margin-left: 2px;
}
@keyframes blink { 50% { opacity: 0; } }

/* 图片 */
.msg-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 6px; }
.msg-images img {
  max-width: 200px; max-height: 200px; border-radius: 10px; border: 1px solid var(--border);
  object-fit: cover; cursor: zoom-in;
}

/* Markdown 样式 */
.msg-content pre {
  background: #0c0e14; border: 1px solid var(--border); border-radius: 8px;
  padding: 12px; overflow-x: auto; margin: 10px 0; font-family: var(--mono); font-size: 13px;
}
.msg-content code { font-family: var(--mono); font-size: .9em; }
.msg-content :not(pre) > code {
  background: rgba(255,255,255,.08); padding: 2px 6px; border-radius: 5px;
}
.msg-content p { margin: 6px 0; }
.msg-content ul, .msg-content ol { padding-left: 22px; margin: 6px 0; }
.msg-content h1, .msg-content h2, .msg-content h3, .msg-content h4 { margin: 12px 0 6px; }
.msg-content blockquote {
  border-left: 3px solid var(--accent); padding: 2px 12px; margin: 8px 0;
  color: var(--text-dim); background: rgba(77,107,254,.06);
}
.msg-content table { border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.msg-content th, .msg-content td { border: 1px solid var(--border); padding: 6px 10px; }
.msg-content th { background: var(--bg-hover); }
.msg-content a { color: var(--accent-2); text-decoration: underline; }
.msg-content img { max-width: 100%; border-radius: 8px; }

/* ---------- 输入区 ---------- */
.composer { padding: 10px 16px 12px; }
.composer-box {
  max-width: 860px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--bg-input); border: 1px solid var(--border); border-radius: 16px;
  padding: 8px 10px; transition: border-color .15s;
}
.composer-box:focus-within { border-color: var(--accent); }
.chat-input {
  flex: 1; background: none; border: none; outline: none; color: var(--text);
  font-size: 15px; line-height: 1.5; resize: none; max-height: 180px;
  font-family: inherit; padding: 6px 4px;
}
.chat-input::placeholder { color: var(--text-faint); }
.send-btn {
  width: 38px; height: 38px; border-radius: 12px; border: none; cursor: pointer;
  background: var(--accent); color: #fff; display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: opacity .15s, transform .1s;
}
.send-btn:hover { opacity: .9; }
.send-btn:active { transform: scale(.94); }
.send-btn:disabled { opacity: .4; cursor: not-allowed; }

.image-previews {
  max-width: 860px; margin: 0 auto 8px; display: flex; gap: 8px; flex-wrap: wrap;
}
.image-preview { position: relative; }
.image-preview img { width: 72px; height: 72px; border-radius: 10px; object-fit: cover; border: 1px solid var(--border); }
.image-preview .rm {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  background: var(--danger); color: #fff; border: none; cursor: pointer; font-size: 12px; line-height: 1;
}
.composer-hint { text-align: center; font-size: 11px; color: var(--text-faint); margin-top: 6px; }

/* ---------- 加载态 ---------- */
.typing { display: inline-flex; gap: 4px; align-items: center; padding: 4px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-dim); animation: bounce 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity:.4; } 30% { transform: translateY(-5px); opacity:1; } }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px);
  background: #2a2f3d; color: #fff; padding: 10px 18px; border-radius: 10px;
  font-size: 14px; box-shadow: var(--shadow); opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s; z-index: 100; max-width: 80vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- 遮罩 ---------- */
.overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 20; }
.overlay.show { display: block; }

/* ---------- 滚动条 ---------- */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: #3a4050; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #4a5060; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- 响应式 ---------- */
@media (max-width: 820px) {
  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: flex; }
  .msg-body { max-width: 88%; }
  .think-toggle { padding: 8px 10px; }
  .token-info { min-width: 0; }
  .token-bar { display: none; }
}
