:root {
  color-scheme: light;
  --bg: #f4f7fb;
  --surface: #ffffff;
  --surface-2: #eef3f8;
  --text: #17202a;
  --muted: #667085;
  --line: #d8e0ea;
  --accent: #0f766e;
  --accent-2: #2563eb;
  --accent-soft: #dff7f2;
  --warn: #b45309;
  --warn-soft: #fff4d6;
  --danger: #c2410c;
  --danger-soft: #ffe8df;
  --shadow: 0 16px 40px rgba(23, 32, 42, 0.10);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #101828;
  --surface: #182230;
  --surface-2: #202c3c;
  --text: #f5f7fb;
  --muted: #a7b3c4;
  --line: #334155;
  --accent: #2dd4bf;
  --accent-2: #60a5fa;
  --accent-soft: #123c3a;
  --warn: #fbbf24;
  --warn-soft: #3b2f11;
  --danger: #fb7185;
  --danger-soft: #3f1d25;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--bg); color: var(--text); }
button, input, select, textarea { font: inherit; }
button {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 750;
}
button:hover { filter: brightness(0.94); }
button:disabled { cursor: not-allowed; opacity: 0.55; filter: none; }
button.secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
button.secondary.active-sound { border-color: color-mix(in srgb, var(--accent), transparent 45%); background: var(--accent-soft); color: var(--accent); }
button.danger { background: var(--danger); }
button.small { padding: 8px 10px; font-size: 13px; }
.hidden { display: none !important; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at 30% 20%, var(--accent-soft), transparent 32%), var(--bg); }
.login-panel {
  width: min(390px, 100%);
  display: grid;
  gap: 14px;
  background: color-mix(in srgb, var(--surface), transparent 4%);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.brand-mark {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  font-weight: 900;
}
.login-panel h1 { margin: 0; font-size: 30px; }
.login-panel p, .hint { margin: 0; color: var(--muted); }
label { display: grid; gap: 6px; color: var(--text); font-size: 14px; font-weight: 650; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 10px 11px;
  outline: none;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent), transparent 78%); }
.error { min-height: 20px; color: var(--danger); font-size: 14px; }

.shell { min-height: 100vh; display: grid; grid-template-columns: 260px minmax(0, 1fr); overflow-x: hidden; }
.sidebar { padding: 18px; border-right: 1px solid var(--line); background: var(--surface); display: grid; grid-template-rows: auto auto 1fr; gap: 18px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand strong, .brand span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.brand span { color: var(--muted); font-size: 13px; margin-top: 2px; }
.nav { display: grid; gap: 8px; }
.nav-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  justify-content: flex-start;
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
  letter-spacing: 0.02em;
  font-size: 13px;
}
.nav-item.active, .nav-item:hover { background: var(--surface-2); color: var(--text); border-color: var(--line); }
.nav-item:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent), transparent 65%); outline-offset: 2px; }
.nav-item svg { width: 19px; height: 19px; flex: 0 0 19px; fill: currentColor; opacity: 0.92; }
.nav-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.avito-note { align-self: end; border: 1px solid color-mix(in srgb, var(--warn), transparent 60%); background: var(--warn-soft); color: var(--warn); border-radius: 10px; padding: 12px; font-size: 13px; font-weight: 800; }
.avito-note.connected { border-color: color-mix(in srgb, var(--accent), transparent 55%); background: var(--accent-soft); color: var(--accent); }

.workspace { min-width: 0; max-width: 100%; overflow-x: hidden; padding: 18px; display: grid; gap: 16px; align-content: start; }
.topbar { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.topbar h1 { margin: 0; font-size: 30px; line-height: 1.1; }
.topbar p { margin: 6px 0 0; color: var(--muted); }
.top-actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.top-actions select { width: 165px; }
.contract-header-total { min-height: 42px; display: grid; align-content: center; gap: 2px; padding: 6px 12px; border: 1px solid color-mix(in srgb, var(--accent), transparent 55%); border-radius: 8px; background: var(--accent-soft); }
.contract-header-total span { color: var(--muted); font-size: 11px; font-weight: 750; }
.contract-header-total strong { color: var(--accent); font-size: 18px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.toast { position: sticky; top: 10px; z-index: 5; width: fit-content; max-width: 100%; padding: 10px 12px; border-radius: 8px; background: var(--accent-soft); color: var(--text); border: 1px solid var(--line); box-shadow: var(--shadow); }

.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 14px; box-shadow: 0 8px 24px rgba(23, 32, 42, 0.04); }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.panel h2, .panel-head h2 { margin: 0; font-size: 18px; }
#dashboardView { display: grid; gap: 18px; align-content: start; }
#leadsView, #contractsView, #avitoView, #quickRepliesView, #integrationsView, #deletedLeadsView { display: grid; gap: 16px; align-content: start; }
.stats { display: grid; grid-template-columns: repeat(8, minmax(110px, 1fr)); gap: 12px; }
.stat { border: 1px solid var(--line); border-radius: 12px; padding: 14px; background: var(--surface); cursor: pointer; }
.stat strong { display: block; font-size: 28px; margin-bottom: 4px; }
.stat span { color: var(--muted); font-size: 13px; font-weight: 700; }
.stat.warn { background: var(--warn-soft); color: var(--warn); }
.stat.accent { background: var(--accent-soft); }

.dashboard-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; align-items: start; }
.dashboard-primary, .dashboard-secondary { display: grid; gap: 14px; align-content: start; min-width: 0; }
.tasks-panel, .client-tasks-panel, .planner-panel { display: grid; gap: 12px; }
.tasks-panel .panel-head, .client-tasks-panel .panel-head, .planner-panel .panel-head { margin-bottom: 0; align-items: flex-start; }
.task-form { display: grid; gap: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: color-mix(in srgb, var(--surface), var(--surface-2) 38%); }
.task-form-row { display: grid; grid-template-columns: minmax(170px, 1fr) minmax(130px, 0.6fr) minmax(180px, 1fr); gap: 10px; }
.task-list { display: grid; gap: 8px; }
.task-section-title { margin-top: 4px; color: var(--muted); font-size: 12px; font-weight: 850; text-transform: uppercase; letter-spacing: 0.02em; }
.task-item { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 10px; align-items: start; border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: var(--surface); }
.task-item.done { opacity: 0.72; background: var(--surface-2); }
.task-item.done .task-main strong { text-decoration: line-through; color: var(--muted); }
.task-check { width: 28px; height: 28px; padding: 0; display: inline-grid; place-items: center; border-radius: 8px; background: var(--surface-2); color: var(--accent); border: 1px solid var(--line); font-size: 16px; line-height: 1; }
.task-main { display: grid; gap: 4px; min-width: 0; }
.task-main strong, .task-main p { overflow-wrap: anywhere; }
.task-main p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.35; }
.task-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.link-button { width: auto; padding: 0; border: 0; border-radius: 0; background: transparent; color: var(--accent); font-size: inherit; font-weight: 850; text-decoration: none; }
.link-button:hover { filter: none; text-decoration: underline; }

.compact-list { display: grid; gap: 8px; }
.empty { border: 1px dashed var(--line); border-radius: 12px; padding: 26px; text-align: center; color: var(--muted); background: var(--surface-2); }
.compact-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.compact-row:hover { background: var(--surface-2); }

.contracts-panel { display: grid; gap: 14px; padding: 0; overflow: hidden; }
.contracts-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 14px 14px 0; }
.contracts-toolbar h2 { margin: 0 0 4px; }
.contracts-toolbar p { margin: 0; }
.contracts-toolbar input { width: min(420px, 46vw); }
.contracts-table { min-width: 0; overflow-x: auto; }
.contracts-table-head, .contract-row { min-width: 820px; display: grid; grid-template-columns: minmax(220px, 1.35fr) minmax(150px, 0.9fr) minmax(180px, 1fr) minmax(145px, 0.75fr) minmax(130px, 0.62fr); gap: 14px; align-items: center; }
.contracts-table-head { padding: 10px 14px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 850; }
.contracts-list { display: grid; }
.contract-row { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.contract-row:last-child { border-bottom: 0; }
.contract-row:hover, .contract-row:focus-visible { background: var(--surface-2); outline: none; }
.contract-cell { min-width: 0; overflow-wrap: anywhere; font-size: 14px; }
.contract-client { display: flex; align-items: center; gap: 10px; }
.contract-client > div { min-width: 0; display: grid; gap: 3px; }
.contract-client strong, .contract-client span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contract-client span, .contract-date { color: var(--muted); font-size: 12px; }
.contract-amount { color: var(--accent); font-size: 16px; font-weight: 900; font-variant-numeric: tabular-nums; }

.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.filters #searchInput { grid-column: span 2; }
.filter-check { min-height: 42px; display: flex; align-items: center; gap: 8px; padding: 0 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); font-weight: 700; cursor: pointer; }
.filter-check input { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.bulk-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.bulk-toolbar strong { display: block; }
.bulk-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.bulk-actions select { width: 180px; }
.group-badge { display: inline-flex; margin-top: 4px; border-radius: 999px; padding: 4px 8px; background: var(--accent-soft); color: var(--accent); border: 1px solid color-mix(in srgb, var(--accent), transparent 55%); font-size: 12px; font-weight: 850; }
.board { display: grid; grid-template-columns: repeat(6, minmax(230px, 1fr)); gap: 12px; overflow-x: auto; padding-bottom: 8px; }
.board.lost-collapsed { grid-template-columns: repeat(5, minmax(230px, 1fr)) 46px; }
.column { min-height: 58vh; background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px; display: grid; align-content: start; gap: 10px; }
.column.drop-target { outline: 3px solid color-mix(in srgb, var(--accent), transparent 48%); outline-offset: -3px; background: color-mix(in srgb, var(--accent-soft), var(--surface-2) 55%); }
.column header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.column h2 { margin: 0; font-size: 15px; }
.column-head-actions { display: flex; align-items: center; gap: 6px; }
.lost-column.collapsed { min-width: 46px; padding: 7px 6px; }
.lost-column.collapsed header { justify-content: center; }
.lost-column-toggle { display: inline-grid; place-items: center; width: 32px; height: 32px; flex: 0 0 32px; border-radius: 8px; }
.lost-column-toggle svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.25; stroke-linecap: round; stroke-linejoin: round; }
.count { color: var(--muted); font-size: 13px; font-weight: 800; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px; display: grid; gap: 9px; cursor: pointer; }
.card.source-avitolog { background: color-mix(in srgb, var(--accent-soft), var(--surface) 35%); border-color: color-mix(in srgb, var(--accent), transparent 48%); }
.card.source-bfl { background: color-mix(in srgb, var(--accent-2), var(--surface) 88%); border-color: color-mix(in srgb, var(--accent-2), transparent 48%); }
.card:hover, .card:focus-visible { border-color: var(--accent); outline: none; }
.card.selected { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent), transparent 76%); }
.card.dragging { opacity: 0.55; }
.card-top { display: grid; grid-template-columns: auto 1fr; gap: 8px; align-items: start; }
.card-check { display: inline-flex; width: 28px; height: 28px; align-items: center; justify-content: center; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--line); cursor: pointer; }
.card-check input { width: 16px; height: 16px; padding: 0; cursor: pointer; }
.card h3 { margin: 0; font-size: 16px; overflow-wrap: anywhere; }
.meta { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.card-created-at { color: var(--muted); font-size: 11px; font-weight: 650; }
.task-marker { width: 15px; height: 15px; flex: 0 0 15px; margin-left: auto; border-radius: 50%; border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--line); }
.task-marker-green { background: #16a34a; }
.task-marker-yellow { background: #facc15; }
.task-marker-red { background: #ef4444; }
.pill { display: inline-flex; border-radius: 999px; padding: 4px 8px; font-size: 12px; font-weight: 800; background: var(--surface-2); color: var(--text); border: 1px solid var(--line); }
.priority-urgent { background: var(--danger-soft); color: var(--danger); }
.priority-high { background: var(--warn-soft); color: var(--warn); }
.overdue { color: var(--danger); font-weight: 850; }
.due-today { color: var(--warn); font-weight: 850; }
.quick-links { display: flex; gap: 8px; flex-wrap: wrap; }
.quick-links a, .quick-link-button { width: auto; min-width: 0; border: 1px solid var(--line); border-radius: 8px; color: var(--accent); background: var(--surface-2); padding: 7px 9px; text-decoration: none; font-size: 13px; font-weight: 800; line-height: 1.2; }
.quick-link-button:hover { filter: none; border-color: var(--accent); }
.quick-links .contact-icon-action { width: 40px; height: 40px; min-width: 40px; flex: 0 0 40px; display: inline-grid; place-items: center; padding: 0; border-radius: 8px; }
.contact-icon-action svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.contact-icon-action span { font-size: 11px; line-height: 1; font-weight: 900; letter-spacing: 0; }
.quick-links .contact-icon-action:hover { filter: none; border-color: var(--accent); background: var(--accent-soft); }
.call-button { min-width: 92px; }
.quick-links .contact-icon-action.call-button { min-width: 40px; }
.call-button:disabled { cursor: wait; opacity: 0.68; }
.call-widget {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 24;
  width: min(360px, calc(100vw - 36px));
  display: grid;
  gap: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.call-widget-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 14px 10px; }
.call-widget-title { min-width: 0; display: flex; align-items: center; gap: 10px; }
.call-widget-title div { min-width: 0; display: grid; gap: 3px; }
.call-widget-title strong, .call-widget-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.call-widget-title strong { font-size: 15px; }
.call-widget-title span { color: var(--accent); font-size: 13px; font-weight: 800; }
.call-live-dot { width: 10px; height: 10px; flex: 0 0 10px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 5px var(--accent-soft); }
.call-widget.ended .call-live-dot { background: var(--muted); box-shadow: 0 0 0 5px var(--surface-2); }
.call-widget.ended .call-widget-title span { color: var(--muted); }
.call-widget-body { display: grid; grid-template-columns: minmax(0, 1fr) minmax(120px, auto); gap: 12px; align-items: center; padding: 8px 14px 14px; }
.call-time-block { display: grid; gap: 2px; }
.call-time-block strong { font-size: 30px; line-height: 1; font-variant-numeric: tabular-nums; }
.call-time-block span, .call-route { color: var(--muted); font-size: 12px; font-weight: 700; }
.call-route { text-align: right; overflow-wrap: anywhere; }
.call-widget-actions { padding: 0 14px 14px; }
.call-widget-actions button { width: 100%; }
.card select { padding: 7px 8px; }

.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(280px, 1fr)); gap: 14px; }
.form-panel { display: grid; gap: 14px; align-content: start; }
.settings-status { display: grid; gap: 8px; padding: 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-2); }
.deleted-leads-entry > div { display: grid; gap: 6px; }
.deleted-leads-workspace { display: grid; gap: 14px; padding: 0; overflow: hidden; }
.deleted-leads-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 14px 0; }
.deleted-leads-toolbar h2 { margin: 0 0 4px; }
.deleted-leads-toolbar p { margin: 0; }
.deleted-leads-actions { min-width: min(100%, 520px); flex: 1 1 520px; display: flex; align-items: center; justify-content: flex-end; gap: 10px; }
.deleted-leads-actions input { min-width: 280px; flex: 1; }
.deleted-leads-actions button { white-space: nowrap; }
.deleted-leads-table { min-width: 0; overflow-x: auto; }
.deleted-leads-table-head, .deleted-lead-row { min-width: 720px; display: grid; grid-template-columns: minmax(260px, 1.25fr) minmax(240px, 1fr) minmax(210px, 0.68fr); gap: 16px; align-items: center; }
.deleted-leads-table-head { padding: 10px 14px; border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); background: var(--surface-2); color: var(--muted); font-size: 12px; font-weight: 850; }
.deleted-leads-list { display: grid; }
.deleted-lead-row { min-height: 72px; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.deleted-lead-row:last-child { border-bottom: 0; }
.deleted-lead-row:hover { background: var(--surface-2); }
.deleted-lead-main, .deleted-lead-meta { min-width: 0; display: grid; gap: 4px; }
.deleted-lead-main strong, .deleted-lead-main span, .deleted-lead-meta span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.deleted-lead-main span, .deleted-lead-meta { color: var(--muted); font-size: 12px; }
.deleted-lead-row select { width: 100%; }
.tiny-helper { color: var(--muted); font-size: 12px; line-height: 1.35; }
.integrations-grid { display: grid; grid-template-columns: repeat(3, minmax(220px, 1fr)); grid-auto-rows: 1fr; gap: 14px; align-items: stretch; }
.integration-card { display: grid; grid-template-rows: auto minmax(76px, auto) auto auto minmax(0, auto) minmax(0, auto); gap: 12px; align-content: start; height: 100%; border-top: 4px solid var(--line); }
.integration-card.connected { border-top-color: var(--accent); }
.integration-card.pending { border-top-color: var(--warn); }
.integration-card.disabled { border-top-color: var(--muted); }
.integration-head { display: flex; gap: 12px; align-items: center; }
.integration-head h2 { margin: 0 0 4px; line-height: 1.1; }
.integration-icon { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 10px; background: var(--accent-soft); color: var(--accent); flex: 0 0 auto; }
.integration-icon svg { width: 22px; height: 22px; fill: currentColor; }
.integration-status { display: inline-flex; border-radius: 999px; padding: 4px 8px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); font-size: 12px; font-weight: 850; }
.integration-status.connected { border-color: color-mix(in srgb, var(--accent), transparent 55%); background: var(--accent-soft); color: var(--accent); }
.integration-metrics { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.integration-card p { margin: 0; line-height: 1.35; }
.integration-metrics div { min-height: 62px; display: grid; align-content: center; border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: var(--surface-2); min-width: 0; }
.integration-metrics strong, .integration-metrics span { display: block; overflow-wrap: anywhere; }
.integration-metrics strong { font-size: 18px; margin-bottom: 3px; }
.integration-metrics span { color: var(--muted); font-size: 12px; font-weight: 800; }
.integration-card .warning-text,
.integration-card .error {
  min-height: 0;
  margin: 0;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.35;
  font-weight: 750;
}
.integration-card .warning-text { background: var(--danger-soft); }
.credential-panel, .ai-control-panel { margin-top: 0; }
.credential-panel .panel-head, .ai-control-panel .panel-head { margin-bottom: 0; align-items: center; }
.credential-panel label { min-width: 0; }
.credential-panel { grid-template-columns: repeat(2, minmax(220px, 1fr)); align-items: end; }
.credential-panel .panel-head, .credential-panel .settings-status, .credential-panel .tiny-helper { grid-column: 1 / -1; }
.credential-panel input:disabled { opacity: 0.72; cursor: not-allowed; background: color-mix(in srgb, var(--surface-2), var(--surface) 45%); }
.credential-panel button:disabled { opacity: 0.58; cursor: not-allowed; filter: none; }
.secret-field { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: center; }
.secret-field button { white-space: nowrap; }
.integrations-ai-link { display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.ai-control-panel { gap: 16px; }
.ai-section { display: grid; gap: 12px; border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: color-mix(in srgb, var(--surface), var(--surface-2) 38%); }
.ai-section h3 { margin: 0; font-size: 15px; }
.ai-overview { display: flex; justify-content: space-between; align-items: center; gap: 14px; }
.ai-overview .settings-status { min-width: min(420px, 100%); }
.ai-profiles-grid { display: grid; grid-template-columns: repeat(2, minmax(320px, 1fr)); gap: 14px; align-items: start; }
.ai-profile-card { display: grid; gap: 12px; border-top: 4px solid var(--line); }
.ai-profile-card.connected { border-top-color: var(--accent); }
.ai-profile-card.pending { border-top-color: var(--warn); }
.ai-card-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.ai-card-head h2 { margin: 0 0 5px; }
.ai-readiness { color: var(--muted); font-size: 12px; font-weight: 750; text-align: right; max-width: 240px; }
.form-grid.compact { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.ai-flags { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.ai-card-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.avito-status-panel { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.avito-automation-panel { display: grid; gap: 12px; }
.automation-layout { display: grid; grid-template-columns: minmax(320px, 1fr) minmax(320px, 1fr); gap: 12px; align-items: start; }
.automation-card { display: grid; gap: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: color-mix(in srgb, var(--surface), var(--surface-2) 35%); }
.automation-card h3 { margin: 0; font-size: 15px; }
.automation-flags { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.broadcast-campaigns { display: grid; gap: 8px; max-height: 220px; overflow: auto; }
.broadcast-campaign { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: var(--surface-2); }
.broadcast-campaign strong, .broadcast-campaign span { display: block; overflow-wrap: anywhere; }
.avito-layout { display: grid; grid-template-columns: minmax(280px, 360px) minmax(0, 1fr); gap: 16px; align-items: start; }
.avito-list-panel, .avito-thread-panel { min-height: 62vh; }
.avito-search { display: grid; gap: 10px; margin-bottom: 12px; }
.inline-check { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--muted); }
.inline-check input { width: auto; }
.avito-chats { display: grid; gap: 8px; max-height: 64vh; overflow: auto; }
.avito-chat {
  width: 100%;
  display: grid;
  gap: 5px;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px;
}
.avito-chat.active, .avito-chat:hover { border-color: var(--accent); background: var(--accent-soft); }
.avito-chat span, .avito-chat small, .avito-chat time { overflow-wrap: anywhere; }
.avito-chat small, .avito-chat time { color: var(--muted); font-size: 12px; font-weight: 650; }
.chat-main { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; min-width: 0; }
.chat-main strong, .chat-main em { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-style: normal; }
.chat-main em { color: var(--muted); font-size: 13px; margin-top: 2px; }
.unread-badge { min-width: 22px; height: 22px; display: inline-grid; place-items: center; border-radius: 999px; background: var(--danger); color: white; font-size: 12px; }
.avatar { position: relative; display: inline-grid; place-items: center; overflow: hidden; border-radius: 999px; color: #fff; font-weight: 900; flex: 0 0 auto; }
.avatar.small { width: 38px; height: 38px; font-size: 14px; }
.avatar.large { width: 44px; height: 44px; font-size: 16px; }
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; z-index: 2; }
.avatar b { position: relative; z-index: 1; }
.avatar-teal { background: linear-gradient(135deg, #0f766e, #14b8a6); }
.avatar-blue { background: linear-gradient(135deg, #2563eb, #38bdf8); }
.avatar-amber { background: linear-gradient(135deg, #b45309, #f59e0b); }
.avatar-rose { background: linear-gradient(135deg, #be123c, #fb7185); }
.avatar-violet { background: linear-gradient(135deg, #6d28d9, #a78bfa); }
.avito-thread-panel { display: grid; grid-template-rows: auto minmax(260px, 1fr) auto; gap: 12px; }
.avito-thread-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; }
.avito-thread-head h2 { margin: 0; font-size: 20px; display: flex; align-items: center; gap: 10px; }
.avito-thread-head .meta a { color: var(--accent); font-weight: 800; text-decoration: none; }
.avito-thread-head .meta a:hover { text-decoration: underline; }
.thread-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.secondary-link { border: 1px solid var(--line); border-radius: 8px; color: var(--accent); background: var(--surface-2); padding: 8px 10px; text-decoration: none; font-size: 13px; font-weight: 800; white-space: nowrap; }
.avito-messages { display: grid; gap: 9px; align-content: start; max-height: 58vh; overflow: auto; padding: 4px; }
.avito-message { max-width: min(680px, 92%); display: grid; gap: 6px; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; background: var(--surface-2); overflow-wrap: anywhere; }
.avito-message.out { justify-self: end; background: var(--accent-soft); }
.avito-message a { color: var(--accent); font-weight: 800; }
.media-thumb {
  width: min(260px, 100%);
  aspect-ratio: 4 / 3;
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0;
  background: var(--surface);
  color: var(--muted);
}
.media-thumb span { position: absolute; inset: 0; display: grid; place-items: center; padding: 12px; font-size: 13px; }
.media-thumb img { position: relative; width: 100%; height: 100%; display: block; object-fit: contain; background: var(--surface); }
.media-thumb img:not([src]), .media-thumb img[src=""] { display: none; }
.media-fallback { display: inline-flex; border: 1px dashed var(--line); border-radius: 8px; padding: 8px 10px; color: var(--muted); font-size: 13px; }
.avito-reply { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; align-items: start; border: 1px solid transparent; border-radius: 12px; }
.avito-reply.drag { border-color: var(--accent); background: color-mix(in srgb, var(--accent-soft), transparent 55%); }
.reply-tools, .reply-actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.reply-tools select { max-width: 260px; }
.reply-tools { justify-content: space-between; }
.reply-actions { justify-content: flex-end; }
#sendComposerBtn { min-width: 132px; }
.icon-button.compact { display: inline-grid; place-items: center; width: 42px; height: 42px; border-radius: 8px; }
.icon-button.compact svg { width: 22px; height: 22px; fill: currentColor; }
.image-preview { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 10px; align-items: center; border: 1px solid var(--line); border-radius: 10px; padding: 8px; background: var(--surface-2); }
.image-preview img { width: 72px; height: 54px; object-fit: contain; border-radius: 8px; background: var(--surface); border: 1px solid var(--line); }
.attachment-info { min-width: 0; }
.attachment-info strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.avito-reply .hint { grid-column: 1 / -1; font-size: 12px; }
.avito-lead-box { display: flex; justify-content: space-between; align-items: center; gap: 10px; border: 1px solid color-mix(in srgb, var(--accent), transparent 55%); background: var(--accent-soft); border-radius: 10px; padding: 10px 12px; }
.quick-reply-form { display: grid; gap: 10px; }
.template-row { display: grid; grid-template-columns: minmax(120px, 180px) 1fr; gap: 12px; align-items: end; }
.template-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.quick-reply-list { display: grid; gap: 8px; max-height: 360px; overflow: auto; }
.quick-template { display: grid; gap: 6px; border: 1px solid var(--line); border-radius: 10px; padding: 10px; background: var(--surface-2); }
.quick-template p { margin: 0; color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
.quick-template .meta { display: block; margin-top: 2px; }
.muted-template { opacity: 0.72; }
.media-lightbox {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(15, 23, 42, 0.76);
}
.media-lightbox img { max-width: min(1100px, 94vw); max-height: 88vh; object-fit: contain; border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); }
.media-lightbox .icon-button { position: fixed; top: 18px; right: 18px; }

body.drawer-open { overflow: hidden; }
.lead-drawer { position: fixed; inset: 0; z-index: 18; width: 100vw; max-width: 100vw; overflow: hidden; display: grid; grid-template-columns: minmax(0, 1fr) min(96vw, 1540px); }
.lead-overlay { background: rgba(15, 23, 42, 0.50); }
.drawer-panel {
  min-width: 0;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  gap: 14px;
  background: var(--surface);
  color: var(--text);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.drawer-head, .drawer-footer { position: sticky; z-index: 2; background: color-mix(in srgb, var(--surface), transparent 2%); }
.drawer-head { top: 0; min-width: 0; display: flex; align-items: flex-start; justify-content: space-between; gap: 14px; padding: 18px 20px 0; }
.drawer-title { display: flex; align-items: center; gap: 12px; min-width: 0; max-width: 100%; }
.drawer-title h2 { margin: 0; font-size: 24px; line-height: 1.1; overflow-wrap: anywhere; }
.drawer-head-actions { min-width: 0; display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.drawer-footer { bottom: 0; display: grid; grid-template-columns: auto 1fr auto auto; gap: 10px; align-items: center; padding: 12px 20px 16px; border-top: 1px solid var(--line); }
.drawer-scroll { min-height: 0; overflow: auto; padding: 0 20px 20px; }
.lead-form h2, .lead-form h3 { margin: 0; }
.warning-text { min-height: 18px; margin: 4px 0 0; color: var(--danger); font-weight: 800; }
.icon-button { width: 38px; height: 38px; padding: 0; background: var(--surface-2); color: var(--text); border: 1px solid var(--line); font-size: 24px; }
.lead-compact-meta { display: flex; flex-wrap: wrap; gap: 8px; padding: 0 20px; }
.lead-compact-meta span { border: 1px solid var(--line); border-radius: 999px; padding: 6px 10px; background: var(--surface-2); color: var(--muted); font-size: 13px; }
.lead-compact-meta strong { color: var(--text); }
.stage-bar { min-width: 0; display: flex; gap: 8px; overflow-x: auto; padding: 0 20px 2px; scrollbar-width: thin; }
.stage-step { min-width: 148px; display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.stage-step span { width: 22px; height: 22px; display: inline-grid; place-items: center; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); font-size: 12px; }
.stage-step.done { color: var(--accent); border-color: color-mix(in srgb, var(--accent), transparent 60%); }
.stage-step.current { color: #fff; background: var(--accent); border-color: var(--accent); }
.stage-step.current span { color: var(--accent); }
.drawer-tabs { min-width: 0; display: flex; gap: 6px; padding: 0 20px; border-bottom: 1px solid var(--line); overflow-x: auto; }
.tab { border-radius: 8px 8px 0 0; background: transparent; color: var(--muted); border: 1px solid transparent; border-bottom: 0; padding: 10px 12px; }
.tab.active { background: var(--surface-2); color: var(--text); border-color: var(--line); }
.drawer-main-grid { display: grid; grid-template-columns: minmax(400px, 1.08fr) minmax(360px, 0.94fr) minmax(300px, 0.78fr); gap: 16px; align-items: start; }
.lead-fields, .lead-focus-panel, .activity-panel { display: grid; gap: 12px; align-content: start; min-width: 0; }
.form-section, .lead-focus-panel > section, .activity-panel > section, .activity-panel > details { border: 1px solid var(--line); border-radius: 10px; background: var(--surface); padding: 12px; }
.form-section summary { cursor: pointer; font-weight: 850; margin: -2px 0 12px; }
.form-section:not([open]) summary { margin-bottom: 0; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.contact-field { min-width: 0; display: grid; gap: 8px; align-content: start; }
.contact-actions { min-height: 33px; align-items: center; }
.contact-actions a, .contact-actions button { min-width: 112px; text-align: center; }
.activity-panel { position: sticky; top: 0; }
.client-request-card { display: grid; gap: 10px; border-left: 4px solid var(--accent) !important; background: color-mix(in srgb, var(--accent-soft), var(--surface) 70%) !important; }
.focus-section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.focus-section-head span { color: var(--accent); border: 1px solid color-mix(in srgb, var(--accent), transparent 58%); border-radius: 999px; padding: 4px 8px; font-size: 11px; font-weight: 850; }
.client-request-card textarea { min-height: 152px; background: var(--surface); line-height: 1.45; }
.next-action-card, .comment-box { display: grid; gap: 10px; }
.next-action-card .meta { display: grid; gap: 4px; }
.focus-action-card { border-color: color-mix(in srgb, var(--warn), transparent 56%) !important; background: color-mix(in srgb, var(--warn-soft), var(--surface) 72%) !important; }
.focus-action-card #nextActionPreview strong { color: var(--text); font-size: 14px; }
.lead-tasks-panel { display: grid; gap: 10px; }
.focus-tasks-card { border-top: 3px solid var(--accent) !important; margin-bottom: 10px; }
.lead-task-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; }
.lead-task-form { display: grid; gap: 8px; }
.focus-tasks-card .lead-task-form { margin-top: 4px; padding-top: 14px; border-top: 1px solid var(--line); }
.lead-task-form-row { display: grid; grid-template-columns: minmax(170px, 1fr) minmax(120px, 0.6fr); gap: 8px; }
.lead-task-list { display: grid; gap: 8px; }
.lead-task-item { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; gap: 8px; align-items: start; border-top: 1px solid var(--line); padding: 10px 0 0; }
.lead-task-item:not(.done) { border-left: 3px solid var(--accent); padding-left: 9px; }
.lead-task-item.task-overdue { border-left-color: var(--danger); }
.lead-task-item.task-overdue .task-main > .meta { color: var(--danger); font-weight: 800; }
.lead-task-item.task-today { border-left-color: var(--warn); }
.lead-task-item.done { opacity: 0.68; }
.lead-task-item.done .task-main strong { color: var(--muted); text-decoration: line-through; }
.compact-empty { padding: 14px; border-radius: 8px; }
.activity-timeline summary { display: flex; align-items: center; justify-content: space-between; gap: 10px; cursor: pointer; list-style: none; font-size: 16px; font-weight: 850; }
.activity-timeline summary::-webkit-details-marker { display: none; }
.activity-timeline summary::after { content: "Показать"; color: var(--muted); font-size: 12px; font-weight: 750; }
.activity-timeline[open] summary::after { content: "Скрыть"; }
.activity-timeline-body { display: grid; gap: 10px; padding-top: 12px; margin-top: 12px; border-top: 1px solid var(--line); }
.activity-head { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; align-items: center; }
.history { max-height: 320px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); }
.history.large { max-height: 68vh; }
.history-item { padding: 10px; border-bottom: 1px solid var(--line); font-size: 13px; }
.history-item:last-child { border-bottom: 0; }
.history-item.comment { background: color-mix(in srgb, var(--accent-soft), transparent 38%); }

@media (max-width: 1280px) {
  .drawer-main-grid { grid-template-columns: minmax(0, 1fr) minmax(340px, 0.9fr); }
  .activity-panel { grid-column: 1 / -1; position: static; grid-template-columns: minmax(280px, 0.7fr) minmax(0, 1.3fr); }
}

@media (max-width: 980px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { grid-template-rows: auto auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .nav { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); }
  .avito-note { align-self: auto; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .filters { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .board { grid-template-columns: repeat(6, minmax(260px, 78vw)); }
  .board.lost-collapsed { grid-template-columns: repeat(5, minmax(260px, 78vw)) 46px; }
  .dashboard-grid, .avito-layout, .automation-layout, .integrations-grid, .ai-profiles-grid { grid-template-columns: 1fr; }
  .credential-panel, .ai-overview, .integrations-ai-link { display: grid; grid-template-columns: 1fr; }
  .avito-list-panel, .avito-thread-panel { min-height: auto; }
  .lead-drawer { grid-template-columns: 1fr; }
  .lead-overlay { display: none; }
  .drawer-panel { width: 100vw; border-left: 0; }
  .drawer-main-grid { grid-template-columns: 1fr; }
  .activity-panel { grid-column: auto; position: static; grid-template-columns: 1fr; }
  .deleted-leads-toolbar { display: grid; }
  .deleted-leads-actions { width: 100%; min-width: 0; justify-content: stretch; }
}

@media (max-width: 640px) {
  .workspace, .sidebar { padding: 12px; }
  .topbar h1 { font-size: 26px; }
  .top-actions, .top-actions button, .top-actions select, .contract-header-total { width: 100%; }
  .nav { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .nav-item { justify-content: center; padding: 9px 6px; gap: 6px; font-size: 12px; }
  .nav-item svg { width: 17px; height: 17px; flex-basis: 17px; }
  .nav-item span { white-space: normal; overflow-wrap: anywhere; line-height: 1.15; }
  .filters, .settings-grid, .form-grid, .form-grid.compact, .task-form-row, .task-item, .lead-task-form-row, .lead-task-item, .ai-flags, .automation-flags, .drawer-footer, .activity-head, .avito-reply, .template-row, .secret-field, .image-preview { grid-template-columns: 1fr; }
  .deleted-lead-row { grid-template-columns: 1fr; gap: 10px; }
  .filters #searchInput { grid-column: auto; }
  .stats { grid-template-columns: 1fr; }
  .avito-status-panel, .avito-thread-head, .avito-lead-box, .drawer-head, .drawer-head-actions { display: grid; }
  .task-actions { justify-content: stretch; }
  .task-actions button { width: 100%; }
  .drawer-head, .drawer-scroll, .drawer-footer, .lead-compact-meta, .stage-bar, .drawer-tabs { padding-left: 12px; padding-right: 12px; }
  .drawer-title h2 { font-size: 21px; }
  .secondary-link { white-space: normal; }
  .bulk-toolbar, .bulk-actions, .bulk-actions button, .bulk-actions select, .reply-tools, .reply-tools select, .attachment-actions, .attachment-actions button { width: 100%; }
  .reply-actions { width: 100%; display: grid; grid-template-columns: 52px minmax(0, 1fr); }
  .reply-actions button { width: 100%; }
  .card { -webkit-user-drag: none; }
  .contracts-toolbar { display: grid; }
  .contracts-toolbar input { width: 100%; }
  .deleted-leads-toolbar, .deleted-leads-actions { display: grid; width: 100%; min-width: 0; }
  .deleted-leads-actions input { min-width: 0; }
  .deleted-leads-table { overflow: visible; }
  .deleted-leads-table-head { display: none; }
  .deleted-leads-list { gap: 10px; padding: 0 12px 12px; }
  .deleted-lead-row { min-width: 0; grid-template-columns: 1fr; gap: 10px; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
  .contracts-table { overflow: visible; }
  .contracts-table-head { display: none; }
  .contracts-list { gap: 10px; padding: 0 12px 12px; }
  .contract-row { min-width: 0; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
  .contract-cell { display: grid; gap: 4px; }
  .contract-cell::before { content: attr(data-label); color: var(--muted); font-size: 11px; font-weight: 800; }
  .contract-client { grid-column: 1 / -1; grid-template-columns: auto minmax(0, 1fr); }
  .contract-client::before { grid-column: 1 / -1; }
  .call-widget { right: 12px; bottom: 12px; width: calc(100vw - 24px); }
  .call-widget-body { grid-template-columns: minmax(0, 1fr) minmax(105px, auto); }
}
