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

:root {
  --accent:    #3b82f6;
  --accent-d:  #2563eb;
  --green:     #22c55e;
  --red:       #ef4444;
  --amber:     #f59e0b;

  /* Dark surfaces */
  --bg:        #0d0f14;
  --surface:   #14171f;
  --surface-2: #1a1e28;
  --sidebar:   #0a0c11;
  --border:    #262b36;
  --border-2:  #313846;

  /* Text */
  --text:      #e5e7eb;
  --text-dim:  #9aa4b2;
  --text-mute: #6b7480;

  --sidebar-w: 224px;
  --radius:    9px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px; color: var(--text); background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ── Login ─────────────────────────────────────────────── */
.login-page { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 1rem; }
.login-box {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  box-shadow: 0 12px 40px rgba(0,0,0,.5); padding: 2.5rem; width: 100%; max-width: 370px; text-align: center;
}
.login-logo { display: flex; flex-direction: column; align-items: center; gap: .5rem; margin-bottom: 1.75rem; }
.login-logo h1 { font-size: 1.35rem; font-weight: 700; letter-spacing: -.01em; }
.login-logo p { color: var(--text-dim); font-size: .85rem; }
.login-hint { margin-top: 1rem; font-size: .85rem; color: var(--text-dim); line-height: 1.5; }
.alert { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; font-size: .85rem; text-align: left; }
.alert-error   { background: rgba(239,68,68,.12);  color: #fca5a5; border: 1px solid rgba(239,68,68,.25); }
.alert-success { background: rgba(34,197,94,.12);   color: #86efac; border: 1px solid rgba(34,197,94,.25); }

/* ── App layout ─────────────────────────────────────────── */
.app-page { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w); background: var(--sidebar); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; flex-shrink: 0; position: fixed; top: 0; left: 0; height: 100vh;
}
.sidebar-logo {
  display: flex; align-items: center; gap: .6rem; padding: 1.25rem 1rem; font-weight: 700;
  font-size: .95rem; letter-spacing: .02em; border-bottom: 1px solid var(--border);
}
.sidebar-nav { flex: 1; padding: .75rem .5rem; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: .7rem; padding: .6rem .75rem; color: var(--text-dim);
  text-decoration: none; border-radius: var(--radius); transition: all .12s; font-size: .875rem; font-weight: 500;
}
.nav-item svg { width: 18px; height: 18px; flex-shrink: 0; }
.nav-item:hover { color: var(--text); background: rgba(255,255,255,.04); }
.nav-item.active { color: #fff; background: rgba(59,130,246,.14); }
.nav-item.active svg { stroke: var(--accent); }
.sidebar-user { padding: 1rem; border-top: 1px solid var(--border); font-size: .8rem; color: var(--text-mute); }
.sidebar-user span { display: block; margin-bottom: .25rem; font-weight: 600; color: var(--text-dim); }
.logout-link { color: var(--text-mute); text-decoration: none; }
.logout-link:hover { color: var(--accent); }

.main-content { margin-left: var(--sidebar-w); flex: 1; padding: 2rem 2.5rem; max-width: 1160px; }

/* ── Sections ───────────────────────────────────────────── */
.section { display: none; }
.section.active { display: block; animation: fade .15s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
.section-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.5rem; }
.section-header h2 { font-size: 1.3rem; font-weight: 700; letter-spacing: -.01em; }

/* ── Cards ──────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.25rem; margin-bottom: 1rem;
}
.card-list { display: flex; flex-direction: column; gap: .75rem; }
.empty-note { color: var(--text-dim); font-size: .875rem; line-height: 1.55; }

/* ── Stat tiles ─────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: .85rem; margin-bottom: 1.25rem; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem 1.25rem;
}
.stat-value { font-size: 1.75rem; font-weight: 700; letter-spacing: -.02em; }
.stat-label { font-size: .78rem; color: var(--text-dim); margin-top: .2rem; text-transform: uppercase; letter-spacing: .04em; }

/* ── Tables ─────────────────────────────────────────────── */
.data-table { width: 100%; border-collapse: collapse; font-size: .85rem; }
.data-table th {
  text-align: left; padding: .6rem .8rem; color: var(--text-mute); font-weight: 600;
  border-bottom: 1px solid var(--border); text-transform: uppercase; font-size: .72rem; letter-spacing: .04em;
}
.data-table td { padding: .7rem .8rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover td { background: rgba(255,255,255,.02); }
.table-empty { text-align: center; color: var(--text-mute); padding: 2rem !important; }

/* ── Badges ─────────────────────────────────────────────── */
.badge { display: inline-block; padding: .18rem .5rem; border-radius: 999px; font-size: .72rem; font-weight: 600; }
.badge-green { background: rgba(34,197,94,.15);  color: #86efac; }
.badge-gray  { background: rgba(255,255,255,.07); color: var(--text-dim); }
.badge-blue  { background: rgba(59,130,246,.15);  color: #93c5fd; }
.badge-amber { background: rgba(245,158,11,.15);  color: #fcd34d; }
.badge-red   { background: rgba(239,68,68,.15);   color: #fca5a5; }

/* ── Buttons ────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; gap: .4rem; padding: .5rem 1rem; border-radius: var(--radius);
  border: 1px solid transparent; font-size: .85rem; font-weight: 600; cursor: pointer; transition: all .12s;
}
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-d); border-color: var(--accent-d); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border-2); }
.btn-secondary:hover { background: #222735; }
.btn-danger { background: var(--red); color: #fff; }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-sm { padding: .35rem .65rem; font-size: .8rem; }

/* ── Forms ──────────────────────────────────────────────── */
.form-group { margin-bottom: 1rem; text-align: left; }
.form-group label { display: block; font-weight: 600; margin-bottom: .4rem; font-size: .82rem; color: var(--text-dim); }
input[type=text], input[type=email], input[type=number], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: .6rem .75rem; border: 1px solid var(--border-2); border-radius: var(--radius);
  font-size: .875rem; font-family: inherit; color: var(--text); background: var(--surface-2); transition: border .12s;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,130,246,.15);
}
textarea { resize: vertical; }
select option { background: var(--surface-2); }
.hint { color: var(--text-mute); font-size: .78rem; margin-top: .35rem; }
.form-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--border); }

/* ── Settings rows ──────────────────────────────────────── */
.setting-row { display: flex; justify-content: space-between; padding: .7rem 0; border-bottom: 1px solid var(--border); font-size: .875rem; }
.setting-row:last-child { border-bottom: none; }
.setting-label { color: var(--text-dim); }
.card-title { font-size: 1rem; font-weight: 700; margin-bottom: .35rem; display: flex; align-items: center; gap: .5rem; }
.card-desc { color: var(--text-dim); font-size: .82rem; margin-bottom: 1.15rem; line-height: 1.45; }
.int-status { font-weight: 400; margin-left: .4rem; }
.lbl-hint { color: var(--text-mute); font-weight: 400; font-size: .72rem; text-transform: none; letter-spacing: 0; }
.chk-row { display: flex; flex-wrap: wrap; gap: .5rem .9rem; }
.chk { display: inline-flex; align-items: center; gap: .35rem; font-weight: 500; font-size: .82rem; color: var(--text-dim); cursor: pointer; }
.chk input { width: auto; }

/* Settings form grid — uses horizontal space on wider screens */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .1rem 1.25rem; }
.form-grid .form-group.full, .form-group.full { grid-column: 1 / -1; }
@media (max-width: 620px) { .form-grid { grid-template-columns: 1fr; } }
.inline-actions { display: flex; gap: .5rem; margin-top: .9rem; }

/* Toggle switch */
.switch { display: inline-flex; align-items: center; gap: .6rem; cursor: pointer; font-weight: 500; font-size: .85rem; color: var(--text-dim); }
.switch input { display: none; }
.switch-slider { width: 38px; height: 22px; background: var(--border-2); border-radius: 999px; position: relative; transition: background .15s; flex-shrink: 0; }
.switch-slider::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; background: #fff; border-radius: 50%; transition: transform .15s; }
.switch input:checked + .switch-slider { background: var(--accent); }
.switch input:checked + .switch-slider::after { transform: translateX(16px); }
.contacts-head-row { display: flex; align-items: center; justify-content: space-between; margin-top: 1.5rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.contacts-head-row .contacts-heading { margin: 0; padding: 0; border: none; }

/* ── Template card ──────────────────────────────────────── */
.tpl-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem 1.25rem;
  display: flex; align-items: center; gap: 1rem;
}
.tpl-card-info { flex: 1; min-width: 0; }
.tpl-card-name { font-weight: 600; margin-bottom: .2rem; }
.tpl-card-subject { color: var(--text-dim); font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── Modal ──────────────────────────────────────────────── */
.modal { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.6); align-items: center; justify-content: center; z-index: 100; padding: 1rem; }
.modal.open { display: flex; }
.modal-box { background: var(--surface); border: 1px solid var(--border-2); border-radius: 14px; padding: 1.5rem; width: 100%; max-width: 520px; max-height: 90vh; overflow-y: auto; scrollbar-width: thin; scrollbar-color: var(--border-2) transparent; }
.modal-box::-webkit-scrollbar { width: 10px; }
.modal-box::-webkit-scrollbar-track { background: transparent; }
.modal-box::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 6px; border: 2px solid var(--surface); }
.modal-box::-webkit-scrollbar-thumb:hover { background: #3a4150; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; position: sticky; top: -1.5rem; background: var(--surface); padding: 1rem 0 .5rem; margin-top: -1rem; z-index: 2; }
.edit-actions { display: flex; justify-content: flex-end; padding-top: .25rem; }
.contacts-heading { margin-top: 1.5rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.modal-header h3 { font-size: 1.1rem; font-weight: 700; }
.modal-close { background: none; border: none; color: var(--text-mute); font-size: 1.5rem; cursor: pointer; line-height: 1; }
.modal-close:hover { color: var(--text); }

/* ── Toast ──────────────────────────────────────────────── */
.toast { display: none; position: fixed; bottom: 1.5rem; right: 1.5rem; padding: .8rem 1.2rem; border-radius: var(--radius); font-size: .875rem; font-weight: 500; z-index: 200; box-shadow: 0 8px 30px rgba(0,0,0,.4); }
.toast.success { display: block; background: var(--green); color: #04140a; }
.toast.error   { display: block; background: var(--red); color: #fff; }

.loading { color: var(--text-mute); font-size: .875rem; padding: 1rem; }
.row-actions { display: flex; gap: .4rem; justify-content: flex-end; white-space: nowrap; }

/* ── Leads: filter bar, pagination, details modal ───────── */
.filter-bar { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; }
.filter-bar input[type=search] { flex: 1; min-width: 180px; }
.filter-bar select { width: auto; min-width: 130px; }
.lead-row { cursor: pointer; }
.details-cell { text-align: right; color: var(--text-mute); }
.lead-row:hover .details-cell { color: var(--accent); }
.pagination { display: flex; align-items: center; justify-content: center; gap: 1rem; padding: .85rem; border-top: 1px solid var(--border); }
.pag-info { font-size: .82rem; color: var(--text-dim); }

.modal-lg { max-width: 680px; }
.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .1rem .5rem; margin-bottom: 1.25rem; }
.detail-row { display: flex; justify-content: space-between; gap: 1rem; padding: .45rem .1rem; border-bottom: 1px solid var(--border); font-size: .85rem; }
.detail-label { color: var(--text-mute); }
.muted { color: var(--text-mute); font-size: .85rem; padding: .4rem 0; }

.contacts-nested { width: 100%; border-collapse: collapse; }
.contacts-nested th { text-align: left; font-size: .68rem; padding: .5rem .8rem; color: var(--text-mute); text-transform: uppercase; letter-spacing: .04em; border-bottom: 1px solid var(--border); }
.contacts-nested td { padding: .55rem .8rem; border-bottom: 1px solid var(--border); font-size: .82rem; }
.contacts-nested tr:last-child td { border-bottom: none; }
.contacts-nested a { color: var(--accent); text-decoration: none; }
.contacts-nested a:hover { text-decoration: underline; }

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 720px) {
  .sidebar { width: 64px; }
  .sidebar-logo span, .nav-item span, .sidebar-user span, .sidebar-user a, .nav-item { font-size: 0; }
  .nav-item { justify-content: center; }
  .nav-item svg { width: 20px; height: 20px; }
  .sidebar-logo { justify-content: center; }
  .main-content { margin-left: 64px; padding: 1.25rem; }
}
