:root {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-elev-2: #1c2128;
  --border: #2d333b;
  --text: #e6edf3;
  --text-dim: #9da7b3;
  --accent: #4c8dff;
  --accent-strong: #3b6fd4;
  --danger: #e5534b;
  --ok: #3fb950;
  --radius: 10px;
  --sidebar-w: 280px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  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: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  border-radius: 8px;
  min-height: 40px;
  padding: 0 14px;
  transition: background 120ms ease, border-color 120ms ease;
}

button:hover { background: #2a313a; border-color: #3d444d; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.icon-btn {
  min-width: 40px;
  padding: 0 10px;
  font-size: 16px;
}

.primary-btn { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; }
.primary-btn:hover { background: var(--accent); }

.danger-btn { background: transparent; border-color: transparent; color: var(--danger); }
.danger-btn:hover { background: rgba(229, 83, 75, 0.12); border-color: transparent; }

.ghost-btn { background: transparent; }

/* ---------- 登录 ---------- */
#login-view {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-card {
  width: min(360px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-card h1 { margin: 0; font-size: 22px; }
.login-sub { margin: -8px 0 8px; color: var(--text-dim); font-size: 13px; }

.login-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--text-dim);
}

.login-card input {
  font: inherit;
  min-height: 44px;
  padding: 0 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
}

.login-card input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.login-card button { margin-top: 6px; min-height: 44px; }

.login-error { color: var(--danger); font-size: 13px; margin: 0; }

/* ---------- 主布局 ---------- */
#app-view {
  height: 100dvh;
  display: grid;
  grid-template-rows: 56px 1fr;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--bg-elev);
  border-bottom: 1px solid var(--border);
}

.brand { font-size: 16px; margin: 0; white-space: nowrap; }

.host-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
  color: var(--text-dim);
  font-size: 13px;
}

.host-picker select {
  font: inherit;
  min-height: 36px;
  padding: 0 10px;
  background: var(--bg-elev-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.user-label { color: var(--text-dim); font-size: 13px; white-space: nowrap; }

/* 桌面网格 */
#app-view { grid-template-columns: var(--sidebar-w) 1fr; grid-template-rows: 56px 1fr; }
.topbar { grid-column: 1 / -1; }
#sidebar-toggle { display: none; }

#sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.panel { padding: 12px; border-bottom: 1px solid var(--border); }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.panel-head h2 { margin: 0; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-dim); }

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

.item-list li {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
  overflow: hidden;
}

.item-list li:hover { background: var(--bg-elev-2); }

.item-list li.active {
  background: rgba(76, 141, 255, 0.14);
  border-color: rgba(76, 141, 255, 0.45);
}

.project-item { padding: 0; }

.project-item > .item-main {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
}

.project-item > .item-main:hover { background: var(--bg-elev-2); }

.project-item.expanded > .item-main,
.project-item.active > .item-main {
  background: rgba(76, 141, 255, 0.10);
}

.chevron {
  flex: none;
  width: 14px;
  font-size: 11px;
  line-height: 18px;
  color: var(--text-dim);
  transition: transform 0.15s ease;
}

.item-text {
  min-width: 0;
  flex: 1;
}

.session-sub {
  list-style: none;
  margin: 4px 0 8px 18px;
  padding: 0 0 0 10px;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.session-sub li {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid transparent;
  overflow: hidden;
}

.session-sub li:hover { background: var(--bg-elev-2); }

.session-sub li.active {
  background: rgba(76, 141, 255, 0.14);
  border-color: rgba(76, 141, 255, 0.45);
}

.session-new {
  color: var(--accent);
  font-size: 12px;
  font-weight: 500;
}

.item-title {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-sub {
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.item-badge { color: var(--ok); font-size: 11px; }

.empty-tip { color: var(--text-dim); font-size: 12px; padding: 8px 4px; }

/* ---------- 聊天区 ---------- */
#chat-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

.chat-empty {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--text-dim);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.msg {
  max-width: min(760px, 92%);
  align-self: flex-start;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  white-space: normal;
  word-break: break-word;
  font-size: 14px;
}

.msg.user { align-self: flex-end; background: rgba(76, 141, 255, 0.12); border-color: rgba(76, 141, 255, 0.35); }

.msg-role {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.msg.assistant .msg-role { color: var(--ok); }
.msg.user .msg-role { color: var(--accent); }

.msg-body { font-size: 14px; line-height: 1.65; overflow-wrap: anywhere; }

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

.msg-body p { margin: 0.5em 0; }
.msg-body h1, .msg-body h2, .msg-body h3, .msg-body h4, .msg-body h5, .msg-body h6 {
  margin: 0.8em 0 0.4em;
  line-height: 1.35;
  font-weight: 600;
}
.msg-body h1 { font-size: 1.3em; }
.msg-body h2 { font-size: 1.2em; }
.msg-body h3 { font-size: 1.1em; }
.msg-body h4, .msg-body h5, .msg-body h6 { font-size: 1em; }

.msg-body ul, .msg-body ol {
  margin: 0.4em 0;
  padding-left: 1.4em;
}
.msg-body li { margin: 0.2em 0; }

.msg-body blockquote {
  margin: 0.5em 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border);
  color: var(--text-dim);
}

.msg-body pre {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
  margin: 0.6em 0;
}

.msg-body pre code {
  background: transparent;
  border: none;
  padding: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  white-space: pre;
}

.msg-body code {
  background: rgba(127, 127, 127, 0.14);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.9em;
}

.msg-body a { color: var(--accent); text-decoration: none; }
.msg-body a:hover { text-decoration: underline; }

.msg-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.8em 0;
}

.msg-error .msg-body { color: var(--danger); }

.input-bar {
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.input-bar textarea {
  flex: 1;
  font: inherit;
  resize: none;
  max-height: 160px;
  min-height: 42px;
  padding: 10px 12px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
}

.input-bar textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.input-actions { display: flex; gap: 8px; align-items: center; }

/* ---------- 移动端 ---------- */
@media (max-width: 768px) {
  #app-view {
    grid-template-columns: 1fr;
    grid-template-rows: 52px 1fr;
  }

  .topbar { padding: 0 10px; gap: 8px; }
  #sidebar-toggle { display: inline-flex; align-items: center; justify-content: center; }
  .host-picker span, .user-label { display: none; }
  .brand { font-size: 15px; }

  #sidebar {
    position: fixed;
    top: 52px;
    bottom: 0;
    left: 0;
    width: min(300px, 82vw);
    z-index: 30;
    transform: translateX(-102%);
    transition: transform 180ms ease;
    box-shadow: 8px 0 24px rgba(0, 0, 0, 0.35);
  }

  #sidebar.open { transform: translateX(0); }

  #sidebar-overlay {
    position: fixed;
    inset: 52px 0 0 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 20;
  }

  .chat-list { padding: 14px 10px; }
  .msg { max-width: 94%; }
  .input-bar { padding: 8px 10px calc(8px + env(safe-area-inset-bottom)); }

  .icon-btn { min-width: 40px; min-height: 40px; }
}

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