:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --panel-2: #f9fafb;
  --border: #e5e7eb;
  --text: #111827;
  --muted: #6b7280;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --note: #fef9c3;
  --note-border: #fde68a;
  --danger: #dc2626;
  --ok: #16a34a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d12;
    --panel: #151821;
    --panel-2: #1b1f2a;
    --border: #2a2f3c;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --accent: #818cf8;
    --accent-soft: #1e1b4b;
    --note: #3a3413;
    --note-border: #5c4f13;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input[type='color'] { width: 64px; height: 38px; padding: 3px; cursor: pointer; }
button { cursor: pointer; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
code, pre { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; }
pre { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow-x: auto; white-space: pre-wrap; word-break: break-all; }
.muted { color: var(--muted); }

/* auth */
.auth { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; display: flex; flex-direction: column; gap: 12px; }
.auth-card h1 { margin: 0 0 4px; font-size: 20px; }
.auth-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.auth-card .setup-only { display: none; }
.auth-card.setup .setup-only { display: flex; }
.error { color: var(--danger); font-size: 13px; min-height: 18px; }
.logo { font-weight: 700; font-size: 18px; }

/* layout */
.app { display: flex; height: 100%; }
.nav { width: 60px; background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; align-items: center; padding: 12px 0; gap: 6px; }
.nav .logo { margin-bottom: 10px; }
.nav a, .nav button { position: relative; width: 42px; height: 42px; display: flex; align-items: center; justify-content: center; border-radius: 10px; text-decoration: none; font-size: 19px; border: 0; background: transparent; padding: 0; }
.nav a.on { background: var(--accent-soft); }
.nav-spacer { flex: 1; }
.nav-badge, .nav-count { position: absolute; top: 2px; right: 0; min-width: 17px; height: 17px; padding: 0 4px; border-radius: 9px; font-size: 10.5px; font-weight: 600; display: flex; align-items: center; justify-content: center; color: #fff; }
.nav-badge { background: #ef4444; }
.nav-count { background: var(--ok); }
.nav-badge:empty, .nav-count:empty { display: none; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 52px; display: flex; align-items: center; gap: 16px; padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--border); }
.team { display: flex; gap: 6px; flex: 1; overflow: hidden; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 99px; background: var(--panel-2); border: 1px solid var(--border); font-size: 12px; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #9ca3af; flex: none; }
.dot.on { background: #22c55e; }
.me { color: var(--muted); white-space: nowrap; }
.view { display: none; flex: 1; min-height: 0; }
.view.on { display: flex; }
.page { flex-direction: column; overflow-y: auto; padding: 20px 24px 40px; gap: 14px; }
.page h2 { margin: 0; font-size: 20px; }
.page > * { max-width: 900px; }

/* inbox */
.convs { width: 320px; flex: none; border-right: 1px solid var(--border); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.convs-head { padding: 12px; display: flex; flex-direction: column; gap: 8px; border-bottom: 1px solid var(--border); }
.tabs { display: flex; gap: 4px; }
.tabs button { flex: 1; padding: 5px; font-size: 13px; }
.tabs button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.conv-list { flex: 1; overflow-y: auto; }
.conv { display: flex; gap: 10px; padding: 12px; border-bottom: 1px solid var(--border); cursor: pointer; }
.conv:hover { background: var(--panel-2); }
.conv.on { background: var(--accent-soft); }
.avatar { position: relative; width: 38px; height: 38px; flex: none; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; font-weight: 600; font-size: 15px; }
.avatar .dot { position: absolute; right: -1px; bottom: -1px; border: 2px solid var(--panel); width: 11px; height: 11px; }
.conv-body { flex: 1; min-width: 0; }
.conv-top { display: flex; gap: 6px; align-items: baseline; }
.conv-name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 11.5px; color: var(--muted); }
.conv-last { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-last.typing { color: var(--accent); font-style: italic; }
.conv.unread .conv-name::after { content: ''; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #ef4444; margin-left: 6px; vertical-align: middle; }
.conv-meta { display: flex; gap: 6px; margin-top: 3px; font-size: 11.5px; color: var(--muted); }
.tag { padding: 0 6px; border-radius: 4px; background: var(--panel-2); border: 1px solid var(--border); }

.thread { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--panel-2); }
.thread .empty { margin: auto; color: var(--muted); }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.thread-head h3 { margin: 0; font-size: 16px; flex: 1; min-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.messages { flex: 1; overflow-y: auto; padding: 16px 20px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 70%; padding: 8px 12px; border-radius: 14px; white-space: pre-wrap; word-wrap: break-word; }
.msg.visitor { align-self: flex-start; background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.operator, .msg.bot { align-self: flex-end; background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.msg.bot { background: #64748b; }
.msg.note { align-self: flex-end; background: var(--note); border: 1px solid var(--note-border); color: var(--text); }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 12.5px; }
.msg-meta { font-size: 11px; color: var(--muted); margin: 6px 4px 0; }
.msg-meta.right { align-self: flex-end; }
.msg-meta.left { align-self: flex-start; }
.msg a { color: inherit; }
.typing-line { padding: 0 20px 6px; min-height: 22px; font-size: 12.5px; color: var(--muted); font-style: italic; }

.composer { background: var(--panel); border-top: 1px solid var(--border); padding: 10px 12px; position: relative; }
.composer.note { background: var(--note); }
.composer-tabs { display: flex; gap: 4px; margin-bottom: 6px; }
.composer-tabs button { border: 0; background: transparent; padding: 3px 8px; font-size: 12.5px; color: var(--muted); }
.composer-tabs button.on { color: var(--text); font-weight: 600; background: var(--panel-2); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { flex: 1; resize: none; min-height: 42px; max-height: 200px; }
.canned-pop { position: absolute; bottom: 100%; left: 12px; right: 12px; max-height: 220px; overflow-y: auto; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.15); }
.canned-pop div { padding: 8px 12px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.canned-pop div.on, .canned-pop div:hover { background: var(--accent-soft); }
.canned-pop b { margin-right: 8px; }

.profile { width: 300px; flex: none; border-left: 1px solid var(--border); background: var(--panel); overflow-y: auto; padding: 16px; }
.profile h4 { margin: 18px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.profile .big { display: flex; gap: 12px; align-items: center; }
.profile .big .avatar { width: 48px; height: 48px; font-size: 19px; }
.profile label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 2px; margin-bottom: 8px; }
.profile dl { margin: 0; display: grid; grid-template-columns: 80px 1fr; gap: 4px 8px; font-size: 13px; }
.profile dt { color: var(--muted); }
.profile dd { margin: 0; word-break: break-all; }
.events { list-style: none; padding: 0; margin: 0; font-size: 12.5px; }
.events li { padding: 4px 0; border-bottom: 1px dashed var(--border); word-break: break-all; }
.events time { color: var(--muted); margin-right: 6px; }

/* cards & forms */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px 18px; }
.card h3 { margin: 0 0 10px; font-size: 15px; }
.form { display: flex; flex-direction: column; gap: 10px; }
.form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.form button { align-self: flex-start; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.inline-form .grow { flex: 1; min-width: 180px; }
.row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.row:last-child { border-bottom: 0; }
.row .grow { flex: 1; min-width: 0; }
.saved { color: var(--ok); font-size: 13px; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; }
.stat .v { font-size: 26px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 12.5px; }
.bars { display: flex; align-items: flex-end; gap: 6px; height: 160px; padding-top: 10px; }
.bars .b { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; gap: 4px; font-size: 11px; color: var(--muted); }
.bars .b i { display: block; width: 100%; max-width: 36px; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; }
table { width: 100%; border-collapse: collapse; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); font-size: 13px; vertical-align: top; }
th { color: var(--muted); font-weight: 500; background: var(--panel-2); }
td.url { word-break: break-all; }

@media (max-width: 1200px) { .profile { display: none; } }
@media (max-width: 760px) {
  .convs { width: 100%; }
  .inbox.has-thread .convs { display: none; }
  .inbox:not(.has-thread) .thread { display: none; }
  .back { display: inline-flex !important; }
  .team { display: none; }
  .page { padding: 16px; }
}
.back { display: none; }

/* channels, reviews, filters */
.filters { display: flex; gap: 6px; flex-wrap: wrap; }
.filters select { flex: 1; min-width: 110px; padding: 6px 8px; font-size: 13px; }
.stars { color: #f59e0b; letter-spacing: 1px; white-space: nowrap; }
.stars .off { color: var(--border); }
.review-card { margin: 0 0 6px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); display: flex; flex-direction: column; gap: 4px; align-self: stretch; }
.review-card .stars { font-size: 18px; }
.review-text { white-space: pre-wrap; }
.conv-meta { min-width: 0; }
.conv-meta > * { white-space: nowrap; }
.conv-meta .tag { overflow: hidden; text-overflow: ellipsis; max-width: 60%; }
.review-meta { display: flex; gap: 6px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.status-pill { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; padding: 1px 8px; border-radius: 99px; border: 1px solid var(--border); white-space: nowrap; }
.status-pill.running { color: var(--ok); border-color: currentColor; }
.status-pill.error { color: var(--danger); border-color: currentColor; }
.status-pill.disabled, .status-pill.idle { color: var(--muted); }
.channel-error { color: var(--danger); font-size: 12.5px; margin-top: 2px; word-break: break-word; }
.channel-form { margin-top: 12px; padding: 14px; border: 1px dashed var(--border); border-radius: 12px; background: var(--panel-2); }
.channel-form .help { font-size: 12px; color: var(--muted); }
.channel-form .actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.channel-form label.check { flex-direction: row; align-items: center; gap: 8px; }
.projects-pick { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 4px; font-size: 12.5px; }
.projects-pick label { display: inline-flex; gap: 4px; align-items: center; }
.composer.review .composer-tabs button[data-mode="reply"]::after { content: ' (публичный)'; }
