/* ── Design tokens ─────────────────────────────────────────── */
:root {
  color-scheme: light;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #f5f5f7;
  --surface-3: #ececf0;
  --border: #e5e5ea;
  --border-strong: #d1d1d6;
  --text: #1d1d1f;
  --text-2: #6e6e73;
  --text-3: #a1a1a6;
  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.1);
  --accent-border: rgba(10, 132, 255, 0.35);
  --primary: #1d1d1f;
  --primary-text: #ffffff;
  --user-bg: #1d1d1f;
  --user-text: #ffffff;
  --danger: #d70015;
  --danger-soft: #fff2f2;
  --success: #248a3d;
  --sidebar-bg: rgba(255, 255, 255, 0.76);
  --code-bg: #1d1d1f;
  --code-text: #f5f5f7;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 6px 20px rgba(0, 0, 0, 0.07);
  --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.16);
  --radius-xs: 6px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-mono: "SF Mono", ui-monospace, Menlo, Consolas, monospace;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0b0d;
  --surface: #171719;
  --surface-2: #1f1f22;
  --surface-3: #29292d;
  --border: #2a2a2e;
  --border-strong: #3a3a3f;
  --text: #f5f5f7;
  --text-2: #a1a1a6;
  --text-3: #6e6e73;
  --accent: #409cff;
  --accent-soft: rgba(64, 156, 255, 0.14);
  --accent-border: rgba(64, 156, 255, 0.4);
  --primary: #f5f5f7;
  --primary-text: #0b0b0d;
  --user-bg: #f5f5f7;
  --user-text: #1d1d1f;
  --danger: #ff453a;
  --danger-soft: rgba(255, 69, 58, 0.12);
  --success: #30d158;
  --sidebar-bg: rgba(17, 17, 19, 0.82);
  --code-bg: #101012;
  --code-text: #f5f5f7;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
  --shadow-md: 0 6px 20px rgba(0, 0, 0, 0.35);
  --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #0b0b0d;
    --surface: #171719;
    --surface-2: #1f1f22;
    --surface-3: #29292d;
    --border: #2a2a2e;
    --border-strong: #3a3a3f;
    --text: #f5f5f7;
    --text-2: #a1a1a6;
    --text-3: #6e6e73;
    --accent: #409cff;
    --accent-soft: rgba(64, 156, 255, 0.14);
    --accent-border: rgba(64, 156, 255, 0.4);
    --primary: #f5f5f7;
    --primary-text: #0b0b0d;
    --user-bg: #f5f5f7;
    --user-text: #1d1d1f;
    --danger: #ff453a;
    --danger-soft: rgba(255, 69, 58, 0.12);
    --success: #30d158;
    --sidebar-bg: rgba(17, 17, 19, 0.82);
    --code-bg: #101012;
    --code-text: #f5f5f7;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 6px 20px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 18px 50px rgba(0, 0, 0, 0.5);
  }
}

/* ── Base ─────────────────────────────────────────────────── */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, input, textarea { font: inherit; }
button { color: inherit; }
::selection { background: var(--accent-soft); color: var(--text); }

.app {
  display: flex;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

main { flex: 1; min-width: 0; height: 100%; display: flex; }
.main-shell {
  flex: 1;
  min-width: 0;
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 28px;
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* ── Brand / header ───────────────────────────────────────── */
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 9px;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 15px;
  line-height: 1;
}
header {
  flex: none;
  display: flex;
  align-items: center;
  gap: 11px;
  min-height: 68px;
  padding: 12px 0;
}
.header-mark { display: none; }
.head-text { min-width: 0; }
header h1 {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.sub {
  margin: 2px 0 0;
  color: var(--text-2);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.head-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 26px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-2);
  font-size: 11px;
  background: var(--surface);
}
.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
}
#theme-toggle {
  width: 32px;
  height: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-2);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}
#theme-toggle:hover { background: var(--surface-2); color: var(--text); }
.theme-icon { width: 16px; height: 16px; }
.icon-sun { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }
:root[data-theme="dark"] .icon-moon { display: none; }

#sidebar-toggle {
  display: none;
  flex: none;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 16px;
  cursor: pointer;
}

/* ── Sidebar ──────────────────────────────────────────────── */
#sidebar {
  width: 280px;
  flex: none;
  display: flex;
  flex-direction: column;
  height: 100%;
  border-right: 1px solid var(--border);
  background: var(--sidebar-bg);
  -webkit-backdrop-filter: blur(24px) saturate(1.3);
  backdrop-filter: blur(24px) saturate(1.3);
}
.sidebar-head {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px 16px 12px;
}
.sidebar-brand { display: flex; align-items: center; gap: 9px; padding: 0 2px; }
.sidebar-brand-text { font-size: 14px; font-weight: 600; letter-spacing: -0.01em; }
#new-session {
  width: 100%;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: none;
  border-radius: 10px;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.15s ease;
}
#new-session:hover { opacity: 0.86; }
#new-session:active { transform: scale(0.99); }
.sidebar-search { flex: none; padding: 0 16px 10px; }
#session-search {
  width: 100%;
  height: 34px;
  padding: 0 11px;
  border: 1px solid transparent;
  border-radius: 9px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 12.5px;
  outline: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}
#session-search::placeholder { color: var(--text-3); }
#session-search:focus {
  background: var(--surface);
  border-color: var(--accent-border);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
#session-list {
  flex: 1;
  overflow-y: auto;
  padding: 2px 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.session-group { display: flex; flex-direction: column; gap: 2px; }
.session-group-label {
  padding: 13px 10px 5px;
  color: var(--text-3);
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
}
.session-item {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  transition: background 0.15s ease;
}
.session-item:hover { background: var(--surface-2); }
.session-item.active { background: var(--surface-3); }
.session-item.active::before {
  content: "";
  position: absolute;
  left: 0;
  top: 10px;
  bottom: 10px;
  width: 2.5px;
  border-radius: 2px;
  background: var(--accent);
}
.session-item .item-head { display: flex; align-items: center; gap: 5px; }
.session-item .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 18px;
  font-weight: 500;
}
.session-item .time { font-size: 11px; color: var(--text-3); }
.session-item .more {
  position: absolute;
  right: 6px;
  top: 6px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-3);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: none;
}
.session-item:hover .more,
.session-item.active .more { display: block; }
.session-item .more:hover { background: var(--surface); color: var(--text); }
.pin-tag {
  flex: none;
  font-size: 9.5px;
  line-height: 16px;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: 4px;
  padding: 0 5px;
}
.rename-input {
  width: 100%;
  font-size: 13px;
  padding: 3px 6px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  outline: none;
}
.session-menu {
  position: absolute;
  right: 6px;
  top: 30px;
  z-index: 30;
  min-width: 116px;
  padding: 5px;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
}
.session-menu button {
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  padding: 7px 9px;
  font-size: 12.5px;
  text-align: left;
  cursor: pointer;
}
.session-menu button:hover { background: var(--surface-2); }
.session-menu button.danger { color: var(--danger); }
.session-menu button.danger:hover { background: var(--danger-soft); }
.session-empty { color: var(--text-3); font-size: 12px; padding: 12px 10px; }
.sidebar-foot {
  flex: none;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 10px 18px;
  border-top: 1px solid var(--border);
  color: var(--text-3);
  font-size: 10.5px;
}
.foot-dot { width: 3px; height: 3px; border-radius: 50%; background: var(--text-3); }

/* ── Empty state ──────────────────────────────────────────── */
#empty-state {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px 16px 80px;
}
#empty-state[hidden] { display: none; }
.empty-mark {
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 16px;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 24px;
  box-shadow: var(--shadow-md);
  margin-bottom: 18px;
}
#empty-state h2 {
  margin: 0 0 7px;
  font-size: 21px;
  font-weight: 600;
  letter-spacing: -0.02em;
}
#empty-state p { margin: 0; color: var(--text-2); font-size: 13px; }
.empty-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 22px;
  max-width: 640px;
}
.empty-chips button {
  padding: 7px 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 12.5px;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.empty-chips button:hover {
  border-color: var(--accent-border);
  color: var(--accent);
  background: var(--accent-soft);
}

/* ── Messages ─────────────────────────────────────────────── */
#messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-anchor: none;
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 8px 4px 26px;
}
#messages:empty { display: none; }
#messages.streaming {
  -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 30px), transparent);
  mask-image: linear-gradient(to bottom, #000 calc(100% - 30px), transparent);
}
.msg {
  max-width: 100%;
  padding: 0;
  font-size: 15px;
  line-height: 1.65;
  white-space: pre-wrap;
}
.msg.user {
  align-self: flex-end;
  max-width: min(720px, 78%);
  padding: 10px 16px;
  border-radius: 18px;
  background: var(--user-bg);
  color: var(--user-text);
  white-space: normal;
}
.msg.agent {
  align-self: stretch;
  background: transparent;
  border: none;
  white-space: normal;
}
.msg.agent.error {
  padding: 12px 14px;
  border: 1px solid rgba(215, 0, 21, 0.25);
  border-radius: var(--radius-md);
  background: var(--danger-soft);
}
.msg.user .md { white-space: pre-wrap; }
.msg.user .md > *:first-child { margin-top: 0; }
.msg.user .md > *:last-child { margin-bottom: 0; }
.msg.user .md a { color: currentColor; text-decoration: underline; }
.msg.user .md code {
  background: rgba(127, 127, 127, 0.22);
  padding: 1px 4px;
  border-radius: 4px;
}
.msg.user .md ul, .msg.user .md ol { margin: 4px 0; padding-left: 20px; }
.msg.user .md blockquote {
  margin: 4px 0;
  padding-left: 9px;
  border-left: 3px solid currentColor;
  opacity: 0.8;
}

/* ── Markdown ─────────────────────────────────────────────── */
.md { min-width: 0; }
.md p { margin: 8px 0; }
.md h1, .md h2, .md h3, .md h4 {
  margin: 18px 0 8px;
  font-weight: 600;
  line-height: 1.35;
  letter-spacing: -0.01em;
}
.md h1 { font-size: 20px; }
.md h2 { font-size: 17px; }
.md h3 { font-size: 15.5px; }
.md h4 { font-size: 14.5px; }
.md > *:first-child { margin-top: 0; }
.md ul, .md ol { margin: 8px 0; padding-left: 22px; }
.md li { margin: 3px 0; }
.md code {
  background: var(--surface-3);
  border-radius: 5px;
  padding: 1px 5px;
  font-size: 0.88em;
  font-family: var(--font-mono);
}
.md pre {
  margin: 10px 0;
  padding: 14px 16px;
  overflow-x: auto;
  border-radius: var(--radius-md);
  background: var(--code-bg);
  color: var(--code-text);
  font-size: 12.5px;
  line-height: 1.6;
}
.md pre code { background: none; padding: 0; color: inherit; }
.md blockquote {
  margin: 10px 0;
  padding: 2px 0 2px 13px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-2);
}
.md a { color: var(--accent); text-decoration: none; }
.md a:hover { text-decoration: underline; }
.md hr { border: none; border-top: 1px solid var(--border); margin: 14px 0; }
.md strong { font-weight: 600; }

/* ── Tables ───────────────────────────────────────────────── */
.table-scroll {
  overflow: auto;
  max-width: 100%;
  max-height: 60vh;
  margin: 12px 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.md table {
  border-collapse: separate;
  border-spacing: 0;
  width: max-content;
  min-width: 100%;
  font-size: 12.5px;
  line-height: 1.45;
  font-variant-numeric: tabular-nums;
}
.md th, .md td {
  padding: 8px 12px;
  text-align: left;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.md th {
  color: var(--text-2);
  font-size: 11.5px;
  font-weight: 600;
  background: var(--surface-2);
}
.md td { color: var(--text); background: var(--surface); }
.md tbody tr:last-child td { border-bottom: none; }
.md tbody tr:hover td { background: var(--surface-2); }
.md thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--surface-2);
  box-shadow: inset 0 -1px 0 var(--border);
}
.md th:first-child,
.md td:first-child {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.md thead th:first-child { z-index: 4; background: var(--surface-2); }
.md tbody tr:hover td:first-child { background: var(--surface-2); }
.md .num { text-align: right; }
.md .long {
  max-width: 280px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.table-scroll::-webkit-scrollbar,
#session-list::-webkit-scrollbar,
#messages::-webkit-scrollbar,
.sheet-panel *::-webkit-scrollbar { width: 8px; height: 8px; }
.table-scroll::-webkit-scrollbar-thumb,
#session-list::-webkit-scrollbar-thumb,
#messages::-webkit-scrollbar-thumb,
.sheet-panel *::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
}
.table-scroll::-webkit-scrollbar-track,
#session-list::-webkit-scrollbar-track,
#messages::-webkit-scrollbar-track { background: transparent; }

/* ── KPI cards ────────────────────────────────────────────── */
.kpi-block { margin: 14px 0 4px; }
.kpi-title {
  margin-bottom: 8px;
  color: var(--text-2);
  font-size: 12px;
  font-weight: 600;
}
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
  gap: 8px;
}
.kpi-card {
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
}
.kpi-label { color: var(--text-2); font-size: 11.5px; }
.kpi-value {
  margin-top: 4px;
  font-size: 21px;
  font-weight: 600;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
}

/* ── Timeline / thinking / activity ───────────────────────── */
.timeline {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}
.timeline[hidden] { display: none; }
.step {
  position: relative;
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 22px;
  padding-left: 16px;
  color: var(--text-2);
  font-size: 12px;
}
.step::before {
  content: "";
  position: absolute;
  left: 2px;
  top: 50%;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--border-strong);
  transform: translateY(-50%);
}
.step.running::before { background: var(--accent); }
.step.done::before { background: var(--success); }
.step.error::before { background: var(--danger); }
.step .mark { color: var(--success); font-weight: 600; flex: none; }
.step.error { color: var(--danger); }
.step.error .mark { color: var(--danger); }
.step .meta {
  padding: 1px 6px;
  border-radius: 5px;
  background: var(--surface-2);
  color: var(--text-3);
  font-size: 10.5px;
  flex: none;
}
.thinking {
  margin: 2px 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  overflow: hidden;
}
.thinking summary {
  padding: 7px 10px;
  color: var(--text-2);
  font-size: 11.5px;
  cursor: pointer;
  user-select: none;
}
.thinking summary:hover { color: var(--text); }
.thinking-body {
  max-height: 220px;
  overflow-y: auto;
  padding: 0 10px 9px;
  color: var(--text-2);
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
}
.activity {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  color: var(--text-2);
  font-size: 12px;
}
.activity[hidden] { display: none; }
.spin {
  width: 11px;
  height: 11px;
  flex: none;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  animation: step-spin 0.75s linear infinite;
}
@keyframes step-spin { to { transform: rotate(360deg); } }
@keyframes stream-in {
  from { opacity: 0.35; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
.stream-enter {
  animation: stream-in 360ms cubic-bezier(0.22, 0.61, 0.36, 1) both;
  will-change: opacity, transform;
}
@keyframes stream-fade {
  from { opacity: 0.55; }
  to { opacity: 1; }
}

/* ── Composer ─────────────────────────────────────────────── */
#composer {
  flex: none;
  position: relative;
  padding: 10px 0 18px;
  background: linear-gradient(to bottom, transparent, var(--bg) 22%);
}
#chat-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 9px; }
#input-wrap {
  position: relative;
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border-strong);
  border-radius: 14px;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
#input-wrap:focus-within {
  border-color: var(--accent-border);
  box-shadow: 0 0 0 3px var(--accent-soft), var(--shadow-sm);
}
#message-input {
  width: 100%;
  height: 44px;
  padding: 0 46px 0 15px;
  border: none;
  border-radius: 14px;
  background: transparent;
  color: var(--text);
  font-size: 14.5px;
  outline: none;
}
#message-input::placeholder { color: var(--text-3); }
#expand-editor {
  position: absolute;
  right: 7px;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
}
#expand-editor:hover { background: var(--surface-2); color: var(--text); }
#composer-actions { display: flex; flex: none; gap: 7px; }
#composer-actions button {
  height: 44px;
  padding: 0 17px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
}
.send-btn { display: inline-flex; align-items: center; gap: 6px; }
#composer-actions button:hover { opacity: 0.88; }
#composer-actions button:disabled { opacity: 0.5; cursor: not-allowed; }
#stop-btn { background: var(--danger); color: #fff; }
#composer-status {
  flex-basis: 100%;
  margin: 0;
  color: var(--danger);
  font-size: 12px;
}
#composer-status[hidden] { display: none; }

/* ── Choice popover ───────────────────────────────────────── */
#choice-popover {
  position: absolute;
  left: 0;
  right: 0;
  bottom: calc(100% + 8px);
  width: auto;
  max-height: min(60vh, 520px);
  overflow-y: auto;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  z-index: 30;
}
#choice-popover[hidden] { display: none; }
#choice-popover:focus { outline: none; }
.choice-title { margin: 0 0 4px; font-size: 14.5px; font-weight: 600; color: var(--text); }
.choice-desc { margin: 0; color: var(--text-2); font-size: 12px; line-height: 1.5; }
.choice-options { display: flex; flex-direction: column; gap: 7px; margin: 10px 0; }
.choice-option {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 9px 12px;
  text-align: left;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease;
}
.choice-option:hover { border-color: var(--accent-border); background: var(--surface-2); }
.choice-option.selected { border-color: var(--accent); background: var(--accent-soft); }
.choice-option .label { color: var(--text); font-size: 13.5px; font-weight: 600; }
.choice-option .detail { color: var(--text-2); font-size: 11.5px; line-height: 1.45; }
.choice-option:disabled { opacity: 0.6; cursor: not-allowed; }
.choice-unsupported { color: var(--danger); font-size: 12.5px; }
.choice-custom { display: flex; gap: 8px; margin: 8px 0 4px; }
.choice-custom input {
  flex: 1;
  height: 36px;
  padding: 0 11px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 12.5px;
  outline: none;
}
.choice-custom input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.choice-custom button {
  padding: 0 14px;
  border: none;
  border-radius: 8px;
  background: var(--surface-3);
  color: var(--text);
  font-size: 12.5px;
  cursor: pointer;
  white-space: nowrap;
}
.choice-custom button:disabled { opacity: 0.5; cursor: not-allowed; }
.choice-footer { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.choice-status { flex: 1; margin: 0; color: var(--danger); font-size: 11.5px; }
.choice-footer button {
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  font-size: 12.5px;
  cursor: pointer;
}
.choice-cancel { background: var(--surface-3); color: var(--text); }
.choice-confirm { background: var(--primary); color: var(--primary-text); }
.choice-footer button:disabled { opacity: 0.55; cursor: not-allowed; }

/* ── Editor sheet ─────────────────────────────────────────── */
body.sheet-open { overflow: hidden; }
#editor-sheet { position: fixed; inset: 0; z-index: 40; }
#editor-sheet[hidden] { display: none; }
#sheet-backdrop {
  display: none;
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.38);
}
#editor-sheet.partial #sheet-backdrop { display: block; }
.sheet-panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 100vh;
  height: 100dvh;
  max-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  box-shadow: 0 -12px 50px rgba(0, 0, 0, 0.24);
  transform: translateY(100%);
  transition: transform 0.24s ease;
}
#editor-sheet.open .sheet-panel { transform: translateY(0); }
#editor-sheet.dragging .sheet-panel { transition: none; }
#sheet-grip {
  flex: none;
  height: 20px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  cursor: ns-resize;
  touch-action: none;
  outline: none;
}
#sheet-grip:focus-visible .grip-bar { background: var(--accent); }
.grip-bar { width: 40px; height: 4px; margin-bottom: 2px; border-radius: 2px; background: var(--border-strong); }
.sheet-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 18px 10px;
  border-bottom: 1px solid var(--border);
}
.sheet-title { color: var(--text); font-size: 14px; font-weight: 600; }
#sheet-close {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 12.5px;
  cursor: pointer;
}
#sheet-close:hover { background: var(--surface-2); color: var(--text); }
#sheet-toolbar {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 7px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
#sheet-toolbar button {
  padding: 4px 8px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-2);
  font-size: 11.5px;
  line-height: 1.4;
  cursor: pointer;
}
#sheet-toolbar button:hover { background: var(--surface-3); color: var(--text); }
.tb-sep { width: 1px; align-self: stretch; margin: 2px 5px; background: var(--border); }
#sheet-editor {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 20px;
  color: var(--text);
  font-size: 15px;
  line-height: 1.7;
  outline: none;
  white-space: pre-wrap;
  word-break: break-word;
}
#sheet-editor.is-empty::before {
  content: attr(data-placeholder);
  color: var(--text-3);
  pointer-events: none;
}
#sheet-popover-slot { flex: none; }
#sheet-popover-slot #choice-popover {
  position: static;
  left: auto;
  right: auto;
  bottom: auto;
  width: auto;
  max-height: 40vh;
  margin: 0 18px 8px;
  z-index: 31;
}
.sheet-foot {
  flex: none;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}
#sheet-status { flex: 1; margin: 0; color: var(--danger); font-size: 12px; }
#sheet-send {
  height: 38px;
  padding: 0 20px;
  border: none;
  border-radius: 10px;
  background: var(--primary);
  color: var(--primary-text);
  font-size: 13.5px;
  cursor: pointer;
}
#sheet-send:hover { opacity: 0.88; }
#sheet-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 1024px) {
  .main-shell { padding: 0 18px; }
  #sidebar { width: 252px; }
}
@media (max-width: 820px) {
  .main-shell { padding: 0 14px; }
  header { min-height: 60px; }
  #sidebar-toggle { display: inline-flex; align-items: center; justify-content: center; }
  .header-mark { display: inline-flex; }
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-lg);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-mask {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 19;
  }
  #sidebar-mask[hidden] { display: none; }
  .session-item .more { display: block; }
  .status-pill { display: none; }
  .msg.user { max-width: 88%; }
  .kpi-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #composer-actions button { padding: 0 13px; }
}
@media (max-width: 520px) {
  .sub { display: none; }
  header h1 { font-size: 14px; }
  #messages { gap: 16px; padding-bottom: 18px; }
  .md { font-size: 14px; }
  .md th, .md td { padding: 7px 10px; }
  .md .long { max-width: 200px; }
  #empty-state { padding-bottom: 40px; }
  #empty-state h2 { font-size: 18px; }
  .empty-chips { gap: 6px; }
  #choice-popover { padding: 12px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  #messages.streaming {
    -webkit-mask-image: none;
    mask-image: none;
  }
}
