:root {
  color-scheme: light dark;
  --bg: #f6f6f4;
  --surface: #ffffff;
  --surface-2: #efefec;
  --border: #dcdcd7;
  --text: #1d1f1d;
  --text-2: #5a5f5a;
  --accent: #0f6b62;
  --accent-text: #ffffff;
  --accent-soft: #e2efec;
  --danger: #b42318;
  --danger-soft: #fbe9e7;
  --warn-soft: #fbf3dd;
  --warn-text: #6b4e00;
  --focus: #0f6b62;
  --code-bg: #f0f0ec;
  --radius: 10px;
  --sidebar-w: 272px;
  --font: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Source Han Sans SC", sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161816;
    --surface: #1e211e;
    --surface-2: #262a26;
    --border: #353a35;
    --text: #e8eae7;
    --text-2: #a3a9a2;
    --accent: #5cc4b5;
    --accent-text: #0d1a18;
    --accent-soft: #1f3632;
    --danger: #f2877b;
    --danger-soft: #3a2220;
    --warn-soft: #362f1a;
    --warn-text: #ecd28c;
    --focus: #5cc4b5;
    --code-bg: #232723;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 var(--font);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 16px; border-radius: 8px; border: 1px solid transparent;
  cursor: pointer; font-weight: 500; white-space: nowrap; background: var(--surface-2);
}
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-danger { background: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .btn-danger { color: #1b0e0c; } }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn-block { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px; border-radius: 8px;
  border: 0; background: transparent; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- boot / login ---------- */
.boot { display: grid; place-items: center; height: 100%; color: var(--text-2); padding: 16px; text-align: center; }
.login { display: grid; place-items: center; height: 100%; padding: 16px; overflow-y: auto; }
.login-card {
  width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 14px; padding: 28px 24px;
}
.login-title { margin: 0 0 4px; font-size: 20px; }
.login-hint { margin: 0 0 20px; color: var(--text-2); font-size: 14px; }
.field-label { display: block; font-size: 14px; font-weight: 500; margin-bottom: 6px; }
.login-card input {
  width: 100%; min-height: 44px; padding: 8px 12px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); margin-bottom: 12px;
}
.form-error { color: var(--danger); font-size: 14px; margin: 0 0 12px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); height: 100dvh; height: 100vh; }
@supports (height: 100dvh) { .app { height: 100dvh; } }

.sidebar {
  display: flex; flex-direction: column; min-height: 0; background: var(--surface);
  border-right: 1px solid var(--border);
}
.sidebar-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 12px 12px 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
}
.brand { font-weight: 600; }
.conv-nav { flex: 1; overflow-y: auto; padding: 4px 8px; }
.conv-empty { color: var(--text-2); font-size: 14px; padding: 8px; margin: 0; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-item {
  display: flex; align-items: baseline; gap: 8px; width: 100%; padding: 9px 10px; border: 0;
  background: transparent; border-radius: 8px; text-align: left; cursor: pointer;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item[aria-current="page"] { background: var(--accent-soft); }
.conv-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.conv-meta { flex-shrink: 0; font-size: 12px; color: var(--text-2); }
.conv-meta.is-running { color: var(--accent); font-weight: 600; }
.sidebar-foot { padding: 12px; padding-bottom: max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; min-height: 56px; padding: 8px 16px;
  padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--border); background: var(--bg);
}
.menu-btn { display: none; margin-left: -8px; }
.chat-title {
  flex: 1; min-width: 0; margin: 0; font-size: 16px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.model-field select {
  min-height: 34px; max-width: 120px; padding: 0 8px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); font-size: 14px;
}

.banner { padding: 8px 16px; font-size: 14px; background: var(--warn-soft); color: var(--warn-text); }
.banner[data-tone="error"] { background: var(--danger-soft); color: var(--danger); }

.scroller { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.messages { max-width: 46rem; margin: 0 auto; padding: 20px 16px 8px; }

/* ---------- empty ---------- */
.empty { padding: 18vh 8px 0; text-align: center; }
.empty-title { margin: 0 0 6px; font-size: 20px; font-weight: 600; }
.empty-hint { margin: 0 auto; max-width: 26em; color: var(--text-2); font-size: 14px; }

/* ---------- messages ---------- */
.msg { margin: 0 0 20px; }
.msg-user { display: flex; justify-content: flex-end; }
.msg-user .msg-body {
  max-width: 85%; background: var(--surface-2); border-radius: 14px 14px 4px 14px; padding: 9px 14px;
}
.user-text { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-assistant .msg-body { overflow-wrap: anywhere; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md .code-block, .md .table-wrap { margin: 0 0 12px; }
.md ul, .md ol { padding-left: 1.4em; }
.md li + li { margin-top: 2px; }
.md h3, .md h4, .md h5, .md h6 { margin: 18px 0 8px; line-height: 1.35; }
.md h3 { font-size: 18px; }
.md h4 { font-size: 16px; }
.md h5, .md h6 { font-size: 15px; }
.md a { color: var(--accent); text-underline-offset: 2px; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.md blockquote { padding-left: 12px; border-left: 3px solid var(--border); color: var(--text-2); }
.md code { font-family: var(--mono); font-size: .9em; background: var(--code-bg); border-radius: 4px; padding: .1em .35em; }
.code-block { border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--code-bg); }
.code-bar {
  display: flex; align-items: center; justify-content: space-between; padding: 2px 4px 2px 12px;
  font-size: 12px; color: var(--text-2); border-bottom: 1px solid var(--border);
}
.code-copy { border: 0; background: transparent; cursor: pointer; font-size: 12px; color: var(--text-2); padding: 6px 8px; border-radius: 6px; }
.code-copy:hover { background: var(--surface-2); color: var(--text); }
.code-block pre { margin: 0; padding: 10px 12px; overflow-x: auto; }
.code-block code { background: none; padding: 0; font-size: 13px; line-height: 1.5; white-space: pre; }
.table-wrap { overflow-x: auto; }
.md table { border-collapse: collapse; font-size: 14px; min-width: 100%; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--surface-2); font-weight: 600; }

.tool {
  display: flex; align-items: baseline; gap: 8px; margin: 6px 0; padding: 6px 10px;
  border-radius: 8px; background: var(--surface); border: 1px solid var(--border); font-size: 13px;
}
.tool-label { flex-shrink: 0; font-weight: 600; }
.tool-summary { flex: 1; min-width: 0; color: var(--text-2); font-family: var(--mono); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-state { flex-shrink: 0; margin-left: auto; color: var(--text-2); font-size: 12px; }
.tool-error .tool-state { color: var(--danger); }
.tool-running .tool-state { color: var(--accent); }

.approval {
  margin: 10px 0; padding: 12px; border-radius: var(--radius); border: 1px solid var(--accent);
  background: var(--accent-soft);
}
.approval:not(.approval-pending) { border-color: var(--border); background: var(--surface); }
.approval-title { margin: 0 0 4px; font-weight: 600; }
.approval-summary { margin: 0 0 8px; color: var(--text-2); font-size: 14px; }
.approval-detail {
  margin: 0 0 10px; padding: 8px 10px; max-height: 240px; overflow: auto; border-radius: 8px;
  background: var(--code-bg); font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere;
}
.approval-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.approval-state { margin: 0; font-size: 13px; color: var(--text-2); }

.notice { margin: 8px 0; padding: 8px 10px; border-radius: 8px; background: var(--warn-soft); color: var(--warn-text); font-size: 14px; }

.msg-status { margin: 6px 0 0; font-size: 13px; color: var(--text-2); }
.status-error { color: var(--danger); }
.status-interrupted { color: var(--warn-text); }
.is-busy::before {
  content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 8px; border-radius: 50%;
  background: var(--accent); vertical-align: middle; animation: pulse 1.2s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: .25; } }
@media (prefers-reduced-motion: reduce) { .is-busy::before { animation: none; } }

/* ---------- composer ---------- */
.composer {
  padding: 8px 16px; padding-bottom: max(12px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--bg);
}
.composer-inner { display: flex; align-items: flex-end; gap: 8px; max-width: 46rem; margin: 0 auto; }
.composer textarea {
  flex: 1; min-width: 0; min-height: 44px; max-height: 40vh; resize: none; padding: 10px 12px;
  border-radius: 10px; border: 1px solid var(--border); background: var(--surface); line-height: 1.5;
}
.composer .btn { min-height: 44px; }
.composer-error { max-width: 46rem; margin: 6px auto 0; }

/* ---------- narrow screens ---------- */
@media (max-width: 767px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(86vw, 300px); z-index: 20;
    transform: translateX(-100%); transition: transform .2s ease; visibility: hidden;
  }
  .drawer-open .sidebar { transform: none; visibility: visible; }
  .scrim { position: fixed; inset: 0; z-index: 10; background: rgb(0 0 0 / .35); }
  .menu-btn { display: inline-grid; }
  .topbar { padding-left: 16px; padding-right: 12px; }
  .model-field select { max-width: 96px; }
  .messages { padding: 16px 16px 8px; }
  .msg-user .msg-body { max-width: 92%; }
}
@media (max-width: 767px) and (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
@media (max-width: 360px) {
  .composer { padding-left: 12px; padding-right: 12px; }
  .composer .btn { padding: 0 12px; }
  .topbar { gap: 4px; }
}
