:root {
  --bg: #f5f6f8;
  --bg-elev: #ffffff;
  --bg-soft: #eef0f4;
  --bg-code: #f2f4f7;
  --text: #15171c;
  --muted: #6b7280;
  --border: #e3e6eb;
  --accent: #4f7cff;
  --accent-2: #8b5cf6;
  --accent-soft: rgba(79, 124, 255, 0.1);
  --danger: #e5484d;
  --danger-soft: rgba(229, 72, 77, 0.1);
  --ok: #16a34a;
  --warn: #b45309;
  --warn-soft: rgba(245, 165, 36, 0.12);
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius: 14px;
  --sidebar-w: 276px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1014;
    --bg-elev: #161a21;
    --bg-soft: #1c212b;
    --bg-code: #11151c;
    --text: #e8eaed;
    --muted: #98a1ad;
    --border: #272d38;
    --accent: #6b8cff;
    --accent-2: #a78bfa;
    --accent-soft: rgba(107, 140, 255, 0.14);
    --danger: #ff6369;
    --danger-soft: rgba(255, 99, 105, 0.14);
    --ok: #3ddc84;
    --warn: #f5a524;
    --warn-soft: rgba(245, 165, 36, 0.14);
    --shadow: 0 14px 40px rgba(0, 0, 0, 0.4);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

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;
}

button, input, textarea {
  font: inherit;
  color: inherit;
}

code, pre, .mono {
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
}

.muted { color: var(--muted); }
.mobile-only { display: none; }

/* ---------- 登录 ---------- */
.login-view {
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 15% 0%, var(--accent-soft), transparent 60%),
    radial-gradient(700px 500px at 90% 100%, rgba(139, 92, 246, 0.1), transparent 60%),
    var(--bg);
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 34px 28px 26px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.login-logo { margin-bottom: 14px; }
.login-card h1 { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.2px; }
.login-card .muted { margin: 0 0 24px; font-size: 13.5px; }

.field-label {
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 7px;
}

.field {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg-soft);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.primary {
  margin-top: 16px;
  width: 100%;
  padding: 12px 16px;
  border: none;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s, transform 0.05s;
}
.primary:hover { filter: brightness(1.06); }
.primary:active { transform: translateY(1px); }
.primary:disabled { opacity: 0.6; cursor: default; }

.error {
  margin: 12px 0 0;
  color: var(--danger);
  font-size: 13px;
}
.login-tip {
  margin: 18px 0 0;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
}
.login-tip code {
  background: var(--bg-soft);
  padding: 1px 5px;
  border-radius: 5px;
}

/* ---------- 应用骨架 ---------- */
.app {
  height: 100dvh;
  display: flex;
}

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 12px 12px;
  z-index: 30;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 8px 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  letter-spacing: -0.2px;
}
.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 0 4px var(--accent-soft);
}

.new-chat {
  margin: 0 4px 10px;
  padding: 10px 12px;
  border: 1px dashed var(--border);
  border-radius: 11px;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.new-chat:hover { background: var(--bg-soft); border-color: var(--accent); }

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 2px 2px 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 9px;
  padding: 9px 10px;
  cursor: pointer;
  color: var(--text);
  border: none;
  background: transparent;
  text-align: left;
  width: 100%;
  font-size: 13.5px;
}
.session-item:hover { background: var(--bg-soft); }
.session-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.session-item .title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.session-item .del {
  opacity: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 5px;
}
.session-item:hover .del { opacity: 1; }
.session-item .del:hover { color: var(--danger); background: var(--danger-soft); }

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 10px 8px 2px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.model-info {
  font-size: 11.5px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.link-btn {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  padding: 4px;
  white-space: nowrap;
}
.link-btn:hover { color: var(--danger); }

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  height: 54px;
  flex: 0 0 54px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-elev) 82%, transparent);
  backdrop-filter: blur(10px);
  z-index: 10;
}
.topbar-title {
  flex: 1;
  font-weight: 600;
  font-size: 14.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-radius: 9px;
  width: 34px;
  height: 34px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
}
.icon-btn:hover { background: var(--bg-soft); }

/* 开关 */
.switch {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  cursor: pointer;
  user-select: none;
}
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track {
  width: 34px;
  height: 20px;
  border-radius: 999px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  position: relative;
  transition: background 0.15s;
}
.switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s, background 0.15s;
}
.switch input:checked + .switch-track { background: var(--accent-soft); border-color: var(--accent); }
.switch input:checked + .switch-track .switch-thumb {
  transform: translateX(14px);
  background: var(--accent);
}
.switch-text { font-size: 12.5px; color: var(--muted); white-space: nowrap; }
.switch input:checked ~ .switch-text { color: var(--accent); }

/* ---------- 审批提示条 ---------- */
.approval-banner {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  color: #1b1300;
  background: var(--warn);
  animation: fade-in 0.18s ease-out;
}
.approval-banner[hidden] { display: none; }
.approval-banner button {
  border: 1px solid rgba(0, 0, 0, 0.25);
  background: rgba(255, 255, 255, 0.55);
  color: #1b1300;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}

/* ---------- 消息区 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 22px 18px 12px;
  scroll-behavior: smooth;
}
.messages-inner,
.messages > .msg,
.messages > .welcome,
.messages > .tool,
.messages > .approval,
.messages > .notice {
  max-width: 860px;
  margin-left: auto;
  margin-right: auto;
}

.welcome {
  text-align: center;
  padding: 9vh 16px 20px;
}
.welcome h2 { margin: 16px 0 6px; font-size: 20px; letter-spacing: -0.3px; }
.welcome p { margin: 0; font-size: 13.5px; }
.welcome-icon { display: inline-flex; }
.suggestions {
  margin-top: 24px;
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  justify-content: center;
}
.chip {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.05s;
}
.chip:hover { border-color: var(--accent); background: var(--accent-soft); }
.chip:active { transform: translateY(1px); }

.msg {
  display: flex;
  gap: 10px;
  margin-bottom: 18px;
  align-items: flex-start;
}
.msg.user { justify-content: flex-end; }

.msg.user .bubble {
  max-width: min(78%, 640px);
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
  border-radius: 16px 16px 4px 16px;
  padding: 10px 14px;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 4px 14px rgba(79, 124, 255, 0.18);
}

.msg.assistant .avatar {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  margin-top: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.msg.assistant .avatar::after {
  content: "";
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #fff;
  opacity: 0.92;
}

.msg.assistant .body {
  flex: 1;
  min-width: 0;
  padding-top: 1px;
}

.msg.assistant .body > :first-child { margin-top: 0; }
.msg.assistant .body > :last-child { margin-bottom: 0; }

/* Markdown */
.body p { margin: 0 0 10px; }
.body h1, .body h2, .body h3, .body h4 {
  margin: 18px 0 8px;
  line-height: 1.35;
  letter-spacing: -0.2px;
}
.body h1 { font-size: 19px; }
.body h2 { font-size: 17px; }
.body h3 { font-size: 15.5px; }
.body ul, .body ol { margin: 0 0 10px; padding-left: 22px; }
.body li { margin: 3px 0; }
.body a { color: var(--accent); text-decoration: none; }
.body a:hover { text-decoration: underline; }
.body blockquote {
  margin: 10px 0;
  padding: 6px 14px;
  border-left: 3px solid var(--accent);
  background: var(--bg-soft);
  border-radius: 0 8px 8px 0;
  color: var(--muted);
}
.body code {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 1px 5px;
  font-size: 0.88em;
}
.body pre {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 10px 0;
  line-height: 1.5;
}
.body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 12.5px;
}
.body table {
  border-collapse: collapse;
  margin: 10px 0;
  width: 100%;
  font-size: 13px;
  display: block;
  overflow-x: auto;
}
.body th, .body td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.body th { background: var(--bg-soft); }
.body hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.body img { max-width: 100%; border-radius: 8px; }

/* 工具卡片 */
.tool {
  border: 1px solid var(--border);
  background: var(--bg-elev);
  border-radius: 12px;
  margin: 0 auto 14px;
  overflow: hidden;
  animation: fade-in 0.18s ease-out;
}
.tool-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  font-size: 12.5px;
}
.tool-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 8px;
}
.tool.running .tool-dot { background: var(--accent); animation: pulse 1s infinite; }
.tool.ok .tool-dot { background: var(--ok); }
.tool.error .tool-dot { background: var(--danger); }
.tool.denied .tool-dot { background: var(--warn); }

.tool-name {
  font-family: "SFMono-Regular", Consolas, monospace;
  font-weight: 600;
  font-size: 12px;
}
.tool-summary {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
  font-family: "SFMono-Regular", Consolas, monospace;
  font-size: 11.5px;
}
.tool-status { font-size: 11px; color: var(--muted); white-space: nowrap; }
.tool-body {
  border-top: 1px solid var(--border);
  background: var(--bg-code);
}
.tool-body summary {
  cursor: pointer;
  padding: 7px 12px;
  font-size: 11.5px;
  color: var(--muted);
  user-select: none;
}
.tool-body pre {
  margin: 0;
  padding: 0 12px 12px;
  max-height: 320px;
  overflow: auto;
  font-size: 11.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

/* 审批卡片 */
.approval {
  border: 1px solid color-mix(in srgb, var(--warn) 55%, var(--border));
  background: var(--warn-soft);
  border-radius: 12px;
  padding: 12px 14px;
  margin: 0 auto 14px;
  animation: fade-in 0.18s ease-out;
}
.approval-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--warn);
  margin-bottom: 4px;
}
.approval-desc {
  font-size: 12.5px;
  color: var(--text);
  word-break: break-word;
  font-family: "SFMono-Regular", Consolas, monospace;
  background: color-mix(in srgb, var(--bg-elev) 60%, transparent);
  border-radius: 7px;
  padding: 6px 9px;
  margin: 6px 0 10px;
}
.approval-actions { display: flex; gap: 8px; }
.approval-actions button {
  border: none;
  border-radius: 8px;
  padding: 7px 14px;
  font-size: 12.5px;
  cursor: pointer;
}
.btn-approve { background: var(--warn); color: #1b1300; font-weight: 600; }
.btn-reject { background: var(--bg-elev); color: var(--text); border: 1px solid var(--border) !important; }
.approval-actions button:disabled { opacity: 0.55; cursor: default; }
.approval-done { font-size: 12.5px; color: var(--muted); }

.notice {
  margin: 0 auto 14px;
  padding: 9px 13px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12.5px;
}

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 6px 0;
}
.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: bounce 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

/* ---------- 输入区 ---------- */
.composer {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 18px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
}
.input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  border-radius: 14px;
  padding: 11px 14px;
  max-height: 42vh;
  line-height: 1.5;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.send-btn, .stop-btn {
  flex: 0 0 40px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.send-btn {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff;
}
.send-btn:disabled { opacity: 0.45; cursor: default; }
.stop-btn {
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 13px;
}

/* ---------- 移动端 ---------- */
@media (max-width: 760px) {
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    transform: translateX(-102%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .drawer-mask {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 25;
  }
  .topbar { padding: 0 12px; height: 52px; flex-basis: 52px; }
  .topbar-title { font-size: 13.5px; }
  .messages { padding: 14px 12px 8px; }
  .msg.user .bubble { max-width: 86%; }
  .composer { padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); }
  .input { padding: 10px 12px; }
  .welcome { padding-top: 6vh; }
  .suggestions { gap: 7px; }
  .chip { padding: 7px 11px; font-size: 12.5px; }
  .switch-text { display: none; }
}

@media (max-width: 380px) {
  .body pre { font-size: 11.5px; }
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.8); }
}
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}
@keyframes fade-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--muted) 35%, transparent);
  border-radius: 999px;
}
::-webkit-scrollbar-track { background: transparent; }
