/* Theme: light by default; dark follows the OS unless the user picked a theme (data-theme on <html>). */
:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --line: #e4e6ea;
  --grid: #e9eaed;
  --accent: #2f5bea;
  --accent-soft: #e8eefe;
  --danger: #c92a2a;
  --ok: #2b8a3e;
  --warn: #b7791f;
  /* categorical series, fixed order (validated palette) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --other: #a3a7ae;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    --bg: #111317; --panel: #1a1d23; --text: #e8eaed; --muted: #9aa1ab; --line: #2a2e36; --grid: #262a31;
    --accent: #6d8cff; --accent-soft: #232b45; --shadow: none; color-scheme: dark;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #0ca30c; --s7: #9085e9; --s8: #e66767; --other: #5f6670;
  }
}
:root[data-theme="dark"] {
  --bg: #111317; --panel: #1a1d23; --text: #e8eaed; --muted: #9aa1ab; --line: #2a2e36; --grid: #262a31;
  --accent: #6d8cff; --accent-soft: #232b45; --shadow: none; color-scheme: dark;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #0ca30c; --s7: #9085e9; --s8: #e66767; --other: #5f6670;
}
:root { --vk: var(--s1); --telegram: var(--s3); --max: var(--s7); }
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 16px; font-weight: 650; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; }
button, input, select { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 7px 12px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
button:disabled { opacity: .5; cursor: default; }
input, select {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 10px; min-width: 0;
}
input:focus, select:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* layout */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px;
  display: flex; flex-direction: column; gap: 4px; position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 700; font-size: 17px; padding: 4px 10px 16px; }
.brand span { color: var(--accent); }
.brand small { display: block; font-size: 11px; font-weight: 400; color: var(--muted); margin-top: 2px; }
.nav a {
  display: block; padding: 8px 10px; border-radius: 8px; color: var(--text); text-decoration: none;
}
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav a:hover { background: var(--bg); }
.who { margin-top: auto; padding: 10px; color: var(--muted); font-size: 13px; }
.who b { display: block; color: var(--text); font-weight: 600; }
main { padding: 24px 28px 48px; min-width: 0; }
.menu-toggle { display: none; }

/* panels */
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); }
.ok { color: var(--ok); }
.warn { color: var(--warn); }

/* filters */
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: end; }
.filters label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.filters input[type=search] { width: 220px; }

/* kpi */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
.kpi { padding: 14px 16px; }
.kpi .label { color: var(--muted); font-size: 12px; }
.kpi .value { font-size: 24px; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }

/* theme toggle */
.theme-toggle { width: 100%; text-align: left; margin-bottom: 8px; }

/* chart */
.chart-head { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.chart-head h2 { margin: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; padding: 5px 10px; background: transparent; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.plot { position: relative; height: 240px; }
.plot svg { display: block; }
.plot text { fill: var(--muted); font-size: 11px; }
.tip {
  position: absolute; pointer-events: none; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 4px 14px rgba(0, 0, 0, .12);
  white-space: nowrap; z-index: 2; color: var(--text);
}
.tip b { display: block; margin-bottom: 4px; }
.tip .r { display: flex; gap: 10px; justify-content: space-between; }
.tip .r span:last-child { font-variant-numeric: tabular-nums; font-weight: 600; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 7px; vertical-align: -1px; flex: none; }
.donut { display: flex; justify-content: center; margin: 4px 0 12px; position: relative; }
.donut svg { overflow: visible; }
.donut .center { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; text-align: center; }
.donut .center b { display: block; font-size: 18px; }
.donut .center span { font-size: 11px; color: var(--muted); }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.dot-vk { background: var(--vk); } .dot-telegram { background: var(--telegram); } .dot-max { background: var(--max); }

/* bars table */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--bg); }
.table-wrap { overflow-x: auto; }

/* posts */
/* feed: every post is its own card */
.feed { display: flex; flex-direction: column; gap: 10px; }
.feed > .empty { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.post { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); transition: border-color .15s; }
.post:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.post .stats { border-top: 1px solid var(--line); padding-top: 8px; margin-top: 8px; }
.post .meta { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.post .src { font-weight: 600; color: var(--text); }
.post .text { margin: 6px 0; white-space: pre-wrap; word-break: break-word; }
.post .stats { font-size: 12px; color: var(--muted); display: flex; gap: 14px; flex-wrap: wrap; align-items: center; }
.tag { display: inline-block; padding: 1px 7px; border-radius: 10px; background: var(--bg); font-size: 11px; color: var(--muted); }
.tag.vk { background: color-mix(in srgb, var(--vk) 15%, transparent); color: var(--vk); }
.tag.telegram { background: color-mix(in srgb, var(--telegram) 18%, transparent); color: #0b7285; }
.tag.max { background: color-mix(in srgb, var(--max) 15%, transparent); color: var(--max); }
.top-item { padding: 8px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.top-item:last-child { border-bottom: 0; }
.top-item .t { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.top-item .n { font-weight: 650; font-variant-numeric: tabular-nums; }

.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 12px; }
.loading { opacity: .55; pointer-events: none; transition: opacity .15s; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

/* dialog */
dialog { border: 1px solid var(--line); border-radius: var(--radius); padding: 0; max-width: 720px; width: calc(100% - 32px);
  background: var(--panel); color: var(--text); }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
dialog .head { display: flex; justify-content: space-between; align-items: center; padding: 14px 16px; border-bottom: 1px solid var(--line); }
dialog .body { padding: 16px; max-height: 70vh; overflow: auto; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: #222; color: #fff;
  padding: 10px 16px; border-radius: 8px; z-index: 10; max-width: 90vw; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 12px; }
.login label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid.three { grid-template-columns: 1fr; }
}
@media (max-width: 800px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 240px; transform: translateX(-100%); transition: transform .2s; z-index: 5; }
  .sidebar.open { transform: none; }
  .menu-toggle { display: inline-block; margin-bottom: 12px; }
  main { padding: 16px; }
  .grid.two { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters input[type=search] { width: 100%; }
  .filters label { flex: 1 1 140px; }
}

/* breakdown lists */
.seg.small button { padding: 3px 8px; font-size: 12px; }
table.breakdown td, table.breakdown th { padding: 6px 3px; }
table.breakdown th.sorted { color: var(--accent); }
table.breakdown td.sorted { background: color-mix(in srgb, var(--accent-soft) 55%, transparent); }
table.breakdown { table-layout: fixed; font-size: 12px; }
table.breakdown th:first-child, table.breakdown td:first-child { width: auto; overflow-wrap: break-word; hyphens: auto; }
table.breakdown th.num, table.breakdown td.num { width: 18%; font-size: 11.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
table.breakdown th { font-size: 11px; }

/* filter bar with advanced search */
.filters-bar { display: flex; flex-direction: column; gap: 10px; }
.filters label.grow { flex: 1 1 240px; }
.filters label.grow input { width: 100%; }
.filters input[type=time] { width: 110px; }
details.adv { border-top: 1px solid var(--line); padding-top: 10px; }
details.adv > summary { cursor: pointer; font-weight: 600; font-size: 13px; color: var(--muted); list-style: none; display: flex; align-items: center; gap: 6px; }
details.adv > summary::-webkit-details-marker { display: none; }
details.adv > summary::before { content: '▸'; transition: transform .15s; }
details.adv[open] > summary::before { transform: rotate(90deg); }
.badge { background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 11px; line-height: 18px; }
.adv-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px 12px; margin-top: 10px; }
.adv-grid label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); min-width: 0; }
.adv-grid select, .adv-grid input { width: 100%; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-filter { display: inline-flex; align-items: center; gap: 4px; background: var(--accent-soft); border-radius: 14px; padding: 2px 4px 2px 10px; font-size: 12px; }
.chip-filter button { border: 0; background: transparent; padding: 0 6px; font-size: 15px; line-height: 1; color: var(--muted); }
.chip-filter button:hover { color: var(--danger); }
@media (max-width: 800px) { .adv-grid { grid-template-columns: 1fr; } }

/* dropdown menu (print formats) */
details.menu { position: relative; }
details.menu > summary { list-style: none; }
details.menu > summary::-webkit-details-marker { display: none; }
.btn { display: inline-block; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.menu-list { position: absolute; right: 0; top: calc(100% + 4px); z-index: 4; background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, .12); display: flex; flex-direction: column; padding: 4px; min-width: 180px; }
.menu-list button { border: 0; text-align: left; background: transparent; }
.menu-list button:hover { background: var(--accent-soft); }

/* metrics refresh progress */
.job-bar { background: var(--accent-soft); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 12px; font-size: 13px; }
.progress { height: 6px; background: var(--panel); border-radius: 3px; margin-top: 8px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); transition: width .4s; }

/* print: layout applies while body.printing is set (before and during the print dialog),
   so blocks can be measured and fitted to the page; @media print only hides the screen UI */
.print-head { display: none; }
body.printing .shell { display: block; }
body.printing main { width: var(--print-w); padding: 0; }
body.printing .sidebar, body.printing .menu-toggle, body.printing .no-print, body.printing .filters-bar,
body.printing .job-bar, body.printing .tip, body.printing .chart-head .seg, body.printing .chart-head button,
body.printing main > #view > .row { display: none !important; }
body.printing .print-head { display: block; margin-bottom: 8px; font-size: 12px; line-height: 1.45; }
body.printing .print-title { font-size: 18px; font-weight: 700; margin-bottom: 2px; }
body.printing .panel { box-shadow: none; padding: 12px; }
body.printing #view .stack { display: block; }
body.printing #view .stack > * + * { margin-top: 10px; }
body.printing .kpis { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 8px; }
body.printing .kpi { padding: 10px 12px; }
body.printing .kpi .value { font-size: 20px; }
body.printing .grid { gap: 10px; }
body.printing .grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
body.printing .grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
body.printing .plot { height: 200px; }
/* compact breakdown rows and smaller donuts */
body.printing .donut { margin: 0 0 6px; }
body.printing .donut svg { width: 150px; height: 150px; }
body.printing .donut .center b { font-size: 15px; }
body.printing table.breakdown { font-size: 11px; }
body.printing table.breakdown td, body.printing table.breakdown th { padding: 3px 2px; line-height: 1.25; }
body.printing table.breakdown td.num, body.printing table.breakdown th.num { width: 16%; font-size: 9.5px; overflow: visible; text-overflow: clip; }
body.printing table.breakdown td:first-child { overflow-wrap: normal; hyphens: manual; }
body.printing .top-item { padding: 5px 0; cursor: default; }
/* narrow pages (A4 portrait): one breakdown per row, donut beside its table */
body.printing.print-narrow .grid.three { display: block; }
body.printing.print-narrow .grid.three > .panel + .panel { margin-top: 10px; }
body.printing.print-narrow .grid.three .panel { display: grid; grid-template-columns: 170px 1fr; column-gap: 16px; align-items: start; }
body.printing.print-narrow .grid.three .panel > h2, body.printing.print-narrow .grid.three .panel > .muted { grid-column: 1 / -1; }
/* whole blocks never split across pages: they move to the next page, or are scaled to fit (see printReport) */
body.printing .print-block { break-inside: avoid; page-break-inside: avoid; }
@media print {
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  body { background: #fff; }
  .toast, dialog { display: none !important; }
  a { color: inherit; }
  /* the printed copy fills the sheet's width exactly; the chart scales with it */
  body.printing main { width: auto; }
  body.printing .plot { height: auto; }
  body.printing .plot svg { width: 100%; height: auto; }
}

/* drill-down crumbs */
.crumbs { font-size: 12px; margin-top: 4px; overflow-wrap: anywhere; }
.crumbs a { color: var(--accent); }
.small-btn { padding: 3px 10px; font-size: 12px; }

/* feed section: clearly separated from the filters */
.feed-section { margin-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.feed-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 10px 16px;
  background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: var(--radius); }
.feed-head h2 { margin: 0; }
.feed-head .count { background: var(--accent-soft); color: var(--accent); border-radius: 12px; padding: 1px 10px; font-weight: 600; font-size: 13px; }
.feed-head .pager { margin: 0; }

/* post card v2: head (who + cluster tags top right), text section, metrics box, actions */
.post-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.post-head .who { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; min-width: 0; font-size: 13px; }
.post-head .who > a, .post-head .who > span:not(.tag) { font-weight: 600; color: var(--text); }
.post-head .when { flex-basis: 100%; }
.post-head .clusters { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; max-width: 45%; }
.tag.ptype.person { background: color-mix(in srgb, var(--s5) 18%, transparent); color: color-mix(in srgb, var(--s5) 70%, var(--text)); }
.tag.ptype.community { background: color-mix(in srgb, var(--s3) 18%, transparent); color: color-mix(in srgb, var(--s3) 70%, var(--text)); }
.post .section-title { margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--line); font-size: 11px; font-weight: 600;
  letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.post .text { margin: 4px 0 10px; }
.metrics-box { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.metrics-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.metrics-row .m { display: flex; align-items: center; gap: 8px; }
.metrics-row .mi { font-size: 16px; width: 22px; text-align: center; }
.metrics-row .m b { display: block; font-variant-numeric: tabular-nums; }
.metrics-row .m .small { display: block; line-height: 1.1; }
.metrics-meta { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); }
.post-actions { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; margin-top: 10px; font-size: 13px; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; font-size: 13px; }
.link-btn:hover { text-decoration: underline; }
@media (max-width: 700px) {
  .post-head { flex-direction: column; }
  .post-head .clusters { max-width: none; justify-content: flex-start; }
  .metrics-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
.filters label.check { flex-direction: row; align-items: center; gap: 6px; color: var(--text); font-size: 13px; }
.sources-table th.sorted { color: var(--accent); }
.sources-table td { font-size: 13px; }

/* themed suggestion list (replaces the browser's datalist popup) */
.suggest-wrap { position: relative; display: block; }
.suggest-wrap input { width: 100%; }
.suggest { position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 6; margin: 0; padding: 4px; list-style: none;
  background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .14); max-height: 280px; overflow: auto; }
.suggest li { padding: 7px 10px; border-radius: 6px; cursor: pointer; font-size: 13px; }
.suggest li.active, .suggest li:hover { background: var(--accent-soft); color: var(--accent); }
.suggest li.none { color: var(--muted); cursor: default; background: none; }

/* MAX account connect */
.account-box { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin: 8px 0 12px;
  display: flex; flex-direction: column; gap: 8px; }
.qr-wrap { display: flex; gap: 14px; align-items: flex-start; flex-wrap: wrap; }
.qr { width: 190px; height: 190px; background: #fff; padding: 6px; border-radius: 8px; flex: none; }
.qr svg { width: 100%; height: 100%; }
.qr-wrap ol { margin: 0; padding-left: 18px; max-width: 320px; }

/* Telegram accounts registry (wide dialog) */
dialog.wide { max-width: 1180px; }
dialog.wide .body { max-height: 80vh; }
.tg-registry .field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.tg-registry .field span { color: var(--muted); }
.tg-registry .field input, .tg-registry .field textarea { width: 100%; }
.tg-registry td { vertical-align: top; }
.tg-registry code { user-select: all; }
button.danger { color: var(--error, #c0392b); }

/* service notifications */
.bell { display: block; width: 100%; margin: 6px 0; text-align: left; }
.bell.alert { border-color: var(--error, #c0392b); color: var(--error, #c0392b); font-weight: 600; }
.notes { position: fixed; right: 16px; bottom: 16px; z-index: 50; display: flex; flex-direction: column; gap: 8px; width: min(380px, calc(100vw - 32px)); }
.note { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--muted); border-radius: 8px; padding: 10px 12px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .18); display: flex; flex-direction: column; gap: 4px; }
.note.inline { box-shadow: none; }
.note.error { border-left-color: var(--error, #c0392b); }
.note.warn { border-left-color: #d4a017; }
.note.ok { border-left-color: #2e9e5b; }
.note.info { border-left-color: var(--accent); }
.note.resolved { opacity: .65; }
.note-title { font-weight: 600; }

/* sources registry editing */
tr.off td { opacity: .55; }
.sel-bar { position: sticky; top: 0; z-index: 5; background: var(--panel); border: 1px solid var(--accent); border-radius: 8px; padding: 8px 12px; }
.sel-bar[hidden] { display: none; }
