:root, [data-theme="dark"] {
  --bg: #0d1117; --panel: #151b24; --panel-2: #1b2330; --line: #263040; --text: #e6ebf2; --muted: #8b97a8;
  --accent: #6c6cff; --accent-soft: #6c6cff22; --green: #22c37a; --red: #f0566a; --amber: #f2a93b; --blue: #4aa3ff;
  --side: #0a0e14; --side-text: #aab4c3; --shadow: 0 8px 24px #0006; --radius: 12px;
}
[data-theme="light"] {
  --bg: #f4f6fa; --panel: #ffffff; --panel-2: #f7f8fb; --line: #e3e7ee; --text: #131a26; --muted: #637083;
  --accent: #5a55f0; --accent-soft: #5a55f018; --side: #0f1522; --side-text: #b4bdcc; --shadow: 0 8px 24px #1a22370d;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.shell { display: flex; min-height: 100vh; min-height: 100dvh; }
.sidebar { width: 260px; flex-shrink: 0; background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 16px 12px; gap: 6px; position: sticky; top: 0; height: 100vh; height: 100dvh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; }
.logo { width: 32px; height: 32px; border-radius: 9px; background: linear-gradient(135deg, #8b8bff, #5050e6); display: grid; place-items: center; color: #fff; font-size: 18px; }
.brand-text strong { display: block; color: #fff; font-size: 15px; }
.brand-text small { display: block; font-size: 10.5px; color: #6f7a8c; }
.new-chat { width: 100%; margin-bottom: 8px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { color: var(--side-text); padding: 8px 10px; border-radius: 8px; display: flex; align-items: center; gap: 6px; text-decoration: none; }
.nav a:hover { background: #ffffff0d; color: #fff; }
.nav a.active { background: #ffffff14; color: #fff; }
.badge { margin-left: auto; background: var(--red); color: #fff; border-radius: 10px; font-size: 11px; padding: 0 7px; line-height: 18px; }
.side-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: .08em; color: #5f6a7c; margin: 14px 10px 4px; }
.chat-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; min-height: 60px; }
.chat-item { display: flex; align-items: center; gap: 4px; border-radius: 8px; padding: 2px 4px 2px 10px; cursor: pointer; }
.chat-item:hover, .chat-item.active { background: #ffffff10; color: #fff; }
.chat-item .title { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 13px; padding: 6px 0; }
.chat-item .acts { display: none; gap: 2px; }
.chat-item:hover .acts, .chat-item.active .acts { display: flex; }
.chat-item .acts button { background: none; border: 0; color: var(--side-text); cursor: pointer; padding: 4px; border-radius: 6px; font-size: 12px; }
.chat-item .acts button:hover { background: #ffffff18; color: #fff; }
.side-footer { display: flex; align-items: center; gap: 8px; font-size: 12px; padding: 10px 8px 0; border-top: 1px solid #ffffff12; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--amber); flex-shrink: 0; }
.dot.ok { background: var(--green); } .dot.bad { background: var(--red); }
.side-footer .icon-btn { margin-left: auto; color: var(--side-text); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px; padding: 12px 28px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); min-height: 58px; }
.topbar h1 { font-size: 17px; margin: 0; font-weight: 650; }
.top-meta { margin-left: auto; font-size: 12px; color: var(--muted); display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.menu-btn { display: none; }
.view { flex: 1; padding: 24px 28px 40px; width: 100%; max-width: 1400px; margin: 0 auto; outline: none; }
.backdrop { display: none; }

.icon-btn { background: none; border: 0; cursor: pointer; color: var(--muted); font-size: 16px; padding: 6px; border-radius: 8px; }
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.btn, .btn-primary, .btn-danger { border-radius: 9px; padding: 8px 14px; cursor: pointer; border: 1px solid var(--line); background: var(--panel-2); font-weight: 550; font-size: 13px; }
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.1); }
.btn-primary:disabled, .btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-danger { color: var(--red); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; font-size: inherit; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.card-pad { padding: 16px 18px; }
.card h3 { margin: 0 0 10px; font-size: 14px; font-weight: 650; }
.grid { display: grid; gap: 14px; }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g-main { grid-template-columns: minmax(0, 1.7fr) minmax(300px, 1fr); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 24px 0 10px; flex-wrap: wrap; }
.section-head h2 { font-size: 15px; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pos { color: var(--green); } .neg { color: var(--red); } .warn { color: var(--amber); }
.metric { padding: 14px 16px; cursor: default; }
.metric.clickable { cursor: pointer; }
.metric.clickable:hover { border-color: var(--accent); }
.metric .label { font-size: 12px; color: var(--muted); }
.metric .value { font-size: 22px; font-weight: 700; margin: 4px 0 2px; letter-spacing: -.02em; font-variant-numeric: tabular-nums; }
.metric .sub { font-size: 12px; }
.src { font-size: 11px; color: var(--muted); }

.table-wrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.t th, .t td { padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; font-size: 13px; }
.t th { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; background: var(--panel-2); position: sticky; top: 0; cursor: default; }
.t th.sortable { cursor: pointer; } .t th.sortable:hover { color: var(--text); }
.t td:first-child, .t th:first-child, .t .left { text-align: left; }
.t tr.row:hover td { background: var(--panel-2); cursor: pointer; }
.t tr:last-child td { border-bottom: 0; }
.asset { display: flex; align-items: center; gap: 9px; }
.asset img { width: 22px; height: 22px; border-radius: 50%; }
.asset .ph { width: 22px; height: 22px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-size: 9px; font-weight: 700; }
.asset b { font-weight: 650; }
.asset small { color: var(--muted); margin-left: 4px; }
.spark { width: 100px; height: 30px; display: block; }

.tag { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); margin: 0 4px 4px 0; white-space: nowrap; }
.tag.warn { background: #f2a93b22; color: var(--amber); }
.tag.bad { background: #f0566a22; color: var(--red); }
.tag.good { background: #22c37a22; color: var(--green); }
.sev { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 6px; text-transform: uppercase; }
.sev.critical { background: #f0566a; color: #fff; } .sev.high { background: #f0566a26; color: var(--red); }
.sev.medium { background: #f2a93b26; color: var(--amber); } .sev.low { background: var(--panel-2); color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line); background: var(--panel-2); border-radius: 20px; padding: 6px 12px; cursor: pointer; font-size: 12.5px; }
.chip:hover, .chip.active { border-color: var(--accent); color: var(--accent); }

.bar { height: 6px; border-radius: 4px; background: var(--panel-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.bar.hot > i { background: linear-gradient(90deg, var(--green), var(--amber), var(--red)); }

/* ---------- Чат ---------- */
.chat-view { display: flex; flex-direction: column; min-height: calc(100dvh - 120px); max-width: 860px; margin: 0 auto; }
.chat-view.empty { justify-content: center; }
.hero { text-align: center; margin-bottom: 18px; }
.hero h2 { font-size: 28px; margin: 0 0 6px; letter-spacing: -.02em; }
.hero p { margin: 0; color: var(--muted); }
.composer { position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 12px 54px 12px 16px; }
.composer:focus-within { border-color: var(--accent); }
.composer textarea { width: 100%; border: 0; outline: none; resize: none; background: transparent; max-height: 220px; min-height: 24px; line-height: 1.5; }
.composer .send { position: absolute; right: 10px; bottom: 10px; width: 36px; height: 36px; border-radius: 10px; border: 0; background: var(--accent); color: #fff; cursor: pointer; font-size: 16px; }
.composer .send:disabled { opacity: .4; cursor: not-allowed; }
.chat-view:not(.empty) .composer-wrap { position: sticky; bottom: 0; padding: 14px 0 6px; background: linear-gradient(transparent, var(--bg) 30%); }
.hint { font-size: 11.5px; color: var(--muted); text-align: center; margin-top: 8px; }
.starter { margin-top: 18px; }
.messages { display: flex; flex-direction: column; gap: 18px; padding-top: 8px; flex: 1; }
.msg.user { align-self: flex-end; max-width: 80%; background: var(--accent); color: #fff; padding: 10px 14px; border-radius: 16px 16px 4px 16px; white-space: pre-wrap; word-break: break-word; }
.msg.assistant { max-width: 100%; }
.msg.assistant .body { line-height: 1.65; word-break: break-word; }
.msg.assistant .body p { margin: 0 0 10px; }
.msg.assistant .body ul { margin: 4px 0 10px; padding-left: 20px; }
.msg.assistant .body li { margin: 3px 0; }
.msg.assistant .body table { border-collapse: collapse; margin: 8px 0 12px; font-size: 13px; display: block; overflow-x: auto; font-variant-numeric: tabular-nums; }
.msg.assistant .body th, .msg.assistant .body td { border: 1px solid var(--line); padding: 6px 10px; text-align: left; white-space: nowrap; }
.msg.assistant .body th { background: var(--panel-2); }
.msg-meta { display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: 11.5px; color: var(--muted); margin-top: 6px; align-items: center; }
.msg-meta details { width: 100%; }
.msg-meta summary { cursor: pointer; }
.qc { margin: 6px 0 0; padding-left: 18px; }
.followups { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; } .typing i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.pulse-row { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; margin-top: 22px; }
.msg-pending { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; }
.hint.busy { color: var(--amber); }

/* Фоновая генерация: точка у «AI-аналитик», спиннер в списке диалогов */
.busy-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-left: auto; flex-shrink: 0; animation: blink 1.2s infinite; }
.busy-dot.on-menu { position: absolute; top: 4px; right: 4px; margin: 0; }
.menu-btn { position: relative; }
.spin { width: 11px; height: 11px; border: 2px solid #ffffff30; border-top-color: var(--accent); border-radius: 50%; flex-shrink: 0; animation: spin .8s linear infinite; }
.stale-mark { font-size: 11px; font-weight: 700; color: var(--amber); flex-shrink: 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
@media (prefers-reduced-motion: reduce) {
  .busy-dot, .typing i, .skeleton { animation: none; }
  .spin { animation-duration: 2.4s; }
}

/* ---------- Формы ---------- */
.form { display: grid; gap: 10px; }
.form-row { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); align-items: end; }
label.f { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
input.i, select.i, textarea.i { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; color: var(--text); width: 100%; min-width: 0; }
input.i:focus, select.i:focus, textarea.i:focus { border-color: var(--accent); outline: none; }
.err { color: var(--red); font-size: 12.5px; }
.note { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; font-size: 12.5px; color: var(--muted); }
.note.warn { border-color: #f2a93b55; color: var(--amber); }
.note.bad { border-color: #f0566a55; color: var(--red); }

/* Память и удаление данных (Настройки) */
.fact-list { list-style: none; margin: 0; padding: 0; }
.fact { padding: 8px 0; border-bottom: 1px solid var(--line); }
.fact-text { white-space: pre-wrap; word-break: break-word; }
.fact-meta { display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; margin-top: 4px; }
.fact-meta .tag { margin: 0; }
.fact-acts { margin-left: auto; display: flex; gap: 10px; }
.link-btn.neg { color: var(--red); }
.wipe-list { display: grid; gap: 10px; }
.wipe-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.wipe-row:last-child { border-bottom: 0; padding-bottom: 0; }
.wipe-row .btn { flex-shrink: 0; }
@media (max-width: 480px) { .wipe-row { flex-direction: column; align-items: stretch; } }
/* Аккаунт: вход через Telegram-бота и перенос данных (Настройки) */
.auth-card .chips { margin-top: 8px; }
.auth-who { margin: 0 0 6px; }
.auth-msg { margin-top: 10px; }
.auth-flow { display: grid; gap: 10px; justify-items: start; }
.auth-code-label { margin: 0; font-weight: 600; }
.auth-code { margin: 0; font-size: 40px; line-height: 1.1; font-weight: 750; letter-spacing: .18em; font-variant-numeric: tabular-nums; color: var(--accent); background: var(--accent-soft); border-radius: 10px; padding: 6px 16px 6px 22px; }
.auth-open { display: inline-block; text-decoration: none; }
.auth-open:hover { text-decoration: none; }
.auth-cmd { display: grid; gap: 4px; width: 100%; }
.auth-cmd-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.auth-cmd code { background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 4px 8px; word-break: break-all; user-select: all; }
.auth-hint { margin: 0; color: var(--amber); }
.auth-foot { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; width: 100%; }
.auth-foot [role="timer"] { font-variant-numeric: tabular-nums; font-weight: 600; color: var(--text); }
.auth-dialog { width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow-y: auto; background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.auth-dialog::backdrop { background: #0009; }
.auth-dialog h2 { font-size: 16px; margin: 0 0 10px; }
.auth-dialog p { margin: 0 0 8px; }
.auth-dialog .note { margin-bottom: 8px; }
.auth-counts { margin: 0 0 12px; padding-left: 20px; }
.auth-dlg-acts { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 8px; }
@media (max-width: 480px) { .auth-dlg-acts { flex-direction: column; } .auth-dlg-acts button { width: 100%; } }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 8px 12px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

.chart-box { position: relative; width: 100%; height: 300px; }
.chart-box canvas { width: 100%; height: 100%; display: block; }
.legend { display: flex; gap: 12px; font-size: 11.5px; color: var(--muted); margin-top: 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 3px; margin-right: 4px; vertical-align: middle; }
.ring { width: 140px; height: 140px; border-radius: 50%; display: grid; place-items: center; margin: 4px auto 12px; }
.ring > div { width: 108px; height: 108px; border-radius: 50%; background: var(--panel); display: grid; place-items: center; text-align: center; }
.ring b { display: block; font-size: 30px; line-height: 1; }
.alloc { display: flex; height: 10px; border-radius: 6px; overflow: hidden; margin: 8px 0; }
.alloc > i { display: block; height: 100%; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.event { padding: 14px 16px; border-left: 3px solid var(--line); }
.event.critical, .event.high { border-left-color: var(--red); }
.event.medium { border-left-color: var(--amber); }
.event .head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.event h4 { margin: 0; font-size: 14px; }
.event .ev { font-size: 12px; color: var(--muted); margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px 14px; }
.event.unread { background: color-mix(in srgb, var(--accent) 6%, var(--panel)); }
.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); }
.skeleton { background: linear-gradient(90deg, var(--panel-2), var(--line), var(--panel-2)); background-size: 200% 100%; animation: sk 1.4s infinite; border-radius: 8px; min-height: 16px; }
@keyframes sk { to { background-position: -200% 0; } }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(20px); background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow); padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: .2s; z-index: 50; max-width: 90vw; }
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { border-color: var(--red); color: var(--red); }

@media (max-width: 1100px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g-main { grid-template-columns: 1fr; }
  .pulse-row { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 820px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 30; transform: translateX(-100%); transition: transform .2s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .backdrop.open { display: block; position: fixed; inset: 0; background: #0008; z-index: 20; }
  .menu-btn { display: inline-block; }
  .topbar { padding: 10px 14px; }
  .view { padding: 16px 12px 32px; }
  .g3, .g2 { grid-template-columns: 1fr; }
  .top-meta .hide-sm { display: none; }
  .hero h2 { font-size: 22px; }
  .chat-view { min-height: calc(100dvh - 100px); }
  .msg.user { max-width: 92%; }
  .t th, .t td { padding: 8px; }
  .hide-sm { display: none; }
}
@media (max-width: 480px) {
  .g4 { grid-template-columns: 1fr 1fr; gap: 8px; }
  .pulse-row { grid-template-columns: 1fr 1fr; }
  .metric .value { font-size: 18px; }
  .chart-box { height: 240px; }
}
