/* ==========================================================================
   Explore: the map screen
   Mobile is an app shell — a full-height map under a floating header, with a
   draggable results sheet over it. At >=64rem it becomes a split view: list on
   the left, sticky map on the right.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Shell
   -------------------------------------------------------------------------- */
.explore-page { overflow: hidden; }
.explore-page body,
body.explore-page { overflow: hidden; }

.explore {
  position: fixed;
  inset: 0;
  /* Dynamic viewport units: browser chrome appearing must not crop the sheet. */
  height: 100dvh;
  overflow: hidden;
}

.explore__map {
  position: absolute;
  inset: 0;
  background: var(--cream-200);
  /* Leaflet gives its own panes z-index 400-800. Without a stacking context
     here those values compete with the page's, and the map paints over the
     floating header and the results sheet — which is exactly what put stray
     marker clusters on top of the category chips. `isolation` contains them. */
  z-index: var(--z-map);
  isolation: isolate;
}

/* Leaflet's own stacking has to sit under our chrome. */
.explore__map .leaflet-tile-pane {
  /* The keyless OSM style is more saturated than a premium light basemap.
     A slight desaturation and lift settles it behind the brand green without
     misrepresenting the data. Harmless on CARTO/MapTiler too. */
  filter: saturate(0.78) brightness(1.04) contrast(0.96);
}

.explore__map .leaflet-container {
  width: 100%;
  height: 100%;
  background: #eef0ea;
  font-family: var(--font-sans);
  /* Leaflet's default is a system serif stack for attribution. */
  font-size: var(--text-xs);
}
.explore__map .leaflet-control-attribution {
  background: rgba(255, 255, 255, 0.82);
  padding: 2px 8px;
  border-radius: var(--radius-sm) 0 0 0;
  color: var(--ink-600);
}
.explore__map .leaflet-control-attribution a { color: var(--green-700); }
.explore__map .leaflet-control-zoom { display: none; }

@media (min-width: 64rem) {
  .explore__map .leaflet-control-zoom {
    display: block;
    border: 0;
    box-shadow: var(--shadow-md);
    border-radius: var(--radius-md);
    overflow: hidden;
  }
  .explore__map .leaflet-control-zoom a {
    width: 38px;
    height: 38px;
    line-height: 38px;
    color: var(--ink-900);
    background: var(--bg-raised);
    border-bottom-color: var(--line);
    font-size: 1.125rem;
  }
  .explore__map .leaflet-control-zoom a:hover { background: var(--cream-50); color: var(--green-700); }
}

/* --------------------------------------------------------------------------
   Floating header
   -------------------------------------------------------------------------- */
.explore-header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: var(--z-header);
  padding-top: var(--safe-top);
  background: linear-gradient(
    to bottom,
    color-mix(in srgb, var(--cream-100) 94%, transparent) 0%,
    color-mix(in srgb, var(--cream-100) 88%, transparent) 72%,
    transparent 100%
  );
  -webkit-backdrop-filter: saturate(160%) blur(12px);
  backdrop-filter: saturate(160%) blur(12px);
  pointer-events: none;
}
/* The gradient is decorative; only the controls inside should take taps. */
.explore-header > * { pointer-events: auto; }

.explore-header__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  min-height: 3.25rem;
}

.explore-header__search {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  padding-bottom: var(--space-2);
}
.explore-header__search .search-pill { flex: 1 1 auto; min-width: 0; min-height: 48px; }

@media (min-width: 64rem) {
  .explore-header__search .search-pill { max-width: 32rem; }
}

/* Filter button with its active count. */
.filter-button { position: relative; flex: none; }
.filter-button__count {
  position: absolute;
  top: -2px;
  right: -2px;
  display: grid;
  place-items: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding-inline: 0.25rem;
  border-radius: var(--radius-pill);
  background: var(--amber-500);
  color: var(--ink-900);
  font-size: 0.6875rem;
  font-weight: 700;
  box-shadow: 0 0 0 2px var(--cream-100);
}

.explore-header .chip-row { padding-bottom: var(--space-3); }

/* --------------------------------------------------------------------------
   Map controls
   -------------------------------------------------------------------------- */
.map-controls {
  position: absolute;
  right: max(var(--gutter), var(--safe-right));
  z-index: calc(var(--z-sheet) - 1);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  /* Sits above the sheet's peek height, tracked by the sheet as it moves. */
  bottom: calc(var(--sheet-peek, 7.5rem) + var(--space-3) + var(--safe-bottom));
  transition: bottom var(--t-med) var(--ease-out);
}
.map-controls .btn--icon { box-shadow: var(--shadow-md); }
.map-controls .btn--icon[aria-pressed="true"] {
  background: var(--green-700);
  border-color: var(--green-700);
  color: #fff;
}

/* "Search this area", Airbnb-style: appears only after the user pans. */
.search-area {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -0.5rem);
  z-index: calc(var(--z-sheet) - 1);
  bottom: calc(var(--sheet-peek, 7.5rem) + var(--space-3) + var(--safe-bottom));
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--t-med) var(--ease-out),
    transform var(--t-med) var(--ease-out),
    visibility var(--t-med);
}
.search-area.is-visible {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
}
.search-area .btn { box-shadow: var(--shadow-lg); }

@media (min-width: 64rem) {
  .map-controls { bottom: calc(var(--space-6) + var(--safe-bottom)); }
  .search-area { bottom: calc(var(--space-6) + var(--safe-bottom)); }
}

/* --------------------------------------------------------------------------
   Markers (Leaflet divIcons)
   -------------------------------------------------------------------------- */
.leaflet-marker-icon.marker-pin { background: none; border: 0; }

/* Standard pin: a tidy branded dot. */
.pin {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--green-700);
  border: 2.5px solid #fff;
  color: #fff;
  box-shadow: 0 2px 6px rgba(19, 32, 26, 0.35);
  transition: transform var(--t-med) var(--ease-spring), box-shadow var(--t-med) var(--ease-out);
}
.pin .icon { width: 0.8125rem; height: 0.8125rem; stroke-width: 2.25; }
.pin:hover, .pin.is-active {
  transform: scale(1.25);
  box-shadow: 0 4px 12px rgba(19, 32, 26, 0.45);
  z-index: 500;
}

/* Premium: a circular badge carrying the farm's own image in a brand ring.
   Much more prominent when zoomed out, and normalising as you zoom in so the
   map stays readable. The scale is driven by a data attribute on the map. */
.pin--premium {
  width: 46px;
  height: 46px;
  border-width: 3px;
  background: var(--green-600);
  box-shadow: 0 0 0 3px var(--green-700), 0 4px 14px rgba(19, 32, 26, 0.4);
  overflow: hidden;
}
.pin--premium img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
/* Keeps a light logo inside the ring rather than bleeding into the map. */
.pin--premium::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(19, 32, 26, 0.25),
              inset 0 -8px 14px -6px rgba(1, 46, 26, 0.55);
  pointer-events: none;
}
.pin--premium .pin__initial {
  font-family: var(--font-display);
  font-size: 1.25rem;
  font-weight: 600;
  color: #fff;
}
.pin--premium:hover, .pin--premium.is-active { transform: scale(1.12); }

/* Area-privacy farms get a soft circle, never a precise pin. */
.pin--area {
  width: 40px;
  height: 40px;
  background: color-mix(in srgb, var(--green-500) 20%, transparent);
  border: 1.5px dashed var(--green-600);
  box-shadow: none;
  color: var(--green-800);
}
.pin--area:hover, .pin--area.is-active {
  background: color-mix(in srgb, var(--green-500) 32%, transparent);
}

/* Zoomed out, premium pins shrink a little so the region stays legible. */
.map--regional .pin--premium { width: 40px; height: 40px; }
.map--regional .pin--premium .pin__initial { font-size: 1.0625rem; }
.map--close .pin--premium { width: 52px; height: 52px; }

.cluster-pin { background: none; border: 0; }

/* Named .map-cluster, not .cluster: base.css already uses .cluster as a
   flex-row layout utility, and the collision turned every card's open/closed
   status group into a green circle. */
.map-cluster {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--green-700);
  border: 3px solid rgba(255, 255, 255, 0.92);
  color: #fff;
  font-size: var(--text-sm);
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(19, 32, 26, 0.35), 0 0 0 6px rgba(0, 81, 44, 0.1);
  transition: transform var(--t-med) var(--ease-spring);
}
.map-cluster:hover { transform: scale(1.08); }
.map-cluster--md { width: 46px; height: 46px; }
.map-cluster--lg { width: 54px; height: 54px; font-size: var(--text-base); }

.user-marker { background: none; border: 0; }

/* --------------------------------------------------------------------------
   Marker preview card
   On mobile it sits above the sheet, not as a cramped Leaflet popup.
   -------------------------------------------------------------------------- */
.preview {
  position: absolute;
  left: max(var(--gutter), var(--safe-left));
  right: max(var(--gutter), var(--safe-right));
  bottom: calc(var(--sheet-peek, 7.5rem) + var(--space-3) + var(--safe-bottom));
  z-index: calc(var(--z-sheet) - 1);
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  text-decoration: none;
  color: inherit;
  opacity: 0;
  visibility: hidden;
  transform: translateY(0.75rem);
  transition:
    opacity var(--t-med) var(--ease-out),
    transform var(--t-med) var(--ease-out),
    visibility var(--t-med);
}
.preview.is-visible { opacity: 1; visibility: visible; transform: translateY(0); }

.preview__media {
  position: relative;
  flex: none;
  width: 5.5rem;
  height: 5.5rem;
  border-radius: var(--radius-md);
  overflow: hidden;
  background: var(--cream-200);
}
.preview__media img { width: 100%; height: 100%; object-fit: cover; }
.preview__body { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: 0.125rem; }
.preview__name {
  font-family: var(--font-display);
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--ink-900);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.preview__meta {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-2);
  font-size: var(--text-xs);
  color: var(--text-muted);
}
.preview__products {
  margin-top: var(--space-1);
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
  font-size: var(--text-sm);
}
.preview__close {
  position: absolute;
  top: -0.5rem;
  right: -0.5rem;
  width: 30px;
  height: 30px;
  min-height: 30px;
  padding: 0;
  border-radius: 50%;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-sm);
  display: grid;
  place-items: center;
}

@media (min-width: 64rem) {
  .preview {
    left: auto;
    right: calc(var(--gutter) + 0.5rem);
    width: 22rem;
    bottom: calc(var(--space-6) + 3.5rem);
  }
}

/* --------------------------------------------------------------------------
   Results sheet
   -------------------------------------------------------------------------- */
.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: var(--z-sheet);
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--explore-header-h, 8.5rem));
  background: var(--bg);
  border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
  box-shadow: var(--shadow-sheet);
  /* Snap offset in px, set by sheet.js. */
  transform: translate3d(0, var(--sheet-y, 100%), 0);
  transition: transform var(--t-slow) var(--ease-out);
  will-change: transform;
}
.sheet.is-dragging {
  transition: none;
  /* No text selection or scroll chaining mid-drag. */
  user-select: none;
}

.sheet__handle {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--gutter) var(--space-3);
  background: none;
  border: 0;
  border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
  width: 100%;
  text-align: center;
  /* The handle owns the drag gesture, so the browser must not also scroll. */
  touch-action: none;
  cursor: grab;
}
.sheet.is-dragging .sheet__handle { cursor: grabbing; }

.sheet__grip {
  width: 2.5rem;
  height: 0.25rem;
  border-radius: var(--radius-pill);
  background: var(--sand-300);
}
.sheet__count {
  margin: 0;
  font-family: var(--font-display);
  font-size: var(--text-md);
  font-weight: 600;
  letter-spacing: var(--tracking-tight);
  color: var(--ink-900);
}
.sheet__hint { margin: 0; font-size: var(--text-xs); color: var(--text-muted); }

.sheet__scroll {
  flex: 1 1 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 max(var(--gutter), var(--safe-left)) calc(var(--space-16) + var(--safe-bottom));
}
/* Only scrollable once expanded, or a drag on the list fights the sheet. */
.sheet[data-state="peek"] .sheet__scroll { overflow-y: hidden; }

.sheet__results {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: 1fr;
}
@media (min-width: 40rem) and (max-width: 63.99rem) {
  .sheet__results { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.sheet__footer {
  margin-top: var(--space-8);
  padding-top: var(--space-6);
  border-top: 1px solid var(--line);
  font-size: var(--text-sm);
  color: var(--text-muted);
}
.sheet__footer-links { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); }

/* --------------------------------------------------------------------------
   Map / List toggle
   -------------------------------------------------------------------------- */
.view-toggle {
  position: absolute;
  left: 50%;
  /* Above the sheet's peek, not on top of its count and hint. */
  bottom: calc(var(--sheet-peek, 7.5rem) + var(--space-3) + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: calc(var(--z-sheet) + 1);
  box-shadow: var(--shadow-lg);
  transition:
    bottom var(--t-med) var(--ease-out),
    opacity var(--t-med) var(--ease-out),
    visibility var(--t-med);
}

/* Once the list is open the handle is the way back to the map, so the pill
   would be a second control for the same job. */
.sheet[data-state="half"] ~ .view-toggle,
.sheet[data-state="full"] ~ .view-toggle {
  opacity: 0;
  visibility: hidden;
}
@media (min-width: 64rem) { .view-toggle { display: none; } }

/* --------------------------------------------------------------------------
   Desktop split view
   -------------------------------------------------------------------------- */
@media (min-width: 64rem) {
  .explore {
    display: grid;
    grid-template-columns: minmax(0, 40%) minmax(0, 60%);
    grid-template-rows: 100dvh;
  }

  .explore__map {
    position: relative;
    inset: auto;
    z-index: var(--z-map);
    isolation: isolate;
    grid-column: 2;
    grid-row: 1;
    /* Sticky map, scrollable list: the map never leaves the viewport. */
    height: 100dvh;
  }

  /* The sheet becomes the left column: no dragging, no rounded top, just a
     scrollable list under the header. */
  .sheet {
    position: relative;
    grid-column: 1;
    grid-row: 1;
    height: 100dvh;
    padding-top: var(--explore-header-h, 9.5rem);
    border-radius: 0;
    box-shadow: none;
    border-right: 1px solid var(--line);
    transform: none !important;
    transition: none;
  }
  .sheet__handle { display: none; }
  .sheet__results { grid-template-columns: 1fr; }
  .sheet__scroll,
  /* data-state is the mobile sheet's business; the desktop column always
     scrolls, or only the first screen of results is reachable. */
  .sheet[data-state="peek"] .sheet__scroll {
    padding-inline: var(--gutter);
    overflow-y: auto;
  }

  .explore-header {
    /* Header spans the list column only, so it does not dim the map. */
    right: 60%;
    background: var(--bg);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    border-bottom: 1px solid var(--line);
    border-right: 1px solid var(--line);
  }
}

@media (min-width: 90rem) {
  .explore { grid-template-columns: minmax(0, 36rem) minmax(0, 1fr); }
  .explore-header { right: auto; width: 36rem; }
}

/* --------------------------------------------------------------------------
   Desktop list/marker hover pairing
   -------------------------------------------------------------------------- */
@media (hover: hover) {
  .card.is-paired { border-color: var(--green-400); box-shadow: var(--shadow-lg); }
}

/* --------------------------------------------------------------------------
   Filters modal: a bottom sheet on mobile, a centred dialog on desktop
   -------------------------------------------------------------------------- */
.filters {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.filters[hidden] { display: none; }

.filters__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(19, 32, 26, 0.45);
  border: 0;
  padding: 0;
  animation: fade-in var(--t-med) var(--ease-out);
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes slide-up { from { transform: translateY(2rem); } to { transform: translateY(0); } }

.filters__panel {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: 92dvh;
  background: var(--bg);
  border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
  box-shadow: var(--shadow-lg);
  animation: slide-up var(--t-slow) var(--ease-out);
}
@media (min-width: 48rem) {
  .filters { align-items: center; padding: var(--space-6); }
  .filters__panel { max-width: 30rem; border-radius: var(--radius-2xl); }
}

.filters__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--line);
}
.filters__head h2 { font-size: var(--text-lg); }

.filters__body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  gap: var(--space-6);
}

.filters__foot {
  display: flex;
  gap: var(--space-3);
  align-items: center;
  padding: var(--space-4) var(--space-5) calc(var(--space-4) + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
  border-radius: 0 0 var(--radius-2xl) var(--radius-2xl);
}
.filters__foot .btn--primary { flex: 1 1 auto; }

.filter-group { display: flex; flex-direction: column; gap: var(--space-3); }
.filter-group__label {
  font-size: var(--text-sm);
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* Distance: large radio pills, easy to hit one-handed. */
.distance-options { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.distance-option {
  position: relative;
  flex: 1 1 4.5rem;
}
.distance-option input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.distance-option span {
  display: grid;
  place-items: center;
  min-height: 48px;
  padding: var(--space-2);
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  background: var(--bg-raised);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--ink-700);
  transition: all var(--t-fast) var(--ease-out);
}
.distance-option input:checked + span {
  background: var(--green-700);
  border-color: var(--green-700);
  color: #fff;
}
.distance-option input:focus-visible + span {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Toggles: a full-width row is a much bigger target than a checkbox. */
.toggle-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 56px;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--bg-raised);
  cursor: pointer;
  transition: border-color var(--t-fast) var(--ease-out), background-color var(--t-fast) var(--ease-out);
}
.toggle-row:hover { border-color: var(--green-300); }
.toggle-row__text { flex: 1 1 auto; }
.toggle-row__title { font-weight: 600; color: var(--ink-900); }
.toggle-row__hint { font-size: var(--text-sm); color: var(--text-muted); }
.toggle-row .icon { color: var(--green-600); }

.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-switch {
  flex: none;
  position: relative;
  width: 3rem;
  height: 1.75rem;
  border-radius: var(--radius-pill);
  background: var(--sand-200);
  transition: background-color var(--t-med) var(--ease-out);
}
.toggle-switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 1.375rem;
  height: 1.375rem;
  border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow-sm);
  transition: transform var(--t-med) var(--ease-spring);
}
.toggle-row input:checked ~ .toggle-switch { background: var(--green-600); }
.toggle-row input:checked ~ .toggle-switch::after { transform: translateX(1.25rem); }
.toggle-row input:focus-visible ~ .toggle-switch {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.toggle-row:has(input:checked) { border-color: var(--green-300); background: var(--green-50); }

/* --------------------------------------------------------------------------
   Typeahead
   -------------------------------------------------------------------------- */
.suggest {
  position: absolute;
  top: calc(100% + var(--space-2));
  left: 0;
  right: 0;
  z-index: calc(var(--z-header) + 1);
  max-height: 60dvh;
  overflow-y: auto;
  padding: var(--space-2);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}
.suggest[hidden] { display: none; }
.suggest__group + .suggest__group { margin-top: var(--space-2); }
.suggest__label {
  padding: var(--space-2) var(--space-3) var(--space-1);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.suggest__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  min-height: 48px;
  padding: var(--space-2) var(--space-3);
  border: 0;
  border-radius: var(--radius-md);
  background: none;
  text-align: left;
  text-decoration: none;
  color: var(--ink-900);
  cursor: pointer;
}
.suggest__item:hover,
.suggest__item[aria-selected="true"] { background: var(--green-50); }
.suggest__item .icon { color: var(--green-600); flex: none; }
.suggest__item-text { flex: 1 1 auto; min-width: 0; }
.suggest__item-label { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.suggest__item-sub { font-size: var(--text-sm); color: var(--text-muted); }

.search-pill { position: relative; }

/* --------------------------------------------------------------------------
   Empty state inside the sheet
   -------------------------------------------------------------------------- */
.sheet .empty-state { margin-top: var(--space-4); }
