/* ===========================================================================
   COMPONENT THEME
   Loaded after main.css / dashboard.css / map.css / camera.css and rebuilds
   every component on the token system in tokens.css.

   Why a layer rather than editing in place: the original stylesheets carry
   1,958 lines of working layout. Replacing the visual language without
   touching layout geometry keeps the restyle reversible and testable.
   =========================================================================== */

/* ---------------------------------------------------------------------------
   1. FOUNDATION
   --------------------------------------------------------------------------- */

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

html { -webkit-text-size-adjust: 100%; }

body {
  background: var(--surface-base);
  color: var(--text-default);
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: 'cv02', 'cv03', 'cv04', 'cv11';
}

h1, h2, h3, h4, h5 {
  font-family: var(--font-heading);
  line-height: var(--leading-tight);
  letter-spacing: var(--tracking-tight);
  color: var(--text-strong);
  margin: 0;
}

/* Tabular figures stop counters jittering as they update. */
.kpi-value, .data-table td, .stat-chip, code, .mono,
[id^="kpi-"], [id$="-count"], [id$="-badge"] {
  font-variant-numeric: tabular-nums;
}

::selection { background: rgb(16 185 129 / 0.3); color: #fff; }

/* ---------------------------------------------------------------------------
   2. FOCUS — the single biggest accessibility gap in the original build,
   which had zero focus styling. Keyboard users could not see where they were.
   :focus-visible so pointer users never see a ring they did not ask for.
   --------------------------------------------------------------------------- */

:focus { outline: none; }

a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible,
.nav-item:focus-visible,
.bottom-nav-item:focus-visible,
.demo-user-card:focus-visible {
  outline: none;
  box-shadow: var(--focus-ring);
  border-radius: var(--radius-sm);
  position: relative;
  z-index: var(--z-sticky);
}

/* Skip link — first tab stop, lets keyboard users bypass the nav. */
.skip-link {
  position: absolute;
  left: var(--space-4);
  top: -100px;
  z-index: var(--z-toast);
  padding: var(--space-3) var(--space-5);
  background: var(--green-600);
  color: #fff;
  font-weight: var(--weight-semibold);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  transition: top var(--duration-fast) var(--ease-out);
}
.skip-link:focus { top: var(--space-4); }

/* ---------------------------------------------------------------------------
   3. APP SHELL
   --------------------------------------------------------------------------- */

.app-header,
header.app-header {
  background: linear-gradient(180deg, var(--slate-900) 0%, rgb(15 23 42 / 0.96) 100%);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-subtle);
  box-shadow: var(--shadow-sm);
  min-height: var(--header-h);
  z-index: var(--z-header);
}

.sidebar,
aside.sidebar {
  background: linear-gradient(180deg, var(--slate-900) 0%, var(--slate-950) 100%);
  border-right: 1px solid var(--border-subtle);
}

/* Section labels failed AA at 3.75:1 in slate-500. Lifted and given
   letter-spacing so they read as structure, not body copy. */
.sidebar-section-title,
.nav-section-label,
.sidebar > div:not(.nav-item):not([class*="nav"]) {
  color: var(--text-subtle);
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
}

.nav-item,
.bottom-nav-item {
  position: relative;
  border-radius: var(--radius-md);
  color: var(--text-muted);
  font-weight: var(--weight-medium);
  min-height: var(--tap-min);
  transition:
    background-color var(--duration-fast) var(--ease-out),
    color var(--duration-fast) var(--ease-out),
    transform var(--duration-instant) var(--ease-out);
}

.nav-item:hover { background: var(--surface-hover); color: var(--text-default); }
.nav-item:active { transform: scale(0.985); }

.nav-item.active {
  background: linear-gradient(90deg, rgb(16 185 129 / 0.16) 0%, rgb(16 185 129 / 0.04) 100%);
  color: var(--text-brand);
  font-weight: var(--weight-semibold);
}

/* Active marker rail — communicates position without relying on colour alone. */
.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 60%;
  border-radius: 0 var(--radius-full) var(--radius-full) 0;
  background: var(--green-400);
  box-shadow: 0 0 12px rgb(52 211 153 / 0.6);
}

.nav-item i, .bottom-nav-item i { transition: transform var(--duration-fast) var(--ease-spring); }
.nav-item:hover i { transform: scale(1.1); }

.nav-badge {
  background: var(--status-danger-bg);
  color: var(--status-danger-fg);
  border: 1px solid var(--status-danger-border);
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  padding: 2px var(--space-2);
  border-radius: var(--radius-full);
}

/* ---------------------------------------------------------------------------
   4. BUTTONS
   --------------------------------------------------------------------------- */

.btn,
.btn-new-plantation,
.btn-logout,
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-normal);
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  user-select: none;
  transition:
    background-color var(--duration-fast) var(--ease-out),
    border-color var(--duration-fast) var(--ease-out),
    box-shadow var(--duration-fast) var(--ease-out),
    transform var(--duration-instant) var(--ease-out);
}

.btn { min-height: var(--control-h-md); padding: 0 var(--space-4); }
.btn:active:not(:disabled) { transform: translateY(1px); }

.btn:disabled,
.btn[disabled] { opacity: 0.45; cursor: not-allowed; pointer-events: none; }

.btn-primary,
.btn-new-plantation {
  background: linear-gradient(180deg, var(--green-500) 0%, var(--green-600) 100%);
  border-color: var(--green-500);
  color: var(--action-primary-text);
  box-shadow: var(--shadow-sm), var(--shadow-inset-top);
}
.btn-primary:hover:not(:disabled),
.btn-new-plantation:hover {
  background: linear-gradient(180deg, var(--green-400) 0%, var(--green-500) 100%);
  box-shadow: var(--shadow-glow-green), var(--shadow-inset-top);
}

.btn-secondary {
  background: var(--action-secondary-bg);
  border-color: var(--border-default);
  color: var(--action-secondary-text);
}
.btn-secondary:hover:not(:disabled) {
  background: var(--action-secondary-bg-hover);
  border-color: var(--border-strong);
}

.btn-danger {
  background: var(--action-danger-bg);
  border-color: var(--red-500);
  color: var(--action-danger-text);
}
.btn-danger:hover:not(:disabled) { background: var(--action-danger-bg-hover); }

.btn-sm {
  min-height: var(--control-h-sm);
  padding: 0 var(--space-3);
  font-size: var(--text-xs);
  gap: var(--space-1);
}

/* Icon-only controls: keep the visual box small but the hit area ≥44px. */
.btn-icon {
  position: relative;
  width: var(--control-h-md);
  height: var(--control-h-md);
  padding: 0;
  background: var(--action-secondary-bg);
  border-color: var(--border-subtle);
  color: var(--text-muted);
}
.btn-icon::after {
  content: '';
  position: absolute;
  inset: calc((var(--tap-min) - var(--control-h-md)) / -2);
}
.btn-icon:hover { background: var(--action-secondary-bg-hover); color: var(--text-strong); }

.btn-logout {
  min-height: var(--control-h-sm);
  padding: 0 var(--space-3);
  background: rgb(239 68 68 / 0.12);
  border-color: rgb(239 68 68 / 0.3);
  color: var(--red-on-dark);
  font-size: var(--text-xs);
}
.btn-logout:hover { background: rgb(239 68 68 / 0.22); color: #fff; }

/* ---------------------------------------------------------------------------
   5. CARDS & PANELS
   --------------------------------------------------------------------------- */

.kpi-card,
.chart-card,
.admin-card-section,
.dashboard-card,
.table-container,
.page-header-block {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--card-radius);
  box-shadow: var(--shadow-sm), var(--shadow-inset-top);
}

/* KPI tiles: a coloured top rail carries the status, the figure dominates. */
.kpi-card {
  position: relative;
  overflow: hidden;
  padding: var(--kpi-pad);
  transition: transform var(--duration-normal) var(--ease-out),
              box-shadow var(--duration-normal) var(--ease-out),
              border-color var(--duration-normal) var(--ease-out);
}
.kpi-card::before {
  content: '';
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  background: linear-gradient(90deg, var(--green-400), var(--green-600));
  opacity: 0.9;
}
.kpi-card:nth-child(2)::before { background: linear-gradient(90deg, var(--blue-400), var(--blue-500)); }
.kpi-card:nth-child(3)::before { background: linear-gradient(90deg, var(--green-300), var(--green-500)); }
.kpi-card:nth-child(4)::before { background: linear-gradient(90deg, var(--amber-400), var(--amber-500)); }
.kpi-card:nth-child(5)::before { background: linear-gradient(90deg, var(--red-400), var(--red-500)); }

.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg), var(--shadow-inset-top);
  border-color: var(--border-default);
}

.kpi-value {
  font-family: var(--font-heading);
  font-size: var(--kpi-value-size);
  font-weight: var(--weight-black);
  line-height: 1;
  letter-spacing: var(--tracking-tight);
  color: var(--text-strong);
}

.kpi-label {
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-subtle);
}

.kpi-sublabel, .kpi-card p {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* ---------------------------------------------------------------------------
   6. DATA TABLES — the core of a monitoring tool; these get the most care.
   --------------------------------------------------------------------------- */

.data-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: var(--text-sm);
}

.data-table thead th {
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  background: var(--table-header-bg);
  backdrop-filter: blur(8px);
  padding: var(--space-3) var(--table-cell-pad-x);
  text-align: left;
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  color: var(--text-subtle);
  border-bottom: 1px solid var(--border-default);
  white-space: nowrap;
}

.data-table tbody td {
  padding: var(--table-cell-pad-y) var(--table-cell-pad-x);
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-default);
  vertical-align: middle;
}

.data-table tbody tr {
  transition: background-color var(--duration-fast) var(--ease-out);
}
.data-table tbody tr:hover { background: var(--surface-hover); }
.data-table tbody tr:last-child td { border-bottom: none; }

/* Row actions stay out of the way until the row is engaged — but remain
   visible on touch, where there is no hover. */
@media (hover: hover) {
  .data-table tbody tr .row-actions { opacity: 0.55; transition: opacity var(--duration-fast); }
  .data-table tbody tr:hover .row-actions,
  .data-table tbody tr:focus-within .row-actions { opacity: 1; }
}

.table-container { overflow: auto; -webkit-overflow-scrolling: touch; }

/* ---------------------------------------------------------------------------
   7. BADGES & STATUS
   Every badge pairs a tinted background with an AA-safe foreground and a
   border, so status survives greyscale printing and colour-blindness.
   --------------------------------------------------------------------------- */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: 3px var(--space-2);
  border-radius: var(--radius-full);
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  white-space: nowrap;
  border: 1px solid transparent;
  line-height: 1.5;
}

.badge.verified,
.badge-active,
.badge.healthy {
  background: var(--status-ok-bg);
  color: var(--status-ok-fg);
  border-color: var(--status-ok-border);
}

.badge.due_soon,
.badge.needs_care {
  background: var(--status-warn-bg);
  color: var(--status-warn-fg);
  border-color: var(--status-warn-border);
}

.badge.overdue,
.badge.damaged {
  background: var(--status-danger-bg);
  color: var(--status-danger-fg);
  border-color: var(--status-danger-border);
}

.badge-inactive,
.badge.unknown {
  background: var(--status-neutral-bg);
  color: var(--status-neutral-fg);
  border-color: var(--status-neutral-border);
}

/* A status dot, so compliance is not signalled by colour alone. */
.badge.verified::before,
.badge.due_soon::before,
.badge.overdue::before {
  content: '';
  width: 6px; height: 6px;
  border-radius: var(--radius-full);
  background: currentColor;
  flex-shrink: 0;
}
.badge.overdue::before { animation: pulse-dot 2s var(--ease-in-out) infinite; }

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.35; }
}

.stat-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  border: 1px solid transparent;
}
.stat-chip.verified { background: var(--status-ok-bg);     color: var(--status-ok-fg);     border-color: var(--status-ok-border); }
.stat-chip.due-soon { background: var(--status-warn-bg);   color: var(--status-warn-fg);   border-color: var(--status-warn-border); }
.stat-chip.overdue  { background: var(--status-danger-bg); color: var(--status-danger-fg); border-color: var(--status-danger-border); }

.user-avatar, .user-avatar-sm, .dept-icon-circle {
  box-shadow: var(--shadow-inset-top), var(--shadow-xs);
  font-weight: var(--weight-bold);
}

/* ---------------------------------------------------------------------------
   8. FORMS
   --------------------------------------------------------------------------- */

.form-control,
input[type="text"], input[type="password"], input[type="number"],
input[type="tel"], input[type="email"], input[type="search"],
select, textarea {
  width: 100%;
  min-height: var(--input-h);
  padding: var(--space-2) var(--space-3);
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: var(--radius-md);
  color: var(--text-default);
  font-family: var(--font-sans);
  /* 16px on mobile prevents iOS Safari from zooming on focus. */
  font-size: var(--text-md);
  transition: border-color var(--duration-fast) var(--ease-out),
              box-shadow var(--duration-fast) var(--ease-out),
              background-color var(--duration-fast) var(--ease-out);
}

@media (min-width: 768px) {
  .form-control, input, select, textarea { font-size: var(--text-base); }
}

.form-control::placeholder { color: var(--slate-500); }

.form-control:hover:not(:disabled) { border-color: var(--border-strong); }

.form-control:focus {
  outline: none;
  border-color: var(--input-border-focus);
  background: rgb(2 6 23 / 0.75);
  box-shadow: 0 0 0 3px rgb(16 185 129 / 0.16);
}

.form-control:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  background: rgb(2 6 23 / 0.3);
}

/* Validation feedback from the browser's own constraint API. */
.form-control:user-invalid {
  border-color: var(--red-500);
  box-shadow: 0 0 0 3px rgb(239 68 68 / 0.14);
}

.form-group { margin-bottom: var(--space-4); }

.form-group label {
  display: block;
  margin-bottom: var(--space-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--text-muted);
}

.form-group small { display: block; margin-top: var(--space-1); color: var(--text-subtle); }

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-4); }
@media (max-width: 640px) { .form-row { grid-template-columns: 1fr; } }

.all-caps { text-transform: uppercase; letter-spacing: var(--tracking-wide); }

/* ---------------------------------------------------------------------------
   9. MODALS
   --------------------------------------------------------------------------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: none;
  align-items: center;
  justify-content: center;
  padding: var(--space-4);
  background: rgb(2 6 23 / 0.78);
  backdrop-filter: blur(6px);
}
.modal-overlay.active { display: flex; animation: overlay-in var(--duration-fast) var(--ease-out); }

.modal-container {
  width: 100%;
  max-width: var(--modal-w);
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  background: var(--surface-overlay);
  border: 1px solid var(--border-default);
  border-radius: var(--modal-radius);
  box-shadow: var(--shadow-xl);
  animation: modal-in var(--duration-normal) var(--ease-out);
  overflow: hidden;
}

@keyframes overlay-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-in {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
  background: rgb(2 6 23 / 0.3);
  flex-shrink: 0;
}
.modal-header h3 { font-size: var(--text-lg); font-weight: var(--weight-bold); }

.modal-body { padding: var(--space-5); overflow-y: auto; flex: 1; }

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--border-subtle);
  background: rgb(2 6 23 / 0.3);
  flex-shrink: 0;
}

.btn-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background-color var(--duration-fast), color var(--duration-fast);
}
.btn-close:hover { background: rgb(239 68 68 / 0.16); color: var(--red-on-dark); }

/* ---------------------------------------------------------------------------
   10. TOASTS
   --------------------------------------------------------------------------- */

#toast-container {
  position: fixed;
  top: calc(var(--header-h) + var(--space-3));
  right: var(--space-4);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  max-width: min(400px, calc(100vw - var(--space-8)));
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface-overlay);
  border: 1px solid var(--border-default);
  border-left: 3px solid var(--slate-500);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  color: var(--text-default);
  font-size: var(--text-sm);
  pointer-events: auto;
  animation: toast-in var(--duration-normal) var(--ease-spring);
}
.toast.success { border-left-color: var(--green-500); }
.toast.error   { border-left-color: var(--red-500); }
.toast.warning { border-left-color: var(--amber-500); }
.toast.info    { border-left-color: var(--blue-500); }

@keyframes toast-in {
  from { opacity: 0; transform: translateX(28px); }
  to   { opacity: 1; transform: none; }
}

/* ---------------------------------------------------------------------------
   11. FOLLOW-UP QUEUE
   --------------------------------------------------------------------------- */

.followup-item-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  padding: var(--space-4);
  margin-bottom: var(--space-3);
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-left: 3px solid var(--slate-600);
  border-radius: var(--card-radius);
  box-shadow: var(--shadow-xs);
  transition: transform var(--duration-fast) var(--ease-out),
              box-shadow var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out);
}
.followup-item-card:hover {
  transform: translateX(3px);
  box-shadow: var(--shadow-md);
  border-color: var(--border-default);
}
.followup-item-card.verified { border-left-color: var(--green-500); }
.followup-item-card.due_soon { border-left-color: var(--amber-500); }
.followup-item-card.overdue  { border-left-color: var(--red-500); }

.followup-item-left { display: flex; align-items: center; gap: var(--space-4); min-width: 0; flex: 1; }

.followup-thumb {
  width: 60px; height: 60px;
  border-radius: var(--radius-md);
  object-fit: cover;
  border: 1px solid var(--border-default);
  flex-shrink: 0;
}

.followup-info { min-width: 0; }
.followup-info h4 {
  font-size: var(--text-base);
  font-weight: var(--weight-bold);
  color: var(--text-strong);
  margin-bottom: 2px;
  overflow-wrap: anywhere;
}
.followup-sub { font-size: var(--text-xs); color: var(--text-muted); }

/* ---------------------------------------------------------------------------
   12. LOGIN
   --------------------------------------------------------------------------- */

.login-screen-wrapper {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-6) var(--space-4);
  background:
    radial-gradient(1100px 600px at 15% -10%, rgb(16 185 129 / 0.12), transparent 60%),
    radial-gradient(900px 500px at 110% 110%, rgb(59 130 246 / 0.10), transparent 60%),
    var(--surface-base);
}

.login-card, .login-box, .login-container {
  background: var(--surface-raised);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-2xl);
  box-shadow: var(--shadow-xl), var(--shadow-inset-top);
}

.login-error-banner {
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
  background: var(--status-danger-bg);
  border: 1px solid var(--status-danger-border);
  border-radius: var(--radius-md);
  color: var(--status-danger-fg);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
}

.btn-login-submit {
  width: 100%;
  min-height: var(--control-h-lg);
  font-size: var(--text-md);
}

.demo-user-card {
  padding: var(--space-3);
  background: var(--surface-inset);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: border-color var(--duration-fast), background-color var(--duration-fast),
              transform var(--duration-instant);
}
.demo-user-card:hover { border-color: var(--border-brand); background: var(--surface-hover); }
.demo-user-card:active { transform: scale(0.985); }
.demo-user-card.selected {
  border-color: var(--green-500);
  background: rgb(16 185 129 / 0.10);
  box-shadow: var(--shadow-glow-green);
}

.btn-toggle-pwd {
  background: transparent;
  border: none;
  color: var(--text-subtle);
  cursor: pointer;
  padding: var(--space-2);
  border-radius: var(--radius-sm);
}
.btn-toggle-pwd:hover { color: var(--text-default); }

/* ---------------------------------------------------------------------------
   13. MAP
   --------------------------------------------------------------------------- */

.leaflet-popup-content-wrapper {
  background: var(--surface-overlay);
  color: var(--text-default);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  padding: 0;
  overflow: hidden;
}
.leaflet-popup-content { margin: 0; width: 290px !important; }
.leaflet-popup-tip { background: var(--surface-overlay); border: 1px solid var(--border-default); }
.leaflet-popup-close-button { color: var(--text-muted) !important; padding: var(--space-2) !important; }

.map-popup-image { width: 100%; height: 130px; object-fit: cover; display: block; }
.map-popup-body { padding: var(--space-3) var(--space-4) var(--space-4); }
.map-popup-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-2); }
.map-popup-unit {
  font-size: var(--text-base);
  font-weight: var(--weight-bold);
  color: var(--text-strong);
  margin-bottom: var(--space-2);
  overflow-wrap: anywhere;
}
.map-popup-meta { display: flex; flex-direction: column; gap: 2px; font-size: var(--text-xs); color: var(--text-muted); margin-bottom: var(--space-3); }
.map-popup-stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2);
  padding: var(--space-3); margin-bottom: var(--space-3);
  background: var(--surface-inset); border-radius: var(--radius-md);
}
.map-popup-stat-item { display: flex; flex-direction: column; gap: 1px; font-size: var(--text-2xs); }
.map-popup-stat-item span:first-child { color: var(--text-subtle); text-transform: uppercase; letter-spacing: var(--tracking-wide); }
.map-popup-stat-item span:last-child { color: var(--text-strong); font-weight: var(--weight-bold); font-size: var(--text-sm); }
.map-popup-actions { display: flex; gap: var(--space-2); }

.btn-popup-action {
  flex: 1;
  min-height: var(--control-h-sm);
  border-radius: var(--radius-md);
  border: 1px solid var(--border-default);
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  cursor: pointer;
  transition: background-color var(--duration-fast);
}
.btn-popup-action.primary { background: var(--green-600); border-color: var(--green-500); color: #fff; }
.btn-popup-action.primary:hover { background: var(--green-500); }
.btn-popup-action.secondary { background: var(--action-secondary-bg); color: var(--text-default); }
.btn-popup-action.secondary:hover { background: var(--action-secondary-bg-hover); }

.leaflet-control-layers, .leaflet-bar {
  background: var(--surface-overlay) !important;
  border: 1px solid var(--border-default) !important;
  border-radius: var(--radius-md) !important;
  box-shadow: var(--shadow-lg) !important;
  color: var(--text-default);
}
.leaflet-bar a { background: var(--surface-overlay) !important; color: var(--text-default) !important; border-bottom-color: var(--border-subtle) !important; }
.leaflet-bar a:hover { background: var(--surface-hover) !important; }

.map-filter-toolbar {
  background: rgb(15 23 42 / 0.92);
  backdrop-filter: blur(12px);
  border: 1px solid var(--border-default);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}

/* ---------------------------------------------------------------------------
   14. CHARTS
   --------------------------------------------------------------------------- */

.chart-card { padding: var(--card-pad); }
.chart-card h3, .chart-header h3 {
  font-size: var(--text-md);
  font-weight: var(--weight-bold);
  margin-bottom: var(--space-1);
}
.chart-header { display: flex; align-items: baseline; justify-content: space-between; gap: var(--space-3); margin-bottom: var(--space-4); }
.chart-header span { font-size: var(--text-xs); color: var(--text-subtle); }

/* ---------------------------------------------------------------------------
   15. PAGE HEADERS
   --------------------------------------------------------------------------- */

.page-header-block {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  padding: var(--space-5);
  margin-bottom: var(--space-5);
  background: linear-gradient(135deg, var(--surface-raised) 0%, var(--surface-default) 100%);
}
.page-title h2 { font-size: var(--text-2xl); font-weight: var(--weight-black); margin-bottom: var(--space-1); }
.page-title p { font-size: var(--text-sm); color: var(--text-muted); margin: 0; }

.admin-card-section { padding: var(--card-pad); margin-bottom: var(--space-5); }
.admin-card-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-4); flex-wrap: wrap; margin-bottom: var(--space-4);
  padding-bottom: var(--space-3); border-bottom: 1px solid var(--border-subtle);
}
.admin-card-header h3 { font-size: var(--text-md); font-weight: var(--weight-bold); }

/* ---------------------------------------------------------------------------
   16. RESPONSIVE
   The original had four media queries. Data-dense tables on a 360px phone
   need more than that — field officers are the primary mobile users.
   --------------------------------------------------------------------------- */

@media (max-width: 1280px) {
  :root { --kpi-value-size: var(--text-3xl); --sidebar-w: 240px; }
}

@media (max-width: 1024px) {
  :root { --card-pad: var(--space-4); --kpi-pad: var(--space-4); }
  .page-header-block { padding: var(--space-4); }
}

@media (max-width: 768px) {
  :root {
    --kpi-value-size: var(--text-2xl);
    --table-cell-pad-x: var(--space-3);
    --header-h: 60px;
  }

  .page-header-block { flex-direction: column; align-items: stretch; }
  .page-title h2 { font-size: var(--text-xl); }

  /* Full-width action buttons are far easier to hit one-handed. */
  .page-header-block .btn,
  .admin-card-header .btn { width: 100%; justify-content: center; }
  .admin-card-header > div:last-child { display: flex; flex-direction: column; gap: var(--space-2); width: 100%; }

  .modal-container { max-width: 100%; max-height: 94dvh; border-radius: var(--radius-lg); }
  .modal-footer { flex-direction: column-reverse; }
  .modal-footer .btn { width: 100%; }

  .followup-item-card { flex-direction: column; align-items: stretch; }
  .followup-item-card > div:last-child { justify-content: space-between; }

  #toast-container { left: var(--space-3); right: var(--space-3); max-width: none; top: auto; bottom: calc(var(--bottom-nav-h) + var(--space-3)); }

  /* Leave room for the fixed bottom nav. */
  .tab-pane { padding-bottom: calc(var(--bottom-nav-h) + var(--space-6)); }
}

@media (max-width: 480px) {
  :root { --card-pad: var(--space-3); --table-cell-pad-x: var(--space-2); --text-base: 0.875rem; }
  .kpi-card { padding: var(--space-4); }
  .followup-thumb { width: 48px; height: 48px; }
  .modal-header, .modal-body, .modal-footer { padding-left: var(--space-4); padding-right: var(--space-4); }
}

/* Short landscape viewports (a phone held sideways in the field). */
@media (max-height: 520px) and (orientation: landscape) {
  .modal-container { max-height: 96dvh; }
  .login-screen-wrapper { padding: var(--space-3); align-items: flex-start; }
}

/* ---------------------------------------------------------------------------
   17. PRINT — the DC Review Pack is printed for meetings.
   --------------------------------------------------------------------------- */

@media print {
  .sidebar, .app-header, .bottom-nav, #toast-container,
  .modal-overlay, .btn, .row-actions { display: none !important; }
  body { background: #fff !important; color: #000 !important; }
  .kpi-card, .admin-card-section, .chart-card {
    background: #fff !important; border: 1px solid #ccc !important;
    box-shadow: none !important; break-inside: avoid;
  }
  .data-table { font-size: 10pt; }
  .data-table thead th { background: #eee !important; color: #000 !important; }
  .badge { border: 1px solid #666 !important; color: #000 !important; background: #fff !important; }
}

/* ---------------------------------------------------------------------------
   18. UTILITIES
   --------------------------------------------------------------------------- */

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.scroll-area { scrollbar-width: thin; scrollbar-color: var(--slate-600) transparent; }
.scroll-area::-webkit-scrollbar { width: 10px; height: 10px; }
.scroll-area::-webkit-scrollbar-track { background: transparent; }
.scroll-area::-webkit-scrollbar-thumb { background: var(--slate-700); border-radius: var(--radius-full); border: 2px solid transparent; background-clip: content-box; }
.scroll-area::-webkit-scrollbar-thumb:hover { background: var(--slate-600); background-clip: content-box; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--surface-base); }
::-webkit-scrollbar-thumb { background: var(--slate-700); border-radius: var(--radius-full); border: 2px solid var(--surface-base); }
::-webkit-scrollbar-thumb:hover { background: var(--slate-600); }

/* Skeleton shimmer for loading states. */
.skeleton {
  background: linear-gradient(90deg, var(--surface-raised) 25%, var(--surface-overlay) 50%, var(--surface-raised) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
  border-radius: var(--radius-sm);
  color: transparent !important;
}
@keyframes shimmer { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.empty-state { text-align: center; padding: var(--space-10) var(--space-4); color: var(--text-muted); }
.empty-state i { font-size: var(--text-3xl); color: var(--slate-600); margin-bottom: var(--space-3); display: block; }

/* ---------------------------------------------------------------------------
   19. CONTRAST CORRECTIONS
   Measured against the composited background, not the declared one. Each of
   these failed WCAG AA at its rendered size; the replacement value is the
   nearest shade that clears 4.5:1.
   --------------------------------------------------------------------------- */

/* Sidebar section labels: slate-500 on the sidebar gradient = 3.79:1 */
.nav-section-title {
  color: var(--slate-300);
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-caps);
  text-transform: uppercase;
  opacity: 0.85;
}

/* "DC OFFICE" brand chip: green-200 on green-800 = 3.77:1 */
.brand-badge {
  background: var(--green-900);
  color: var(--green-100);
  border: 1px solid var(--green-700);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-wide);
}

/* Notification count: white on red-500 = 3.76:1 at 10px.
   Darkening the chip is better than enlarging it in a dense header. */
.notification-count {
  background: #991b1b;
  color: #ffffff;
  border: 1px solid var(--red-500);
  font-weight: var(--weight-bold);
  font-size: var(--text-2xs);
  min-width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
}
#offline-pending-badge {
  background: #92400e;
  color: #ffffff;
  border: 1px solid var(--amber-500);
}

/* ---------------------------------------------------------------------------
   20. TOUCH TARGETS
   Header controls sat at 34-36px. The visual size is kept (a 44px-tall pill
   would unbalance the header) and the hit area is extended with a pseudo
   element, which is what WCAG 2.5.5 actually measures.
   --------------------------------------------------------------------------- */

.btn-new-plantation,
.btn-logout {
  position: relative;
  min-height: var(--control-h-sm);
}
.btn-new-plantation::after,
.btn-logout::after {
  content: '';
  position: absolute;
  left: 0; right: 0;
  top: 50%;
  transform: translateY(-50%);
  height: var(--tap-min);
}

/* On touch devices there is room to simply make them bigger. */
@media (pointer: coarse) {
  .btn-new-plantation, .btn-logout, .btn, .btn-icon {
    min-height: var(--tap-min);
  }
  .btn-sm { min-height: 38px; }
}

/* ---------------------------------------------------------------------------
   21. DEPARTMENT IDENTITY
   Department colours (#059669, #DC2626, #7C3AED …) are chosen to separate pins
   on a map. Used as small text on a dark surface they land between 2.6:1 and
   4.4:1 — below AA. So the colour moves to a dot and the label stays in a
   readable neutral: identity is preserved, legibility is not traded away.
   --------------------------------------------------------------------------- */

.dept-tag {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: var(--weight-semibold);
  color: var(--text-default);
  min-width: 0;
}

.dept-tag::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
  background: var(--dept-color, var(--green-500));
  box-shadow: 0 0 0 2px rgb(255 255 255 / 0.10);
  flex-shrink: 0;
}

.dept-tag span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Map pin glyph: decorative, sits on a solid coloured pin with a light ring. */
.pin-inner-icon { text-shadow: 0 1px 2px rgb(0 0 0 / 0.5); }

/* Header avatar initials: the original green-on-green sat at 3.77:1. */
.user-avatar {
  background: linear-gradient(135deg, var(--green-700), var(--green-900));
  color: #ffffff;
  border: 1px solid var(--green-600);
  font-weight: var(--weight-bold);
  letter-spacing: var(--tracking-wide);
}

/* ---------------------------------------------------------------------------
   22. HORIZONTAL OVERFLOW CONTAINMENT
   At 390px the document scrolled to 696px: the header action cluster did not
   wrap, and wide tables pushed the page instead of scrolling inside their own
   container. A data tool that shifts sideways under the thumb is unusable in
   the field, so overflow is contained at every level here.
   --------------------------------------------------------------------------- */

html, body { max-width: 100%; overflow-x: hidden; }

/* A flex/grid child defaults to min-width:auto, which refuses to shrink below
   its content. This single declaration is what lets wide tables scroll inside
   their container rather than stretching every ancestor. */
.dashboard-container,
.tab-pane,
.admin-card-section,
.table-container,
.page-header-block,
.followup-item-card,
.followup-item-left,
.followup-info,
main, section {
  min-width: 0;
}

.table-container {
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-x: contain;
}

/* Keep the table readable while scrolling rather than crushing the columns. */
.table-container .data-table { min-width: 640px; }

@media (max-width: 768px) {
  .header-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: var(--space-2);
    min-width: 0;
  }

  /* The label is redundant next to the icon once space is tight. */
  .btn-new-plantation span,
  .btn-logout span { display: none; }
  .btn-new-plantation, .btn-logout { padding: 0 var(--space-3); }

  /* Collapse the identity block to the avatar; the full name is in the
     profile menu and does not need to consume a third of a phone header. */
  .user-details-brief { display: none; }
  .user-profile-badge { gap: var(--space-2); padding: var(--space-1); }

  .app-header { padding-left: var(--space-3); padding-right: var(--space-3); }
  .brand-sub, .brand-badge { display: none; }
  .brand-info h1 { font-size: var(--text-base); }

  /* Scroll hint: a soft fade at the right edge of a scrollable table. */
  .table-container { position: relative; }
  .table-container::after {
    content: '';
    position: sticky;
    right: 0; top: 0;
    float: right;
    width: 28px; height: 100%;
    background: linear-gradient(90deg, transparent, var(--surface-raised));
    pointer-events: none;
  }
}

@media (max-width: 480px) {
  .table-container .data-table { min-width: 560px; }
  .kpi-grid, .dashboard-kpi-row { grid-template-columns: 1fr 1fr !important; gap: var(--space-3) !important; }
  .chart-grid, .dashboard-charts-row { grid-template-columns: 1fr !important; }
}

@media (max-width: 768px) {
  /* Keep the header to one row: the brand title wrapped to three lines and
     pushed the action cluster onto a second row. */
  .app-header {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: nowrap;
  }
  .brand-section { min-width: 0; flex: 1; }
  .brand-info h1 {
    font-size: var(--text-sm);
    line-height: 1.25;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .header-actions { flex-wrap: nowrap; flex-shrink: 0; }
  .brand-logo { width: 30px; height: 30px; }
}

@media (max-width: 480px) {
  /* On the narrowest phones the title becomes a single line. */
  .brand-info h1 { -webkit-line-clamp: 1; font-size: var(--text-xs); }
  .header-actions { gap: var(--space-1); }
  .btn-icon { width: 36px; height: 36px; }
}

/* ===========================================================================
   23. FIRST-RUN SETUP WIZARD
   Shown once, before the portal has any departments, tehsils or staff.
   =========================================================================== */

.setup-screen-wrapper {
  display: none;
  min-height: 100dvh;
  align-items: flex-start;
  justify-content: center;
  padding: var(--space-8) var(--space-4);
  overflow-y: auto;
}

.setup-card {
  width: 100%;
  max-width: 820px;
  background: var(--glass-bg);
  -webkit-backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-saturate));
  backdrop-filter: blur(var(--glass-blur)) saturate(var(--glass-saturate));
  border: 1px solid var(--glass-edge);
  border-radius: var(--radius-2xl);
  box-shadow: var(--glass-shadow-float);
  overflow: hidden;
}

.setup-header {
  padding: var(--space-6) var(--space-8) var(--space-4);
  border-bottom: 1px solid var(--border-subtle);
  background: rgb(2 6 23 / 0.3);
}
.setup-header .brand {
  display: flex; align-items: center; gap: var(--space-3);
  margin-bottom: var(--space-4);
}
.setup-header .brand img { width: 38px; height: 38px; }
.setup-header .brand span {
  font-family: var(--font-heading);
  font-size: var(--text-md);
  font-weight: var(--weight-bold);
  color: var(--text-strong);
}

/* --- progress rail ------------------------------------------------------ */

.setup-progress {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  flex-wrap: wrap;
}

.setup-step {
  display: flex; align-items: center; gap: var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-subtle);
  white-space: nowrap;
}
.setup-step-dot {
  width: 24px; height: 24px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-full);
  border: 1px solid var(--border-default);
  font-size: var(--text-2xs);
  font-weight: var(--weight-bold);
  background: var(--surface-inset);
  flex-shrink: 0;
}
.setup-step.current { color: var(--text-strong); font-weight: var(--weight-semibold); }
.setup-step.current .setup-step-dot {
  background: var(--green-600); border-color: var(--green-400); color: #fff;
  box-shadow: 0 0 0 3px rgb(16 185 129 / 0.2);
}
.setup-step.done { color: var(--text-brand); }
.setup-step.done .setup-step-dot {
  background: rgb(16 185 129 / 0.18); border-color: var(--green-500); color: var(--green-300);
}
.setup-step-line { flex: 1; height: 1px; min-width: 10px; background: var(--border-subtle); }

/* --- body --------------------------------------------------------------- */

.setup-body { padding: var(--space-6) var(--space-8); }

.setup-title {
  font-size: var(--text-2xl);
  font-weight: var(--weight-black);
  margin-bottom: var(--space-2);
}
.setup-lead {
  font-size: var(--text-base);
  color: var(--text-muted);
  line-height: var(--leading-normal);
  margin: 0 0 var(--space-5);
  max-width: 62ch;
}

.setup-notice {
  padding: var(--space-3) var(--space-4);
  margin-bottom: var(--space-4);
  border-radius: var(--radius-md);
  background: var(--status-info-bg);
  border: 1px solid var(--status-info-border);
  color: var(--status-info-fg);
  font-size: var(--text-sm);
  line-height: 1.5;
}
.setup-notice.warn {
  background: var(--status-warn-bg);
  border-color: var(--status-warn-border);
  color: var(--status-warn-fg);
}

.req { color: var(--red-400); }

/* --- repeatable rows ---------------------------------------------------- */

.setup-row {
  padding: var(--space-4);
  margin-bottom: var(--space-3);
  background: var(--surface-inset);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
}
.setup-row-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: var(--space-3);
  font-size: var(--text-sm);
  color: var(--text-muted);
}
.btn-remove {
  width: 28px; height: 28px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-md);
  background: rgb(239 68 68 / 0.12);
  border: 1px solid rgb(239 68 68 / 0.3);
  color: var(--red-on-dark);
  cursor: pointer;
  transition: background-color var(--duration-fast);
}
.btn-remove:hover { background: rgb(239 68 68 / 0.24); }

.setup-details {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px dashed var(--border-subtle);
}
.setup-details summary {
  cursor: pointer;
  font-size: var(--text-xs);
  font-weight: var(--weight-semibold);
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--text-subtle);
  margin-bottom: var(--space-3);
}
.setup-details summary:hover { color: var(--text-default); }

/* --- review ------------------------------------------------------------- */

.setup-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--space-3);
}
.setup-summary > div {
  padding: var(--space-4);
  background: var(--surface-inset);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  text-align: center;
}
.setup-summary span {
  display: block;
  font-size: var(--text-2xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--text-subtle);
  margin-bottom: var(--space-1);
}
.setup-summary strong {
  font-family: var(--font-heading);
  font-size: var(--text-xl);
  color: var(--text-strong);
}

.setup-creds { display: flex; flex-direction: column; gap: var(--space-2); margin-top: var(--space-3); }
.setup-cred {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-4); flex-wrap: wrap;
  padding: var(--space-3) var(--space-4);
  background: var(--surface-inset);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
}
.setup-cred strong { display: block; color: var(--text-strong); font-size: var(--text-sm); }
.setup-cred span { font-size: var(--text-xs); color: var(--text-subtle); }
.setup-cred-login { display: flex; gap: var(--space-2); }
.setup-cred-login code {
  padding: 3px var(--space-2);
  border-radius: var(--radius-sm);
  background: rgb(2 6 23 / 0.5);
  font-size: var(--text-xs);
  color: var(--text-brand);
}
.setup-cred-login code.pw { color: var(--status-warn-fg); }

/* --- footer ------------------------------------------------------------- */

.setup-footer {
  display: flex; align-items: center; justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-8);
  border-top: 1px solid var(--border-subtle);
  background: rgb(2 6 23 / 0.3);
}
.setup-count { font-size: var(--text-xs); color: var(--text-subtle); }

@media (max-width: 640px) {
  .setup-screen-wrapper { padding: var(--space-4) var(--space-2); }
  .setup-header, .setup-body, .setup-footer {
    padding-left: var(--space-4); padding-right: var(--space-4);
  }
  .setup-step span { display: none; }
  .setup-footer { flex-direction: column-reverse; align-items: stretch; }
  .setup-footer > div { display: flex; }
  .setup-footer .btn { flex: 1; justify-content: center; }
}
