/* =========================================================
   Route generator — refreshed UI
   ---------------------------------------------------------
   Three themes (trailhead / compass / topo) and two layouts
   (sidebar / floating) driven by data-attrs on <html>.
   Tokens are defined per theme, structure is theme-agnostic.
   ========================================================= */

@import url("https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&family=JetBrains+Mono:wght@400;500;600&family=IBM+Plex+Sans:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&family=Space+Grotesk:wght@400;500;600;700&family=DM+Mono:wght@400;500&family=Cormorant+Garamond:wght@500;600;700&display=swap");

/* ----- Theme: Trailhead (default, sporty/outdoorsy) ----- */
:root,
:root[data-theme="trailhead"] {
  --bg: #fbf8f3;
  --bg-elev: #ffffff;
  --panel: #ffffff;
  --panel-soft: #f6f2ea;
  --fg: #1d1a16;
  --fg-soft: #423d36;
  --muted: #837a6e;
  --muted-soft: #b3aa9b;
  --border: #e8e1d2;
  --border-strong: #d6cdb9;
  --accent: #d4541c;
  --accent-soft: rgba(212, 84, 28, 0.10);
  --accent-fg: #ffffff;
  --green: #2f6845;
  --danger: #b91c1c;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(60, 50, 30, 0.05);
  --shadow: 0 6px 18px rgba(60, 50, 30, 0.08);
  --shadow-lg: 0 18px 48px rgba(60, 50, 30, 0.14);
  --font-sans: "Manrope", system-ui, -apple-system, "Segoe UI", sans-serif;
  --font-display: "Manrope", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "Cascadia Mono", monospace;
  --header-weight: 800;
  --tracking-display: -0.02em;
  --uppercase-tracking: 0.08em;
}

/* ----- Theme: Compass (minimal, monochrome) ----- */
:root[data-theme="compass"] {
  --bg: #fafafa;
  --bg-elev: #ffffff;
  --panel: rgba(255, 255, 255, 0.94);
  --panel-soft: #f4f4f5;
  --fg: #09090b;
  --fg-soft: #27272a;
  --muted: #71717a;
  --muted-soft: #a1a1aa;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --accent: #0a0a0a;
  --accent-soft: rgba(0, 0, 0, 0.06);
  --accent-fg: #ffffff;
  --green: #16a34a;
  --danger: #dc2626;
  --radius: 8px;
  --radius-sm: 6px;
  --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, 0.14);
  --font-sans: "IBM Plex Sans", system-ui, sans-serif;
  --font-display: "IBM Plex Sans", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, monospace;
  --header-weight: 600;
  --tracking-display: -0.015em;
  --uppercase-tracking: 0.12em;
}

/* ----- Theme: Topo (cartographic / paper map) ----- */
:root[data-theme="topo"] {
  --bg: #efe9d8;
  --bg-elev: #faf6ea;
  --panel: #faf6ea;
  --panel-soft: #f2ecd8;
  --fg: #2a241c;
  --fg-soft: #4a4234;
  --muted: #756c57;
  --muted-soft: #a99f86;
  --border: #d6cba8;
  --border-strong: #b6a986;
  --accent: #4f6d4a;
  --accent-soft: rgba(79, 109, 74, 0.12);
  --accent-fg: #faf6ea;
  --green: #4f6d4a;
  --danger: #973018;
  --radius: 4px;
  --radius-sm: 2px;
  --radius-lg: 6px;
  --shadow-sm: 0 1px 0 var(--border);
  --shadow: 0 2px 0 var(--border);
  --shadow-lg: 0 6px 0 var(--border);
  --font-sans: "Space Grotesk", system-ui, sans-serif;
  --font-display: "Cormorant Garamond", Georgia, serif;
  --font-mono: "DM Mono", ui-monospace, monospace;
  --header-weight: 600;
  --tracking-display: 0;
  --uppercase-tracking: 0.14em;
}

/* =====================================================
   Reset + base
   ===================================================== */
* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 12px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
}

input, select, textarea {
  font-family: inherit;
  color: inherit;
}

::selection {
  background: var(--accent);
  color: var(--accent-fg);
}

/* =====================================================
   App layout shells
   ===================================================== */
.app {
  display: flex;
  width: 100%;
  height: 100%;
  position: relative;
}

#map {
  flex: 1;
  height: 100vh;
  min-width: 0;
  background: var(--panel-soft);
}

/* ---- Sidebar layout (default) ---- */
:root[data-layout="sidebar"] .panel {
  width: 320px;
  flex-shrink: 0;
  height: 100vh;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

/* ---- Floating layout ---- */
:root[data-layout="floating"] .app {
  display: block;
}
:root[data-layout="floating"] #map {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100vh;
}
:root[data-layout="floating"] .panel {
  position: fixed;
  top: 12px;
  left: 12px;
  width: 300px;
  max-height: calc(100vh - 24px);
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--panel);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  z-index: 1100;
  display: flex;
  flex-direction: column;
}

/* In floating layout the panel sits over the top-left of the viewport,
   where Leaflet places its zoom control by default. Shove the zoom
   control over so it doesn't peek through the panel's rounded corner. */
:root[data-layout="floating"] .leaflet-top.leaflet-left {
  left: auto;
  right: 12px;
  top: 60px;
}

/* ===== Thin / themed scrollbar inside the panel =====
   Default browser scrollbar makes the floating panel look square on
   the right edge. Thin overlay-style scrollbar keeps the rounded
   corners visually intact. */
.panel {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.panel::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
.panel::-webkit-scrollbar-track {
  background: transparent;
}
.panel::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 3px;
}
.panel:hover::-webkit-scrollbar-thumb {
  background: var(--border-strong);
}
.panel::-webkit-scrollbar-thumb:hover {
  background: var(--muted-soft);
}

/* =====================================================
   Panel internals
   ===================================================== */
.panel {
  padding: 10px 10px 12px;
  gap: 8px;
}

.panel-section {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.panel-section + .panel-section {
  padding-top: 7px;
  border-top: 1px solid var(--border);
  margin-top: 0;
}

:root[data-theme="topo"] .panel-section + .panel-section {
  border-top: 1px dashed var(--border-strong);
}

/* Topo theme — paper map feel: ridge edges on the panel */
:root[data-theme="topo"][data-layout="sidebar"] .panel {
  border-right: 2px solid var(--border-strong);
  background-image:
    repeating-linear-gradient(
      0deg,
      transparent 0,
      transparent 32px,
      rgba(177, 156, 109, 0.06) 32px,
      rgba(177, 156, 109, 0.06) 33px
    );
}

/* ---- Brand row ---- */
.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0;
}

.brand-mark {
  width: 20px;
  height: 20px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: var(--accent-fg);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-family: var(--font-display);
  font-weight: var(--header-weight);
  font-size: 11px;
}

:root[data-theme="topo"] .brand-mark {
  border-radius: 50%;
  border: 2px solid var(--accent);
  background: var(--panel);
  color: var(--accent);
}

.brand-name {
  font-family: var(--font-display);
  font-weight: var(--header-weight);
  font-size: 13px;
  line-height: 1.1;
  letter-spacing: var(--tracking-display);
  color: var(--fg);
  flex: 1;
}

:root[data-theme="topo"] .brand-name {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0;
  line-height: 1;
}

.brand-version {
  font-size: 8.5px;
  font-family: var(--font-mono);
  color: var(--muted-soft);
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

/* =====================================================
   Segmented controls (mode tabs)
   ===================================================== */
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  padding: 2px;
  background: var(--panel-soft);
  border-radius: calc(var(--radius) - 2px);
  border: 1px solid var(--border);
}

/* Activity + Route style stacked into one compact row */
.mode-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

/* =====================================================
   Prefs "Tune" expander (collapsed by default)
   ===================================================== */
.prefs-details {
  margin: 0;
  gap: 0;
}
.prefs-details summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--fg-soft);
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  user-select: none;
  transition: background-color 0.12s, border-color 0.12s;
}
.prefs-details summary::-webkit-details-marker { display: none; }
.prefs-details summary:hover { border-color: var(--border-strong); }
.prefs-details[open] summary {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-bottom: none;
}
.prefs-tune-label::before {
  content: "";
  display: inline-block;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 7px;
  vertical-align: middle;
  transform: translateY(-1px);
}
.prefs-tune-chev {
  display: inline-flex;
  color: var(--muted);
  transition: transform 0.18s ease;
}
.prefs-details[open] .prefs-tune-chev {
  transform: rotate(180deg);
}
.prefs-details[open] .prefs {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-top: none;
  padding-top: 2px;
  margin-top: 0;
}

/* Hide the details wrapper entirely when its fieldset is hidden
   (map.js sets [hidden] on .prefs[data-mode] based on active mode). */
.prefs-details:has(> .prefs[hidden]) {
  display: none;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented label {
  padding: 4px 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
  border-radius: calc(var(--radius) - 4px);
  cursor: pointer;
  transition: background-color 0.12s ease, box-shadow 0.12s ease;
  user-select: none;
}

.segmented label .seg-ico {
  width: 14px;
  height: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.segmented label .seg-ico svg {
  width: 14px;
  height: 14px;
}

.segmented input:checked + label {
  background: var(--bg-elev);
  color: var(--fg);
  box-shadow: var(--shadow-sm);
}

:root[data-theme="trailhead"] .segmented input:checked + label {
  color: var(--accent);
}

:root[data-theme="topo"] .segmented input:checked + label {
  background: var(--accent);
  color: var(--accent-fg);
  box-shadow: none;
}

.segmented label:hover {
  color: var(--fg-soft);
}

.segmented input:focus-visible + label {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* =====================================================
   Field labels & rows
   ===================================================== */
.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: var(--uppercase-tracking);
  text-transform: uppercase;
  color: var(--muted);
  margin: 0;
}

:root[data-theme="topo"] .section-title {
  font-family: var(--font-mono);
  font-weight: 500;
}

.section-title .section-meta {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
  color: var(--muted-soft);
  font-size: 10px;
  white-space: nowrap;
  flex-shrink: 0;
  margin-left: 8px;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field-label {
  font-size: 12px;
  color: var(--fg-soft);
  font-weight: 500;
}

/* ===== Number input + select base ===== */
.input,
select.input {
  width: 100%;
  padding: 6px 9px;
  font-size: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--fg);
  outline: none;
  transition: border-color 0.12s;
}

.input:focus,
select.input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.with-unit {
  position: relative;
}
.with-unit .input { padding-right: 30px; }
.with-unit .unit {
  position: absolute;
  right: 9px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 11px;
  color: var(--muted);
  font-family: var(--font-mono);
}

/* =====================================================
   Sliders (preferences)
   ===================================================== */
.prefs {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 7px 8px 8px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 0;
}

:root[data-theme="topo"] .prefs {
  background: transparent;
  border: 1px dashed var(--border-strong);
}

.prefs > legend { display: none; }

.pref-row {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 0;
}

.pref-spectrum {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 6px;
  align-items: baseline;
  min-width: 0;
  line-height: 1.1;
}

.pref-name {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--fg);
  white-space: nowrap;
  text-align: center;
}

.pref-end {
  font-size: 9px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pref-end:first-child { text-align: left; }
.pref-end:last-child  { text-align: right; }

.slider-row {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

input[type="range"] {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  height: 18px;
  cursor: pointer;
  min-width: 0;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 3px;
  background: var(--border-strong);
  border-radius: 2px;
}
input[type="range"]::-moz-range-track {
  height: 3px;
  background: var(--border-strong);
  border-radius: 2px;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-elev);
  margin-top: -5px;
  cursor: grab;
  box-shadow: 0 0 0 1px var(--border-strong);
}
input[type="range"]::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--bg-elev);
  cursor: grab;
  box-shadow: 0 0 0 1px var(--border-strong);
}

input[type="range"]:active::-webkit-slider-thumb { cursor: grabbing; }

.pref-value {
  min-width: 22px;
  text-align: right;
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--muted);
  font-weight: 500;
}

/* ===== Pref checkbox option ===== */
.pref-option {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 1px 0;
  font-size: 11px;
  color: var(--fg-soft);
  cursor: pointer;
  user-select: none;
  line-height: 1.25;
}

.pref-option input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border: 1.5px solid var(--border-strong);
  border-radius: 3px;
  background: var(--bg-elev);
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
  transition: background-color 0.12s, border-color 0.12s, box-shadow 0.12s, transform 0.12s;
}
.pref-option input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}
.pref-option input[type="checkbox"]:checked::after {
  content: "";
  position: absolute;
  left: 3px;
  top: 0;
  width: 4px;
  height: 8px;
  border: solid white;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.pref-note {
  margin: 3px 0 0;
  font-size: 9.5px;
  color: var(--muted);
  line-height: 1.3;
}
.pref-note code {
  font-family: var(--font-mono);
  font-size: 0.95em;
  background: var(--bg-elev);
  padding: 0 4px;
  border-radius: 3px;
  border: 1px solid var(--border);
}
.pref-note-warn {
  color: #d97706;
  font-weight: 600;
}

/* =====================================================
   Buttons
   ===================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 10px;
  font-size: 11.5px;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  transition: background-color 0.12s, border-color 0.12s, box-shadow 0.12s, transform 0.12s;
  background: transparent;
  color: var(--fg);
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.btn-primary:hover { filter: brightness(0.94); }
.btn-primary:active { transform: translateY(1px); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-secondary {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1px solid var(--border-strong);
}
.btn-secondary:hover { background: var(--panel-soft); }

.btn-ghost {
  background: transparent;
  color: var(--fg-soft);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--panel-soft); }

.btn-block {
  width: 100%;
}

.btn-sm {
  padding: 4px 8px;
  font-size: 11px;
}

/* =====================================================
   Waypoint mode UI
   ===================================================== */
.waypoint-empty {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  background: var(--accent-soft);
  border: 1px dashed var(--accent);
  border-radius: var(--radius);
  font-size: 12.5px;
  color: var(--fg-soft);
  line-height: 1.45;
}

.waypoint-empty .we-ico {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-family: var(--font-mono);
  font-weight: 600;
}

.return-toggle {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 9px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  transition: background-color 0.12s, border-color 0.12s, box-shadow 0.12s, transform 0.12s;
}

.return-toggle:hover {
  border-color: var(--border-strong);
}

.return-toggle.is-on {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.return-toggle .rt-ico {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--accent);
}

.return-toggle .rt-ico svg {
  width: 16px;
  height: 16px;
}

.return-toggle .rt-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.return-toggle .rt-title {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--fg);
  line-height: 1.2;
}

.return-toggle .rt-subtitle {
  font-size: 9.5px;
  color: var(--muted);
  line-height: 1.2;
}

/* Hide the original checkbox; the label container is the visible control */
.return-toggle input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 30px;
  height: 17px;
  border-radius: 9px;
  background: var(--border-strong);
  position: relative;
  cursor: pointer;
  transition: background 0.18s;
  flex-shrink: 0;
}
.return-toggle input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: white;
  transition: transform 0.18s;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}
.return-toggle input[type="checkbox"]:checked {
  background: var(--accent);
}
.return-toggle input[type="checkbox"]:checked::after {
  transform: translateX(13px);
}

/* ---- Waypoint list ---- */
.wp-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.wp-row {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: grab;
  user-select: none;
  transition: background-color 0.12s, border-color 0.12s, box-shadow 0.12s, transform 0.12s;
  position: relative;
}

.wp-row:hover {
  border-color: var(--border-strong);
}

.wp-row:active { cursor: grabbing; }
.wp-row.is-dragging { opacity: 0.35; }
.wp-row.is-dropzone {
  border-color: var(--accent);
  border-style: dashed;
  background: var(--accent-soft);
}

/* The Start / Return rows aren't draggable */
.wp-row.wp-row-start,
.wp-row.wp-row-return {
  cursor: default;
  background: var(--panel-soft);
}

.wp-row-num {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 9.5px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-display);
}

.wp-row-num-start {
  background: var(--bg-elev);
  color: var(--fg);
  border: 1.5px solid var(--accent);
}

.wp-row-body {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.wp-row-title {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--fg);
  line-height: 1.2;
}

.wp-row-coords {
  font-size: 9px;
  color: var(--muted);
  font-family: var(--font-mono);
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.wp-leg-stats {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 9.5px;
  margin-top: 0;
  color: var(--muted);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.wp-leg-dist {
  color: var(--fg-soft);
  font-weight: 500;
}

.wp-leg-climb {
  color: var(--muted);
}

.wp-leg-sep { color: var(--muted-soft); }

.wp-row-remove {
  flex-shrink: 0;
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
  transition: background-color 0.12s, border-color 0.12s, box-shadow 0.12s, transform 0.12s;
}
.wp-row-remove:hover {
  background: var(--accent-soft);
  color: var(--accent);
}

/* =====================================================
   Status & stats
   ===================================================== */
.status {
  font-size: 11px;
  line-height: 1.4;
  min-height: 1em;
  color: var(--muted);
}
.status[data-kind="info"] {
  color: var(--fg-soft);
}
.status[data-kind="info"]::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  margin-right: 6px;
  vertical-align: middle;
  animation: pulse 1.2s infinite;
}
.status[data-kind="error"] { color: var(--danger); }
@keyframes pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

.stats-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 9px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* When the panel scrolls, pin the route summary to the bottom so the
   KPIs + Download button stay reachable without scrolling. */
.stats-sticky {
  position: sticky;
  bottom: -1px;
  margin-top: auto;
  box-shadow: 0 -10px 16px -10px rgba(0, 0, 0, 0.10),
              0 1px 3px rgba(0, 0, 0, 0.04);
}

.stats-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.stats-header h2 { margin: 0; }
.stats-header .route-picker { margin: 0; }
.stats-header .route-picker:empty { display: none; }
.stats-header:has(> .route-picker:empty):not(:has(h2)) { display: none; }

/* Collapsible "Details" expander — visible button-style row */
.stats-details {
  margin: 0;
}
.stats-details summary {
  list-style: none;
  cursor: pointer;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 7px;
  padding: 7px 11px;
  font-size: 11.5px;
  font-weight: 600;
  color: var(--fg-soft);
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  user-select: none;
  transition: background-color 0.12s, border-color 0.12s, color 0.12s;
}
.stats-details summary::-webkit-details-marker { display: none; }
.stats-details summary:hover {
  border-color: var(--border-strong);
  color: var(--fg);
  background: var(--bg-elev);
}
.stats-details[open] summary {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  border-bottom-color: transparent;
}
.stats-details-ico {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
}
.stats-details-label {
  letter-spacing: 0.01em;
}
.stats-details-chev {
  display: inline-flex;
  transition: transform 0.18s ease;
  color: var(--muted);
}
.stats-details[open] .stats-details-chev {
  transform: rotate(180deg);
}
.stats-details-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
}

:root[data-theme="topo"] .stats-card {
  background: var(--panel-soft);
  border: 1px solid var(--border-strong);
}

.stats-card h2 {
  margin: 0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: var(--uppercase-tracking);
  text-transform: uppercase;
  color: var(--muted);
}

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
}

.kpi-grid[data-cols="2"] {
  grid-template-columns: 1fr 1fr;
}

.kpi {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 2px 0;
}

.kpi-label {
  font-size: 9.5px;
  letter-spacing: var(--uppercase-tracking);
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 600;
}

.kpi-value {
  font-family: var(--font-display);
  font-weight: var(--header-weight);
  font-size: 16px;
  line-height: 1.05;
  color: var(--fg);
  font-variant-numeric: tabular-nums;
  letter-spacing: var(--tracking-display);
}

:root[data-theme="topo"] .kpi-value {
  font-family: var(--font-display);
  font-size: 20px;
  letter-spacing: 0;
}

.kpi-hint {
  font-size: 9px;
  color: var(--muted-soft);
  font-family: var(--font-mono);
}

.stat-row[hidden] { display: none; }

/* Route picker (round-trip mode) */
.route-picker {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.route-pick {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg-elev);
  cursor: pointer;
  font-size: 12px;
  color: var(--fg-soft);
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}
.route-pick .swatch {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}
.route-pick.is-selected {
  border-color: currentColor;
  background: var(--bg-elev);
  font-weight: 600;
}

.route-picker[hidden] { display: none; }

/* Slider rows in stats (opacity / waypoint count) */
.opacity-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--fg-soft);
}
.opacity-row label {
  flex: 0 0 auto;
  min-width: 70px;
  color: var(--muted);
  font-size: 10px;
}
.opacity-row input[type="range"] { flex: 1; }
.opacity-row[hidden] { display: none; }

/* =====================================================
   Trail-composition breakdown (MTB only)
   ===================================================== */
.breakdown {
  margin: 0;
  padding: 8px 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 10.5px;
  color: var(--fg-soft);
}

:root[data-theme="topo"] .breakdown {
  background: var(--panel-soft);
}

.bd-section-title {
  display: grid;
  grid-template-columns: 1fr 2.4em 2.4em;
  gap: 6px;
  align-items: baseline;
  font-weight: 700;
  font-size: 10px;
  letter-spacing: var(--uppercase-tracking);
  text-transform: uppercase;
  color: var(--muted);
  margin: 10px 0 4px;
}
.bd-section-title:first-child { margin-top: 0; }
.bd-section-title.bd-section-title-single { display: block; }

.bd-row {
  display: grid;
  grid-template-columns: 1fr 2.4em 2.4em;
  gap: 6px;
  align-items: baseline;
  padding: 2px 0;
}
.bd-row.bd-row-single { grid-template-columns: 1fr auto; }

.bd-label { min-width: 0; color: var(--fg-soft); }
.bd-col {
  text-align: right;
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: var(--fg);
}
.bd-col-head {
  color: var(--muted-soft);
  font-weight: 400;
}

.breakdown[hidden] { display: none; }

/* =====================================================
   Start readout (footer)
   ===================================================== */
.readout {
  font-family: var(--font-mono);
  font-size: 9.5px;
  color: var(--muted);
  letter-spacing: -0.01em;
  padding-top: 2px;
}

.start-hint {
  font-size: 10px;
  color: var(--muted);
  line-height: 1.3;
  margin-top: 2px;
  opacity: 0.85;
}
.start-hint[hidden] { display: none; }

.panel-footer {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  text-align: center;
}
.kofi-link {
  display: inline-block;
  font-size: 11px;
  color: var(--muted);
  text-decoration: none;
  padding: 4px 8px;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
.kofi-link:hover,
.kofi-link:focus-visible {
  color: var(--accent);
  background: var(--bg-elev);
}

/* =====================================================
   Map controls overlay (top-right) — compact
   ===================================================== */
.map-controls {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 450;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: flex-end;
}

.basemap-picker {
  display: flex;
  background: var(--panel);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px;
  box-shadow: var(--shadow);
  gap: 0;
}

.basemap-picker .bm-btn {
  appearance: none;
  border: none;
  background: transparent;
  padding: 5px 9px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-radius: calc(var(--radius-sm) - 2px);
  font-family: inherit;
  white-space: nowrap;
}

.basemap-picker .bm-btn.is-active {
  background: var(--accent);
  color: var(--accent-fg);
  box-shadow: var(--shadow-sm);
}

.basemap-picker .bm-btn:hover:not(.is-active) {
  color: var(--fg);
}

/* The original <select id="basemap"> is hidden — the segmented picker
   above mirrors its value. */
#basemap { display: none; }

/* =====================================================
   Route widget — floating bottom-right (elevation + stats)
   ===================================================== */
.route-widget {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 450;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 340px;
  max-width: calc(100vw - 24px);
}

:root[data-layout="sidebar"] .route-widget {
  max-width: calc(100vw - 320px - 24px);
}

/* Elevation profile (lives inside .route-widget) */
#elevation {
  position: relative;
  right: auto;
  bottom: auto;
  width: 100%;
  height: 90px;
  background: var(--panel);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  overflow: hidden;
}

#elevation[hidden] { display: none; }
#elevation svg { display: block; width: 100%; height: 100%; }

/* Stats card (lives inside .route-widget — sits under the elevation chart) */
.route-widget #stats {
  background: var(--panel);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  padding: 9px 11px;
  gap: 7px;
}

.route-widget #stats[hidden] { display: none; }

/* =====================================================
   Map-injected divIcon styling (markers + arrows)
   ===================================================== */
.route-arrow { background: transparent; border: none; pointer-events: none; }

.wp-marker, .wp-num-marker {
  background: transparent;
  border: none;
  cursor: grab;
}
.wp-marker:active, .wp-num-marker:active { cursor: grabbing; }

/* Start marker — classic teardrop pin (SVG divIcon) filled with the
   theme accent and a white center dot. */
.start-marker {
  background: transparent;
  border: none;
  cursor: grab;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.28));
}
.start-marker:active { cursor: grabbing; }
.start-marker svg { display: block; }

.start-tooltip {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
  font-family: var(--font-sans);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
}
.start-tooltip::before { display: none; }

.wp-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}
.wp-inner {
  width: 6px;
  height: 6px;
  background: white;
  border-radius: 50%;
}

.wp-num-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 12px;
  font-weight: 700;
  border: 2.5px solid white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-display);
}

#map.waypoint-mode-active { cursor: crosshair; }
#map.waypoint-mode-active .leaflet-interactive { cursor: crosshair; }

/* Leaflet attribution lives at bottom-right; respect it visually */
.leaflet-control-attribution {
  font-size: 10px !important;
  font-family: var(--font-mono) !important;
  background: rgba(255, 255, 255, 0.85) !important;
  padding: 2px 6px !important;
}

/* =====================================================
   Tweaks panel
   ===================================================== */
.tweaks-panel {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  background: var(--panel);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 320px;
}

.tweaks-panel[hidden] { display: none !important; }

.tweaks-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.tweaks-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: var(--uppercase-tracking);
  text-transform: uppercase;
  color: var(--muted);
}

.tweaks-close {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
  width: 24px;
  height: 24px;
  border-radius: 6px;
}
.tweaks-close:hover { background: var(--panel-soft); color: var(--fg); }

.tweaks-grid {
  display: flex;
  gap: 14px;
}

.tweak-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tweak-group-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: var(--uppercase-tracking);
  text-transform: uppercase;
  color: var(--muted);
}

.tweak-radio {
  display: flex;
  gap: 4px;
  background: var(--panel-soft);
  border-radius: 8px;
  padding: 3px;
}

.tweak-radio button {
  flex: 1;
  appearance: none;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}

.tweak-radio button.is-active {
  background: var(--accent);
  color: var(--accent-fg);
}

/* =====================================================
   Misc utility
   ===================================================== */
[hidden] { display: none !important; }

.muted-row {
  font-size: 11px;
  color: var(--muted);
  font-family: var(--font-mono);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* =====================================================
   Responsive (mobile)
   ===================================================== */
@media (max-width: 760px) {
  /* Stack panel + map vertically on phones, regardless of layout pref.
     We need to beat the floating-layout's position:fixed on the map and
     panel, so the selectors below match layout-scoped specificity. */
  :root[data-layout] .app,
  :root .app {
    display: flex !important;
    flex-direction: column;
    width: 100%;
    height: 100vh;
  }

  :root[data-layout] #map,
  :root #map {
    position: relative !important;
    inset: auto !important;
    width: 100% !important;
    height: 60vh !important;
    flex: 0 0 60vh !important;
    order: 1;
  }

  :root[data-layout] .panel,
  :root .panel {
    position: relative !important;
    inset: auto !important;
    top: auto !important;
    left: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: 40vh !important;
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 8px 10px 12px;
    order: 3;
    flex: 1 1 auto !important;
    background: var(--panel);
    border-top: 1px solid var(--border) !important;
    z-index: 1 !important;
  }

  /* Map controls — keep top-right, smaller */
  .map-controls {
    top: 8px;
    right: 8px;
    gap: 5px;
  }

  /* Leaflet zoom back to default top-left on mobile */
  :root[data-layout="floating"] .leaflet-top.leaflet-left,
  .leaflet-top.leaflet-left {
    left: 8px !important;
    right: auto !important;
    top: 8px !important;
  }

  /* Route widget — docked in normal flow between the map and panel.
     Elevation is hidden by default and revealed when the details row is
     opened. Stats are laid out as a compact 2-col grid: KPIs left,
     Download right, Details below spanning full width. */
  .route-widget {
    position: relative !important;
    right: auto !important;
    left: auto !important;
    bottom: auto !important;
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    z-index: 1;
    order: 2;
    flex: 0 0 auto !important;
    gap: 0 !important;
  }

  #elevation { display: none !important; }

  .route-widget:has(.stats-details[open]) #elevation {
    display: block !important;
    height: 80px !important;
    border: none !important;
    border-top: 1px solid var(--border) !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    background: var(--bg-elev) !important;
  }

  .route-widget #stats {
    display: grid !important;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    column-gap: 8px;
    row-gap: 0;
    border: none !important;
    border-top: 1px solid var(--border) !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    background: var(--bg-elev) !important;
    padding: 0;
  }

  .route-widget .stats-header { display: none; }

  .route-widget .kpi-grid {
    grid-column: 1;
    grid-row: 1;
    display: flex !important;
    flex: 1 1 auto;
    gap: 12px;
    padding: 7px 0 7px 10px;
    align-items: baseline;
    min-width: 0;
  }
  .route-widget .kpi {
    flex-direction: row !important;
    align-items: baseline;
    gap: 5px;
    padding: 0 !important;
  }
  .route-widget .kpi-label { font-size: 9px; color: var(--muted); }
  .route-widget .kpi-value { font-size: 13px !important; line-height: 1; }
  .route-widget .kpi-hint { display: none; }

  .route-widget #download {
    grid-column: 2;
    grid-row: 1;
    align-self: center;
    margin: 5px 10px 5px 0;
    padding: 7px 11px !important;
    font-size: 12px !important;
    width: auto !important;
  }
  .route-widget #download svg { width: 13px; height: 13px; }

  .route-widget .stats-details {
    grid-column: 1 / -1;
    grid-row: 2;
    display: block !important;
    margin: 0;
  }
  .route-widget .stats-details summary {
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
    padding: 6px 10px;
  }
  .route-widget .stats-details[open] summary {
    border-bottom: 1px solid var(--border);
  }
  .route-widget .stats-details-body {
    border-left: none;
    border-right: none;
    border-radius: 0;
    padding: 8px 10px 10px;
  }

  /* Mobile-friendly hit targets */
  input[type="range"] { height: 28px; }
  input[type="range"]::-webkit-slider-thumb {
    width: 18px; height: 18px; margin-top: -7.5px;
  }
  input[type="range"]::-moz-range-thumb {
    width: 18px; height: 18px;
  }
  .return-toggle { padding: 8px 10px; }
  .return-toggle input[type="checkbox"] {
    width: 36px; height: 20px; border-radius: 10px;
  }
  .return-toggle input[type="checkbox"]::after {
    width: 16px; height: 16px;
  }
  .return-toggle input[type="checkbox"]:checked::after {
    transform: translateX(16px);
  }
  .pref-option input[type="checkbox"] {
    width: 18px; height: 18px;
  }
  .pref-option input[type="checkbox"]:checked::after {
    left: 5px; top: 2px;
  }
  .btn { padding: 9px 12px; font-size: 13px; }
  .wp-row { padding: 8px 10px; }
  .wp-row-num { width: 22px; height: 22px; font-size: 11px; }
  .segmented label { padding: 7px 6px; font-size: 13px; }

  /* Tweaks panel — full width at the very bottom */
  .tweaks-panel {
    bottom: 8px;
    left: 8px;
    right: 8px;
    transform: none;
    width: calc(100% - 16px);
    min-width: 0;
  }
}

/* Extra-small phones — give the map a touch less room so the docked
   widget + panel both stay tappable. */
@media (max-width: 380px) {
  .basemap-picker .bm-btn { padding: 4px 7px; font-size: 10px; }
  :root[data-layout] #map,
  :root #map { height: 58vh !important; flex: 0 0 58vh !important; }
  :root[data-layout] .panel,
  :root .panel { max-height: 42vh !important; }
  .route-widget .kpi-grid { gap: 8px; }
  .route-widget .kpi-value { font-size: 12px !important; }
}
