/* ============================================================
   Te Rōpū Kāhui — NZ Charities Register Map Explorer
   Design language: editorial civic-data product.
   ============================================================ */

:root {
  /* ink & paper */
  --ink-0: #0f1419;
  --ink-1: #1a2230;
  --ink-2: #3d4a5c;
  --ink-3: #6b7a8d;
  --paper: #f7f5f0;
  --paper-2: #efede6;
  --panel: #ffffff;
  --line: #e2dfd6;

  /* sea-teal accent family */
  --accent: #0e6f6b;
  --accent-deep: #0a4f4c;
  --accent-soft: #d8e9e6;

  /* sector palette (12) — distinct, muted-editorial */
  --s-community: #0e6f6b;
  --s-health: #b3543f;
  --s-education: #356a9a;
  --s-religious: #7b5aa6;
  --s-social: #c2842b;
  --s-sport: #4d7c41;
  --s-arts: #b0486e;
  --s-environment: #3e8a7d;
  --s-disability: #555f8f;
  --s-international: #8a6d3b;
  --s-fundraising: #96562e;
  --s-other: #5d6d7a;

  --danger: #b3402e;
  --radius: 10px;
  --shadow-1: 0 1px 2px rgba(15,20,25,.07), 0 2px 8px rgba(15,20,25,.06);
  --shadow-2: 0 4px 16px rgba(15,20,25,.14), 0 12px 40px rgba(15,20,25,.12);
  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
  --font-display: 'Newsreader', 'Iowan Old Style', Georgia, serif;
  --topbar-h: 58px;
}

* { box-sizing: border-box; }
html, body { height: 100%; overflow-x: clip; }
body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--paper);
  color: var(--ink-1);
  font-size: 14.5px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }

/* ---------------- top bar ---------------- */
#topbar {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  background: var(--ink-0);
  color: #f2f0ea;
  position: relative;
  z-index: 1000;
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.brand .kicker {
  font-size: 10.5px; letter-spacing: .14em; text-transform: uppercase;
  color: #9fb3ad; white-space: nowrap;
}
.brand h1 {
  font-family: var(--font-display);
  font-size: 20px; font-weight: 600; margin: 0; white-space: nowrap;
  letter-spacing: .01em;
}
.brand h1 .nz { color: #7ec9bf; font-style: italic; font-weight: 500; }

#search-wrap { flex: 1; max-width: 560px; position: relative; margin-left: auto; }
#search {
  width: 100%;
  height: 38px;
  border: 1px solid #33404e;
  border-radius: 999px;
  background: #1c2530;
  color: #f2f0ea;
  padding: 0 40px 0 38px;
  font-size: 14px;
  outline: none;
  transition: border-color .15s, background .15s;
}
#search:focus { border-color: #4fae9f; background: #222d3a; }
#search::placeholder { color: #7d8ea0; }
#search-icon {
  position: absolute; left: 13px; top: 50%; transform: translateY(-50%);
  color: #7d8ea0; pointer-events: none;
}
#search-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 26px; height: 26px; border: 0; border-radius: 50%;
  background: transparent; color: #7d8ea0; cursor: pointer; display: none;
  align-items: center; justify-content: center; font-size: 15px;
}
#search-clear:hover { background: #2c3a49; color: #fff; }
#search.has-value + #search-clear, #search-wrap.has-value #search-clear { display: flex; }

#search-results {
  position: absolute; top: 46px; left: 0; right: 0;
  background: var(--panel); color: var(--ink-1);
  border-radius: 12px; box-shadow: var(--shadow-2);
  max-height: 380px; overflow-y: auto; display: none;
  border: 1px solid var(--line);
}
#search-results.open { display: block; }
.sr-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; cursor: pointer; border-bottom: 1px solid #f0eee7;
}
.sr-item:last-child { border-bottom: 0; }
.sr-item:hover, .sr-item.active { background: var(--accent-soft); }
.sr-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.sr-name { font-weight: 600; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sr-meta { color: var(--ink-3); font-size: 12px; white-space: nowrap; margin-left: auto; }
.sr-empty { padding: 14px; color: var(--ink-3); font-size: 13px; }

#btn-about {
  border: 1px solid #33404e; background: transparent; color: #c8d3cd;
  border-radius: 999px; height: 34px; padding: 0 14px; cursor: pointer;
  font-size: 12.5px; white-space: nowrap;
}
#btn-about:hover { border-color: #4fae9f; color: #fff; }

/* ---------------- layout ---------------- */
#app {
  display: grid;
  grid-template-columns: 316px 1fr;
  height: calc(100% - var(--topbar-h));
}
#sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 16px 16px 90px;
  position: relative;
  z-index: 900;
}
#map-wrap { position: relative; min-width: 0; overflow: hidden; }
#map { position: absolute; inset: 0; }

/* ---------------- sidebar ---------------- */
.sb-section { margin-bottom: 20px; }
.sb-label {
  font-size: 10.5px; letter-spacing: .13em; text-transform: uppercase;
  color: var(--ink-3); margin: 0 0 8px; font-weight: 600;
  display: flex; justify-content: space-between; align-items: center;
}
.sb-label .clear-link {
  font-size: 11px; letter-spacing: 0; text-transform: none;
  color: var(--accent); cursor: pointer; background: none; border: 0; padding: 2px 4px;
}
.sb-label .clear-link:hover { text-decoration: underline; }

#result-summary {
  font-family: var(--font-display);
  font-size: 17px; color: var(--ink-1); margin: 2px 0 14px;
  line-height: 1.35;
}
#result-summary strong { font-weight: 600; }
#result-summary .dim { color: var(--ink-3); font-size: 13.5px; font-family: var(--font-ui); }

.chip-grid { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--paper); color: var(--ink-2);
  border-radius: 999px; padding: 5px 11px; font-size: 12.5px; cursor: pointer;
  display: inline-flex; align-items: center; gap: 7px;
  transition: background .12s, border-color .12s;
  min-height: 30px;
}
.chip:hover { border-color: #b8b4a6; }
.chip.on { background: var(--ink-1); border-color: var(--ink-1); color: #f4f2ec; }
.chip .c-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.chip .c-n { font-size: 11px; opacity: .65; font-variant-numeric: tabular-nums; }

select.sb-select {
  width: 100%; height: 36px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--paper); padding: 0 8px; font-size: 13px; color: var(--ink-1);
  font-family: inherit;
}

.seg { display: flex; background: var(--paper-2); border-radius: 9px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; border: 0; background: transparent; padding: 7px 4px; font-size: 12px;
  border-radius: 7px; cursor: pointer; color: var(--ink-2); font-weight: 500;
  min-height: 34px;
}
.seg button.on { background: var(--panel); color: var(--ink-1); box-shadow: var(--shadow-1); }

/* ---------------- map overlays ---------------- */
.leaflet-container { background: #e9e6de; font-family: var(--font-ui); }
.leaflet-popup-content-wrapper {
  border-radius: 12px; box-shadow: var(--shadow-2); font-family: var(--font-ui);
}
.leaflet-popup-content { margin: 14px 16px; font-size: 13px; line-height: 1.4; }

.nzc-cluster {
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; color: #fff;
  background: rgba(14, 111, 107, .88);
  border: 2px solid rgba(255,255,255,.9);
  box-shadow: 0 2px 10px rgba(10,79,76,.35);
  font-variant-numeric: tabular-nums;
  transition: transform .12s;
}
.nzc-cluster:hover { transform: scale(1.07); }

.org-marker {
  border-radius: 50%;
  border: 1.5px solid rgba(255,255,255,.85);
  box-shadow: 0 1px 3px rgba(15,20,25,.4);
}

#hover-tip {
  position: absolute; pointer-events: none; z-index: 950;
  background: var(--ink-0); color: #f2f0ea; border-radius: 8px;
  padding: 7px 10px; font-size: 12px; max-width: 260px;
  box-shadow: var(--shadow-2); display: none; line-height: 1.35;
}
#hover-tip .t-name { font-weight: 600; }
#hover-tip .t-meta { color: #a9b7ae; font-size: 11px; margin-top: 1px; }

/* Leaflet marker tooltips */
.nzc-tip {
  background: var(--ink-0); color: #f2f0ea; border: 0; border-radius: 8px;
  padding: 7px 10px; font-size: 12px; box-shadow: var(--shadow-2);
  line-height: 1.35; opacity: 1 !important;
}
.nzc-tip::before { display: none; }
.nzc-tip .t-name { font-weight: 600; max-width: 240px; white-space: normal; }
.nzc-tip .t-meta { color: #a9b7ae; font-size: 11px; margin-top: 1px; }

#stats-card {
  position: absolute; top: 14px; right: 14px; z-index: 940;
  background: rgba(255,255,255,.96); backdrop-filter: blur(6px);
  border-radius: 14px; box-shadow: var(--shadow-2);
  width: 300px; padding: 14px 16px 12px;
  border: 1px solid rgba(226,223,214,.7);
  transition: transform .2s, opacity .2s;
}
#stats-card.hidden { transform: translateY(-8px); opacity: 0; pointer-events: none; }
#stats-card h3 {
  margin: 0 0 2px; font-size: 11px; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-3); font-weight: 600;
}
#stats-card .big {
  font-family: var(--font-display); font-size: 26px; font-weight: 600;
  color: var(--ink-1); line-height: 1.15; margin-bottom: 10px;
}
#stats-card .big small { font-size: 13px; color: var(--ink-3); font-family: var(--font-ui); font-weight: 400; }
.stat-rows { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; margin-bottom: 10px; }
.stat-rows .sr { min-width: 0; }
.stat-rows .sr .v { font-weight: 700; font-size: 15px; font-variant-numeric: tabular-nums; }
.stat-rows .sr .k { font-size: 10.5px; color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.bar-col { margin: 4px 0 2px; }
.bar-col .bar-row {
  display: grid; grid-template-columns: 92px 1fr 40px; gap: 8px; align-items: center;
  font-size: 11px; margin: 3px 0; color: var(--ink-2);
}
.bar-row .bl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: right; }
.bar-row .bt { height: 8px; border-radius: 4px; background: var(--paper-2); overflow: hidden; }
.bar-row .bf { height: 100%; border-radius: 4px; }
.bar-row .bn { font-variant-numeric: tabular-nums; color: var(--ink-3); text-align: right; }

#map-legend {
  position: absolute; bottom: 16px; left: 16px; z-index: 940;
  background: rgba(255,255,255,.94); backdrop-filter: blur(4px);
  border-radius: 10px; box-shadow: var(--shadow-1); padding: 9px 12px;
  font-size: 11px; color: var(--ink-2); border: 1px solid rgba(226,223,214,.7);
  max-width: 270px;
}
#map-legend .lg-row { display: flex; align-items: center; gap: 6px; margin: 2px 0; }
#map-legend .lg-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
#map-legend .lg-note { color: var(--ink-3); margin-top: 5px; font-size: 10.5px; line-height: 1.4; }

#zoom-note {
  position: absolute; bottom: 16px; right: 16px; z-index: 940;
  background: rgba(15,20,25,.82); color: #e8e6df; font-size: 11px;
  border-radius: 999px; padding: 6px 12px; display: none;
}

/* ---------------- detail panel ---------------- */
#detail {
  position: absolute; top: 0; right: 0; bottom: 0; width: 380px; max-width: 92vw;
  background: var(--panel); z-index: 960;
  box-shadow: -8px 0 32px rgba(15,20,25,.16);
  transform: translateX(102%);
  transition: transform .28s cubic-bezier(.32,.72,.35,1);
  display: flex; flex-direction: column;
  border-left: 1px solid var(--line);
}
#detail.open { transform: translateX(0); }
#detail-head {
  padding: 18px 20px 14px; border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--paper) 0%, var(--panel) 100%);
}
#detail-head .d-sector {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase;
  color: var(--ink-2); margin-bottom: 8px;
}
#detail-head .d-sector .c-dot { width: 9px; height: 9px; border-radius: 50%; }
#detail-name {
  font-family: var(--font-display); font-size: 22px; font-weight: 600;
  line-height: 1.2; margin: 0 0 6px; color: var(--ink-1);
}
#detail-sub { font-size: 12px; color: var(--ink-3); display: flex; flex-wrap: wrap; gap: 4px 12px; }
#detail-close {
  position: absolute; top: 12px; right: 12px; width: 32px; height: 32px;
  border-radius: 50%; border: 1px solid var(--line); background: var(--panel);
  cursor: pointer; font-size: 16px; color: var(--ink-2); z-index: 2;
}
#detail-close:hover { background: var(--paper-2); }
#detail-body { overflow-y: auto; padding: 16px 20px 30px; flex: 1; }

.d-block { margin-bottom: 20px; }
.d-block h4 {
  margin: 0 0 8px; font-size: 10.5px; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ink-3); font-weight: 600;
}
.d-kv { display: grid; grid-template-columns: 118px 1fr; gap: 5px 10px; font-size: 13px; }
.d-kv .k { color: var(--ink-3); }
.d-kv .v { color: var(--ink-1); overflow-wrap: anywhere; }
.d-tag { display: inline-block; background: var(--paper-2); border-radius: 6px; padding: 2px 8px; font-size: 12px; margin: 0 5px 5px 0; color: var(--ink-2); }
.d-purpose { font-size: 13px; line-height: 1.55; color: var(--ink-2); font-family: var(--font-display); font-style: italic; }
.fin-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.fin-cell { background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.fin-cell .v { font-weight: 700; font-size: 16px; font-variant-numeric: tabular-nums; }
.fin-cell .k { font-size: 10.5px; color: var(--ink-3); text-transform: uppercase; letter-spacing: .05em; margin-top: 2px; }
.d-note { font-size: 11px; color: var(--ink-3); line-height: 1.5; }
.d-link {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 4px;
  color: var(--accent); font-size: 13px; font-weight: 600; text-decoration: none;
}
.d-link:hover { text-decoration: underline; }

/* ---------------- about / freshness modal ---------------- */
#modal-scrim {
  position: fixed; inset: 0; background: rgba(15,20,25,.5); z-index: 1200;
  display: none; align-items: center; justify-content: center; padding: 18px;
}
#modal-scrim.open { display: flex; }
#modal {
  background: var(--panel); border-radius: 16px; max-width: 640px; width: 100%;
  max-height: 86vh; overflow-y: auto; padding: 26px 28px 30px; box-shadow: var(--shadow-2);
  position: relative;
}
#modal h2 { font-family: var(--font-display); font-size: 24px; margin: 0 0 4px; font-weight: 600; }
#modal h3 { font-size: 13px; margin: 18px 0 6px; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-3); }
#modal p { margin: 8px 0; font-size: 13.5px; color: var(--ink-2); line-height: 1.6; }
#modal .fine { font-size: 12px; color: var(--ink-3); }
#modal-close {
  position: absolute; top: 14px; right: 14px; width: 32px; height: 32px;
  border-radius: 50%; border: 1px solid var(--line); background: var(--panel); cursor: pointer;
}
#modal-close:hover { background: var(--paper-2); }

/* freshness banner */
#fresh-banner {
  position: absolute; top: 14px; left: 70px; justify-content: center;
  z-index: 945; background: var(--ink-0); color: #e8e6df;
  border-radius: 999px; padding: 7px 14px; font-size: 12px;
  display: none; align-items: center; gap: 8px; box-shadow: var(--shadow-2);
  max-width: calc(100% - 400px);
}
#fresh-banner.show { display: flex; }
#fresh-banner .dot { width: 8px; height: 8px; border-radius: 50%; background: #e0b34c; flex: none; }
#fresh-banner.ok .dot { background: #58b98a; }
#fresh-banner button {
  background: rgba(255,255,255,.12); color: #fff; border: 0; border-radius: 999px;
  padding: 3px 10px; font-size: 11.5px; cursor: pointer; margin-left: 4px; white-space: nowrap;
}
#fresh-banner button:hover { background: rgba(255,255,255,.22); }

/* loading state */
#loading {
  position: absolute; inset: 0; z-index: 1100; background: var(--paper);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  transition: opacity .3s;
}
#loading.gone { opacity: 0; pointer-events: none; }
#loading .ld-title { font-family: var(--font-display); font-size: 26px; font-weight: 600; color: var(--ink-1); }
#loading .ld-sub { color: var(--ink-3); font-size: 13px; }
#loading .ld-bar { width: 220px; height: 4px; border-radius: 2px; background: var(--paper-2); overflow: hidden; }
#loading .ld-bar i { display: block; height: 100%; width: 40%; background: var(--accent); border-radius: 2px; animation: ldmv 1.1s infinite ease-in-out; }
@keyframes ldmv { 0% { margin-left: -40%; } 100% { margin-left: 100%; } }

/* mobile FABs */
#fab-filters, #fab-stats { display: none; }

/* focus visibility */
:focus-visible { outline: 2px solid #4fae9f; outline-offset: 2px; }

/* ---------------- responsive ---------------- */
@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr; }
  #sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; top: auto;
    max-height: 62vh; z-index: 970; border-right: 0;
    border-top-left-radius: 18px; border-top-right-radius: 18px;
    box-shadow: 0 -8px 32px rgba(15,20,25,.25);
    transform: translateY(calc(100% - 0px));
    transition: transform .28s cubic-bezier(.32,.72,.35,1);
    padding-bottom: 40px;
  }
  #sidebar.open { transform: translateY(0); }
  .brand h1 { font-size: 17px; }
  .brand .kicker { display: none; }
  #btn-about { padding: 0 10px; }
  #stats-card {
    top: auto; bottom: 84px; right: 12px; left: 12px; width: auto;
    max-height: 42vh; overflow-y: auto;
  }
  #stats-card.hidden-mobile { display: none; }
  #map-legend { display: none; }
  #detail { width: 100%; max-width: 100%; border-left: 0; }
  #fab-filters {
    display: flex; position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
    z-index: 965; background: var(--ink-0); color: #f2f0ea; border: 0;
    border-radius: 999px; padding: 0 20px; height: 46px; align-items: center; gap: 8px;
    font-size: 14px; font-weight: 600; box-shadow: var(--shadow-2); cursor: pointer;
  }
  #fab-filters .badge {
    background: #4fae9f; color: #062726; border-radius: 999px; padding: 1px 8px;
    font-size: 11.5px; font-weight: 700;
  }
  #fab-stats {
    display: flex; position: fixed; bottom: 18px; right: 14px; z-index: 965;
    background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
    width: 46px; height: 46px; align-items: center; justify-content: center;
    box-shadow: var(--shadow-2); cursor: pointer; color: var(--ink-1);
  }
  #search-results { max-height: 50vh; }
  #fresh-banner { max-width: calc(100% - 84px); left: 70px; font-size: 11px; border-radius: 14px; }
  #detail-head { padding-left: 20px; }
  #detail-head #detail-name { padding-right: 8px; }
  #fab-filters, #fab-stats { z-index: 940; }
  #detail { z-index: 980; }
  /* B1: keep the zoom control clear of the open detail sheet header */
  body.detail-open .leaflet-control-zoom { display: none; }
  /* touch-friendly filter controls */
  .chip { min-height: 40px; padding: 8px 14px; font-size: 13px; }
  .seg { flex-wrap: wrap; }
  .seg button { min-height: 44px; font-size: 12.5px; }
  select.sb-select { height: 44px; font-size: 14px; }
  #reset-all { height: 46px !important; }
}
@media (max-width: 420px) {
  .brand h1 .long { display: none; }
  #topbar { gap: 8px; padding: 0 10px; }
}

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

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