:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface-1: #fcfcfb;
  --surface-2: #f0efeb;
  --border: #e2e1dc;
  --grid: #ebeae6;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e6f0fc;
  --danger: #c62f2e;
  --good: #1b7f4a;
  --danger-soft: #fdecec;
  --warn-soft: #fff4dc;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 2px 8px rgba(0, 0, 0, .04);
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #111110;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --border: #33332f;
    --grid: #2a2a27;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #8f8e86;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-soft: #1b2a3d;
    --danger: #e66767;
    --good: #4cc38a;
    --danger-soft: #3a1f1f;
    --warn-soft: #3a2f16;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
.topbar .wrap { display: flex; align-items: center; gap: 12px; height: 56px; }
.brand { font-weight: 650; color: var(--text-primary); display: flex; align-items: center; gap: 8px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px;
  background: linear-gradient(135deg, var(--series-2), var(--series-8));
  display: inline-block;
}
.badge {
  font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: var(--warn-soft); color: var(--text-secondary);
}

.wrap { max-width: 1680px; margin: 0 auto; padding: 0 20px; }
main.wrap { padding-top: 24px; padding-bottom: 48px; }

h1 { font-size: 24px; line-height: 1.25; margin: 0 0 4px; font-weight: 650; }
.subtitle { color: var(--text-secondary); margin: 0; }
.crumbs { font-size: 14px; color: var(--text-muted); margin-bottom: 8px; }

.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
#content > .card + .card, main > .card + .card, #content > .card { margin-top: 16px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.card-title { font-weight: 600; margin: 0; font-size: 16px; }
.card-body { padding: 18px; }
.muted { color: var(--text-muted); }
.small { font-size: 13px; }

input[type="search"], input[type="date"], select {
  font: inherit; font-size: 14px;
  color: var(--text-primary);
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-height: 36px;
}
input[type="search"] { min-width: 300px; }
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

button, .btn {
  font: inherit; font-size: 14px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  color: var(--text-primary);
  border-radius: 8px;
  padding: 7px 12px;
  min-height: 36px;
  cursor: pointer;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.05); }

/* ---------- Список счётчиков ---------- */
.counters-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.counter-card {
  display: block; padding: 16px 18px; color: inherit;
  transition: border-color .15s, transform .15s;
}
.counter-card:hover { text-decoration: none; border-color: var(--accent); transform: translateY(-1px); }
.counter-card .name { font-weight: 600; font-size: 16px; margin-bottom: 2px; display: flex; gap: 6px; align-items: center; }
.counter-card .site { color: var(--text-secondary); font-size: 14px; word-break: break-all; }
.counter-card .meta { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; font-size: 12px; }
.tag { padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-secondary); }
.tag.ok { background: var(--accent-soft); color: var(--accent-ink); }
.star { color: var(--series-4); }

/* ---------- Период ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface-1); }
.seg button { border: 0; border-radius: 0; border-right: 1px solid var(--border); min-height: 34px; }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.dates { display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; }

/* ---------- KPI ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin: 16px 0; }
.kpi { padding: 14px 18px; }
.kpi .label { font-size: 13px; color: var(--text-secondary); }
.kpi .value { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; margin-top: 2px; }

/* ---------- График ---------- */
.chart-box { position: relative; height: 340px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 12px; font-size: 13px; color: var(--text-secondary); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 12px; height: 3px; border-radius: 2px; display: inline-block; }
.chart-empty { position: absolute; inset: 0; display: grid; place-items: center; color: var(--text-muted); text-align: center; padding: 20px; }

/* ---------- Таблица ---------- */
.table-wrap { overflow-x: auto; overflow-y: hidden; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--grid); white-space: nowrap; }
td.goal-name { white-space: normal; min-width: 220px; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-muted); font-weight: 600; background: var(--surface-1); position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text-primary); }
th .arrow { opacity: .8; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
.chk { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.chk input { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; border: 1px solid var(--border); }
.bar-cell { display: flex; align-items: center; gap: 8px; justify-content: flex-end; }
.bar { height: 6px; border-radius: 3px; background: var(--accent); opacity: .35; min-width: 1px; }
.goal-id { color: var(--text-muted); font-size: 12px; }

/* Условие цели */
td.goal-cond { white-space: normal; min-width: 220px; max-width: 420px; font-size: 13px; color: var(--text-secondary); }
.cond + .cond { margin-top: 4px; }
.cond-label { color: var(--text-muted); }
.cond-step { color: var(--text-primary); }
.cond-or { font-size: 11px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-muted); font-weight: 600; margin-right: 2px; }
.cond code {
  font: 12px/1.4 ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  background: var(--surface-2); color: var(--text-primary);
  padding: 1px 6px; border-radius: 4px; overflow-wrap: anywhere;
}

/* График под строкой цели — на всю ширину таблицы */
tr.goal-row.has-chart td { border-bottom: 0; padding-bottom: 4px; }
tr.goal-chart-row td { padding: 0 14px 14px; border-bottom: 1px solid var(--border); }
tbody tr.goal-chart-row:hover { background: transparent; }
.goal-chart { position: relative; height: 130px; }
.goal-chart .chart-empty { font-size: 13px; }

/* ---------- Состояния ---------- */
.alert { padding: 12px 16px; border-radius: 8px; margin: 12px 0; font-size: 14px; }
.alert.error { background: var(--danger-soft); color: var(--danger); }
.alert.info { background: var(--warn-soft); color: var(--text-secondary); }
.empty { padding: 40px 20px; text-align: center; color: var(--text-muted); }
.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--grid), var(--surface-2)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 6px; height: 14px; }
@keyframes sk { from { background-position: 200% 0; } to { background-position: -200% 0; } }
.loading-veil { opacity: .5; pointer-events: none; transition: opacity .15s; }

@media (max-width: 640px) {
  .wrap { padding: 0 16px; }
  input[type="search"] { min-width: 0; width: 100%; }
  .chart-box { height: 260px; }
  tr.step-chart-row td { padding-left: 14px; }
  .goal-chart { height: 140px; position: sticky; left: 14px; width: calc(100vw - 62px); }
  .seg { display: flex; width: 100%; overflow-x: auto; }
  .seg button { flex: 0 0 auto; white-space: nowrap; }
  .dates { width: 100%; }
  .dates input[type="date"] { flex: 1 1 130px; min-width: 0; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 12px 14px; }
  .kpi .value { font-size: 20px; }
  .card-head .toolbar { width: 100%; }
  .card-head .toolbar input { flex: 1; }
  h1 { font-size: 20px; }
}

/* ---------- Шаги составных целей ---------- */
.steps-toggle {
  margin-top: 6px; min-height: 26px; padding: 2px 8px; font-size: 12px;
  color: var(--accent-ink); background: var(--accent-soft); border-color: transparent;
}
.steps-toggle:hover { background: var(--accent-soft); filter: brightness(.97); }
tr.step-row td { background: var(--surface-2); padding-top: 8px; padding-bottom: 8px; font-size: 13px; }
tr.step-row.has-chart td { border-bottom: 0; padding-bottom: 2px; }
tr.goal-row.steps-open td { border-bottom: 1px solid var(--grid); }
tr.step-chart-row td { background: var(--surface-2); padding-left: 64px; border-bottom: 1px solid var(--grid); }
tbody tr.step-row:hover td { background: var(--grid); }
td.step-name { padding-left: 28px; }
.step-name-inner { display: flex; gap: 10px; align-items: flex-start; }
.step-num {
  flex: 0 0 auto; width: 22px; height: 22px; border-radius: 50%;
  display: inline-grid; place-items: center; font-size: 12px; font-weight: 600;
  background: var(--surface-1); color: var(--text-secondary); border: 1px solid var(--border);
}
.bar.step { opacity: .2; }

/* ---------- Сворачивание общего графика ---------- */
.collapse-btn { min-height: 30px; padding: 4px 10px; font-size: 13px; color: var(--text-secondary); display: inline-flex; align-items: center; gap: 4px; }
.collapse-icon { display: inline-block; width: 12px; text-align: center; }
#chart-card.collapsed .card-head { border-bottom: 0; }

/* ================= Своя Метрика: раскладка с боковым меню ================= */
.wrap.app { display: grid; grid-template-columns: 236px minmax(0, 1fr); gap: 24px; align-items: start; padding-top: 20px; padding-bottom: 48px; }
.app > .content { min-width: 0; }
.sidenav { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; padding-bottom: 16px; }
.nav-mobile { display: none; }
.nav-group { margin-bottom: 14px; }
.nav-title { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); font-weight: 600; padding: 4px 10px; }
.nav-item { display: block; padding: 6px 10px; border-radius: 6px; color: var(--text-secondary); font-size: 14px; line-height: 1.35; }
.nav-item:hover { background: var(--surface-2); color: var(--text-primary); text-decoration: none; }
.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }

/* Вкладки отчётов раздела */
.tabs { display: flex; flex-wrap: wrap; gap: 4px; margin: -4px 0 14px; border-bottom: 1px solid var(--border); }
.tab { padding: 8px 12px; color: var(--text-secondary); font-size: 14px; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab:hover { color: var(--text-primary); text-decoration: none; }
.tab.active { color: var(--text-primary); border-bottom-color: var(--accent); font-weight: 600; }
#period + #error, #period { margin-bottom: 0; }
#content > .card:first-child { margin-top: 16px; }

/* Древовидная таблица отчёта */
.report-table th.dim-col, .report-table td.dim-col { min-width: 280px; white-space: normal; }
.report-table td.num, .report-table th.num { white-space: nowrap; }
.report-table th.num { white-space: normal; min-width: 90px; vertical-align: bottom; }
.tree-cell { display: flex; align-items: flex-start; gap: 6px; }
.tree-toggle { min-height: 22px; width: 22px; padding: 0; font-size: 12px; line-height: 1; flex: 0 0 auto; color: var(--text-secondary); }
.tree-spacer { display: inline-block; width: 22px; flex: 0 0 auto; }
.dim-value { word-break: break-word; padding-top: 1px; }
.totals-row td { font-weight: 600; background: var(--surface-2); }
tr.lvl-1 td, tr.lvl-2 td, tr.lvl-3 td, tr.lvl-4 td, tr.lvl-5 td { font-size: 13px; }
tr.lvl-1 td.dim-col, tr.lvl-2 td.dim-col, tr.lvl-3 td.dim-col { color: var(--text-secondary); }
.more-row td { padding-top: 4px; padding-bottom: 8px; }
.link-btn { border: 0; background: none; color: var(--accent-ink); padding: 0; min-height: 0; font-size: 13px; }
.link-btn:hover { background: none; text-decoration: underline; }
.table-foot { padding: 12px 18px; border-top: 1px solid var(--grid); }
.table-foot:empty { display: none; }

/* Сравнение сегментов */
.compare-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.segment-card .card-body { display: grid; gap: 10px; }
.segment-badge { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 6px; font-weight: 700; font-size: 13px; color: #fff; }
.segment-badge.a { background: var(--series-1); }
.segment-badge.b { background: var(--series-2); }
.field { display: grid; gap: 4px; font-size: 13px; color: var(--text-secondary); }
.field input[type="text"] { font: inherit; font-size: 14px; color: var(--text-primary); background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-height: 36px; width: 100%; }
.field-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.metric-checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.cmp-table th.subhead { text-align: right; font-size: 11px; position: static; }
#kpis { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }
.cmp-table th.group { text-align: center; border-left: 1px solid var(--grid); }
.cmp-table td.a-col, .cmp-table th.a-col { border-left: 1px solid var(--grid); }
.delta { font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.delta.up { color: var(--good); }
.delta.down { color: var(--danger); }
.delta.flat { color: var(--text-muted); }
.kpi .pair { display: flex; gap: 14px; align-items: baseline; flex-wrap: nowrap; margin-top: 4px; }
.kpi .pair .value { font-size: 20px; }
.kpi .seg-label { font-size: 11px; color: var(--text-muted); display: block; }

@media (max-width: 960px) {
  .wrap.app { grid-template-columns: 1fr; gap: 12px; }
  .sidenav { position: static; max-height: none; overflow: visible; padding: 0; }
  .nav-desktop { display: none; }
  .nav-mobile { display: grid; gap: 4px; }
  .nav-mobile select { width: 100%; }
  .compare-grid { grid-template-columns: 1fr; }
  .report-table th.dim-col, .report-table td.dim-col { min-width: 200px; }
}

/* ---------- Фильтры и настройка отчёта ---------- */
.panel-slot { margin-top: 12px; }
.panel-slot:empty { display: none; }
.filter-panel > summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 12px 18px; user-select: none;
}
.filter-panel > summary::-webkit-details-marker { display: none; }
.filter-panel[open] > summary { border-bottom: 1px solid var(--border); }
.filter-panel[open] .filter-edit { visibility: hidden; }
.filter-summary-title { font-weight: 600; font-size: 15px; }
.filter-panel.active .filter-summary-title::after { content: ''; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-left: 6px; vertical-align: middle; }
.filter-chips { display: flex; flex-wrap: wrap; gap: 6px; flex: 1; min-width: 0; }
.filter-edit { margin-left: auto; }
.chip { display: inline-flex; align-items: center; max-width: 100%; padding: 2px 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-ink); font-size: 12px; line-height: 1.5; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cond-list { display: grid; gap: 10px; margin-bottom: 12px; }
.cond-row { display: grid; grid-template-columns: minmax(180px, 240px) minmax(130px, 180px) minmax(0, 1fr) auto; gap: 8px; align-items: start; }
.cond-row select, .cond-row input[type="text"] { width: 100%; }
.cond-row input[type="text"], .values-box input[type="search"] { font: inherit; font-size: 14px; color: var(--text-primary); background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; min-height: 36px; }
.cond-remove { width: 36px; padding: 0; font-size: 18px; line-height: 1; color: var(--text-secondary); }
.values-chosen { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; min-height: 36px; }
.values-box { margin-top: 8px; border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--surface-1); }
.values-box input[type="search"] { width: 100%; min-width: 0; margin-bottom: 6px; }
.values-list { max-height: 220px; overflow-y: auto; display: grid; gap: 4px; padding: 2px; }
.values-list .chk { justify-content: flex-start; }
.toolbar-spacer { flex: 1; }
.config-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.config-title { font-weight: 600; font-size: 14px; margin-bottom: 8px; }
.config-goals { display: grid; gap: 6px; max-height: 260px; overflow-y: auto; }
.levels { display: grid; gap: 6px; }
.level-row { display: flex; align-items: center; gap: 6px; }
.level-row select { flex: 1; min-width: 0; }
.level-row button { width: 34px; padding: 0; }
@media (max-width: 760px) {
  .cond-row { grid-template-columns: 1fr 1fr auto; }
  .cond-row .cond-value { grid-column: 1 / -1; grid-row: 2; }
  .config-grid { grid-template-columns: 1fr; }
}
