:root {
  --bg: #f6f7f6;
  --panel: #ffffff;
  --ink: #1b2320;
  --ink-soft: #5b6b64;
  --line: #e2e6e3;
  --accent: #1f6f54;
  --accent-ink: #ffffff;
  --accent-soft: #e3f1eb;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --add: #157f3d;
  --add-bg: #e6f4ea;
  --del: #b3261e;
  --del-bg: #fbe9e7;
  --code-bg: #0f1a15;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 30, 24, .06), 0 8px 24px rgba(16, 30, 24, .06);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.6;
}
button, select, textarea { font: inherit; color: inherit; }

.layout { display: flex; height: 100vh; height: 100dvh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 280px; flex: 0 0 280px;
  background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  padding-top: var(--safe-top);
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; }
.brand { font-weight: 700; font-size: 16px; letter-spacing: .2px; }
.session-list { flex: 1; overflow-y: auto; padding: 4px 10px 16px; }
.session-empty { color: var(--ink-soft); text-align: center; margin-top: 24px; font-size: 13px; }
.session-item {
  display: block; width: 100%; text-align: left;
  border: 0; background: transparent; border-radius: 10px;
  padding: 10px 12px; margin: 2px 0; cursor: pointer;
}
.session-item:hover { background: var(--bg); }
.session-item.active { background: var(--accent-soft); }
.session-item .t { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-item .m { font-size: 12px; color: var(--ink-soft); margin-top: 2px; }

/* ---------- Main ---------- */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; padding-top: calc(10px + var(--safe-top));
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.menu-btn { display: none; }
.selects { display: flex; gap: 10px; flex-wrap: wrap; }
.sel { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-soft); }
.sel select {
  max-width: 160px; padding: 6px 8px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--panel);
}

/* ---------- Config banner ---------- */
.config-banner { margin: 12px 16px 0; }
.guide-card {
  background: #fff8e6; border: 1px solid #f0d98a; border-radius: var(--radius);
  padding: 12px 14px; font-size: 13px;
}
.guide-card b { color: #8a6d1a; }

/* ---------- Messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; }
.msg { display: flex; margin: 0 0 16px; }
.msg.user { justify-content: flex-end; }
.bubble {
  max-width: 760px; padding: 10px 14px; border-radius: var(--radius);
  background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow);
  overflow-wrap: anywhere;
}
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.msg.tool .bubble { background: var(--bg); font-size: 13px; color: var(--ink-soft); }
.bubble .role-tag { font-size: 11px; color: var(--ink-soft); margin-bottom: 4px; }
.msg.user .role-tag { color: #d8efe6; }

/* tool status chips */
.tool-row { display: flex; align-items: center; gap: 8px; font-size: 13px; padding: 6px 0; }
.tool-chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px; border-radius: 999px;
  background: var(--bg); border: 1px solid var(--line); font-size: 12px;
}
.tool-chip.running { border-color: var(--accent); color: var(--accent); }
.tool-chip.error { border-color: var(--del); color: var(--del); }
.tool-result { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; white-space: pre-wrap; color: var(--ink-soft); }

/* markdown */
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 8px 0; }
.md ul, .md ol { margin: 8px 0; padding-left: 22px; }
.md code {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .9em;
  background: rgba(27,35,32,.07); padding: 1px 5px; border-radius: 5px;
}
.msg.user .md code { background: rgba(255,255,255,.18); }
.md pre {
  background: var(--code-bg); color: #e6efe9; border-radius: 10px;
  padding: 12px; overflow-x: auto; margin: 10px 0;
}
.md pre code { background: transparent; color: inherit; padding: 0; }

/* diff */
.diff {
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px;
  background: var(--code-bg); color: #cdd8d2; border-radius: 10px;
  padding: 10px; overflow-x: auto; white-space: pre;
}
.diff .add { color: #8fd6a8; background: rgba(21,127,61,.18); display: block; }
.diff .del { color: #f2a7a1; background: rgba(179,38,30,.18); display: block; }
.diff .hunk { color: #9fb3aa; }

/* approval card */
.approval {
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel);
  max-width: 760px; box-shadow: var(--shadow); overflow: hidden;
}
.approval-head { padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 600; }
.approval-meta { padding: 8px 14px; font-size: 12px; color: var(--ink-soft); }
.approval-files { max-height: 260px; overflow: auto; }
.file-diff { border-top: 1px solid var(--line); }
.file-diff summary { padding: 8px 14px; cursor: pointer; font-size: 13px; }
.file-diff .path { font-family: ui-monospace, monospace; }
.approval-actions { display: flex; gap: 10px; padding: 12px 14px; border-top: 1px solid var(--line); }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 10px;
  padding: 8px 12px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-danger { background: var(--danger-soft); border-color: var(--del); color: var(--del); }

/* ---------- Composer ---------- */
.composer { padding: 10px 16px calc(10px + var(--safe-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer-inner { display: flex; gap: 10px; align-items: flex-end; }
#inputBox {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 12px;
  padding: 10px 12px; max-height: 180px; background: var(--bg);
}
.composer-hint { font-size: 11px; color: var(--ink-soft); margin-top: 6px; text-align: center; }

.scrim { display: none; }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 30; width: min(84vw, 300px);
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .scrim.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
  .menu-btn { display: inline-block; }
  .selects { width: 100%; }
  .sel select { max-width: none; flex: 1; }
  .sel { flex: 1 1 40%; }
  .messages { padding: 14px 12px; }
  .bubble { max-width: 100%; }
}
