:root {
  --bg: #ffffff;
  --sidebar: #f7f7f8;
  --text: #16171a;
  --muted: #6c6d78;
  --border: #dadbe1;
  --bubble: #f0f1f4;
  --accent: #0046a0;
  --accent-text: #ffffff;
  --hover: #ececf0;
  --code-bg: #f4f5f8;
  --claude: #d4673f;
  --grok: #1d1e22;
  --ok: #1f9d55;
  --warn: #d68a00;
  --off: #a4a5ae;
  --shadow: 0 6px 28px rgba(20, 22, 40, 0.10);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1a1b1e; --sidebar: #141518; --text: #ececf0; --muted: #9b9ca8; --border: #2b2c31;
    --bubble: #2a2b31; --accent: #4d8dff; --accent-text: #0a0b0d; --hover: #232429; --code-bg: #121316;
    --claude: #e0795a; --grok: #3a3b42; --shadow: 0 6px 28px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1a1b1e; --sidebar: #141518; --text: #ececf0; --muted: #9b9ca8; --border: #2b2c31;
  --bubble: #2a2b31; --accent: #4d8dff; --accent-text: #0a0b0d; --hover: #232429; --code-bg: #121316;
  --claude: #e0795a; --grok: #3a3b42; --shadow: 0 6px 28px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- layout */
.app { display: grid; grid-template-columns: 272px 1fr; height: 100dvh; }
#sidebar {
  background: var(--sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.side-top { padding: 14px 12px 8px; }
.brand { display: flex; align-items: center; gap: 10px; padding: 2px 6px 14px; font-weight: 650; letter-spacing: -0.01em; }
.brand img { width: 26px; height: 26px; border-radius: 7px; }
#newChat {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  background: transparent; border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; font-weight: 550;
}
#newChat:hover { background: var(--hover); }
#convList { flex: 1; overflow-y: auto; padding: 6px 8px; min-height: 0; }
.conv-label { font-size: 12px; color: var(--muted); padding: 12px 8px 4px; font-weight: 600; }
.conv {
  display: flex; align-items: center; gap: 4px; border-radius: 10px; padding: 0 4px 0 0; margin: 1px 0;
}
.conv > button.open {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 10px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 14.5px;
}
.conv:hover, .conv.active { background: var(--hover); }
.conv > button.open { display: flex; align-items: center; gap: 8px; }
.conv .t { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .owner { flex: none; font-size: 11px; font-weight: 600; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; padding: 1px 7px; }
.conv .del { opacity: 0; background: none; border: 0; color: var(--muted); padding: 6px 8px; border-radius: 8px; font-size: 16px; line-height: 1; }
.conv:hover .del, .conv .del:focus-visible { opacity: 1; }
.conv .del:hover { color: #d64545; }
.side-bottom { border-top: 1px solid var(--border); padding: 10px 12px; display: flex; align-items: center; gap: 10px; }
.me { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; font-size: 14.5px; font-weight: 550; }
.me span.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: var(--accent); color: var(--accent-text); font-weight: 650; font-size: 14px;
}
#logout { background: none; border: 1px solid var(--border); border-radius: 9px; padding: 6px 10px; font-size: 13px; color: var(--muted); }
#logout:hover { background: var(--hover); color: var(--text); }

main { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
header {
  display: flex; align-items: center; gap: 12px; padding: 10px 18px; min-height: 56px; border-bottom: 1px solid transparent;
}
#menuBtn { display: none; background: none; border: 0; font-size: 22px; padding: 4px 8px; border-radius: 8px; }
#title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#agentStatus { display: flex; gap: 8px; flex: none; }
.pill {
  display: inline-flex; align-items: center; gap: 7px; font-size: 13px; color: var(--muted);
  border: 1px solid var(--border); border-radius: 999px; padding: 4px 11px 4px 9px;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); }
.pill[data-s="ready"] .dot { background: var(--ok); }
.pill[data-s="working"] .dot { background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
.pill[data-s="needs help"] .dot, .pill[data-s="starting"] .dot { background: var(--warn); }

#scroll { flex: 1; overflow-y: auto; min-height: 0; scroll-behavior: smooth; }
.col { width: min(780px, 100% - 32px); margin: 0 auto; }
#thread { padding: 14px 0 28px; display: flex; flex-direction: column; gap: 22px; }

/* ---------- empty state */
#empty { display: none; padding: 9vh 0 0; text-align: center; }
#empty.show { display: block; }
#empty h1 { font-size: clamp(26px, 4vw, 34px); letter-spacing: -0.02em; margin: 0 0 8px; font-weight: 650; }
#empty p { color: var(--muted); margin: 0 0 28px; }
.chips { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; text-align: left; }
.chip {
  background: transparent; border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; text-align: left;
  display: flex; flex-direction: column; gap: 2px;
}
.chip:hover { background: var(--hover); }
.chip b { font-weight: 600; font-size: 15px; }
.chip span { color: var(--muted); font-size: 13.5px; }

/* ---------- messages */
.msg { display: flex; gap: 12px; align-items: flex-start; }
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--bubble); border-radius: 20px; padding: 10px 16px; max-width: min(85%, 600px); white-space: pre-wrap; overflow-wrap: anywhere; }
.ucol { display: flex; flex-direction: column; align-items: flex-end; max-width: min(85%, 600px); }
.ucol .bubble { max-width: 100%; }
.ucol .from { font-size: 12px; font-weight: 600; color: var(--muted); margin: 0 10px 3px; }
.msg.agent .who, .msg.internal .who { font-weight: 650; font-size: 14.5px; margin-bottom: 2px; display: flex; align-items: center; gap: 8px; }
.badge { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-size: 13px; font-weight: 700; color: #fff; flex: none; margin-top: 1px; }
.badge.Claude { background: var(--claude); }
.badge.Grok { background: var(--grok); }
.badge.other { background: var(--muted); }
.content { min-width: 0; flex: 1; overflow-wrap: anywhere; }
.msg.internal { opacity: 0.92; }
.msg.internal .content { border-left: 3px solid var(--border); padding-left: 12px; font-size: 15px; }
.msg.internal .who { color: var(--muted); font-weight: 600; font-size: 13.5px; }
.msg.system { justify-content: center; }
.msg.system .bubble { color: var(--muted); font-size: 14px; background: var(--hover); border-radius: 999px; padding: 6px 14px; }
.when { color: var(--muted); font-size: 12px; font-weight: 400; }

.md > :first-child { margin-top: 0; } .md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; } .md ul, .md ol { margin: 0 0 12px; padding-left: 22px; } .md li { margin: 3px 0; }
.md h1, .md h2, .md h3 { margin: 18px 0 8px; line-height: 1.3; letter-spacing: -0.01em; }
.md h1 { font-size: 1.35em; } .md h2 { font-size: 1.2em; } .md h3 { font-size: 1.07em; }
.md a { color: var(--accent); }
.md blockquote { margin: 0 0 12px; padding: 2px 14px; border-left: 3px solid var(--border); color: var(--muted); }
.md code { background: var(--code-bg); padding: 2px 6px; border-radius: 6px; font: 0.88em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.md pre { position: relative; background: var(--code-bg); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; overflow-x: auto; margin: 0 0 12px; }
.md pre code { background: none; padding: 0; font-size: 13.5px; }
.md table { border-collapse: collapse; margin: 0 0 12px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.md img { max-width: 100%; border-radius: 10px; }
.copy { position: absolute; top: 8px; right: 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 3px 9px; font-size: 12px; color: var(--muted); }
.copy:hover { color: var(--text); }

.typing { display: none; }
.typing.show { display: flex; }
.typing .dots { display: inline-flex; gap: 5px; padding: 11px 2px; }
.typing .dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: bounce 1.2s infinite ease-in-out; }
.typing .dots i:nth-child(2) { animation-delay: 0.15s; } .typing .dots i:nth-child(3) { animation-delay: 0.3s; }
.typing .label { color: var(--muted); font-size: 13.5px; padding-top: 6px; }

/* ---------- composer */
.composer-wrap { padding: 8px 0 18px; background: linear-gradient(to top, var(--bg) 70%, transparent); }
.composer {
  border: 1px solid var(--border); border-radius: 26px; background: var(--bg); box-shadow: var(--shadow); padding: 10px 12px 10px 16px;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); }
#files { display: flex; flex-wrap: wrap; gap: 8px; padding: 2px 0 8px; }
#files:empty { display: none; }
.file-chip { display: inline-flex; align-items: center; gap: 8px; background: var(--bubble); border-radius: 12px; padding: 5px 6px 5px 11px; font-size: 13.5px; max-width: 100%; }
.file-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.file-chip button { background: none; border: 0; color: var(--muted); font-size: 16px; line-height: 1; padding: 2px 6px; border-radius: 6px; }
#input {
  width: 100%; border: 0; outline: 0; background: transparent; color: inherit; resize: none; font: inherit; line-height: 1.5;
  max-height: 220px; min-height: 28px; padding: 4px 0;
}
#input::placeholder { color: var(--muted); }
.tools { display: flex; align-items: center; gap: 8px; padding-top: 4px; }
#attach { width: 36px; height: 36px; border-radius: 50%; border: 0; background: none; color: var(--muted); font-size: 20px; display: grid; place-items: center; }
#attach:hover { background: var(--hover); color: var(--text); }
.seg { display: inline-flex; background: var(--bubble); border-radius: 999px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: none; border-radius: 999px; padding: 5px 13px; font-size: 13.5px; color: var(--muted); font-weight: 550; }
.seg button[aria-pressed="true"] { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.14); }
#send {
  margin-left: auto; width: 38px; height: 38px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; font-size: 18px;
}
#send:disabled { background: var(--hover); color: var(--off); cursor: default; }
.hint { text-align: center; color: var(--muted); font-size: 12px; padding-top: 8px; }

/* ---------- mobile */
#veil { display: none; }
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  #sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 86vw); transform: translateX(-102%); transition: transform 0.22s ease; z-index: 30; }
  body.nav-open #sidebar { transform: none; box-shadow: var(--shadow); }
  body.nav-open #veil { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.35); z-index: 20; }
  #menuBtn { display: block; }
  header { padding: 8px 12px; }
  .chips { grid-template-columns: 1fr; }
  #agentStatus .pill span.txt { display: none; }
  #agentStatus .pill { padding: 6px 9px; }
  .hint { display: none; }
  .composer-wrap { padding-bottom: max(10px, env(safe-area-inset-bottom)); }
  .seg button { padding: 5px 10px; }
}
@keyframes pulse { 50% { opacity: 0.35; } }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: 0.45; } 30% { transform: translateY(-4px); opacity: 1; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; scroll-behavior: auto !important; transition: none !important; } }

/* ---------- login */
.login-body { display: grid; place-items: center; min-height: 100dvh; padding: 20px; }
.card { width: min(400px, 100%); background: var(--bg); border: 1px solid var(--border); border-radius: 22px; box-shadow: var(--shadow); padding: 30px 28px 26px; }
.card .logo { width: 44px; height: 44px; border-radius: 12px; display: block; margin: 0 0 18px; }
.card h1 { font-size: 24px; margin: 0 0 4px; letter-spacing: -0.02em; }
.card p.sub { color: var(--muted); margin: 0 0 22px; font-size: 15px; }
.card label { display: block; font-size: 13.5px; font-weight: 600; margin: 14px 0 6px; }
.card input {
  width: 100%; font: inherit; color: inherit; background: var(--bg); border: 1px solid var(--border); border-radius: 12px; padding: 11px 13px;
}
.card input:focus { outline: 0; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.card button.primary { width: 100%; margin-top: 20px; border: 0; background: var(--accent); color: var(--accent-text); border-radius: 12px; padding: 12px; font-weight: 650; }
.card button.primary:disabled { opacity: 0.6; cursor: default; }
.err { color: #d64545; font-size: 14px; min-height: 20px; margin-top: 12px; }
