/* Map page (Feature 1). Colours, spacing and type come from css/tokens.css only. */

/* Screen-reader-only helper (alias of .visually-hidden in base.css) */
.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Page layout ---------- */
.map-page {
  display: grid;
  grid-template-columns: minmax(0, 1fr);   /* stacked below 1024px */
  gap: var(--space-4);
  min-width: 0;
}
.map-page__main,
.map-page__side {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  min-width: 0;                            /* prevents horizontal page scroll */
}

/* Map and Detail_Panel side by side at 1024px and wider */
@media (min-width: 1024px) {
  .map-page {
    grid-template-columns: minmax(0, 1fr) 320px;
    align-items: start;
  }
}
@media (min-width: 1280px) {
  .map-page { grid-template-columns: minmax(0, 1fr) 360px; }
}

/* ---------- Search ---------- */
.map-search {
  padding: var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  position: relative;
}
.map-search__label {
  display: block;
  margin-bottom: var(--space-2);
  font-size: var(--font-size-sm);
  font-weight: 600;
}
.map-search__row { display: flex; gap: var(--space-2); }
.map-search__input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: var(--space-2) var(--space-3);
  font: inherit;
  font-size: var(--font-size-base);
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-text-muted);
  border-radius: var(--radius);
}
.map-search__input::placeholder { color: var(--color-text-muted); opacity: 1; }
.map-search__input:hover { border-color: var(--color-brand); }
.map-search__input:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
  border-color: var(--color-brand);
}
.map-search__button,
.map-overlay__retry {
  min-height: 44px;
  padding: var(--space-2) var(--space-4);
  font: inherit;
  font-size: var(--font-size-base);
  font-weight: 600;
  color: var(--color-on-brand);
  background: var(--color-brand);
  border: 1px solid var(--color-brand);
  border-radius: var(--radius);
  cursor: pointer;
}
.map-search__button:hover,
.map-overlay__retry:hover { background: var(--color-brand-dark); border-color: var(--color-brand-dark); }
.map-search__button:active,
.map-overlay__retry:active { background: var(--color-text); border-color: var(--color-text); }
.map-search__button:focus-visible,
.map-overlay__retry:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}
.map-search__message {
  margin-top: var(--space-2);
  font-size: var(--font-size-sm);
  color: var(--color-text);
}
.map-search__results {
  list-style: none;
  margin: var(--space-2) 0 0;
  padding: 0;
  max-height: 240px;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}
.map-search__results[hidden],
.map-search__message[hidden] { display: none; }
.map-search__result {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 44px;
  padding: var(--space-2) var(--space-3);
  font: inherit;
  text-align: left;
  color: var(--color-text);
  background: var(--color-surface);
  border: 0;
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
}
.map-search__results li:last-child .map-search__result { border-bottom: 0; }
.map-search__result:hover { background: var(--color-brand-tint); }
.map-search__result:active { background: var(--color-border); }
.map-search__result:focus-visible,
.map-search__result[aria-selected="true"] {
  background: var(--color-brand-tint);
  outline: 3px solid var(--color-focus);
  outline-offset: -3px;
}
.map-search__result-area { font-size: var(--font-size-sm); color: var(--color-text-muted); }

/* Search suggestions (autocomplete list) */
.map-search__suggestions {
  list-style: none;
  margin: var(--space-2) 0 0;
  padding: 0;
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  background: var(--color-surface);
}
.map-search__suggestions[hidden] { display: none; }
.map-search__suggestion {
  display: flex;
  flex-direction: column;
  min-height: 44px;
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  cursor: pointer;
}
.map-search__suggestions li:last-child { border-bottom: 0; }
.map-search__suggestion:hover { background: var(--color-brand-tint); }
.map-search__suggestion:active { background: var(--color-border); }
.map-search__suggestion[aria-selected="true"] {
  background: var(--color-brand-tint);
  outline: 3px solid var(--color-focus);
  outline-offset: -3px;
}
.map-search__suggestion-name { font-size: var(--font-size-base); }
.map-search__suggestion-area { color: var(--color-text-muted); }
/* Bold plus underline, so the match does not rely on colour. */
.map-search__suggestions mark {
  background: transparent;
  color: inherit;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.map-search__suggestion--empty {
  min-height: 44px;
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  cursor: default;
}

/* ---------- Map view and overlays ---------- */
.map-view-wrap {
  position: relative;
  background: var(--color-border);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
}
.map-view {
  width: 100%;
  height: clamp(360px, 60vh, 640px);
  font-family: var(--font-family);
}
.map-view:focus-visible { outline: 3px solid var(--color-focus); outline-offset: -3px; }

.map-overlay {
  position: absolute;
  inset: 0;
  z-index: 1000;                           /* above Leaflet panes and controls */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-4);
  text-align: center;
  background: var(--color-surface);
  color: var(--color-text);
}
.map-overlay[hidden] { display: none; }
.map-overlay__text { font-size: var(--font-size-base); max-width: 40ch; }
.map-overlay--error .map-overlay__text { font-weight: 600; }
.map-overlay__spinner {
  width: 32px;
  height: 32px;
  border: 4px solid var(--color-border);
  border-top-color: var(--color-brand);
  border-radius: 50%;
  animation: map-spin 0.9s linear infinite;
}
@keyframes map-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .map-overlay__spinner { animation: none; }
}

/* Empty state or data error shown inside the map region */
.map-view__message {
  position: absolute;
  left: var(--space-4);
  right: var(--space-4);
  top: var(--space-4);
  z-index: 900;
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- Markers (Leaflet divIcon content) ---------- */
/* Remove Leaflet's default divIcon box so only our marker shows. */
.leaflet-div-icon.marker-icon,
.marker-icon {
  background: transparent;
  border: 0;
}
/* The divIcon has zero size; centre the marker above its coordinate.
   `translate` composes with the hover/selected `transform`. */
.marker-icon { overflow: visible; }
.marker-icon > .marker {
  position: absolute;
  left: 0;
  top: 0;
  translate: -50% -100%;
}
.marker {
  --marker-colour: var(--color-risk-unknown);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-1);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  font-weight: 600;
  line-height: 1.25;
  white-space: nowrap;
  color: var(--color-text);
  background: var(--color-surface);
  border: 2px solid var(--marker-colour);
  border-radius: 50%;
  box-shadow: var(--shadow);
  cursor: pointer;
  transform-origin: center bottom;
  transition: transform 0.12s, box-shadow 0.12s;
}
.marker--low      { --marker-colour: var(--color-risk-low); }
.marker--moderate { --marker-colour: var(--color-risk-moderate); }
.marker--high     { --marker-colour: var(--color-risk-high); }
.marker--unknown  { --marker-colour: var(--color-risk-unknown); }

.marker__icon {
  flex: none;
  width: 20px;
  height: 20px;
  color: var(--marker-colour);
}
.marker__label { overflow: hidden; text-overflow: ellipsis; }

.marker:hover { transform: scale(1.08); box-shadow: 0 3px 8px rgba(27, 39, 51, 0.3); }
.marker:active { transform: scale(1.02); }
.marker:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 3px;
}

/* Selected: larger, with a solid outline ring (shape, not colour change) */
.marker--selected {
  transform: scale(1.25);
  font-weight: 700;
  box-shadow: 0 0 0 3px var(--color-surface), 0 0 0 6px var(--color-text);
}
.marker--selected:hover { transform: scale(1.3); }
.marker--selected:active { transform: scale(1.2); }
.marker--selected:focus-visible { outline-offset: 8px; }

/* Leaflet tooltip (hover and focus) */
.leaflet-tooltip.marker-tooltip {
  padding: var(--space-2) var(--space-3);
  font-family: var(--font-family);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-text-muted);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  white-space: nowrap;
}
.leaflet-tooltip.marker-tooltip::before { display: none; }

/* Leaflet controls: keep readable and focusable */
.map-view .leaflet-control-zoom a { width: 36px; height: 36px; line-height: 36px; }
.map-view .leaflet-control-zoom a:focus-visible,
.map-view .leaflet-control-attribution a:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}
.map-view .leaflet-control-attribution { font-size: var(--font-size-sm); }

/* ---------- Risk badge (legend, detail panel) ---------- */
.risk-badge {
  --badge-colour: var(--color-risk-unknown);
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  font-size: var(--font-size-sm);
  font-weight: 700;
  color: var(--color-on-brand);
  background: var(--badge-colour);
  border-radius: var(--radius);
}
.risk-badge--low      { --badge-colour: var(--color-risk-low); }
.risk-badge--moderate { --badge-colour: var(--color-risk-moderate); }
.risk-badge--high     { --badge-colour: var(--color-risk-high); }
.risk-badge--unknown  { --badge-colour: var(--color-risk-unknown); }
.risk-badge__icon { flex: none; width: 18px; height: 18px; }

/* ---------- Risk legend ---------- */
.risk-legend__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.risk-legend__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}
.risk-legend__count {
  font-size: var(--font-size-base);
  font-weight: 700;
  color: var(--color-text);
}
.risk-legend__updated {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
}

/* ---------- Detail panel ---------- */
.detail-panel__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-3);
}
.detail-panel__close {
  flex: none;
  width: 44px;
  height: 44px;
  font: inherit;
  font-size: var(--font-size-lg);
  line-height: 1;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-text-muted);
  border-radius: var(--radius);
  cursor: pointer;
}
.detail-panel__close[hidden] { display: none; }
.detail-panel__close:hover { background: var(--color-brand-tint); border-color: var(--color-brand); }
.detail-panel__close:active { background: var(--color-border); }
.detail-panel__close:focus-visible {
  outline: 3px solid var(--color-focus);
  outline-offset: 2px;
}
.detail-panel__prompt { color: var(--color-text-muted); }
.detail-panel__content[hidden] { display: none; }
.detail-panel__content { display: flex; flex-direction: column; gap: var(--space-4); }

.detail-panel__name { font-size: var(--font-size-lg); }
.detail-panel__area { font-size: var(--font-size-sm); color: var(--color-text-muted); }

.detail-section {
  padding-top: var(--space-3);
  border-top: 1px solid var(--color-border);
}
.detail-section__title {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-bottom: var(--space-1);
  font-size: var(--font-size-base);
  font-weight: 600;
}
.detail-section__icon { flex: none; width: 20px; height: 20px; color: var(--color-brand); }
.detail-section__value { font-size: var(--font-size-lg); font-weight: 700; }
.detail-section__meta,
.detail-section__fallback { font-size: var(--font-size-sm); color: var(--color-text-muted); }

.incident-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.incident-list__item {
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-sm);
  background: var(--color-bg);
  border-radius: var(--radius);
}
.incident-list__date { font-weight: 700; }

/* ---------- Risk explanation ("Why this level") ---------- */
.risk-explanation {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  font-size: var(--font-size-sm);
  color: var(--color-text);
}
.risk-explanation__title {
  margin: 0;
  font-size: var(--font-size-base);
  font-weight: 700;
  color: var(--color-text);
}
.risk-explanation__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.risk-explanation__row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: baseline;
  gap: var(--space-2) var(--space-3);
  padding-bottom: var(--space-2);
  border-bottom: 1px solid var(--color-border);
}
.risk-explanation__row:last-child { border-bottom: 0; padding-bottom: 0; }
.risk-explanation__label {
  font-weight: 700;
  color: var(--color-text);
}
.risk-explanation__value {
  color: var(--color-text-muted);
  overflow-wrap: anywhere;
}
/* Points are always text (for example "1 pt" or "n/a"); the outline keeps
   them readable without colour. */
.risk-explanation__points {
  padding: var(--space-1) var(--space-2);
  font-size: var(--font-size-sm);
  font-weight: 700;
  white-space: nowrap;
  color: var(--color-brand-dark);
  background: var(--color-brand-tint);
  border: 1px solid var(--color-brand);
  border-radius: var(--radius);
}
.risk-explanation__summary {
  margin: 0;
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--color-text);
  background: var(--color-bg);
  border-left: 4px solid var(--color-brand);
  border-radius: var(--radius);
}
/* Partial-data and insufficient-data notes: dashed outline, thick left
   border and a bold label, so they differ from the summary by form. */
.risk-explanation__note {
  margin: 0;
  padding: var(--space-2) var(--space-3);
  font-size: var(--font-size-sm);
  color: var(--color-text);
  background: var(--color-surface);
  border: 2px dashed var(--color-text-muted);
  border-left: 6px solid var(--color-text);
  border-radius: var(--radius);
}
.risk-explanation__note strong,
.risk-explanation__note b {
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

/* ---------- Tablet (768px to 1023px): stacked, tighter map ---------- */
@media (max-width: 1023px) {
  .map-view { height: clamp(320px, 50vh, 520px); }
}
@media (max-width: 800px) {
  .map-search__row { flex-wrap: wrap; }
  .map-search__input { flex-basis: 100%; }
}

/* ---------- Map insights (below the map) ---------- */
.map-insights {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-4);
  margin-top: var(--space-4);
  min-width: 0;
}
.coverage__list { list-style: none; margin: var(--space-3) 0 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-3); }
.coverage__item { padding-left: var(--space-3); border-left: 4px solid var(--color-border); }
.coverage__item--partial { border-left-style: dashed; }
.coverage__item--none { border-left-style: dotted; }
.coverage__head { display: flex; justify-content: space-between; gap: var(--space-3); font-weight: 700; }
.coverage__note { font-size: var(--font-size-sm); color: var(--color-text-muted); }
.coverage__names { list-style: none; margin: var(--space-2) 0 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-1); }
.coverage__name { font-size: var(--font-size-sm); display: flex; flex-wrap: wrap; gap: var(--space-1) var(--space-3); align-items: baseline; }
.coverage__link {
  padding: 0;
  font: inherit;
  font-weight: 600;
  color: var(--color-brand-dark);
  background: none;
  border: 0;
  text-decoration: underline;
  cursor: pointer;
}
.coverage__link:hover { color: var(--color-text); }
.coverage__link:focus-visible { outline: 3px solid var(--color-brand); outline-offset: 2px; }
.coverage__reason { color: var(--color-text-muted); }

.method__summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--color-brand-dark);
  padding: var(--space-2) 0;
}
.method__summary:focus-visible { outline: 3px solid var(--color-brand); outline-offset: 2px; }
.method__body { display: flex; flex-direction: column; gap: var(--space-3); padding-top: var(--space-2); }
.method__table { width: 100%; border-collapse: collapse; font-size: var(--font-size-sm); }
.method__caption { text-align: left; font-weight: 700; padding-bottom: var(--space-2); }
.method__table th, .method__table td { padding: var(--space-2) var(--space-3); text-align: left; border: 1px solid var(--color-border); }
.method__table thead th { background: var(--color-bg); }
.method__steps { margin: 0; padding-left: var(--space-5); display: flex; flex-direction: column; gap: var(--space-1); }
.method__note { font-size: var(--font-size-sm); color: var(--color-text-muted); }
