:root {
  color-scheme: dark;
  --bg: #0d0f14;
  --bg-soft: #12151d;
  --panel: #161a24;
  --panel-2: #1b2130;
  --line: #262d3d;
  --text: #e6e9f0;
  --text-dim: #98a2b8;
  --text-faint: #6b768e;
  --accent: #6d5efc;
  --accent-soft: rgba(109, 94, 252, 0.16);
  --ok: #3ecf8e;
  --warn: #f0b429;
  --err: #ff6b6b;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", "JetBrains Mono", Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.65;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: grid;
  grid-template-columns: 264px 1fr;
  height: 100vh;
  overflow: hidden;
}

/* ---------- sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px 12px;
  background: var(--bg-soft);
  border-right: 1px solid var(--line);
  min-height: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
}
.brand-dot {
  width: 26px;
  height: 26px;
  flex: none;
  border-radius: 50%;
  border: 2.5px solid var(--accent);
  background: radial-gradient(circle at 72% 72%, var(--accent) 0 34%, transparent 35%);
}
.brand strong { display: block; font-size: 14px; letter-spacing: 0.2px; }
.brand small { display: block; color: var(--text-faint); font-size: 11px; }

button, input, select, textarea { font: inherit; color: inherit; }

.primary-block {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font-weight: 600;
  transition: filter 0.15s;
}
.primary-block:hover { filter: brightness(1.1); }

.convo-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin: 0 -4px;
  padding: 0 4px;
  min-height: 0;
}

.convo {
  position: relative;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 9px;
  cursor: pointer;
  border: 1px solid transparent;
}
.convo:hover { background: var(--panel); }
.convo.active { background: var(--accent-soft); border-color: rgba(109, 94, 252, 0.4); }
.convo .t {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.convo .p {
  grid-column: 1 / -1;
  color: var(--text-faint);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.convo .del {
  border: 0;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  padding: 0 4px;
  border-radius: 5px;
  line-height: 1;
  visibility: hidden;
}
.convo:hover .del { visibility: visible; }
.convo .del:hover { color: var(--err); background: rgba(255, 107, 107, 0.12); }

.sidebar-foot { border-top: 1px solid var(--line); padding-top: 10px; }
.health {
  font-size: 11px;
  color: var(--text-faint);
  font-family: var(--mono);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.health i { font-style: normal; display: flex; align-items: center; gap: 5px; }
.health i::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--text-faint); }
.health i.ok::before { background: var(--ok); }
.health i.warn::before { background: var(--warn); }
.health i.err::before { background: var(--err); }

/* ---------- main ---------- */
.main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--bg-soft);
}
.topbar-title { min-width: 0; flex: 1; }
.title-input {
  width: 100%;
  max-width: 460px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 2px 6px;
  margin-left: -6px;
  font-size: 15px;
  font-weight: 600;
}
.title-input:hover { border-color: var(--line); }
.title-input:focus { outline: none; border-color: var(--accent); background: var(--panel); }
.meta-line { display: flex; align-items: center; gap: 8px; margin-left: 0; }

.badge {
  font-size: 10.5px;
  font-family: var(--mono);
  padding: 1px 7px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--text-faint);
  background: var(--panel);
}
.badge.live { color: var(--ok); border-color: rgba(62, 207, 142, 0.35); }

.topbar-controls { display: flex; align-items: center; gap: 12px; flex: none; }
.ctl { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--text-faint); }
.ctl select {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 4px 7px;
  font-size: 12px;
  color: var(--text);
  cursor: pointer;
  max-width: 150px;
}
.ctl.checkbox { cursor: pointer; user-select: none; }
.ctl.checkbox input { accent-color: var(--accent); cursor: pointer; }

/* ---------- thread ---------- */
.thread {
  overflow-y: auto;
  padding: 24px 0 8px;
  scroll-behavior: smooth;
}
.thread-inner { max-width: 860px; margin: 0 auto; padding: 0 24px; display: flex; flex-direction: column; gap: 22px; }

.empty { max-width: 620px; margin: 12vh auto 0; padding: 0 24px; text-align: center; color: var(--text-dim); }
.empty h2 { color: var(--text); font-size: 20px; margin: 0 0 8px; }
.empty code { font-family: var(--mono); font-size: 12px; background: var(--panel); padding: 1px 5px; border-radius: 5px; color: var(--text); }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 22px; }
.suggestions button {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 12.5px;
  color: var(--text-dim);
  cursor: pointer;
}
.suggestions button:hover { border-color: var(--accent); color: var(--text); background: var(--accent-soft); }

.msg { display: flex; gap: 12px; }
.msg.user { flex-direction: row-reverse; }
.avatar {
  flex: none;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 700;
  margin-top: 2px;
  background: var(--panel-2);
  color: var(--text-dim);
  border: 1px solid var(--line);
}
.msg.user .avatar { background: var(--accent); color: #fff; border-color: transparent; }
.bubble { min-width: 0; flex: 1; }
.msg.user .bubble { display: flex; justify-content: flex-end; }
.msg.user .bubble > .content {
  background: var(--accent-soft);
  border: 1px solid rgba(109, 94, 252, 0.3);
  border-radius: var(--radius);
  padding: 10px 14px;
  max-width: 82%;
  white-space: pre-wrap;
  word-break: break-word;
}

.blocks { display: flex; flex-direction: column; gap: 10px; }
.content { min-width: 0; }
.prose { word-break: break-word; }
.prose > :first-child { margin-top: 0; }
.prose > :last-child { margin-bottom: 0; }
.prose p { margin: 0 0 10px; }
.prose h1, .prose h2, .prose h3, .prose h4 { margin: 18px 0 8px; line-height: 1.35; }
.prose h1 { font-size: 19px; }
.prose h2 { font-size: 17px; }
.prose h3 { font-size: 15px; }
.prose ul, .prose ol { margin: 0 0 10px; padding-left: 22px; }
.prose li { margin: 3px 0; }
.prose li > p { margin: 0 0 4px; }
.prose li > ul, .prose li > ol { margin: 4px 0 0; }
.prose blockquote {
  margin: 0 0 10px;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--line);
  color: var(--text-dim);
}
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.prose a { color: #8f9bff; text-decoration: none; }
.prose a:hover { text-decoration: underline; }
.prose code.inline {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 1px 5px;
}
.prose table { border-collapse: collapse; margin: 0 0 12px; font-size: 13px; display: block; overflow-x: auto; }
.prose th, .prose td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.prose th { background: var(--panel); font-weight: 600; }
.prose img { max-width: 100%; border-radius: 8px; }

.code-block {
  position: relative;
  background: #0a0c11;
  border: 1px solid var(--line);
  border-radius: 10px;
  margin: 0 0 12px;
  overflow: hidden;
}
.code-block pre { margin: 0; padding: 30px 14px 12px; overflow-x: auto; }
.code-block code { font-family: var(--mono); font-size: 12.5px; line-height: 1.6; color: #d8deee; }
.code-lang {
  position: absolute;
  top: 7px;
  left: 12px;
  font-family: var(--mono);
  font-size: 10.5px;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.copy-code {
  position: absolute;
  top: 5px;
  right: 8px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 11px;
  padding: 2px 8px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
.code-block:hover .copy-code { opacity: 1; }
.copy-code:hover { color: var(--text); border-color: var(--accent); }

/* tool cards */
.tool {
  border: 1px solid var(--line);
  border-left: 2px solid var(--panel-2);
  border-radius: 9px;
  background: var(--bg-soft);
  overflow: hidden;
  font-size: 12.5px;
}
.tool.err { border-left-color: var(--err); }
.tool.done { border-left-color: var(--ok); }
.tool summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}
.tool summary::-webkit-details-marker { display: none; }
.tool summary::before { content: "▸"; color: var(--text-faint); font-size: 10px; }
.tool[open] summary::before { content: "▾"; }
.tool .tool-name { font-family: var(--mono); font-weight: 600; color: var(--text); }
.tool .tool-hint { color: var(--text-faint); font-family: var(--mono); font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.tool .tool-state { font-size: 10.5px; color: var(--text-faint); flex: none; }
.tool .tool-state.pending { color: var(--warn); }
.tool .tool-state.err { color: var(--err); }
.tool .tool-state.ok { color: var(--ok); }
.tool .io { border-top: 1px solid var(--line); padding: 8px 12px; }
.tool .io h2 { display: none; }
.tool .io-label {
  font-family: var(--mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-faint);
  margin-bottom: 4px;
}
.tool pre {
  margin: 0 0 8px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 300px;
  overflow: auto;
  color: var(--text-dim);
}

.thinking {
  border: 1px dashed var(--line);
  border-radius: 9px;
  padding: 6px 10px;
  color: var(--text-faint);
  font-size: 12.5px;
  background: rgba(255, 255, 255, 0.015);
}
.thinking summary { cursor: pointer; list-style: none; user-select: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary::before { content: "◦ "; }
.thinking pre { margin: 6px 0 0; white-space: pre-wrap; font-family: var(--sans); line-height: 1.6; }

.notice {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 11px;
  font-size: 12.5px;
  color: var(--text-dim);
  background: var(--panel);
}
.error-box {
  border: 1px solid rgba(255, 107, 107, 0.4);
  background: rgba(255, 107, 107, 0.08);
  color: #ffb3b3;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12.5px;
  white-space: pre-wrap;
  word-break: break-word;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  padding-top: 2px;
}
.stats span b { color: var(--text-dim); font-weight: 600; }

.cursor {
  display: inline-block;
  width: 7px;
  height: 14px;
  background: var(--accent);
  vertical-align: -2px;
  animation: blink 1s steps(2, start) infinite;
  margin-left: 2px;
}
@keyframes blink { to { visibility: hidden; } }

/* ---------- composer ---------- */
.composer { border-top: 1px solid var(--line); background: var(--bg-soft); padding: 12px 24px 14px; }
.composer-inner {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px 8px 8px 14px;
  transition: border-color 0.15s;
}
.composer-inner:focus-within { border-color: var(--accent); }
#input {
  flex: 1;
  border: 0;
  background: transparent;
  resize: none;
  max-height: 240px;
  min-height: 24px;
  padding: 4px 0;
  outline: none;
  line-height: 1.6;
}
#input::placeholder { color: var(--text-faint); }
.composer-actions { display: flex; gap: 8px; flex: none; }
.send {
  background: var(--accent);
  border: 0;
  border-radius: 9px;
  color: #fff;
  padding: 6px 16px;
  cursor: pointer;
  font-weight: 600;
}
.send:disabled { opacity: 0.45; cursor: not-allowed; }
.send:not(:disabled):hover { filter: brightness(1.1); }
.ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 9px;
  color: var(--text-dim);
  padding: 6px 12px;
  cursor: pointer;
}
.ghost:not(:disabled):hover { border-color: var(--err); color: var(--err); }
.ghost:disabled { opacity: 0.35; cursor: not-allowed; }

/* ---------- voice ---------- */
.mic {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text-dim);
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}
.mic:not(:disabled):hover { color: var(--text); border-color: var(--accent); }
.mic:disabled { opacity: 0.4; cursor: not-allowed; }
.mic[data-phase="listening"] {
  color: #fff;
  background: var(--accent);
  border-color: transparent;
  animation: micPulse 1.9s ease-out infinite;
}
.mic[data-phase="speaking"] { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.mic[data-phase="sending"], .mic[data-phase="idle"] { color: var(--warn); border-color: rgba(240, 180, 41, 0.45); }
.mic[data-phase="blocked"] { color: var(--err); border-color: var(--err); }
@keyframes micPulse {
  0% { box-shadow: 0 0 0 0 rgba(109, 94, 252, 0.5); }
  70% { box-shadow: 0 0 0 10px rgba(109, 94, 252, 0); }
  100% { box-shadow: 0 0 0 0 rgba(109, 94, 252, 0); }
}

.voice-bar { max-width: 860px; margin: 8px auto 0; display: flex; flex-direction: column; gap: 6px; }
.voice-line { display: flex; align-items: baseline; gap: 10px; min-height: 15px; }
.voice-status { flex: none; font-size: 11px; color: var(--text-faint); font-family: var(--mono); }
.voice-caption {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.voice-ctl-row { display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap; }
.voice-ctl-row[hidden] { display: none; }
.ctl.mini select { max-width: 190px; }
.ctl.mini input[type="range"] { width: 108px; accent-color: var(--accent); cursor: pointer; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 6px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-thumb:hover { background: #35405a; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 880px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 20; width: 250px; transform: translateX(-100%); transition: transform 0.2s; }
  .sidebar.open { transform: none; }
}
