/* ===== CSS Custom Properties ===== */
:root {
  --color-road: #9E9E9E;
  --color-pavement: #2196F3;
  --color-pcn: #4CAF50;
  --color-shared: #FF9800;
  --color-trail: #795548;

  --panel-width: 320px;
  --panel-bg: #ffffff;
  --panel-shadow: 2px 0 12px rgba(0,0,0,0.12);

  --btn-primary: #2563eb;
  --btn-primary-hover: #1d4ed8;
  --btn-secondary: #f3f4f6;
  --btn-secondary-hover: #e5e7eb;

  --text-primary: #111827;
  --text-secondary: #6b7280;
  --text-error: #dc2626;

  --border: #e5e7eb;
  --radius: 8px;
  --radius-sm: 4px;

  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; font-family: var(--font); color: var(--text-primary); }

/* ===== Layout ===== */
#app {
  display: flex;
  height: 100vh;
  width: 100vw;
}

/* ===== Side Panel (desktop) ===== */
#panel {
  width: var(--panel-width);
  min-width: var(--panel-width);
  height: 100%;
  background: var(--panel-bg);
  box-shadow: var(--panel-shadow);
  display: flex;
  flex-direction: column;
  z-index: 10;
  overflow: hidden;
}

#panel-header {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#panel-header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
}

#panel-header p {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 3px;
}

#panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ===== Form Sections ===== */
.form-section { display: flex; flex-direction: column; gap: 6px; }

.field-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

/* ===== Search Field ===== */
.search-row {
  display: flex;
  gap: 6px;
}

#start-input,
#end-input,
#via-input {
  flex: 1;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.15s;
}
#start-input:focus,
#end-input:focus,
#via-input:focus { border-color: var(--btn-primary); }

#search-btn,
#end-search-btn,
#via-search-btn {
  padding: 9px 14px;
  background: var(--btn-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.875rem;
  transition: background 0.15s;
}
#search-btn:hover,
#end-search-btn:hover,
#via-search-btn:hover { background: var(--btn-secondary-hover); }

.field-label-optional {
  font-size: 0.68rem;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-secondary);
}

.field-hint {
  font-size: 0.72rem;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* ===== Distance Buttons ===== */
.distance-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
}

.dist-btn {
  padding: 7px 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--btn-secondary);
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
}
.dist-btn:hover { background: var(--btn-secondary-hover); }
.dist-btn-custom { width: 100%; margin-top: 4px; }
.dist-btn.active {
  background: var(--btn-primary);
  color: white;
  border-color: var(--btn-primary);
}

.custom-distance-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}
.custom-distance-row label { font-size: 0.8rem; color: var(--text-secondary); white-space: nowrap; }
#custom-dist {
  width: 80px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
  outline: none;
}
#custom-dist:focus { border-color: var(--btn-primary); }

/* ===== Radio Groups ===== */
.radio-group {
  display: flex;
  gap: 6px;
}

.radio-btn {
  flex: 1;
  position: relative;
}

.radio-btn input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.radio-btn label {
  display: block;
  padding: 8px 6px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text-primary);
}

.radio-btn input[type="radio"]:checked + label {
  background: var(--btn-primary);
  color: white;
  border-color: var(--btn-primary);
}

/* ===== Route preferences ===== */
.pref-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
  font-size: 0.8rem;
  color: var(--text-primary);
  cursor: pointer;
}
.pref-toggle input { width: 16px; height: 16px; accent-color: var(--btn-primary); cursor: pointer; }
.pref-via-label { margin-top: 8px; }
.path-pct { color: var(--text-secondary); }
.route-note {
  margin: 6px 0 2px;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  background: #fffbeb;
  border: 1px solid #fcd34d;
  color: #92400e;
  font-size: 0.72rem;
  line-height: 1.35;
}
.via-marker { font-size: 1.4rem; filter: drop-shadow(0 1px 2px rgba(0,0,0,0.4)); cursor: default; }

/* ===== Buttons ===== */
.btn-primary {
  width: 100%;
  padding: 11px;
  background: var(--btn-primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn-primary:hover:not(:disabled) { background: var(--btn-primary-hover); }
.btn-primary:disabled { opacity: 0.65; cursor: not-allowed; }

.btn-secondary {
  width: 100%;
  padding: 10px;
  background: var(--btn-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  display: none;
}
.btn-secondary:hover:not(:disabled) { background: var(--btn-secondary-hover); }
.btn-secondary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-secondary.visible { display: block; }
.btn-secondary.active {
  background: #dbeafe;
  border-color: #3b82f6;
  color: #1e40af;
}

/* ===== Manual mode ===== */

#manual-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: #eff6ff;
  border: 1px solid #3b82f6;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 0.78rem;
  font-weight: 500;
  color: #1e40af;
  z-index: 5;
  white-space: nowrap;
  display: none;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
#manual-banner.visible { display: block; }

#manual-controls {
  position: absolute;
  bottom: 32px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  gap: 8px;
  z-index: 5;
}
#manual-controls.visible { display: flex; }
#manual-controls button {
  padding: 8px 16px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
#manual-undo-btn { background: white; color: var(--text-primary); }
#manual-undo-btn:disabled { opacity: 0.5; cursor: not-allowed; }
#manual-clear-btn { background: white; color: var(--text-error); border-color: #fecaca; }
#manual-clear-btn:disabled { opacity: 0.5; cursor: not-allowed; }
#manual-finish-btn { background: var(--btn-primary); color: white; border-color: var(--btn-primary); }
#manual-finish-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.manual-waypoint-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #2563eb;
  border: 2px solid white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
  cursor: pointer;
}
.manual-waypoint-dot.first {
  background: #16a34a;
}

/* ===== Error message ===== */
#error-msg {
  font-size: 0.8rem;
  color: var(--text-error);
  display: none;
  padding: 8px 10px;
  background: #fef2f2;
  border-radius: var(--radius-sm);
  border: 1px solid #fecaca;
}
#error-msg.visible { display: block; }

/* ===== Map ===== */
#map {
  flex: 1;
  height: 100%;
  position: relative;
}

/* ===== Demo banner ===== */
#demo-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: #fef3c7;
  border: 1px solid #f59e0b;
  border-radius: 20px;
  padding: 6px 16px;
  font-size: 0.78rem;
  font-weight: 500;
  color: #92400e;
  z-index: 5;
  white-space: nowrap;
  display: none;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
#demo-banner.visible { display: block; }

/* ===== Stats Card ===== */
#stats-card {
  position: absolute;
  top: 12px;
  right: 12px;
  background: white;
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  padding: 14px 16px;
  min-width: 200px;
  max-width: 240px;
  z-index: 5;
  display: none;
}
#stats-card.visible { display: block; }

#stats-card h3 {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
}
.stat-label { font-size: 0.8rem; color: var(--text-secondary); }
.stat-value { font-size: 0.95rem; font-weight: 700; color: var(--text-primary); }

.path-breakdown { margin-top: 10px; border-top: 1px solid var(--border); padding-top: 10px; }
.path-breakdown-title { font-size: 0.7rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; color: var(--text-secondary); margin-bottom: 6px; }
.path-row { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; font-size: 0.78rem; }
.path-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* ===== Legend ===== */
#legend {
  position: absolute;
  bottom: 28px;
  left: 12px;
  background: white;
  border-radius: var(--radius);
  box-shadow: 0 2px 10px rgba(0,0,0,0.12);
  padding: 10px 14px;
  z-index: 5;
  font-size: 0.75rem;
}

#legend h4 {
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-secondary);
  margin-bottom: 7px;
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 5px;
}
.legend-row:last-child { margin-bottom: 0; }
.legend-line {
  width: 24px;
  height: 4px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* ===== Spinner ===== */
.spinner {
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  display: none;
}
.spinner.visible { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Mapbox popup overrides ===== */
.mapboxgl-popup-content {
  border-radius: var(--radius) !important;
  padding: 12px 14px !important;
  font-family: var(--font) !important;
  font-size: 0.82rem !important;
  max-width: 200px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15) !important;
}
.poi-popup-name { font-weight: 600; margin-bottom: 4px; }
.poi-popup-dist { color: var(--text-secondary); font-size: 0.75rem; }

/* ===== Locate me button ===== */
#locate-btn {
  position: absolute;
  bottom: 80px;
  right: 12px;
  width: 36px;
  height: 36px;
  background: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  z-index: 5;
  transition: background 0.15s;
}
#locate-btn:hover { background: #f3f4f6; }
#locate-btn.locating { cursor: progress; animation: locate-pulse 1s ease-in-out infinite; }
@keyframes locate-pulse { 50% { opacity: 0.4; } }

/* ===== Mobile styles ===== */
@media (max-width: 768px) {
  #app { flex-direction: column; }

  #panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    min-width: unset;
    height: auto;
    max-height: 70vh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
    z-index: 20;
    transition: max-height 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  }

  #panel.collapsed { max-height: 64px; }

  #map { height: 100vh; width: 100vw; }

  #panel-header {
    padding: 12px 20px;
    cursor: pointer;
    user-select: none;
  }

  .drag-handle {
    width: 36px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
    margin: 0 auto 10px;
  }

  #stats-card {
    top: auto;
    bottom: 80px;
    right: 8px;
    left: 8px;
    max-width: unset;
  }

  #legend { bottom: 40px; left: 8px; }

  #demo-banner { top: 8px; font-size: 0.72rem; }
}

/* ===== Drag-to-reroute handle ===== */
/* Named distinctly from the mobile panel's .drag-handle grab-bar */
.reroute-handle {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #2563eb;
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
  cursor: crosshair;
}

/* ── Startup fallback ─────────────────────────────────────── */
/* Hidden at first; appears after 4 s only if js/main.js never ran and removed it
   (page opened via file://, config.js missing, or a script failed to load). */
.load-fallback {
  /* Collapsed and invisible until the animation flips it (works in all browsers,
     unlike animating display). */
  visibility: hidden;
  height: 0;
  overflow: hidden;
  font-size: 0.8rem;
  color: #991b1b;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  border-width: 0;
  animation: load-fallback-show 0s 4s forwards;
}
@keyframes load-fallback-show {
  to { visibility: visible; height: auto; padding: 10px 12px; margin-bottom: 8px; border-width: 1px; }
}
