:root {
  --bg: #0f1115;
  --panel: #161a22;
  --panel-2: #1f2530;
  --text: #e6edf3;
  --muted: #8b96a7;
  --accent: #1f6feb;
  --accent-hi: #4187f5;
  --danger: #d9534f;
  --ok: #2ea043;
  --history: #6b7385;
  --shadow: 0 8px 24px rgba(0,0,0,.35);
  --radius: 14px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  /* Kill iOS long-press text selection + callout menu so long-pressing
     the map to drop a pin doesn't also highlight nearby chrome text. */
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  user-select: none;
}

/* Form fields still need normal selection behavior so users can edit. */
input, textarea, select, [contenteditable] {
  -webkit-user-select: text;
  user-select: text;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid #232a36;
  /* Must beat the FAB z-index (600) so the dropdown menu renders above it. */
  z-index: 700;
}

.brand {
  font-weight: 700;
  letter-spacing: .5px;
}

.topbar-chips {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 70vw;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  max-width: 180px;
}

.chip span:last-child {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--history);
  flex-shrink: 0;
}

.user-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  flex-shrink: 0;
}

#mapWrap {
  position: relative;
  flex: 1;
  min-height: 0;
}

#map {
  position: absolute;
  inset: 0;
  background: #1a1f2a;
}

.fab {
  position: absolute;
  z-index: 600;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border: none;
  border-radius: 999px;
  font-weight: 600;
  font-size: 15px;
  color: white;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform .08s ease, background .15s ease;
}
.fab:active { transform: scale(.97); }

.fab-primary {
  bottom: calc(24px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: 18px 32px;
  font-size: 17px;
  background: var(--accent);
  box-shadow: 0 10px 28px rgba(0,0,0,.4), 0 0 0 4px rgba(31,111,235,.12);
}
.fab-primary svg { width: 26px; height: 26px; }
.fab-primary:hover { background: var(--accent-hi); }
.fab-primary:active { transform: translateX(-50%) scale(.97); }

.fab-secondary {
  bottom: calc(32px + var(--safe-bottom));
  right: 16px;
  padding: 12px 14px;
  background: var(--panel-2);
  color: var(--text);
  position: relative;
}

.fab.fab-filter {
  /* Stacked below the locate button, top-right of the map. */
  top: 64px;
  right: 16px;
}

.filter-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--accent);
  color: white;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 700;
  padding: 0 5px;
  line-height: 14px;
  min-width: 14px;
  height: 14px;
  text-align: center;
}
.filter-badge.hidden { display: none; }

.fab-tiny {
  top: 16px;
  right: 16px;
  padding: 10px;
  background: var(--panel-2);
  color: var(--text);
  position: absolute;
}

.menu-btn {
  background: var(--panel-2);
  border-radius: 10px;
  padding: 6px 8px;
  color: var(--text);
  font-size: 0;
  line-height: 0;
  cursor: pointer;
}
.menu-btn:hover { background: #242a36; }

.menu-dropdown {
  position: absolute;
  top: 56px;
  right: 14px;
  min-width: 260px;
  background: var(--panel);
  border: 1px solid #232a36;
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 800;
  overflow: hidden;
  animation: menuDown .12s ease-out;
}
.menu-dropdown.hidden { display: none; }

@keyframes menuDown {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.menu-item {
  display: flex;
  align-items: center;
  gap: 14px;
  width: 100%;
  padding: 15px 18px;
  background: transparent;
  border: none;
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  text-align: left;
  cursor: pointer;
}
.menu-item + .menu-item {
  border-top: 1px solid #232a36;
}
.menu-item:hover { background: rgba(255,255,255,.04); }
.menu-item svg { color: var(--muted); flex-shrink: 0; }

.menu-mode {
  justify-content: space-between;
  cursor: default;
  padding: 12px 18px;
  gap: 10px;
}
.menu-mode:hover { background: transparent; }
.menu-mode > span:first-child {
  font-weight: 600;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.mode-toggle {
  display: inline-flex;
  background: var(--panel-2);
  border: 1px solid #2a3140;
  border-radius: 999px;
  padding: 2px;
}
.mode-opt {
  background: transparent;
  border: none;
  color: var(--muted);
  font-family: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.mode-opt.active {
  background: var(--accent);
  color: white;
}

/* Pick Up status bar (under top bar) */
.pickup-status {
  background: var(--panel);
  border-bottom: 1px solid #232a36;
  padding: 8px 14px 10px;
  z-index: 450;
}
.pickup-status.hidden { display: none; }

.pickup-status-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  font-size: 13px;
}
.pickup-status-route {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}
.pickup-status-count {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.pickup-progress-bar {
  height: 4px;
  background: var(--panel-2);
  border-radius: 999px;
  overflow: hidden;
}
.pickup-progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width .25s ease;
}

/* Pickup confirm modal extras */
.pickup-target {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel-2);
  border: 1px solid #2a3140;
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 12px;
}
.pickup-target-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}
.pickup-target-label {
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 2px;
}
.pickup-target-route {
  font-size: 12px;
  color: var(--muted);
}
.pickup-distance {
  margin-top: 0;
  margin-bottom: 14px;
}
.pickup-distance span {
  color: var(--text);
  font-weight: 700;
}

.modal-actions-stacked {
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}
.modal-actions-stacked .btn { width: 100%; text-align: center; text-decoration: none; }

/* Disabled FAB state */
.fab:disabled {
  opacity: .55;
  cursor: not-allowed;
}

/* Closest-pin highlight (Pick Up mode) */
.pin-marker.closest {
  box-shadow: var(--shadow),
              0 0 0 3px rgba(255, 255, 255, 0.9),
              0 0 0 7px rgba(31, 111, 235, 0.55);
  animation: pinPulse 1.5s ease-in-out infinite;
}
@keyframes pinPulse {
  0%, 100% {
    box-shadow: var(--shadow),
                0 0 0 3px rgba(255, 255, 255, 0.9),
                0 0 0 7px rgba(31, 111, 235, 0.55);
  }
  50% {
    box-shadow: var(--shadow),
                0 0 0 3px rgba(255, 255, 255, 0.9),
                0 0 0 12px rgba(31, 111, 235, 0.2);
  }
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 1000;
  padding: 0;
}
.modal.hidden { display: none; }

.modal-card {
  background: var(--panel);
  width: 100%;
  max-width: 480px;
  border-top-left-radius: var(--radius);
  border-top-right-radius: var(--radius);
  padding: 20px 18px calc(20px + var(--safe-bottom));
  box-shadow: var(--shadow);
  animation: slideUp .18s ease-out;
}

@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

@media (min-width: 640px) {
  .modal { align-items: center; padding: 20px; }
  .modal-card { border-radius: var(--radius); }
}

.modal h2 {
  margin: 0 0 4px;
  font-size: 18px;
}

.modal-sub {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 13px;
}

.modal label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 12px;
}

.modal input,
.modal textarea,
.modal select {
  display: block;
  width: 100%;
  margin-top: 6px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid #2a3140;
  border-radius: 10px;
  padding: 11px 12px;
  font-size: 15px;
  font-family: inherit;
  resize: vertical;
}
.modal input:focus,
.modal textarea:focus,
.modal select:focus {
  outline: none;
  border-color: var(--accent);
}

.projects-list {
  margin: 0 0 12px;
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid #2a3140;
  border-radius: 10px;
  background: var(--panel-2);
}

.project-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid #232a36;
  cursor: pointer;
}
.project-row:last-child { border-bottom: none; }
.project-row:hover { background: rgba(255,255,255,.03); }
.project-row.active {
  background: rgba(31,111,235,.12);
}

.project-swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid rgba(255,255,255,.6);
}

.project-name {
  flex: 1;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.project-active-tag {
  font-size: 11px;
  color: var(--accent-hi);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.project-delete {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}
.project-delete:hover { color: var(--danger); }

.filter-row .project-check {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 2px solid #3a4250;
  background: transparent;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 0;
}
.filter-row.checked .project-check {
  background: var(--accent);
  border-color: var(--accent);
  font-size: 14px;
}
.filter-row.checked .project-check::before {
  content: "✓";
  line-height: 1;
}

.project-empty {
  padding: 16px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.project-add-row {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}
.project-add-row input {
  flex: 1;
  margin-top: 0;
}
.project-add-row .btn {
  margin-top: 0;
  white-space: nowrap;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 8px;
}

.btn {
  border: none;
  padding: 11px 16px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
}
.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hi); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-danger { background: var(--danger); color: white; }

.btn-icon {
  padding: 10px 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}
.btn-icon svg { display: block; }

.muted { color: var(--muted); font-weight: 400; }

.history {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 92vw);
  background: var(--panel);
  z-index: 900;
  box-shadow: -8px 0 24px rgba(0,0,0,.35);
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform .2s ease;
}
.history.hidden { transform: translateX(100%); pointer-events: none; }

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid #232a36;
}
.history-head h2 { margin: 0; font-size: 16px; }

.history-filter {
  padding: 10px 16px;
  border-bottom: 1px solid #232a36;
}
.history-filter label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin: 0;
}
.history-filter select {
  display: block;
  width: 100%;
  margin-top: 6px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid #2a3140;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.history-item {
  padding: 12px 16px;
  border-bottom: 1px solid #1f2530;
  font-size: 14px;
}
.history-item .h-label {
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}
.history-item .h-project-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.history-item .h-tag {
  margin-left: auto;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  color: white;
  font-weight: 600;
  letter-spacing: .3px;
}
.history-item .h-tag.done    { background: var(--history); }
.history-item .h-tag.missing { background: var(--danger); }
.history-item .h-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}
.history-item .h-actions {
  margin-top: 8px;
  display: flex;
  gap: 8px;
}
.history-empty {
  padding: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(110px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  z-index: 2000;
  box-shadow: var(--shadow);
  border: 1px solid #2a3140;
}
.toast.hidden { display: none; }

/* Leaflet popup overrides */
.leaflet-popup-content-wrapper {
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
.leaflet-popup-tip { background: var(--panel); }
.leaflet-popup-content {
  margin: 14px 16px;
  font-size: 14px;
  min-width: 240px;
}
.leaflet-popup-content .p-label { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.leaflet-popup-content .p-meta { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.leaflet-popup-content .p-notes { margin: 6px 0 10px; white-space: pre-wrap; }
.leaflet-popup-content .p-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.leaflet-popup-content .p-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.leaflet-popup-content .p-header-tags {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.leaflet-popup-content .p-edit {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  line-height: 0;
  flex-shrink: 0;
}
.leaflet-popup-content .p-edit:hover {
  color: var(--text);
  background: rgba(255,255,255,.06);
}

.leaflet-popup-content .p-pill {
  display: inline-block;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--ok);
  color: white;
}
.leaflet-popup-content .p-pill.done { background: var(--history); }
.leaflet-popup-content .p-pill.missing { background: var(--danger); }
.leaflet-popup-content .p-project {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: 6px;
  font-size: 12px;
  color: var(--muted);
}
.leaflet-popup-content .p-project-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

/* "You are here" marker — pulsing dot */
.me-marker {
  position: relative;
  width: 22px;
  height: 22px;
}
.me-marker .me-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #1f6feb;
  border: 2px solid white;
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
  transform: translate(-50%, -50%);
}
.me-marker .me-pulse {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(31, 111, 235, 0.35);
  transform: translate(-50%, -50%);
  animation: mePulse 1.8s ease-out infinite;
}
@keyframes mePulse {
  0%   { transform: translate(-50%, -50%) scale(.6); opacity: .8; }
  100% { transform: translate(-50%, -50%) scale(2.6); opacity: 0; }
}

/* Custom pin look — active vs picked up */
.pin-marker {
  width: 28px;
  height: 28px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 2px solid white;
  background: var(--accent);
  box-shadow: var(--shadow);
}
.pin-marker.done {
  opacity: .5;
  filter: grayscale(.4);
}
.pin-marker .pin-dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 8px;
  background: white;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
