:root {
  --bg: #e6ecf6;
  --bg-2: #dde4f0;
  --panel: #ffffff;
  --panel-2: #f9fafd;
  --border: #dde2ec;
  --border-soft: #e5e9f1;
  --text: #1a1f2c;
  --muted: #5f6878;
  --muted-2: #8b94a6;
  --accent: #4361ee;
  --accent-2: #5b3ee5;
  --violet: #8b5cf6;
  --teal: #10b981;
  --red: #ef4444;
  --amber: #f59e0b;
  --shadow: 0 10px 30px rgba(20,28,52,.08), 0 2px 6px rgba(20,28,52,.04);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); font-family: 'Inter', system-ui, sans-serif; font-size: 14px; }
body { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--bg-2);
  border-right: 1px solid var(--border-soft);
  padding: 18px 14px;
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 6px 22px; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px;
  background: radial-gradient(120% 120% at 30% 30%, #6a5cff 0%, #1f2bff 60%, #0b0d14 100%);
  box-shadow: 0 0 0 1px #2a2f48 inset, 0 6px 18px rgba(106,92,255,.35);
}
.brand-name { font-weight: 700; font-size: 16px; letter-spacing: .2px; }
.brand-name span { font-weight: 500; color: var(--muted); }
.nav-section { font-size: 11px; letter-spacing: .12em; color: var(--muted-2); padding: 6px 10px; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 10px;
  color: var(--muted); cursor: pointer;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--panel); color: var(--text); }
.nav-item.active {
  background: rgba(67,97,238,.12);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px rgba(67,97,238,.25);
}

.user {
  display: flex; align-items: center; gap: 10px;
  padding: 10px; border-top: 1px solid var(--border-soft); margin-top: 8px;
}
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: linear-gradient(135deg, #6a5cff, #4f7cff);
  display: grid; place-items: center; font-weight: 700; color: white;
}
.user-name { font-weight: 600; }
.user-sub { color: var(--muted); font-size: 12px; }

/* ---------- Main ---------- */
.main { padding: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 28px; border-bottom: 1px solid var(--border-soft);
  background: rgba(255,255,255,.78);
  backdrop-filter: saturate(160%) blur(8px);
  -webkit-backdrop-filter: saturate(160%) blur(8px);
  position: sticky; top: 0; z-index: 5;
}
.search {
  display: flex; align-items: center; gap: 10px;
  background: var(--panel); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 12px; width: 380px;
  color: var(--muted);
}
.search input {
  background: transparent; border: 0; outline: 0;
  color: var(--text); width: 100%;
}
.topbar-right { display: flex; align-items: center; gap: 10px; }
.pill {
  padding: 6px 10px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--border);
  color: var(--muted); font-size: 12px;
}
.icon-btn {
  background: var(--panel); border: 1px solid var(--border);
  width: 34px; height: 34px; border-radius: 10px;
  display: grid; place-items: center; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { color: var(--text); border-color: var(--text); }

.page { padding: 28px; }
.page.hidden { display: none; }
.page-head {
  display: flex; align-items: end; justify-content: space-between;
  margin-bottom: 22px;
}
.page-head h1 { margin: 0 0 6px; font-size: 26px; font-weight: 700; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 14px; border-radius: 9px;
  border: 1px solid transparent; cursor: pointer; font-weight: 600;
  font-size: 13px;
}
.btn-primary {
  background: linear-gradient(180deg, #5b86ff, #3e6df0);
  color: white; border-color: #4870e0;
  box-shadow: 0 6px 16px rgba(67,97,238,.35);
}
.btn-primary:hover { filter: brightness(1.05); }
.btn-ghost {
  background: var(--panel); color: var(--text); border-color: var(--border);
}
.btn-ghost:hover { background: var(--panel-2); }
.btn-danger {
  background: rgba(239,68,68,.1); color: #dc2626; border-color: rgba(239,68,68,.35);
}
.btn-sm { padding: 6px 10px; font-size: 12px; }

/* ---------- Stats ---------- */
.stat-row {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px;
  margin-bottom: 20px;
}
.stat {
  display: flex; align-items: center; gap: 14px;
  padding: 18px 20px; border-radius: 14px;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border-soft);
  box-shadow: var(--shadow);
}
.stat-icon {
  width: 44px; height: 44px; border-radius: 11px;
  display: grid; place-items: center;
}
.stat-icon--violet { background: rgba(139,92,246,.14); color: #7c3aed; }
.stat-icon--teal   { background: rgba(16,185,129,.14); color: #059669; }
.stat-icon--red    { background: rgba(239,68,68,.14); color: #dc2626; }
.stat-icon--amber  { background: rgba(245,158,11,.14); color: #b45309; }
.stat-label { color: var(--muted); font-size: 12px; }
.stat-value { font-size: 26px; font-weight: 700; }

/* Chevron used to expand/collapse the device row's employee list on the
   Biometrics page. <span> defaults to a text I-beam cursor; pointer makes
   the clickability obvious. user-select prevents the arrow from getting
   highlighted on rapid clicks. */
.chev {
  cursor: pointer;
  display: inline-block;
  width: 14px;
  text-align: center;
  user-select: none;
  color: var(--muted, #6b7280);
}
.chev:hover { color: var(--text, #111827); }

/* ---------- Tables ---------- */
.table-card {
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border-soft);
  border-radius: 14px; overflow-x: auto; box-shadow: var(--shadow);
}
.table-card::-webkit-scrollbar { height: 9px; }
.table-card::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.table-card::-webkit-scrollbar-thumb:hover { background: var(--muted-2); }
.table-card::-webkit-scrollbar-track { background: transparent; }

.table { width: 100%; min-width: max-content; border-collapse: collapse; }
.table th {
  text-align: left; font-size: 11px; letter-spacing: .08em; color: var(--muted);
  font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--border-soft);
  background: rgba(15,23,42,.025);
  white-space: nowrap;
  position: sticky; top: 0; z-index: 1;
}
.table th.actions { text-align: right; }
.table td {
  padding: 11px 12px; border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
  max-width: 280px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.table td[colspan] {
  max-width: none; white-space: normal; overflow: visible; text-overflow: clip;
}
.table td.cell-wrap {
  max-width: none; white-space: normal; overflow: visible; text-overflow: clip;
}
.table td:has(.row-actions) {
  max-width: none; overflow: visible; text-overflow: clip; text-align: right;
}
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover { background: rgba(15,23,42,.04); }
.empty { text-align: center; color: var(--muted); padding: 40px 0 !important; }
.cell-mono { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; color: var(--muted); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

/* badges */
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600;
  border: 1px solid transparent;
}
.badge-online  { background: rgba(16,185,129,.12); color: #047857; border-color: rgba(16,185,129,.35); }
.badge-offline { background: rgba(239,68,68,.10); color: #b91c1c; border-color: rgba(239,68,68,.35); }
.badge-pending { background: rgba(245,158,11,.10); color: #b45309; border-color: rgba(245,158,11,.35); }
.badge-sent    { background: rgba(67,97,238,.12); color: #2547d4; border-color: rgba(67,97,238,.35); }
.badge-done    { background: rgba(16,185,129,.12); color: #047857; border-color: rgba(16,185,129,.35); }
.badge-failed  { background: rgba(239,68,68,.10); color: #b91c1c; border-color: rgba(239,68,68,.35); }
.badge-role    { background: rgba(139,92,246,.12); color: #6d28d9; border-color: rgba(139,92,246,.35); }
.badge-role-normal { background: var(--panel-2); color: var(--muted); border-color: var(--border); }

.dot { width: 6px; height: 6px; border-radius: 50%; display: inline-block; }
.dot-online { background: #4adba0; box-shadow: 0 0 0 3px rgba(16,185,129,.25); }
.dot-offline { background: #ff6e6e; }
.dot-pending { background: #f5c46d; box-shadow: 0 0 0 3px rgba(245,158,11,.20); }

/* ---------- Filter row ---------- */
.filter-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; margin-bottom: 14px;
  background: var(--panel); border: 1px solid var(--border-soft);
  border-radius: 12px;
}
.filter-row label { color: var(--muted); font-size: 12px; }
.filter-row select, .filter-row input {
  background: var(--bg-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 7px 10px; font-size: 13px; min-width: 220px;
}

/* ---------- Reports ---------- */
.report-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.report-card {
  display: flex; align-items: center; gap: 14px;
  padding: 18px 20px; border-radius: 14px;
  background: linear-gradient(180deg, var(--panel), var(--panel-2));
  border: 1px solid var(--border-soft); cursor: pointer;
  transition: transform .1s, border-color .15s;
}
.report-card:hover { transform: translateY(-1px); border-color: rgba(67,97,238,.4); }
.report-icon {
  width: 42px; height: 42px; border-radius: 11px;
  display: grid; place-items: center;
  background: rgba(67,97,238,.12); color: #2547d4;
}
.report-title { font-weight: 600; }

/* ---------- Modal ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(15,23,42,.45); backdrop-filter: blur(4px);
  display: grid; place-items: center; z-index: 50;
}
.modal.hidden { display: none; }
.modal-card {
  width: min(520px, 92vw);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px; box-shadow: 0 30px 60px rgba(15,23,42,.18);
  animation: pop .15s ease-out;
}
@keyframes pop { from { transform: scale(.97); opacity: 0 } to { transform: scale(1); opacity: 1 } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 20px; border-bottom: 1px solid var(--border-soft);
}
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-body { padding: 18px 20px; display: flex; flex-direction: column; gap: 8px; }
.modal-body label { font-size: 12px; color: var(--muted); margin-top: 8px; }
.modal-body input, .modal-body select {
  background: var(--bg-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 9px; padding: 9px 12px;
  font-size: 13px; outline: none;
  /* A select otherwise sizes to its widest <option> (long timezone names)
     and escapes the modal card. Fill the column instead. */
  width: 100%; max-width: 100%; box-sizing: border-box;
}
.modal-body input:focus, .modal-body select:focus { border-color: #4f7cff; }
.req { color: #dc2626; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
/* Grid items refuse to shrink below their content width by default
   (min-width:auto), which lets wide selects / device lists blow out the
   modal. Allow the columns to actually be 1fr. */
.grid-2 > div { min-width: 0; }
.checkbox { display: flex; align-items: center; gap: 8px; color: var(--text); margin-top: 14px; }
.checkbox input { width: 16px; height: 16px; }
.multi-check {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 6px 8px;
  max-height: 132px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
  font-size: 13px;
}
.multi-check label {
  display: flex; align-items: center; gap: 8px;
  margin: 0 !important; padding: 4px 4px;
  font-size: 13px; color: var(--text);
  cursor: pointer; border-radius: 6px;
  min-width: 0;
}
.multi-check label:hover { background: var(--panel-2); }
.multi-check input { width: 15px; height: 15px; margin: 0; flex: 0 0 auto; }
.multi-check .muted { font-size: 12px; padding: 6px 4px; }
.multi-check .mono { font-family: ui-monospace,Menlo,Consolas,monospace; }
/* Long "SERIAL — location" rows truncate instead of widening the column. */
.multi-check label span {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.multi-check-all {
  float: right;
  display: inline-flex !important; align-items: center; gap: 6px;
  font-size: 12px !important; color: var(--muted) !important;
  margin-top: 0 !important; cursor: pointer;
}
.multi-check-all input { width: 14px; height: 14px; margin: 0; }
.modal-foot {
  display: flex; justify-content: flex-end; gap: 8px;
  padding-top: 14px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed; top: 24px; left: 50%; transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--border);
  padding: 12px 16px; border-radius: 10px; box-shadow: var(--shadow);
  font-size: 13px; z-index: 100; min-width: 240px;
}
.toast.hidden { display: none; }
.toast.error { border-color: rgba(239,68,68,.4); }
.toast.ok    {
  background: #10b981;
  border-color: #059669;
  color: #fff;
  font-weight: 600;
}

code { background: var(--panel-2); padding: 1px 6px; border-radius: 4px; font-size: 12px; }

/* ---------- Role gating ---------- */
/* The body class is set to role-<Role> after /api/auth/me. Viewers see no
   write actions; Managers see no delete actions; Admins see everything. */
.role-Viewer #btnAddCompany,
.role-Viewer #btnAddEmp,
.role-Viewer #btnAddAgent,
.role-Viewer #btnAddBiometric,
.role-Viewer #btnAddUser,
.role-Viewer button[data-edit-co],
.role-Viewer button[data-edit-ag],
.role-Viewer button[data-edit-emp],
.role-Viewer button[data-edit-dev],
.role-Viewer button[data-edit-user],
.role-Viewer button[data-add-emp],
.role-Viewer button[data-sync-users],
.role-Viewer button[data-push],
.role-Viewer button[data-push-greythr],
.role-Viewer button[data-reset-pw] { display: none; }

.role-Viewer button[data-del],
.role-Viewer button[data-del-co],
.role-Viewer button[data-del-ag],
.role-Viewer button[data-del-user],
.role-Manager button[data-del],
.role-Manager button[data-del-co],
.role-Manager button[data-del-ag],
.role-Manager button[data-del-user],
.role-Manager #btnAddUser,
.role-Manager button[data-edit-user],
.role-Manager button[data-reset-pw] { display: none; }

/* ---------- Column filters ---------- */
.col-filter-btn {
  background: transparent; border: 1px solid var(--border); border-radius: 6px;
  width: 20px; height: 20px; padding: 0;
  font-size: 11px; line-height: 1; color: var(--muted); cursor: pointer;
  vertical-align: middle;
  float: right;
}
.col-filter-btn:hover { background: var(--panel-2); color: var(--text); }
.col-filter-btn.active { background: rgba(67,97,238,.12); color: var(--accent); border-color: rgba(67,97,238,.4); }

.col-filter-pop {
  position: absolute;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 10px;
  z-index: 50; min-width: 220px;
  display: flex; gap: 6px; align-items: center;
}
.col-filter-pop input {
  flex: 1; padding: 7px 9px; font-size: 13px; font-family: inherit;
  border: 1px solid var(--border); border-radius: 6px; background: #fff; color: var(--text);
  outline: none;
}
.col-filter-pop input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(67,97,238,.15); }
.col-filter-pop .clear-btn {
  background: transparent; border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; font-size: 12px; color: var(--muted); cursor: pointer;
}
.col-filter-pop .clear-btn:hover { background: var(--panel-2); color: var(--text); }

/* ---------- Email Settings page ---------- */
#page-email-settings .page-head h1 {
  font-size: 28px; letter-spacing: -.01em;
}
.email-settings-card {
  max-width: 760px;
  position: relative;
  background: #ffffff;
  border: 1px solid #e2e6ef;
  border-radius: 18px;
  box-shadow: 0 18px 40px -18px rgba(31, 35, 48, .18), 0 2px 6px rgba(31, 35, 48, .04);
  padding: 40px 44px;
  overflow: hidden;
}
.email-settings-card::before {
  content: ""; position: absolute; top: 0; left: 0; right: 0; height: 4px;
  background: linear-gradient(90deg, var(--accent) 0%, var(--accent-2) 100%);
}
.email-settings-hero {
  display: flex; gap: 18px; align-items: center;
  padding-bottom: 24px; margin-bottom: 28px;
  border-bottom: 1px solid #eef0f5;
}
.email-settings-hero h2 {
  margin: 0 0 4px 0; font-size: 18px; font-weight: 700;
  color: var(--text); letter-spacing: -.005em;
}
.email-settings-hero p { margin: 0; font-size: 13px; color: var(--muted); }
.email-settings-icon {
  width: 52px; height: 52px; border-radius: 14px;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  color: #ffffff;
  display: grid; place-items: center; flex-shrink: 0;
  box-shadow: 0 8px 20px -6px rgba(67,97,238,.45);
}
.email-settings-form .form-row { margin-bottom: 22px; }
.email-settings-form label {
  display: block; font-size: 11px; font-weight: 700;
  color: #4a5160; margin-bottom: 8px;
  letter-spacing: .09em; text-transform: uppercase;
}
.email-settings-form .req { color: #ef4444; margin-left: 2px; }
.email-settings-form input {
  width: 100%; padding: 12px 15px;
  background: #fafbfd; color: var(--text);
  border: 1px solid #dfe3ec; border-radius: 11px;
  font-size: 14px; outline: none;
  transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
  box-sizing: border-box;
  font-family: inherit;
}
.email-settings-form input::placeholder { color: #a0a8b8; }
.email-settings-form input:hover { background: #ffffff; border-color: #cdd3df; }
.email-settings-form input:focus {
  background: #ffffff;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(67,97,238,.12);
}
.email-settings-form .field-help {
  margin: 8px 2px 0; font-size: 12px; color: var(--muted);
}
.email-settings-actions {
  margin-top: 32px; padding-top: 24px;
  border-top: 1px solid #eef0f5;
  display: flex; justify-content: flex-end; gap: 10px;
}
.email-settings-actions .btn-primary {
  padding: 11px 26px; font-weight: 600; font-size: 13.5px;
  background: linear-gradient(135deg, var(--accent) 0%, var(--accent-2) 100%);
  border: none; color: #ffffff;
  box-shadow: 0 8px 18px -8px rgba(67,97,238,.55);
  transition: transform .12s ease, box-shadow .15s ease;
}
.email-settings-actions .btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 22px -10px rgba(67,97,238,.65);
  filter: none;
}
