:root {
  --bg: #0a0c10;
  --bg-soft: #11141b;
  --panel: #151922;
  --panel-2: #1b202b;
  --border: #262d3a;
  --text: #e8ecf3;
  --text-dim: #98a3b8;
  --text-faint: #6b7688;
  --accent: #4f8cff;
  --accent-soft: rgba(79, 140, 255, .14);
  --user-bubble: #23304a;
  --danger: #ff5d5d;
  --ok: #38d39f;
  --radius: 14px;
  --shadow: 0 18px 50px rgba(0, 0, 0, .45);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

html[data-theme="light"] {
  --bg: #f5f7fb;
  --bg-soft: #ffffff;
  --panel: #ffffff;
  --panel-2: #f0f3f9;
  --border: #e2e7f0;
  --text: #131722;
  --text-dim: #5b6577;
  --text-faint: #8b95a7;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, .1);
  --user-bubble: #e4ecff;
  --shadow: 0 18px 50px rgba(20, 30, 60, .12);
}

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

html, body { height: 100%; }

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

svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }

code, pre { font-family: var(--mono); }

/* ======================= 布局 ======================= */
.app { display: flex; height: 100%; min-height: 0; }

.sidebar {
  width: 268px;
  flex: 0 0 268px;
  background: var(--bg-soft);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  transition: margin .22s ease, transform .22s ease;
}

.app.collapsed .sidebar { margin-left: -268px; }

.sidebar-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px;
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }

.logo {
  width: 34px; height: 34px; flex: 0 0 34px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: #fff; font-size: 13px; font-weight: 700; letter-spacing: .5px;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text strong { font-size: 14px; }
.brand-text span { font-size: 11px; color: var(--text-faint); }

.icon-btn {
  width: 32px; height: 32px; flex: 0 0 32px;
  display: grid; place-items: center;
  padding: 7px; border-radius: 9px;
  color: var(--text-dim);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

.new-chat {
  margin: 4px 14px 10px;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px;
  border-radius: 11px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid transparent;
  font-size: 14px; font-weight: 600;
  transition: filter .15s;
}
.new-chat:hover { filter: brightness(1.12); }
.new-chat svg { width: 16px; height: 16px; }

.conv-list { flex: 1 1 auto; overflow-y: auto; padding: 4px 8px 10px; }

.conv-item {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 10px; margin-bottom: 2px;
  border-radius: 10px;
  color: var(--text-dim);
  font-size: 13.5px;
  cursor: pointer;
  transition: background .15s, color .15s;
}
.conv-item:hover { background: var(--panel-2); color: var(--text); }
.conv-item.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
.conv-item .title { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .del { opacity: 0; flex: 0 0 22px; width: 22px; height: 22px; padding: 4px; border-radius: 6px; color: var(--text-faint); }
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { background: rgba(255, 93, 93, .16); color: var(--danger); }

.sidebar-foot { border-top: 1px solid var(--border); padding: 8px; display: flex; flex-direction: column; gap: 2px; }

.foot-btn {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 10px; border-radius: 9px;
  color: var(--text-dim); font-size: 13.5px;
  transition: background .15s, color .15s;
}
.foot-btn:hover { background: var(--panel-2); color: var(--text); }
.foot-btn svg { width: 16px; height: 16px; flex: 0 0 16px; }

.scrim { display: none; }

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

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  flex: 0 0 auto;
}

.topbar-mid { flex: 1 1 auto; display: flex; align-items: center; gap: 8px; min-width: 0; }

.model-select {
  max-width: 260px; min-width: 120px;
  padding: 7px 10px;
  border-radius: 9px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 13px;
  outline: none;
}

.provider-tag {
  font-size: 11.5px; color: var(--text-faint);
  padding: 3px 8px; border-radius: 999px;
  background: var(--panel-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 220px;
}

.topbar-right { display: flex; gap: 2px; }

/* ======================= 聊天区 ======================= */
.chat { flex: 1 1 auto; overflow-y: auto; overflow-x: hidden; scroll-behavior: smooth; }

.welcome {
  max-width: 760px; margin: 0 auto;
  padding: clamp(32px, 9vh, 96px) 20px 24px;
  text-align: center;
}
.welcome-logo {
  width: 54px; height: 54px; margin: 0 auto 18px;
  display: grid; place-items: center; border-radius: 15px;
  background: linear-gradient(135deg, var(--accent), #8b5cf6);
  color: #fff; font-weight: 700; letter-spacing: .5px;
  box-shadow: 0 10px 30px var(--accent-soft);
}
.welcome h1 { font-size: clamp(22px, 3.4vw, 30px); font-weight: 700; letter-spacing: -.4px; }
.welcome p { color: var(--text-dim); font-size: 14px; margin-top: 10px; max-width: 560px; margin-inline: auto; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 24px; }
.chip {
  padding: 9px 14px; border-radius: 11px;
  background: var(--panel); border: 1px solid var(--border);
  color: var(--text-dim); font-size: 13px;
  transition: border-color .15s, color .15s, transform .15s;
}
.chip:hover { border-color: var(--accent); color: var(--text); transform: translateY(-1px); }

.welcome-tip { margin-top: 18px; font-size: 12.5px; color: var(--text-faint); }
.welcome-tip a, .welcome-tip button { color: var(--accent); text-decoration: none; }

.messages { max-width: 820px; margin: 0 auto; padding: 22px 20px 28px; display: flex; flex-direction: column; gap: 20px; }

.msg { display: flex; gap: 12px; }
.msg .avatar {
  flex: 0 0 30px; width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center;
  font-size: 12px; font-weight: 700;
}
.msg.user .avatar { background: var(--user-bubble); color: var(--text); }
.msg.assistant .avatar { background: linear-gradient(135deg, var(--accent), #8b5cf6); color: #fff; }
.msg .body { flex: 1 1 auto; min-width: 0; padding-top: 3px; }
.msg .who { font-size: 12px; color: var(--text-faint); margin-bottom: 5px; display: flex; align-items: center; gap: 8px; }
.msg .content { overflow-wrap: anywhere; }
.msg .content > *:first-child { margin-top: 0; }
.msg .content > *:last-child { margin-bottom: 0; }

.msg.user .content { white-space: pre-wrap; }

.content p { margin: 0 0 10px; }
.content h1, .content h2, .content h3, .content h4 { margin: 18px 0 9px; line-height: 1.35; font-weight: 700; }
.content h1 { font-size: 1.32em; }
.content h2 { font-size: 1.2em; }
.content h3 { font-size: 1.08em; }
.content ul, .content ol { margin: 0 0 10px; padding-left: 22px; }
.content li { margin: 3px 0; }
.content a { color: var(--accent); text-decoration: none; border-bottom: 1px solid transparent; }
.content a:hover { border-bottom-color: currentColor; }
.content blockquote { margin: 10px 0; padding: 2px 0 2px 12px; border-left: 3px solid var(--border); color: var(--text-dim); }
.content hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.content table { width: 100%; border-collapse: collapse; margin: 10px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.content th, .content td { border: 1px solid var(--border); padding: 7px 10px; text-align: left; }
.content th { background: var(--panel-2); font-weight: 600; }

.content code.inline {
  font-size: .88em; padding: .16em .42em; border-radius: 5px;
  background: var(--panel-2); border: 1px solid var(--border);
}

.codeblock { margin: 10px 0; border: 1px solid var(--border); border-radius: 11px; overflow: hidden; background: var(--panel); }
.codeblock-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 10px; font-size: 11.5px; color: var(--text-faint);
  background: var(--panel-2); border-bottom: 1px solid var(--border);
}
.codeblock-head .copy { font-size: 11.5px; padding: 3px 8px; border-radius: 6px; color: var(--text-dim); }
.codeblock-head .copy:hover { background: var(--panel); color: var(--text); }
.codeblock pre { margin: 0; padding: 12px; overflow-x: auto; font-size: 12.8px; line-height: 1.62; }
.codeblock code { white-space: pre; }

.msg-actions { display: flex; gap: 4px; margin-top: 8px; opacity: 0; transition: opacity .15s; }
.msg.assistant:hover .msg-actions, .msg.assistant:focus-within .msg-actions { opacity: 1; }
.msg-actions button {
  display: flex; align-items: center; gap: 5px;
  padding: 4px 9px; border-radius: 7px;
  font-size: 12px; color: var(--text-faint);
}
.msg-actions button:hover { background: var(--panel-2); color: var(--text); }
.msg-actions svg { width: 13px; height: 13px; flex: 0 0 13px; }

.typing { display: inline-flex; gap: 4px; align-items: center; height: 20px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); animation: blink 1.3s infinite; }
.typing i:nth-child(2) { animation-delay: .18s; }
.typing i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

.cursor-blink::after { content: "▋"; margin-left: 2px; color: var(--accent); animation: caret 1s steps(1) infinite; }
@keyframes caret { 50% { opacity: 0; } }

.err-box {
  border: 1px solid rgba(255, 93, 93, .35);
  background: rgba(255, 93, 93, .09);
  color: var(--danger);
  padding: 9px 12px; border-radius: 9px; font-size: 13px;
  white-space: pre-wrap; overflow-wrap: anywhere;
}

/* ======================= 输入区 ======================= */
.composer-wrap {
  flex: 0 0 auto;
  padding: 8px 16px 12px;
  background: linear-gradient(to top, var(--bg) 62%, transparent);
}
.composer {
  max-width: 820px; margin: 0 auto;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 8px 8px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  transition: border-color .15s, box-shadow .15s;
}
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.composer textarea {
  flex: 1 1 auto;
  resize: none;
  border: none; outline: none; background: none;
  color: var(--text);
  font: inherit; font-size: 14.5px; line-height: 1.55;
  max-height: 200px; padding: 7px 0;
}
.composer textarea::placeholder { color: var(--text-faint); }
.composer-actions { display: flex; align-items: center; gap: 6px; }
.hint { font-size: 11px; color: var(--text-faint); }

.send-btn {
  width: 34px; height: 34px; flex: 0 0 34px;
  display: grid; place-items: center; padding: 8px;
  border-radius: 10px;
  background: var(--accent); color: #fff;
  transition: filter .15s, opacity .15s, transform .1s;
}
.send-btn:hover { filter: brightness(1.1); }
.send-btn:active { transform: scale(.96); }
.send-btn:disabled { opacity: .38; cursor: not-allowed; }
.send-btn.stop { background: var(--danger); }

.disclaimer { max-width: 820px; margin: 7px auto 0; text-align: center; font-size: 11px; color: var(--text-faint); }

/* ======================= 设置弹窗 ======================= */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  padding: 20px;
  background: rgba(0, 0, 0, .58);
  backdrop-filter: blur(3px);
}
.modal[hidden] { display: none; }

.modal-card {
  width: min(620px, 100%);
  max-height: min(88vh, 900px);
  display: flex; flex-direction: column;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: pop .16s ease-out;
}
@keyframes pop { from { opacity: 0; transform: translateY(10px) scale(.985); } }

.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 15px 18px; border-bottom: 1px solid var(--border);
}
.modal-head h2 { font-size: 16px; font-weight: 700; }

.modal-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 12.5px; font-weight: 600; color: var(--text-dim); }
.field .val { color: var(--accent); font-family: var(--mono); }
.sub { font-size: 11.5px; color: var(--text-faint); }
.sub code { background: var(--panel-2); padding: 1px 4px; border-radius: 4px; }

.input {
  width: 100%;
  padding: 9px 11px;
  border-radius: 9px;
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  font: inherit; font-size: 13.5px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.input { resize: vertical; line-height: 1.55; }
.input.mono { font-family: var(--mono); font-size: 12.5px; }
input[type="range"] { accent-color: var(--accent); width: 100%; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

.profile-row, .secret-row { display: flex; gap: 8px; align-items: center; }
.profile-row .input { flex: 1 1 auto; }

.btn {
  padding: 9px 15px; border-radius: 9px;
  font-size: 13.5px; font-weight: 600;
  transition: filter .15s, background .15s, color .15s;
  white-space: nowrap;
}
.btn.ghost { background: var(--panel); border: 1px solid var(--border); color: var(--text-dim); }
.btn.ghost:hover { color: var(--text); border-color: var(--accent); }
.btn.ghost.danger:hover { color: var(--danger); border-color: var(--danger); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.advanced { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--panel); }
.advanced summary { font-size: 12.5px; color: var(--text-dim); cursor: pointer; }
.advanced > *:not(summary) { margin-top: 10px; }

.test-result { font-size: 12.5px; min-height: 18px; }
.test-result.ok { color: var(--ok); }
.test-result.err { color: var(--danger); }
.test-result.loading { color: var(--text-dim); }

.modal-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding: 13px 18px; border-top: 1px solid var(--border);
  background: var(--bg-soft);
}

.toast {
  position: fixed; left: 50%; bottom: 28px; z-index: 90;
  transform: translateX(-50%);
  padding: 10px 18px; border-radius: 11px;
  background: var(--panel-2); border: 1px solid var(--border);
  color: var(--text); font-size: 13px;
  box-shadow: var(--shadow);
  animation: toastIn .18s ease-out;
}
.toast[hidden] { display: none; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } }

.only-mobile { display: none; }

/* ======================= 滚动条 ======================= */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

/* ======================= 响应式 ======================= */
@media (max-width: 760px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 40;
    transform: translateX(-100%);
    box-shadow: var(--shadow);
  }
  .app.collapsed .sidebar { margin-left: 0; }
  .app:not(.collapsed) .sidebar { transform: translateX(0); }
  .app.mobile-open { }
  .app.mobile-open .sidebar { transform: translateX(0); }
  .scrim.show {
    display: block; position: fixed; inset: 0; z-index: 39;
    background: rgba(0, 0, 0, .55);
  }
  .only-mobile { display: grid; }
  .welcome { padding-top: 40px; }
  .messages { padding-inline: 14px; }
  .composer-wrap { padding-inline: 10px; }
  .model-select { max-width: 150px; }
  .provider-tag { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
