:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #e0e3e8;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --ok: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --radius: 8px;
}

* { box-sizing: border-box; }

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

.layout { display: flex; min-height: 100vh; }

nav.sidebar {
  width: 230px;
  flex-shrink: 0;
  background: #14172150;
  background: #171b26;
  color: #e5e7eb;
  padding: 20px 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

nav.sidebar .brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 20px 14px 20px;
}
.brand-mark {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 7px;
}
.brand-text { min-width: 0; line-height: 1.25; }
.brand-name {
  display: block;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.02em;
  color: #fff;
}
.brand-tagline {
  display: block;
  font-size: 10px;
  color: #8b93a7;
  white-space: normal;
}

nav.sidebar .tz-note {
  font-size: 11px;
  color: #8b93a7;
  padding: 0 20px 18px 20px;
  border-bottom: 1px solid #2a2f3d;
  margin-bottom: 12px;
  cursor: help;
}

nav.sidebar a {
  display: block;
  padding: 10px 20px;
  color: #cbd2e1;
  text-decoration: none;
  font-size: 13.5px;
}

nav.sidebar a:hover { background: #232838; color: #fff; }
nav.sidebar a.active { background: var(--accent); color: #fff; font-weight: 500; }

/* --- Sidebar user info + sign out (templates/base.html) -- pinned to
   the bottom of the sidebar via margin-top: auto (nav.sidebar is a
   flex column), so it stays put regardless of how many nav links are
   above it (which varies by role -- see nav_sections). --- */
.sidebar-user {
  margin-top: auto;
  padding: 14px 20px 4px 20px;
  border-top: 1px solid #2a2f3d;
}
.sidebar-user-name { font-size: 13px; font-weight: 600; color: #fff; }
.sidebar-user-role { font-size: 11px; color: #8b93a7; margin: 2px 0 10px 0; }
.sidebar-user-link {
  display: block;
  font-size: 12px;
  color: #cbd2e1;
  text-decoration: none;
  padding: 4px 0;
}
.sidebar-user-link:hover { color: #fff; }
.sidebar-logout {
  width: 100%;
  margin-top: 6px;
  background: none;
  border: 1px solid #2a2f3d;
  color: #cbd2e1;
  font-size: 12.5px;
  padding: 7px 0;
}
.sidebar-logout:hover { background: #232838; color: #fff; border-color: #3a4054; }

main {
  flex: 1;
  /* Without this, a flex item's default min-width is "auto", which
     stops it shrinking below the intrinsic width of whatever's inside
     it -- a wide table would then push the WHOLE page into horizontal
     scroll instead of just .table-wrap below scrolling on its own.
     min-width: 0 lets main shrink normally and hands overflow handling
     to .table-wrap where it belongs. */
  min-width: 0;
  padding: 28px 32px;
}

h1 { font-size: 20px; margin: 0 0 4px 0; }
h2 { font-size: 16px; margin: 28px 0 10px 0; }
.subtitle { color: var(--muted); margin: 0 0 20px 0; font-size: 13px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  margin-bottom: 18px;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}

.card a { text-decoration: none; color: inherit; display: block; }
.card .number { font-size: 26px; font-weight: 600; margin: 4px 0; }
.card .label { color: var(--muted); font-size: 12.5px; }
.card.alert-nonzero .number { color: var(--bad); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
}

th, td {
  text-align: left;
  padding: 7px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  max-width: 320px;
  overflow: hidden;
  text-overflow: ellipsis;
}

th {
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.02em;
  position: sticky;
  top: 0;
  background: var(--panel);
}

tr:hover td { background: #fafbfc; }

.table-wrap { overflow-x: auto; }

.empty-state {
  color: var(--muted);
  padding: 18px 0;
  font-style: italic;
}

.error-box {
  background: #fff4f4;
  border: 1px solid #f3c9c9;
  color: #8a1f1f;
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 12.5px;
  margin-bottom: 14px;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: #e5e7eb;
  color: #374151;
}
.badge-medium { background: #fef3c7; color: #92400e; }
.badge-high { background: #fee2e2; color: #b91c1c; }
.badge-critical { background: #7f1d1d; color: #fff; }

.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill-on { background: #dcfce7; color: #166534; }
.pill-off { background: #f3f4f6; color: #4b5563; }

.flash { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 14px; font-size: 13px; }
.flash-success { background: #ecfdf3; color: #166534; border: 1px solid #bbf7d0; }
.flash-error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.flash-warning { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }

form.inline { display: inline; }

.recipient-form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.recipient-form label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.recipient-form input[type=text],
.recipient-form select {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  min-width: 180px;
  font-family: inherit;
}

button, .btn {
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:hover, .btn:hover { background: var(--accent-dark); }
button.secondary, .btn.secondary { background: #fff; color: var(--text); border-color: var(--border); }
button.secondary:hover, .btn.secondary:hover { background: #f3f4f6; }
button.danger { background: #fff; color: var(--bad); border-color: #f3c9c9; }
button.danger:hover { background: #fef2f2; }
/* A disabled button here always means "this is the currently active
   choice" (see e.g. Settings' polling-override buttons) rather than
   "unavailable" -- keep it legible instead of the usual heavy fade. */
button:disabled, .btn:disabled { cursor: default; opacity: 0.85; }
button:disabled:hover, .btn:disabled:hover { background: var(--accent); }

.toggle-switch-form { display: flex; align-items: center; gap: 14px; margin: 10px 0; }

/* --- Settings page: per-alert-type SMS mute rows (templates/settings.html) --- */
.alert-type-rows { margin: 10px 0; }
.alert-type-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.alert-type-row:last-child { border-bottom: none; }
.alert-type-row form { margin: 0; }

.search-box input {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  width: 260px;
  margin-bottom: 14px;
}

.meta-row { color: var(--muted); font-size: 12.5px; margin: 6px 0 16px 0; }

@media (max-width: 800px) {
  .layout { flex-direction: column; }
  nav.sidebar { width: 100%; padding: 10px 0; min-height: 0; }
  nav.sidebar a { display: inline-block; padding: 8px 12px; }
  .sidebar-user { margin-top: 0; width: 100%; }
  main { padding: 18px; }
}

/* --- Page loader: top progress bar + delayed spinner overlay ---
   See the script at the bottom of base.html for when these classes
   get added/removed. The bar animates toward (not to) 90% so it never
   visibly "completes" -- the whole page gets replaced by the browser
   once the real response arrives, same trick tools like NProgress use. */
#page-loader-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: 3px;
  width: 0;
  background: var(--accent);
  z-index: 1000;
  opacity: 0;
  transition: width 4s cubic-bezier(0.1, 0.6, 0.2, 1), opacity 0.15s ease;
}
#page-loader-bar.is-loading {
  width: 90%;
  opacity: 1;
  transition: width 4s cubic-bezier(0.1, 0.6, 0.2, 1), opacity 0.1s ease;
}

#page-loader-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.55);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
}
#page-loader-overlay.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.page-loader-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 22px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--text);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.12);
}

.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* --- Alerts page: per-row "Investigated" action (templates/alerts.html) ---
   The actions cell holds a <details> element, not plain text -- undo
   the generic th/td truncation rules (max-width/overflow/nowrap) that
   would otherwise clip the expanded form. */
.actions-cell {
  max-width: none;
  overflow: visible;
  white-space: normal;
  text-overflow: clip;
  vertical-align: top;
}

.investigate summary {
  cursor: pointer;
  list-style: none;
}
.investigate summary::-webkit-details-marker { display: none; }
.investigate[open] summary { margin-bottom: 8px; }

.investigate-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  min-width: 220px;
  max-width: 320px;
}

.investigate-form label {
  font-size: 11.5px;
  color: var(--muted);
}

.investigate-form textarea,
.investigate-form input[type=text],
.investigate-form select {
  font-family: inherit;
  font-size: 12.5px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  white-space: normal;
}

.investigate-form button {
  align-self: flex-start;
  font-size: 12.5px;
  padding: 6px 12px;
}

/* --- Alerts page: Level 5/6 tampering rows colored by current location
   (templates/alerts.html's tamper_row_class, db/18_windhoek_geofence.sql's
   in_windhoek) -- red inside Windhoek, amber outside it. Solid, fairly
   saturated fills (not a subtle wash) since this is meant to read as an
   unmissable "urgent, act on this row" signal, with white text for
   contrast. tr:hover's default background (line ~125) would otherwise
   wash these out on hover, so both states redeclare a (slightly darker)
   background of their own for the hover case too. */
tr.row-tamper-windhoek td { background: #dc2626; color: #fff; }
tr.row-tamper-outside td { background: #d97706; color: #fff; }
tr.row-tamper-windhoek:hover td { background: #b91c1c; }
tr.row-tamper-outside:hover td { background: #b45309; }
tr.row-tamper-windhoek td[title], tr.row-tamper-outside td[title] { color: #fff; }

.legend-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  vertical-align: middle;
  margin: 0 2px;
}
.legend-swatch-red { background: #dc2626; }
.legend-swatch-amber { background: #d97706; }

/* --- Manual refresh link (templates/base.html) --- Pages don't
   auto-update (see base.html's comment for why the old SSE-driven
   auto-refresh was removed); this is the sidebar link that reloads the
   current page on demand. Styled like the nav links above it but
   visually distinct (pill/outline) so it doesn't read as another page
   to navigate to. */
nav.sidebar a.refresh-link {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0 20px 16px 20px;
  padding: 6px 12px;
  border: 1px solid #2a2f3d;
  border-radius: 999px;
  font-size: 12.5px;
  color: #cbd2e1;
  width: fit-content;
}
nav.sidebar a.refresh-link:hover { background: #232838; color: #fff; border-color: #3a4054; }
.refresh-icon { font-size: 13px; line-height: 1; }

/* --- Rules page: visual pipeline builder (templates/rules.html) ---
   The six-level ladder is a fixed pipeline, not a free-form graph --
   see the AskUserQuestion decision: "Visual pipeline, draggable
   tolerances/toggles", not a general flowchart editor. Cards are
   chained top-to-bottom with .rule-arrow connectors; each tunable
   card (levels 1/2/4/5) has an enable switch and up to two range
   sliders, structural cards (levels 3/6) have just the switch. */
.rules-pipeline {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  max-width: 640px;
  margin: 0 auto;
}

.rule-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: 16px 20px;
  transition: opacity 0.15s ease;
}

.rule-card-structural {
  border-left-color: var(--warn);
  background: #fffdf7;
}

.rule-card.rule-disabled {
  opacity: 0.55;
  border-left-color: var(--border);
}

.rule-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.rule-card-head h3 { margin: 0; font-size: 14.5px; flex: 1; }

.rule-desc { margin: 0; font-size: 12.5px; color: var(--text); line-height: 1.5; }
.rule-desc strong { color: var(--accent-dark); }

.rule-note {
  margin: 10px 0 0 0;
  padding: 8px 10px;
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-radius: 6px;
  font-size: 12px;
  color: #92400e;
}

.rule-slider { margin: 12px 0 0 0; }
.rule-slider label {
  display: block;
  font-size: 11.5px;
  color: var(--muted);
  margin-bottom: 4px;
}
.rule-slider input[type=range] {
  width: 100%;
  accent-color: var(--accent);
}
.rule-card.rule-disabled input[type=range] { accent-color: var(--muted); }

.rule-arrow {
  text-align: center;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  margin: 4px 0;
}

/* Toggle switch -- per-level enable/disable (levels 1-6, rules.html)
   and used nowhere else yet, but generic enough to reuse. */
.switch {
  position: relative;
  display: inline-block;
  width: 38px;
  height: 22px;
  flex-shrink: 0;
}
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-slider {
  position: absolute;
  inset: 0;
  background: #d1d5db;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease;
}
.switch-slider::before {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.15s ease;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}
.switch input:checked + .switch-slider { background: var(--ok); }
.switch input:checked + .switch-slider::before { transform: translateX(16px); }
.switch input:focus-visible + .switch-slider { outline: 2px solid var(--accent); outline-offset: 2px; }

.rules-save-bar {
  /* Deliberately NOT position:sticky -- with six tall cards above it,
     a sticky bar ends up floating mid-page and overlapping whatever
     card it happens to land on while scrolling (confirmed visually).
     It's the form's last element, "at the bottom" as the page's own
     copy says, not a persistent toolbar. */
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 18px auto 0 auto;
  max-width: 640px;
  padding: 14px 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

@media (max-width: 700px) {
  .rules-pipeline, .rules-save-bar { max-width: 100%; }
}

/* --- Click-through detail panel (templates/base.html, opened from any
   table rendered with detail=true -- currently the Alerts page's five
   tables, templates/alerts.html). A row that opens it gets a pointer
   cursor and a slightly stronger hover than the plain tr:hover above,
   so it reads as clickable rather than just highlighted; the panel
   itself slides in from the right over a dimmed backdrop. --- */
tr.row-detail-trigger { cursor: pointer; }
tr.row-detail-trigger:hover td { background: #eef2ff; }
/* The tamper-level red/amber rows (row-tamper-windhoek/-outside) set
   their own background above and must stay legible on hover -- don't
   let this weaker highlight override that. */
tr.row-tamper-windhoek.row-detail-trigger:hover td,
tr.row-tamper-outside.row-detail-trigger:hover td { background: inherit; }

#detail-panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 18, 28, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease;
  z-index: 1010;
}
#detail-panel-backdrop.is-open { opacity: 1; pointer-events: auto; }

#detail-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 380px;
  max-width: 92vw;
  background: var(--panel);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.12);
  z-index: 1011;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.18s ease;
}
#detail-panel.is-open { transform: translateX(0); }

.detail-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.detail-panel-header h2 { margin: 0; font-size: 15px; }
#detail-panel-close {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  padding: 2px 6px;
  cursor: pointer;
}
#detail-panel-close:hover { color: var(--text); background: #f3f4f6; border-radius: 6px; }

#detail-panel-body {
  margin: 0;
  overflow-y: auto;
}

/* Each group is one labeled chunk of the panel (Vehicle, Rental,
   Renter, Tampering, Tracking -- see app.py's DETAIL_PANEL_SECTIONS),
   alternately shaded so a long row's data reads as a handful of
   distinct sections instead of one flat wall of fields (per the
   request in chat: the all-white list "felt very overwhelming"). */
.detail-group {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.detail-group:last-child { border-bottom: none; }
.detail-group-alt { background: var(--bg); }

.detail-group h3 {
  margin: 0 0 8px 0;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent-dark);
}

.detail-group dl { margin: 0; }
.detail-group dt {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  color: var(--muted);
  margin-top: 10px;
}
.detail-group dt:first-child { margin-top: 0; }
.detail-group dd {
  margin: 3px 0 0 0;
  font-size: 13.5px;
  color: var(--text);
  word-break: break-word;
}

@media (max-width: 800px) {
  #detail-panel { width: 100vw; max-width: 100vw; }
}

/* --- Templates page: SMS wording editor (templates/templates.html) --- */
.template-textarea {
  width: 100%;
  font-family: inherit;
  font-size: 13px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  line-height: 1.5;
}
.template-actions { margin-top: 8px; }

/* --- Login page (templates/login.html) -- standalone, doesn't extend
   base.html (no sidebar to show before anyone's logged in), so it
   needs its own body/centering rules rather than reusing .layout. --- */
.login-body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding: 20px;
}
.login-wrap { width: 100%; max-width: 360px; }
.login-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 28px 28px 28px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  text-align: center;
}
.login-logo { width: 100%; max-width: 220px; height: auto; margin-bottom: 8px; }
.login-form { text-align: left; margin-top: 18px; }
.login-form label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 14px 0 4px 0;
}
.login-form label:first-child { margin-top: 0; }
.login-form input {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 14px;
  font-family: inherit;
}
.login-form button {
  width: 100%;
  margin-top: 18px;
  padding: 10px;
  font-size: 14px;
}
.login-card .flash { text-align: left; margin-top: 16px; margin-bottom: 0; }

/* --- Users & Roles admin screens (templates/users.html, roles.html) --- */
.role-form-basics {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.role-form-basics > div { flex: 1; min-width: 200px; }
.role-form-basics label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}
.role-form-basics input[type=text] {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
}
.role-section-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 8px 16px;
  margin-bottom: 16px;
  padding: 12px 14px;
  background: var(--bg);
  border-radius: 6px;
}
.role-section-checkbox {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
}
.role-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.role-badges { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.role-edit summary {
  cursor: pointer;
  list-style: none;
}
.role-edit summary::-webkit-details-marker { display: none; }
.role-edit[open] summary { margin-bottom: 12px; }
.role-edit .role-form { min-width: 320px; }

/* Full-width equivalent of .rules-save-bar above, for pages (like
   Templates) whose panels aren't constrained to that page's narrower
   640px pipeline column. */
.form-save-bar {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 18px;
  padding: 14px 20px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
