:root {
  --brand: #0b8a72; --brand-2: #0b6f86; --brand-ink: #ffffff;
  --bg: #f0f2f5; --panel: #ffffff; --panel-2: #f6f7f9; --hover: #f2f5f8; --line: #e6e8eb; --line-2: #eef0f2;
  --text: #1c2730; --text-2: #3b4752; --muted: #7a8691;
  --accent: #1677ff; --accent-soft: #e6f0ff; --accent-ink: #ffffff; --select: #d9ebff;
  --chat-bg: #e9e5df; --bubble-in: #ffffff; --bubble-out: #dcf6c8; --bubble-out-ink: #1c2730;
  --danger: #d93025; --ok: #1a9b4b; --warn: #c27c0e; --shadow: 0 1px 2px rgba(16,24,32,.08); --shadow-lg: 0 8px 28px rgba(16,24,32,.18);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #0d6e5d; --brand-2: #0b5568;
    --bg: #0f1418; --panel: #172026; --panel-2: #1c262d; --hover: #1f2b33; --line: #26323a; --line-2: #212c33;
    --text: #e6ecef; --text-2: #c3cdd3; --muted: #8b98a2;
    --accent: #4c95ff; --accent-soft: #1b2c45; --select: #1d3550;
    --chat-bg: #11181d; --bubble-in: #1f2a31; --bubble-out: #20402b; --bubble-out-ink: #e6ecef;
    --danger: #ff6b61; --ok: #43c47a; --warn: #f0a83a; --shadow: none; --shadow-lg: 0 8px 28px rgba(0,0,0,.5);
  }
}
:root[data-theme="dark"] {
  --brand: #0d6e5d; --brand-2: #0b5568;
  --bg: #0f1418; --panel: #172026; --panel-2: #1c262d; --hover: #1f2b33; --line: #26323a; --line-2: #212c33;
  --text: #e6ecef; --text-2: #c3cdd3; --muted: #8b98a2;
  --accent: #4c95ff; --accent-soft: #1b2c45; --select: #1d3550;
  --chat-bg: #11181d; --bubble-in: #1f2a31; --bubble-out: #20402b; --bubble-out-ink: #e6ecef;
  --danger: #ff6b61; --ok: #43c47a; --warn: #f0a83a; --shadow: none; --shadow-lg: 0 8px 28px rgba(0,0,0,.5);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 "Segoe UI", Roboto, system-ui, -apple-system, sans-serif; -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }
.grow { flex: 1; }
button, input, select, textarea { font: inherit; color: inherit; }
svg.i { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ico svg.i { width: 18px; height: 18px; }

.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 6px 12px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; justify-content: center; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.small { padding: 3px 9px; font-size: 12px; border-radius: 6px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; cursor: default; }
.icon-btn { width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent; color: var(--text-2); display: inline-grid; place-items: center; cursor: pointer; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.on { color: var(--accent); background: var(--accent-soft); }
.icon-btn.on-dark { color: var(--brand-ink); }
.icon-btn.on-dark:hover { background: rgba(255,255,255,.14); color: var(--brand-ink); }
input[type=text], input[type=password], input[type=search], select, textarea {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 10px; width: 100%; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.logo-mark { width: 30px; height: 30px; border-radius: 50%; background: #fff; color: var(--brand); display: inline-grid; place-items: center; font-weight: 800; font-size: 16px; }

/* ---------- login ---------- */
.login { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, var(--brand), var(--brand-2)); }
.login-card { width: min(360px, 100%); background: var(--panel); border-radius: 16px; padding: 28px 24px; display: grid; gap: 12px; box-shadow: var(--shadow-lg); }
.login-logo { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 20px; }
.login-logo .logo-mark { background: var(--brand); color: #fff; }
.login-card p { margin: 0; }

/* ---------- shell ---------- */
.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.topbar { height: 56px; flex: none; display: flex; align-items: center; gap: 18px; padding: 0 14px; color: var(--brand-ink); background: linear-gradient(90deg, var(--brand), var(--brand-2)); }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 18px; }
.topnav { display: flex; gap: 4px; }
.nav-item { border: 0; background: transparent; color: rgba(255,255,255,.9); padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 500; white-space: nowrap; }
.nav-item .short { display: none; }
.nav-item:hover { background: rgba(255,255,255,.12); color: #fff; }
.nav-item.active { background: rgba(0,0,0,.22); color: #fff; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.scope { width: auto; max-width: 260px; background: rgba(255,255,255,.14); border-color: rgba(255,255,255,.25); color: #fff; padding: 6px 10px; }
.scope option { color: var(--text); background: var(--panel); }
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: rgba(255,255,255,.85); }
.live i { width: 8px; height: 8px; border-radius: 50%; background: #ffb4a8; }
.live.on i { background: #7cf0a6; box-shadow: 0 0 0 3px rgba(124,240,166,.25); }

.workspace { flex: 1; min-height: 0; display: grid; grid-template-columns: 380px minmax(0, 1fr) 340px; grid-template-rows: minmax(0, 1fr); }
.workspace > * { min-height: 0; }

/* ---------- conversation list ---------- */
.list-pane { display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--line); }
.list-toolbar { display: flex; gap: 6px; padding: 10px 10px 6px; }
.search { flex: 1; display: flex; align-items: center; gap: 6px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; color: var(--muted); }
.search input { border: 0; background: transparent; padding: 7px 0; box-shadow: none !important; }
.seg { display: flex; gap: 4px; padding: 0 10px 8px; border-bottom: 1px solid var(--line); }
.seg-item { border: 1px solid var(--line); background: var(--panel); color: var(--text-2); border-radius: 999px; padding: 3px 12px; font-size: 13px; cursor: pointer; }
.seg-item.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.conv-list { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.conv { display: grid; grid-template-columns: 48px minmax(0, 1fr); gap: 10px; padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line-2); position: relative; }
.conv:hover { background: var(--hover); }
.conv.active { background: var(--select); }
.conv .avatar { width: 48px; height: 48px; }
.conv .top { display: flex; align-items: baseline; gap: 8px; }
.conv .cname { flex: 1; font-size: 15px; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .time { font-size: 12px; color: var(--muted); white-space: nowrap; }
.conv .preview { display: flex; align-items: center; gap: 4px; color: var(--text-2); font-size: 13px; margin-top: 1px; }
.conv .preview span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .preview svg.i { width: 14px; height: 14px; color: var(--muted); }
.conv.unread .cname { font-weight: 700; }
.conv.unread .preview { color: var(--text); font-weight: 600; }
.conv.unread::after { content: ""; position: absolute; right: 12px; bottom: 14px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
.conv .meta { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; margin-top: 4px; min-height: 18px; padding-right: 18px; }
.conv .page-name { font-size: 11px; color: var(--muted); margin-right: 2px; max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .flag { color: var(--ok); display: inline-grid; }
.conv .flag svg.i { width: 15px; height: 15px; }
.list-more { padding: 12px; text-align: center; }
.list-empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.avatar { width: 40px; height: 40px; border-radius: 50%; object-fit: cover; background: var(--panel-2); flex: none; }
.avatar.fallback { display: inline-grid; place-items: center; color: #fff; font-weight: 700; font-size: 16px; }

.pill { display: inline-flex; align-items: center; height: 18px; padding: 0 7px; border-radius: 999px; font-size: 11px; font-weight: 600; color: #fff; background: var(--pill, #8a96a3); white-space: nowrap; max-width: 120px; overflow: hidden; text-overflow: ellipsis; }
.pill.sys { background: transparent; color: var(--warn); border: 1px solid currentColor; }
.pill.ai { background: var(--accent-soft); color: var(--accent); }
.pill.ai.err { background: transparent; color: var(--danger); border: 1px solid currentColor; }
.pill.ai.sent { background: transparent; color: var(--ok); border: 1px solid currentColor; }

/* ---------- chat ---------- */
.chat-pane { display: flex; flex-direction: column; min-width: 0; background: var(--chat-bg); }
.chat-empty { margin: auto; text-align: center; color: var(--muted); }
.empty-art { width: 120px; height: 90px; margin: 0 auto 10px; border-radius: 18px; background: linear-gradient(135deg, var(--panel), var(--panel-2)); box-shadow: var(--shadow); position: relative; }
.empty-art::before, .empty-art::after { content: ""; position: absolute; left: 18px; right: 30px; height: 10px; border-radius: 6px; background: var(--line); top: 26px; }
.empty-art::after { top: 48px; right: 50px; }
.chat { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.chat-head { display: flex; gap: 10px; align-items: center; padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 60px; }
.chat-head .who { flex: 1; min-width: 0; }
.chat-head .name { font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-actions { display: flex; gap: 2px; }
.back { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px 8px; display: flex; flex-direction: column; gap: 2px; overscroll-behavior: contain; }
.day-sep { align-self: center; margin: 12px 0 8px; font-size: 12px; color: var(--text-2); background: var(--panel); padding: 2px 12px; border-radius: 999px; box-shadow: var(--shadow); }
.row { display: flex; align-items: flex-end; gap: 8px; max-width: 78%; }
.row.out { align-self: flex-end; flex-direction: row-reverse; }
.row .avatar { width: 30px; height: 30px; font-size: 12px; }
.row .avatar.spacer { visibility: hidden; }
.row.gap { margin-top: 8px; }
.bubble { padding: 7px 11px; border-radius: 16px; background: var(--bubble-in); box-shadow: var(--shadow); white-space: pre-wrap; word-break: break-word; min-width: 40px; }
.row.out .bubble { background: var(--bubble-out); color: var(--bubble-out-ink); }
.row.in.first .bubble { border-top-left-radius: 6px; }
.row.out.first .bubble { border-top-right-radius: 6px; }
.bubble img { display: block; max-width: 240px; max-height: 260px; border-radius: 10px; margin: 2px 0; cursor: zoom-in; }
.bubble .media-only { padding: 0; background: transparent; box-shadow: none; }
.bubble .btns { display: grid; gap: 4px; margin-top: 6px; }
.bubble .btns span { border: 1px solid rgba(0,0,0,.1); background: rgba(255,255,255,.55); border-radius: 8px; padding: 4px 8px; font-size: 13px; text-align: center; }
.bubble a { color: var(--accent); }
.mt { font-size: 11px; color: var(--muted); margin: 2px 4px 0; }
.row.out .mt { text-align: right; }
.msg-col { display: flex; flex-direction: column; min-width: 0; }
.row.out .msg-col { align-items: flex-end; }
.sys-note { align-self: center; font-size: 12px; color: var(--muted); margin: 4px 0; }

.suggest-bar { margin: 0 12px 6px; background: var(--panel); border: 1px solid var(--accent); border-radius: 12px; padding: 8px 10px; display: grid; gap: 6px; box-shadow: var(--shadow); }
.suggest-bar .sb-head { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--accent); font-weight: 600; }
.suggest-bar .sb-head .grow { flex: 1; }
.suggest-bar ol { margin: 0; padding-left: 18px; font-size: 13px; display: grid; gap: 2px; }
.suggest-bar .sb-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.chat-head, .suggest-bar, .tag-grid, .composer { flex: none; }
.messages { min-height: 0; }
.suggest-bar ol { max-height: 30vh; overflow-y: auto; }
.tag-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); grid-auto-rows: 22px; gap: 3px; padding: 6px 8px; background: var(--panel); border-top: 1px solid var(--line); max-height: 132px; overflow-y: auto; }
.tag-grid button { border: 0; border-radius: 4px; padding: 3px 4px; font-size: 11px; cursor: pointer; color: var(--text); background: color-mix(in srgb, var(--tc, #8a96a3) 22%, var(--panel)); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tag-grid button.on { background: var(--tc, #8a96a3); color: #fff; font-weight: 700; box-shadow: inset 0 0 0 2px rgba(255,255,255,.35); }

.composer { background: var(--panel); border-top: 1px solid var(--line); padding: 8px 10px 6px; display: grid; gap: 4px; }
.composer textarea { border: 0; resize: none; padding: 6px 4px; min-height: 34px; max-height: 160px; overflow-y: hidden; box-shadow: none !important; background: transparent; }
.composer-bar { display: flex; align-items: center; gap: 2px; }
.send-btn { width: 38px; height: 34px; border: 0; border-radius: 8px; background: transparent; color: var(--accent); cursor: pointer; display: inline-grid; place-items: center; }
.send-btn:hover { background: var(--accent-soft); }
.send-btn svg.i { width: 22px; height: 22px; }

/* ---------- side panel ---------- */
.side-pane { background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; }
.side-tabs { display: flex; border-bottom: 1px solid var(--line); flex: none; }
.side-tab { flex: 1; border: 0; background: transparent; padding: 14px 8px 12px; cursor: pointer; color: var(--text-2); border-bottom: 2px solid transparent; font-weight: 500; }
.side-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.side-close { display: none; align-self: center; margin-right: 6px; }
.count { display: inline-grid; place-items: center; min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px; font-size: 11px; background: var(--danger); color: #fff; }
.side-body { flex: 1; overflow-y: auto; padding: 14px; }
.section { margin-bottom: 18px; }
.section h4 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.profile { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.profile .avatar { width: 52px; height: 52px; }
.profile .pname { font-weight: 700; font-size: 15px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--panel-2); border-radius: 10px; padding: 8px; text-align: center; }
.stat b { display: block; font-size: 17px; }
.stat span { font-size: 11px; color: var(--muted); }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; font-size: 13px; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }

.ai-actions { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.run { border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 10px; display: grid; gap: 6px; }
.run.suggested { border-color: var(--accent); }
.run .head { display: flex; justify-content: space-between; gap: 6px; font-size: 12px; align-items: center; }
.run .code { font-weight: 700; font-size: 13px; }
.run textarea { font-size: 13px; }
.run .replies { font-size: 13px; display: grid; gap: 3px; }
.run .replies div { background: var(--panel-2); border-radius: 8px; padding: 4px 8px; }
.run .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.status { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--panel-2); color: var(--text-2); }
.status.suggested { background: var(--accent-soft); color: var(--accent); }
.status.sent { color: var(--ok); }
.status.error, .status.aborted { color: var(--danger); }

/* ---------- modal / misc ---------- */
.modal { width: min(1000px, calc(100vw - 24px)); max-height: calc(100vh - 24px); border: 0; border-radius: 14px; padding: 0; background: var(--panel); color: var(--text); box-shadow: var(--shadow-lg); }
.modal::backdrop { background: rgba(10,16,22,.45); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px 10px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 16px; overflow: auto; max-height: calc(100vh - 100px); display: grid; gap: 14px; }
.card { border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 10px; }
.card h3 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.field { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.field > input, .field > select, .field > textarea { color: var(--text); font-size: 14px; }
.check { display: flex; gap: 6px; align-items: center; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { border-bottom: 1px solid var(--line); padding: 6px; text-align: left; vertical-align: top; }
.table tr.click { cursor: pointer; }
.table tr.click:hover td { background: var(--hover); }
.stack { display: grid; gap: 10px; }
.kb-row { display: grid; gap: 6px; border: 1px dashed var(--line); border-radius: 8px; padding: 8px; }
.table-wrap { overflow-x: auto; }
.table.kb { min-width: 980px; }
.table.kb td { min-width: 90px; }
.table.kb td:nth-child(4), .table.kb td:nth-child(5) { min-width: 240px; }
.table textarea, .table input[type=text] { font-size: 13px; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #1f2a33; color: #fff; padding: 9px 16px; border-radius: 10px; z-index: 60; max-width: calc(100vw - 32px); box-shadow: var(--shadow-lg); font-size: 13px; }
.lightbox { position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.85); display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 92vh; border-radius: 8px; }

/* ---------- responsive ---------- */
@media (max-width: 1280px) {
  .workspace { grid-template-columns: 340px minmax(0, 1fr); }
  .side-pane { position: fixed; right: 0; top: 56px; bottom: 0; width: min(380px, 100vw); z-index: 30; box-shadow: var(--shadow-lg); transform: translateX(105%); transition: transform .18s ease; }
  .side-pane.open { transform: none; }
  .side-close { display: inline-grid; }
}
@media (max-width: 820px) {
  .topbar { gap: 8px; padding: 0 8px; }
  .brand-name, .live-text { display: none; }
  .topnav .nav-item { padding: 8px 9px; font-size: 13px; }
  .nav-item .long { display: none; }
  .nav-item .short { display: inline; }
  .suggest-bar ol { max-height: 22vh; }
  .scope { max-width: 130px; font-size: 13px; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .chat-pane { display: none; }
  .app.in-chat .list-pane { display: none; }
  .app.in-chat .chat-pane { display: flex; }
  .back { display: inline-grid; }
  .row { max-width: 88%; }
  .grid2 { grid-template-columns: 1fr; }
  .side-pane { top: 0; width: 100vw; }
}
@media (max-width: 420px) {
  .topnav .nav-item { padding: 8px 6px; font-size: 12px; }
  .scope { max-width: 96px; }
}
