:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --border: #e3e7ee;
  --text: #1a2230;
  --muted: #6b7686;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --danger-hover: #b91c1c;
  --success: #16a34a;
  --shadow: 0 4px 16px rgba(20, 30, 50, 0.08);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

.screen { min-height: 100vh; }
.muted { color: var(--muted); }
.center { text-align: center; }
.error { color: var(--danger); margin: 8px 0 0; }

/* ---------- Login ---------- */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 32px;
  width: 100%;
  max-width: 380px;
}

.login-card h1 {
  margin: 0 0 4px;
  font-size: 22px;
}

.login-card p { margin: 0 0 20px; }

/* ---------- Forms ---------- */
form label {
  display: block;
  margin-bottom: 14px;
  font-weight: 500;
  font-size: 13px;
  color: var(--text);
}

form input,
form select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}

form input:focus,
form select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.password-wrap {
  position: relative;
  display: block;
  margin-top: 6px;
}
.password-wrap input {
  margin-top: 0;
  padding-right: 40px;
}

/* ---------- Buttons ---------- */
button {
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  border-radius: 8px;
  padding: 9px 14px;
  border: 1px solid transparent;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

button:disabled { opacity: 0.6; cursor: not-allowed; }

#login-submit,
.primary {
  background: var(--primary);
  color: #fff;
  width: 100%;
  font-weight: 600;
  margin-top: 4px;
}

.primary { width: auto; }
#login-submit:hover:not(:disabled),
.primary:hover:not(:disabled) { background: var(--primary-hover); }

.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
.ghost:hover:not(:disabled) { background: #eef1f7; }

.password-toggle {
  position: absolute;
  top: 50%;
  right: 6px;
  transform: translateY(-50%);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--muted);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.password-toggle:hover { color: var(--primary); background: rgba(37, 99, 235, 0.08); }
.password-toggle:focus-visible {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.password-toggle svg { display: block; }

.danger {
  background: var(--danger);
  color: #fff;
}
.danger:hover:not(:disabled) { background: var(--danger-hover); }

/* ---------- App shell ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 60;
}

.brand { font-weight: 700; font-size: 16px; }

.tabs { display: flex; gap: 4px; }

.tab {
  background: transparent;
  border: none;
  padding: 8px 14px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 500;
}

.tab.active {
  background: #eef2fb;
  color: var(--primary);
}

.header-right { display: flex; align-items: center; gap: 12px; }

.app-main { padding: 24px; max-width: 1200px; margin: 0 auto; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.toolbar input[type="search"] {
  flex: 1;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface);
}

.toolbar input[type="search"]:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

/* ---------- Table (Users tab uses plain <table>) ---------- */
.table-wrap {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: visible;
}

table {
  width: 100%;
  border-collapse: collapse;
}

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

tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: #f8fafc; }

/* ---------- Data grid (Data tab subtabs) ----------
   CSS Grid based "table". Sticky columns work reliably here unlike <table>. */
#tab-data .table-wrap {
  max-height: calc(100vh - var(--data-sticky-h, 120px) - var(--data-toolbar-h, 56px) - 120px);
  overflow: auto;
}

.dg {
  display: block;
  min-width: 100%;
  width: max-content;
  font-size: 14px;
}

.dg-row {
  display: grid;
  grid-template-columns: var(--dg-cols);
  align-items: stretch;
  border-bottom: 1px solid var(--border);
}

.dg-body .dg-row:last-child { border-bottom: none; }

.dg-cell {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  min-width: 0;            /* allow content to shrink/ellipsize */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background: var(--surface);
}

/* Header row */
.dg-head {
  position: sticky;
  top: 0;
  z-index: 4;
}
.dg-head .dg-cell {
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
}

/* Sticky columns (any column with sticky:true, including #) */
.dg-cell.dg-sticky {
  position: sticky;
  z-index: 2;
}
.dg-head .dg-cell.dg-sticky { z-index: 5; }

/* The # column gets a subtle right separator AND right-aligned numbers */
.dg-cell.dg-num {
  justify-content: flex-end;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Visual separator on the LAST sticky cell of each row. We don't know which
   column is last-sticky from CSS alone, so apply a subtle shadow to all
   sticky cells; only the rightmost-visible one shows because the next cell
   covers the others. */
.dg-cell.dg-sticky {
  box-shadow: 1px 0 0 var(--border);
}

/* Row hover & click affordance */
.dg-row-click { cursor: pointer; }
.dg-body .dg-row-click:hover .dg-cell,
.dg-body .dg-row:hover .dg-cell { background: #eef2fb; }

/* Empty/loading/error placeholder spans the full grid width */
.dg-row.dg-empty { grid-template-columns: 1fr; }
.dg-cell.dg-fullspan {
  justify-content: center;
  white-space: normal;
  color: var(--muted);
}
.dg-cell.dg-fullspan.error { color: var(--danger); }

/* Inline links inside cells inherit the cell layout but render as text */
.dg-cell a.cell-link {
  color: var(--primary);
  text-decoration: none;
}
.dg-cell a.cell-link:hover { text-decoration: underline; }

.actions-col { width: 1%; white-space: nowrap; }

.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.row-actions button {
  padding: 5px 10px;
  font-size: 12px;
}

.role-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  background: #eef2fb;
  color: var(--primary);
}
.role-badge.manager { background: #fef3c7; color: #92400e; }

/* ---------- Pagination ---------- */
.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 16px;
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 100;
}

.modal {
  background: var(--surface);
  border-radius: 12px;
  padding: 24px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow);
}

.modal h2 { margin: 0 0 16px; font-size: 18px; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #1a2230;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  z-index: 200;
  font-size: 13px;
  max-width: 90vw;
}

.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }

/* ---------- Sub-tabs (Data tab) ---------- */
/* The .data-sticky wrapper pins sub-tabs + master filter bar to the
   viewport (just under the app header). Use the opaque app surface
   colour so rows scrolling underneath cannot peek through. */
.data-sticky {
  position: sticky;
  top: 56px; /* roughly the app-header height */
  background: var(--bg);
  z-index: 40;
  /* Pull the bar to the panel's edges so there are no transparent gutters
     where row content can show through. Zero bottom margin so the sub-tab
     toolbar can stack flush below it. */
  margin: -24px -24px 0;
  padding: 12px 24px 12px;
  border-bottom: 1px solid var(--border);
}

.subtabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
  padding-bottom: 8px;
}

.subtab-btn {
  background: transparent;
  border: 1px solid transparent;
  padding: 7px 14px;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 500;
  font-size: 13px;
}

.subtab-btn:hover { background: #eef1f7; }

.subtab-btn.active {
  background: var(--primary);
  color: #fff;
}

.subtab-panel { display: none; }
.subtab-panel.active { display: block; }

/* ---------- DataTable inline toolbar ---------- */
.dt-toolbar {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.dt-toolbar:empty { display: none; margin-bottom: 0 !important; }

/* Inside the Data tab, pin the sub-tab toolbar just below the sticky
   sub-tab + master-filter block. --data-sticky-h is set by JS based on
   the actual height of .data-sticky (it can wrap on narrow screens). */
#tab-data .dt-toolbar {
  position: sticky;
  /* Subtract 1px so the toolbar overlaps .data-sticky's bottom border,
     guaranteeing no transparent strip even with sub-pixel rounding. */
  top: calc(var(--data-sticky-h, 120px) - 1px);
  z-index: 30;
  background: var(--bg);
  /* Pull to edges so it visually merges with .data-sticky above. Add a
     bottom margin to leave a small gap between the toolbar and the table. */
  margin: 0 -24px 16px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
}

.dt-toolbar input[type="search"] {
  flex: 1;
  min-width: 200px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  background: var(--surface);
  font-family: inherit;
}

.dt-toolbar input[type="search"]:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.dt-toolbar .inline-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.dt-toolbar select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-family: inherit;
  font-size: 13px;
}

/* Bigger, more visible horizontal scrollbar on data tables (so users see the
   affordance for "more columns to the right"). */
#tab-data .table-wrap {
  scrollbar-width: auto;
  scrollbar-color: #94a3b8 #e2e8f0;
}
#tab-data .table-wrap::-webkit-scrollbar { height: 12px; }
#tab-data .table-wrap::-webkit-scrollbar-track {
  background: #e2e8f0;
  border-radius: 0 0 12px 12px;
}
#tab-data .table-wrap::-webkit-scrollbar-thumb {
  background: #94a3b8;
  border-radius: 6px;
  border: 2px solid #e2e8f0;
}
#tab-data .table-wrap::-webkit-scrollbar-thumb:hover { background: #64748b; }

/* ---------- Detail list (modal body) ---------- */
.detail-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 8px 16px;
  margin: 0;
}

.detail-list dt {
  font-weight: 600;
  color: var(--muted);
  font-size: 13px;
}

.detail-list dd {
  margin: 0;
  word-break: break-word;
}

.modal.modal-wide { max-width: 720px; }

.modal-body { max-height: 65vh; overflow-y: auto; }

.lightbox-img {
  max-width: 100%;
  max-height: 60vh;
  display: block;
  margin: 0 auto;
  border-radius: 8px;
}

.detail-photo-img {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: cover;
  border-radius: 8px;
  cursor: zoom-in;
  border: 1px solid var(--border);
}

.detail-photo-img:hover { opacity: 0.95; }

/* ---------- Side-by-side media row (photo + map) ---------- */
.media-row {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  display: grid;
  gap: 16px;
}

.media-row.one { grid-template-columns: 1fr; }
.media-row.two { grid-template-columns: 1fr 1fr; }

@media (max-width: 720px) {
  .media-row.two { grid-template-columns: 1fr; }
}

.media-cell { display: flex; flex-direction: column; gap: 8px; min-width: 0; }

.media-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.media-cell-actions { display: flex; gap: 8px; }

.map-container {
  width: 100%;
  height: 320px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}

/* When the modal-body scrolls, Leaflet controls should still sit above */
.leaflet-container { font: inherit; }

/* ---------- Master filter bar (Data tab) ---------- */
.master-filters {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 0 0;
}

.master-filters-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-right: 2px;
}

.master-filters .inline-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px 4px 2px 10px;
  height: 32px;
}

.master-filters .inline-label:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.master-filters input[type="date"],
.master-filters .inline-label select {
  border: none;
  background: transparent;
  padding: 4px 6px;
  font-family: inherit;
  font-size: 13px;
  color: var(--text);
  outline: none;
  cursor: pointer;
}

.master-filters input[type="date"] { min-width: 120px; }
.master-filters .inline-label select { padding-right: 18px; }

/* Visual divider between filter groups */
.master-filters .filter-sep {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 4px;
}

/* Reset button — slightly subdued so it's not the visual focus */
.master-filters .filter-reset {
  height: 32px;
  padding: 0 12px;
  font-size: 12px;
  margin-left: auto;
}

/* ---------- MultiSelect ---------- */
.ms-trigger {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 10px;
  height: 32px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
}

.ms-trigger:hover { background: #eef1f7; }

.ms-trigger.has-selection {
  border-color: var(--primary);
  color: var(--primary);
  background: #eef2fb;
}

.ms-caret { font-size: 10px; opacity: 0.6; }

.ms-panel {
  position: absolute;
  z-index: 200;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  width: 280px;
  display: flex;
  flex-direction: column;
}

.ms-search { padding: 10px; border-bottom: 1px solid var(--border); }

.ms-search input {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: var(--surface);
}

.ms-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.ms-bulk {
  display: flex;
  gap: 8px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}

.ms-bulk button {
  background: transparent;
  border: none;
  padding: 4px 6px;
  font-size: 12px;
  color: var(--primary);
  cursor: pointer;
  border-radius: 4px;
}

.ms-bulk button:hover { background: #eef2fb; }

.ms-list {
  max-height: 280px;
  overflow-y: auto;
  padding: 4px 0;
}

.ms-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13px;
}

.ms-item:hover { background: #f8fafc; }
.ms-item input { margin: 0; }

.ms-item-text {
  display: flex;
  flex-direction: column;
  line-height: 1.3;
  min-width: 0;
}

.ms-sublabel {
  font-size: 11px;
  color: var(--muted);
}

.ms-empty { padding: 12px; font-size: 13px; }

.ms-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  background: #f8fafc;
}

.ms-actions button {
  padding: 5px 12px;
  font-size: 12px;
  border-radius: 6px;
  font-family: inherit;
  cursor: pointer;
}

.ms-actions .ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.ms-actions .ghost:hover { background: #eef1f7; }

.ms-actions .primary {
  background: var(--primary);
  border: 1px solid var(--primary);
  color: #fff;
  width: auto;
  font-weight: 600;
}
.ms-actions .primary:hover { background: var(--primary-hover); }

/* Sub-filter slot in toolbar */
.sub-filter-host { display: inline-flex; }

/* Sort dropdown — push it to the right edge of the toolbar */
.dt-toolbar .sort-label { margin-left: auto; }

/* ---------- Cell links (e.g. lead name → history) ---------- */
.cell-link {
  color: var(--primary);
  cursor: pointer;
  text-decoration: none;
  font-weight: 500;
}
.cell-link:hover { text-decoration: underline; }

/* ---------- Lead History dialog ---------- */
.lh-header {
  display: flex;
  gap: 14px;
  padding: 14px;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 16px;
}

.lh-avatar {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-weight: 600;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.lh-meta { min-width: 0; flex: 1; }

.lh-name { font-size: 16px; font-weight: 600; }
.lh-sub  { font-size: 13px; margin-bottom: 8px; }

.lh-fields {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 12px;
  margin: 0;
  font-size: 13px;
}
.lh-fields dt { color: var(--muted); font-weight: 500; }
.lh-fields dd { margin: 0; }

.lh-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

@media (max-width: 720px) {
  .lh-grid { grid-template-columns: 1fr; }
}

.lh-section {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.lh-section-title {
  margin: 0;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

.lh-section-body { padding: 4px 0; max-height: 50vh; overflow-y: auto; }

.lh-empty { padding: 16px; font-size: 13px; }

.lh-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.lh-row {
  display: flex;
  gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  align-items: flex-start;
}
.lh-row:last-child { border-bottom: none; }
.lh-row:hover { background: #f8fafc; }

.lh-row-icon { font-size: 14px; line-height: 1.4; }

.lh-row-body { min-width: 0; flex: 1; }

.lh-row-main {
  font-size: 13px;
  line-height: 1.4;
  word-break: break-word;
}
.lh-row-sub  { font-size: 12px; margin-top: 2px; }

/* Help our DataTable's inline page-info breathe */
.pagination .dt-page-info { min-width: 180px; text-align: center; }
