:root {
  --accent: #10a37f;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

:root[data-theme="dark"] {
  --bg: #212121;
  --bg-sidebar: #171717;
  --bg-elevated: #2f2f2f;
  --bg-hover: #2a2a2a;
  --bg-input: #303030;
  --text: #ececec;
  --text-muted: #9b9b9b;
  --text-dim: #6e6e6e;
  --border: #3d3d3d;
  --user-bubble: #303030;
  --code-bg: #0d0d0d;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-sidebar: #f9f9f9;
  --bg-elevated: #ffffff;
  --bg-hover: #ececec;
  --bg-input: #ffffff;
  --text: #0d0d0d;
  --text-muted: #6b6b6b;
  --text-dim: #9a9a9a;
  --border: #e3e3e3;
  --user-bubble: #f4f4f4;
  --code-bg: #f6f8fa;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--text-muted); font-size: 12px; }

/* ---------------- login ---------------- */
.login-screen {
  height: 100%;
  display: grid;
  place-items: center;
  background: radial-gradient(1200px 600px at 50% -10%, rgba(16, 163, 127, 0.18), transparent 60%), var(--bg);
}
.login-card {
  width: min(380px, 92vw);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: var(--shadow);
}
.login-logo { display: flex; justify-content: center; }
.login-card h1 { margin: 6px 0 0; font-size: 22px; text-align: center; }
.login-sub { margin: 0 0 8px; text-align: center; color: var(--text-muted); font-size: 13px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); }
.login-card input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}
.login-card input:focus { outline: 2px solid rgba(16, 163, 127, 0.45); border-color: var(--accent); }
.login-error { color: #ff6b6b; font-size: 13px; }
.primary-btn {
  background: var(--accent);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.primary-btn:hover { filter: brightness(1.07); }
.primary-btn:disabled { opacity: 0.6; cursor: default; }
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 8px 12px;
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
}
.ghost-btn:hover { background: var(--bg-hover); }

/* ---------------- layout ---------------- */
.app { display: flex; height: 100%; }
.sidebar {
  width: 264px;
  flex: 0 0 264px;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.2s ease;
}
.app.collapsed .sidebar { margin-left: -264px; }
.app.collapsed .topbar-title { display: block; }

.sidebar-head { padding: 10px; }
.new-chat {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
}
.new-chat:hover { background: var(--bg-hover); }
.sidebar-search { padding: 0 10px 8px; }
.sidebar-search input {
  width: 100%;
  background: var(--bg-hover);
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 8px 10px;
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
}
.sidebar-search input:focus { outline: none; border-color: var(--border); }

.thread-list-wrap { flex: 1; overflow-y: auto; padding: 4px 10px 10px; }
.sidebar-label { font-size: 11px; color: var(--text-dim); padding: 10px 8px 6px; letter-spacing: 0.04em; }
.thread-list { display: flex; flex-direction: column; gap: 2px; }
.thread-item {
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 9px;
  color: var(--text);
  padding: 9px 10px;
  cursor: pointer;
  font-size: 13.5px;
  font-family: inherit;
  display: flex;
  flex-direction: column;
  gap: 3px;
  overflow: hidden;
}
.thread-item:hover { background: var(--bg-hover); }
.thread-item.active { background: var(--bg-hover); }
.thread-item .t-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-item .t-meta { font-size: 11px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-foot { border-top: 1px solid var(--border); padding: 8px 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.side-row { display: flex; gap: 6px; }
.side-btn {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  background: transparent;
  border: 0;
  border-radius: 9px;
  color: var(--text-muted);
  padding: 9px 10px;
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  overflow: hidden;
  white-space: nowrap;
}
.side-btn:hover { background: var(--bg-hover); color: var(--text); }
.side-btn span { overflow: hidden; text-overflow: ellipsis; }
#cwdBtn span { font-family: var(--mono); font-size: 12px; }
.user-chip { display: flex; align-items: center; gap: 8px; padding: 8px 10px 0; font-size: 13px; color: var(--text-muted); }
.avatar {
  width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 12px; font-weight: 600;
}

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 56px;
  flex: 0 0 56px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
}
.icon-btn {
  background: transparent;
  border: 0;
  color: var(--text-muted);
  border-radius: 8px;
  width: 32px; height: 32px;
  display: grid; place-items: center;
  cursor: pointer;
  font-size: 15px;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.topbar-title { font-size: 14px; font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { display: flex; gap: 8px; align-items: center; }
.select-wrap select {
  background: var(--bg-hover);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 6px 10px;
  font-size: 12.5px;
  font-family: inherit;
  cursor: pointer;
}

/* ---------------- messages ---------------- */
.messages { flex: 1; overflow-y: auto; padding: 24px 0 8px; scroll-behavior: smooth; }
.messages > * { max-width: 780px; margin: 0 auto; padding: 0 24px; }

.empty-state { text-align: center; padding-top: 8vh; display: flex; flex-direction: column; align-items: center; gap: 10px; }
.empty-logo { margin-bottom: 4px; }
.empty-state h2 { margin: 0; font-size: 26px; font-weight: 600; }
.empty-state p { margin: 0; color: var(--text-muted); font-size: 13.5px; }
.empty-state code { font-family: var(--mono); background: var(--bg-hover); padding: 1px 5px; border-radius: 5px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.suggestions button {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}
.suggestions button:hover { background: var(--bg-hover); color: var(--text); }

.turn { padding: 8px 24px 18px; display: flex; flex-direction: column; gap: 12px; }
.turn.user { align-items: flex-end; }
.bubble-user {
  background: var(--user-bubble);
  border-radius: 18px;
  padding: 10px 16px;
  max-width: 82%;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.6;
}
.turn.assistant { align-items: stretch; }
.assistant-head { display: flex; align-items: center; gap: 8px; color: var(--text-muted); font-size: 12.5px; }
.assistant-head .badge {
  width: 22px; height: 22px; border-radius: 6px; background: var(--accent);
  display: grid; place-items: center; color: #fff; font-size: 11px; font-weight: 700;
}
.assistant-body { display: flex; flex-direction: column; gap: 10px; }

.markdown { line-height: 1.65; word-break: break-word; }
.markdown p { margin: 0 0 10px; }
.markdown p:last-child { margin-bottom: 0; }
.markdown h1, .markdown h2, .markdown h3 { margin: 14px 0 8px; font-size: 16px; }
.markdown ul, .markdown ol { margin: 6px 0 10px; padding-left: 22px; }
.markdown li { margin: 3px 0; }
.markdown code { font-family: var(--mono); font-size: 12.8px; background: var(--bg-hover); padding: 1.5px 5px; border-radius: 5px; }
.markdown pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  overflow-x: auto;
  margin: 8px 0;
  position: relative;
}
.markdown pre code { background: none; padding: 0; font-size: 12.8px; line-height: 1.55; }
.markdown a { color: #58a6ff; }
.markdown blockquote { margin: 8px 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--text-muted); }
.markdown table { border-collapse: collapse; margin: 8px 0; font-size: 13px; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 5px 9px; }

.tool-card {
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--bg-elevated);
  overflow: hidden;
  font-size: 13px;
}
.tool-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  user-select: none;
}
.tool-head:hover { background: var(--bg-hover); }
.tool-head .tool-icon { color: var(--text-muted); display: grid; place-items: center; }
.tool-head .tool-name { font-weight: 600; font-size: 12.5px; }
.tool-head .tool-status { margin-left: auto; color: var(--text-dim); font-size: 11.5px; white-space: nowrap; }
.tool-head code.cmdline {
  font-family: var(--mono);
  font-size: 12.4px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
}
.tool-body { border-top: 1px solid var(--border); }
.tool-card.collapsed .tool-body { display: none; }
.tool-output {
  margin: 0;
  padding: 10px 12px;
  background: var(--code-bg);
  font-family: var(--mono);
  font-size: 12.2px;
  line-height: 1.5;
  max-height: 320px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-all;
}
.tool-card.failed { border-color: #7f2c2c; }
.tool-card.failed .tool-status { color: #ff7b72; }
.change-list { margin: 0; padding: 8px 12px; list-style: none; display: flex; flex-direction: column; gap: 5px; }
.change-list li { display: flex; gap: 8px; align-items: center; font-family: var(--mono); font-size: 12.2px; }
.kind { font-size: 10.5px; padding: 1px 6px; border-radius: 5px; font-family: var(--font); }
.kind.add { background: rgba(63, 185, 80, 0.18); color: #3fb950; }
.kind.update { background: rgba(210, 153, 34, 0.18); color: #d29922; }
.kind.delete { background: rgba(248, 81, 73, 0.18); color: #f85149; }

details.reasoning {
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 6px 12px;
  color: var(--text-muted);
  font-size: 13px;
}
details.reasoning summary { cursor: pointer; font-size: 12.5px; }
details.reasoning .reasoning-text { margin-top: 8px; white-space: pre-wrap; line-height: 1.6; font-size: 12.8px; }

.turn-footer { font-size: 11.5px; color: var(--text-dim); display: flex; gap: 12px; }
.log-line { font-family: var(--mono); font-size: 11.8px; color: var(--text-dim); white-space: pre-wrap; }
.error-card {
  border: 1px solid #7f2c2c;
  background: rgba(248, 81, 73, 0.08);
  color: #ff7b72;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
  white-space: pre-wrap;
}
.spinner {
  width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid var(--text-dim); border-top-color: transparent;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
.thinking { display: flex; align-items: center; gap: 8px; color: var(--text-muted); font-size: 13px; }

/* ---------------- composer ---------------- */
.composer-wrap { padding: 6px 24px 16px; }
.composer {
  max-width: 780px;
  margin: 0 auto;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 10px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  box-shadow: var(--shadow);
}
.composer textarea {
  width: 100%;
  background: transparent;
  border: 0;
  resize: none;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.55;
  max-height: 220px;
  outline: none;
  padding: 4px 4px 0;
}
.composer-bar { display: flex; align-items: center; }
.composer-meta { flex: 1; padding-left: 4px; }
.status-text { font-size: 11.5px; color: var(--text-dim); }
.composer-buttons { display: flex; align-items: center; gap: 8px; }
.send-btn {
  width: 32px; height: 32px; border-radius: 50%;
  background: var(--accent); color: #fff; border: 0;
  display: grid; place-items: center; cursor: pointer;
}
.send-btn:disabled { opacity: 0.45; cursor: default; }
.stop-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12.5px;
  cursor: pointer;
  font-family: inherit;
}
.stop-btn:hover { background: var(--bg-hover); }
.composer-note { max-width: 780px; margin: 8px auto 0; text-align: center; font-size: 11px; color: var(--text-dim); }

/* ---------------- modal ---------------- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.55); display: grid; place-items: center; z-index: 50; }
.modal-card {
  width: min(620px, 92vw);
  height: min(560px, 82vh);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.modal-path { display: flex; gap: 8px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); }
.modal-path input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 10px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12.5px;
}
.cwd-list { flex: 1; overflow-y: auto; padding: 6px; }
.cwd-row {
  display: flex; align-items: center; gap: 8px;
  width: 100%; text-align: left;
  background: transparent; border: 0; color: var(--text);
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  font-family: var(--mono); font-size: 12.7px;
}
.cwd-row:hover { background: var(--bg-hover); }
.cwd-row .size { margin-left: auto; color: var(--text-dim); font-size: 11px; }
.modal-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 14px; border-top: 1px solid var(--border); }
.modal-card.small { height: auto; max-width: 420px; }
.modal-card.wide { width: min(760px, 94vw); height: min(680px, 88vh); }
.modal-body { padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.modal-body label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); }
.modal-body input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
}
.modal-body input:focus { outline: 2px solid rgba(16, 163, 127, 0.45); border-color: var(--accent); }

/* ---------------- 账号与权限 ---------------- */
.role-chip {
  margin-left: auto;
  font-size: 11px;
  color: var(--accent);
  border: 1px solid rgba(16, 163, 127, 0.5);
  border-radius: 999px;
  padding: 1px 7px;
}
.avatar.small { width: 22px; height: 22px; font-size: 11px; }
.users-toolbar { display: flex; align-items: center; gap: 10px; }
.users-toolbar .primary-btn { padding: 7px 12px; font-size: 13px; }

.users-list { display: flex; flex-direction: column; gap: 8px; overflow-y: auto; flex: 1; min-height: 80px; }
.user-row {
  display: flex; align-items: flex-start; gap: 10px;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
}
.user-row.disabled { opacity: 0.65; }
.user-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.user-line { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.user-line code { font-family: var(--mono); font-size: 12px; color: var(--text-muted); }
.user-meta { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 11.5px; color: var(--text-muted); }
.user-actions { display: flex; gap: 6px; flex-shrink: 0; }
.user-actions .ghost-btn { padding: 5px 9px; font-size: 12px; }
.ghost-btn.danger { color: #ff7a7a; border-color: rgba(255, 122, 122, 0.4); }
.ghost-btn:disabled { opacity: 0.4; cursor: default; }

.tag {
  font-size: 11px; border-radius: 999px; padding: 1px 8px;
  border: 1px solid var(--border); color: var(--text-muted);
}
.tag.admin { color: var(--accent); border-color: rgba(16, 163, 127, 0.5); }
.tag.off { color: #ff7a7a; border-color: rgba(255, 122, 122, 0.45); }
.tag.me { color: var(--text); border-color: var(--text-dim); }

.user-form {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  max-height: 60%;
}
.user-form-head { display: flex; align-items: center; justify-content: space-between; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.user-form select {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
}
.perm-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; }
.perm { flex-direction: row !important; align-items: center; gap: 8px !important; color: var(--text) !important; font-size: 13px !important; }
.perm input { width: 15px; height: 15px; padding: 0; accent-color: var(--accent); }
.user-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-muted); }

@media (max-width: 640px) {
  .grid-2, .perm-grid { grid-template-columns: 1fr; }
  .user-row { flex-direction: column; }
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  background: #2f2f2f;
  color: #fff;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  box-shadow: var(--shadow);
  z-index: 60;
  max-width: 80vw;
}

@media (max-width: 820px) {
  .sidebar { position: absolute; z-index: 20; height: 100%; }
  .app.collapsed .sidebar { margin-left: -264px; }
  .app:not(.collapsed) .sidebar { box-shadow: var(--shadow); }
  .topbar-title { display: block; }
}
