:root {
  --surface: rgba(15, 23, 42, 0.92);
  --surface-solid: #0f172a;
  --surface-card: #1e293b;
  --border: rgba(148, 163, 184, 0.24);
  --text: #f8fafc;
  --text-muted: #94a3b8;
  --green: #22c55e;
  --green-hover: #16a34a;
  --red: #ef4444;
  --red-hover: #dc2626;
  --accent: #10b981;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  background: #e2e8f0;
  color: var(--text);
  overscroll-behavior: none;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

.hidden {
  display: none !important;
}

/* Fullscreen Map Canvas */
.map-fullscreen {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  z-index: 1;
  background: #e2e8f0;
}

.map-fullscreen.crosshair-cursor {
  cursor: crosshair !important;
}

/* Crisp monochrome filter for Black & White OpenStreetMap */
.leaflet-layer.gp-tiles-bw,
.gp-tiles-bw {
  filter: grayscale(100%) contrast(1.06);
}

/* Keep Leaflet attribution discreet and clear of mobile corners */
.leaflet-control-attribution {
  font-size: 10px !important;
  background: rgba(255, 255, 255, 0.72) !important;
  padding: 1px 5px !important;
}

/* Floating Top Controls */
.floating-top {
  position: fixed;
  top: calc(12px + var(--safe-top));
  left: calc(12px + var(--safe-left));
  right: calc(12px + var(--safe-right));
  z-index: 900;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  pointer-events: none;
}

.floating-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
}

.basemap-select {
  appearance: none;
  -webkit-appearance: none;
  background-color: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 9px 32px 9px 14px;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.28);
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23cbd5e1' stroke-width='2.5'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}

.basemap-select:focus {
  outline: none;
  border-color: var(--accent);
}

.basemap-select option,
.basemap-select optgroup {
  background: var(--surface-solid);
  color: var(--text);
}

.menu-wrap {
  position: relative;
}

.icon-pill {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.28);
  touch-action: manipulation;
}

.dropdown-menu {
  position: absolute;
  top: 44px;
  left: 0;
  min-width: 176px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 5px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.dropdown-item {
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.dropdown-item:hover,
.dropdown-item:active {
  background: var(--surface-card);
}

/* Placement Mode Banner */
.placement-pill {
  pointer-events: auto;
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 6px 8px 6px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

.pill-btn {
  border: 1px solid var(--border);
  background: var(--surface-card);
  color: var(--text);
  border-radius: 999px;
  padding: 5px 11px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.pill-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #022c22;
}

/* Floating Bottom-Right FABs */
.floating-fabs {
  position: fixed;
  right: calc(16px + var(--safe-right));
  bottom: calc(28px + var(--safe-bottom));
  z-index: 850;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  transition: transform 0.18s ease;
}

body.dialog-open .floating-fabs {
  transform: translateY(-218px);
}

.fab {
  border-radius: 50%;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.36);
  touch-action: manipulation;
  transition: transform 0.12s ease, background 0.12s ease;
}

.fab:active {
  transform: scale(0.94);
}

.fab-secondary {
  width: 46px;
  height: 46px;
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
}

.fab-primary {
  width: 56px;
  height: 56px;
  background: var(--accent);
  border-color: rgba(255, 255, 255, 0.25);
  color: #022c22;
  font-size: 30px;
  font-weight: 700;
  line-height: 1;
}

.fab-primary.active {
  background: #f59e0b;
  transform: rotate(45deg);
}

/* Simplified Overlay Edit Dialog */
.light-dialog {
  position: fixed;
  left: 50%;
  bottom: calc(20px + var(--safe-bottom));
  transform: translateX(-50%);
  width: calc(100vw - 24px);
  max-width: 340px;
  z-index: 950;
  background: var(--surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 14px 16px;
  box-shadow: 0 14px 38px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.light-dialog-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.dialog-title-group {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.dialog-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

.dialog-sub {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}

.dialog-live-readout {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(2, 6, 23, 0.65);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
}

.phase-lamp {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
}

.phase-lamp.green {
  background: var(--green);
  box-shadow: 0 0 8px var(--green);
}

.phase-lamp.red {
  background: var(--red);
  box-shadow: 0 0 8px var(--red);
}

.phase-rem {
  font-size: 15px;
  font-weight: 800;
  color: #ffffff;
}

.phase-rem small,
.phase-next small {
  font-size: 11px;
  color: var(--text-muted);
}

.phase-sep {
  color: #475569;
}

.phase-next {
  font-size: 13px;
  font-weight: 600;
  color: #cbd5e1;
}

.dialog-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  line-height: 1;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.dialog-close:hover {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
}

/* 4-Option Light Type Selector inside Overlay Dialog */
.type-selector {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  background: rgba(2, 6, 23, 0.55);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
}

.type-opt {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--text-muted);
  padding: 6px 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.type-opt:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.05);
}

.type-opt.active {
  background: var(--surface-card);
  border-color: var(--accent);
  color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.dual-svg {
  display: inline-flex;
  align-items: center;
  gap: 1px;
}

/* Primary Transforming "Set Green NOW" <-> "Set Red NOW" Button */
.phase-action-btn {
  width: 100%;
  min-height: 52px;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.01em;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s ease, background 0.15s ease;
}

.phase-action-btn:active {
  transform: scale(0.98);
}

.phase-action-btn.is-green {
  background: var(--green);
  color: #052e16;
  box-shadow: 0 4px 16px rgba(34, 197, 94, 0.38);
}

.phase-action-btn.is-green:hover {
  background: var(--green-hover);
  color: #ffffff;
}

.phase-action-btn.is-red {
  background: var(--red);
  color: #ffffff;
  box-shadow: 0 4px 16px rgba(239, 68, 68, 0.42);
}

.phase-action-btn.is-red:hover {
  background: var(--red-hover);
}

.light-dialog-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.dialog-hint {
  font-size: 12px;
  color: var(--text-muted);
}

.btn-delete-text {
  background: transparent;
  border: none;
  color: #f87171;
  font-size: 13px;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-delete-text:hover {
  background: rgba(239, 68, 68, 0.16);
}

/* Custom SVG Traffic Light Markers (48px touch-friendly hit area) */
.gp-marker-wrap {
  background: transparent;
  border: none;
}

.gp-marker {
  position: relative;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.gp-dir-svg {
  position: absolute;
  inset: 0;
  width: 48px;
  height: 48px;
  pointer-events: none;
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.75));
}

.gp-core {
  position: relative;
  z-index: 2;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #0f172a;
  border: 2.5px solid #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10.5px;
  font-weight: 800;
  color: #052e16;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.65);
}

.gp-core.green {
  background: #22c55e;
  border-color: #ffffff;
  color: #052e16;
  box-shadow: 0 0 12px rgba(34, 197, 94, 0.85);
}

.gp-core.red {
  background: #ef4444;
  border-color: #ffffff;
  color: #ffffff;
  box-shadow: 0 0 12px rgba(239, 68, 68, 0.85);
}

.gp-marker.selected .gp-core {
  outline: 3px solid #fbbf24;
  outline-offset: 2px;
}

.gp-id-tag {
  position: absolute;
  bottom: -9px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.92);
  color: #f8fafc;
  border: 1px solid #475569;
  border-radius: 4px;
  padding: 0 4px;
  font-size: 9.5px;
  font-weight: 700;
  line-height: 13px;
  white-space: nowrap;
  z-index: 3;
}

/* Corner Symbol Badge on Map Marker (hidden for "general") */
.gp-type-badge {
  position: absolute;
  top: 2px;
  right: 0;
  z-index: 4;
  height: 17px;
  min-width: 17px;
  padding: 0 3px;
  border-radius: 999px;
  background: #0f172a;
  border: 1.5px solid #ffffff;
  color: #ffffff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 1px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.65);
  pointer-events: none;
}

.gp-type-badge.is-combined {
  right: -6px;
  padding: 0 4px;
}

/* 44x44px touch target around a crisp 26x26px handle circle */
.gp-dir-handle-hit {
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
}

.gp-dir-handle {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #10b981;
  border: 2px solid #ffffff;
  color: #052e16;
  font-size: 12px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}

/* Toast */
.toast {
  position: fixed;
  top: calc(60px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 1100;
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13.5px;
  font-weight: 600;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  white-space: nowrap;
}

/* Utility Modals (Token & CSV) */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.75);
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-card {
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 100%;
  max-width: 400px;
  padding: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
}

.modal-card-wide {
  max-width: 520px;
}

.modal-card h3 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.modal-desc {
  margin: 0 0 12px;
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.45;
}

.input {
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 10px 12px;
  font-size: 16px; /* Prevents iOS auto-zoom on focus */
  width: 100%;
}

.input:focus {
  outline: none;
  border-color: var(--accent);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-size: 14px;
  cursor: pointer;
}

.error-text {
  color: #f87171;
  font-size: 13px;
  margin: 8px 0 0;
}

.modal-actions {
  margin-top: 16px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.spacer {
  flex: 1;
}

.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--accent);
  color: #022c22;
}

.btn-ghost {
  background: var(--surface-card);
  border-color: var(--border);
  color: var(--text);
}

/* Mobile-specific refinements */
@media (max-width: 640px) {
  .basemap-select {
    font-size: 16px; /* Prevents iOS Safari zoom on select */
    padding: 8px 30px 8px 12px;
    max-width: calc(100vw - 76px);
  }

  .leaflet-control-zoom {
    display: none; /* Pinch-to-zoom on mobile keeps the map uncluttered */
  }
}
