* {
  box-sizing: border-box;
}

:root {
  --bg: #f4f6fb;
  --surface: rgba(255, 255, 255, 0.92);
  --surface-strong: #eef2f9;
  --text: #080f2b;
  --muted: #5d6577;
  --border: rgba(8, 15, 43, 0.12);
  --primary: #080f2b;
  --primary-strong: #121a3f;
  --accent: #ffa300;
  --success: #0f7a45;
  --warning: #b56b00;
  --danger: #c63d3d;
  --shadow: 0 22px 60px rgba(8, 15, 43, 0.12);
  --radius: 18px;
  --sidebar: 264px;
}

html {
  min-height: 100%;
  color: var(--text);
  font-family: "Montserrat", "Open Sans", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
  overscroll-behavior: contain;
}

body {
  min-height: 100%;
  margin: 0;
  position: relative;
  background:
    radial-gradient(circle at 10% 0%, rgba(255, 163, 0, 0.12), transparent 28%),
    radial-gradient(circle at 92% 12%, rgba(8, 15, 43, 0.08), transparent 24%),
    linear-gradient(180deg, #fafbfe 0%, #f1f4f9 100%);
  color: var(--text);
  overflow-x: hidden;
}

body.auth-required .app-shell,
body.auth-required .mobile-tabs {
  display: none;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(135deg, rgba(8, 15, 43, 0.05) 0%, rgba(8, 15, 43, 0) 30%),
    linear-gradient(325deg, rgba(255, 163, 0, 0.05) 0%, rgba(255, 163, 0, 0) 28%);
  opacity: 0.8;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.25);
  outline-offset: 2px;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar) minmax(0, 1fr);
  min-height: 100vh;
}

.auth-screen {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
}

.auth-screen[hidden] {
  display: none;
}

.auth-panel {
  display: grid;
  width: min(420px, 100%);
  gap: 14px;
  justify-items: start;
  border: 1px solid rgba(8, 15, 43, 0.1);
  border-radius: calc(var(--radius) + 8px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.97) 0%, rgba(247, 249, 253, 0.97) 100%);
  padding: 28px;
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}

.auth-panel::before {
  content: '';
  position: absolute;
  inset: 0 auto auto 0;
  width: 160px;
  height: 160px;
  background: radial-gradient(circle at center, rgba(255, 163, 0, 0.18), transparent 68%);
  transform: translate(-40px, -60px);
  pointer-events: none;
}

.auth-panel p {
  color: var(--muted);
  line-height: 1.45;
  max-width: 34ch;
}

.login-widget {
  min-height: 44px;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid var(--border);
  background: rgba(251, 252, 254, 0.78);
  backdrop-filter: blur(16px);
  padding: 22px 20px;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 28px;
}

.brand-mark {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border-radius: 14px;
  background: linear-gradient(145deg, var(--primary) 0%, #11183a 100%);
  color: white;
  font-weight: 700;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.brand strong,
.brand span {
  display: block;
}

.brand span {
  color: var(--muted);
  font-size: 13px;
}

.nav-tabs {
  display: grid;
  gap: 6px;
}

.nav-item,
.mobile-tab {
  min-height: 42px;
  border: 1px solid transparent;
  border-radius: 14px;
  background: transparent;
  color: var(--muted);
  text-align: left;
  padding: 0 12px;
  font-weight: 650;
  transition: background 160ms ease, color 160ms ease, border-color 160ms ease, transform 160ms ease;
}

.nav-item:hover,
.mobile-tab:hover {
  background: rgba(8, 15, 43, 0.03);
  color: var(--text);
  border-color: rgba(8, 15, 43, 0.08);
}

.nav-item.is-active,
.mobile-tab.is-active {
  background: rgba(255, 163, 0, 0.12);
  color: var(--primary);
  border-color: rgba(255, 163, 0, 0.28);
}

.workspace {
  min-width: 0;
  padding: 28px 30px 30px;
}

.topbar {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.15;
  font-weight: 760;
}

h2 {
  font-size: 20px;
  line-height: 1.25;
}

h3 {
  font-size: 15px;
  line-height: 1.35;
}

.topbar p,
.section-head p,
.hint {
  margin-top: 5px;
  color: var(--muted);
  font-size: 14px;
}

.topbar-actions,
.modal-actions,
.form-row,
.toolbar {
  display: flex;
  gap: 10px;
  align-items: center;
}

.button {
  display: inline-flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: 14px;
  padding: 0 16px;
  color: inherit;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.2;
  text-decoration: none;
  white-space: normal;
  transition: background 160ms ease, border-color 160ms ease, color 160ms ease, transform 160ms ease, box-shadow 160ms ease;
}

.button:hover {
  transform: translateY(-1px);
}

.button-primary {
  background: var(--primary);
  color: white;
  box-shadow: 0 10px 24px rgba(8, 15, 43, 0.18);
}

.button-primary:hover {
  background: var(--primary-strong);
}

.button-accent {
  background: var(--accent);
  color: #241900;
  box-shadow: 0 10px 24px rgba(255, 163, 0, 0.22);
}

.button-accent:hover {
  background: #ffb21f;
}

.button-ghost {
  border-color: rgba(8, 15, 43, 0.12);
  background: rgba(255, 255, 255, 0.92);
  color: var(--text);
}

.button-ghost:hover {
  background: rgba(8, 15, 43, 0.04);
}

.button-danger {
  border-color: rgba(220, 38, 38, 0.22);
  background: #fff5f5;
  color: var(--danger);
}

.view {
  display: none;
}

.view.is-active {
  display: block;
}

.toolbar {
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.lead-layout {
  display: grid;
  grid-template-columns: minmax(320px, 0.95fr) minmax(360px, 1.05fr);
  gap: 16px;
  align-items: start;
}

.lead-list,
.source-list {
  display: grid;
  gap: 10px;
}

.lead-card,
.source-row,
.panel,
.detail-panel,
.stat-card {
  border: 1px solid rgba(8, 15, 43, 0.1);
  border-radius: calc(var(--radius) + 2px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.98) 0%, rgba(248, 250, 253, 0.98) 100%);
  box-shadow: 0 1px 1px rgba(8, 15, 43, 0.03), 0 16px 40px rgba(8, 15, 43, 0.05);
}

.lead-card {
  display: grid;
  width: 100%;
  gap: 10px;
  padding: 14px;
  text-align: left;
  transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

.lead-card:hover,
.lead-card.is-selected {
  border-color: rgba(255, 163, 0, 0.5);
  box-shadow: var(--shadow);
  transform: translateY(-1px);
}

.lead-meta,
.source-meta,
.card-actions,
.tag-row,
.stats-row {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.lead-summary {
  color: var(--text);
  line-height: 1.45;
}

.muted {
  color: var(--muted);
}

.tag {
  display: inline-flex;
  min-height: 24px;
  align-items: center;
  border-radius: 999px;
  background: var(--surface-strong);
  color: #334155;
  padding: 0 9px;
  font-size: 12px;
  font-weight: 700;
}

.tag.success {
  background: #dcfce7;
  color: #166534;
}

.tag.warning {
  background: #fef3c7;
  color: #92400e;
}

.tag.primary {
  background: #dbeafe;
  color: #1d4ed8;
}

.tag.danger {
  background: #fee2e2;
  color: #991b1b;
}

.detail-panel {
  position: sticky;
  top: 28px;
  min-height: 520px;
  padding: 22px;
}

.detail-body {
  display: grid;
  gap: 16px;
}

.detail-section {
  display: grid;
  gap: 8px;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.kv {
  border: 1px solid rgba(8, 15, 43, 0.08);
  border-radius: 14px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.82);
}

.kv span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 4px;
}

.empty-state {
  display: grid;
  min-height: 240px;
  place-content: center;
  text-align: center;
  color: var(--muted);
  padding: 24px;
}

.empty-state strong {
  color: var(--text);
  margin-bottom: 4px;
}

.empty-state.compact {
  min-height: 140px;
}

.source-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 14px;
  align-items: center;
  padding: 14px;
}

.source-row.is-problem {
  border-color: rgba(198, 61, 61, 0.42);
  box-shadow: 0 12px 30px rgba(198, 61, 61, 0.09);
}

.section-head {
  display: flex;
  align-items: end;
  justify-content: space-between;
  margin-bottom: 14px;
}

.panel-head {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  justify-content: space-between;
}

.settings-grid,
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.panel,
.stat-card {
  padding: 22px;
}

.settings-grid .panel:first-child,
.recommendation-panel,
.channel-stats-panel {
  grid-column: 1 / -1;
}

.filter-groups,
.recommendation-list,
.channel-stats-list {
  display: grid;
  gap: 12px;
  margin-top: 16px;
}

.filter-group,
.recommendation-row,
.channel-stat-row {
  border: 1px solid rgba(8, 15, 43, 0.08);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.74);
  padding: 14px;
}

.filter-group-head,
.channel-stat-title {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
}

.filter-chip-list,
.source-stats,
.channel-stat-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.source-efficiency-stats {
  padding-top: 2px;
}

.source-cost,
.source-efficiency-reason {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.source-cost-note,
.source-efficiency-reason.is-warning {
  color: var(--warning);
  font-weight: 700;
}

.filter-chip {
  display: inline-flex;
  min-height: 30px;
  align-items: center;
  border-radius: 999px;
  padding: 0 11px;
  background: var(--surface-strong);
  color: #334155;
  font-size: 13px;
  font-weight: 750;
}

.filter-chip.success {
  background: #dcfce7;
  color: #166534;
}

.filter-chip.primary {
  background: #dbeafe;
  color: #1d4ed8;
}

.filter-chip.danger {
  background: #fee2e2;
  color: #991b1b;
}

.recommendation-summary {
  margin-top: 14px;
}

.recommendation-row {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

.recommendation-row strong,
.recommendation-row span {
  display: block;
}

.metric {
  display: inline-grid;
  min-width: 92px;
  gap: 2px;
  border: 1px solid rgba(8, 15, 43, 0.08);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.8);
  padding: 8px 10px;
}

.metric small {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}

.metric strong {
  color: var(--text);
  font-size: 15px;
}

.channel-stat-row {
  display: grid;
  gap: 8px;
}

.field {
  display: grid;
  gap: 6px;
  color: var(--text);
  font-size: 13px;
  font-weight: 650;
}

.field.compact {
  min-width: 160px;
}

input,
select,
textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid rgba(8, 15, 43, 0.14);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.95);
  color: var(--text);
  padding: 11px 13px;
  transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

textarea {
  resize: vertical;
}

input:hover,
select:hover,
textarea:hover {
  border-color: rgba(8, 15, 43, 0.22);
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: rgba(255, 163, 0, 0.7);
  box-shadow: 0 0 0 4px rgba(255, 163, 0, 0.16);
}

.toggle-field {
  display: flex;
  min-height: 44px;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-weight: 650;
  border: 1px solid rgba(8, 15, 43, 0.1);
  border-radius: 14px;
  padding: 0 14px;
  background: rgba(255, 255, 255, 0.88);
}

.toggle-field input {
  width: 18px;
  min-height: 18px;
  accent-color: var(--primary);
}

.modal {
  width: min(520px, calc(100vw - 28px));
  border: 1px solid rgba(8, 15, 43, 0.1);
  border-radius: calc(var(--radius) + 6px);
  padding: 0;
  box-shadow: var(--shadow);
  background: transparent;
  overflow: hidden;
}

.lead-modal {
  width: min(720px, calc(100vw - 28px));
}

.modal::backdrop {
  background: rgba(8, 15, 43, 0.42);
  backdrop-filter: blur(4px);
}

.modal-card {
  display: grid;
  gap: 16px;
  padding: 22px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.98) 0%, rgba(247, 250, 255, 0.98) 100%);
  position: relative;
}

.lead-modal-card {
  max-height: min(760px, calc(100dvh - 32px));
  overflow: auto;
}

.modal-card::before {
  content: '';
  position: absolute;
  inset: 0 0 auto;
  height: 4px;
  background: linear-gradient(90deg, var(--accent) 0%, rgba(255, 163, 0, 0.4) 55%, rgba(8, 15, 43, 0.1) 100%);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.icon-button {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  border: 1px solid rgba(8, 15, 43, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  color: var(--muted);
  font-weight: 800;
  box-shadow: 0 8px 18px rgba(8, 15, 43, 0.08);
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 20;
  max-width: min(420px, calc(100vw - 36px));
  border-radius: 16px;
  background: var(--text);
  color: white;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 160ms ease, transform 160ms ease;
  pointer-events: none;
}

.toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.check-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.check-list span {
  border-left: 3px solid var(--accent);
  padding-left: 10px;
  color: #364152;
}

.mobile-tabs {
  display: none;
}

@media (max-width: 900px) {
  body {
    min-height: 100dvh;
  }

  .app-shell {
    grid-template-columns: 1fr;
    min-height: 100dvh;
  }

  .sidebar {
    display: none;
  }

  .workspace {
    padding: 18px 14px calc(112px + env(safe-area-inset-bottom));
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    gap: 12px;
  }

  .topbar-actions {
    width: 100%;
  }

  .topbar-actions .button {
    flex: 1;
  }

  .toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
  }

  .lead-layout,
  .settings-grid,
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .lead-layout {
    gap: 12px;
  }

  .lead-list {
    order: 1;
  }

  .detail-panel {
    display: none;
  }

  .lead-modal {
    width: calc(100vw - 20px);
    max-width: none;
    max-height: calc(100dvh - 20px);
  }

  .lead-modal-card {
    max-height: calc(100dvh - 20px);
    padding: 18px;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }

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

  .panel-head,
  .recommendation-row,
  .filter-group-head,
  .channel-stat-title {
    align-items: stretch;
    flex-direction: column;
  }

  .panel-head .button {
    width: 100%;
  }

  .source-row,
  .panel,
  .stat-card,
  .lead-card {
    border-radius: 16px;
  }

  .settings-grid .panel:first-child {
    order: 1;
  }

  .settings-grid .panel:nth-child(2) {
    order: 2;
  }

  .settings-grid .panel:last-child {
    order: 3;
  }

  .settings-grid .button[type="submit"] {
    position: sticky;
    bottom: calc(82px + env(safe-area-inset-bottom));
    z-index: 4;
    width: 100%;
    box-shadow: 0 12px 30px rgba(8, 15, 43, 0.2);
  }

  .filter-chip-list,
  .source-stats,
  .channel-stat-metrics {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .filter-chip,
  .metric {
    min-width: 0;
    width: 100%;
  }

  .stats-grid {
    align-items: stretch;
  }

  .stat-card {
    min-height: 118px;
  }

  .mobile-tabs {
    position: fixed;
    right: 10px;
    bottom: calc(10px + env(safe-area-inset-bottom));
    left: 10px;
    z-index: 10;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    border: 1px solid rgba(8, 15, 43, 0.12);
    border-radius: 18px;
    background: rgba(255, 255, 255, 0.94);
    padding: 6px;
    box-shadow: var(--shadow);
    backdrop-filter: blur(16px);
  }

  .mobile-tab {
    min-height: 42px;
    text-align: center;
    padding: 0 6px;
    font-size: 12px;
  }

  .toast {
    right: 12px;
    bottom: calc(78px + env(safe-area-inset-bottom));
    left: 12px;
    max-width: none;
  }
}

@media (max-width: 520px) {
  h1 {
    font-size: 24px;
  }

  .toolbar,
  .form-row,
  .modal-actions,
  .card-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar {
    display: grid;
    grid-template-columns: 1fr;
  }

  .topbar-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .topbar-actions .button {
    min-width: 0;
    padding-inline: 10px;
  }

  .lead-card {
    padding: 12px;
  }

  .lead-meta,
  .tag-row {
    gap: 6px;
  }

  .field.compact {
    min-width: 100%;
  }

  .toggle-field {
    justify-content: flex-start;
  }

  .detail-grid {
    grid-template-columns: 1fr;
  }

  .detail-body {
    gap: 14px;
  }

  .detail-section h2 {
    font-size: 18px;
  }

  .card-actions .button {
    width: 100%;
  }

  .lead-modal-card .card-actions {
    position: sticky;
    bottom: 0;
    z-index: 2;
    margin-inline: -18px;
    margin-bottom: -18px;
    border-top: 1px solid rgba(8, 15, 43, 0.1);
    background: rgba(255, 255, 255, 0.96);
    padding: 12px 18px calc(12px + env(safe-area-inset-bottom));
    backdrop-filter: blur(12px);
  }

  .mobile-tabs {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .mobile-tab {
    border-radius: 12px;
    font-size: 11px;
    padding: 0 3px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
  }
}
/* Compact lead cards in narrow Telegram desktop and mobile windows. */
@media (max-width: 900px) {
  .lead-card {
    gap: 8px;
    padding: 12px;
  }

  .lead-meta {
    gap: 6px;
  }

  .lead-meta > .muted {
    font-size: 12px;
  }

  .lead-card .tag {
    min-height: 22px;
    padding: 0 8px;
    font-size: 11px;
  }

  .lead-summary {
    font-size: 14px;
    line-height: 1.4;
    overflow-wrap: anywhere;
  }

  .lead-card .tag-row {
    min-width: 0;
    font-size: 12px;
    overflow-wrap: anywhere;
  }
}


/* Keep the complete app readable in narrow desktop panels and phones. */
@media (max-width: 900px) {
  .workspace { padding: 16px 14px 90px; }
  .view { min-width: 0; }
  .panel, .stat-card, .source-row, .lead-card, .rejection-card { min-width: 0; }
  .panel { padding: 16px; border-radius: 16px; }
  .panel h2 { font-size: 18px; line-height: 1.3; }
  .hint, .muted { overflow-wrap: anywhere; }
  .button, input, select, textarea { max-width: 100%; }
  .filter-form { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rejection-meta { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
  .rejection-card p { font-size: 14px; line-height: 1.45; overflow-wrap: anywhere; }
}
@media (max-width: 520px) {
  .workspace { padding: 12px 10px 84px; }
  .panel { padding: 12px; }
  .filter-form, .settings-grid, .stats-grid, .detail-grid { grid-template-columns: minmax(0, 1fr); }
  .panel-head { align-items: flex-start; gap: 10px; flex-wrap: wrap; }
  .panel-head .button { width: 100%; }
  .rejection-card .card-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .rejection-card .card-actions .button { min-width: 0; padding-inline: 8px; }
}
