:root {
  --bg: #f7f7f8; --panel: #ffffff; --panel-2: #f2f2f4; --border: #e4e4e8; --border-strong: #d0d0d7;
  --text: #18181b; --muted: #6b6b76; --faint: #9a9aa4;
  --accent: #4f46e5; --accent-weak: #eef0ff; --accent-text: #fff;
  --danger: #dc2626; --ok: #16a34a;
  --radius: 8px; --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.06);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111114; --panel: #18181c; --panel-2: #202026; --border: #2a2a31; --border-strong: #3a3a43;
    --text: #ececf0; --muted: #a0a0ab; --faint: #6e6e78;
    --accent: #7c74ff; --accent-weak: #25234a; --accent-text: #fff;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); font-size: 12.5px; }
h1 { font-size: 20px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 13px; margin: 0 0 8px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
p { margin: 0 0 8px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12px; }
.boot { padding: 40px; color: var(--muted); }

/* ---------- shell */
.shell { display: grid; grid-template-columns: 236px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; padding: 16px 16px 12px; font-weight: 700; font-size: 15px; }
.brand img { width: 22px; height: 22px; }
.nav { flex: 1; overflow: auto; padding: 4px 8px; }
.nav-section { padding: 14px 8px 6px; font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.nav a { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; color: var(--text); }
.nav a:hover { background: var(--panel-2); text-decoration: none; }
.nav a.active { background: var(--accent-weak); color: var(--accent); font-weight: 550; }
.nav a .icon { width: 18px; text-align: center; flex: none; }
.nav a .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav a .count { font-size: 11px; background: var(--accent); color: var(--accent-text); border-radius: 10px; padding: 0 6px; font-weight: 600; }
.sidebar-foot { border-top: 1px solid var(--border); padding: 10px 16px; display: flex; justify-content: space-between; align-items: center; font-size: 13px; }
main { overflow: auto; min-width: 0; }
.page { padding: 24px 28px 60px; max-width: 1400px; }
.page.narrow { max-width: 920px; }
.page-head { display: flex; align-items: flex-start; gap: 16px; margin-bottom: 18px; }
.page-head .title { flex: 1; min-width: 0; }
.page-head .title p { margin: 4px 0 0; color: var(--muted); }
.row-flex { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }

/* ---------- controls */
button, .btn { font: inherit; font-weight: 550; border: 1px solid var(--border-strong); background: var(--panel); color: var(--text); padding: 6px 12px; border-radius: 6px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
button:hover, .btn:hover { background: var(--panel-2); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--danger); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--panel-2); }
button.sm { padding: 3px 8px; font-size: 12px; }
button:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--border-strong); border-radius: 6px; padding: 6px 9px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
input[type=checkbox], input[type=radio] { width: auto; accent-color: var(--accent); }
textarea { resize: vertical; min-height: 64px; }
textarea.code, input.code { font-family: var(--mono); font-size: 12.5px; }
label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
label.field > small { display: block; color: var(--faint); font-size: 12px; margin-top: 3px; }
label.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 14px; }

.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.card-head h2 { margin: 0; flex: 1; }

/* ---------- pills & chips */
.pill { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 12px; font-weight: 550; white-space: nowrap; background: var(--panel-2); color: var(--muted); border: 1px solid var(--border); }
.pill-gray { background: #f1f1f3; color: #52525b; border-color: #e1e1e5; }
.pill-red { background: #fef2f2; color: #b91c1c; border-color: #fecaca; }
.pill-orange { background: #fff7ed; color: #c2410c; border-color: #fed7aa; }
.pill-amber { background: #fffbeb; color: #a16207; border-color: #fde68a; }
.pill-green { background: #f0fdf4; color: #15803d; border-color: #bbf7d0; }
.pill-teal { background: #f0fdfa; color: #0f766e; border-color: #99f6e4; }
.pill-blue { background: #eff6ff; color: #1d4ed8; border-color: #bfdbfe; }
.pill-purple { background: #faf5ff; color: #7e22ce; border-color: #e9d5ff; }
.pill-pink { background: #fdf2f8; color: #be185d; border-color: #fbcfe8; }
@media (prefers-color-scheme: dark) {
  .pill-gray { background: #27272a; color: #c4c4cc; border-color: #3f3f46; }
  .pill-red { background: #3b1414; color: #fca5a5; border-color: #5f1d1d; }
  .pill-orange { background: #3a1f0d; color: #fdba74; border-color: #5c3214; }
  .pill-amber { background: #3a2c0a; color: #fcd34d; border-color: #5c4510; }
  .pill-green { background: #0f2e1a; color: #86efac; border-color: #1a4a2b; }
  .pill-teal { background: #0c2e2a; color: #5eead4; border-color: #134e48; }
  .pill-blue { background: #102246; color: #93c5fd; border-color: #1e3a6e; }
  .pill-purple { background: #2a1440; color: #d8b4fe; border-color: #44216a; }
  .pill-pink { background: #3a1028; color: #f9a8d4; border-color: #5c1a40; }
}
.chip { display: inline-block; font-family: var(--mono); font-size: 11.5px; padding: 0 6px; border-radius: 4px; background: var(--panel-2); border: 1px solid var(--border); margin: 1px 3px 1px 0; white-space: nowrap; }
.badge { font-size: 11px; border-radius: 9px; padding: 0 6px; background: var(--panel-2); color: var(--muted); font-weight: 600; }
.badge.hot { background: var(--accent); color: var(--accent-text); }
.badge.warn { background: #f59e0b; color: #fff; }

/* ---------- tabs */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 14px; overflow-x: auto; }
.tabs a { padding: 8px 12px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; font-weight: 550; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }

/* ---------- table */
.toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.toolbar input[type=search] { max-width: 320px; }
.table-wrap { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; background: var(--panel-2); border-bottom: 1px solid var(--border); padding: 7px 10px; white-space: nowrap; position: sticky; top: 0; cursor: pointer; user-select: none; }
table.data th.sorted { color: var(--text); }
table.data td { padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; max-width: 340px; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr { cursor: pointer; }
table.data tbody tr:hover td { background: var(--panel-2); }
table.data tbody tr.flash td { animation: flash 1.2s ease-out; }
@keyframes flash { from { background: var(--accent-weak); } to { background: transparent; } }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td .clip { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
td.mono-cell { font-family: var(--mono); font-size: 12px; overflow-wrap: anywhere; }
.empty { padding: 40px; text-align: center; color: var(--muted); }
.empty h2 { color: var(--text); }
.load-more { text-align: center; padding: 10px; }

/* ---------- dashboard cards */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.dcard { display: block; background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; color: var(--text); transition: border-color .15s, box-shadow .15s; }
.dcard:hover { border-color: var(--accent); box-shadow: var(--shadow); text-decoration: none; }
.dcard .top { display: flex; gap: 10px; align-items: center; margin-bottom: 6px; }
.dcard .icon { font-size: 20px; }
.dcard .nm { font-weight: 650; font-size: 15px; flex: 1; }
.dcard p { color: var(--muted); min-height: 20px; }
.qline { display: flex; align-items: center; gap: 8px; font-size: 12.5px; padding: 3px 0; }
.qline .ql { flex: 1; color: var(--muted); }
.dcard.new { display: flex; align-items: center; justify-content: center; border-style: dashed; color: var(--muted); min-height: 140px; font-weight: 550; }

/* template picker */
.tpls { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; margin-bottom: 16px; }
.tpl { border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 12px; cursor: pointer; background: var(--panel); }
.tpl:hover { border-color: var(--accent); }
.tpl.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-weak); }
.tpl .t { font-weight: 650; margin-bottom: 3px; }
.tpl .d { color: var(--muted); font-size: 12.5px; }

/* ---------- drawer & modal */
.overlay { position: fixed; inset: 0; background: rgba(10,10,20,.35); z-index: 20; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(620px, 100vw); background: var(--panel); border-left: 1px solid var(--border); z-index: 21; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.drawer-head { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.drawer-head .t { flex: 1; min-width: 0; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.drawer-body { flex: 1; overflow: auto; padding: 16px 18px; }
.drawer-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; }
.modal { position: fixed; z-index: 31; top: 6vh; left: 50%; transform: translateX(-50%); width: min(760px, 94vw); max-height: 88vh; display: flex; flex-direction: column; background: var(--panel); border-radius: 12px; box-shadow: var(--shadow); border: 1px solid var(--border); }
.modal-overlay { z-index: 30; }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; }
.modal-head h2 { margin: 0; flex: 1; }
.modal-body { padding: 18px 20px; overflow: auto; }
.modal-foot { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }
.banner { padding: 10px 12px; border-radius: 6px; margin-bottom: 14px; font-size: 13px; border: 1px solid var(--border); background: var(--panel-2); }
.banner.info { background: var(--accent-weak); border-color: transparent; }
.banner.warn { background: #fffbeb; border-color: #fde68a; color: #92400e; }
@media (prefers-color-scheme: dark) { .banner.warn { background: #3a2c0a; border-color: #5c4510; color: #fcd34d; } }
.group-box { border: 1px solid var(--border); border-radius: 6px; padding: 12px 12px 0; margin-bottom: 12px; }
.group-box > .gl { font-size: 12px; font-weight: 650; margin-bottom: 8px; }
.links a { display: block; font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* activity */
.feed { list-style: none; margin: 0; padding: 0; }
.feed li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.feed li:last-child { border-bottom: 0; }
.feed .who { font-weight: 600; }
.feed .when { color: var(--faint); font-size: 12px; white-space: nowrap; }
.feed .what { flex: 1; min-width: 0; }
.feed .note { color: var(--muted); margin-top: 2px; white-space: pre-wrap; }
.feed .av { width: 22px; height: 22px; border-radius: 50%; flex: none; display: grid; place-items: center; font-size: 11px; font-weight: 700; color: #fff; background: #71717a; }
.feed .av.agent { background: var(--accent); }
.feed.clickable li { cursor: pointer; }
.feed.clickable li:hover { background: var(--panel-2); }

/* ---------- schema editor */
.fe { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 6px; background: var(--panel); }
.fe-row { display: grid; grid-template-columns: 1.1fr 1.2fr 130px auto; gap: 6px; padding: 6px; align-items: center; }
.fe-row input, .fe-row select { padding: 4px 7px; }
.fe-actions { display: flex; gap: 2px; }
.fe-more { padding: 4px 10px 10px; border-top: 1px dashed var(--border); }
.fe-children { margin: 0 6px 6px 22px; }
.opt-row { display: grid; grid-template-columns: 1fr 110px auto; gap: 6px; margin-bottom: 4px; }
.cond-row { display: grid; grid-template-columns: 1.4fr 130px 1.2fr auto; gap: 6px; margin-bottom: 6px; }
.queue-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }

/* ---------- snippets */
.snip { position: relative; }
.snip pre { padding-right: 96px !important; overflow-wrap: anywhere; word-break: normal; background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px 12px; margin: 0 0 12px; overflow: auto; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; max-height: 260px; }
.snip button { position: absolute; top: 6px; right: 6px; }
.token { font-family: var(--mono); font-size: 13px; padding: 10px 12px; background: var(--panel-2); border: 1px dashed var(--accent); border-radius: 6px; word-break: break-all; }
.steps { counter-reset: step; }
.step { position: relative; padding-left: 34px; margin-bottom: 18px; }
.step::before { counter-increment: step; content: counter(step); position: absolute; left: 0; top: 0; width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-size: 12px; font-weight: 700; }
.roles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.role { border: 1px solid var(--border-strong); border-radius: var(--radius); padding: 10px; cursor: pointer; }
.role.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-weak); }
.role .t { font-weight: 650; }
.role .d { font-size: 12px; color: var(--muted); }
.instructions { white-space: pre-wrap; background: var(--panel-2); border-radius: 6px; padding: 10px 12px; font-size: 13px; margin-bottom: 12px; border: 1px solid var(--border); }
details > summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-bottom: 8px; }

/* ---------- auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: min(400px, 100%); padding: 28px; }
.auth h1 { margin-bottom: 4px; }
.auth p { color: var(--muted); margin-bottom: 18px; }

#toasts { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 50; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast { background: #18181b; color: #fff; padding: 9px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 13px; max-width: 560px; }
.toast.error { background: var(--danger); }

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .grid2, .grid3, .roles { grid-template-columns: 1fr; }
  .page { padding: 16px; }
}

/* column sizing */
table.data td.mono-cell { min-width: 260px; }
table.data td.text-cell { min-width: 170px; }
table.data td.long-cell { min-width: 220px; }
