:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #f0f2f5; --border: #dde1e7;
  --text: #1c2430; --muted: #66707e; --accent: #1f6feb; --accent-ink: #ffffff;
  --ok: #1a7f37; --warn: #9a6700; --danger: #cf222e; --user-bubble: #e8f0fe;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1318; --surface: #171c23; --surface-2: #1f262f; --border: #2c3540;
    --text: #e6eaef; --muted: #98a2b0; --accent: #4c8dff; --accent-ink: #0b1220;
    --ok: #3fb950; --warn: #d29922; --danger: #f85149; --user-bubble: #1c2a44;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1em; }
code { font-family: var(--mono); font-size: 12.5px; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }
h2 { font-size: 18px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 10px; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { font-family: var(--mono); font-size: 13px; resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; font-size: 13px; }
label.check { flex-direction: row; align-items: center; gap: 6px; font-weight: 400; }
label.check input { width: auto; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 7px 14px; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 500; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.spacer { flex: 1; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 14px; display: flex; flex-direction: column; gap: 12px; }

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 28px; display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow); }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.brand.big { font-size: 20px; }
.logo { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; }

/* shell */
.app { display: grid; grid-template-columns: 190px 1fr; grid-template-rows: 54px 1fr; height: 100vh; }
.topbar { grid-column: 1 / 3; display: flex; align-items: center; gap: 12px; padding: 0 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.pill.ok { color: var(--ok); }
.pill.bad { color: var(--danger); }
.presence { display: flex; gap: 6px; }
.avatar { width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 700; color: #fff; position: relative; }
.avatar::after { content: ""; position: absolute; right: -1px; bottom: -1px; width: 9px; height: 9px; border-radius: 50%; background: #2da44e; border: 2px solid var(--surface); }
.usermenu { position: relative; }
.dropdown { position: absolute; right: 0; top: 40px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 150px; z-index: 10; }
.dropdown button { background: none; border: 0; text-align: left; padding: 9px 14px; cursor: pointer; color: var(--text); font: inherit; }
.dropdown button:hover { background: var(--surface-2); }
.sidenav { background: var(--surface); border-right: 1px solid var(--border); padding: 12px 8px; display: flex; flex-direction: column; gap: 2px; }
.sidenav a { color: var(--text); text-decoration: none; padding: 8px 12px; border-radius: 8px; display: flex; align-items: center; justify-content: space-between; }
.sidenav a:hover { background: var(--surface-2); }
.sidenav a.active { background: var(--user-bubble); color: var(--accent); font-weight: 600; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; font-weight: 700; }
.content { overflow: auto; padding: 18px 20px; min-width: 0; }
.view-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.tabs { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.tabs button { background: var(--surface); border: 0; padding: 6px 12px; cursor: pointer; color: var(--muted); font: inherit; }
.tabs button.active { background: var(--surface-2); color: var(--text); font-weight: 600; }

/* chat */
.view#view-chat { height: 100%; }
.chat-layout { display: grid; grid-template-columns: 250px 1fr; gap: 14px; height: calc(100vh - 54px - 36px); }
.conv-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; overflow: auto; }
.conv-list ul { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-direction: column; gap: 2px; }
.conv-list li { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.conv-list li:hover { background: var(--surface-2); }
.conv-list li.active { background: var(--user-bubble); }
.conv-list .t { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-list .m { font-size: 11.5px; color: var(--muted); }
.running-dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--warn); margin-right: 5px; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.chat-pane { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--border); display: flex; gap: 10px; align-items: baseline; }
.chat-log { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 860px; }
.msg.user { align-self: flex-end; background: var(--user-bubble); padding: 10px 14px; border-radius: 12px 12px 2px 12px; white-space: pre-wrap; }
.msg.user .author { font-size: 11.5px; font-weight: 700; color: var(--accent); display: block; margin-bottom: 2px; }
.msg.assistant { align-self: flex-start; }
.msg.tool { align-self: flex-start; font-size: 12.5px; color: var(--muted); border: 1px dashed var(--border); border-radius: 8px; padding: 4px 10px; max-width: 100%; }
.msg.tool summary { cursor: pointer; }
.msg.tool pre { white-space: pre-wrap; word-break: break-word; max-height: 320px; overflow: auto; font-family: var(--mono); font-size: 12px; }
.msg.tool.err { border-color: var(--danger); color: var(--danger); }
.msg.notice { align-self: center; font-size: 12.5px; color: var(--warn); }
.msg.file { align-self: flex-end; font-size: 12px; color: var(--muted); }
.composer { border-top: 1px solid var(--border); padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.composer textarea { font-family: inherit; font-size: 14px; }
.composer-actions { display: flex; align-items: center; gap: 10px; }
.composer-actions .muted { flex: 1; }
.attach-list { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--surface-2); border-radius: 999px; padding: 2px 10px; font-size: 12px; display: inline-flex; gap: 6px; align-items: center; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--muted); }
.typing { color: var(--muted); font-size: 12.5px; }

/* markdown */
.markdown { line-height: 1.6; overflow-wrap: anywhere; }
.markdown p { margin: 0 0 8px; }
.markdown pre { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow: auto; }
.markdown table { border-collapse: collapse; margin: 8px 0; display: block; overflow-x: auto; }
.markdown th, .markdown td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.markdown h1, .markdown h2, .markdown h3 { margin: 12px 0 6px; }

/* operations */
.op { border-left: 4px solid var(--border); }
.op.pending { border-left-color: var(--warn); }
.op.done { border-left-color: var(--ok); }
.op.failed { border-left-color: var(--danger); }
.op.rejected { opacity: .7; }
.op.danger-op { border-left-color: var(--danger); }
.op-head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.op-head .id { font-weight: 700; }
.status { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); }
.status.pending { color: var(--warn); }
.status.done { color: var(--ok); }
.status.failed { color: var(--danger); }
.danger-box { color: var(--danger); font-weight: 600; }
details pre, pre.json { background: var(--surface-2); padding: 10px; border-radius: 8px; overflow: auto; max-height: 420px; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-word; }
pre.json { max-height: 70vh; }

/* docs */
.docs-layout { display: grid; grid-template-columns: 300px 1fr; gap: 14px; }
.doc-list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; max-height: calc(100vh - 100px); overflow: auto; }
.doc-list ul { list-style: none; padding: 0; margin: 8px 0 0; }
.doc-list li { padding: 5px 8px; border-radius: 6px; cursor: pointer; font-size: 12.5px; font-family: var(--mono); word-break: break-all; }
.doc-list li:hover, .doc-list li.active { background: var(--surface-2); }
.doc-pane { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-width: 0; }
.doc-editor { min-height: 60vh; }

/* activity / tables */
.activity { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.activity li { padding: 8px 14px; border-bottom: 1px solid var(--border); display: flex; gap: 12px; font-size: 13px; }
.activity li:last-child { border-bottom: 0; }
.activity .when { color: var(--muted); min-width: 130px; font-variant-numeric: tabular-nums; }
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); font-size: 13px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; font-size: 13px; }
.kv .k { color: var(--muted); }
.notice { background: var(--surface-2); padding: 10px; border-radius: 8px; font-family: var(--mono); }

.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px; max-width: 360px; box-shadow: var(--shadow); font-size: 13px; }

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 54px auto 1fr; }
  .topbar { grid-column: 1; }
  .sidenav { flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .chat-layout, .docs-layout { grid-template-columns: 1fr; height: auto; }
  .chat-pane { height: 70vh; }
  .grid2 { grid-template-columns: 1fr; }
  .content { padding: 12px 16px; }
}
