:root {
  --ink: #0B0924;
  --ink-2: #4a4862;
  --ink-3: #7a7891;
  --paper: #ffffff;
  --mist: #F2F3F7;
  --line: rgba(11, 9, 36, 0.12);
  --r3: #1d6b45;
  --r2: #2c64a8;
  --r1: #7a6a2a;
  --gold: #b8862b;
  --focus: #3d7bff;
  --shadow: 0 6px 24px rgba(11, 9, 36, 0.14), 0 1px 3px rgba(11, 9, 36, 0.1);
  /* Inputs, chips and buttons sit on --field. Filled things (toast,
     primary button, badges) use --solid with white text. */
  --field: #ffffff;
  --solid: #0B0924;
  --solid-hover: #26234a;
  --map-bg: #e9edf2;
  --grip: #cfd1dc;
  --cluster: rgb(11, 9, 36);
  --cluster-ring: rgba(11, 9, 36, 0.35);
  --tag-r3: #e3f1e9;
  --tag-r2: #e4edf8;
  --tag-r1: #f3efdf;
  --tag-aff: #f7eedb;
  --tag-aff-text: #8a6116;
  --tiles: none;
  color-scheme: light;
  --panel-w: 380px;
  --radius: 14px;
  --font: Lexend, system-ui, -apple-system, "Segoe UI", sans-serif;
  --serif: Merriweather, Georgia, serif;
}

/* Dark mode follows the device setting unless the map's sun/moon button
   picked one (data-theme on <html>, applied by theme.js). Keep the two
   blocks below the same. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ink: #ecebf5;
    --ink-2: #b6b4c9;
    --ink-3: #8c8aa3;
    --paper: #15142a;
    --mist: #201f38;
    --line: rgba(255, 255, 255, 0.12);
    --r3: #3f9e6e;
    --r2: #5b8fd6;
    --r1: #b09a4a;
    --gold: #c99a3f;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5), 0 1px 3px rgba(0, 0, 0, 0.4);
    --field: #1b1a31;
    --solid: #4a4594;
    --solid-hover: #5a55a8;
    --map-bg: #1c1d24;
    --grip: #4a4962;
    --cluster: #4a4594;
    --cluster-ring: rgba(122, 116, 220, 0.38);
    --tag-r3: rgba(63, 158, 110, 0.18);
    --tag-r2: rgba(91, 143, 214, 0.18);
    --tag-r1: rgba(176, 154, 74, 0.18);
    --tag-aff: rgba(201, 154, 63, 0.18);
    --tag-aff-text: #dcb46a;
    --tiles: invert(1) hue-rotate(180deg) brightness(0.9) contrast(0.85) saturate(0.6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --ink: #ecebf5;
  --ink-2: #b6b4c9;
  --ink-3: #8c8aa3;
  --paper: #15142a;
  --mist: #201f38;
  --line: rgba(255, 255, 255, 0.12);
  --r3: #3f9e6e;
  --r2: #5b8fd6;
  --r1: #b09a4a;
  --gold: #c99a3f;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5), 0 1px 3px rgba(0, 0, 0, 0.4);
  --field: #1b1a31;
  --solid: #4a4594;
  --solid-hover: #5a55a8;
  --map-bg: #1c1d24;
  --grip: #4a4962;
  --cluster: #4a4594;
  --cluster-ring: rgba(122, 116, 220, 0.38);
  --tag-r3: rgba(63, 158, 110, 0.18);
  --tag-r2: rgba(91, 143, 214, 0.18);
  --tag-r1: rgba(176, 154, 74, 0.18);
  --tag-aff: rgba(201, 154, 63, 0.18);
  --tag-aff-text: #dcb46a;
  --tiles: invert(1) hue-rotate(180deg) brightness(0.9) contrast(0.85) saturate(0.6);
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  letter-spacing: 0.01em;
  color: var(--ink);
  background: var(--mist);
  overflow: hidden;
}

button,
input {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.skip-link {
  position: absolute;
  left: 8px;
  top: -40px;
  z-index: 3000;
  padding: 8px 12px;
  background: var(--solid);
  color: #fff;
  border-radius: 8px;
}

.skip-link:focus {
  top: 8px;
}

#map {
  position: fixed;
  inset: 0 0 0 var(--panel-w);
  background: var(--map-bg);
}

/* ---------- Panel ---------- */

.panel {
  position: fixed;
  inset: 0 auto 0 0;
  width: var(--panel-w);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  border-right: 1px solid var(--line);
  box-shadow: 2px 0 16px rgba(11, 9, 36, 0.06);
}

.sheet-handle {
  display: none;
}

.panel-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 18px 10px;
}

.panel-header h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.3rem;
  line-height: 1.2;
}

.tagline {
  margin: 2px 0 0;
  font-size: 0.82rem;
  color: var(--ink-2);
}

.search {
  position: relative;
  display: flex;
  gap: 8px;
  padding: 6px 18px 10px;
}

.search-icon {
  position: absolute;
  left: 30px;
  top: 50%;
  width: 18px;
  height: 18px;
  margin-top: -11px;
  fill: var(--ink-3);
  pointer-events: none;
}

#search {
  flex: 1;
  min-width: 0;
  height: 42px;
  padding: 0 12px 0 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--mist);
  transition: border-color 0.15s, background 0.15s;
}

#search:focus {
  outline: none;
  border-color: var(--ink);
  background: var(--field);
}

.icon-btn {
  flex: 0 0 42px;
  height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--field);
  cursor: pointer;
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: var(--ink);
}

.icon-btn:hover {
  background: var(--mist);
}

.icon-btn[aria-busy="true"] svg {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.filters {
  min-width: 0; /* fieldsets default to min-content */
  margin: 0;
  padding: 0 18px 12px;
  border: 0;
  border-bottom: 1px solid var(--line);
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
}

.chip input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.chip span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.82rem;
  background: var(--field);
  color: var(--ink-2);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.chip span b {
  display: inline-grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 0.72rem;
  background: var(--mist);
}

.chip input:checked + span {
  background: var(--solid);
  border-color: var(--solid);
  color: #fff;
}

.chip-r3 input:checked + span { background: var(--r3); border-color: var(--r3); }
.chip-r2 input:checked + span { background: var(--r2); border-color: var(--r2); }
.chip-r1 input:checked + span { background: var(--r1); border-color: var(--r1); }
.chip-aff input:checked + span { background: var(--gold); border-color: var(--gold); }

.chip input:checked + span b {
  background: rgba(255, 255, 255, 0.22);
}

.chip input:focus-visible + span {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.results-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 10px 18px 6px;
}

#count {
  margin: 0;
  font-size: 0.82rem;
  color: var(--ink-2);
}

.link-btn {
  padding: 0;
  border: 0;
  background: none;
  font-size: 0.82rem;
  color: var(--r2);
  cursor: pointer;
}

.link-btn:hover {
  text-decoration: underline;
}

.results {
  flex: 1;
  min-height: 0;
  margin: 0;
  padding: 0 10px 10px;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.results:focus {
  outline: none;
}

.result {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 10px;
  width: 100%;
  padding: 10px 8px;
  border: 0;
  border-radius: 10px;
  background: none;
  text-align: left;
  cursor: pointer;
}

.result:hover,
.result.is-active {
  background: var(--mist);
}

.result-name {
  display: block;
  font-weight: 500;
  line-height: 1.3;
}

.result-addr {
  display: block;
  margin-top: 2px;
  font-size: 0.8rem;
  color: var(--ink-2);
}

.result-dist {
  font-size: 0.75rem;
  color: var(--ink-3);
  white-space: nowrap;
  padding-top: 2px;
}

.result img {
  margin-top: 1px;
}

.results-more,
.results-empty {
  padding: 14px 8px;
  font-size: 0.82rem;
  color: var(--ink-2);
  text-align: center;
}

.badge {
  display: inline-grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  font-size: 0.8rem;
  font-weight: 600;
  color: #fff;
  background: var(--solid);
}

.badge.r3 { background: var(--r3); }
.badge.r2 { background: var(--r2); }
.badge.r1 { background: var(--r1); }

.about {
  border-top: 1px solid var(--line);
  padding: 0 18px;
  font-size: 0.85rem;
  color: var(--ink-2);
}

.about summary {
  padding: 12px 0;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
}

.about[open] {
  max-height: 50vh;
  overflow-y: auto;
  padding-bottom: 14px;
}

.about p {
  margin: 0 0 10px;
}

.about dl {
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 8px 10px;
  margin: 0 0 12px;
  align-items: center;
}

.about dd {
  margin: 0;
}

.about a {
  color: var(--r2);
}

.visits {
  margin: 0;
  padding: 0 18px 12px;
  font-size: 0.78rem;
  color: var(--ink-3);
}

/* ---------- Map chrome ---------- */

.leaflet-container {
  font-family: var(--font);
}

.leaflet-bar {
  border: 0 !important;
  box-shadow: var(--shadow) !important;
  border-radius: 10px !important;
  overflow: hidden;
}

.leaflet-bar a {
  width: 36px !important;
  height: 36px !important;
  line-height: 36px !important;
  color: var(--ink) !important;
  background: var(--paper) !important;
  border-bottom-color: var(--line) !important;
}

.leaflet-bar a:hover {
  background: var(--mist) !important;
}

.theme-btn {
  display: grid !important;
  place-items: center;
}

.theme-btn svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

/* OSM has no dark tiles, so dark mode inverts the light ones. */
.leaflet-tile-pane {
  filter: var(--tiles);
}

.leaflet-control-attribution {
  font-size: 10px;
  background: color-mix(in srgb, var(--paper) 80%, transparent) !important;
  color: var(--ink-2);
}

.leaflet-control-attribution a {
  color: var(--r2);
}

.leaflet-attribution-flag {
  display: none !important;
}

/* The pin's background matches its ring, so the seal's soft edge blends
   into the ring instead of showing a white fringe. Ring widths are whole
   pixels so the ring is even all the way round. */
.pin {
  border-radius: 50%;
  background: var(--ring, var(--ink));
  box-shadow:
    0 0 0 3px var(--ring, var(--ink)),
    0 0 0 7px var(--halo, rgba(11, 9, 36, 0.25)),
    0 2px 6px rgba(0, 0, 0, 0.35);
}

.pin-r3 { --ring: var(--r3); --halo: rgba(29, 107, 69, 0.3); }
.pin-r2 { --ring: var(--r2); --halo: rgba(44, 100, 168, 0.3); }
.pin-r1 { --ring: var(--r1); --halo: rgba(122, 106, 42, 0.3); }
.pin-aff { --ring: var(--gold); --halo: rgba(184, 134, 43, 0.35); }

.pin.is-active {
  box-shadow:
    0 0 0 3px var(--ring, var(--ink)),
    0 0 0 8px rgba(61, 123, 255, 0.45),
    0 3px 8px rgba(0, 0, 0, 0.4);
}

.cluster {
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  font-weight: 600;
  background: var(--cluster);
  box-shadow: 0 0 0 6px var(--cluster-ring), 0 2px 8px rgba(0, 0, 0, 0.35);
}

.cluster-s { font-size: 0.75rem; }
.cluster-m { font-size: 0.875rem; }
.cluster-l { font-size: 1.05rem; }

/* "Near me" and searched ZIP or town locations. */
.loc-dot {
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

.loc-you {
  background: #3d7bff;
}

.loc-place {
  background: var(--solid);
}

/* ---------- Popup ---------- */

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--paper);
  color: var(--ink);
}

.leaflet-popup-content-wrapper {
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0;
}

.leaflet-popup-content {
  margin: 0;
  width: min(300px, 78vw) !important;
  font-size: 0.85rem;
  line-height: 1.45;
}

.leaflet-container a.leaflet-popup-close-button {
  top: 8px;
  right: 8px;
  width: 26px;
  height: 26px;
  font: 20px/26px var(--font);
  color: var(--ink-3);
  border-radius: 50%;
}

.leaflet-container a.leaflet-popup-close-button:hover {
  background: var(--mist);
  color: var(--ink);
}

.pop {
  padding: 14px 16px 14px;
}

.pop h2 {
  margin: 0 28px 4px 0;
  font-size: 1.02rem;
  font-weight: 600;
  line-height: 1.3;
}

.pop-addr {
  margin: 0 0 10px;
  color: var(--ink-2);
}

.pop-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 24px;
  padding: 0 9px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 500;
  background: var(--mist);
  color: var(--ink);
}

.tag.r3 { background: var(--tag-r3); color: var(--r3); }
.tag.r2 { background: var(--tag-r2); color: var(--r2); }
.tag.r1 { background: var(--tag-r1); color: var(--r1); }
.tag.aff { background: var(--tag-aff); color: var(--tag-aff-text); }

.pop-actions {
  display: flex;
  gap: 6px;
}

.leaflet-container .pop-actions a,
.pop-actions button {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--field);
  color: var(--ink);
  font-size: 0.8rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}

.leaflet-container .pop-actions a.primary {
  background: var(--solid);
  border-color: var(--solid);
  color: #fff;
}

.pop-actions a:hover,
.pop-actions button:hover {
  background: var(--mist);
}

.leaflet-container .pop-actions a.primary:hover {
  background: var(--solid-hover);
}

.pop-site {
  display: block;
  margin: -4px 0 12px;
  font-size: 0.8rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaflet-container .pop-site {
  color: var(--r2);
}

.toast {
  position: fixed;
  left: calc((100vw + var(--panel-w)) / 2);
  bottom: 24px;
  z-index: 2500;
  transform: translateX(-50%);
  padding: 9px 16px;
  border-radius: 999px;
  background: var(--solid);
  color: #fff;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
}

/* ---------- Mobile: panel becomes a bottom sheet ---------- */

@media (max-width: 760px) {
  #map {
    inset: 0;
  }

  .panel {
    inset: auto 0 0 0;
    width: auto;
    height: 88dvh;
    border-right: 0;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -4px 24px rgba(11, 9, 36, 0.18);
    transform: translateY(calc(100% - var(--peek)));
    transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
    --peek: 178px;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .panel[data-state="half"] {
    transform: translateY(45%);
  }

  .panel[data-state="full"] {
    transform: none;
  }

  .sheet-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    width: 100%;
    padding: 8px 0 4px;
    border: 0;
    background: none;
    cursor: pointer;
  }

  .sheet-grip {
    width: 40px;
    height: 5px;
    border-radius: 999px;
    background: var(--grip);
  }

  .sheet-label {
    font-size: 0.85rem;
    font-weight: 500;
  }

  .panel-header {
    display: none;
  }

  .search {
    padding-top: 4px;
  }

  #search {
    font-size: 16px; /* stops iOS zooming on focus */
  }

  .results-head {
    display: none;
  }

  .toast {
    left: 50%;
    bottom: calc(var(--peek, 178px) + 16px);
  }

  .leaflet-bottom.leaflet-right,
  .leaflet-bottom.leaflet-left {
    bottom: 170px;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
