/* ---------------------------------------------------------------- tokens */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text: #14181f;
  --text-dim: #5a6473;
  --border: #dfe3ea;
  --accent: #1d4ed8;
  --accent-soft: #e8eeff;
  --warn-bg: #fff8e6;
  --warn-border: #e8c86a;
  --crisis-bg: #fdf0f3;
  --crisis-border: #e3a9b8;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .07);
  --radius: 10px;

  --c-mental-health-clinic: #2563eb;
  --c-private-practice: #7c3aed;
  --c-sud-outpatient: #059669;
  --c-sud-residential: #0891b2;
  --c-sud-detox: #b45309;
  --c-ipv-support: #db2777;
  --c-apip: #b91c1c;
  --c-parenting: #ea580c;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1319;
    --surface: #171c24;
    --surface-2: #1e242e;
    --text: #e8ecf2;
    --text-dim: #9aa5b4;
    --border: #2a3240;
    --accent: #7ea2ff;
    --accent-soft: #1b2740;
    --warn-bg: #2b2412;
    --warn-border: #6b5726;
    --crisis-bg: #2a161d;
    --crisis-border: #6d3547;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);

    --c-mental-health-clinic: #6b9bff;
    --c-private-practice: #a983f7;
    --c-sud-outpatient: #34d399;
    --c-sud-residential: #38bdf8;
    --c-sud-detox: #f0a63c;
    --c-ipv-support: #f472b6;
    --c-apip: #f87171;
    --c-parenting: #fb923c;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

h1, h2, h3 { margin: 0; line-height: 1.25; }

button { font: inherit; cursor: pointer; }

a { color: var(--accent); }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 2000;
  background: var(--surface); padding: .6rem 1rem; border-radius: 0 0 var(--radius) 0;
}
.skip-link:focus { left: 0; }

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

/* ---------------------------------------------------------------- topbar */
.topbar {
  flex: none;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .7rem clamp(.75rem, 2vw, 1.25rem);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: max(.7rem, env(safe-area-inset-top));
}
.topbar__title h1 { font-size: 1.05rem; letter-spacing: -.01em; }
.topbar__title p { margin: .15rem 0 0; font-size: .78rem; color: var(--text-dim); }

.crisis-toggle {
  flex: none;
  background: var(--crisis-bg);
  border: 1px solid var(--crisis-border);
  color: var(--text);
  padding: .45rem .8rem; border-radius: 999px;
  font-size: .82rem; font-weight: 600;
}
.crisis-toggle:hover { filter: brightness(.97); }

.crisis {
  flex: none;
  background: var(--crisis-bg);
  border-bottom: 1px solid var(--crisis-border);
  padding: .85rem clamp(.75rem, 2vw, 1.25rem);
  max-height: 40vh; overflow: auto;
}
.crisis h2 { font-size: .9rem; margin-bottom: .4rem; }
.crisis ul { margin: 0; padding-left: 1.1rem; font-size: .87rem; }
.crisis li { margin: .15rem 0; }
.crisis__note { margin: .5rem 0 0; font-size: .8rem; color: var(--text-dim); }

/* ---------------------------------------------------------------- layout */
.layout {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
}

.panel {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.panel__controls {
  flex: none;
  padding: .8rem clamp(.7rem, 1.5vw, 1rem);
  border-bottom: 1px solid var(--border);
  display: grid; gap: .7rem;
}

/* search */
.search { position: relative; }
.search input {
  width: 100%; padding: .55rem 2rem .55rem .7rem;
  font: inherit; color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius);
}
.search input::placeholder { color: var(--text-dim); }
.search__clear {
  position: absolute; right: .3rem; top: 50%; transform: translateY(-50%);
  background: none; border: 0; color: var(--text-dim);
  font-size: 1.2rem; line-height: 1; padding: .2rem .4rem; border-radius: 6px;
}

/* filters */
.filters { border: 0; margin: 0; padding: 0; }
.filters legend {
  display: flex; align-items: baseline; justify-content: space-between;
  width: 100%; padding: 0 0 .4rem;
  font-size: .72rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-dim);
}
.filters__actions { display: flex; gap: .35rem; }
.filters__actions button {
  background: none; border: 1px solid var(--border); border-radius: 6px;
  color: var(--text-dim); font-size: .68rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em; padding: .15rem .4rem;
}
.filters__actions button:hover { color: var(--text); border-color: var(--text-dim); }

.filter {
  display: flex; align-items: center; gap: .5rem;
  padding: .28rem .4rem; border-radius: 7px; cursor: pointer;
  font-size: .855rem;
}
.filter:hover { background: var(--surface-2); }
.filter input { accent-color: var(--accent); margin: 0; flex: none; }
.filter__dot {
  width: 11px; height: 11px; border-radius: 50%; flex: none;
  background: var(--dot); box-shadow: 0 0 0 2px color-mix(in srgb, var(--dot) 25%, transparent);
}
.filter__label { flex: 1; min-width: 0; }
.filter__n {
  flex: none; font-size: .74rem; color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.locate {
  width: 100%; padding: .5rem; border-radius: var(--radius);
  background: var(--accent-soft); color: var(--accent);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  font-size: .84rem; font-weight: 600;
}
.locate:hover { filter: brightness(.97); }
.locate[disabled] { opacity: .6; cursor: progress; }

/* meta */
.panel__meta {
  flex: none; display: flex; justify-content: space-between; gap: .5rem;
  padding: .5rem clamp(.7rem, 1.5vw, 1rem);
  font-size: .76rem; color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}

/* results */
.results {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  list-style: none; margin: 0; padding: .4rem;
}
.results:focus { outline: none; }

.card {
  width: 100%; text-align: left;
  background: none; border: 1px solid transparent; border-radius: var(--radius);
  padding: .55rem .6rem; display: grid; gap: .25rem;
  border-bottom: 1px solid var(--border);
}
.card:hover { background: var(--surface-2); }
.card[aria-current="true"] {
  background: var(--accent-soft);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
}
.card__name {
  font-weight: 650; font-size: .89rem; color: var(--text);
  display: flex; align-items: baseline; gap: .4rem;
}
.card__name::before {
  content: ""; width: 9px; height: 9px; border-radius: 50%;
  flex: none; background: var(--dot); transform: translateY(-1px);
}
.card__addr { font-size: .79rem; color: var(--text-dim); }
.card__dist {
  font-size: .74rem; color: var(--accent); font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.card__tags { display: flex; flex-wrap: wrap; gap: .25rem; margin-top: .1rem; }

.tag {
  font-size: .68rem; font-weight: 600; line-height: 1.6;
  padding: 0 .4rem; border-radius: 4px;
  color: var(--dot);
  background: color-mix(in srgb, var(--dot) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--dot) 30%, transparent);
}

.empty { padding: 2rem 1rem; text-align: center; color: var(--text-dim); font-size: .86rem; }

/* footer */
.panel__footer {
  flex: none; padding: .7rem clamp(.7rem, 1.5vw, 1rem);
  border-top: 1px solid var(--border);
  font-size: .72rem; color: var(--text-dim);
}
.panel__footer p { margin: 0 0 .4rem; }
.panel__footer p:last-child { margin-bottom: 0; }
.panel__warn {
  background: var(--warn-bg); border: 1px solid var(--warn-border);
  border-radius: 8px; padding: .45rem .55rem; color: var(--text);
}
.panel__footer details { margin-top: .45rem; }
.panel__footer summary {
  cursor: pointer; font-weight: 600; color: var(--text-dim);
  list-style: none; display: flex; align-items: center; gap: .3rem;
}
.panel__footer summary::-webkit-details-marker { display: none; }
.panel__footer summary::before { content: "▸"; font-size: .7em; }
.panel__footer details[open] summary { margin-bottom: .35rem; }
.panel__footer details[open] summary::before { content: "▾"; }

/* ---------------------------------------------------------------- map */
.map-wrap { position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: var(--surface-2); }

.panel-toggle { display: none; }

/* leaflet overrides */
.leaflet-container { font: inherit; background: var(--surface-2); }
.leaflet-popup-content-wrapper {
  background: var(--surface); color: var(--text);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.leaflet-popup-tip { background: var(--surface); }
.leaflet-popup-content { margin: .75rem .85rem; width: 268px !important; }
.leaflet-popup-close-button { color: var(--text-dim) !important; }
.leaflet-bar a, .leaflet-control-attribution {
  background: var(--surface); color: var(--text);
}
.leaflet-bar a { border-bottom-color: var(--border); }
.leaflet-bar a:hover { background: var(--surface-2); color: var(--text); }
.leaflet-control-attribution a { color: var(--accent); }
@media (prefers-color-scheme: dark) {
  .leaflet-tile { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); }
}

.marker { border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); }
@media (prefers-color-scheme: dark) { .marker { border-color: #10151c; } }
.marker--active { box-shadow: 0 0 0 5px color-mix(in srgb, var(--dot) 35%, transparent); }

/* popup */
.pop__name { font-size: .95rem; font-weight: 700; margin-bottom: .3rem; }
.pop__tags { display: flex; flex-wrap: wrap; gap: .25rem; margin-bottom: .45rem; }
.pop__row { font-size: .82rem; margin: .18rem 0; }
.pop__row--dim { color: var(--text-dim); }
.pop__programs {
  margin: .45rem 0 .25rem; padding-left: 1.05rem;
  font-size: .78rem; color: var(--text-dim);
}
.pop__programs li { margin: .1rem 0; }
.pop__note {
  font-size: .77rem; color: var(--text-dim);
  border-left: 2px solid var(--border); padding-left: .5rem; margin: .4rem 0;
}
.pop__links { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .55rem; }
.pop__links a {
  font-size: .78rem; font-weight: 600; text-decoration: none;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 6px; padding: .25rem .5rem; color: var(--accent);
}
.pop__links a:hover { background: var(--accent-soft); }
.pop__src {
  margin-top: .5rem; padding-top: .4rem; border-top: 1px solid var(--border);
  font-size: .68rem; color: var(--text-dim);
}

/* ---------------------------------------------------------------- mobile */
@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 1fr; position: relative; }

  .map-wrap { grid-area: 1 / 1; }

  .panel {
    grid-area: 1 / 1; z-index: 500;
    align-self: end;
    width: 100%; height: 68%;
    border-right: 0; border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    box-shadow: var(--shadow);
    transition: transform .22s ease;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .panel[data-collapsed="true"] { transform: translateY(100%); }

  /* Keep the caveat and the terms link; the sources disclosure is the part that
     costs too much room in a bottom sheet. */
  .panel__footer { padding: .5rem .7rem; }
  .panel__footer details { display: none; }

  /* The stacked checkbox list is too tall for a bottom sheet — lay the service
     types out as a single horizontally scrolling row of chips instead. */
  .panel__controls { gap: .55rem; padding: .6rem .7rem; }

  .filters legend { padding-bottom: .3rem; }

  #filter-list {
    display: flex; gap: .35rem;
    overflow-x: auto; overscroll-behavior-x: contain;
    padding-bottom: .25rem;
    scrollbar-width: none;
  }
  #filter-list::-webkit-scrollbar { display: none; }

  .filter {
    flex: none; padding: .3rem .55rem;
    border: 1px solid var(--border); border-radius: 999px;
    background: var(--surface-2); white-space: nowrap;
    font-size: .8rem;
  }
  .filter input { position: absolute; opacity: 0; pointer-events: none; }
  .filter:has(input:checked) {
    background: color-mix(in srgb, var(--dot) 14%, var(--surface));
    border-color: color-mix(in srgb, var(--dot) 45%, transparent);
  }
  .filter:not(:has(input:checked)) { opacity: .55; }
  .filter:not(:has(input:checked)) .filter__dot { background: var(--text-dim); box-shadow: none; }
  .filter__n { font-size: .7rem; }

  .locate { padding: .42rem; font-size: .8rem; }

  .panel-toggle {
    display: block; position: absolute; z-index: 600;
    left: 50%; bottom: calc(.7rem + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    background: var(--surface); color: var(--text);
    border: 1px solid var(--border); border-radius: 999px;
    padding: .5rem 1.1rem; font-size: .84rem; font-weight: 650;
    box-shadow: var(--shadow);
  }
  /* Lift the toggle above the sheet when it is open, so it stays reachable. */
  .panel[data-collapsed="false"] ~ .map-wrap .panel-toggle {
    bottom: calc(68% + .6rem);
  }
  .topbar__title p { display: none; }
}

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

/* ------------------------------------------------- static document pages */
/* The map app locks scrolling; document pages need it back. */
body.doc-page {
  display: block;
  overflow: auto;
  background: var(--bg);
}

.doc {
  max-width: 46rem;
  margin: 0 auto;
  padding: clamp(1.5rem, 4vw, 3rem) clamp(1rem, 4vw, 2rem) 4rem;
}
.doc h1 { font-size: clamp(1.5rem, 3.5vw, 2rem); letter-spacing: -.02em; }
.doc h2 {
  font-size: 1.02rem; margin: 2rem 0 .5rem;
  padding-top: 1rem; border-top: 1px solid var(--border);
}
.doc p { margin: 0 0 .85rem; }
.doc__updated { color: var(--text-dim); font-size: .82rem; margin-top: .35rem; }
.doc__lede { font-size: 1.02rem; margin-top: 1.25rem; }
.doc__back { font-size: .86rem; margin-bottom: 1.5rem; }
.doc__back a { text-decoration: none; font-weight: 600; }
.doc__back a:hover { text-decoration: underline; }
.doc__back--end {
  margin: 2.5rem 0 0; padding-top: 1.25rem; border-top: 1px solid var(--border);
}

.panel__footer nav { margin-top: .5rem; }
.panel__footer nav a { text-decoration: none; font-weight: 600; }
.panel__footer nav a:hover { text-decoration: underline; }
