:root {
  --bg: #f7f7f8;
  --sidebar-bg: #ececee;
  --panel: #ffffff;
  --text: #202124;
  --text-muted: #787a80;
  --border: #e3e3e6;
  --hover: #e2e2e5;
  --active: #dcdce0;
  --accent: #5b5bd6;
  --accent-hover: #4d4dc4;
  --accent-text: #ffffff;
  --user-bubble: #e9e9f5;
  --assistant-bubble: #ffffff;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  --code-bg: #1e1e24;
}

body[data-theme="dark"] {
  --bg: #1b1b1f;
  --sidebar-bg: #141416;
  --panel: #25252a;
  --text: #ededf0;
  --text-muted: #9a9aa2;
  --border: #36363d;
  --hover: #303036;
  --active: #3a3a42;
  --accent: #8b8bf0;
  --accent-hover: #9c9cf5;
  --accent-text: #141416;
  --user-bubble: #35354a;
  --assistant-bubble: #2b2b31;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  --code-bg: #121216;
}

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

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

/* ---------- Sidebar ---------- */
.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 10px 10px;
  transition: margin-left 0.25s ease;
  z-index: 30;
}
.sidebar.collapsed { margin-left: -260px; }

.sidebar-top { display: flex; align-items: center; justify-content: space-between; padding: 0 6px 14px; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 17px; }
.brand-icon { color: var(--accent); font-size: 20px; }
.collapse-btn { display: none; }

.new-chat-btn {
  display: flex; align-items: center; justify-content: center; gap: 6px;
  width: 100%; padding: 10px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); border-radius: 10px;
  font-size: 14px; cursor: pointer; transition: background 0.15s;
}
.new-chat-btn:hover { background: var(--hover); }
.plus { font-size: 17px; line-height: 1; color: var(--accent); }

.chat-list { flex: 1; overflow-y: auto; margin-top: 12px; padding-right: 2px; }
.chat-list::-webkit-scrollbar, .messages::-webkit-scrollbar { width: 6px; }
.chat-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.chat-item {
  display: flex; align-items: center; gap: 8px; padding: 9px 10px;
  border-radius: 9px; cursor: pointer; margin-bottom: 3px; position: relative;
}
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); font-weight: 600; }
.chat-icon { font-size: 14px; opacity: 0.7; flex-shrink: 0; }
.chat-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.chat-del {
  opacity: 0; border: none; background: transparent; color: var(--text-muted);
  cursor: pointer; font-size: 15px; padding: 0 3px; border-radius: 4px;
}
.chat-item:hover .chat-del { opacity: 1; }
.chat-del:hover { color: #d9534f; background: var(--panel); }

.sidebar-bottom { border-top: 1px solid var(--border); padding-top: 8px; }
.side-link {
  display: flex; align-items: center; gap: 9px; width: 100%; padding: 9px 10px;
  background: transparent; border: none; color: var(--text); border-radius: 9px;
  cursor: pointer; font-size: 13px; text-align: left;
}
.side-link:hover { background: var(--hover); }
.side-link span { font-size: 16px; color: var(--accent); }
.side-note { font-size: 11px; color: var(--text-muted); padding: 4px 10px 2px; }
.version-tag { display: inline-block; background: var(--accent); color: var(--accent-text); border-radius: 5px; padding: 0 6px; font-weight: 700; font-size: 10.5px; }

.sidebar-mask { display: none; }

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

.topbar {
  height: 58px; flex-shrink: 0; display: flex; align-items: center;
  padding: 0 20px; border-bottom: 1px solid var(--border); background: var(--bg);
}
.menu-btn { display: none; margin-right: 10px; }
.topbar-title { font-weight: 600; font-size: 15px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.model-badge {
  font-size: 12px; color: var(--text-muted); border: 1px solid var(--border);
  padding: 3px 10px; border-radius: 20px; max-width: 200px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.icon-btn {
  width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center;
  background: transparent; border: none; border-radius: 8px; color: var(--text);
  cursor: pointer; font-size: 17px; transition: background 0.15s;
}
.icon-btn:hover { background: var(--hover); }

.messages { flex: 1; overflow-y: auto; padding: 24px 20px 10px; }

.welcome { max-width: 620px; margin: 12vh auto 0; text-align: center; }
.welcome-icon { font-size: 44px; color: var(--accent); margin-bottom: 10px; }
.welcome h1 { font-size: 26px; font-weight: 700; margin-bottom: 10px; }
.welcome p { color: var(--text-muted); font-size: 14px; margin-bottom: 28px; }
.welcome-tips { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.tip-card {
  padding: 11px 15px; border: 1px solid var(--border); border-radius: 12px;
  background: var(--panel); color: var(--text); cursor: pointer;
  font-size: 13px; transition: transform 0.15s, border-color 0.15s;
}
.tip-card:hover { transform: translateY(-2px); border-color: var(--accent); }

.message { display: flex; gap: 12px; margin-bottom: 22px; max-width: 820px; margin-left: auto; margin-right: auto; }
.message.user { flex-direction: row-reverse; }
.avatar {
  width: 32px; height: 32px; flex-shrink: 0; border-radius: 9px;
  display: flex; align-items: center; justify-content: center; font-size: 15px; font-weight: 700;
}
.message.user .avatar { background: var(--user-bubble); color: var(--accent); }
.message.assistant .avatar { background: var(--accent); color: var(--accent-text); }
.bubble {
  padding: 9px 14px; border-radius: 14px; background: var(--assistant-bubble);
  border: 1px solid var(--border); min-width: 0; overflow-wrap: anywhere;
}
.message.user .bubble { background: var(--user-bubble); border-color: transparent; border-top-right-radius: 4px; }
.message.assistant .bubble { border-top-left-radius: 4px; }
.bubble.empty::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

.bubble p { margin: 0.3em 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: 0.6em 0 0.3em; line-height: 1.35; }
.bubble h1 { font-size: 1.3em; } .bubble h2 { font-size: 1.18em; } .bubble h3 { font-size: 1.08em; }
.bubble ul { margin: 0.4em 0; padding-left: 1.4em; }
.bubble li { margin: 0.15em 0; }
.bubble blockquote { border-left: 3px solid var(--accent); padding-left: 10px; color: var(--text-muted); margin: 0.4em 0; }
.bubble a { color: var(--accent); }
.bubble hr { border: none; border-top: 1px solid var(--border); margin: 0.8em 0; }
.inline-code { background: var(--hover); padding: 1px 5px; border-radius: 5px; font-family: Consolas, Monaco, monospace; font-size: 0.9em; }
.reasoning { border-left: 2px solid var(--accent); background: rgba(91,91,214,0.06); border-radius: 6px; margin-bottom: 8px; font-size: 12.5px; }
.reasoning summary { cursor: pointer; color: var(--text-muted); padding: 5px 10px; user-select: none; }
.reasoning-body { padding: 2px 10px 8px; color: var(--text-muted); line-height: 1.6; white-space: pre-wrap; overflow-wrap: anywhere; }
.code-block { background: var(--code-bg); color: #e6e6e6; border-radius: 9px; overflow: hidden; margin: 0.6em 0; }
.code-header { display: flex; justify-content: space-between; align-items: center; padding: 5px 12px; background: rgba(255,255,255,0.07); font-size: 11px; color: #b9b9c2; }
.copy-code-btn { background: transparent; border: 1px solid #55555e; color: #d0d0d8; border-radius: 5px; cursor: pointer; font-size: 11px; padding: 1px 8px; }
.copy-code-btn:hover { background: rgba(255,255,255,0.1); }
.code-block code { display: block; padding: 12px; overflow-x: auto; font-family: Consolas, Monaco, monospace; font-size: 12.5px; line-height: 1.55; }

.msg-actions { display: flex; gap: 4px; margin-top: 5px; }
.msg-action { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 12px; padding: 2px 6px; border-radius: 5px; }
.msg-action:hover { background: var(--hover); color: var(--text); }

/* ---------- Composer ---------- */
.composer-wrap { padding: 8px 20px 12px; background: var(--bg); flex-shrink: 0; }
.composer {
  max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 8px 8px 8px 14px; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none; background: transparent;
  color: var(--text); font: inherit; line-height: 1.5; max-height: 180px;
  padding: 5px 0;
}
.composer textarea::placeholder { color: var(--text-muted); }
.composer-actions { display: flex; align-items: center; gap: 4px; }
.send-btn {
  width: 36px; height: 36px; border-radius: 10px; border: none;
  background: var(--accent); color: var(--accent-text); cursor: pointer;
  font-size: 16px; display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }
#stopBtn { color: #d9534f; font-size: 14px; }
.composer-hint { text-align: center; color: var(--text-muted); font-size: 11px; margin-top: 7px; }

/* ---------- Modal ---------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px;
}
.modal {
  width: 100%; max-width: 580px; max-height: 90vh; display: flex; flex-direction: column;
  background: var(--panel); border-radius: 16px; box-shadow: var(--shadow); overflow: hidden;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { font-size: 16px; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.notice { background: rgba(91,91,214,0.09); border: 1px solid rgba(91,91,214,0.25); border-radius: 9px; padding: 10px 12px; font-size: 12.5px; line-height: 1.65; margin-bottom: 18px; }
.notice strong { color: var(--accent); }

.field { display: block; margin-bottom: 15px; }
.field-label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field-label em { color: var(--accent); font-style: normal; font-weight: 700; }
.field-help { display: block; font-size: 11px; color: var(--text-muted); margin-top: 4px; }
.field input, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); font: inherit; font-size: 13px; outline: none;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }
.field textarea { resize: vertical; line-height: 1.5; }
.pw-row { display: flex; gap: 6px; }
.pw-row input { flex: 1; }
.mini-btn { padding: 0 12px; border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 8px; cursor: pointer; font-size: 12px; }
.mini-btn:hover { background: var(--hover); }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.preset-row { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; margin-bottom: 16px; }
.preset-row .field-label { margin: 0 2px 0 0; font-weight: 400; color: var(--text-muted); font-size: 12px; }
.preset-btn { border: 1px solid var(--border); background: var(--bg); color: var(--text); border-radius: 7px; padding: 4px 9px; font-size: 12px; cursor: pointer; }
.preset-btn:hover { border-color: var(--accent); color: var(--accent); }
.test-line { display: flex; align-items: center; gap: 12px; }
.test-btn { border: 1px solid var(--accent); background: transparent; color: var(--accent); border-radius: 8px; padding: 7px 14px; cursor: pointer; font-size: 13px; }
.test-btn:hover { background: rgba(91,91,214,0.08); }
.test-btn:disabled { opacity: 0.5; cursor: wait; }
.test-result { font-size: 12px; color: var(--text-muted); }
.test-result.ok { color: #2e9e63; } .test-result.err { color: #d9534f; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); }
.cancel-btn, .save-btn { padding: 9px 20px; border-radius: 9px; cursor: pointer; font-size: 13px; }
.cancel-btn { border: 1px solid var(--border); background: var(--panel); color: var(--text); }
.save-btn { border: none; background: var(--accent); color: var(--accent-text); font-weight: 600; }
.save-btn:hover { background: var(--accent-hover); }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .sidebar { position: fixed; top: 0; bottom: 0; left: 0; }
  .sidebar.collapsed { margin-left: -260px; }
  .sidebar-mask.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 20; }
  .collapse-btn, .menu-btn { display: inline-flex; }
  .messages { padding: 16px 12px 6px; }
  .composer-wrap { padding: 6px 10px 10px; }
  .welcome { margin-top: 8vh; }
  .model-badge { display: none; }
  .two-col { grid-template-columns: 1fr; }
}
