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

:root {
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  --text:   #1d1d1f;
  --text-2: #6e6e73;
  --text-3: #aeaeb2;
  --blue:   #0071e3;
  --blue-light: rgba(0,113,227,0.10);
  --blue-ring:  rgba(0,113,227,0.25);
  --green: #34c759;
  --red:   #ff3b30;
  --red-light: rgba(255,59,48,0.10);
  --surface: rgba(255,255,255,0.92);
  --border:  rgba(0,0,0,0.08);
  --shadow:  0 4px 20px rgba(0,0,0,0.10);
  --shadow-lg: 0 8px 40px rgba(0,0,0,0.14);
  --r: 14px;
  --r-sm: 10px;
  --panel-w: 300px;
  --panel-peek: 80px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: var(--text);
  background: #e8e8ed;
}

/* ─── Map ─────────────────────────────────────────── */

#map {
  position: fixed;
  inset: 0;
  z-index: 0;
}

/* Leaflet controls */
.leaflet-control-zoom {
  border: none !important;
  box-shadow: var(--shadow) !important;
  border-radius: var(--r-sm) !important;
  overflow: hidden;
}
.leaflet-control-zoom a {
  width: 36px !important;
  height: 36px !important;
  line-height: 36px !important;
  font-size: 16px !important;
  font-weight: 400 !important;
  color: var(--text) !important;
  background: var(--surface) !important;
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border) !important;
}
.leaflet-control-zoom a:last-child { border-bottom: none !important; }
.leaflet-control-zoom a:hover { color: var(--blue) !important; background: white !important; }

.leaflet-control-attribution {
  background: rgba(255,255,255,0.75) !important;
  backdrop-filter: blur(8px);
  font-size: 10px !important;
  padding: 2px 6px !important;
  border-radius: 6px 0 0 0;
}
.leaflet-control-attribution a { color: var(--text-2) !important; }

/* Custom markers */
.wp-marker {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2.5px solid white;
  box-shadow: 0 2px 8px rgba(0,0,0,0.28);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font);
  font-size: 12px;
  font-weight: 700;
  color: white;
  background: var(--blue);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  user-select: none;
}
.wp-marker:hover { transform: scale(1.15); box-shadow: 0 4px 12px rgba(0,0,0,0.3); }
.wp-marker.start { background: var(--green); }
.wp-marker.end   { background: var(--red); }

/* ─── Search ─────────────────────────────────────── */

#search-container {
  position: fixed;
  top: 14px;
  left: calc(var(--panel-w) + 16px);
  right: 14px;
  max-width: 400px;
  z-index: 200;
}

#search-bar {
  height: 46px;
  background: var(--surface);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  transition: box-shadow 0.2s, border-color 0.2s;
}
#search-bar:focus-within {
  box-shadow: var(--shadow), 0 0 0 3px var(--blue-ring);
  border-color: rgba(0,113,227,0.4);
}

.search-icon {
  width: 17px;
  height: 17px;
  color: var(--text-3);
  flex-shrink: 0;
}

#search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-family: var(--font);
  font-size: 15px;
  color: var(--text);
  min-width: 0;
}
#search-input::placeholder { color: var(--text-2); }

#search-clear {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text-3);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
#search-clear.visible { opacity: 1; pointer-events: auto; }
#search-clear:hover { background: var(--text-2); }
#search-clear svg { width: 9px; height: 9px; color: white; stroke-width: 3; }

#search-results {
  margin-top: 6px;
  background: var(--surface);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  display: none;
}
#search-results.visible { display: block; }

.search-result {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.search-result:last-child { border-bottom: none; }
.search-result:hover, .search-result.selected { background: rgba(0,0,0,0.04); }
.search-result:active { background: rgba(0,0,0,0.08); }

.result-pin {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--blue-light);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.result-pin svg { width: 14px; height: 14px; color: var(--blue); }

.result-text { min-width: 0; }
.result-name {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.result-sub {
  font-size: 12px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

/* ─── Panel ──────────────────────────────────────── */

#panel {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--panel-w);
  background: var(--surface);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 28px rgba(0,0,0,0.07);
  z-index: 100;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#panel-handle { display: none; }

#panel-header {
  padding: 20px 14px 10px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  flex-shrink: 0;
}

#panel-title {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.4px;
}

#panel-stats {
  font-size: 13px;
  color: var(--text-2);
  margin-top: 2px;
}

/* ─── Icon button ─────────────────────────────────── */

.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(0,0,0,0.06);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: rgba(0,0,0,0.10); }
.icon-btn.active { background: var(--blue-light); }
.icon-btn.active svg { color: var(--blue); }
.icon-btn svg { width: 16px; height: 16px; color: var(--text); }

/* ─── Waypoint list ───────────────────────────────── */

#waypoint-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 8px;
  scroll-behavior: smooth;
}

#waypoint-list::-webkit-scrollbar { width: 4px; }
#waypoint-list::-webkit-scrollbar-track { background: transparent; }
#waypoint-list::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.14); border-radius: 2px; }

/* Empty state */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 44px 24px 24px;
  text-align: center;
  color: var(--text-2);
}
.empty-state svg {
  width: 44px;
  height: 44px;
  color: var(--text-3);
  margin-bottom: 14px;
}
.empty-state p {
  font-size: 14px;
  line-height: 1.6;
  max-width: 220px;
}

/* Waypoint item */
.wp-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 8px;
  border-radius: 10px;
  transition: background 0.1s;
  position: relative;
}
.wp-item:hover { background: rgba(0,0,0,0.04); }
.wp-item.dragging { opacity: 0.4; background: transparent; }
.wp-item.drag-over { background: var(--blue-light); }

.drag-handle {
  cursor: grab;
  color: var(--text-3);
  display: flex;
  align-items: center;
  padding: 2px 0;
  flex-shrink: 0;
}
.drag-handle:active { cursor: grabbing; }
.drag-handle svg { width: 14px; height: 14px; }

.wp-badge {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--blue);
  color: white;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s;
}
.wp-badge.start { background: var(--green); }
.wp-badge.end   { background: var(--red); }

.wp-info {
  flex: 1;
  min-width: 0;
}
.wp-name {
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text);
}
.wp-name.loading { color: var(--text-3); }
.wp-coords {
  font-size: 11px;
  color: var(--text-3);
  font-variant-numeric: tabular-nums;
  margin-top: 1px;
}

.wp-remove {
  width: 20px;
  height: 20px;
  border: none;
  background: transparent;
  border-radius: 50%;
  cursor: pointer;
  color: var(--text-3);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
}
.wp-item:hover .wp-remove { opacity: 1; }
.wp-remove:hover { background: var(--red-light); color: var(--red); }
.wp-remove svg { width: 10px; height: 10px; stroke-width: 2.5; }

/* Connector between items */
.wp-connector {
  width: 2px;
  height: 6px;
  background: rgba(0,113,227,0.2);
  margin-left: calc(6px + 14px + 6px + 12px); /* matches badge center */
  border-radius: 1px;
}

/* ─── Actions ─────────────────────────────────────── */

#panel-actions {
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 7px;
  flex-shrink: 0;
}

.btn {
  border: none;
  border-radius: var(--r-sm);
  cursor: pointer;
  font-family: var(--font);
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 10px 16px;
  transition: background 0.15s, opacity 0.2s, transform 0.1s;
  width: 100%;
  letter-spacing: -0.1px;
}
.btn:active:not(:disabled) { transform: scale(0.98); }
.btn:disabled { opacity: 0.35; pointer-events: none; }
.btn svg { width: 15px; height: 15px; flex-shrink: 0; }

.btn.primary { background: var(--blue); color: white; }
.btn.primary:hover { background: #0077ed; }

.btn-row {
  display: flex;
  gap: 6px;
}
.btn-row .btn {
  flex: 1;
  font-size: 13px;
  padding: 9px 8px;
}

.btn.secondary { background: var(--blue-light); color: var(--blue); }
.btn.secondary:hover { background: rgba(0,113,227,0.15); }

.btn.danger { background: var(--red-light); color: var(--red); }
.btn.danger:hover { background: rgba(255,59,48,0.15); }

/* ─── Routing indicator ───────────────────────────── */

#routing-indicator {
  position: fixed;
  z-index: 150;
  top: 70px;
  left: calc(var(--panel-w) + 16px);
  background: var(--surface);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 6px 12px 6px 10px;
  font-size: 12px;
  color: var(--text-2);
  display: flex;
  align-items: center;
  gap: 7px;
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 0.2s, transform 0.2s;
}
#routing-indicator.visible {
  opacity: 1;
  transform: translateY(0);
}

.spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
  flex-shrink: 0;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ─── Mobile ──────────────────────────────────────── */

@media (max-width: 767px) {
  #search-container {
    top: 10px;
    left: 10px;
    right: 10px;
    max-width: none;
  }

  #panel {
    top: auto;
    right: 0;
    width: 100%;
    border-right: none;
    border-radius: 20px 20px 0 0;
    border-top: 1px solid var(--border);
    box-shadow: 0 -4px 28px rgba(0,0,0,0.10);
    height: var(--panel-peek);
    transition: height 0.35s cubic-bezier(0.32, 0.72, 0, 1);
  }
  #panel.open {
    height: min(70vh, 520px);
  }

  #panel-handle {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0 6px;
    cursor: pointer;
    flex-shrink: 0;
  }
  #panel-handle::after {
    content: '';
    width: 36px;
    height: 4px;
    background: var(--border);
    border-radius: 2px;
  }

  #panel-header {
    padding: 4px 14px 10px;
    cursor: pointer;
  }

  #routing-indicator {
    top: auto;
    left: 50%;
    bottom: calc(var(--panel-peek) + 10px);
    transform: translateX(-50%) translateY(6px);
  }
  #routing-indicator.visible {
    transform: translateX(-50%) translateY(0);
  }

  .leaflet-control-zoom { display: none; }

  .leaflet-bottom.leaflet-right {
    bottom: calc(var(--panel-peek) + 4px);
  }
}

@media (min-width: 768px) {
  #panel-handle { display: none; }
}

/* ─── Search messages ─────────────────────────────── */

.search-msg {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px;
  font-size: 13px;
  color: var(--text-2);
  text-align: center;
}

/* ─── Route options (toggles) ─────────────────────── */

#route-options {
  padding: 8px 14px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 0;
  cursor: pointer;
  user-select: none;
}

.toggle-label {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.toggle-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  letter-spacing: -0.1px;
}
.toggle-sub {
  font-size: 11px;
  color: var(--text-2);
  margin-top: 1px;
}

/* iOS-style switch */
.switch {
  position: relative;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}
.switch input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.slider {
  position: absolute;
  inset: 0;
  background: rgba(120,120,128,0.32);
  border-radius: 999px;
  transition: background 0.25s;
  pointer-events: none;
}
.slider::before {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  transition: transform 0.25s cubic-bezier(0.32, 0.72, 0, 1);
}
.switch input:checked + .slider { background: var(--green); }
.switch input:checked + .slider::before { transform: translateX(18px); }
.switch input:focus-visible + .slider { box-shadow: 0 0 0 3px var(--blue-ring); }

/* ─── Static list rows (current location / return) ── */

.wp-item.static { cursor: default; }
.wp-item.static:hover { background: transparent; }

.wp-badge.glyph {
  background: var(--blue);
  color: white;
}
.wp-badge.glyph svg {
  width: 13px;
  height: 13px;
}
.wp-badge.glyph.start { background: var(--green); }
.wp-badge.glyph.end   { background: var(--red); }

.live-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  margin-left: 6px;
  vertical-align: middle;
  animation: live-blink 1.6s ease-in-out infinite;
}
@keyframes live-blink {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.3; }
}

/* ─── Live location marker ────────────────────────── */

.current-dot {
  position: relative;
  width: 22px;
  height: 22px;
}
.current-dot-core {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  background: var(--blue);
  border: 2.5px solid white;
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
}
.current-dot-pulse {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 22px;
  height: 22px;
  margin: -11px 0 0 -11px;
  background: var(--blue);
  border-radius: 50%;
  opacity: 0.35;
  animation: dot-pulse 2s ease-out infinite;
}
@keyframes dot-pulse {
  0%   { transform: scale(0.6); opacity: 0.4; }
  100% { transform: scale(2.4); opacity: 0; }
}
