:root {
  --bg: #111318;
  --panel: #1b1e27;
  --panel-2: #242835;
  --text: #eef0f6;
  --text-dim: #9aa0b4;
  --accent: #ff8a00;
  --accent-2: #34d399;
  --danger: #ef4444;
  --border: #2e3340;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}

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

html, body {
  margin: 0; padding: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

#map { position: absolute; inset: 0; background: #0c0d10; }

/* Leaflet dark tweaks */
.leaflet-tile-pane { filter: brightness(0.62) invert(1) contrast(0.9) hue-rotate(180deg) saturate(0.6); }
.leaflet-control-attribution { background: rgba(17,19,24,0.7) !important; color: var(--text-dim) !important; font-size: 10px !important; }
.leaflet-control-attribution a { color: var(--text-dim) !important; }
.leaflet-control-zoom { display: none; }
.leaflet-popup-content-wrapper { background: var(--panel); color: var(--text); border-radius: 12px; }
.leaflet-popup-tip { background: var(--panel); }
.leaflet-popup-content { margin: 12px 14px; min-width: 180px; }
.leaflet-popup-content h4 { margin: 0 0 4px; font-size: 15px; }
.leaflet-popup-content img { width: 100%; border-radius: 8px; margin-bottom: 6px; display: block; }

/* Grid cell labels */
.grid-cell-label {
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 12px; color: #0b0c0f;
  text-shadow: 0 1px 1px rgba(255,255,255,0.25);
  pointer-events: none;
}
.city-marker {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  border-radius: 50%; border: 2px solid rgba(255,255,255,0.75);
  font-weight: 700; color: #0b0c0f; text-align: center; line-height: 1.05;
  box-shadow: 0 2px 8px rgba(0,0,0,0.45);
}
.city-marker .cm-name { font-size: 10px; text-transform: uppercase; letter-spacing: 0.02em; }
.city-marker .cm-pct { font-size: 13px; }

/* Top bar */
#topbar {
  position: absolute; top: calc(10px + var(--safe-top)); left: 10px; right: 10px;
  display: flex; justify-content: space-between; align-items: flex-start;
  z-index: 500; pointer-events: none;
}
.pill {
  pointer-events: auto;
  background: rgba(27,30,39,0.92); border: 1px solid var(--border);
  border-radius: 999px; padding: 8px 14px; font-size: 13px;
  display: flex; align-items: center; gap: 6px; backdrop-filter: blur(6px);
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
}
.pill-btn { border: 1px solid var(--border); color: var(--text); font: inherit; cursor: pointer; }
.pill[data-state="synced"] { border-color: var(--accent-2); }
.pill[data-state="syncing"] { border-color: var(--accent); }
.pill[data-state="error"] { border-color: var(--danger); }

#toast {
  position: absolute; left: 50%; top: calc(60px + var(--safe-top));
  transform: translateX(-50%) translateY(-10px);
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 14px; font-size: 13px; z-index: 900; opacity: 0; pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease; max-width: 88vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Floating action column */
#fab-col {
  position: absolute; right: 12px; bottom: calc(84px + var(--safe-bottom));
  display: flex; flex-direction: column; gap: 10px; z-index: 500;
}
.fab {
  width: 46px; height: 46px; border-radius: 50%; border: 1px solid var(--border);
  background: rgba(27,30,39,0.92); color: var(--text); font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.35); cursor: pointer;
}
.fab[aria-pressed="true"] { background: var(--accent); color: #16130a; border-color: var(--accent); }

/* Route bar */
.sheet-bar {
  position: absolute; left: 10px; right: 10px; bottom: calc(70px + var(--safe-bottom));
  background: var(--panel-2); border: 1px solid var(--accent); border-radius: 12px;
  padding: 10px 14px; z-index: 550; display: flex; align-items: center; justify-content: space-between;
  gap: 10px; font-size: 13px;
}
.sheet-bar.hidden { display: none; }

/* Bottom sheet */
.sheet-handle-btn {
  position: absolute; left: 50%; bottom: calc(10px + var(--safe-bottom)); transform: translateX(-50%);
  background: var(--panel); border: 1px solid var(--border); color: var(--text);
  border-radius: 999px; padding: 8px 16px; font-size: 12px; z-index: 720; cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,0.35);
}
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 700;
  background: var(--panel); border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0; box-shadow: 0 -4px 20px rgba(0,0,0,0.4);
  max-height: 78vh; display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform 0.28s ease;
  padding-bottom: var(--safe-bottom);
}
.sheet:not(.closed) { transform: translateY(0); }
.sheet-grip { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 10px auto 4px; }
.sheet-tabs { display: flex; gap: 4px; padding: 6px 12px; border-bottom: 1px solid var(--border); }
.tab-btn {
  flex: 1; background: transparent; border: none; color: var(--text-dim); font: inherit; font-size: 13px;
  padding: 10px 4px; border-radius: 8px; cursor: pointer;
}
.tab-btn.active { background: var(--panel-2); color: var(--text); font-weight: 600; }
.tab-panel { display: none; padding: 14px 16px 20px; overflow-y: auto; }
.tab-panel.active { display: block; }

.hint { font-size: 13px; color: var(--text-dim); margin: 0 0 10px; line-height: 1.4; }
.hint-small { font-size: 11px; color: var(--text-dim); margin-top: 8px; line-height: 1.4; }
.hint-small a { color: var(--accent); }

.row { display: flex; gap: 8px; margin-bottom: 10px; align-items: center; }
input[type="text"] {
  flex: 1; background: var(--panel-2); border: 1px solid var(--border); color: var(--text);
  border-radius: 10px; padding: 11px 12px; font-size: 14px; min-width: 0;
}
input[type="range"] { flex: 1; }
.slider-label { font-size: 12px; color: var(--text-dim); display: block; margin-bottom: 6px; }

.addr-field { margin-bottom: 8px; }
.addr-field input[type="text"] { width: 100%; }
.addr-suggestions:not(:empty) {
  margin-top: 4px; border-radius: 10px; overflow: hidden; border: 1px solid var(--border);
}
.addr-suggestions div {
  padding: 9px 12px; font-size: 13px; background: var(--panel-2); cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.addr-suggestions div:last-child { border-bottom: none; }
.addr-suggestions div:active { background: #2c313f; }

.btn-primary, .btn-secondary {
  border-radius: 10px; padding: 11px 14px; font-size: 13px; font-weight: 600; border: none; cursor: pointer;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #16130a; }
.btn-secondary { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.btn-primary:disabled, .btn-secondary:disabled { opacity: 0.4; cursor: not-allowed; }
.link-btn { background: none; border: none; color: var(--accent); font-size: 13px; cursor: pointer; }

.card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; margin-top: 8px;
}
.card.hidden { display: none; }
.player-name { font-weight: 700; font-size: 15px; margin-bottom: 8px; }
.player-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.player-grid span { display: block; font-size: 16px; font-weight: 700; color: var(--accent); }
.player-grid label { display: block; font-size: 10px; color: var(--text-dim); margin-top: 2px; }

#routeStops { list-style: none; margin: 10px 0 0; padding: 0; }
#routeStops li {
  display: flex; justify-content: space-between; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border);
  font-size: 13px;
}
#routeStops li .stop-idx { color: var(--accent); font-weight: 700; margin-right: 6px; }
#routeStops li .stop-dist { color: var(--text-dim); font-size: 12px; white-space: nowrap; }

#cityList { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.city-row {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; background: var(--panel-2);
  border-radius: 10px; cursor: pointer; border: 1px solid transparent;
}
.city-row:active { border-color: var(--accent); }
.city-row .cr-bar { flex: 1; height: 8px; border-radius: 4px; background: #333846; overflow: hidden; }
.city-row .cr-bar-fill { height: 100%; border-radius: 4px; }
.city-row .cr-name {
  width: 100px; font-size: 12px; font-weight: 700; flex-shrink: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.city-row .cr-pct { width: 62px; text-align: right; font-size: 11px; color: var(--text-dim); flex-shrink: 0; }

.modal {
  position: absolute; inset: 0; background: rgba(0,0,0,0.6); z-index: 900;
  display: flex; align-items: flex-start; justify-content: center; padding-top: 15vh;
}
.modal.hidden { display: none; }
.modal-content { background: var(--panel); border-radius: 14px; padding: 16px; width: min(90vw, 380px); border: 1px solid var(--border); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; font-size: 14px; font-weight: 600; }
#lookupSuggestions { max-height: 40vh; overflow-y: auto; margin-top: 8px; }
#lookupSuggestions div { padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 14px; cursor: pointer; }

.filter-group { margin-bottom: 16px; }
.filter-group:last-child { margin-bottom: 0; }
.filter-check {
  display: flex; align-items: center; gap: 8px; padding: 9px 2px; font-size: 14px; cursor: pointer;
}
.filter-check input { width: 17px; height: 17px; accent-color: var(--accent); flex-shrink: 0; }
.filter-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; border: 1px solid rgba(255,255,255,0.4); }

.user-heading-marker { position: relative; width: 36px; height: 36px; transition: transform 0.15s linear; }
.user-core {
  position: absolute; left: 50%; top: 50%; width: 14px; height: 14px; margin: -7px 0 0 -7px;
  background: #4d9dff; border: 3px solid #fff; border-radius: 50%; box-shadow: 0 0 0 4px rgba(77,157,255,0.3);
}
.user-cone {
  position: absolute; left: 50%; top: 50%; width: 0; height: 0;
  border-left: 9px solid transparent; border-right: 9px solid transparent;
  border-bottom: 22px solid rgba(77,157,255,0.4);
  margin: -30px 0 0 -9px;
  display: none;
}
.user-heading-marker.has-heading .user-cone { display: block; }

@media (min-width: 720px) {
  .sheet { left: auto; right: 16px; bottom: 16px; width: 380px; border-radius: 16px; max-height: 82vh; box-shadow: 0 8px 30px rgba(0,0,0,0.5); }
  .sheet-handle-btn { left: auto; right: 16px; transform: none; }
  #fab-col { right: 410px; }
  .sheet.closed ~ #fab-col { right: 12px; }
}
