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

/* Visually hidden but accessible to screen readers and search engines */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  white-space: nowrap;
  border: 0;
}

body {
  background: #0d1117;
  color: #c9d1d9;
  font-family: 'Georgia', serif;
  height: 100vh;
  overflow: hidden;
}

/* ── World view ──────────────────────────────────────────────────────────── */
#world-view {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  /* Anchor map at top-left; layoutWorldMap() centres it via initial pan.
     Keeping the same anchor on every viewport makes the pinch/wheel math
     identical (transform-origin: 0 0 of #map-container) on phone and
     desktop alike. */
  align-items: flex-start;
  justify-content: flex-start;
  background: #0d1117;
  overflow: hidden;
}

#map-container {
  position: relative;
  flex-shrink: 0;
  transform-origin: 0 0;
  /* JS owns pan + pinch on this element; stop the browser from doing its
     own pinch-zoom or panning on top. */
  touch-action: none;
  cursor: grab;
}
#map-container:active { cursor: grabbing; }

#world-map {
  display: block;
  user-select: none;
  /* Cap the natural 4017×2048 size before layoutWorldMap() runs — otherwise
     a brief layout pass at natural size pushes the body wider than the
     viewport, and on mobile that bloats window.innerWidth for the rest of
     the session (every layoutWorldMap re-fits to the inflated value, never
     shrinking back). */
  max-width: 100vw;
  max-height: 100vh;
}

/* ── Region labels ───────────────────────────────────────────────────────── */
#label-layer {
  position: absolute;
  inset: 0;
  pointer-events: none; /* layer itself is transparent; buttons handle events */
}

.region-btn {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: all;
  background: transparent;
  border: none;
  color: transparent;
  padding: 3px 9px;
  border-radius: 3px;
  font-family: Georgia, serif;
  font-size: 11px;
  line-height: 1.3;
  cursor: pointer;
  white-space: nowrap;
}
.region-btn:hover {
  background: transparent;
}
/* Reveal the label when keyboard-focused so tab-through users can see
   which region they're on. region-btn text is otherwise transparent. */
.region-btn:focus-visible {
  color: #c9d8f0;
  outline: 1px dotted rgba(110, 170, 230, 0.78);
  outline-offset: 2px;
}

/* ── Coord display (dev mode, press D) ──────────────────────────────────── */
#coord-display, #detail-coord-display {
  position: fixed;
  bottom: 10px;
  left: 12px;
  font-size: 11px;
  color: rgba(180, 200, 220, 0.6);
  pointer-events: none;
  z-index: 300;
  display: none;
  background: rgba(8,12,18,0.78);
  padding: 4px 10px;
  border-radius: 3px;
  font-family: ui-monospace, Menlo, monospace;
  white-space: pre;
  max-width: calc(100vw - 24px);
}
#detail-coord-display.has-bbox { color: #60a5fa; }

/* Box being drawn while shift-dragging in detail dev mode. */
#draw-box {
  position: absolute;
  border: 2px dashed #60a5fa;
  background: rgba(96, 165, 250, 0.10);
  pointer-events: none;
  display: none;
  z-index: 6;
}
#draw-box.editing {
  border-style: solid;
  background: rgba(96, 165, 250, 0.06);
  pointer-events: all;
  cursor: move;
}
.resize-handle {
  position: absolute;
  width: 12px;
  height: 12px;
  background: #60a5fa;
  border: 1px solid #0d1117;
  border-radius: 2px;
  pointer-events: all;
  display: none;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.5);
}
#draw-box.editing .resize-handle { display: block; }
.resize-handle[data-corner="nw"] { left: -7px; top:    -7px; cursor: nwse-resize; }
.resize-handle[data-corner="ne"] { right:-7px; top:    -7px; cursor: nesw-resize; }
.resize-handle[data-corner="sw"] { left: -7px; bottom: -7px; cursor: nesw-resize; }
.resize-handle[data-corner="se"] { right:-7px; bottom: -7px; cursor: nwse-resize; }

#bbox-editor {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: none;
  align-items: center;
  gap: 10px;
  background: rgba(8, 12, 18, 0.92);
  border: 1px solid rgba(96, 165, 250, 0.55);
  padding: 6px 12px;
  border-radius: 4px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px;
  color: #e6edf3;
  z-index: 7;
  backdrop-filter: blur(4px);
}
#bbox-editor.visible { display: flex; }
#bbox-editor-target {
  color: #60a5fa;
  max-width: 320px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
#bbox-editor button {
  background: none;
  border: 1px solid rgba(96, 165, 250, 0.55);
  color: #60a5fa;
  padding: 4px 12px;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
  transition: background 0.12s, border-color 0.12s;
}
#bbox-editor button:hover { background: rgba(96, 165, 250, 0.16); }
#bbox-editor button.cancel {
  border-color: rgba(180, 200, 220, 0.45);
  color: #b8c2cc;
}
#bbox-editor button:disabled { opacity: 0.45; cursor: default; }
#bbox-editor .status { color: rgba(180, 200, 220, 0.65); }
#bbox-editor .status.ok    { color: #6ee7b7; }
#bbox-editor .status.error { color: #f4a4a4; }

/* ── Region-link overlays (clickable "To <region>" perimeter labels) ─────── */
/* Each box sits over a red "To Mystery Lake" / "TO ASH CANYON" label that the
   OCR pass detected. The button itself is invisible — we only show a soft
   ring on hover/focus so the underlying red text in the map art keeps its
   look. Positioned by positionRegionLinks; z-index keeps it under the search
   highlight pulse but above the tool badges' anchor layer. */
#region-links-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  overflow: hidden;
}
.region-link {
  position: absolute;
  pointer-events: all;
  cursor: pointer;
  background: transparent;
  border: 0;
  border-radius: 6px;
  padding: 0;
  box-sizing: border-box;
  transition: box-shadow 0.18s ease;
}
/* Soft radial halo on hover, mirroring the resource-highlight glow but
   in the region-link's red palette so the two emphasis styles share an
   aesthetic without losing their distinct identities. No vignette here —
   hover shouldn't dim the surrounding map. */
.region-link:hover,
.region-link:focus-visible {
  outline: none;
  box-shadow:
    0 0 16px  3px  rgba(244, 164, 164, 0.65),
    0 0 36px 12px  rgba(244, 164, 164, 0.35),
    0 0 72px 22px  rgba(244, 164, 164, 0.18);
}

/* ── Visited places (click a label to mark it) ───────────────────────────── */
/* Invisible targets over every place label; hover shows a soft ring, and a
   visited place gets a green tint plus a ✓ chip at its top-right corner.
   Hidden in dev mode so the bbox editor gets the clicks. */
#places-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  overflow: hidden;
}
body.dev-mode #places-layer { display: none; }
.place-target {
  position: absolute;
  pointer-events: all;
  cursor: pointer;
  background: transparent;
  border: 0;
  border-radius: 4px;
  padding: 0;
}
.place-target:hover,
.place-target:focus-visible {
  outline: 2px solid rgba(110, 170, 230, 0.75);
}
.place-target.visited {
  background: rgba(46, 160, 67, 0.22);
  outline: 2px solid rgba(46, 160, 67, 0.85);
}
.place-target.visited::after {
  content: '✓';
  position: absolute;
  top: -9px;
  right: -9px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #2ea043;
  color: #fff;
  font: bold 12px/18px system-ui, sans-serif;
  text-align: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}
#visited-progress {
  font: 12px/1 ui-monospace, Menlo, monospace;
  color: rgba(126, 231, 135, 0.85);
  white-space: nowrap;
}
.result-visited { color: #7ee787; }

/* ── All-boxes overlay (dev mode) ────────────────────────────────────────── */
/* Renders every PLACE_BOXES entry for the current region as a labelled
   translucent rectangle so misplaced boxes can be spotted at a glance. */
#all-boxes-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: none;
  z-index: 4;
  overflow: hidden;
}
#all-boxes-layer.visible { display: block; }
.all-box {
  position: absolute;
  border: 1px solid rgba(96, 165, 250, 0.55);
  background: rgba(96, 165, 250, 0.06);
  border-radius: 2px;
  pointer-events: all;
  cursor: pointer;
  box-sizing: border-box;
}
.all-box:hover {
  background: rgba(96, 165, 250, 0.22);
  border-color: #60a5fa;
  z-index: 6;
}
.all-box-label {
  position: absolute;
  left: 0;
  bottom: 100%;
  margin-bottom: 2px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px;
  line-height: 1.2;
  color: #e2eeff;
  background: rgba(8, 12, 18, 0.78);
  padding: 1px 4px;
  white-space: nowrap;
  border-radius: 2px;
  pointer-events: none;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.all-box:hover .all-box-label {
  color: #fff;
  background: rgba(96, 165, 250, 0.92);
}

/* ── Tool badges (per-place crafting-tool tags) ──────────────────────────── */
/* Pinned to each PLACE_BOXES entry that has matching PLACE_TOOLS. Always
   visible in the detail view; sits below the search highlight so it doesn't
   compete when a place is being framed. */
#tools-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  overflow: hidden;
}
.tool-badges {
  position: absolute;
  display: flex;
  gap: 2px;
  pointer-events: none;
  /* Anchor at the box's top-centre; positionToolBadges writes the live
     transform (translate + scale-with-zoom) and the transform-origin keeps
     the cluster pinned to the bbox top while it scales up with the map. */
  transform: translate(-50%, -100%);
  transform-origin: 50% 100%;
  margin-top: -2px;
}
.tool-badge {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  font: 600 10px/16px ui-monospace, Menlo, monospace;
  text-align: center;
  color: #0b1018;
  background: #f5d36b;
  border: 1px solid rgba(8, 12, 18, 0.85);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}
.tool-badge.tool-forge             { background: #f5996b; }
.tool-badge.tool-workbench         { background: #9ec5ff; }
.tool-badge.tool-ammunition_workbench { background: #c9a3ff; }
.tool-badge.tool-milling_machine   { background: #ff9ec5; }
.tool-badge.tool-bed               { background: #b5e8a3; }
.tool-badge.tool-stove             { background: #ffb070; }
.tool-badge.tool-first_aid         { background: #f4a4a4; }
.tool-badge.tool-ice_fishing_hut   { background: #a3e0ff; }
.tool-badge.tool-ice_fishing_hole  { background: #cce8ff; }
/* Filtered mode: dim non-matching badges, accent matching ones. */
#tools-layer.filtered .tool-badge { opacity: 0.18; }
#tools-layer.filtered .tool-badge.match { opacity: 1; outline: 2px solid #fff; }

/* ── Global place search (world view) ────────────────────────────────────── */
#search-wrap {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  width: min(420px, calc(100vw - 32px));
  font-family: Georgia, serif;
}
#place-search {
  width: 100%;
  background: rgba(8, 12, 18, 0.92);
  border: 1px solid rgba(110, 170, 230, 0.32);
  color: #e6edf3;
  padding: 8px 12px;
  border-radius: 4px;
  font-family: inherit;
  font-size: 14px;
  outline: none;
  transition: border-color 0.12s;
  backdrop-filter: blur(4px);
}
#place-search::placeholder { color: rgba(180, 200, 220, 0.45); }
#place-search:focus { border-color: rgba(110, 170, 230, 0.78); }

#search-results {
  margin-top: 4px;
  background: rgba(8, 12, 18, 0.96);
  border: 1px solid rgba(110, 170, 230, 0.28);
  border-radius: 4px;
  max-height: 60vh;
  overflow-y: auto;
  display: none;
  backdrop-filter: blur(4px);
}
.search-result {
  display: flex;
  align-items: baseline;
  gap: 12px;
  width: 100%;
  background: none;
  border: none;
  border-bottom: 1px solid rgba(110, 170, 230, 0.10);
  color: #c9d1d9;
  padding: 7px 12px;
  font-family: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
  transition: background 0.1s;
}
.search-result .result-name {
  /* Soak up the remaining width so region + tools sit flush right. */
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.search-result:last-child { border-bottom: none; }
.search-result:hover,
.search-result.active {
  background: rgba(110, 170, 230, 0.16);
  color: #f0f6ff;
}
.search-result .result-region {
  font-size: 11px;
  color: rgba(180, 200, 220, 0.55);
  flex-shrink: 0;
  white-space: nowrap;
}
.search-result .result-tools {
  display: inline-flex;
  gap: 3px;
  flex-shrink: 0;
}
.search-result .result-tools .tool-badge {
  width: 14px;
  height: 14px;
  font-size: 9px;
  line-height: 14px;
  border-width: 1px;
  box-shadow: none;
  opacity: 0.55;
}
.search-result .result-tools .tool-badge.match { opacity: 1; }
.search-result:hover .result-tools .tool-badge,
.search-result.active .result-tools .tool-badge { opacity: 1; }
/* Region-scoped resource rows (query was a resource keyword like "moose"). */
.search-result .resource-swatch {
  flex-shrink: 0;
  align-self: center;
}
.search-result .result-count {
  flex-shrink: 0;
  color: rgba(180, 220, 180, 0.85);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
}
#search-empty {
  padding: 8px 12px;
  font-size: 12px;
  color: rgba(180, 200, 220, 0.45);
  font-style: italic;
}

/* Hide search whenever the detail modal is up */
body.detail-open #search-wrap { display: none; }

/* ── Offline bulk download ───────────────────────────────────────────────── */
#offline-btn {
  position: fixed;
  right: 14px;
  bottom: 14px;
  z-index: 50;
  background: rgba(8, 12, 18, 0.92);
  border: 1px solid rgba(110, 170, 230, 0.32);
  color: #8bb8e8;
  padding: 6px 14px;
  border-radius: 14px;
  font-family: Georgia, serif;
  font-size: 12px;
  cursor: pointer;
  backdrop-filter: blur(4px);
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
#offline-btn:hover:not(:disabled) {
  background: rgba(110, 170, 230, 0.18);
  border-color: rgba(110, 170, 230, 0.72);
  color: #f0f6ff;
}
#offline-btn:disabled {
  color: rgba(180, 220, 180, 0.75);
  border-color: rgba(180, 220, 180, 0.3);
  cursor: default;
}
#offline-btn[hidden] { display: none; }
body.detail-open #offline-btn { display: none; }

/* ── Detail view (modal) ─────────────────────────────────────────────────── */
#detail-view {
  display: none;
  position: fixed;
  inset: 0;
  background: #0d1117;
  z-index: 100;
}

#detail-dialog {
  width: 100%;
  height: 100%;
  background: #0d1117;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#detail-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 10px 18px;
  background: rgba(8, 12, 18, 0.97);
  border-bottom: 1px solid rgba(110, 170, 230, 0.22);
  flex-shrink: 0;
}

#back-btn {
  background: none;
  border: 1px solid rgba(110, 170, 230, 0.42);
  color: #8bb8e8;
  padding: 5px 14px;
  border-radius: 3px;
  cursor: pointer;
  font-family: Georgia, serif;
  font-size: 14px;
  transition: background 0.12s, border-color 0.12s;
  white-space: nowrap;
  flex-shrink: 0;
}
#back-btn:hover {
  background: rgba(110, 170, 230, 0.15);
  border-color: rgba(110, 170, 230, 0.8);
}

#region-title {
  font-size: 19px;
  font-weight: normal;
  color: #e6edf3;
  letter-spacing: 0.4px;
}

#zoom-controls {
  position: absolute;
  bottom: 14px;
  right: 16px;
  display: flex;
  align-items: center;
  gap: 4px;
  z-index: 10;
  background: rgba(8, 12, 18, 0.75);
  padding: 5px 8px;
  border-radius: 5px;
  backdrop-filter: blur(4px);
}

#zoom-level {
  font-size: 11px;
  color: rgba(180, 200, 220, 0.6);
  min-width: 36px;
  text-align: center;
  font-family: Georgia, serif;
}

.zoom-btn {
  background: none;
  border: 1px solid rgba(110, 170, 230, 0.42);
  color: #8bb8e8;
  width: 28px;
  height: 28px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  transition: background 0.12s, border-color 0.12s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.zoom-btn:hover {
  background: rgba(110, 170, 230, 0.15);
  border-color: rgba(110, 170, 230, 0.8);
}
.zoom-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

#map-tabs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* ── Tools nearby (detail-view header strip) ────────────────────────────── */
/* One pill per rare crafting tool (forge / workbench / ammo / milling).
   If the open region has the tool: shows count + a "here" hint, click jumps
   to the first instance with the tool filter active. If not: shows the
   nearest place + hop count via BFS over region adjacencies. */
/* Second header row: tool pills, then resource pills. */
#header-pills {
  display: flex;
  flex-basis: 100%;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}
/* The two groups flow as one run of pills (display: contents) so they wrap
   pill-by-pill instead of as blocks. */
#tools-nearby { display: contents; }
.tool-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 6px;
  background: rgba(20, 28, 40, 0.72);
  border: 1px solid rgba(110, 170, 230, 0.22);
  border-radius: 14px;
  cursor: pointer;
  color: #c9d1d9;
  font: 11px/1.2 inherit;
  transition: background 0.12s, border-color 0.12s;
}
.tool-pill:hover {
  background: rgba(110, 170, 230, 0.18);
  border-color: rgba(110, 170, 230, 0.55);
  color: #f0f6ff;
}
.tool-pill.here {
  border-color: rgba(150, 220, 150, 0.45);
}
.tool-pill .tool-badge {
  width: 14px;
  height: 14px;
  font-size: 9px;
  line-height: 14px;
  border-width: 1px;
  box-shadow: none;
}
.tool-pill-label {
  font-weight: 500;
  max-width: 14ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool-pill-region {
  color: rgba(180, 200, 220, 0.6);
  font-size: 10px;
}
.tool-pill-hops {
  color: #8bb8e8;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px;
  padding-left: 4px;
  border-left: 1px solid rgba(110, 170, 230, 0.22);
}
.tool-pill-count {
  color: rgba(180, 220, 180, 0.85);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px;
}
.tool-pill.expanded {
  background: rgba(110, 170, 230, 0.22);
  border-color: rgba(110, 170, 230, 0.7);
  color: #f0f6ff;
}
/* "Tool here" cycling — same UX shape as resource-pill cycling. The index
   chip and ✕ are only visible when the pill is .cycling (i.e. there's a
   live toolHereCycle pointed at this tool). */
.tool-pill.cycling {
  background: rgba(110, 170, 230, 0.22);
  border-color: rgba(110, 170, 230, 0.7);
  color: #f0f6ff;
}
.tool-pill-cycle-index {
  color: #8bb8e8;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px;
  padding-left: 6px;
  border-left: 1px solid rgba(110, 170, 230, 0.22);
}
.tool-pill-cycle-index:empty { display: none; }
.tool-pill-cycle-close {
  display: none;
  margin-left: 4px;
  padding: 0 4px;
  color: rgba(220, 230, 245, 0.55);
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  border-radius: 8px;
  transition: color 0.12s, background 0.12s;
}
.tool-pill-cycle-close:hover {
  color: #ffd1d1;
  background: rgba(244, 164, 164, 0.18);
}
.tool-pill.cycling .tool-pill-cycle-close { display: inline; }

/* ── Resources panel (legend-icon detections) ───────────────────────────── */
/* Follows the tool pills in #header-pills; a gap before the first pill sets
   the two groups apart. Each pill is one resource type; click cycles hits. */
#resources-panel { display: contents; }
#resources-panel .resource-pill:first-child { margin-left: 12px; }
.resource-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px 4px 6px;
  background: rgba(20, 28, 40, 0.72);
  border: 1px solid rgba(110, 170, 230, 0.22);
  border-radius: 14px;
  cursor: pointer;
  color: #c9d1d9;
  font: 11px/1.2 inherit;
  transition: background 0.12s, border-color 0.12s;
}
.resource-pill:hover {
  background: rgba(110, 170, 230, 0.18);
  border-color: rgba(110, 170, 230, 0.55);
  color: #f0f6ff;
}
.resource-pill.active {
  /* Echo the resource colour in the border so the active pill's identity
     is unambiguous, even across very different resource types. */
  border-color: var(--resource-color, rgba(110, 170, 230, 0.7));
  background: color-mix(in srgb, var(--resource-color, #6eaae6) 18%, rgba(20, 28, 40, 0.72));
  color: #f0f6ff;
}
.resource-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.18);
  flex-shrink: 0;
}
.resource-pill-label {
  font-weight: 500;
  white-space: nowrap;
}
.resource-pill-count {
  color: rgba(180, 220, 180, 0.85);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px;
}
.resource-pill-index {
  /* Only rendered when the pill is active; empty otherwise. The leading
     border-left visually separates "1 / 5" from the count. */
  color: #8bb8e8;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 10px;
  padding-left: 6px;
  border-left: 1px solid rgba(110, 170, 230, 0.22);
}
.resource-pill-index:empty {
  display: none;
}
/* Pill ✕ — only rendered for the active pill (CSS-driven via .active
   parent). Click clears the cycle without closing the region. Padding is
   generous so the ✕ has a reasonable tap target without enlarging the pill
   when inactive. */
.resource-pill-close {
  display: none;
  margin-left: 4px;
  padding: 0 4px;
  color: rgba(220, 230, 245, 0.55);
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  border-radius: 8px;
  transition: color 0.12s, background 0.12s;
}
.resource-pill-close:hover {
  color: #ffd1d1;
  background: rgba(244, 164, 164, 0.18);
}
.resource-pill.active .resource-pill-close {
  display: inline;
}

/* ── Legend popover ─────────────────────────────────────────────────────── */
#legend-btn {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  margin-left: auto;
  background: none;
  border: 1px solid rgba(110, 170, 230, 0.32);
  border-radius: 50%;
  color: #8bb8e8;
  font-family: Georgia, serif;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s;
}
#legend-btn:hover,
#legend-btn[aria-expanded="true"] {
  background: rgba(110, 170, 230, 0.18);
  border-color: rgba(110, 170, 230, 0.72);
  color: #f0f6ff;
}
#legend-popover {
  position: fixed;
  z-index: 30;
  min-width: 240px;
  max-width: 320px;
  max-height: 70vh;
  overflow-y: auto;
  background: rgba(8, 12, 18, 0.97);
  border: 1px solid rgba(110, 170, 230, 0.42);
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  padding: 12px 14px;
  color: #c9d1d9;
  font-size: 13px;
}
#legend-popover[hidden] { display: none; }
#legend-popover-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(110, 170, 230, 0.18);
  margin-bottom: 10px;
}
#legend-popover-title {
  flex: 1;
  font-weight: 500;
  color: #e6edf3;
}
#legend-close {
  background: none;
  border: none;
  color: rgba(180, 200, 220, 0.55);
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}
#legend-close:hover { color: #f0f6ff; }
.legend-section h3 {
  margin: 10px 0 6px;
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: rgba(180, 200, 220, 0.55);
}
.legend-note {
  margin: 0 0 6px;
  font-size: 11px;
  color: rgba(180, 200, 220, 0.55);
}
.legend-rows {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 14px;
}
.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 0;
}
.legend-row .tool-badge {
  width: 16px;
  height: 16px;
  font-size: 10px;
  line-height: 16px;
  border-width: 1px;
  box-shadow: none;
  flex-shrink: 0;
}

/* ── Route preview popover ──────────────────────────────────────────────── */
/* Anchored below a clicked tool pill; lists each region in the path so the
   user can step through one transition at a time, instead of teleporting. */
#route-popover {
  position: fixed;
  z-index: 30;
  min-width: 280px;
  max-width: 360px;
  background: rgba(8, 12, 18, 0.97);
  border: 1px solid rgba(110, 170, 230, 0.42);
  border-radius: 6px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  padding: 12px 14px;
  color: #c9d1d9;
  font-size: 13px;
}
#route-popover[hidden] { display: none; }
#route-popover-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(110, 170, 230, 0.18);
  margin-bottom: 10px;
}
#route-popover-title {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  font-weight: 500;
  color: #e6edf3;
}
#route-popover-title .tool-badge {
  width: 16px;
  height: 16px;
  font-size: 10px;
  line-height: 16px;
  border-width: 1px;
  box-shadow: none;
}
#route-popover-close {
  background: none;
  border: none;
  color: rgba(180, 200, 220, 0.55);
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}
#route-popover-close:hover { color: #f0f6ff; }
#route-popover-steps {
  list-style: none;
  margin: 0;
  padding: 0;
}
.route-step {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 0;
  position: relative;
}
/* Connector line between steps. */
.route-step:not(:last-child)::after {
  content: '';
  position: absolute;
  left: 7px;
  top: 26px;
  width: 1px;
  height: calc(100% - 14px);
  background: rgba(110, 170, 230, 0.32);
}
.route-step-dot {
  width: 14px;
  text-align: center;
  font-size: 12px;
  line-height: 1;
  color: #8bb8e8;
  flex-shrink: 0;
}
.route-step-origin .route-step-dot { color: #b5e8a3; }
.route-step-dest   .route-step-dot { color: #f5d36b; }
.route-step-meta {
  flex: 1;
  min-width: 0;
}
.route-step-name {
  color: #e6edf3;
  font-weight: 500;
}
.route-step-note {
  color: rgba(180, 200, 220, 0.55);
  font-size: 11px;
}
.route-step-btn {
  background: none;
  border: 1px solid rgba(110, 170, 230, 0.32);
  color: #8bb8e8;
  padding: 3px 10px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 11px;
  flex-shrink: 0;
  transition: background 0.12s, border-color 0.12s;
}
.route-step-btn:hover {
  background: rgba(110, 170, 230, 0.18);
  border-color: rgba(110, 170, 230, 0.72);
  color: #f0f6ff;
}
#route-popover-travel {
  margin-top: 12px;
  width: 100%;
  background: rgba(245, 211, 107, 0.16);
  border: 1px solid rgba(245, 211, 107, 0.45);
  color: #f5d36b;
  padding: 8px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.12s, border-color 0.12s;
}
#route-popover-travel:hover {
  background: rgba(245, 211, 107, 0.28);
  border-color: rgba(245, 211, 107, 0.72);
}

/* ── Back-to-origin breadcrumb ──────────────────────────────────────────── */
#back-to-origin-btn {
  background: rgba(110, 170, 230, 0.12);
  border: 1px solid rgba(110, 170, 230, 0.35);
  color: #8bb8e8;
  padding: 4px 12px;
  border-radius: 3px;
  cursor: pointer;
  font-family: Georgia, serif;
  font-size: 13px;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.12s, border-color 0.12s;
}
#back-to-origin-btn:hover {
  background: rgba(110, 170, 230, 0.24);
  border-color: rgba(110, 170, 230, 0.7);
  color: #f0f6ff;
}
#back-to-origin-btn[hidden] { display: none; }

.tab-btn {
  background: none;
  border: 1px solid rgba(110, 170, 230, 0.28);
  color: #8bb8e8;
  padding: 4px 11px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 12px;
  font-family: Georgia, serif;
  transition: background 0.12s, border-color 0.12s;
}
.tab-btn:hover { background: rgba(110, 170, 230, 0.13); }
.tab-btn.active {
  background: rgba(110, 170, 230, 0.22);
  border-color: rgba(110, 170, 230, 0.72);
  color: #c9d8f0;
}

#map-canvas {
  flex: 1;
  overflow: hidden;
  position: relative;
  cursor: grab;
  /* Stop the browser running its own pinch-zoom on top of ours: without
     this, two-finger pinch on the canvas also scales the visual viewport,
     dragging the detail header (pills, tabs) off-screen as a side effect. */
  touch-action: none;
}
#map-canvas:active { cursor: grabbing; }

#detail-img {
  position: absolute;
  transform-origin: 0 0;
  user-select: none;
  max-width: none;
}

/* While loadDetailMap is swapping the source for a different region, hide
   the image and every overlay that's positioned against the old transform.
   Removed the moment focusHighlight/fitMap repositions everything, so the
   user sees the destination scene fully composed instead of catching the
   intermediate frame. visibility (not display) keeps zoom-controls /
   coord-display hit-tested and prevents layout shifts. */
#map-canvas.map-loading #detail-img,
#map-canvas.map-loading #region-links-layer,
#map-canvas.map-loading #tools-layer,
#map-canvas.map-loading #places-layer,
#map-canvas.map-loading #all-boxes-layer,
#map-canvas.map-loading #highlight-box {
  visibility: hidden;
}

/* ── Highlighted-place overlay (search target) ───────────────────────────── */
/* The pulse fades a dim spotlight in and out, then settles to a plain glowing
   border so the box stays as a "you searched for this" marker without dimming
   the rest of the map. */
#highlight-box {
  position: absolute;
  /* No hard border or fill — only a soft radial halo via stacked shadows.
     The bbox is wiki text, often irregular; a tight rectangle made it look
     like a placeholder. The halo radiates outward from the bbox edges,
     drawing the eye without caging the label. */
  border-radius: 6px;
  pointer-events: none;
  display: none;
  z-index: 5;
  animation: highlight-pulse 1.1s ease-out 1 forwards;
}
/* In dev mode the highlight is grabbable: click it to start editing the
   stored bbox without having to shift-drag a fresh one. A faint outline
   makes the grab affordance visible since there's no border in normal
   mode. */
#highlight-box.dev-grab {
  pointer-events: all;
  cursor: grab;
  outline: 2px dashed rgba(96, 165, 250, 0.85);
  outline-offset: 2px;
}
#highlight-box.dev-grab:active { cursor: grabbing; }
/* Single smooth fade: the rest of the map dims briefly so the eye lands on
   the highlight, then the vignette fades out leaving a soft halo around
   the place. The halo is a stack of three blurred shadows — tight bright
   core, medium falloff, and wide soft glow — so it reads as light, not as
   a hard outline. */
@keyframes highlight-pulse {
  0% {
    box-shadow:
      0 0  0  9999px rgba(8, 12, 18, 0.55),
      0 0 22px  6px  rgba(96, 165, 250, 0.9),
      0 0 50px 18px  rgba(96, 165, 250, 0.55),
      0 0 96px 32px  rgba(96, 165, 250, 0.3);
  }
  100% {
    box-shadow:
      0 0  0  9999px rgba(8, 12, 18, 0),
      0 0 18px  4px  rgba(96, 165, 250, 0.65),
      0 0 40px 14px  rgba(96, 165, 250, 0.35),
      0 0 80px 26px  rgba(96, 165, 250, 0.18);
  }
}

#zoom-hint {
  position: absolute;
  bottom: 60px;
  right: 16px;
  font-size: 11px;
  color: rgba(220, 230, 240, 0.85);
  background: rgba(8, 12, 18, 0.75);
  padding: 3px 8px;
  border-radius: 4px;
  pointer-events: none;
}

/* ── Mobile / narrow viewport ────────────────────────────────────────────── */
/* Phone-sized layout: stack the detail header into rows, turn the tab and
   tool-pill rows into horizontally scrollable strips, dock the route popover
   as a bottom sheet, and grow tap targets to ~44px. The world map and detail
   map already pinch-zoom; this block fixes the chrome around them.
   The second condition (short + landscape) catches phones held sideways —
   a phone in landscape is ~800px wide so max-width:640 wouldn't fire, but
   the ~390px height makes the wrapped desktop header eat most of the screen.
   iPads in landscape are ~768px tall so they're not affected. */
@media (max-width: 640px), (max-height: 500px) and (orientation: landscape) {
  /* Search: 16px font prevents iOS auto-zoom on focus. */
  #search-wrap {
    top: 10px;
    width: calc(100vw - 16px);
  }
  #place-search {
    font-size: 16px;
    padding: 11px 14px;
  }
  #search-results {
    max-height: 70vh;
  }
  .search-result {
    padding: 12px 14px;
    font-size: 14px;
    gap: 10px;
  }
  .search-result .result-region { font-size: 12px; }

  /* World labels: grow the (transparent) tap area so a finger can hit them. */
  .region-btn {
    padding: 10px 14px;
    font-size: 13px;
  }

  /* Detail header: stack title row, then tabs strip, then tools strip.
     Each strip scrolls horizontally instead of wrapping into a tall pile. */
  #detail-header {
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 10px;
    align-items: center;
  }

  /* The legend button leaves the header (where it forced a mostly-empty
     fourth row) and joins the zoom controls at the bottom edge, styled to
     match them. Its popover already docks as a bottom sheet on phones. */
  #legend-btn {
    position: fixed;
    left: 16px;
    bottom: 14px;
    z-index: 10;
    width: 44px;
    height: 44px;
    font-size: 20px;
    background: rgba(8, 12, 18, 0.75);
  }
  #region-title {
    font-size: 16px;
    flex: 1 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  #back-btn {
    padding: 8px 12px;
    font-size: 16px;
    min-width: 40px;
  }
  #back-to-origin-btn {
    font-size: 12px;
    padding: 6px 10px;
  }

  /* Map-variant tabs: scroll horizontally instead of wrapping. */
  #map-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    flex-basis: 100%;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  #map-tabs::-webkit-scrollbar { display: none; }
  .tab-btn { flex-shrink: 0; padding: 8px 14px; font-size: 13px; }

  /* Tool + resource pills share one horizontally scrolling strip, so the
     header stays at three short rows instead of four. */
  #header-pills {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  #header-pills::-webkit-scrollbar { display: none; }
  .tool-pill { flex-shrink: 0; padding: 6px 12px 6px 8px; }
  .resource-pill { flex-shrink: 0; padding: 6px 12px 6px 8px; }

  /* Route popover: dock as a bottom sheet. The !important overrides override
     the inline top/right that anchorRoutePopover() sets relative to a pill —
     on a phone-width viewport that anchor would push the popover off-screen. */
  #route-popover {
    top: auto !important;
    right: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    max-width: none;
    min-width: 0;
    border-radius: 8px 8px 0 0;
    padding: 14px 16px calc(14px + env(safe-area-inset-bottom)) 16px;
    border-bottom: none;
    /* dvh tracks the visible viewport (excludes iOS Safari's URL bar)
       so the sheet's edges aren't tucked under browser chrome. */
    max-height: 70dvh;
    overflow-y: auto;
  }
  .route-step { padding: 10px 0; }
  .route-step-btn { padding: 8px 12px; font-size: 13px; }
  #route-popover-travel { padding: 12px; font-size: 14px; }

  /* Legend popover: same bottom-sheet docking as the route popover. */
  #legend-popover {
    top: auto !important;
    right: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    max-width: none;
    min-width: 0;
    border-radius: 8px 8px 0 0;
    padding: 14px 16px calc(14px + env(safe-area-inset-bottom)) 16px;
    border-bottom: none;
    max-height: 70dvh;
    overflow-y: auto;
  }

  /* Zoom controls: 40px hit area, easier to tap than 28px. */
  .zoom-btn { width: 40px; height: 40px; font-size: 20px; }
  #zoom-controls { padding: 4px 6px; gap: 6px; }
  #zoom-level { min-width: 42px; font-size: 12px; }

  /* The hint references scroll wheels and arrow keys — not useful on touch. */
  #zoom-hint { display: none; }

  /* Right-edge fade on the horizontally scrolling strips while more pills
     are hidden that way; JS toggles the class from scroll position. */
  #map-tabs.scroll-fade,
  #header-pills.scroll-fade {
    -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent);
    mask-image: linear-gradient(90deg, #000 calc(100% - 28px), transparent);
  }
}

/* Phone in landscape: viewport is wide enough to sit map-tabs inline with
   the title row, saving ~32px of header height. The pill strip keeps its
   own row — it has too many pills to fit. */
@media (max-height: 500px) and (orientation: landscape) {
  #detail-header { padding: 6px 10px; gap: 6px; }
  #map-tabs {
    flex-basis: auto;
    flex-shrink: 1;
    max-width: 45%;
  }
}
