:root {
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
}

/* ===== 主题变量 ===== */
[data-theme="dark"] {
  --bg: #1a1b26;
  --bg-2: #16161e;
  --panel: #1f2030;
  --panel-2: #24263a;
  --border: #2c2e40;
  --text: #c0caf5;
  --text-dim: #7a82ab;
  --accent: #7aa2f7;
  --accent-2: #bb9af7;
  --user-bg: #2a3050;
  --bot-bg: #1f2030;
  --tool-bg: #20322a;
  --tool-border: #2e7d57;
  --warn-bg: #3a2e1a;
  --warn-border: #d8a657;
  --danger: #f7768e;
}
[data-theme="light"] {
  --bg: #f5f6fa;
  --bg-2: #ffffff;
  --panel: #ffffff;
  --panel-2: #eef1f8;
  --border: #dde1ec;
  --text: #1f2430;
  --text-dim: #6b7280;
  --accent: #3b5bdb;
  --accent-2: #7048e8;
  --user-bg: #dbe4ff;
  --bot-bg: #ffffff;
  --tool-bg: #e6f4ea;
  --tool-border: #51cf66;
  --warn-bg: #fff4e0;
  --warn-border: #f08c00;
  --danger: #e03131;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; height: 100dvh; }

/* ===== 侧栏 ===== */
#sidebar {
  width: 260px; flex-shrink: 0;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  transition: margin-left .2s ease;
}
#sidebar.collapsed { margin-left: -260px; }
.sidebar-head { display: flex; gap: 8px; padding: 12px; }
.sidebar-head .primary { flex: 1; }
#session-list { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
#session-list li {
  padding: 10px 12px; border-radius: var(--radius); cursor: pointer;
  color: var(--text-dim); font-size: 14px; margin-bottom: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
#session-list li:hover { background: var(--panel-2); color: var(--text); }
#session-list li.active { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
#session-list li .del { opacity: 0; color: var(--danger); font-size: 12px; }
#session-list li:hover .del { opacity: .8; }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 6px; }

/* 移动端遮罩 */
#overlay { display: none; }

/* ===== 主区 ===== */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}
#open-sidebar { display: none; }
#title { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ===== 状态指示器 ===== */
.status {
  display: flex; align-items: center; gap: 6px;
  font-size: 13px; padding: 4px 10px; border-radius: 999px;
  background: var(--panel-2); color: var(--text-dim);
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-dim); }
.status.idle .dot { background: #51cf66; }
.status.thinking { color: var(--accent-2); }
.status.thinking .dot { background: var(--accent-2); animation: pulse 1s infinite; }
.status.tool { color: var(--tool-border); }
.status.tool .dot { background: var(--tool-border); animation: pulse 1s infinite; }
.status.replying { color: var(--accent); }
.status.replying .dot { background: var(--accent); animation: pulse 1s infinite; }
.status.error { color: var(--danger); }
.status.error .dot { background: var(--danger); }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.3} }

/* ===== 消息区 ===== */
#messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 16px; }
.msg { max-width: 860px; width: 100%; margin: 0 auto; display: flex; gap: 10px; }
.msg.user { flex-direction: row-reverse; }
.avatar {
  width: 32px; height: 32px; border-radius: 9px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; background: var(--panel-2); border: 1px solid var(--border);
  overflow: hidden; cursor: pointer; user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.msg .col { display: flex; flex-direction: column; gap: 4px; min-width: 0; max-width: calc(100% - 44px); }
.msg.user .col { align-items: flex-end; }
.msg .meta { font-size: 11px; color: var(--text-dim); display: flex; gap: 8px; align-items: center; }
.bubble {
  padding: 11px 14px; border-radius: var(--radius);
  line-height: 1.65; word-break: break-word;
  border: 1px solid var(--border); position: relative;
}
.msg.user .bubble { background: var(--user-bg); white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--bot-bg); }

/* Markdown 渲染样式 */
.bubble p { margin: 0 0 .6em; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h1, .bubble h2, .bubble h3 { margin: .6em 0 .4em; line-height: 1.3; }
.bubble h1 { font-size: 1.3em; } .bubble h2 { font-size: 1.18em; } .bubble h3 { font-size: 1.06em; }
.bubble ul, .bubble ol { margin: .3em 0 .6em; padding-left: 1.4em; }
.bubble li { margin: .2em 0; }
.bubble a { color: var(--accent); }
.bubble blockquote { margin: .4em 0; padding: 2px 12px; border-left: 3px solid var(--accent); color: var(--text-dim); }
.bubble code { background: rgba(127,127,127,.18); padding: 1px 5px; border-radius: 5px; font-size: .92em; font-family: ui-monospace, Menlo, Consolas, monospace; }
.bubble pre { background: var(--bg-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; margin: .5em 0; }
.bubble pre code { background: none; padding: 0; }

/* 复制按钮 */
.copy-btn { background: none; border: none; color: var(--text-dim); cursor: pointer; font-size: 11px; padding: 0; }
.copy-btn:hover { color: var(--accent); }

/* 工具调用气泡 */
.tool-call {
  background: var(--tool-bg); border: 1px solid var(--tool-border);
  border-radius: 8px; padding: 8px 12px; font-size: 13px;
  color: var(--text); font-family: ui-monospace, Menlo, Consolas, monospace;
}
.tool-call .tname { font-weight: 600; }
.tool-call.err { background: var(--warn-bg); border-color: var(--danger); }
.tool-call details { margin-top: 4px; }
.tool-call summary { cursor: pointer; color: var(--text-dim); }
.tool-call pre { margin: 6px 0 0; white-space: pre-wrap; word-break: break-word; }

/* ===== 警告条 ===== */
#warning-banner {
  margin: 0 18px 8px; padding: 8px 12px; border-radius: 8px;
  background: var(--warn-bg); border: 1px solid var(--warn-border);
  color: var(--text); font-size: 13px;
}
.hidden { display: none !important; }

/* ===== 输入区 ===== */
#composer {
  display: flex; gap: 10px; padding: 12px 14px;
  border-top: 1px solid var(--border); background: var(--bg-2);
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}
#input {
  flex: 1; resize: none; max-height: 180px;
  background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 12px; font: inherit; line-height: 1.5; outline: none;
}
#input:focus { border-color: var(--accent); }

/* ===== 按钮 ===== */
.btn {
  border: 1px solid var(--border); background: var(--panel);
  color: var(--text); border-radius: var(--radius);
  padding: 9px 14px; font: inherit; cursor: pointer; transition: filter .15s;
}
.btn:hover { filter: brightness(1.1); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.icon { padding: 8px 10px; }
.btn.small { flex: 1; font-size: 13px; padding: 7px; }

/* ===== 设置弹窗 ===== */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center;
}
.modal-box {
  width: min(420px, 92vw); background: var(--bg-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; display: flex; flex-direction: column; gap: 14px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.field input {
  background: var(--panel); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 11px; font: inherit; outline: none;
}
.field input:focus { border-color: var(--accent); }
.modal-foot { display: flex; justify-content: flex-end; }

/* ===== 移动端 ===== */
@media (max-width: 720px) {
  #sidebar { position: fixed; z-index: 30; height: 100dvh; box-shadow: 2px 0 16px rgba(0,0,0,.4); }
  #sidebar.collapsed { margin-left: -280px; }
  #open-sidebar { display: inline-flex; }
  #toggle-sidebar { display: inline-flex; }
  #overlay { display: block; position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.45); }
  .msg .col { max-width: calc(100% - 44px); }
}
@media (min-width: 721px) {
  #toggle-sidebar { display: none; }
}
