/* ── Route maps + optimizer (assets/js/route-map.js, route-optimize.js) ──────
   Colours come from the app's tokens so every palette, light or dark, works.
   A route's own colour arrives as the inline custom property --rc. */

.rmap {
  position: relative;
  isolation: isolate; /* keep Leaflet panes (z up to 1000) inside the map so the toggles popover can float over it */
  width: 100%;
  min-height: 240px;
  height: 300px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md, 10px);
  background: var(--surface-chip, var(--card));
  overflow: hidden;
  z-index: 0;               /* keep Leaflet's panes under app overlays */
}
.rmap.rmap-offline { display: flex; align-items: center; justify-content: center; height: auto; }
.rmap-offline-note { color: var(--text-muted); font-size: var(--fs-sm); padding: var(--space-4, 16px); text-align: center; }

.rmap-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg, 14px);
  padding: var(--space-3, 12px);
  display: flex; flex-direction: column; gap: var(--space-2, 8px);
  min-width: 0;
}

/* Markers (L.divIcon). Leaflet's default .leaflet-div-icon paints a white box. */
.rmap-divicon { background: none; border: 0; }
.rmap-marker {
  width: 26px; height: 26px; box-sizing: border-box;
  border-radius: 50%;
  background: var(--rc, var(--accent));
  color: var(--ri, #fff);
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
  display: flex; align-items: center; justify-content: center;
  font: 700 12px/1 var(--font-family, system-ui, sans-serif);
  font-variant-numeric: tabular-nums;
}
.rmap-marker.done {
  background: #d4d6db;
  color: #111827; /* fixed dark ink: a light route colour on this grey is unreadable */
  border-color: var(--rc, var(--accent));
}
.rmap-endpoint {
  width: 14px; height: 14px; box-sizing: border-box;
  border-radius: 3px;
  background: var(--rc, var(--accent));
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
}

/* Popups follow the theme instead of Leaflet's fixed white. */
.rmap .leaflet-popup-content-wrapper,
.rmap .leaflet-popup-tip { background: var(--card); color: var(--text); }
.rmap .leaflet-popup-content { margin: 10px 12px; }
.rmap .leaflet-container a.leaflet-popup-close-button { color: var(--text-muted); }
.rmap-pop { display: flex; flex-direction: column; gap: 4px; font-size: var(--fs-sm); min-width: 160px; }
.rmap-pop-title { font-weight: 700; color: var(--text); }
.rmap-pop-addr  { color: var(--text-muted); }
.rmap-pop-meta  { color: var(--text-muted); display: flex; align-items: center; gap: 6px; }
.rmap-pop-open  { align-self: flex-start; margin-top: 4px; }

.rmap-swatch {
  display: inline-block; flex: none;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--rc, var(--accent));
  border: 1px solid rgba(0, 0, 0, .15);
}

/* Route / tech visibility: a one-line button opening a popover of toggles
   (mountToggles). Never wraps, never scrolls sideways. */
.rmap-toggles { display: flex; min-width: 0; }
.rmap-toggles:empty { display: none; }
.rmap-tg { position: relative; min-width: 0; max-width: 100%; }
.rmap-tg-btn {
  display: inline-flex; align-items: center; gap: 8px;
  max-width: 100%; min-height: 40px; padding: 6px 12px;
  border-radius: var(--radius-pill, 999px);
  border: 1px solid var(--border);
  background: var(--card); color: var(--text);
  font: inherit; font-size: var(--fs-sm); font-weight: 600;
  cursor: pointer; white-space: nowrap;
}
.rmap-tg-btn:focus-visible, .rmap-tg-item:focus-visible, .rmap-tg-all:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.rmap-tg-dots { display: inline-flex; flex: none; }
.rmap-tg-dots .rmap-swatch { width: 10px; height: 10px; border: 2px solid var(--card); box-sizing: content-box; }
.rmap-tg-dots .rmap-swatch + .rmap-swatch { margin-left: -5px; }
.rmap-tg-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.rmap-tg-caret {
  flex: none; width: 7px; height: 7px; margin: -3px 2px 0;
  border-right: 2px solid var(--text-muted); border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg);
}
.rmap-tg-btn[aria-expanded="true"] .rmap-tg-caret { transform: rotate(-135deg); margin-top: 3px; }
.rmap-tg-menu {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: var(--z-popover, 300);
  min-width: 200px; max-width: min(320px, calc(100vw - 32px));
  max-height: min(60vh, 340px); overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px;
  padding: 6px;
  background: var(--card); color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md, 10px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .18);
}
.rmap-tg-menu[hidden] { display: none; }
.rmap-tg-menu .rmap-chip {
  display: flex; align-items: center; gap: 10px; width: 100%;
  min-height: 40px; padding: 6px 8px;
  border: 0; border-radius: var(--radius-sm, 6px); box-shadow: none;
  background: transparent; color: var(--text); opacity: 1;
  font: inherit; font-size: var(--fs-sm); text-align: left; cursor: pointer;
}
.rmap-tg-menu .rmap-chip:hover { background: var(--surface-chip, var(--bg)); }
.rmap-tg-menu .rmap-chip[aria-pressed="false"] { color: var(--text-muted); }
.rmap-tg-menu .rmap-chip[aria-pressed="false"] .rmap-swatch { background: transparent; border-color: var(--rc, var(--accent)); }
.rmap-tg-check {
  flex: none; position: relative;
  width: 18px; height: 18px; box-sizing: border-box;
  border: 2px solid var(--border); border-radius: 4px;
}
.rmap-chip[aria-pressed="true"] .rmap-tg-check { background: var(--accent); border-color: var(--accent); }
.rmap-chip[aria-pressed="true"] .rmap-tg-check::after {
  content: ''; position: absolute; left: 4px; top: 0;
  width: 5px; height: 10px;
  border-right: 2px solid var(--accent-ink, #fff); border-bottom: 2px solid var(--accent-ink, #fff);
  transform: rotate(45deg);
}
.rmap-tg-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rmap-tg-foot { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 4px; }
.rmap-tg-all {
  width: 100%; min-height: 40px; padding: 6px 8px;
  border: 0; background: transparent; color: var(--accent-text, var(--accent));
  font: inherit; font-size: var(--fs-sm); font-weight: 600; text-align: left; cursor: pointer;
  border-radius: var(--radius-sm, 6px);
}

/* Map card head: title + Today/Tomorrow switch (dashboard) */
.rmap-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; }
.rmap-head-title { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rmap-seg {
  display: inline-flex; flex: none;
  border: 1px solid var(--border); border-radius: var(--radius-pill, 999px);
  padding: 2px; background: var(--surface-chip, var(--bg));
}
.rmap-seg button {
  min-height: 40px; padding: 4px 14px;
  border: 0; border-radius: var(--radius-pill, 999px);
  background: transparent; color: var(--text-muted);
  font: inherit; font-size: var(--fs-sm); font-weight: 600; cursor: pointer; white-space: nowrap;
}
.rmap-seg button[aria-pressed="true"] { background: var(--card); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, .15); }
.rmap-seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.rmap-status { font-size: var(--fs-sm); color: var(--text-muted); min-height: 0; }
.rmap-status:empty { display: none; }
.rmap-status.err { color: var(--danger, var(--warning)); }

/* Stop clusters (Leaflet.markercluster): a QUANTITY, styled unlike a single
   numbered stop — bigger white disc, thick route-colour ring, "×N" in dark
   ink. Fixed white/ink, not theme tokens: it sits on map tiles, which do not
   follow the app theme, and dark-on-white passes contrast for every tech colour. */
.rmap-cluster {
  width: 40px; height: 40px; box-sizing: border-box;
  border-radius: 50%;
  background: #fff;
  color: #111827;
  border: 5px solid var(--rc, var(--accent));
  box-shadow: 0 0 0 1.5px #fff, 0 2px 5px rgba(0, 0, 0, .35);
  display: flex; align-items: center; justify-content: center;
  font: 800 13px/1 var(--font-family, system-ui, sans-serif);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.rmap-cluster.done { background: #d4d6db; }

/* Popup links (customer / property) */
.rmap-pop-link {
  display: inline; padding: 0; margin: 0;
  border: 0; background: none;
  font: inherit; color: var(--accent-text, var(--accent)); text-align: left;
  text-decoration: underline; text-underline-offset: 2px; cursor: pointer;
}
.rmap-pop-addr .rmap-pop-link { color: var(--text-muted); text-decoration-color: var(--border); }
.rmap-pop-link:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Stops that could not be plotted */
.rmap-flags {
  border: 1px solid var(--warning-edge, var(--border));
  background: var(--warning-light, var(--card));
  color: var(--text);
  border-radius: var(--radius-md, 10px);
  padding: 8px 10px;
  font-size: var(--fs-sm);
}
.rmap-flags-head { font-weight: 700; margin-bottom: 4px; }
.rmap-flags ul { margin: 0; padding-left: 0; list-style: none; display: flex; flex-direction: column; gap: 2px; }
.rmap-flags-num { display: inline-block; min-width: 1.6em; font-weight: 700; font-variant-numeric: tabular-nums; }
.rmap-flags-why { color: var(--text-muted); }

/* Shared bits for the modals */
.rmap-muted { color: var(--text-muted); }
.rmap-warn  { color: var(--warning); }
.rmap-help  { color: var(--text-muted); font-size: var(--fs-sm); margin: 0 0 12px; line-height: 1.5; }

/* Start / end editor */
.rmap-ep { border-top: 1px solid var(--border); padding: 12px 0; display: flex; flex-direction: column; gap: 6px; }
.rmap-ep-title { font-weight: 700; color: var(--text); margin-top: 6px; }
.rmap-ep-current { font-size: var(--fs-sm); color: var(--text); overflow-wrap: anywhere; }
.rmap-ep-row { display: flex; gap: 6px; min-width: 0; }
.rmap-ep-row .form-input { flex: 1 1 auto; min-width: 0; }
.rmap-ep-result { font-size: var(--fs-sm); color: var(--text-muted); overflow-wrap: anywhere; }
.rmap-ep-result:empty { display: none; }
.rmap-ep-clear { align-self: flex-start; }

/* Saved places (manager + the Optimize window's Start / End bar) */
.rmap-pl-list { display: flex; flex-direction: column; }
.rmap-pl-row { display: flex; align-items: center; gap: 6px; min-height: 44px; padding: 6px 0; border-top: 1px solid var(--border); min-width: 0; }
.rmap-pl-text { flex: 1 1 auto; min-width: 0; }
.rmap-pl-text .form-input { width: 100%; }
.rmap-pl-label { font-weight: 600; color: var(--text); overflow-wrap: anywhere; }
.rmap-pl-addr { font-size: var(--fs-sm); color: var(--text-muted); overflow-wrap: anywhere; }
.rmap-pl-empty { font-size: var(--fs-sm); margin: 0 0 8px; }
.rmap-pl-pickrow { display: flex; gap: 10px; min-width: 0; }
.rmap-pl-pickrow label { display: flex; flex-direction: column; gap: 4px; flex: 1 1 0; min-width: 0; font-size: var(--fs-sm); color: var(--text-muted); }
.rmap-pl-pickrow select { width: 100%; min-height: 44px; }
.rmap-pv-ends { display: flex; flex-direction: column; gap: 8px; padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px solid var(--border); }
.rmap-pv-newplace { display: flex; flex-direction: column; gap: 6px; }
.rmap-pv-newplace[hidden] { display: none; }
/* Narrow phones: the address gets its own line instead of shrinking beside Add / Cancel. */
.rmap-pv-newplace .rmap-ep-row { flex-wrap: wrap; }
.rmap-pv-newplace .rmap-ep-addr { flex: 1 1 220px; }
.rmap-pv-keep { min-height: 36px; }

/* Anchor editor */
.rmap-anchor { display: flex; flex-direction: column; gap: 6px; }
.rmap-radio { display: flex; align-items: center; gap: 8px; min-height: 36px; color: var(--text); cursor: pointer; }
.rmap-window { display: flex; gap: 10px; flex-wrap: wrap; }
.rmap-window label { display: flex; flex-direction: column; gap: 4px; font-size: var(--fs-sm); color: var(--text-muted); flex: 1 1 120px; min-width: 0; }

/* Optimize preview */
.rmap-preview { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.rmap-preview .rmap { height: 260px; }
.rmap-pv-total { font-size: var(--fs-md, 16px); color: var(--text); }
.rmap-pv-legend { display: flex; align-items: center; gap: 6px; font-size: var(--fs-xs, 12px); color: var(--text-muted); }
.rmap-pv-ghost, .rmap-pv-solid { display: inline-block; width: 22px; height: 0; border-top: 3px dashed #8a8f98; }
.rmap-pv-solid { border-top: 4px solid var(--accent); margin-left: 8px; }
.rmap-pv-route {
  border: 1px solid var(--border); border-radius: var(--radius-md, 10px);
  padding: 10px; display: flex; flex-direction: column; gap: 6px; min-width: 0;
  background: var(--card);
}
.rmap-pv-off { opacity: .75; }
.rmap-pv-head { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); min-width: 0; }
.rmap-pv-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rmap-pv-status { font-size: var(--fs-sm); color: var(--text); }
.rmap-pv-warn { margin: 0; padding-left: 18px; font-size: var(--fs-sm); color: var(--warning); }
.rmap-pv-orders > summary { cursor: pointer; font-size: var(--fs-sm); color: var(--text-muted); min-height: 32px; display: flex; align-items: center; }
.rmap-pv-cols { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }
.rmap-pv-colhead { font-size: var(--fs-xs, 12px); text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin-bottom: 4px; }
.rmap-order { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; font-size: var(--fs-sm); }
.rmap-order li { display: flex; gap: 6px; min-width: 0; }
.rmap-order-num { flex: none; min-width: 1.6em; text-align: right; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.rmap-order-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.rmap-pv-extra { display: flex; align-items: center; gap: 8px; font-size: var(--fs-sm); color: var(--text); }
.rmap-pv-mapcol, .rmap-pv-side { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.rmap-pv-mapcol { gap: 6px; }
/* After list: a stop whose position changed is tinted, with its old number. */
.rmap-order li.rmap-pv-moved { background: color-mix(in srgb, var(--accent) 12%, transparent); border-radius: 4px; }
.rmap-order li.rmap-pv-moved .rmap-order-num { color: var(--text); font-weight: 700; }
.rmap-pv-was { flex: none; margin-left: auto; padding-left: 4px; font-size: var(--fs-xs, 12px); color: var(--text-muted); white-space: nowrap; }
/* Desktop: wider modal, map on the left at full height, lists scrolling on the right. */
@media (min-width: 1024px) {
  .modal-box:has(> .rmap-preview) { max-width: min(1100px, calc(100vw - 48px)); }
  .rmap-preview {
    display: grid; grid-template-columns: minmax(0, 3fr) minmax(300px, 2fr);
    grid-template-rows: auto minmax(0, 1fr); grid-template-areas: "map total" "map side";
    column-gap: 16px; row-gap: 0; height: min(70vh, 680px);
  }
  .rmap-pv-total { grid-area: total; margin-bottom: 10px; }
  .rmap-pv-mapcol { grid-area: map; min-height: 0; }
  .rmap-preview .rmap.rmap-pv-map { height: auto; flex: 1 1 auto; min-height: 0; }
  .rmap-pv-side { grid-area: side; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding-right: 2px; }
}

@media (max-width: 380px) {
  .rmap { height: 260px; }
  .rmap-preview .rmap { height: 220px; }
}

/* Dashboard map while a Today↔Tomorrow switch loads: the old day stays
   visible but dimmed and inert, so a stale stop can't be opened. */
.rmap-loading { opacity: .5; pointer-events: none; transition: opacity .15s; }
