/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --danger-dark: #b91c1c;
  --bg: #f8fafc;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.06);

  --badge-lead: #dbeafe;
  --badge-lead-text: #1e40af;
  --badge-prospect: #fef3c7;
  --badge-prospect-text: #92400e;
  --badge-client: #dcfce7;
  --badge-client-text: #166534;
  --badge-inactive: #f1f5f9;
  --badge-inactive-text: #475569;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  font-size: 14px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* Header */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 32px;
}

.logo {
  font-weight: 700;
  font-size: 18px;
  color: var(--primary);
  letter-spacing: .5px;
}

nav { display: flex; gap: 8px; }

/* Cloud Sync Button */
.sync-status-btn {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  font-size: 15px;
  cursor: pointer;
  color: var(--muted);
  transition: background .15s, color .15s;
}
.sync-status-btn:hover { background: var(--bg); color: var(--text); }

.sync-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #cbd5e1;
  display: inline-block;
  transition: background .3s;
}
.sync-dot-on { background: #22c55e; }

/* Header right group */
.header-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }

/* Hamburger — mobile only */
.hamburger {
  display: none;
  margin-left: auto;
  background: none;
  border: none;
  font-size: 22px;
  color: var(--text);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: var(--radius);
  line-height: 1;
}
.hamburger:hover { background: var(--bg); }

/* Side Drawer */
.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 40;
  backdrop-filter: blur(2px);
}
.drawer-backdrop.open { display: block; }

.side-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 260px;
  background: var(--surface);
  z-index: 50;
  display: flex;
  flex-direction: column;
  box-shadow: -4px 0 24px rgba(0,0,0,.15);
  transform: translateX(100%);
  transition: transform .25s ease;
}
.side-drawer.open { transform: translateX(0); }

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}

.drawer-close {
  background: none;
  border: none;
  font-size: 18px;
  color: var(--muted);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius);
}
.drawer-close:hover { background: var(--bg); color: var(--text); }

.drawer-nav {
  display: flex;
  flex-direction: column;
  padding: 12px 12px 0;
}

.drawer-link {
  text-decoration: none;
  color: var(--text);
  font-weight: 500;
  font-size: 15px;
  padding: 12px 16px;
  border-radius: var(--radius);
  transition: background .15s;
}
.drawer-link:hover { background: var(--bg); }
.drawer-link.active { background: #eff6ff; color: var(--primary); }

.drawer-divider {
  height: 1px;
  background: var(--border);
  margin: 12px 0;
}

.drawer-action {
  background: none;
  border: none;
  text-align: left;
  padding: 12px 28px;
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background .15s;
}
.drawer-action:hover { background: var(--bg); }
.drawer-signout { color: var(--danger); }

nav a {
  text-decoration: none;
  color: var(--muted);
  font-weight: 500;
  padding: 6px 14px;
  border-radius: var(--radius);
  transition: background .15s, color .15s;
}

nav a:hover { background: var(--bg); color: var(--text); }
nav a.active { background: #eff6ff; color: var(--primary); }

/* Main */
main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 32px 24px;
}

.view { display: flex; flex-direction: column; gap: 24px; }
.hidden { display: none !important; }

/* Stat Cards */
.stat-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  cursor: pointer;
  transition: box-shadow .15s, transform .1s;
  box-shadow: var(--shadow);
}

.stat-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,.12); transform: translateY(-1px); }

.stat-card .count {
  font-size: 36px;
  font-weight: 700;
  color: var(--primary);
  line-height: 1;
  margin-bottom: 6px;
}

.stat-card .label { color: var(--muted); font-weight: 500; font-size: 13px; text-transform: uppercase; letter-spacing: .5px; }

/* Contacts View */
.contacts-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
.header-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.export-toast {
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
}
.export-toast-ok  { background: #dcfce7; color: #166534; }
.export-toast-err { background: #fee2e2; color: #991b1b; }

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

.filters input, .filters select {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color .15s;
}

.filters input { flex: 1; min-width: 200px; }
.filters input:focus, .filters select:focus { border-color: var(--primary); }

/* Table */
#contacts-table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  max-height: calc(100vh - 260px);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#contacts-table-wrap::-webkit-scrollbar { width: 7px; height: 7px; }
#contacts-table-wrap::-webkit-scrollbar-track { background: transparent; }
#contacts-table-wrap::-webkit-scrollbar-thumb { background: var(--border); border-radius: 99px; }
#contacts-table-wrap::-webkit-scrollbar-corner { background: transparent; }

table { width: 100%; border-collapse: collapse; min-width: 1100px; }

thead th {
  background: #f8fafc;
  position: sticky;
  top: 0;
  z-index: 1;
  text-align: left;
  padding: 12px 16px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  position: relative;
  overflow: visible;
}

.col-resizer {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 5px;
  cursor: col-resize;
  user-select: none;
  z-index: 2;
  border-right: 2px solid #94a3b8;
  transition: border-color .15s;
}
.col-resizer:hover  { border-color: var(--primary); }
.col-resizer.resizing { border-color: var(--primary); border-width: 3px; }

tbody tr { border-bottom: 1px solid var(--border); transition: background .1s; }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

td { padding: 12px 16px; vertical-align: middle; }

.cell-name { font-weight: 500; }
.cell-muted { color: var(--muted); }
.cell-actions { display: flex; gap: 8px; }

/* Badges */
.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 600;
}

.badge-Lead { background: var(--badge-lead); color: var(--badge-lead-text); }
.badge-Prospect { background: var(--badge-prospect); color: var(--badge-prospect-text); }
.badge-Client { background: var(--badge-client); color: var(--badge-client-text); }
.badge-Inactive { background: var(--badge-inactive); color: var(--badge-inactive-text); }

.badge-cat { background: #ede9fe; color: #5b21b6; }

.cell-source a { color: var(--primary); text-decoration: none; font-size: 13px; }
.cell-source a:hover { text-decoration: underline; }

#compose-contact-list label:hover { background: #f8fafc; }
#compose-contact-list label:last-child { border-bottom: none; }

.btn-email-contact {
  background: none;
  border: none;
  padding: 0;
  color: var(--primary);
  font-size: 13px;
  cursor: pointer;
  text-align: left;
}
.btn-email-contact:hover { text-decoration: underline; }

/* Sortable headers */
.th-sort { cursor: pointer; user-select: none; white-space: nowrap; }
.th-sort:hover { color: var(--primary); }
.th-sorted { color: var(--primary); }
.sort-icon { font-size: 11px; }

/* Buttons */
.btn-primary, .btn-secondary, .btn-danger, .btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s;
}

.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary { background: #f1f5f9; color: var(--text); }
.btn-secondary:hover { background: #e2e8f0; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-dark); }

.btn-icon {
  padding: 4px 10px;
  font-size: 13px;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
}

.btn-icon:hover { background: var(--bg); color: var(--text); }

.btn-icon-danger { color: var(--danger); border-color: #fecaca; }
.btn-icon-danger:hover { background: #fff1f2; }

/* Empty State */
.empty-state { text-align: center; color: var(--muted); padding: 48px; }

/* Modal / Dialog */
dialog {
  border: none;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,.18);
  padding: 0;
  max-width: 580px;
  width: 90vw;
  background: var(--surface);
}

dialog::backdrop { background: rgba(0,0,0,.4); backdrop-filter: blur(2px); }

dialog h2 { font-size: 18px; font-weight: 700; padding: 24px 24px 0; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 20px 24px;
}

.form-grid label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 500; color: var(--muted); }
.form-grid label .required { color: var(--danger); }

.form-grid input, .form-grid select, .form-grid textarea {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  transition: border-color .15s;
  font-family: inherit;
}

.form-grid input:focus, .form-grid select:focus, .form-grid textarea:focus { border-color: var(--primary); }
.form-grid textarea { resize: vertical; }
.form-grid .full-width { grid-column: 1 / -1; }

.form-error {
  color: var(--danger);
  font-size: 13px;
  padding: 0 24px 8px;
  margin-top: -8px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  background: #f8fafc;
  border-radius: 0 0 12px 12px;
}

.delete-content { padding: 24px; display: flex; flex-direction: column; gap: 16px; }
.delete-content h2 { padding: 0; }
.delete-content .modal-actions { padding: 0; border: none; background: none; border-radius: 0; }

/* FAB — mobile only */
.fab {
  display: none;
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 28px;
  border: none;
  box-shadow: 0 4px 16px rgba(37,99,235,.4);
  cursor: pointer;
  z-index: 20;
  align-items: center;
  justify-content: center;
  transition: background .15s, transform .1s;
}
.fab:hover { background: var(--primary-dark); transform: scale(1.05); }

/* ── Mobile ─────────────────────────────────────────────────────────── */
@media (max-width: 640px) {
  /* Header */
  .header-inner { padding: 0 16px; gap: 16px; }
  nav, .header-right { display: none; }
  .hamburger { display: block; }

  /* Main */
  main { padding: 16px; }

  /* Dashboard cards — 2 columns on mobile */
  .stat-cards { grid-template-columns: 1fr 1fr; gap: 12px; }
  .stat-card { padding: 16px; }
  .stat-card .count { font-size: 28px; }

  /* Contacts header — stack */
  .contacts-header { flex-direction: column; align-items: stretch; }
  .contacts-header h1 { font-size: 20px; }
  .header-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .header-actions .btn-primary { display: none; } /* replaced by FAB */

  /* Filters — full width */
  .filters { flex-direction: column; }
  .filters input { min-width: unset; width: 100%; }
  .filters select { width: 100%; }

  /* FAB */
  .fab { display: inline-flex; }

  /* Table → Card layout */
  #contacts-table-wrap {
    background: transparent;
    border: none;
    box-shadow: none;
    overflow: visible;
    max-height: none;
  }

  table { min-width: unset; width: 100%; }
  table, thead, tbody, tr, td { display: block; }
  thead { display: none; }

  tbody tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 12px;
    padding: 4px 0;
  }

  tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
  }

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

  tbody td::before {
    content: attr(data-label);
    font-weight: 600;
    font-size: 11px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .5px;
    flex-shrink: 0;
    margin-right: 12px;
  }

  /* Hide less important columns on mobile */
  tbody td[data-label="Source"] { display: none; }

  /* Name cell — larger */
  .cell-name { font-size: 15px; font-weight: 600; }

  /* Actions — right aligned */
  .cell-actions { justify-content: flex-end; }
  .cell-actions::before { display: none; }

  /* Modal — full screen */
  dialog {
    width: 100vw;
    max-width: 100vw;
    border-radius: 16px 16px 0 0;
    position: fixed;
    bottom: 0;
    top: auto;
    margin: 0;
    max-height: 92vh;
    overflow-y: auto;
  }

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

  .modal-actions { position: sticky; bottom: 0; }
}

/* ── Login Screen ───────────────────────────────────────────────────────── */
#login-screen {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0,0,0,.12);
  padding: 40px;
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.login-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 22px;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: .5px;
}

.login-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--muted);
}

.login-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 15px;
  outline: none;
  transition: border-color .15s;
}

.login-card input:focus { border-color: var(--primary); }

.login-error {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  min-height: 18px;
}

.btn-login {
  width: 100%;
  padding: 11px;
  font-size: 15px;
  font-weight: 600;
}
