:root {
  --teal: #7bd1eb;
  --teal-dark: #40a3ba;
  --navy: #0f172b;
  --body-text: #45556c;
  --accent: #314158;
  --bg-alt: #f8fafc;
  --line: #d9d9d9;
  --amber: #42ae70;
  --card-gray: #5d6b7a; /* darkened from #738290 to meet WCAG AA contrast at small sizes */
  --white: #ffffff;
  --radius: 10px;
  --shadow: 0 2px 12px rgba(15, 23, 43, 0.12);
  --shadow-lg: 0 8px 30px rgba(15, 23, 43, 0.18);
  --sidebar-width: 380px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow-x: hidden;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--navy);
  background: var(--bg-alt);
}

.app {
  display: flex;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--white);
  border-right: 1px solid var(--line);
  z-index: 10;
}

.sidebar-top {
  background: var(--teal);
  padding: 14px 16px;
}

.search-row {
  display: flex;
  align-items: center;
  background: var(--white);
  border-radius: 8px;
  padding: 8px 10px;
  gap: 8px;
}

.icon-search {
  width: 18px;
  height: 18px;
  color: var(--body-text);
  flex-shrink: 0;
}

#searchInput {
  border: none;
  outline: none;
  flex: 1;
  font-size: 14px;
  font-family: inherit;
  color: var(--navy);
  min-width: 0;
}

#refreshBtn {
  border: none;
  background: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  color: var(--teal);
  padding: 2px;
  flex-shrink: 0;
  transition: transform 0.4s ease;
}

#refreshBtn svg { width: 18px; height: 18px; }
#refreshBtn.spinning svg { animation: spin 0.7s linear; }
@keyframes spin { to { transform: rotate(360deg); } }

.filters-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  border: none;
  background: var(--teal-dark);
  color: var(--white);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  padding: 10px 16px;
  cursor: pointer;
}

.chevron { width: 16px; height: 16px; transition: transform 0.2s ease; }
.filters-toggle[aria-expanded="true"] .chevron { transform: rotate(90deg); }

.filters-panel {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.25s ease;
  background: var(--bg-alt);
  border-bottom: 1px solid var(--line);
}

.filters-panel.open {
  max-height: 320px;
  overflow-y: auto;
}

.filter-group {
  padding: 10px 16px 4px;
}

.filter-group h4 {
  margin: 6px 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent);
}

.filter-options {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--white);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12.5px;
  color: var(--body-text);
  cursor: pointer;
  user-select: none;
}

/* Visually hidden but still focusable, so keyboard users can toggle filters. */
.filter-chip input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.filter-chip {
  position: relative;
}

.filter-chip:has(input:focus-visible) {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.filter-chip .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.filter-chip.checked {
  background: var(--navy);
  border-color: var(--navy);
  color: var(--white);
}

.filters-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px 14px;
  background: var(--bg-alt);
  border-top: 1px solid var(--line);
}

.apply-filters {
  flex: 1;
  border: none;
  background: var(--navy);
  color: var(--white);
  font-family: inherit;
  font-size: 13.5px;
  font-weight: 700;
  padding: 10px 14px;
  border-radius: 8px;
  cursor: pointer;
}

.apply-filters:hover { background: var(--teal-dark); }

.clear-filters {
  flex-shrink: 0;
  border: none;
  background: none;
  color: var(--teal-dark);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}

.sheet-handle { display: none; }

.sheet-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.results-count {
  padding: 10px 16px 4px;
  font-size: 12.5px;
  color: var(--body-text);
  flex-shrink: 0;
}

.results-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 12px 16px;
}

/* ---------- Result cards ---------- */

.result-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin: 8px 0;
  overflow: hidden;
  cursor: pointer;
  background: var(--white);
  transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.result-card:hover, .result-card.active {
  box-shadow: var(--shadow);
  border-color: var(--teal);
}

.result-card:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.result-card .card-bar {
  height: 8px;
}

.result-card .card-body {
  padding: 12px 14px 14px;
}

.result-card h3 {
  margin: 0 0 10px;
  font-size: 19px;
  font-weight: 700;
  color: var(--navy);
  text-align: left;
}

.result-card .card-role {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 10px;
}

.role-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--navy);
}

.role-badge .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.card-divider {
  border: none;
  border-top: 2px solid var(--teal);
  margin: 0 0 10px;
}

.card-meta {
  font-size: 11.5px;
  color: var(--card-gray);
  margin: 0 0 10px;
  line-height: 1.4;
}

.card-meta .meta-sep {
  color: var(--line);
}

.card-meta a {
  color: inherit;
  text-decoration: none;
}

.card-meta a:hover { text-decoration: underline; }

.result-card .card-row {
  font-size: 12.5px;
  color: var(--card-gray);
  margin: 4px 0;
  line-height: 1.4;
}

.result-card .card-row b {
  color: var(--accent);
  font-weight: 600;
}

.result-card a {
  color: var(--teal-dark);
  text-decoration: none;
  word-break: break-word;
}

.result-card a:hover { text-decoration: underline; }

.no-results {
  padding: 30px 10px;
  text-align: center;
  color: var(--body-text);
  font-size: 13.5px;
}

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

.map-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
  background: var(--bg-alt);
}

.leaflet-control-recenter a {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  color: var(--navy);
}

.leaflet-control-recenter svg {
  width: 16px;
  height: 16px;
}

.map-legend {
  position: absolute;
  bottom: 20px;
  left: 16px;
  background: rgba(255,255,255,0.95);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 10px 14px;
  font-size: 12px;
  color: var(--body-text);
  z-index: 5;
  max-width: 260px;
}

.map-legend .legend-title {
  font-weight: 700;
  color: var(--navy);
  margin-bottom: 6px;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.map-legend .legend-row {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 3px 0;
}

.map-legend .dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ---------- Leaflet marker / popup theming ---------- */

.resource-marker {
  border-radius: 50%;
  border: 2px solid var(--white);
  box-shadow: 0 2px 6px rgba(15,23,43,0.35);
}

.resource-marker.hover, .resource-marker.selected {
  box-shadow: 0 0 0 3px rgba(31,128,128,0.35), 0 2px 6px rgba(15,23,43,0.35);
}

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

.leaflet-popup-content {
  margin: 0;
  width: 300px !important;
}

.popup-entry {
  border-bottom: 1px solid var(--line);
}

.popup-entry:last-child { border-bottom: none; }

.popup-entry .popup-bar { height: 6px; }

.popup-logo-banner {
  width: 100%;
  height: 150px;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--white);
  font-weight: 700;
  font-size: 24px;
  letter-spacing: 0.03em;
}

.popup-logo-banner img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: var(--white);
  padding: 8px;
  box-sizing: border-box;
}

.popup-entry .popup-body {
  padding: 12px 14px;
}

.popup-entry h3 {
  margin: 0 0 3px;
  font-size: 14.5px;
  color: var(--navy);
}

.popup-entry .popup-role {
  font-size: 11px;
  font-weight: 600;
  color: var(--body-text);
  margin-bottom: 6px;
}

.popup-entry .popup-row {
  font-size: 12px;
  color: var(--body-text);
  margin: 3px 0;
  line-height: 1.4;
}

.popup-entry .popup-row b { color: var(--accent); }

.popup-entry a { color: var(--teal-dark); text-decoration: none; }
.popup-entry a:hover { text-decoration: underline; }

.leaflet-popup-tip { box-shadow: none; }

/* ---------- Loading overlay ---------- */

.loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--white);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 999;
  color: var(--body-text);
  font-size: 14px;
}

.loading-overlay.hidden { display: none; }

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--line);
  border-top-color: var(--teal);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.error-banner {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #fef2f2;
  border: 1px solid #fca5a5;
  color: #991b1b;
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 1000;
  max-width: 90%;
  box-shadow: var(--shadow);
}

.toast-banner {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--navy);
  color: var(--white);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13.5px;
  font-weight: 600;
  z-index: 1000;
  max-width: 90%;
  box-shadow: var(--shadow-lg);
}

/* ---------- Mobile: bottom sheet ---------- */

@media (max-width: 860px) {
  .app { position: relative; }

  .map-wrap {
    position: absolute;
    inset: 0;
    width: 100%;
    /* Explicit z-index (even at 0) forms a stacking context, so Leaflet's
       internal panes/controls (which use z-index up to 1000) stay trapped
       below the sidebar instead of escaping to compare against it directly. */
    z-index: 0;
  }

  .sidebar {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-height: 86vh;
    border-right: none;
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
    box-shadow: 0 -6px 24px rgba(15, 23, 43, 0.2);
    z-index: 15;
  }

  .sidebar-top { border-radius: 16px 16px 0 0; }

  .sheet-handle {
    position: relative;
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    border: none;
    background: var(--white);
    font-family: inherit;
    cursor: pointer;
    padding: 18px 16px 12px;
    flex-shrink: 0;
  }

  .sheet-handle .handle-bar {
    display: block;
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: 999px;
    background: var(--line);
  }

  .sheet-handle .handle-label {
    flex: 1;
    text-align: left;
    font-size: 13px;
    font-weight: 600;
    color: var(--accent);
  }

  .sheet-handle .chevron {
    transform: rotate(90deg);
    color: var(--body-text);
  }

  .sheet-handle[aria-expanded="true"] .chevron { transform: rotate(-90deg); }

  .sheet-body {
    flex: none;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.28s ease;
  }

  .sidebar.sheet-expanded .sheet-body {
    max-height: calc(86vh - 118px);
  }

  .results-list { max-height: none; }

  .filters-panel.open { max-height: 40vh; }
}
