:root {
  color-scheme: light dark;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef2f9;
  --surface-3: #e4e9f3;
  --text: #111827;
  --muted: #667085;
  --border: #e4e7ec;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-soft: #dbeafe;
  --user-bubble: #2563eb;
  --user-text: #ffffff;
  --assistant-bubble: #ffffff;
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --shadow: 0 18px 50px rgba(15, 23, 42, 0.10);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --surface: #111827;
    --surface-2: #172036;
    --surface-3: #1f2a44;
    --text: #e5e7eb;
    --muted: #9aa4b2;
    --border: #243049;
    --accent: #60a5fa;
    --accent-hover: #93c5fd;
    --accent-soft: #1e3a8a;
    --user-bubble: #2563eb;
    --user-text: #ffffff;
    --assistant-bubble: #111827;
    --danger: #fda29b;
    --danger-soft: #3b1d1d;
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- 登录 / 初始化 ---------- */
.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 12% 8%, var(--accent-soft), transparent 62%),
    radial-gradient(700px 500px at 88% 92%, #ede9fe, transparent 62%),
    var(--bg);
}

.auth-shell { width: 100%; max-width: 440px; }

.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 26px;
  padding: 36px 34px 28px;
  box-shadow: var(--shadow);
}

.brand-mark {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: transparent;
  color: #fff;
  font-weight: 800;
  letter-spacing: 0.5px;
  margin-bottom: 18px;
  overflow: hidden;
}

.brand-mark img {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: inherit;
}

.brand-mark.small {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  margin: 0;
  font-size: 13px;
}

.auth-card h1 { margin: 0 0 6px; font-size: 24px; }

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 22px;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 7px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.form input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}

.form input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn {
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  cursor: pointer;
  font-weight: 600;
  transition: 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.primary:disabled { opacity: 0.55; cursor: not-allowed; }

.btn.ghost {
  background: transparent;
  color: var(--muted);
  padding: 8px 10px;
}
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }

.alert {
  margin-top: 16px;
  padding: 11px 14px;
  border-radius: 12px;
  background: var(--danger-soft);
  color: var(--danger);
  font-size: 14px;
  border: 1px solid var(--border);
}

/* ---------- 主界面 ---------- */
.app-shell {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 22px 18px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  min-height: 0;
}

.brand { display: flex; align-items: center; gap: 11px; }
.brand-name { font-weight: 800; letter-spacing: 0.2px; }
.brand-sub { font-size: 12px; color: var(--muted); }

.section-label {
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 700;
  margin-top: 8px;
}

.project-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  min-height: 0;
}

.project {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  text-align: left;
  padding: 12px 13px;
  border-radius: 13px;
  border: 1px solid transparent;
  background: transparent;
  color: inherit;
  cursor: pointer;
  transition: 0.15s ease;
}

.project:hover { background: var(--surface-2); }
.project:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.project-name { font-weight: 650; font-size: 14px; }
.project-meta { font-size: 12px; color: var(--muted); }

.project-row {
  display: flex;
  align-items: stretch;
  gap: 4px;
}

.project-row .project {
  flex: 1;
  width: auto;
  min-width: 0;
}

.project-repo {
  flex: none;
  width: 30px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
}

.project-repo:hover {
  background: var(--surface-2);
  color: var(--accent);
}

.project-repo:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.sidebar-footer {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--muted);
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #22c55e;
  box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.18);
}

.conversations-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 4px;
}

.new-chat-form { margin: 0; }

.new-chat {
  width: 24px;
  height: 24px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  display: grid;
  place-items: center;
}

.new-chat:hover { border-color: var(--accent); color: var(--accent); }

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 220px;
  overflow-y: auto;
  min-height: 0;
}

.conversation-search,
.project-search {
  display: flex;
  gap: 6px;
}

.conversation-search input,
.project-search input {
  width: 100%;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 13px;
  outline: none;
}

.conversation-search input:focus,
.project-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-clear {
  display: inline-block;
  margin: 2px 0 4px;
  font-size: 12px;
  color: var(--muted);
}

.conversation {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  color: var(--muted);
  text-decoration: none;
  font-size: 13px;
}

.conversation:hover { background: var(--surface-2); color: var(--text); }
.conversation.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.conversation-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-meta { flex: none; font-size: 11px; opacity: 0.8; }

.delete-form { margin: 0; }
.delete-form .btn { padding: 8px; font-size: 12px; }

.title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 8px;
  font-size: 14px;
}

.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.rename-form {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

.rename-form[hidden] { display: none; }

.rename-form input[name="title"] {
  flex: 1;
  min-width: 160px;
  padding: 8px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-size: 14px;
  outline: none;
}

.rename-form input[name="title"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn.small { padding: 8px 12px; font-size: 13px; }

.chat {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px clamp(18px, 4vw, 46px);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.chat-header h1 { margin: 0; font-size: 17px; }
.chat-header p { margin: 3px 0 0; }
.header-actions { display: flex; align-items: center; gap: 10px; }

.badge {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  padding: 5px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
}

.user-name { font-size: 13px; color: var(--muted); }
.logout-form { margin: 0; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px clamp(18px, 4vw, 46px);
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}

.msg { display: flex; gap: 11px; max-width: 920px; }
.msg.user { flex-direction: row-reverse; margin-left: auto; }

.avatar {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 800;
  background: var(--surface-2);
  color: var(--muted);
  border: 1px solid var(--border);
}

.msg.user .avatar {
  background: var(--accent);
  color: #fff;
  border-color: transparent;
}

.bubble {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.65;
  font-size: 14.5px;
  padding: 13px 16px;
  border-radius: 16px;
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.04);
}

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-color: transparent;
  border-bottom-right-radius: 6px;
}

.msg.assistant .bubble { border-bottom-left-radius: 6px; }

.empty-state {
  margin: auto;
  text-align: center;
  max-width: 560px;
  padding: 24px;
}

.empty-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  font-size: 24px;
  background: var(--accent-soft);
  color: var(--accent);
}

.empty-state h2 { margin: 0 0 8px; font-size: 20px; }

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
  justify-content: center;
  margin-top: 20px;
}

.suggestion {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: 0.15s;
}

.suggestion:hover { border-color: var(--accent); color: var(--accent); }

.composer {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 14px clamp(18px, 4vw, 46px) 10px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 180px;
  min-height: 46px;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
  line-height: 1.5;
}

.composer textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer .btn { height: 46px; padding: 0 20px; }

.status {
  min-height: 24px;
  padding: 0 clamp(18px, 4vw, 46px) 12px;
  color: var(--muted);
  font-size: 12px;
  background: var(--surface);
}

/* ---------- 设置页 ---------- */
.settings-page { background: var(--bg); }

.settings-shell,
.project-shell {
  max-width: 980px;
  margin: 0 auto;
  padding: 28px 20px 64px;
}

.settings-header,
.project-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 22px 24px;
  margin-bottom: 16px;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.04);
}

.card h2 { margin: 0 0 14px; font-size: 16px; }

.kv > div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.kv > div:last-child { border-bottom: 0; }
.kv span { color: var(--muted); }

code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 12px;
  background: var(--surface-2);
  padding: 2px 6px;
  border-radius: 6px;
}

.status-pill {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
}

.status-pill.ok { background: rgba(34, 197, 94, 0.15); color: #16a34a; }
.status-pill.fail { background: var(--danger-soft); color: var(--danger); }

.test-result {
  margin-top: 14px;
  min-height: 22px;
  font-size: 13px;
}

.test-result.ok { color: #16a34a; }
.test-result.fail { color: var(--danger); }

.table-wrap { overflow-x: auto; }

.usage-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.usage-table th,
.usage-table td {
  text-align: left;
  padding: 9px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.usage-table th { color: var(--muted); font-weight: 600; }

.project-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
}

.todo-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.todo-list li {
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 14px;
}

.log-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  font-size: 13px;
  line-height: 1.65;
  max-height: 600px;
  overflow: auto;
  margin: 0;
}

.recent-logs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.recent-logs span {
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 12px;
  color: var(--muted);
}

.messages::-webkit-scrollbar,
.project-list::-webkit-scrollbar { width: 10px; }

.messages::-webkit-scrollbar-thumb,
.project-list::-webkit-scrollbar-thumb {
  background: var(--surface-3);
  border-radius: 999px;
  border: 3px solid transparent;
  background-clip: padding-box;
}

@media (max-width: 840px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .sidebar {
    flex-direction: row;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
  }

  .brand,
  .section-label,
  .sidebar-footer { display: none; }

  .project-list { flex-direction: row; overflow-x: auto; gap: 8px; }
  .project { min-width: 148px; background: var(--surface-2); }

  .conversations-label { margin-top: 0; }
  .conversation-search,
  .project-search { min-width: 160px; }
  .conversation-list { flex-direction: row; overflow-x: auto; max-height: none; gap: 6px; }
  .conversation { min-width: 140px; background: var(--surface-2); }
  .rename-form { width: 100%; }

  .chat-header { padding: 14px 16px; flex-wrap: wrap; }
  .chat-header p { display: none; }
  .header-actions { flex-wrap: wrap; justify-content: flex-end; }
  .user-name { display: none; }
  .messages { padding: 18px 16px; }
  .composer { padding: 10px 14px 6px; }
  .status { padding: 0 14px 10px; }

  .settings-shell,
  .project-shell { padding: 18px 14px 48px; }
  .settings-header,
  .project-header { flex-wrap: wrap; }
  .card { padding: 18px; }
  .kv > div { flex-wrap: wrap; }
  .project-actions { flex-direction: column; align-items: stretch; }
  .log-content { padding: 12px; }
}
