/*!
 * Lead Dashboard — стили. Все селекторы под префиксом .ld, чтобы встраиваться в любой проект.
 * Высота задаётся переменной --ld-height (по умолчанию 100vh): .ld { --ld-height: 800px; }
 */

.ld {
  --ld-bg: #f4f7fb;
  --ld-card: #ffffff;
  --ld-card-2: #f8fafd;
  --ld-line: #e6ebf3;
  --ld-text: #1b2437;
  --ld-muted: #7b869b;
  --ld-primary: #1f6bff;
  --ld-primary-soft: #e8f0ff;
  --ld-good: #12a150;
  --ld-good-soft: #e5f7ec;
  --ld-bad: #e5484d;
  --ld-bad-soft: #fdeceb;
  --ld-shadow: 0 1px 2px rgba(20, 35, 70, .04), 0 4px 14px rgba(20, 35, 70, .04);
  --ld-radius: 12px;

  --ld-yandex: #2f80ed;
  --ld-vk: #8b5cf6;
  --ld-email: #22b45e;
  --ld-ivr: #f5a524;
  --ld-ai_sender: #ec4899;
  --ld-other: #94a3b8;

  --ld-st-1: #3b82f6;
  --ld-st-2: #22c55e;
  --ld-st-3: #8b5cf6;
  --ld-st-4: #ef4444;
  --ld-st-5: #14b8a6;

  position: relative;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 12px;
  height: var(--ld-height, 100vh);
  min-height: 560px;
  padding: 14px 18px;
  overflow: hidden;
  background: var(--ld-bg);
  color: var(--ld-text);
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.ld[data-theme="dark"] {
  color-scheme: dark;
  --ld-bg: #0b1226;
  --ld-card: #121b36;
  --ld-card-2: #16213f;
  --ld-line: #223056;
  --ld-text: #e8edfb;
  --ld-muted: #8a97b8;
  --ld-primary: #4c8dff;
  --ld-primary-soft: #1a2c58;
  --ld-good: #34d27b;
  --ld-good-soft: #123a2a;
  --ld-bad: #ff6b70;
  --ld-bad-soft: #3d1b27;
  --ld-shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
}

.ld *, .ld *::before, .ld *::after { box-sizing: border-box; }
.ld button, .ld input, .ld select { font: inherit; color: inherit; }
.ld [hidden] { display: none !important; }

/* ---------- Шапка и фильтры ---------- */
.ld-head { flex: none; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px 16px; }
.ld-title { display: flex; align-items: baseline; gap: 4px 14px; flex-wrap: wrap; min-width: 0; }
.ld-title h1 { margin: 0; font-size: 22px; line-height: 1.2; font-weight: 700; letter-spacing: -.01em; }
.ld-title p { margin: 0; color: var(--ld-muted); font-size: 12px; }
.ld-actions { display: flex; align-items: center; gap: 8px; }
.ld-filters { flex: 1 0 100%; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.ld-input, .ld-select {
  height: 34px; padding: 0 10px; border: 1px solid var(--ld-line); border-radius: 8px;
  background: var(--ld-card); outline: none; transition: border-color .15s, box-shadow .15s;
}
.ld-input:focus, .ld-select:focus { border-color: var(--ld-primary); box-shadow: 0 0 0 3px var(--ld-primary-soft); }
.ld-select { padding-right: 26px; cursor: pointer; min-width: 128px; }
.ld[data-theme="dark"] .ld-input { color-scheme: dark; }

.ld-seg { display: inline-flex; padding: 3px; gap: 2px; border: 1px solid var(--ld-line); border-radius: 9px; background: var(--ld-card); }
.ld-seg button {
  height: 26px; padding: 0 11px; border: 0; border-radius: 6px; background: transparent;
  color: var(--ld-muted); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.ld-seg button:hover { color: var(--ld-text); }
.ld-seg button.is-active { background: var(--ld-primary-soft); color: var(--ld-primary); font-weight: 600; }
.ld-seg.is-small button { height: 22px; padding: 0 8px; font-size: 12px; }

.ld-dates { display: flex; align-items: center; gap: 4px; }
.ld-dates .ld-input { width: 128px; padding: 0 8px; }
.ld-dates i { color: var(--ld-muted); font-style: normal; }

.ld-btn {
  height: 34px; padding: 0 14px; border: 1px solid var(--ld-line); border-radius: 8px;
  background: var(--ld-card); cursor: pointer; font-weight: 600; white-space: nowrap;
  transition: background .15s, border-color .15s, transform .05s;
}
.ld-btn:hover { border-color: var(--ld-primary); }
.ld-btn:active { transform: translateY(1px); }
.ld-btn.is-primary { background: var(--ld-primary); border-color: var(--ld-primary); color: #fff; }
.ld-btn.is-primary:hover { filter: brightness(1.08); }
.ld-btn.is-icon { width: 34px; padding: 0; font-size: 15px; }
.ld-btn.is-danger { color: var(--ld-bad); }
.ld-btn.is-small { height: 28px; padding: 0 10px; font-size: 12px; }

/* ---------- Карточки KPI ---------- */
.ld-kpis { flex: none; display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 12px; }
.ld-card {
  background: var(--ld-card); border: 1px solid var(--ld-line); border-radius: var(--ld-radius);
  box-shadow: var(--ld-shadow);
}
.ld-kpi { padding: 11px 14px 10px; min-width: 0; }
.ld-kpi-label { color: var(--ld-muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ld-kpi-value { margin-top: 2px; font-size: 24px; line-height: 1.15; font-weight: 700; letter-spacing: -.02em; white-space: nowrap; }
.ld-kpi-value small { font-size: 14px; font-weight: 600; color: var(--ld-muted); margin-left: 2px; }
.ld-kpi-foot { display: flex; align-items: center; gap: 6px; margin-top: 3px; min-height: 17px; color: var(--ld-muted); font-size: 11.5px; white-space: nowrap; overflow: hidden; }
.ld-delta { font-weight: 600; font-size: 11.5px; }
.ld-delta.is-good { color: var(--ld-good); }
.ld-delta.is-bad { color: var(--ld-bad); }
.ld-delta.is-flat { color: var(--ld-muted); }

/* ---------- Сетка панелей ---------- */
.ld-mid {
  flex: 0 0 clamp(200px, 29vh, 310px); min-height: 0;
  display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(0, 1.05fr) minmax(0, 1.15fr); gap: 12px;
}
.ld-bottom { flex: 1 1 0; min-height: 0; display: grid; grid-template-columns: minmax(0, 1.08fr) minmax(0, 1fr); gap: 12px; }
.ld.is-summary .ld-bottom { grid-template-columns: minmax(0, 1fr); }
.ld.is-summary [data-role="leads-panel"] { display: none; }

.ld-panel { display: flex; flex-direction: column; min-width: 0; min-height: 0; padding: 12px 14px; }
.ld-panel-head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.ld-panel-head h2 { margin: 0; font-size: 14px; font-weight: 700; white-space: nowrap; }
.ld-panel-head .ld-sub { color: var(--ld-muted); font-size: 12px; font-weight: 400; margin-left: 6px; }
.ld-panel-body { position: relative; flex: 1 1 0; min-height: 0; }

/* ---------- Графики ---------- */
.ld-chart { position: absolute; inset: 0; }
.ld-chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.ld-axis text { fill: var(--ld-muted); font-size: 10.5px; }
.ld-grid line { stroke: var(--ld-line); stroke-dasharray: 3 4; }
.ld-legend { flex: none; display: flex; flex-wrap: wrap; gap: 2px 14px; margin: -2px 0 6px; min-height: 16px; }
.ld-legend button {
  display: inline-flex; align-items: center; gap: 5px; padding: 0; border: 0; background: none;
  color: var(--ld-muted); font-size: 12px; cursor: pointer;
}
.ld-legend button.is-off { opacity: .4; text-decoration: line-through; }
.ld-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; flex: none; }
.ld-tip {
  position: absolute; z-index: 5; pointer-events: none; min-width: 120px; padding: 8px 10px;
  background: var(--ld-card); border: 1px solid var(--ld-line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(10, 20, 50, .18); font-size: 12px;
}
.ld-tip b { display: block; margin-bottom: 4px; }
.ld-tip div { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.ld-tip div span:last-child { margin-left: auto; padding-left: 12px; font-weight: 600; }

/* Донат */
.ld-donut { position: absolute; inset: 0; display: flex; align-items: center; gap: 14px; }
.ld-donut-ring { position: relative; flex: none; height: 100%; max-height: 160px; aspect-ratio: 1; }
.ld-donut-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ld-donut-ring circle { fill: none; stroke-width: 13; cursor: pointer; transition: opacity .15s, stroke-width .15s; }
.ld-donut-ring circle:hover { stroke-width: 15; }
.ld-donut-ring.has-filter circle:not(.is-on) { opacity: .28; }
.ld-donut-center { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; pointer-events: none; }
.ld-donut-center b { font-size: 22px; line-height: 1.1; }
.ld-donut-center span { color: var(--ld-muted); font-size: 11px; }
.ld-statuses { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.ld-statuses button {
  white-space: nowrap; display: flex; align-items: center; gap: 7px; padding: 4px 6px; border: 0; border-radius: 6px;
  background: transparent; text-align: left; cursor: pointer;
}
.ld-statuses button:hover, .ld-statuses button.is-active { background: var(--ld-card-2); }
.ld-statuses button.is-active { outline: 1px solid var(--ld-line); }
.ld-statuses .n { margin-left: auto; font-weight: 700; }
.ld-statuses .p { color: var(--ld-muted); min-width: 40px; text-align: right; font-size: 11.5px; }

/* Столбцы цены */
.ld-bars { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-around; gap: 4px; }
.ld-bar { display: grid; grid-template-columns: 112px minmax(0, 1fr) auto; align-items: center; gap: 8px; cursor: pointer; }
.ld-bar-name { display: flex; align-items: center; gap: 6px; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ld-bar-track { height: 16px; border-radius: 5px; background: var(--ld-card-2); overflow: hidden; }
.ld-bar-fill { height: 100%; border-radius: 5px; min-width: 3px; transition: width .35s ease; }
.ld-bar-val { min-width: 74px; text-align: right; font-weight: 700; font-size: 12.5px; white-space: nowrap; }
.ld-bar.is-best .ld-bar-val { color: var(--ld-good); }
.ld-bar.is-dim { opacity: .35; }

/* ---------- Таблицы ---------- */
.ld-table-wrap { position: absolute; inset: 0; overflow: auto; }
.ld-table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: 12.5px; }
.ld-table th {
  position: sticky; top: 0; z-index: 2; padding: 7px 8px; background: var(--ld-card-2); color: var(--ld-muted);
  font-size: 11px; font-weight: 600; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--ld-line);
}
.ld-table th:first-child, .ld-table td:first-child { text-align: left; padding-left: 10px; }
.ld-table td { padding: 8px 8px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--ld-line); font-variant-numeric: tabular-nums; }
.ld-table tbody tr:last-child td { border-bottom: 0; }
.ld-table.is-left th, .ld-table.is-left td { text-align: left; }
.ld-table.is-left th:last-child, .ld-table.is-left td:last-child { text-align: left; }
.ld-table tbody tr.is-click { cursor: pointer; }
.ld-table tbody tr.is-click:hover td { background: var(--ld-card-2); }
.ld-table tr.is-selected td { background: var(--ld-primary-soft); }
.ld-table tr.is-total td { font-weight: 700; background: var(--ld-card-2); position: sticky; bottom: 0; border-top: 1px solid var(--ld-line); }
.ld-src { display: inline-flex; align-items: center; gap: 7px; font-weight: 600; }
.ld-badge-src {
  display: inline-flex; align-items: center; justify-content: center; width: 22px; height: 22px; flex: none;
  border-radius: 6px; color: #fff; font-size: 10px; font-weight: 700; letter-spacing: -.02em;
}
.ld-best { color: var(--ld-good); font-weight: 700; }
.ld-best::after { content: " ★"; font-size: 10px; }
.ld-na { color: var(--ld-muted); }

.ld-pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600; }
.ld-pill.s1 { background: color-mix(in srgb, var(--ld-st-1) 16%, transparent); color: var(--ld-st-1); }
.ld-pill.s2 { background: color-mix(in srgb, var(--ld-st-2) 18%, transparent); color: var(--ld-good); }
.ld-pill.s3 { background: color-mix(in srgb, var(--ld-st-3) 18%, transparent); color: var(--ld-st-3); }
.ld-pill.s5 { background: color-mix(in srgb, var(--ld-st-5) 18%, transparent); color: var(--ld-st-5); }
.ld-pill.s4 { background: color-mix(in srgb, var(--ld-st-4) 16%, transparent); color: var(--ld-bad); }

.ld-empty, .ld-loading {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--ld-muted); text-align: center; padding: 12px;
}
.ld-more { padding: 10px; text-align: center; color: var(--ld-muted); }
.ld.is-loading .ld-kpis, .ld.is-loading .ld-mid { opacity: .6; transition: opacity .15s; }

/* ---------- Модальные окна ---------- */
.ld-overlay {
  position: absolute; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: rgba(10, 18, 40, .5); backdrop-filter: blur(2px);
}
.ld-modal {
  width: 100%; max-width: 560px; max-height: 100%; display: flex; flex-direction: column;
  background: var(--ld-card); border: 1px solid var(--ld-line); border-radius: 14px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
.ld-modal.is-wide { max-width: 760px; }
.ld-modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--ld-line); }
.ld-modal-head h3 { margin: 0; font-size: 16px; }
.ld-modal-body { padding: 16px 18px; overflow: auto; }
.ld-form { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.ld-form .is-full { grid-column: 1 / -1; }
.ld-form .ld-input, .ld-form .ld-select { width: 100%; }
.ld-form-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; }
.ld-form-actions .ld-msg { margin-left: auto; font-size: 12px; }
.ld-msg.is-error { color: var(--ld-bad); }
.ld-msg.is-ok { color: var(--ld-good); }
.ld-modal h4 { margin: 18px 0 8px; font-size: 13px; }
.ld-modal .ld-table th, .ld-modal .ld-table td { padding: 6px 8px; }
.ld-scroll { max-height: 220px; overflow: auto; border: 1px solid var(--ld-line); border-radius: 8px; }
.ld-code {
  display: block; padding: 10px 12px; border-radius: 8px; background: var(--ld-card-2); border: 1px solid var(--ld-line);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; word-break: break-all; user-select: all;
}
.ld-hint { color: var(--ld-muted); font-size: 12px; margin: 6px 0; }
.ld-modal ul { margin: 6px 0; padding-left: 18px; }

.ld-toast {
  position: absolute; left: 50%; bottom: 18px; z-index: 60; transform: translateX(-50%);
  padding: 9px 16px; border-radius: 9px; background: var(--ld-text); color: var(--ld-bg); font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25);
}
.ld-toast.is-error { background: var(--ld-bad); color: #fff; }

/* ---------- Адаптив: на узких экранах панели укладываются в колонку, страница скроллится ---------- */
@media (max-width: 1180px) {
  .ld { height: auto; min-height: var(--ld-height, 100vh); overflow: visible; }
  .ld-kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .ld-mid, .ld-bottom { display: flex; flex-direction: column; flex: none; }
  .ld-mid .ld-panel { height: 260px; }
  .ld-bottom .ld-panel { height: 380px; }
}
@media (max-width: 640px) {
  .ld { padding: 10px; }
  .ld-kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ld-form { grid-template-columns: 1fr; }
}
