/* ============ 设计令牌 ============ */
:root {
  --bg: #0d0e12;
  --bg-2: #12141a;
  --panel: #171a22;
  --panel-2: #1d212c;
  --border: #262b37;
  --border-soft: #202531;
  --text: #e9ebf2;
  --text-2: #9aa1b0;
  --text-3: #5f6775;
  --accent: #ff6b3d;
  --accent-soft: rgba(255, 107, 61, 0.14);
  --accent-border: rgba(255, 107, 61, 0.35);
  --user-bubble: #2b3040;
  --user-bubble-text: #eef0f6;
  --code-bg: #0b0d12;
  --danger: #ff5f5f;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, "JetBrains Mono", monospace;
}

[data-theme="light"] {
  --bg: #f4f5f7;
  --bg-2: #ffffff;
  --panel: #ffffff;
  --panel-2: #f0f1f4;
  --border: #e2e4ea;
  --border-soft: #eceef2;
  --text: #1c1f26;
  --text-2: #5a6170;
  --text-3: #8a91a0;
  --accent: #e0551f;
  --accent-soft: rgba(224, 85, 31, 0.1);
  --accent-border: rgba(224, 85, 31, 0.35);
  --user-bubble: #e0551f;
  --user-bubble-text: #ffffff;
  --code-bg: #f0f1f4;
  --danger: #d63b3b;
  --shadow: 0 10px 30px rgba(30, 34, 45, 0.12);
}

/* ============ 基础 ============ */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; cursor: pointer; }

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

/* ============ 侧边栏 ============ */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--bg-2);
  border-right: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}
.sidebar-top { padding: 16px 14px 12px; border-bottom: 1px solid var(--border-soft); }
.brand { display: flex; align-items: center; gap: 9px; margin-bottom: 14px; }
.brand-mark { width: 26px; height: 26px; color: var(--accent); flex-shrink: 0; }
.brand-name { font-size: 15px; font-weight: 700; letter-spacing: 0.2px; }

.btn {
  border: none; border-radius: var(--radius-sm);
  font-size: 13.5px; font-weight: 600;
  padding: 9px 14px; transition: background 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  width: 100%; color: #fff;
  background: linear-gradient(135deg, #ff6b3d, #ff8f4d);
  box-shadow: 0 4px 14px rgba(255, 107, 61, 0.25);
}
.btn-primary:hover { filter: brightness(1.06); }
.btn-ghost {
  width: 100%; color: var(--text-2);
  background: transparent; border: 1px solid var(--border);
}
.btn-ghost:hover { color: var(--text); background: var(--panel-2); }

.conv-list { flex: 1; overflow-y: auto; padding: 10px 8px; }
.conv-list::-webkit-scrollbar { width: 6px; }
.conv-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.conv-item {
  display: flex; align-items: center; gap: 8px;
  width: 100%; text-align: left;
  padding: 9px 10px; margin-bottom: 2px;
  border-radius: var(--radius-sm); border: none;
  background: transparent; color: var(--text-2); font-size: 13px;
}
.conv-item .conv-icon { flex-shrink: 0; opacity: 0.65; font-size: 12px; }
.conv-item .conv-text {
  flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-item .conv-del {
  opacity: 0; border: none; background: none; color: var(--text-3);
  font-size: 14px; padding: 2px 4px; border-radius: 5px;
}
.conv-item:hover { background: var(--panel-2); color: var(--text); }
.conv-item:hover .conv-del { opacity: 1; }
.conv-item .conv-del:hover { color: var(--danger); }
.conv-item.active { background: var(--panel-2); color: var(--text); }
.conv-item.active .conv-del { opacity: 1; }

.sidebar-bottom { padding: 10px 14px 14px; border-top: 1px solid var(--border-soft); }
.settings-toggle-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.settings-label { font-size: 12.5px; font-weight: 600; color: var(--text-3); letter-spacing: 0.5px; }

.icon-btn {
  border: none; background: transparent; color: var(--text-2);
  width: 30px; height: 30px; border-radius: 8px; font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.settings { border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--panel); }
.settings summary {
  list-style: none; cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
  padding: 11px 13px; font-size: 13.5px; font-weight: 600;
}
.settings summary::-webkit-details-marker { display: none; }
.settings .chev { transition: transform 0.2s; font-size: 11px; color: var(--text-3); }
.settings[open] .chev { transform: rotate(180deg); }
.settings-body { padding: 4px 13px 13px; display: flex; flex-direction: column; gap: 13px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 12.5px; font-weight: 600; color: var(--text-2); }
.field-label span { color: var(--accent); font-variant-numeric: tabular-nums; }
textarea, select, input[type="range"] { font-family: inherit; }
textarea, select {
  width: 100%; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 13px; padding: 8px 10px; resize: vertical;
}
textarea:focus, select:focus { outline: none; border-color: var(--accent-border); }
select { cursor: pointer; }
.field-hint { margin: 0; font-size: 11.5px; color: var(--text-3); line-height: 1.5; }

/* 开关 */
.toggle {
  width: 44px; height: 24px; border-radius: 12px; border: none;
  background: var(--border); position: relative; transition: background 0.2s;
}
.toggle .knob {
  position: absolute; top: 2px; left: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff; transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.toggle[aria-checked="true"] { background: var(--accent); }
.toggle[aria-checked="true"] .knob { transform: translateX(20px); }

input[type="range"] { accent-color: var(--accent); padding: 0; }

.sidebar-foot { margin: 12px 2px 0; font-size: 11px; color: var(--text-3); line-height: 1.6; }

/* ============ 主区 ============ */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 18px; border-bottom: 1px solid var(--border-soft);
}
.menu-btn { display: none; }
.chat-title { flex: 1; margin: 0; font-size: 15px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-badge {
  font-size: 11.5px; color: var(--text-2);
  background: var(--panel); border: 1px solid var(--border);
  padding: 4px 10px; border-radius: 999px;
  font-family: var(--mono);
}

.messages-wrap { flex: 1; overflow-y: auto; position: relative; }
.messages-wrap::-webkit-scrollbar { width: 8px; }
.messages-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.messages { max-width: 820px; margin: 0 auto; padding: 22px 20px 40px; display: flex; flex-direction: column; gap: 26px; }

.empty-state {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px;
  color: var(--text-2); text-align: center; padding: 0 20px;
}
.empty-state h2 { margin: 10px 0 0; font-size: 20px; font-weight: 700; color: var(--text); }
.empty-state p { margin: 0; font-size: 13.5px; color: var(--text-3); }
.empty-mark {
  width: 62px; height: 62px; border-radius: 20px;
  background: var(--accent-soft); border: 1px solid var(--accent-border);
  display: flex; align-items: center; justify-content: center;
  color: var(--accent);
}
.empty-mark svg { width: 34px; height: 34px; }
.empty-state.hidden { display: none; }

/* ============ 消息 ============ */
.msg { display: flex; gap: 12px; animation: msg-in 0.22s ease; }
@keyframes msg-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.msg.user { flex-direction: row-reverse; }

.avatar {
  width: 32px; height: 32px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center; font-size: 15px;
}
.msg.user .avatar { background: var(--user-bubble); color: var(--user-bubble-text); }
.msg.assistant .avatar {
  background: linear-gradient(135deg, #ff6b3d, #ff8f4d);
  color: #fff;
}
.avatar svg { width: 18px; height: 18px; }

.msg-body { min-width: 0; max-width: 86%; }
.msg.user .msg-body { display: flex; flex-direction: column; align-items: flex-end; }
.msg-bubble {
  padding: 11px 14px; border-radius: var(--radius);
  font-size: 14.5px; line-height: 1.7;
  overflow-wrap: break-word;
}
.msg.user .msg-bubble { background: var(--user-bubble); color: var(--user-bubble-text); border-radius: var(--radius) 4px var(--radius) var(--radius); }
.msg.assistant .msg-bubble { background: var(--panel); border: 1px solid var(--border-soft); border-radius: 4px var(--radius) var(--radius) var(--radius); }

/* 思考过程 */
.reasoning {
  margin-bottom: 8px; border: 1px dashed var(--border);
  border-radius: var(--radius-sm); overflow: hidden;
  background: var(--bg-2);
}
.reasoning summary {
  list-style: none; cursor: pointer; padding: 7px 11px;
  font-size: 12px; color: var(--text-3); font-weight: 600;
  display: flex; align-items: center; gap: 6px;
}
.reasoning summary::-webkit-details-marker { display: none; }
.reasoning[open] summary { border-bottom: 1px dashed var(--border); }
.reasoning .reasoning-body {
  padding: 10px 12px; font-size: 12.5px; color: var(--text-2);
  line-height: 1.65; white-space: pre-wrap; max-height: 260px; overflow-y: auto;
}

/* 流式光标 */
.stream-cursor {
  display: inline-block; width: 8px; height: 15px;
  background: var(--accent); border-radius: 2px; vertical-align: -2px;
  animation: blink 0.9s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ============ Markdown ============ */
.md { overflow-wrap: break-word; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3, .md h4 { margin: 1.1em 0 0.5em; line-height: 1.35; }
.md h1 { font-size: 1.45em; } .md h2 { font-size: 1.3em; }
.md h3 { font-size: 1.15em; } .md h4 { font-size: 1.05em; }
.md p { margin: 0.5em 0; }
.md ul, .md ol { margin: 0.5em 0; padding-left: 1.5em; }
.md li { margin: 0.25em 0; }
.md blockquote {
  margin: 0.6em 0; padding: 0.4em 1em;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft); border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-2);
}
.md a { text-decoration: underline; text-underline-offset: 2px; }
.md code.inline-code {
  font-family: var(--mono); font-size: 0.88em;
  background: var(--code-bg); border: 1px solid var(--border-soft);
  padding: 1.5px 6px; border-radius: 5px; color: var(--accent);
}
.md table { border-collapse: collapse; margin: 0.7em 0; width: 100%; font-size: 13.5px; }
.md th, .md td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.md th { background: var(--panel-2); font-weight: 600; }
.md tr:nth-child(even) td { background: var(--bg-2); }
.md hr { border: none; border-top: 1px solid var(--border); margin: 1em 0; }

.code-block { margin: 0.8em 0; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--code-bg); }
.code-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 12px; background: var(--panel-2);
  border-bottom: 1px solid var(--border);
  font-size: 11.5px; color: var(--text-3);
  font-family: var(--mono);
}
.copy-btn {
  border: none; background: transparent; color: var(--text-3);
  font-size: 11.5px; padding: 3px 8px; border-radius: 5px; font-family: inherit;
}
.copy-btn:hover { background: var(--border); color: var(--text); }
.code-block pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code-block code {
  font-family: var(--mono); font-size: 13px; line-height: 1.6; color: var(--text);
}

.msg-meta { margin-top: 5px; font-size: 11px; color: var(--text-3); display: flex; gap: 8px; align-items: center; }
.msg.user .msg-meta { justify-content: flex-end; }

/* 错误消息 */
.msg.error .msg-bubble { border-color: var(--danger); background: rgba(255, 95, 95, 0.08); color: var(--danger); }

/* ============ 输入区 ============ */
.composer {
  max-width: 820px; margin: 0 auto; width: calc(100% - 32px);
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 16px; padding: 10px 12px;
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: var(--accent-border); }
#input {
  flex: 1; border: none; outline: none; resize: none;
  background: transparent; color: var(--text);
  font-family: inherit; font-size: 14.5px; line-height: 1.6;
  max-height: 160px; padding: 4px 2px;
}
#input::placeholder { color: var(--text-3); }
.send-btn, .stop-btn {
  width: 38px; height: 38px; border-radius: 11px; border: none; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.send-btn { background: var(--accent); color: #fff; }
.send-btn:hover { filter: brightness(1.08); }
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.send-btn svg { width: 18px; height: 18px; }
.stop-btn { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.stop-btn span { display: block; width: 11px; height: 11px; background: var(--accent); border-radius: 2px; }
.composer-hint { text-align: center; margin: 8px 0 12px; font-size: 11.5px; color: var(--text-3); }

/* ============ 遮罩 / 响应式 ============ */
.scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  opacity: 0; pointer-events: none; transition: opacity 0.2s; z-index: 9;
}
.scrim.show { opacity: 1; pointer-events: auto; }

@media (max-width: 760px) {
  .sidebar {
    position: fixed; z-index: 10; top: 0; bottom: 0; left: 0;
    transform: translateX(-100%); box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .menu-btn { display: inline-flex; }
  .msg-body { max-width: 92%; }
  .chat-title { font-size: 14px; }
}
