:root {
  --bg: #0f1115; --panel: #161920; --panel2: #1c2029; --line: #272c37; --text: #e6e8ee; --muted: #8b93a5;
  --accent: #6ea8fe; --accent2: #2f5fb3; --ok: #4fd18b; --warn: #f2c14e; --bad: #ff6b6b; --user: #23304a;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 15px; color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); height: 100vh; overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel2); border-radius: 8px; padding: 7px 12px; }
button:hover { border-color: #3a4250; }
button.primary { background: var(--accent2); border-color: var(--accent2); color: #fff; }
button.primary:hover { background: #3a6fcc; }
button.danger { background: #5a2229; border-color: #7a2c35; }
button.ghost, label.ghost { background: transparent; }
button.icon, label.icon { padding: 6px 9px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
button.wide { width: 100%; margin-top: 8px; }
.muted { color: var(--muted); font-size: 13px; }
.err { color: var(--bad); min-height: 1em; font-size: 13px; }

#app { display: grid; grid-template-columns: 270px 1fr auto; height: 100vh; }
#side { background: var(--panel); border-right: 1px solid var(--line); padding: 14px 12px; display: flex; flex-direction: column; overflow-y: auto; }
.brand { font-size: 16px; padding: 4px 6px 14px; }
#groups { flex: 1; }
.group { margin-bottom: 14px; }
.groupHead { display: flex; align-items: center; justify-content: space-between; font-size: 11px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); padding: 4px 6px; cursor: pointer; user-select: none; }
.group.closed .agents { display: none; }
.agentItem { display: flex; align-items: center; gap: 9px; padding: 8px 8px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.agentItem:hover { background: var(--panel2); }
.agentItem.active { background: var(--panel2); border-color: var(--line); }
.agentItem .em { font-size: 19px; width: 24px; text-align: center; }
.agentItem .nm { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #3a4150; flex: none; }
.dot.run { background: var(--warn); box-shadow: 0 0 0 3px rgba(242,193,78,.18); animation: pulse 1.2s infinite; }
.dot.unread { background: var(--accent); }
@keyframes pulse { 50% { opacity: .45; } }

#main { display: flex; flex-direction: column; min-width: 0; height: 100vh; }
.empty { margin: auto; text-align: center; max-width: 420px; color: var(--muted); }
.empty .big { font-size: 54px; }
.empty h2 { color: var(--text); }
#agentView { display: flex; flex-direction: column; height: 100vh; min-height: 0; }
#agentView[hidden] { display: none; }
#agentHead { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 20px; border-bottom: 1px solid var(--line); }
#agentHead .title { display: flex; gap: 12px; align-items: center; min-width: 0; flex: 1; }
#agentHead .title > div { min-width: 0; }
#agentHead button { white-space: nowrap; }
#aEmoji { font-size: 30px; }
#agentHead h1 { font-size: 18px; margin: 0; }
#aDesc { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#tabs { display: flex; gap: 6px; padding: 8px 16px; border-bottom: 1px solid var(--line); overflow-x: auto; flex: none; }
.tab { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel); white-space: nowrap; cursor: pointer; max-width: 240px; font-size: 13px; }
.tab span.t { overflow: hidden; text-overflow: ellipsis; }
.tab.active { background: var(--panel2); border-color: var(--accent2); }
.tab .x { opacity: .45; padding: 0 2px; }
.tab .x:hover { opacity: 1; color: var(--bad); }
.tab.add { color: var(--accent); }

#chat { flex: 1; overflow-y: auto; padding: 20px max(20px, calc((100% - 860px) / 2)); min-height: 0; }
.msg { margin: 0 0 14px; line-height: 1.5; }
.msg.user { background: var(--user); padding: 10px 14px; border-radius: 12px; margin-left: 12%; white-space: pre-wrap; }
.msg.user.auto { background: transparent; border: 1px dashed var(--line); color: var(--muted); font-size: 13px; margin-left: 0; }
.msg.user .files { margin-top: 6px; font-size: 12px; color: var(--muted); }
.msg.text { padding: 2px 2px; }
.msg.text p { margin: .4em 0; }
.msg.text pre { background: #0b0d11; border: 1px solid var(--line); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.msg.text code { background: #0b0d11; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.msg.text pre code { padding: 0; background: none; }
.msg.text a { color: var(--accent); }
.msg.text table { border-collapse: collapse; font-size: 13px; }
.msg.text td, .msg.text th { border: 1px solid var(--line); padding: 4px 8px; }
.tools { margin: 0 0 12px; }
.tools summary { cursor: pointer; color: var(--muted); font-size: 13px; list-style: none; }
.tools summary::before { content: "▸ "; }
.tools[open] summary::before { content: "▾ "; }
.tool { font-size: 12.5px; color: var(--muted); padding: 3px 0 3px 12px; border-left: 2px solid var(--line); font-family: ui-monospace, Menlo, monospace; }
.tool b { color: #b8c2d6; font-weight: 600; }
.tool .res { display: block; opacity: .75; white-space: pre-wrap; max-height: 5.5em; overflow: hidden; }
.tool.bad b { color: var(--bad); }
.done { font-size: 12px; color: var(--muted); margin: -4px 0 18px; }
.done.bad { color: var(--bad); }
.notice { font-size: 13px; color: var(--warn); margin-bottom: 12px; }
.typing { color: var(--muted); font-size: 13px; margin: 6px 0 14px; }
.typing::after { content: "…"; animation: pulse 1s infinite; }

#composer { border-top: 1px solid var(--line); padding: 10px 16px 14px; background: var(--bg); }
#composer .row { display: flex; gap: 8px; align-items: flex-end; max-width: 900px; margin: 0 auto; }
#input { flex: 1; resize: none; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; max-height: 260px; }
#input:focus { outline: none; border-color: var(--accent2); }
#attached { max-width: 900px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: 12px; background: var(--panel2); border: 1px solid var(--line); border-radius: 20px; padding: 3px 10px; }
.chip.busy { opacity: .6; }

#drawer { width: 430px; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; height: 100vh; }
#drawer[hidden] { display: none; }
.drawerHead { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); }
#drawerBody { overflow-y: auto; padding: 12px 14px; flex: 1; }
#drawerBody h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 18px 0 8px; }
.card { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; margin-bottom: 8px; font-size: 13px; }
.card b { display: block; margin-bottom: 3px; }
.card.click { cursor: pointer; }
.card.click:hover { border-color: var(--accent2); }
.tag { display: inline-block; font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 10px; padding: 0 7px; margin-left: 4px; }
pre.doc { white-space: pre-wrap; font-size: 12.5px; background: #0b0d11; border: 1px solid var(--line); border-radius: 8px; padding: 10px; max-height: 60vh; overflow-y: auto; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px; width: min(620px, 94vw); padding: 22px 24px; }
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h2 { margin: 0 0 4px; }
dialog label { display: block; margin: 12px 0 0; font-size: 13px; color: var(--muted); }
dialog label input, dialog label textarea, dialog select, dialog #newCat { display: block; width: 100%; margin-top: 4px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; color: var(--text); }
dialog label.inline { display: flex; gap: 8px; align-items: center; color: var(--text); margin: 6px 0; }
dialog label.inline input { width: auto; display: inline; margin: 0; }
dialog fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 14px 0 0; padding: 8px 12px 12px; }
dialog legend { color: var(--muted); font-size: 13px; padding: 0 6px; }
.grid2 { display: grid; grid-template-columns: 1fr 90px; gap: 10px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

@media (max-width: 820px) {
  #app { grid-template-columns: 1fr; }
  #side { display: none; }
  #drawer { position: fixed; inset: 0; width: auto; z-index: 5; }
}
[hidden] { display: none !important; }
