/* Dispatch Board — Map tab (js/map-view.js). Every class here starts with .map- so nothing reaches the rest of the board.
   Leaflet's own stylesheet is added to <head> when the tab first opens, so it wins ties: the overrides below use
   .map-canvas … (two classes) to stay ahead of it. */

.map-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); height: clamp(520px, calc(100vh - 170px), 1000px); }
.map-side { display: flex; flex-direction: column; min-width: 0; border-right: 1px solid var(--border); background: var(--surface-2); }
.map-side-head { padding: 12px; border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 9px; }
.map-find { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--ctl-radius, 10px); padding: 7px 10px; color: var(--ink-faint); }
.map-find input { border: none; background: transparent; color: var(--ink); font: inherit; font-size: 13px; width: 100%; outline: none; }
.map-find:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.map-layer-row { display: flex; flex-wrap: wrap; gap: 4px 10px; }
.map-layer { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-muted); cursor: pointer; }
.map-layer input { margin: 0; }

.map-side-body { flex: 1; overflow-y: auto; padding: 10px 10px 16px; }
.map-side-foot { padding: 8px 12px; border-top: 1px solid var(--border); }
.map-side-foot p { margin: 0; }
.map-group + .map-group { margin-top: 14px; }
.map-group-h { display: flex; align-items: center; gap: 7px; font-size: 12px; font-weight: 700; color: var(--ink-faint); margin: 2px 2px 8px; }
.map-empty { margin: 2px 2px 6px; }
.map-dim { color: var(--ink-faint); }

/* Unassigned load cards: the things you drag onto a truck. */
.map-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; margin-bottom: 7px; cursor: grab; box-shadow: var(--shadow); transition: border-color .12s ease, transform .08s ease; }
.map-card:hover { border-color: var(--border-strong); transform: translateY(-1px); }
.map-card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.map-card.sel { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.map-card.dragging { opacity: 0.5; cursor: grabbing; }
.map-card-top { display: flex; align-items: center; gap: 5px; flex-wrap: wrap; margin-bottom: 2px; }
.map-card-cust { font-weight: 600; font-size: var(--fs-md); color: var(--ink); }
.map-card-id { font-size: var(--fs-sm); color: var(--ink-muted); }
.map-card-route { font-size: 12px; color: var(--ink); margin-top: 3px; }
.map-card-meta { font-size: var(--fs-sm); color: var(--ink-faint); margin-top: 2px; }
.map-card-acts { display: flex; gap: 6px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.map-card-acts .assign-select { border: 1px solid var(--border-strong); border-radius: 999px; padding: 4px 8px; font: inherit; font-size: 12px; background: var(--surface-2); color: var(--ink); max-width: 100%; }

/* Driver rows: the drop target for people who'd rather not aim at a moving truck. */
.map-driver { display: flex; align-items: flex-start; gap: 8px; padding: 7px 9px; border: 1px solid transparent; border-radius: 9px; cursor: pointer; }
.map-driver:hover { background: var(--surface); border-color: var(--border); }
.map-driver:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.map-driver.sel { background: var(--surface); border-color: var(--accent); }
.map-driver.drop-target { background: var(--accent-soft); border: 1px dashed var(--accent); }
.map-driver .dot { margin-top: 5px; }
.map-driver .dot.stale { opacity: 0.45; }
.map-driver-main { min-width: 0; }
.map-driver-name { font-size: var(--fs-md); font-weight: 600; color: var(--ink); }
.map-driver-sub { font-size: var(--fs-sm); color: var(--ink-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* The map itself. */
/* overflow:hidden keeps the map tiles, which are laid out well past the edges, from widening the page. */
.map-main { position: relative; min-width: 0; overflow: hidden; isolation: isolate; } /* Leaflet's z-indexes (400–1000) stay inside the map, under drawers and toasts */
.map-canvas { position: absolute; inset: 0; background: var(--surface-3); }
.map-canvas:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.map-canvas.leaflet-container { font-family: "Public Sans", -apple-system, "Segoe UI", sans-serif; font-size: 12px; background: var(--surface-3); }
.map-canvas .leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 82%, transparent); color: var(--ink-faint); font-size: 10px; }
.map-canvas .leaflet-control-attribution a { color: var(--ink-muted); }
.map-canvas .leaflet-bar a { background: var(--surface); color: var(--ink); border-bottom-color: var(--border); }
.map-canvas .leaflet-bar a:hover { background: var(--surface-2); color: var(--ink); }
.map-canvas .leaflet-popup-content-wrapper, .map-canvas .leaflet-popup-tip { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-pop, var(--shadow)); }
.map-canvas .leaflet-popup-content { margin: 10px 12px; line-height: 1.4; }
.map-canvas .leaflet-tooltip { background: var(--surface); color: var(--ink); border: 1px solid var(--border); box-shadow: var(--shadow); font-size: var(--fs-sm); }
.map-canvas .leaflet-tooltip-top::before { border-top-color: var(--border); }
.map-canvas .map-icon { background: none; border: none; }

.map-tools { position: absolute; top: 10px; right: 10px; z-index: 500; display: flex; gap: 6px; }
/* These sit on top of the map, so they need a solid background of their own whatever the button style is. */
.map-tools .btn { background: var(--surface); color: var(--ink); border: 1px solid var(--border-strong); box-shadow: var(--shadow); }
.map-tools .btn:hover { background: var(--surface-2); }
.map-note { position: absolute; top: 10px; left: 52px; right: 10px; z-index: 500; max-width: 420px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 12px; }
.map-note-h { font-family: var(--font-head); font-weight: 650; font-size: var(--fs-lg); margin-bottom: 2px; }
.map-note p { margin: 0; font-size: 12px; color: var(--ink-muted); }
.map-legend { position: absolute; left: 10px; bottom: 16px; z-index: 500; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); font-size: var(--fs-sm); max-width: calc(100% - 20px); }
.map-legend summary { padding: 5px 10px; cursor: pointer; color: var(--ink-muted); font-weight: 600; }
.map-legend-body { display: flex; flex-wrap: wrap; gap: 4px 12px; padding: 0 10px 8px; color: var(--ink-muted); }
.map-legend-body span { display: inline-flex; align-items: center; gap: 5px; }
.map-key { width: 10px; height: 10px; border-radius: 50%; flex: none; display: inline-block; }
.map-key.s-available { background: var(--status-available); }
.map-key.s-transit { background: var(--status-transit); }
.map-key.s-risk { background: var(--status-risk); }
.map-key.s-offduty { background: var(--status-offduty); }
.map-key.s-stale { background: var(--status-offduty); opacity: 0.4; }
.map-key.k-open { background: var(--surface); border: 2px solid var(--accent); }
.map-key.k-assigned { background: var(--status-transit); }
.map-key.k-site { border-radius: 3px; background: var(--surface-3); border: 1px solid var(--border-strong); }

/* Markers. */
.map-truck { position: relative; width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; }
.map-truck-pin { width: 26px; height: 26px; border-radius: 50%; display: flex; align-items: center; justify-content: center; color: var(--on-status); background: var(--status-offduty); border: 2px solid var(--surface); box-shadow: 0 1px 4px rgba(16, 24, 40, 0.45); transition: transform .12s ease; }
.map-truck.s-available .map-truck-pin { background: var(--status-available); }
.map-truck.s-transit .map-truck-pin { background: var(--status-transit); }
.map-truck.s-risk .map-truck-pin { background: var(--status-risk); }
.map-truck.s-offduty .map-truck-pin { background: var(--status-offduty); }
.map-truck.stale .map-truck-pin { background: var(--status-offduty); opacity: 0.55; border-style: dashed; }
.map-truck.moving .map-truck-pin { box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 28%, transparent), 0 1px 4px rgba(16, 24, 40, 0.45); }
.map-truck.sel .map-truck-pin { transform: scale(1.15); box-shadow: 0 0 0 4px var(--accent-soft), 0 1px 4px rgba(16, 24, 40, 0.45); }
.map-truck.drop-target .map-truck-pin { transform: scale(1.35); box-shadow: 0 0 0 6px var(--accent-soft), 0 1px 4px rgba(16, 24, 40, 0.45); }
.map-truck-tag { position: absolute; left: 32px; top: 50%; transform: translateY(-50%); white-space: nowrap; font-size: 11px; font-weight: 600; color: var(--ink); background: color-mix(in srgb, var(--surface) 88%, transparent); border-radius: 5px; padding: 1px 5px; pointer-events: none; }
/* While a load is being dragged, trucks are easier to hit and easier to see. */
.map-canvas.map-dropping .map-truck-pin { transform: scale(1.2); }
.map-canvas.map-dropping .map-stop, .map-canvas.map-dropping .map-site { opacity: 0.45; }

.map-stop { width: 22px; height: 22px; border-radius: 50%; background: var(--status-transit); border: 2px solid var(--surface); color: var(--on-status); font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; box-shadow: 0 1px 3px rgba(16, 24, 40, 0.4); }
.map-stop.open { background: var(--surface); border: 2px solid var(--accent); color: var(--accent); }
.map-stop.late { background: var(--status-critical); }
.map-stop.sel { box-shadow: 0 0 0 4px var(--accent-soft), 0 1px 3px rgba(16, 24, 40, 0.4); }
.map-seq { width: 24px; height: 24px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); color: var(--accent); font-size: 11px; font-weight: 700; line-height: 20px; text-align: center; box-shadow: 0 1px 3px rgba(16, 24, 40, 0.35); }
.map-seq.done { background: var(--status-available); border-color: var(--status-available); color: var(--on-status); }
.map-site { width: 20px; height: 20px; border-radius: 5px; background: var(--surface-3); border: 1px solid var(--border-strong); color: var(--ink-muted); font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }
.map-site.k-yard { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.map-canvas .map-route { stroke: var(--ink-faint); }
.map-canvas .map-route-live { stroke: var(--accent); }

/* Popups. */
.map-pop-h { font-family: var(--font-head); font-weight: 650; font-size: var(--fs-lg); color: var(--ink); }
.map-pop-sub { font-size: var(--fs-sm); color: var(--ink-faint); margin-bottom: 4px; }
.map-pop-line { font-size: 12px; color: var(--ink-muted); margin-top: 2px; }
.map-pop-muted { color: var(--ink-faint); font-style: italic; }
.map-pop-acts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.map-pop-list { list-style: none; margin: 4px 0 0; padding: 0; max-height: 190px; overflow-y: auto; }
.map-pop-list li { display: flex; align-items: center; gap: 6px; padding: 3px 0; border-top: 1px solid var(--border); font-size: var(--fs-sm); color: var(--ink-muted); }
.map-pop-list li span { flex: 1; min-width: 0; }

/* Dark mode: the OpenStreetMap tiles are the only part that can't follow the theme's colours, so they're inverted. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .map-canvas .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.9); }
}
:root[data-theme="dark"] .map-canvas .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.9); }

/* The pop-out window (index.html?map=1): the board with only the map on screen. */
.map-popout .app { max-width: none; padding: 0; }
.map-popout #appRoot > *:not(#view-map):not(#storageWarning) { display: none !important; }
.map-popout #view-map { margin: 0; border: none; border-radius: 0; }
.map-popout .map-layout { height: 100vh; }

@media (max-width: 860px) {
  .map-layout { grid-template-columns: 250px minmax(0, 1fr); }
  .map-truck-tag { display: none; }
}
@media (max-width: 720px) {
  .map-layout { grid-template-columns: minmax(0, 1fr); height: auto; }
  .map-side { border-right: none; border-bottom: 1px solid var(--border); }
  .map-side-body { max-height: 320px; }
  .map-main { height: 460px; }
}
