:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #1b2433;
  --muted: #667085;
  --border: #e4e7ec;
  --primary: #2f5bea;
  --primary-hover: #2449c4;
  --primary-soft: #eaf0ff;
  --danger: #d92d20;
  --danger-soft: #fef3f2;
  --ok: #067647;
  --ok-soft: #ecfdf3;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Cairo', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  font-size: 15px;
  line-height: 1.6;
}

button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.3; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- controls ---------- */

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 14px; font-weight: 600; }
.field .req { color: var(--danger); }

input, select, textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.field.invalid input, .field.invalid select { border-color: var(--danger); }
.field .err { color: var(--danger); font-size: 13px; min-height: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .6; cursor: progress; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-hover); }
.btn.danger { color: var(--danger); border-color: #fecdca; background: var(--danger-soft); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }

.alert { padding: 10px 14px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); margin: 0; }

/* ---------- public form ---------- */

.form-page { padding: 32px 16px; }
.form-card {
  max-width: 760px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 32px;
}
.form-card h1 { font-size: 26px; }
.form-card > .muted { margin: 6px 0 24px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.form-grid .full { grid-column: 1 / -1; }
.form-actions { margin-top: 20px; display: flex; flex-direction: column; gap: 12px; }
.form-actions .btn { padding: 12px; font-size: 16px; }
.hp { position: absolute; inset-inline-start: -9999px; width: 1px; height: 1px; opacity: 0; }
.success { text-align: center; padding: 32px 8px; }
.success .check {
  width: 64px; height: 64px; margin: 0 auto 16px;
  display: grid; place-items: center;
  border-radius: 50%; background: var(--ok-soft); color: var(--ok); font-size: 32px;
}

body.embedded { background: transparent; padding: 0; }
body.embedded .form-card { box-shadow: none; border: 0; padding: 8px; max-width: none; }

@media (max-width: 640px) {
  .form-page { padding: 16px; }
  .form-card { padding: 20px 16px; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- admin: login ---------- */

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 28px;
  display: flex; flex-direction: column; gap: 14px;
}

/* ---------- admin: shell ---------- */

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 20px; margin-inline-end: auto; }
.topbar .user { color: var(--muted); font-size: 14px; }

.page { padding: 20px 24px 40px; max-width: 1500px; margin: 0 auto; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow);
}
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 28px; font-weight: 700; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat.hot .value { color: #b42318; }

.layout { display: grid; grid-template-columns: 270px minmax(0, 1fr); gap: 20px; align-items: start; }

.sidebar { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 76px; max-height: calc(100vh - 92px); overflow-y: auto; }
.facet { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.facet summary { padding: 10px 14px; font-weight: 700; cursor: pointer; list-style: none; display: flex; justify-content: space-between; }
.facet summary::-webkit-details-marker { display: none; }
.facet summary::after { content: '▾'; color: var(--muted); }
.facet:not([open]) summary::after { content: '◂'; }
.facet ul { list-style: none; margin: 0; padding: 0 6px 8px; }
.facet button {
  width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 6px 8px; border: 0; border-radius: 6px; background: none; cursor: pointer; text-align: start;
}
.facet button:hover { background: var(--surface-2); }
.facet button.active { background: var(--primary-soft); color: var(--primary); font-weight: 700; }
.facet .count { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.facet .empty { color: var(--muted); font-size: 13px; padding: 4px 8px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 12px; }
.toolbar .search { flex: 1 1 240px; }
.toolbar .field { flex: 0 1 160px; }
.toolbar .field > span { font-size: 12px; color: var(--muted); font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 6px 3px 10px; border-radius: 999px;
  background: var(--primary-soft); color: var(--primary); font-size: 13px; font-weight: 600;
}
.chip button { border: 0; background: none; color: inherit; cursor: pointer; font-size: 15px; line-height: 1; padding: 0 4px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 820px; }
th, td { padding: 10px 14px; text-align: start; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 13px; color: var(--muted); font-weight: 700; background: var(--surface-2); white-space: nowrap; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }
td .sub { color: var(--muted); font-size: 13px; }
td .name { font-weight: 700; }
.phone { font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty-state { padding: 48px 16px; text-align: center; color: var(--muted); }

.pager { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 14px; border-top: 1px solid var(--border); color: var(--muted); font-size: 14px; }
.pager .btns { display: flex; gap: 6px; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 700; white-space: nowrap; }
.p-high   { background: #fee4e2; color: #b42318; }
.p-medium { background: #fef0c7; color: #93370d; }
.p-low    { background: #f2f4f7; color: #475467; }
.s-new         { background: #eaf0ff; color: #2449c4; }
.s-contacted   { background: #f4ebff; color: #6941c6; }
.s-interested  { background: #e0f2fe; color: #026aa2; }
.s-negotiation { background: #fef0c7; color: #93370d; }
.s-won         { background: #dcfae6; color: #067647; }
.s-lost        { background: #f2f4f7; color: #475467; }
.repeat { font-size: 12px; color: var(--muted); margin-inline-start: 4px; }

/* ---------- admin: lead dialog ---------- */

dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(16, 24, 40, .25);
  color: var(--text);
}
dialog::backdrop { background: rgba(16, 24, 40, .45); }
.dlg-head {
  position: sticky; top: 0; z-index: 1;
  display: flex; align-items: center; gap: 10px;
  padding: 16px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.dlg-head h2 { font-size: 20px; margin-inline-end: auto; }
.icon-btn { border: 0; background: none; font-size: 24px; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); }
.dlg-body { padding: 20px; display: flex; flex-direction: column; gap: 20px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.details { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 20px; margin: 0; }
.details div { min-width: 0; }
.details dt { color: var(--muted); font-size: 13px; }
.details dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.details .full { grid-column: 1 / -1; }
.details .message { white-space: pre-wrap; font-weight: 400; background: var(--surface-2); padding: 10px 12px; border-radius: 8px; }
.controls { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; padding: 14px; background: var(--surface-2); border-radius: var(--radius); }
.score-note { font-size: 13px; color: var(--muted); }
.notes h3 { font-size: 16px; margin-bottom: 10px; }
.note-form { display: flex; gap: 8px; align-items: start; margin-bottom: 12px; }
.note-form textarea { flex: 1; }
.note-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.note-list li { border: 1px solid var(--border); border-radius: 8px; padding: 8px 12px; }
.note-list .meta { color: var(--muted); font-size: 12.5px; }
.note-list .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.dlg-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding-top: 8px; border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; }

.toast {
  position: fixed; bottom: 20px; inset-inline-start: 20px; z-index: 50;
  background: #1b2433; color: #fff; padding: 10px 16px; border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .2); font-weight: 600;
}

@media (max-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; max-height: none; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
}
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; }
  .page { padding: 16px 16px 32px; }
  .details, .controls { grid-template-columns: minmax(0, 1fr); }
  .toolbar .field { flex: 1 1 140px; }
}
