:root {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #f4f5f7;
  --text: #1f2933;
  --text-dim: #66727d;
  --border: #e2e6ea;
  --accent: #e2603f;
  --accent-ink: #ffffff;
  --shadow: 0 6px 24px rgba(20, 28, 38, .16);
  --shadow-sm: 0 2px 10px rgba(20, 28, 38, .12);
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --topbar-h: 48px;
  --topbar-bottom: calc(var(--safe-top) + 10px + var(--topbar-h));
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10151b;
    --surface: #1b232c;
    --surface-2: #242e39;
    --text: #eef1f4;
    --text-dim: #9aa7b2;
    --border: #313d49;
    --shadow: 0 6px 24px rgba(0, 0, 0, .45);
    --shadow-sm: 0 2px 10px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

/* `hidden` must always win, even over components that set their own display */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 0;
  background: var(--surface-2);
}

button { font: inherit; }

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

/* ---------- Top bar ---------- */
.topbar {
  position: absolute;
  top: calc(var(--safe-top) + 10px);
  left: 10px;
  right: 10px;
  z-index: 1000;
  display: flex;
  gap: 8px;
  align-items: center;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }

.search {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  height: var(--topbar-h);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.search__icon {
  width: 20px;
  height: 20px;
  margin: 0 6px 0 12px;
  flex-shrink: 0;
  fill: var(--text-dim);
}
.search input {
  flex: 1;
  min-width: 0;
  height: 100%;
  border: 0;
  background: transparent;
  font-size: 16px; /* < 16px makes iOS zoom on focus */
  color: var(--text);
  outline: none;
  padding-right: 6px;
}
.search input::placeholder { color: var(--text-dim); }
.search input::-webkit-search-decoration,
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }

.search__clear {
  border: 0;
  background: var(--surface-2);
  color: var(--text-dim);
  width: 26px;
  height: 26px;
  margin-right: 9px;
  border-radius: 50%;
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.iconbtn {
  position: relative;
  width: var(--topbar-h);
  height: var(--topbar-h);
  flex-shrink: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  cursor: pointer;
}
.iconbtn svg { width: 21px; height: 21px; fill: var(--text); }
.iconbtn.is-on { background: var(--accent); }
.iconbtn.is-on svg { fill: var(--accent-ink); }

.badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  border: 2px solid var(--surface);
  font-size: 11px;
  font-weight: 700;
  line-height: 14px;
  text-align: center;
}
.iconbtn.is-on .badge { background: var(--surface); color: var(--accent); border-color: var(--accent); }

.linkbtn {
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
  padding: 6px 2px;
  cursor: pointer;
}

/* ---------- Search results ---------- */
.results {
  position: absolute;
  top: calc(var(--topbar-bottom) + 6px);
  left: 10px;
  right: 10px;
  z-index: 1000;
  max-height: 60vh;
  max-height: 60dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.results__item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: transparent;
  padding: 12px 14px;
  cursor: pointer;
  color: var(--text);
}
.results__item:last-child { border-bottom: 0; }
.results__item:hover { background: var(--surface-2); }
.results__line { font-size: 15px; font-weight: 600; }
.results__line .g { color: var(--accent); }
.results__meta { font-size: 13px; color: var(--text-dim); margin-top: 2px; }
.results__empty { padding: 16px 14px; color: var(--text-dim); font-size: 14px; }

/* ---------- Panels (grade filter + boulder sheet) ----------
   Bottom sheet on phones, anchored panel on wide screens. */
.panel {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1100;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow);
  padding: 6px 18px calc(var(--safe-bottom) + 16px);
  max-height: 62vh;
  max-height: 62dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: panel-up .2s ease-out;
}
@keyframes panel-up { from { transform: translateY(100%); } to { transform: translateY(0); } }
@media (prefers-reduced-motion: reduce) { .panel { animation: none; } }

.panel__grip {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 6px auto 10px;
}
.panel__close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: var(--surface-2);
  color: var(--text-dim);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.panel__head h2 { margin: 0; font-size: 17px; font-weight: 700; }
.panel__note { margin: 12px 0 2px; font-size: 13px; color: var(--text-dim); }

/* ---------- Grade grid ----------
   An auto-fitting grid: it reflows to however many columns the panel can hold,
   so it never clips and never scrolls sideways — at any screen width, with any
   number of grades. Every chip keeps the same width, including the last row. */
.grades {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(62px, 1fr));
  gap: 8px;
  margin-top: 12px;
}
.chip {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 11px 6px;
  font-size: 14px;
  font-weight: 600;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  text-align: center;
}
.chip.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

/* ---------- Stat line & toast ---------- */
.statline {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 12px);
  transform: translateX(-50%);
  z-index: 800;
  background: var(--surface);
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
  pointer-events: none;
  white-space: nowrap;
}
.toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 48px);
  transform: translateX(-50%);
  z-index: 1300;
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  pointer-events: none;
  white-space: nowrap;
}

/* ---------- Floating action button ---------- */
.fab {
  position: absolute;
  right: 14px;
  bottom: calc(var(--safe-bottom) + 44px);
  z-index: 900;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: grid;
  place-items: center;
}
.fab svg { width: 23px; height: 23px; fill: var(--text); }
.fab.is-active svg { fill: var(--accent); }
.fab.is-busy { opacity: .6; }

/* ---------- Markers ---------- */
.mello-marker {
  width: 34px;
  height: 34px;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, .45));
}
/* Selected boulder: a static accent glow that follows the stone silhouette
   (drop-shadow respects alpha, unlike a border/outline around the box). */
.mello-marker--active {
  filter:
    drop-shadow(0 0 3px var(--accent))
    drop-shadow(0 0 7px var(--accent))
    drop-shadow(0 2px 3px rgba(0, 0, 0, .5));
}
.mello-cluster {
  width: 36px;
  height: 36px;
  background: var(--accent);
  color: #fff;
  border: 3px solid rgba(255, 255, 255, .9);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 14px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .35);
}

/* ---------- Boulder sheet body ---------- */
.sheet__head { padding-right: 40px; }
.sheet__head h2 { margin: 0; font-size: 19px; font-weight: 700; line-height: 1.25; }
.sheet__sector { margin: 3px 0 0; font-size: 13px; color: var(--text-dim); }
.sheet__lines { margin: 14px 0 0; display: flex; flex-direction: column; gap: 8px; }

.line {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
  border: 1px solid transparent;
}
.line:hover { border-color: var(--accent); }
.line__grade {
  flex-shrink: 0;
  min-width: 42px;
  text-align: center;
  font-weight: 700;
  font-size: 13px;
  color: var(--accent);
}
.line__name { flex: 1; font-size: 15px; font-weight: 600; }
.line__play { width: 20px; height: 20px; fill: var(--text-dim); flex-shrink: 0; }

.sheet__actions { display: flex; gap: 8px; margin-top: 14px; }
.actionbtn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.actionbtn svg { width: 17px; height: 17px; fill: var(--accent); }

/* ---------- List view ---------- */
.listview {
  position: absolute;
  inset: 0;
  z-index: 1200;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}
.listview__bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 14px 10px;
  border-bottom: 1px solid var(--border);
}
.listview__bar h2 { margin: 0; font-size: 18px; }
.listview__body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 6px 0 calc(var(--safe-bottom) + 24px);
}
.listview__empty { padding: 28px 18px; color: var(--text-dim); font-size: 14px; text-align: center; }

.sector { border-bottom: 1px solid var(--border); }
.sector__head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 14px 16px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  text-align: left;
}
.sector__count { font-size: 12px; font-weight: 600; color: var(--text-dim); white-space: nowrap; }
.sector__body { padding: 0 16px 10px; }
.sector.is-collapsed .sector__body { display: none; }
.sector__head .caret { transition: transform .15s; fill: var(--text-dim); width: 16px; height: 16px; vertical-align: -3px; }
.sector.is-collapsed .sector__head .caret { transform: rotate(-90deg); }

.blk { padding: 8px 0; }
.blk__name {
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  padding: 0;
  cursor: pointer;
  text-align: left;
}
.blk__name:hover { color: var(--accent); }
.blk__lines { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.blk__line {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}
.blk__line .g { font-weight: 700; color: var(--accent); }

/* ---------- Wide screens ---------- */
@media (min-width: 720px) {
  .topbar { left: 12px; right: auto; width: 400px; }
  .results { left: 12px; right: auto; width: 400px; }

  /* panels become anchored cards instead of bottom sheets */
  .panel {
    left: 12px;
    right: auto;
    width: 400px;
    border-radius: 16px;
    animation: none;
  }
  .panel--filter {
    top: calc(var(--topbar-bottom) + 6px);
    bottom: auto;
    max-height: min(70vh, 520px);
  }
  .panel--sheet {
    bottom: 12px;
    max-height: min(70vh, 620px);
  }

  .fab { width: 46px; height: 46px; bottom: calc(var(--safe-bottom) + 28px); }
  .statline { left: auto; right: 14px; transform: none; }
  .toast { left: auto; right: 14px; transform: none; bottom: calc(var(--safe-bottom) + 60px); }
  .listview {
    left: 12px;
    top: 12px;
    bottom: 12px;
    right: auto;
    width: 420px;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: var(--shadow);
  }
}

/* ---------- Leaflet tweaks ---------- */
.leaflet-control-zoom { display: none; }
@media (min-width: 720px) { .leaflet-control-zoom { display: block; } }
.leaflet-control-attribution {
  font-size: 10px;
  background: rgba(255, 255, 255, .7);
}
.leaflet-tooltip {
  font-weight: 600;
  border: 0;
  box-shadow: var(--shadow-sm);
}
@media (prefers-color-scheme: dark) {
  .leaflet-control-attribution { background: rgba(20, 25, 30, .7); }
  .leaflet-control-attribution a { color: #9aa7b2; }
  .leaflet-tooltip { background: var(--surface); color: var(--text); }
  .leaflet-tooltip-top::before { border-top-color: var(--surface); }
}
