:root {
  --bg-root: #090e1a;
  --bg-surface: #111827;
  --bg-elevated: #172133;
  --bg-card: #131c2e;
  --bg-card-hover: #1a263d;
  --border-subtle: #25334d;
  --border-strong: #374b70;
  --text-primary: #eef4ff;
  --text-secondary: #9fb0c9;
  --text-muted: #70829e;
  --accent: #38bdf8;
  --accent-strong: #0284c7;
  --tier-surge: #ef4444;
  --tier-surge-bg: rgba(239, 68, 68, 0.16);
  --tier-high: #f59e0b;
  --tier-high-bg: rgba(245, 158, 11, 0.16);
  --tier-moderate: #22c55e;
  --tier-moderate-bg: rgba(34, 197, 94, 0.16);
  --tier-calm: #64748b;
  --tier-calm-bg: rgba(100, 116, 139, 0.18);
  --danger: #f87171;
  --warning: #fbbf24;
  --positive: #34d399;
}

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

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background-color: var(--bg-root);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 14px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

button,
select {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

#app {
  position: absolute;
  inset: 0;
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-root);
}

/* Top Bar (48px ultra-slim, GPU promoted above Leaflet) */
#topBar {
  height: 48px;
  flex: 0 0 48px;
  position: relative;
  z-index: 2000;
  transform: translate3d(0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 0 6px;
  background: #0d1424;
  border-bottom: 1px solid var(--border-subtle);
}

.brand-cluster {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 1 auto;
  min-width: 0;
}

.brand-mark {
  width: 26px;
  height: 26px;
  flex: 0 0 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  color: #ffffff;
  font-weight: 800;
  font-size: 11px;
  letter-spacing: 0.03em;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-title {
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: 0.01em;
}

.brand-clock {
  font-size: 10px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.top-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  flex: 1 1 auto;
  min-width: 0;
}

.location-pill {
  min-height: 36px;
  flex: 0 1 112px;
  min-width: 64px;
  padding: 0 8px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
}

.location-pill span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-pill {
  display: none;
  align-items: center;
  height: 24px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  border: 1px solid transparent;
}

.status-pill.is-live {
  background: rgba(34, 197, 94, 0.15);
  color: var(--positive);
  border-color: rgba(52, 211, 153, 0.35);
}

.status-pill.is-partial {
  background: rgba(245, 158, 11, 0.16);
  color: var(--warning);
  border-color: rgba(251, 191, 36, 0.35);
}

.status-pill.is-syncing,
.status-pill.is-offline {
  background: rgba(148, 163, 184, 0.16);
  color: var(--text-secondary);
  border-color: var(--border-subtle);
}

.icon-btn {
  min-width: 36px;
  min-height: 36px;
  padding: 0 6px;
  flex: 0 0 auto;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
}

.icon-btn:hover,
.location-pill:hover {
  border-color: var(--accent);
}

/* Controls Bar (GPU promoted above Leaflet) */
#controlsBar {
  position: relative;
  z-index: 2000;
  transform: translate3d(0, 0, 0);
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
}

.category-strip {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
  flex: 1 1 auto;
  min-width: 0;
}

.category-strip::-webkit-scrollbar {
  display: none;
}

.chip-btn {
  min-height: 36px;
  padding: 0 11px;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
}

.chip-btn.is-active {
  background: rgba(56, 189, 248, 0.18);
  border-color: var(--accent);
  color: #e0f2fe;
}

.controls-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.sort-select {
  min-height: 36px;
  padding: 0 8px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 12px;
  font-weight: 600;
}

.view-segmented {
  display: inline-flex;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  overflow: hidden;
  background: var(--bg-root);
}

.view-btn {
  min-height: 36px;
  min-width: 48px;
  padding: 0 10px;
  border: 0;
  border-right: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.view-btn:last-child {
  border-right: 0;
}

.view-btn.is-active {
  background: var(--accent-strong);
  color: #ffffff;
}

/* Alert Banner (MRT Disruption / PUB Flood Alert) */
#alertBanner {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: rgba(239, 68, 68, 0.16);
  border-bottom: 1px solid rgba(248, 113, 113, 0.4);
  color: #fecaca;
  font-size: 12px;
  font-weight: 600;
  position: relative;
  z-index: 1500;
}

#alertBanner.is-visible {
  display: flex;
}

/* Workspace (Mobile Portrait First) */
#workspace {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Strict Leaflet v2.2 Isolation */
#mapPane {
  position: relative;
  z-index: 1;
  isolation: isolate;
  contain: layout paint;
  overflow: hidden;
  background: #070b14;
}

#map {
  width: 100%;
  height: 100%;
}

#map.is-pin-drop {
  cursor: crosshair;
}

.map-overlay-bar {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 900;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
  pointer-events: none;
}

.map-overlay-btn {
  pointer-events: auto;
  min-height: 36px;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: rgba(13, 20, 36, 0.9);
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.map-overlay-btn.is-active {
  border-color: var(--accent);
  color: #e0f2fe;
  background: rgba(2, 132, 199, 0.35);
}

.pin-drop-banner {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  z-index: 950;
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid var(--accent);
  font-size: 12px;
  font-weight: 600;
}

.pin-drop-banner.is-visible {
  display: flex;
}

/* Tap-to-Cycle Handle */
#sheetHandle {
  flex: 0 0 38px;
  min-height: 38px;
  position: relative;
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  border: 0;
  border-top: 1px solid var(--border-subtle);
  border-bottom: 1px solid var(--border-subtle);
  background: #10192b;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
  cursor: pointer;
}

.sheet-grip {
  width: 28px;
  height: 4px;
  border-radius: 999px;
  background: var(--border-strong);
}

#listPane {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  position: relative;
  z-index: 2;
  background: var(--bg-root);
}

/* View Mode Rules (Mobile Portrait) */
.view-split #mapPane {
  flex: 0 0 44%;
  height: 44%;
}

.view-split #listPane {
  flex: 1 1 56%;
}

.view-map #mapPane {
  flex: 1 1 100%;
  height: 100%;
}

.view-map #listPane {
  display: none;
}

.view-list #mapPane {
  display: none;
}

.view-list #listPane {
  flex: 1 1 100%;
}

.list-summary-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-subtle);
  background: #0c1220;
}

.hotspot-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 10px 10px 18px;
}

.empty-state {
  padding: 28px 16px;
  text-align: center;
  color: var(--text-secondary);
}

.hotspot-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border-subtle);
  border-left-width: 4px;
  background: var(--bg-card);
  cursor: pointer;
}

.hotspot-card:hover {
  background: var(--bg-card-hover);
}

.hotspot-card.is-selected {
  border-color: var(--accent);
}

.tier-border-surge {
  border-left-color: var(--tier-surge);
}

.tier-border-high {
  border-left-color: var(--tier-high);
}

.tier-border-moderate {
  border-left-color: var(--tier-moderate);
}

.tier-border-calm {
  border-left-color: var(--tier-calm);
}

.card-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.card-title-wrap {
  min-width: 0;
}

.card-title {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}

.card-sub {
  margin-top: 2px;
  font-size: 12px;
  color: var(--text-secondary);
}

.demand-badge {
  flex: 0 0 auto;
  min-width: 62px;
  padding: 4px 8px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  border: 1px solid transparent;
}

.demand-badge.tier-surge {
  background: var(--tier-surge-bg);
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.45);
}

.demand-badge.tier-high {
  background: var(--tier-high-bg);
  color: #fcd34d;
  border-color: rgba(245, 158, 11, 0.45);
}

.demand-badge.tier-moderate {
  background: var(--tier-moderate-bg);
  color: #86efac;
  border-color: rgba(34, 197, 94, 0.45);
}

.demand-badge.tier-calm {
  background: var(--tier-calm-bg);
  color: #cbd5e1;
  border-color: rgba(100, 116, 139, 0.45);
}

.demand-num {
  font-size: 18px;
  font-weight: 800;
  line-height: 1.1;
}

.demand-tier {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.04em;
}

.card-metrics {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.fare-lift {
  font-size: 12px;
  font-weight: 700;
  color: #7dd3fc;
  background: rgba(56, 189, 248, 0.12);
  padding: 3px 8px;
  border-radius: 6px;
}

.erp-tag {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  background: rgba(148, 163, 184, 0.12);
  padding: 3px 8px;
  border-radius: 6px;
}

.erp-tag.is-active {
  color: #fde68a;
  background: rgba(245, 158, 11, 0.16);
}

.signal-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.signal-pill {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-secondary);
}

.signal-pill.is-positive {
  border-color: rgba(52, 211, 153, 0.4);
  background: rgba(16, 185, 129, 0.14);
  color: #6ee7b7;
}

.signal-pill.is-warning {
  border-color: rgba(251, 191, 36, 0.4);
  background: rgba(245, 158, 11, 0.14);
  color: #fde68a;
}

.signal-pill.is-danger {
  border-color: rgba(248, 113, 113, 0.5);
  background: rgba(239, 68, 68, 0.18);
  color: #fecaca;
}

.carpark-row {
  font-size: 12px;
  color: var(--text-secondary);
  padding-top: 6px;
  border-top: 1px dashed var(--border-subtle);
}

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

.card-btn {
  flex: 1 1 0;
  min-height: 38px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.card-btn.is-primary {
  background: rgba(2, 132, 199, 0.25);
  border-color: var(--accent);
  color: #e0f2fe;
}

/* Custom Leaflet CSP-Safe Markers (Zero inline styles) */
.hotspot-marker-wrap,
.driver-marker-wrap,
.incident-marker-wrap,
.flood-marker-wrap {
  background: transparent;
  border: 0;
}

.hotspot-pin {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 2px solid #090e1a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 800;
  color: #ffffff;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.55);
}

.hotspot-pin.tier-surge {
  background: #dc2626;
}

.hotspot-pin.tier-high {
  background: #d97706;
}

.hotspot-pin.tier-moderate {
  background: #16a34a;
}

.hotspot-pin.tier-calm {
  background: #475569;
}

.hotspot-pin.is-selected {
  border: 3px solid #38bdf8;
  transform: scale(1.14);
}

.driver-pin {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #38bdf8;
  border: 3px solid #ffffff;
  box-shadow: 0 0 0 6px rgba(56, 189, 248, 0.28);
}

.incident-pin {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: #f59e0b;
  color: #090e1a;
  border: 2px solid #090e1a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 13px;
}

.flood-pin {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #ef4444;
  color: #ffffff;
  border: 2px solid #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 14px;
}

/* Leaflet Popup Styling */
.copilot-popup .leaflet-popup-content-wrapper,
.copilot-popup .leaflet-popup-tip {
  background: #111827;
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
}

.popup-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 170px;
}

.popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.popup-title {
  font-size: 13px;
  font-weight: 700;
}

.popup-score {
  font-size: 11px;
  font-weight: 800;
  padding: 2px 6px;
  border-radius: 6px;
}

.popup-score.tier-surge {
  background: var(--tier-surge-bg);
  color: #fca5a5;
}

.popup-score.tier-high {
  background: var(--tier-high-bg);
  color: #fcd34d;
}

.popup-score.tier-moderate {
  background: var(--tier-moderate-bg);
  color: #86efac;
}

.popup-score.tier-calm {
  background: var(--tier-calm-bg);
  color: #cbd5e1;
}

.popup-meta {
  font-size: 12px;
  color: var(--text-secondary);
}

.popup-detail-btn {
  min-height: 36px;
  padding: 0 10px;
  border-radius: 7px;
  border: 1px solid var(--accent);
  background: rgba(2, 132, 199, 0.28);
  color: #e0f2fe;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

/* Modals (strict z-index: 9999, hidden + pointer-events: none when closed) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: none;
  pointer-events: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(5, 9, 18, 0.78);
}

.modal-backdrop.is-open {
  display: flex;
  pointer-events: auto;
}

.modal-panel {
  width: 100%;
  max-width: 500px;
  max-height: 88%;
  overflow-y: auto;
  border-radius: 14px;
  border: 1px solid var(--border-strong);
  background: #101828;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 800;
}

.modal-close-btn {
  min-width: 36px;
  min-height: 36px;
  border-radius: 8px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-weight: 700;
  cursor: pointer;
}

.modal-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px;
  border-radius: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border-subtle);
}

.modal-section h4 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--accent);
}

.modal-section p,
.modal-section ul {
  margin: 0;
  font-size: 13px;
  color: var(--text-secondary);
}

.modal-section ul {
  padding-left: 18px;
}

.is-active-window {
  color: var(--positive);
  font-weight: 700;
}

.modal-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.action-btn {
  flex: 1 1 140px;
  min-height: 42px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.action-btn.is-accent {
  background: var(--accent-strong);
  border-color: var(--accent);
  color: #ffffff;
}

.preset-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.preset-zone-btn {
  min-height: 40px;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-elevated);
  color: var(--text-primary);
  font-size: 12px;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.preset-zone-btn:hover {
  border-color: var(--accent);
}

.feedback-text {
  font-size: 12px;
  color: var(--warning);
  min-height: 16px;
}

.detail-score-box {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.detail-score-pill,
.detail-lift-pill {
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 800;
}

.detail-score-pill.tier-surge {
  background: var(--tier-surge-bg);
  color: #fca5a5;
}

.detail-score-pill.tier-high {
  background: var(--tier-high-bg);
  color: #fcd34d;
}

.detail-score-pill.tier-moderate {
  background: var(--tier-moderate-bg);
  color: #86efac;
}

.detail-score-pill.tier-calm {
  background: var(--tier-calm-bg);
  color: #cbd5e1;
}

.detail-lift-pill {
  background: rgba(56, 189, 248, 0.16);
  color: #7dd3fc;
}

@media (min-width: 560px) {
  #topBar {
    padding: 0 12px;
    gap: 8px;
  }

  .top-actions {
    gap: 6px;
  }

  .location-pill {
    flex: 0 1 180px;
    max-width: 180px;
    font-size: 12px;
  }

  .icon-btn {
    padding: 0 10px;
    font-size: 12px;
  }

  .status-pill {
    display: inline-flex;
  }
}

/* Desktop Split View */
@media (min-width: 900px) {
  #workspace.view-split {
    flex-direction: row;
  }

  #workspace.view-split #listPane {
    flex: 0 0 430px;
    height: 100%;
    order: 1;
    border-right: 1px solid var(--border-subtle);
  }

  #workspace.view-split #mapPane {
    flex: 1 1 auto;
    height: 100%;
    order: 2;
  }

  #sheetHandle {
    display: none;
  }
}
