/* Экран «Аналитика ключей» (server/web/keys.js). Всё — только под #tab-keys, классы с префиксом kx-:
   экран перенесён из трекера, и его стили не должны протекать в остальной Respublika.
   Токены трекера → токены Respublika: --surface → --panel, --surface-2 → --panel2, --text-2 → --text2,
   --good → --ok, --bad → --err. Глобальные правила Respublika для button/input/select/th/td рассчитаны на
   формы (высокие поля, заголовки КАПСОМ, всё влево) — здесь они переопределены: аналитике нужны компактные
   контролы и числа по правому краю. */

#tab-keys.tab-pane { max-width: 1400px; }
#tab-keys .kx { --kx-zebra: color-mix(in srgb, var(--panel2) 42%, var(--panel)); }

/* ---------- общие мелочи ---------- */
#tab-keys .kx-dim { color: var(--muted); }
#tab-keys .kx-small { color: var(--muted); font-size: 12.5px; line-height: 1.6; margin: 10px 0 0; }
#tab-keys .kx-pos { color: var(--ok); }
#tab-keys .kx-neg { color: var(--err); }
#tab-keys .kx-dash { color: var(--muted); }
#tab-keys .kx-grow { flex: 1; }
#tab-keys .kx-clamp {
  display: inline-block; max-width: 340px; overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap; vertical-align: bottom;
}
#tab-keys .kx-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; margin-bottom: 14px;
}

/* ---------- кнопки и поля (перебиваем глобальные button/input/select) ---------- */
#tab-keys .kx-btn {
  height: 32px; padding: 0 12px; font-size: 12.5px; font-weight: 600; white-space: nowrap;
  background: var(--panel2); border: 1px solid var(--border); color: var(--text2); border-radius: var(--radius-sm);
}
#tab-keys .kx-btn:hover { background: var(--panel3); border-color: var(--border2); color: var(--text); filter: none; }
#tab-keys .kx-btn.on { background: var(--panel3); border-color: var(--accent); color: var(--text); }
#tab-keys .kx-btn.primary { background: var(--accent); border-color: transparent; color: #0b0e12; }
#tab-keys .kx-btn.primary:hover { filter: brightness(1.08); }
#tab-keys .kx-btn.kx-ico { width: 34px; height: 34px; padding: 0; flex: none; }
#tab-keys input:not([type=checkbox]), #tab-keys select { height: 34px; font-size: 13px; padding: 0 10px; }
#tab-keys select { padding-right: 28px; background-position: calc(100% - 14px) 14px, calc(100% - 9px) 14px; }
#tab-keys input[type=date] { padding: 0 8px; color-scheme: dark; }
#tab-keys input[type=checkbox] { width: 15px; height: 15px; padding: 0; margin: 0; accent-color: var(--accent); }
#tab-keys input.kx-badval { border-color: var(--err); box-shadow: 0 0 0 3px rgba(255, 107, 107, .15); }

/* сегменты (период, режим): как вкладки Respublika в шапке */
#tab-keys .kx-seg {
  display: inline-flex; gap: 2px; padding: 3px; max-width: 100%; overflow-x: auto; scrollbar-width: none;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 11px;
}
#tab-keys .kx-seg::-webkit-scrollbar { display: none; }
#tab-keys .kx-seg button {
  flex: none; height: 30px; padding: 0 12px; border: 0; border-radius: 8px; white-space: nowrap;
  background: transparent; color: var(--muted); font-size: 13px; font-weight: 600;
}
#tab-keys .kx-seg button:hover { background: var(--panel3); color: var(--text2); filter: none; }
#tab-keys .kx-seg button.on { background: var(--panel3); color: var(--text); box-shadow: inset 0 0 0 1px var(--border2); }

/* ---------- верх: период и статус ---------- */
#tab-keys .kx-top { display: flex; align-items: center; gap: 10px 16px; flex-wrap: wrap; margin-bottom: 16px; }
#tab-keys .kx-period { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-width: 0; max-width: 100%; }
#tab-keys .kx-dates { display: inline-flex; align-items: center; gap: 6px; }
#tab-keys .kx-dates input { height: 36px; width: 142px; }
#tab-keys .kx-sync {
  margin-left: auto; display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0;
  color: var(--muted); font-size: 12.5px;
}
#tab-keys .kx-sync:empty { display: none; }
#tab-keys .kx-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--ok); box-shadow: 0 0 0 3px rgba(70, 209, 158, .14); }
#tab-keys .kx-dot.busy { background: var(--accent); box-shadow: 0 0 0 3px rgba(110, 168, 254, .16); animation: kx-pulse 1s ease-in-out infinite; }
#tab-keys .kx-dot.warn { background: var(--warn); box-shadow: 0 0 0 3px rgba(255, 180, 84, .16); }
#tab-keys .kx-dot.off { background: var(--err); box-shadow: 0 0 0 3px rgba(255, 107, 107, .16); }
#tab-keys .kx-sync-err {
  max-width: 440px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: help;
  color: var(--warn); background: rgba(255, 180, 84, .09); border: 1px solid rgba(255, 180, 84, .32);
  border-radius: 999px; padding: 2px 10px; font-size: 12px; font-weight: 600;
}
@keyframes kx-pulse { 50% { opacity: .35; } }

/* ---------- тело: загрузка, притушение при пересчёте ---------- */
#tab-keys .kx-body { transition: opacity .18s ease; }
#tab-keys .kx-body.kx-busy { opacity: .55; }
#tab-keys .kx-loading {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 26px 18px 22px; color: var(--text2); font-size: 14px; font-weight: 500;
}
#tab-keys .kx-spin {
  width: 16px; height: 16px; border-radius: 50%; flex: none;
  border: 2px solid var(--border2); border-top-color: var(--accent); animation: kx-spin .8s linear infinite;
}
@keyframes kx-spin { to { transform: rotate(360deg); } }
#tab-keys .kx-skel i, #tab-keys .kx-skel b, #tab-keys .kx-skel-tbl i {
  display: block; border-radius: 6px;
  background: linear-gradient(90deg, var(--panel2) 0%, var(--panel3) 50%, var(--panel2) 100%);
  background-size: 200% 100%; animation: kx-shimmer 1.3s ease-in-out infinite;
}
#tab-keys .kx-skel i { height: 10px; width: 55%; }
#tab-keys .kx-skel b { height: 22px; width: 75%; margin-top: 10px; }
#tab-keys .kx-skel-tbl { display: grid; gap: 12px; }
#tab-keys .kx-skel-tbl i { height: 14px; }
#tab-keys .kx-skel-tbl i:first-child { height: 30px; }
@keyframes kx-shimmer { from { background-position: 100% 0; } to { background-position: -100% 0; } }

/* ---------- ошибки и пустая выборка ---------- */
#tab-keys .kx-down {
  display: flex; gap: 14px; align-items: flex-start; padding: 20px 22px;
  border-color: rgba(255, 107, 107, .35); background: linear-gradient(180deg, rgba(255, 107, 107, .06), var(--panel) 70%);
}
#tab-keys .kx-down-ico {
  width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; flex: none;
  background: rgba(255, 107, 107, .14); color: var(--err);
}
#tab-keys .kx-down-ico svg { width: 20px; height: 20px; }
#tab-keys .kx-down h3 { margin: 2px 0 6px; font-size: 16px; font-weight: 700; }
#tab-keys .kx-down p { margin: 0 0 14px; color: var(--text2); font-size: 13.5px; line-height: 1.55; }
#tab-keys .kx-down p:last-child { margin-bottom: 0; }
#tab-keys .kx-empty { text-align: center; padding: 26px 20px; }
#tab-keys .kx-empty-ico {
  width: 40px; height: 40px; margin: 0 auto 10px; border-radius: 12px; display: grid; place-items: center;
  background: var(--panel3); color: var(--muted);
}
#tab-keys .kx-empty-ico svg { width: 20px; height: 20px; }
#tab-keys .kx-empty p { margin: 0 auto; max-width: 620px; color: var(--text2); font-size: 13.5px; line-height: 1.6; }
#tab-keys .kx-empty-btns { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }

/* ---------- фильтры ---------- */
#tab-keys .kx-filters-head { display: flex; align-items: center; gap: 8px 10px; flex-wrap: wrap; margin-bottom: 16px; }
#tab-keys .kx-lbl { color: var(--muted); font-size: 12.5px; margin-left: 6px; }
#tab-keys .kx-fgrid { display: grid; gap: 12px 14px; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); align-items: end; }
#tab-keys .kx-f { display: flex; flex-direction: column; gap: 6px; margin: 0; min-width: 0; color: var(--muted); font-size: 12px; font-weight: 500; }
#tab-keys .kx-f > span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tab-keys .kx-f select, #tab-keys .kx-f input { width: 100%; min-width: 0; color: var(--text); }
#tab-keys .kx-f-topic { grid-column: span 2; }
#tab-keys .kx-range { display: flex; align-items: center; gap: 6px; }
#tab-keys .kx-range input { flex: 1; }
#tab-keys .kx-note { margin: 14px 0 0; color: var(--muted); font-size: 12.5px; line-height: 1.6; }
#tab-keys .kx-note b { color: var(--text2); font-weight: 600; }

/* подсказки топика */
#tab-keys .kx-ac { position: relative; display: flex; gap: 6px; }
#tab-keys .kx-ac input { flex: 1; }
/* по ширине поля: поле бывает в правой колонке сетки, и список шире него уезжал за край экрана */
#tab-keys .kx-ac-list {
  position: absolute; z-index: 30; top: calc(100% + 6px); left: 0; right: 0;
  max-height: 360px; overflow-y: auto; padding: 4px;
  background: var(--panel2); border: 1px solid var(--border2); border-radius: 10px; box-shadow: var(--shadow);
}
#tab-keys .kx-ac-item {
  display: flex; gap: 10px; align-items: center; padding: 7px 9px; border-radius: 7px;
  color: var(--text); font-size: 13px; font-weight: 400; cursor: pointer;
}
#tab-keys .kx-ac-item .kx-clamp { flex: 1; min-width: 0; max-width: none; }
#tab-keys .kx-ac-item.on, #tab-keys .kx-ac-item:hover { background: var(--panel3); }
#tab-keys .kx-ac-item.cur { box-shadow: inset 2px 0 0 var(--accent); }
/* топик без кликов за период: пояснение — второй строкой, иначе оно съедало название */
#tab-keys .kx-ac-item.dim { opacity: .62; flex-direction: column; align-items: stretch; gap: 1px; }
#tab-keys .kx-ac-item.dim small { font-size: 11.5px; }
#tab-keys .kx-ac-sp { font-weight: 600; font-variant-numeric: tabular-nums; }
#tab-keys .kx-ac-roi { min-width: 46px; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- плашки ---------- */
#tab-keys .kx-kpis { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(176px, 1fr)); margin-bottom: 14px; }
#tab-keys .kx-kpi {
  position: relative; overflow: hidden; padding: 13px 15px 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
}
#tab-keys .kx-kpi::before {
  content: ""; position: absolute; inset: 0 0 auto 0; height: 2px; opacity: .45;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
}
#tab-keys .kx-kpi.kx-skel::before { display: none; }
#tab-keys .kx-kpi-l { color: var(--text2); font-size: 12px; font-weight: 500; }
#tab-keys .kx-kpi-v { margin-top: 3px; font-size: 22px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; white-space: nowrap; }
#tab-keys .kx-kpi-h { margin-top: 3px; color: var(--muted); font-size: 11.5px; line-height: 1.4; }

/* ---------- «Точность оценок» ---------- */
#tab-keys .kx-fold {
  display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 8px; width: 100%; height: auto; padding: 0;
  background: none; border: 0; color: var(--text); text-align: left; font-size: 14px; font-weight: 600; cursor: pointer;
}
#tab-keys .kx-fold:hover { filter: none; }
#tab-keys .kx-fold:active { transform: none; }
#tab-keys .kx-fold:hover .kx-fold-t, #tab-keys .kx-fold:hover .kx-fold-more { color: var(--accent); }
#tab-keys .kx-chev { display: inline-block; width: 12px; color: var(--muted); transition: transform .15s ease; }
#tab-keys .kx-acc.open .kx-chev { transform: rotate(90deg); }
#tab-keys .kx-fold-sum { color: var(--muted); font-weight: 400; font-size: 13px; }
#tab-keys .kx-fold-sum b { color: var(--text2); font-weight: 600; }
#tab-keys .kx-fold-sum b.kx-pos { color: var(--ok); }
#tab-keys .kx-fold-more { margin-left: auto; color: var(--muted); font-weight: 500; font-size: 12.5px; }
#tab-keys .kx-acc-body { display: grid; grid-template-columns: minmax(240px, 1fr) minmax(0, 1.7fr); gap: 18px 22px; margin-top: 16px; }
#tab-keys .kx-relbar { display: flex; height: 8px; border-radius: 999px; overflow: hidden; background: var(--panel3); }
#tab-keys .kx-relbar i { display: block; height: 100%; }
#tab-keys .r-good { background: var(--ok); }
#tab-keys .r-mid { background: var(--warn); }
#tab-keys .r-rough { background: var(--border2); }
#tab-keys .kx-rel-legend { display: flex; gap: 6px 14px; flex-wrap: wrap; margin-top: 8px; color: var(--text2); font-size: 12.5px; }
#tab-keys .kx-rel-legend i { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 6px; vertical-align: -.5px; }
#tab-keys .kx-facts { margin: 14px 0 0; padding-left: 18px; color: var(--text2); font-size: 13px; line-height: 1.6; }
#tab-keys .kx-facts li + li { margin-top: 6px; }
#tab-keys .kx-acc-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }
#tab-keys .kx-acc-tbl { font-size: 13px; }
#tab-keys .kx-acc-tbl th, #tab-keys .kx-acc-tbl td { padding: 7px 10px; text-align: right; }
#tab-keys .kx-acc-tbl th { position: static; text-transform: none; letter-spacing: 0; font-size: 12px; font-weight: 600; color: var(--text2); cursor: help; }
#tab-keys .kx-acc-tbl .t { text-align: left; }
#tab-keys .kx-meter {
  display: inline-block; width: 46px; height: 5px; margin-right: 8px; vertical-align: 2px;
  border-radius: 999px; background: var(--panel3); overflow: hidden;
}
#tab-keys .kx-meter i { display: block; height: 100%; background: linear-gradient(90deg, var(--accent), var(--ok)); }

/* ---------- «показаны не все» ---------- */
#tab-keys .kx-hbar {
  display: flex; align-items: center; gap: 8px 12px; flex-wrap: wrap; margin-bottom: 14px; padding: 9px 10px 9px 14px;
  background: rgba(255, 180, 84, .07); border: 1px solid rgba(255, 180, 84, .3); border-radius: var(--radius);
  color: var(--text2); font-size: 13px;
}
#tab-keys .kx-hbar b { color: var(--text); }
#tab-keys .kx-hbar .kx-btn { margin-left: auto; height: 30px; }

/* ---------- таблица ---------- */
#tab-keys .kx-tcard { padding-bottom: 14px; }
#tab-keys .kx-tcard-head { display: flex; align-items: baseline; gap: 4px 12px; flex-wrap: wrap; margin-bottom: 12px; }
#tab-keys .kx-tcard-head h3 { margin: 0; font-size: 15px; font-weight: 700; letter-spacing: -.1px; }
#tab-keys .kx-legend { color: var(--muted); font-size: 12.5px; }
#tab-keys .kx-tbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
#tab-keys .kx-search { position: relative; flex: 0 1 300px; min-width: 180px; }
#tab-keys .kx-search svg { position: absolute; left: 10px; top: 50%; width: 15px; height: 15px; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
#tab-keys .kx-search input { width: 100%; height: 32px; padding-left: 32px; }
#tab-keys .kx-count { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
#tab-keys .kx-more { margin-top: 10px; }

#tab-keys .kx-colpanel {
  margin-bottom: 10px; padding: 12px; border-radius: var(--radius-sm);
  background: var(--panel2); border: 1px solid var(--border);
}
#tab-keys .kx-colpanel .kx-small { margin: 0; }
#tab-keys .kx-colpanel ul {
  list-style: none; margin: 10px 0; padding: 0; display: grid; gap: 6px;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
}
#tab-keys .kx-colpanel li {
  display: flex; align-items: center; gap: 8px; padding: 6px 9px; border-radius: 7px; cursor: grab;
  background: var(--panel); border: 1px solid var(--border); font-size: 13px;
}
#tab-keys .kx-colpanel li:hover { border-color: var(--border2); }
#tab-keys .kx-colpanel label { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--text2); cursor: pointer; }
#tab-keys .kx-colpanel-foot { display: flex; gap: 8px; flex-wrap: wrap; }
#tab-keys .kx-grip { color: var(--muted); font-size: 11px; letter-spacing: -2px; user-select: none; }
#tab-keys .kx-dropto { box-shadow: inset 2px 0 0 var(--accent) !important; }

/* Своя прокрутка у таблицы: заголовок и первая колонка остаются на месте, пока листаешь 300 строк и 17 колонок. */
#tab-keys .kx-tbl-wrap {
  overflow: auto; max-height: calc(100vh - 150px);
  border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--panel);
}
#tab-keys .kx-tbl-wrap::-webkit-scrollbar-track { background: var(--panel); }
#tab-keys .kx-tbl-wrap::-webkit-scrollbar-thumb { border-color: var(--panel); }
#tab-keys .kx-tbl { width: 100%; font-size: 13px; }
#tab-keys .kx-tbl th, #tab-keys .kx-tbl td { padding: 8px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
#tab-keys .kx-tbl th {
  position: sticky; top: 0; z-index: 2; background: var(--panel2); cursor: pointer; user-select: none;
  color: var(--text2); font-size: 12px; font-weight: 600; text-transform: none; letter-spacing: 0;
}
#tab-keys .kx-tbl th:hover, #tab-keys .kx-tbl th.sorted { color: var(--text); }
#tab-keys .kx-tbl th[draggable="true"] { cursor: grab; }
#tab-keys .kx-tbl th.sorted .kx-th::after { content: "↓"; margin-left: 4px; color: var(--accent); }
#tab-keys .kx-tbl th.sorted.asc .kx-th::after { content: "↑"; }
#tab-keys .kx-tbl .t { text-align: left; }
#tab-keys .kx-tbl td { background: var(--panel); }
#tab-keys .kx-tbl td:nth-child(even) { background: var(--kx-zebra); }
#tab-keys .kx-tbl tbody tr:hover td { background: var(--panel2); }
#tab-keys .kx-tbl tbody tr:last-child td { border-bottom: 0; }
#tab-keys .kx-tbl th:first-child, #tab-keys .kx-tbl td:first-child { position: sticky; left: 0; z-index: 1; box-shadow: inset -1px 0 0 var(--border); }
#tab-keys .kx-tbl thead th:first-child { z-index: 3; }
#tab-keys .kx-tbl td:first-child .kx-clamp { max-width: min(340px, 40vw); }
#tab-keys .kx-tbl td.kx-main { font-weight: 700; color: var(--text); }
#tab-keys .kx-tbl tr.kx-click { cursor: pointer; }
#tab-keys .kx-tbl tr.kx-click:hover td:first-child { box-shadow: inset 3px 0 0 var(--accent), inset -1px 0 0 var(--border); }
#tab-keys .kx-tbl td.kx-none { text-align: center; color: var(--muted); padding: 30px 14px; }
#tab-keys .kx-tbl small { font-size: 11.5px; }
#tab-keys .kx-tbl th.cw, #tab-keys .kx-tbl td.cw { overflow: hidden; text-overflow: ellipsis; }
#tab-keys .kx-tbl td.cw .kx-clamp { max-width: 100%; }
#tab-keys .kx-rs { position: absolute; top: 0; right: -3px; bottom: 0; width: 7px; cursor: col-resize; z-index: 2; }
#tab-keys .kx-rs:hover { background: color-mix(in srgb, var(--accent) 45%, transparent); }
#tab-keys.kx-resizing, #tab-keys.kx-resizing * { cursor: col-resize !important; user-select: none !important; }

/* метки */
#tab-keys .kx-fire { cursor: help; }
#tab-keys .kx-mix { color: var(--warn); font-size: 12px; font-weight: 600; cursor: help; white-space: nowrap; }
#tab-keys .kx-geo { cursor: help; }
#tab-keys .kx-rel-good { color: var(--ok); }
#tab-keys .kx-rel-mid { color: var(--warn); }
#tab-keys .kx-rel-rough { color: var(--muted); }

/* ---------- узкий экран ---------- */
@media (max-width: 1100px) {
  /* minmax(0, …), а не 1fr: иначе ширину колонки задаёт таблица корзин, и блок вылезает за карточку */
  #tab-keys .kx-acc-body { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  #tab-keys .kx-sync { margin-left: 0; width: 100%; }
  #tab-keys .kx-f-topic { grid-column: 1 / -1; }
  #tab-keys .kx-fold-more { margin-left: 0; }
}
@media (max-width: 560px) {
  /* на телефоне сегменты переносятся: при прокрутке вбок «30/90 дней» и «Топики» просто не было видно */
  #tab-keys .kx-seg { flex-wrap: wrap; }
  #tab-keys .kx-seg button { padding: 0 10px; }
  #tab-keys .kx-card { padding: 14px; }
  #tab-keys .kx-fgrid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  #tab-keys .kx-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  #tab-keys .kx-kpi { padding: 11px 12px 10px; }
  #tab-keys .kx-kpi-v { font-size: 18px; }
  #tab-keys .kx-dates { width: 100%; }
  #tab-keys .kx-dates input { flex: 1; width: auto; min-width: 0; }
  #tab-keys .kx-search { flex: 1 1 100%; }
  #tab-keys .kx-tbl-wrap { max-height: calc(100vh - 110px); }
  #tab-keys .kx-down { flex-direction: column; }
  #tab-keys .kx-hbar .kx-btn { margin-left: 0; }
}
