:root {
  --bg: #e8edf3;
  --surface: #ffffff;
  --text: #1a2332;
  --muted: #64748b;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --accent: #0ea5e9;
  --accent-dark: #0284c7;
  --success: #10b981;
  --danger: #ef4444;
  --warn: #f59e0b;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.07);
  --radius: 12px;
  --radius-lg: 16px;
  --funnel-color: #0ea5e9;
  --space-xs: 6px;
  --space-sm: 10px;
  --space-md: 16px;
  --space-lg: 24px;
  --content-max: 1120px;
  --sticky-top: 8px;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

.app { min-height: 100vh; }

/* ——— Шапка ——— */
.hero {
  background: linear-gradient(135deg, #0c4a6e 0%, #0369a1 45%, #0ea5e9 100%);
  color: #fff;
  padding: 24px 20px 32px;
  position: relative;
  overflow: hidden;
}

.hero::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 85% 15%, rgba(255, 255, 255, .14) 0%, transparent 55%);
  pointer-events: none;
}

.hero-inner {
  position: relative;
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 0 4px;
}

.hero-badge {
  display: inline-block;
  font-size: .7rem;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  background: rgba(255, 255, 255, .16);
  padding: 4px 10px;
  border-radius: 20px;
  margin-bottom: 8px;
}

.hero h1 {
  margin: 0 0 4px;
  font-size: clamp(1.35rem, 4vw, 1.65rem);
  font-weight: 700;
  letter-spacing: -.02em;
}

.hero-sub {
  margin: 0;
  opacity: .9;
  font-size: .9rem;
}

/* ——— Контейнер ——— */
.container {
  max-width: var(--content-max);
  margin: -16px auto 48px;
  padding: 0 var(--space-md);
  position: relative;
  z-index: 1;
}

.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: var(--space-md) var(--space-lg);
  margin-bottom: var(--space-md);
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, .85);
}

.alert {
  border-left: 4px solid var(--warn);
  background: #fffbeb;
}

/* ——— Панель управления ——— */
.controls {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
  padding: var(--space-lg);
}

.controls-row {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.controls-label {
  display: block;
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  margin: 0 0 var(--space-sm);
}

.toolbar-dates-row {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

/* Вкладки Сделки / Лиды */
.view-nav {
  display: inline-flex;
  gap: 4px;
  padding: 4px;
  background: var(--border-light);
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.view-tab {
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: .875rem;
  font-weight: 600;
  padding: 9px 20px;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  transition: background .15s, color .15s, box-shadow .15s;
}

.view-tab:hover:not(.active) {
  color: var(--text);
  background: rgba(255, 255, 255, .6);
}

.view-tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.view-tab[data-view="deals"].active {
  color: var(--accent-dark);
}

.view-tab[data-view="leads"].active {
  color: #6d28d9;
}

/* Воронки */
.funnel-wrap {
  transition: opacity .2s;
}

.funnel-wrap.funnel-hidden {
  opacity: .5;
  pointer-events: none;
}

.funnel-wrap.funnel-hidden .funnel-nav {
  filter: grayscale(.3);
}

.funnel-wrap.funnel-hidden::after {
  content: 'Только для сделок';
  display: block;
  font-size: .72rem;
  color: var(--muted);
  margin-top: var(--space-xs);
}

.funnel-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.funnel-tab {
  border: 1px solid var(--border);
  cursor: pointer;
  font-family: inherit;
  font-size: .8125rem;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  transition: all .15s ease;
}

.funnel-tab:hover:not(.active) {
  color: var(--text);
  border-color: #cbd5e1;
  background: var(--border-light);
}

.funnel-tab.active {
  color: #fff;
  background: var(--tab-color, var(--accent));
  border-color: var(--tab-color, var(--accent));
  box-shadow: 0 3px 12px color-mix(in srgb, var(--tab-color, var(--accent)) 40%, transparent);
}

/* Фильтры периода */
.toolbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: end;
  gap: var(--space-md) var(--space-lg);
  margin: 0;
  padding-top: var(--space-sm);
  border-top: 1px solid var(--border);
}

.toolbar-block {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.toolbar-dates {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.toolbar-actions {
  align-items: flex-end;
}

.period-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.preset {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-family: inherit;
  font-size: .8125rem;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}

.preset:hover {
  color: var(--text);
  border-color: #cbd5e1;
}

.preset.active {
  background: color-mix(in srgb, var(--funnel-color) 14%, #fff);
  border-color: color-mix(in srgb, var(--funnel-color) 45%, var(--border));
  color: var(--text);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-label {
  font-size: .7rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.field input[type="date"] {
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .9rem;
  font-family: inherit;
  background: var(--surface);
  min-width: 142px;
  color: var(--text);
}

.field input:focus {
  outline: none;
  border-color: var(--funnel-color);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--funnel-color) 20%, transparent);
}

.btn-primary {
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  font-size: .875rem;
  padding: 10px 20px;
  border-radius: 8px;
  background: linear-gradient(180deg, var(--funnel-color) 0%, color-mix(in srgb, var(--funnel-color) 85%, #000) 100%);
  color: #fff;
  box-shadow: 0 3px 12px color-mix(in srgb, var(--funnel-color) 35%, transparent);
  transition: transform .12s, box-shadow .12s, opacity .12s;
  white-space: nowrap;
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 5px 16px color-mix(in srgb, var(--funnel-color) 42%, transparent);
}

.btn-primary:disabled {
  opacity: .6;
  cursor: wait;
  transform: none;
}

/* ——— Статус загрузки ——— */
.status {
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-bottom: var(--space-md);
  font-size: .875rem;
  font-weight: 500;
}

.status.loading {
  background: #e0f2fe;
  color: #0369a1;
  display: flex;
  align-items: center;
  gap: 10px;
}

.status.loading::before {
  content: '';
  width: 16px;
  height: 16px;
  border: 2px solid #0369a1;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .65s linear infinite;
  flex-shrink: 0;
}

@keyframes spin { to { transform: rotate(360deg); } }

.status.error {
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecaca;
}

.hidden { display: none !important; }

/* ——— Мета отчёта ——— */
.report {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.report-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  font-size: .875rem;
  color: var(--muted);
}

.report-meta strong { color: var(--text); }

.report-meta .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--funnel-color);
}

/* ——— Липкая навигация ——— */
.section-nav {
  position: sticky;
  top: var(--sticky-top);
  z-index: 30;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  margin-bottom: var(--space-md);
  background: rgba(255, 255, 255, .95);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.nav-label {
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--muted);
  margin-right: 2px;
  flex-shrink: 0;
}

.section-tag {
  border: 1px solid var(--border);
  background: var(--surface);
  font-family: inherit;
  font-size: .75rem;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 999px;
  cursor: pointer;
  color: var(--muted);
  transition: all .12s;
  white-space: nowrap;
}

.section-tag:hover {
  border-color: var(--funnel-color);
  color: var(--text);
}

.section-tag.active {
  background: var(--funnel-color);
  border-color: var(--funnel-color);
  color: #fff;
}

/* ——— Секции отчёта ——— */
.sections-grid {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.report-section {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
  scroll-margin-top: 56px;
}

.section-head {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border: none;
  background: var(--border-light);
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: background .12s;
}

.section-head:hover { background: #eef2f7; }

.section-icon {
  font-size: 1.1rem;
  line-height: 1;
  flex-shrink: 0;
}

.section-title-text {
  flex: 1;
  font-size: .95rem;
  font-weight: 700;
  color: var(--text);
}

.chevron {
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform .2s;
  margin-right: 2px;
}

.report-section.collapsed .chevron { transform: rotate(-45deg); }

.report-section.collapsed .section-body { display: none; }

.section-body {
  padding: var(--space-md) var(--space-lg) var(--space-lg);
}

.section-body > :first-child {
  margin-top: 0;
}

/* ——— KPI ——— */
.kpi-row-hero {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.kpi-card {
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--border-light);
  border: 1px solid var(--border);
}

.kpi-card.highlight {
  background: linear-gradient(145deg, color-mix(in srgb, var(--funnel-color) 10%, #fff) 0%, #fff 100%);
  border-color: color-mix(in srgb, var(--funnel-color) 22%, var(--border));
}

.kpi-card .value {
  display: block;
  font-size: clamp(1.25rem, 3vw, 1.5rem);
  font-weight: 700;
  letter-spacing: -.02em;
  line-height: 1.2;
  word-break: break-word;
}

.kpi-card .label {
  display: block;
  font-size: .75rem;
  color: var(--muted);
  margin-top: 4px;
  font-weight: 500;
}

.metric-inline {
  font-size: .95rem;
  margin: var(--space-md) 0 0;
  padding-top: var(--space-md);
  border-top: 1px solid var(--border);
}

.metric-inline strong {
  font-size: 1.35rem;
  color: var(--funnel-color);
  font-weight: 700;
}

/* ——— Воронка (конверсия) ——— */
.funnel-viz {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.funnel-viz-steps {
  margin: 0;
  padding: 0;
}

.funnel-viz-steps .funnel-step {
  display: grid;
  grid-template-columns: minmax(120px, 32%) 1fr 44px minmax(64px, 80px);
  gap: 14px;
  align-items: center;
  padding: 14px 16px;
  background: var(--border-light);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: .875rem;
  min-height: 0;
}

.funnel-step .name {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  line-height: 1.3;
  min-width: 0;
}

.funnel-step .name span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
}

.stage-dot {
  flex-shrink: 0;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  box-shadow: 0 0 0 2px #fff;
}

.funnel-bar-wrap {
  height: 32px;
  background: #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
  min-width: 0;
}

.funnel-bar {
  height: 100%;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--step-color, var(--funnel-color)), color-mix(in srgb, var(--step-color, var(--funnel-color)) 70%, #fff));
  min-width: 4px;
  transition: width .45s ease;
}

.funnel-step .cnt {
  font-weight: 700;
  text-align: right;
  font-size: .95rem;
  font-variant-numeric: tabular-nums;
}

.funnel-step .pct {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 1px;
  line-height: 1.2;
}

.funnel-step .pct-main {
  font-weight: 700;
  font-size: .85rem;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.funnel-step .pct-sub {
  font-size: .68rem;
  color: var(--muted);
}

/* ——— Бары (источники) ——— */
.sub-title {
  margin: 20px 0 12px;
  font-size: .75rem;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .04em;
}

.sub-title:first-child { margin-top: 0; }

.bar-chart {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 8px;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(100px, 30%) 1fr auto;
  gap: 12px;
  align-items: center;
  font-size: .85rem;
}

.bar-label {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.bar-track {
  height: 26px;
  background: #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
  min-width: 0;
}

.bar-fill {
  height: 100%;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--funnel-color), color-mix(in srgb, var(--funnel-color) 68%, #fff));
  min-width: 3px;
  transition: width .35s ease;
}

.bar-val {
  font-weight: 700;
  font-size: .8rem;
  color: var(--muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* ——— Облако тегов ——— */
.tag-cloud {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: var(--space-md);
  padding: 14px;
  background: var(--border-light);
  border-radius: var(--radius);
  border: 1px dashed color-mix(in srgb, var(--funnel-color) 30%, var(--border));
}

.tag-cloud-item {
  border: 1px solid var(--border);
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
  transition: transform .12s, border-color .12s, background .12s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  line-height: 1.3;
}

.tag-cloud-item.tag-sm { font-size: .75rem; }
.tag-cloud-item.tag-md { font-size: .8125rem; }
.tag-cloud-item.tag-lg { font-size: .875rem; padding: 7px 14px; }

.tag-cloud-item:hover {
  transform: translateY(-1px);
  border-color: var(--funnel-color);
  background: color-mix(in srgb, var(--funnel-color) 8%, #fff);
}

.tag-val {
  font-size: .85em;
  font-weight: 700;
  color: var(--funnel-color);
  background: color-mix(in srgb, var(--funnel-color) 12%, transparent);
  padding: 1px 6px;
  border-radius: 5px;
  font-variant-numeric: tabular-nums;
}

.leads-accent .tag-cloud {
  background: #f5f3ff;
  border-color: color-mix(in srgb, #8b5cf6 28%, var(--border));
}

.leads-accent .tag-val { color: #6d28d9; }

/* ——— Таблицы ——— */
.table-wrap {
  overflow-x: auto;
  margin-top: 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  -webkit-overflow-scrolling: touch;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
}

th {
  text-align: left;
  padding: 10px 14px;
  font-weight: 600;
  color: var(--muted);
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  background: var(--border-light);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }

tbody tr:hover td { background: #f8fafc; }

td.num, th.num { text-align: right; }

td.empty {
  text-align: center;
  color: var(--muted);
  font-style: italic;
  padding: 20px;
}

.pill {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: .75rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.pill-good { background: #d1fae5; color: #047857; }
.pill-mid { background: #e0f2fe; color: #0369a1; }
.pill-low { background: #fee2e2; color: #b91c1c; }

.hint {
  margin: 0;
  font-size: .875rem;
  color: var(--muted);
  padding: 8px 0;
}

code {
  background: var(--border-light);
  padding: 2px 8px;
  border-radius: 6px;
  font-size: .85em;
  word-break: break-all;
}

/* ——— Адаптив ——— */
@media (max-width: 900px) {
  .toolbar {
    grid-template-columns: 1fr;
    gap: var(--space-md);
  }

  .toolbar-actions .btn-primary {
    width: 100%;
  }
}

@media (max-width: 700px) {
  .container {
    padding: 0 12px;
    margin-bottom: 32px;
  }

  .controls {
    padding: var(--space-md);
    gap: 14px;
  }

  .card { padding: var(--space-md); }

  .view-nav {
    display: flex;
    width: 100%;
  }

  .view-tab {
    flex: 1;
    text-align: center;
    padding: 10px 12px;
  }

  .funnel-nav { gap: 6px; }

  .funnel-tab {
    padding: 7px 12px;
    font-size: .75rem;
  }

  .section-nav {
    top: 0;
    border-radius: 0;
    margin-left: -12px;
    margin-right: -12px;
    width: calc(100% + 24px);
    border-left: none;
    border-right: none;
  }

  .section-body {
    padding: var(--space-md);
  }

  .funnel-viz-steps .funnel-step {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 12px 14px;
  }

  .funnel-step .cnt,
  .funnel-step .pct {
    text-align: left;
    align-items: flex-start;
  }

  .funnel-step .cnt { font-size: 1.1rem; }

  .bar-row {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .kpi-row-hero {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 400px) {
  .kpi-row-hero { grid-template-columns: 1fr; }

  .toolbar-dates {
    flex-direction: column;
    width: 100%;
  }

  .field input[type="date"] {
    width: 100%;
    min-width: 0;
  }
}
