/* ============================================================================
   map-legend.css  —  the ONE stylesheet for every below-map legend across
   CrimeGrade, BestNeighborhood, and future sites.

   Loaded UP FRONT (before the map bundle) by each *_map_loader.php via
   ensureCss(MAP_LEGEND_CSS, 'map-legend'), so the legend is styled at first
   paint — long before the user clicks the teaser to load the live map. That
   is why this lives outside shared-map/src/ and is NOT part of any bundle:
   the bundle CSS only loads on the map-trigger click, which is too late.

   ── DO NOT fork this file or copy rules into a page. ──
   Pages express their few unique traits through the CSS variables below;
   everything structural (padding, fonts, toggle, icon, divider, clip-path,
   bar height, breakpoints) lives here and only here. See
   map-legend-guidelines.md. The defaults reproduce CrimeGrade "overall
   crime" — the gold standard — pixel-for-pixel.

   ── Variables a page MAY set on the .mlg-extlegend root (and nothing else) ──
     --mlg-grad-low / --mlg-grad-mid / --mlg-grad-high
                            3-stop gradient ends. Default = CG green/yellow/red.
                            Override the three under .mlg-cb-on for colorblind.
     --mlg-bar-gradient     full linear-gradient(...) string. Use ONLY for
                            multi-stop ramps (noise, politics) that cannot be a
                            single 3-stop. Replaces the bar fill without
                            touching .mlg-extlegend-bar.
     --mlg-bar-gradient-cb  colorblind variant of --mlg-bar-gradient.
     --mlg-end-low / --mlg-end-high
                            end-label colors (default: the low/high gradient
                            colors — required when --mlg-bar-gradient is used,
                            since the grad-* vars then no longer match the bar).
     --mlg-mark-size        in-bar mark font-size (default 18px = CG grades;
                            multi-character numeric marks usually want 14px).
     --mlg-cb-accent        "on"-state accent for switch + status text
                            (default: --mlg-grad-low).
     --mlg-seg-count        segment count (segmented mode; drives the narrow
                            fallback grid columns).
   ========================================================================= */

.mlg-extlegend {
  /* Default palette = CrimeGrade "overall crime". */
  --mlg-grad-low:  #2f7d4a;
  --mlg-grad-mid:  #e0c95c;
  --mlg-grad-high: #c94b4b;
  color: #222;
  /* Pin line-height so the legend renders identically on every site instead of
     inheriting the host theme's body line-height. (Divi's default 1.7em made the
     label rows tall on BestNeighborhood, so the fixed paddings looked cramped;
     CrimeGrade's body is line-height:inherit ≈ normal, which is ~1.2, so this
     matches the CG look and leaves it unchanged.) Bar marks set their own. */
  line-height: 1.2;
  margin: 0;
  background: #f4f4f4;
  border: 1px solid #d9d9d9;
  border-top: none;
  border-bottom: none;
}
/* WordPress's wpautop drops stray <p> tags around the loader's injected
   <style>/<script> — sometimes right before the legend, and sometimes (when it
   mangles the multi-line markup) as an empty first child INSIDE the legend.
   Either one's default ~1em paragraph margins open a gap. The legend never holds
   a real paragraph, and the <p> before it carries only non-rendering nodes, so
   collapse both. (A <style> inside a display:none element still applies its
   rules, and the inline <script> already ran at parse time — so hiding the
   wrappers changes nothing but the spacing.) Two separate rules so the universal
   first one still applies in a browser that lacks :has. */
.mlg-extlegend p { display: none; }
p:has(+ .mlg-extlegend) { display: none; }
/* Default colorblind palette — applied whenever the toggle is on, unless the
   page overrides these three (or supplies --mlg-bar-gradient-cb). */
.mlg-extlegend.mlg-cb-on {
  --mlg-grad-low:  #4A90D9;
  --mlg-grad-mid:  #D4B86A;
  --mlg-grad-high: #D4782B;
}

/* ── Bar ──────────────────────────────────────────────────────────────────
   Full-width trapezium: top edge flush with the map's bottom edge, bottom
   corners tapered 12px so the inset is a constant pixel size at any width. */
.mlg-extlegend-bar-wrap {
  position: relative;
  width: 100%;
  height: 32px;
}
.mlg-extlegend-bar {
  width: 100%;
  height: 100%;
  background: var(--mlg-bar-gradient,
    linear-gradient(to right,
      var(--mlg-grad-low) 0%,
      var(--mlg-grad-mid) 50%,
      var(--mlg-grad-high) 100%));
  clip-path: polygon(0 0, 100% 0, calc(100% - 12px) 100%, 12px 100%);
}
.mlg-extlegend.mlg-cb-on .mlg-extlegend-bar {
  background: var(--mlg-bar-gradient-cb,
    var(--mlg-bar-gradient,
      linear-gradient(to right,
        var(--mlg-grad-low) 0%,
        var(--mlg-grad-mid) 50%,
        var(--mlg-grad-high) 100%)));
}

/* ── Segmented variant (categorical: race, satellite, providers) ──
   Each segment is a flex child carrying its own background inline. */
.mlg-extlegend--segmented .mlg-extlegend-bar {
  background: transparent;
  display: flex;
}
.mlg-extlegend-seg {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.15;
  padding: 0 4px;
  color: #f5f5f5;
  font-weight: 700;
  font-size: 13px;
  text-shadow: 0 1px 1px rgba(0,0,0,0.7), 0 2px 4px rgba(0,0,0,0.4);
}

/* ── In-bar marks (gradient mode) ──
   One class for CG grade letters, noise dBA numbers, and politics D+/R+ marks.
   Interior marks position via inline `left:%`; the two end marks add
   data-edge so they sit inside the taper exactly like CG's A+ / F. */
.mlg-extlegend-mark {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-weight: 700;
  font-size: var(--mlg-mark-size, 18px);
  line-height: 1;
  color: #f5f5f5;
  text-shadow: 0 1px 1px rgba(0,0,0,0.65), 0 2px 4px rgba(0,0,0,0.35);
  pointer-events: none;
  white-space: nowrap;
}
.mlg-extlegend-mark[data-edge="low"]  { left: 24px; }
.mlg-extlegend-mark[data-edge="high"] { left: calc(100% - 24px); }
/* Emphasized mark (noise "55 EPA", politics "Even") — dark text + white halo
   so it reads on a light midpoint. */
.mlg-extlegend-mark--emph {
  font-weight: 800;
  color: #1a1a1a;
  text-shadow: 0 0 3px rgba(255,255,255,0.95), 0 0 6px rgba(255,255,255,0.7);
}

/* ── Card body ── */
.mlg-extlegend-inner {
  padding: 10px 16px 14px;
}
/* End labels. 2 = ends (flex space-between); 1 = centered; 3+ = absolute via
   inline left:%. */
.mlg-extlegend-ends {
  display: flex;
  justify-content: space-between;
  font-weight: 700;
}
.mlg-extlegend-ends--centered { justify-content: center; }
.mlg-extlegend-ends--absolute {
  position: relative;
  display: block;
  height: 1.4em;
}
.mlg-extlegend-ends--absolute .mlg-extlegend-end {
  position: absolute;
  transform: translateX(-50%);
  white-space: nowrap;
}
.mlg-extlegend-end--low  { color: var(--mlg-end-low,  var(--mlg-grad-low)); }
.mlg-extlegend-end--high { color: var(--mlg-end-high, var(--mlg-grad-high)); }
.mlg-extlegend-end--emph { font-weight: 800; }

/* Optional centered title (categorical legends, e.g. "Majority race by area"). */
.mlg-extlegend-title {
  text-align: center;
  font-weight: 700;
  margin-top: 6px;
}

/* Narrow-screen fallback for segmented legends: the in-bar labels vanish and
   reappear as a text grid below the bar. Hidden by default; shown ≤540px. */
.mlg-extlegend-fallback { display: none; }

/* ── Divider + colorblind toggle row ── */
.mlg-extlegend-divider {
  margin: 12px -16px 0;
  height: 1px;
  background: #c4c4c4;
}
.mlg-extlegend-cb {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 12px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.mlg-extlegend-cb-icon {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}
.mlg-extlegend-cb-icon .mlg-cb-icon-bg   { fill: #fff; stroke: #222; stroke-width: 0.5; }
.mlg-extlegend-cb-icon .mlg-cb-icon-half { fill: #222; }
.mlg-extlegend-cb-icon .mlg-cb-icon-ring { stroke: #222; }
.mlg-extlegend-cb-text { color: #222; }
.mlg-extlegend-cb-status {
  color: #555;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-thickness: 1.5px;
  margin-left: 2px;
}
.mlg-extlegend.mlg-cb-on .mlg-extlegend-cb-status {
  color: var(--mlg-cb-accent, var(--mlg-grad-low));
}
.mlg-extlegend-cb-switch {
  margin-left: auto;
  position: relative;
  width: 40px;
  height: 22px;
  border: 0;
  border-radius: 11px;
  background: #ccc;
  cursor: pointer;
  padding: 0;
  transition: background 0.15s ease;
  flex-shrink: 0;
}
.mlg-extlegend-cb-switch:focus-visible {
  outline: 2px solid var(--mlg-cb-accent, var(--mlg-grad-low));
  outline-offset: 2px;
}
.mlg-extlegend-cb-switch-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,0.25);
  transition: transform 0.15s ease;
}
.mlg-extlegend.mlg-cb-on .mlg-extlegend-cb-switch {
  background: var(--mlg-cb-accent, var(--mlg-grad-low));
}
.mlg-extlegend.mlg-cb-on .mlg-extlegend-cb-switch-thumb {
  transform: translateX(18px);
}

/* ── Responsive ── */
/* Tablet & below: the "on/off" word is redundant beside the pill — drop it. */
@media (max-width: 1023px) {
  .mlg-extlegend-cb-status { display: none; }
}
/* Phone: shrink labels, tighten padding, flip segmented bars to the grid. */
@media (max-width: 540px) {
  .mlg-extlegend-ends,
  .mlg-extlegend-cb-text,
  .mlg-extlegend-title { font-size: 15px; }
  .mlg-extlegend-inner { padding: 10px 10px 14px; }
  .mlg-extlegend-divider { margin-left: -10px; margin-right: -10px; }
  .mlg-extlegend--segmented .mlg-extlegend-seg { font-size: 0; padding: 0; }
  .mlg-extlegend--segmented .mlg-extlegend-fallback {
    display: grid;
    grid-template-columns: repeat(var(--mlg-seg-count, 4), 1fr);
    gap: 2px;
    margin: 0 0 6px;
  }
  .mlg-extlegend--segmented .mlg-extlegend-fallback span {
    text-align: center;
    font-size: 11px;
    font-weight: 600;
    color: #333;
  }
}
/* Very narrow: hide marks flagged data-narrow-hide (e.g. politics inner marks
   D+25 / R+25 that would otherwise collide). */
@media (max-width: 380px) {
  .mlg-extlegend-mark[data-narrow-hide] { display: none; }
}
