/* CardinalMap — mobile-first PWA */
:root {
  --bg:    #ffffff;
  --bg-2:  #f5f7fa;
  --bg-3:  #e9edf2;            /* tertiary surface (search result hover, sheet header) */
  --fg:    #0d1117;            /* primary text — near-black for AA on white */
  --fg-2:  #4a5260;            /* secondary text — bumped from #555 for AA contrast */
  --fg-3:  #6b7280;            /* tertiary text / placeholder */
  --line:  #d6dbe1;            /* separators, sheet popups */
  --accent:#0a84ff;
  --accent-2:#ffd166;
  --shadow: 0 6px 18px rgba(0,0,0,.18);
  --sheet-collapsed-h: 110px;
  --sheet-peek-h: 38vh;
  --sheet-full-h: 82vh;
  --topbar-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 14px;
  --topbar-max: 720px;
}

/* ---------- DARK MODE ----------
 * Bumped fg, fg-2 to higher contrast against the dark background so text is
 * legible everywhere (sheet, popup, compass, search results). Previously
 * fg=#f4f6f8 / fg-2=#aab1b8 — readable, but the popup against the map
 * (where the bg behind it varies) was muddy.
 */
@media (prefers-color-scheme: dark) {
  :root {
    --bg:    #0d1117;
    --bg-2:  #1b2027;
    --bg-3:  #252b34;
    --fg:    #f7f9fb;            /* primary text — near-white */
    --fg-2:  #d2d8df;            /* secondary text — much brighter than before */
    --fg-3:  #9aa3ad;
    --line:  #30363d;
    --accent:#7cc4ff;            /* brighter accent so it pops against dark */
    --accent-2:#ffd166;
    --shadow: 0 8px 24px rgba(0,0,0,.65);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; width: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  color: var(--fg);
  background: var(--bg);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
#map { position: fixed; inset: 0; z-index: 0; }

/* Topbar — capped on desktop so search doesn't span the whole screen */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 8px) 12px 8px 12px;
  background: linear-gradient(180deg, rgba(0,0,0,.35), rgba(0,0,0,0));
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.topbar-wrap {
  width: 100%;
  max-width: var(--topbar-max);
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.icon-btn {
  background: var(--bg); color: var(--fg); border: none; border-radius: 999px;
  width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow); cursor: pointer; flex-shrink: 0;
}
.icon-btn:active { transform: scale(.96); }
.search-wrap { position: relative; flex: 1; min-width: 0; }
#search-input {
  width: 100%; height: 40px; padding: 0 14px;
  border: none; border-radius: 999px; outline: none;
  background: var(--bg); color: var(--fg);
  font: inherit;
  box-shadow: var(--shadow);
}
#search-input::placeholder { color: var(--fg-3); }

.search-results {
  position: absolute; top: 46px; left: 0; right: 0;
  margin: 0; padding: 6px 0; list-style: none;
  background: var(--bg); border-radius: var(--radius);
  box-shadow: var(--shadow); max-height: 60vh; overflow: auto;
  z-index: 20;
  color: var(--fg);
}
.search-results li {
  padding: 10px 14px; cursor: pointer;
  border-bottom: 1px solid var(--line);
  color: var(--fg);
}
.search-results li:last-child { border-bottom: 0; }
.search-results li:hover, .search-results li:active { background: var(--bg-3); }

/* City / County segmented toggle — sits next to the search input */
.seg-toggle {
  display: inline-flex;
  background: var(--bg);
  border-radius: 999px;
  box-shadow: var(--shadow);
  height: 40px;
  padding: 4px;
  flex-shrink: 0;
  gap: 2px;
}
.seg-btn {
  background: transparent;
  color: var(--fg-2);
  border: none;
  padding: 0 12px;
  border-radius: 999px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  min-width: 56px;
  transition: background .15s ease, color .15s ease;
}
.seg-btn:hover { color: var(--fg); }
.seg-btn.active {
  background: var(--accent);
  color: #ffffff;
  box-shadow: 0 1px 3px rgba(0,0,0,.25);
}
@media (prefers-color-scheme: dark) {
  .seg-btn.active { color: #0d1117; }   /* dark text on light accent in dark mode */
}

/* Compass — pinned to the left, above the bottom sheet */
.compass {
  position: fixed; left: 12px; bottom: calc(var(--sheet-collapsed-h) + var(--safe-bottom) + 12px);
  z-index: 5; display: grid;
  grid-template-columns: 36px 36px 36px;
  grid-template-rows: 36px 36px 36px;
  gap: 2px;
  background: var(--bg); border-radius: 12px;
  padding: 6px;
  box-shadow: var(--shadow);
}
.compass .dir {
  background: transparent; border: none; cursor: pointer;
  color: var(--fg); border-radius: 8px;
  font-weight: 700; font-size: 12px;
  display: flex; align-items: center; justify-content: center;
}
.compass .dir:active { background: var(--bg-2); }
.compass .dir.active { background: var(--accent); color: #fff; }
.compass .dir.center { font-size: 18px; color: var(--fg-2); }

/* Bottom sheet — taller collapsed state so the first row is always visible */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  background: var(--bg); border-top-left-radius: 18px; border-top-right-radius: 18px;
  box-shadow: var(--shadow);
  height: var(--sheet-collapsed-h);
  transition: height .25s ease;
  z-index: 6;
  padding: 0 14px calc(var(--safe-bottom) + 14px) 14px;
  display: flex; flex-direction: column;
  touch-action: pan-y;
  max-width: var(--topbar-max);
  margin: 0 auto;
  color: var(--fg);
}
.sheet.expanded { height: var(--sheet-full-h); }
.sheet.peek { height: var(--sheet-peek-h); }
.sheet-handle {
  width: 44px; height: 4px; background: var(--fg-2); opacity: .55;
  border-radius: 4px; margin: 8px auto 6px;
}
.sheet-header {
  display: flex; align-items: center; gap: 10px;
  padding-bottom: 8px; border-bottom: 1px solid var(--line);
}
.sheet-title {
  font-weight: 700; font-size: 16px; color: var(--fg);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.sheet-subtitle {
  font-size: 12px; color: var(--fg-2); margin-top: 2px;
}
.pill {
  background: var(--bg-3); color: var(--fg);
  border-radius: 999px; padding: 4px 10px; font-size: 12px; font-weight: 600;
  flex-shrink: 0;
}
.sheet-toggle {
  background: transparent; border: none; cursor: pointer; color: var(--fg-2);
  width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 999px; font-size: 18px;
}
.sheet-toggle:hover  { background: var(--bg-2); color: var(--fg); }
.sheet-toggle:active { background: var(--bg-3); }
.sheet-list { list-style: none; margin: 0; padding: 8px 0 0 0; overflow: auto; flex: 1; }
.sheet-list li {
  padding: 10px 4px; border-bottom: 1px solid var(--line);
  display: flex; gap: 10px; align-items: flex-start; cursor: pointer;
  color: var(--fg);
}
.sheet-list li:active { background: var(--bg-2); }
.sheet-list .name { font-weight: 700; color: var(--fg); }
.sheet-list .meta { font-size: 12px; color: var(--fg-2); margin-top: 2px; }

/* Skeleton loaders */
.skeleton-row { display: flex; gap: 10px; padding: 10px 4px; }
.skeleton {
  background: linear-gradient(90deg, var(--bg-2) 0%, var(--bg-3) 50%, var(--bg-2) 100%);
  background-size: 200% 100%;
  animation: skel 1.2s ease-in-out infinite;
  height: 14px; border-radius: 6px; flex: 1;
}
.skeleton.small { flex: 0 0 40%; }
@keyframes skel { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* MapLibre controls */
.maplibregl-ctrl-top-right, .maplibregl-ctrl-top-left { display: none; }
.maplibregl-ctrl-bottom-right { margin-bottom: calc(var(--sheet-collapsed-h) + var(--safe-bottom) + 8px); }
.maplibregl-ctrl-attrib { background: rgba(0,0,0,.55); color: #fff; }
.maplibregl-ctrl-attrib a { color: #d2d8df; }

/* POI popup */
.maplibregl-popup-content {
  background: var(--bg);
  color: var(--fg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 12px 14px;
  font-size: 14px;
  border: 1px solid var(--line);
}
.maplibregl-popup-tip {
  border-top-color: var(--bg) !important;
  border-bottom-color: var(--bg) !important;
}
.poi-popup { line-height: 1.4; }
.poi-popup .popup-name  { font-weight: 700; font-size: 15px; margin-bottom: 2px; color: var(--fg); }
.poi-popup .popup-brand { font-size: 12px; color: var(--fg-2); margin-bottom: 4px; }
.poi-popup .popup-addr  { font-size: 13px; margin-bottom: 4px; color: var(--fg); }
.poi-popup .popup-cat   { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; margin-bottom: 4px; font-weight: 700; }
.poi-popup .popup-dist  { font-size: 12px; color: var(--fg-2); margin-bottom: 4px; }
.poi-popup .popup-hours { font-size: 12px; margin-bottom: 4px; color: var(--fg); }
.poi-popup a { color: var(--accent); text-decoration: none; font-weight: 600; }
.poi-popup a:hover { text-decoration: underline; }

/* Toast */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sheet-collapsed-h) + var(--safe-bottom) + 16px);
  background: var(--fg); color: var(--bg);
  padding: 8px 14px; border-radius: 999px;
  font-size: 13px; box-shadow: var(--shadow);
  z-index: 30; max-width: 80vw; text-align: center;
}

@media (min-width: 800px) {
  .sheet { left: 50%; transform: translateX(-50%); width: var(--topbar-max); }
  .maplibregl-ctrl-bottom-right { right: calc(50vw - var(--topbar-max)/2 + 8px) !important; }
}