/* Totemia GIS — map viewer UI aligned with the portal bold type system
 * (registry labels, green headlines, heavy values). Linked separately so it
 * satisfies the viewer Content-Security-Policy (style-src-elem 'self'). */

:root {
  --gis-ivory: #f4f5f7;
  --gis-paper: #ffffff;
  --gis-gold: #c4981f;
  --gis-gold-soft: #dcc48a;
  --gis-green: #123c32;
  --gis-ink: #14171a;
  --gis-muted: #5b636c;
  --gis-line: #e3e6ea;
  --gis-red: #9f2f24;
  --gis-font: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* Zero-size pin at the marker coordinate; the card is laid out above it. */
.gis-popup-anchor {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  display: none;
  z-index: 6;
  pointer-events: none;
}

/* Smart placement (map-viewer.js placePopup) drives --popup-lift (gap above the
 * marker), --popup-shift-x (horizontal clamp so the card stays on-screen), and
 * --popup-tail-x (keeps the tail over the marker). `.flipped` puts the card
 * BELOW the marker when there isn't room above. Defaults keep the original
 * centred-above look if the helper is ever unavailable. */
.gis-popup {
  position: absolute;
  left: 50%;
  bottom: var(--popup-lift, 0);
  transform: translateX(var(--popup-shift-x, -50%));
  min-width: 220px;
  max-width: min(300px, 82vw);
  pointer-events: auto;
  background: var(--gis-paper);
  border: 1px solid var(--gis-line);
  border-top: 3px solid var(--gis-gold);
  border-radius: 12px;
  box-shadow: 0 12px 28px rgba(20, 23, 20, 0.2);
  color: var(--gis-ink);
  font-family: var(--gis-font);
  padding: 16px 18px 15px;
  animation: gisPopupIn 0.14s ease-out;
}

.gis-popup.flipped {
  bottom: auto;
  top: var(--popup-lift, 0);
}

/* Pointer tail aimed at the marker; tracks --popup-tail-x as the card shifts. */
.gis-popup::after {
  content: "";
  position: absolute;
  left: var(--popup-tail-x, 50%);
  bottom: -9px;
  width: 16px;
  height: 16px;
  transform: translateX(-50%) rotate(45deg);
  background: var(--gis-paper);
  border-right: 1px solid var(--gis-line);
  border-bottom: 1px solid var(--gis-line);
}

/* When flipped below, the tail moves to the top edge and points up. */
.gis-popup.flipped::after {
  bottom: auto;
  top: -9px;
  border-right: 0;
  border-bottom: 0;
  border-left: 1px solid var(--gis-line);
  border-top: 1px solid var(--gis-line);
}

.gis-popup-close {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--gis-muted);
  font-family: var(--gis-font);
  font-size: 22px;
  font-weight: 400;
  line-height: 1;
  cursor: pointer;
}

.gis-popup-close:hover {
  background: rgba(159, 47, 36, 0.1);
  color: var(--gis-red);
}

/* Scroll inside the card (not the whole popup) so a cover image + long
 * description never runs off-screen, while the ::after pointer tail and the
 * close button stay pinned to the card frame. */
.gis-popup-body {
  max-height: min(60dvh, 440px);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* registry-label */
.gis-popup-tag {
  display: block;
  margin: 0 0 5px;
  color: var(--gis-muted);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* official-section / party-doc headline */
.gis-popup-title {
  margin: 0 24px 14px 0;
  color: var(--gis-green);
  font-size: 18px;
  font-weight: 800;
  line-height: 1.15;
  letter-spacing: -0.02em;
}

/* Editor-supplied cover image — flat framed thumbnail, no decorative effects. */
.gis-popup-cover {
  display: block;
  width: 100%;
  max-height: 160px;
  object-fit: cover;
  margin: 0 0 13px;
  border: 1px solid var(--gis-line);
  border-radius: 8px;
  background: var(--gis-ivory);
}

/* Editor-supplied description — reads as prose, not a metadata row. */
.gis-popup-desc {
  margin: 0 0 14px;
  color: var(--gis-ink);
  font-size: 13.5px;
  font-weight: 500;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.gis-popup-facts {
  display: flex;
  flex-direction: column;
  gap: 11px;
  margin: 0;
}

/* transfer-from-label */
.gis-popup-facts dt {
  display: block;
  margin: 0 0 3px;
  color: var(--gis-muted);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

/* transfer-from-value / record-meta emphasis */
.gis-popup-facts dd {
  display: block;
  margin: 0;
  color: var(--gis-ink);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.35;
  word-break: break-word;
}

.gis-popup-facts dd.gis-popup-area {
  color: var(--gis-green);
  font-weight: 800;
}

.gis-popup-facts dd.gis-popup-coords {
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums lining-nums;
  letter-spacing: 0.03em;
}

.gis-popup-facts dd.gis-popup-meta {
  font-weight: 700;
  color: var(--gis-ink);
}

/* Role-gated POI edit affordance row (the button reuses .gis-region-edit from
 * gis-regions.css, which is loaded alongside this stylesheet). */
.gis-poi-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 13px;
}

@keyframes gisPopupIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Legend — official-stat card, bottom-right. */
.gis-legend {
  position: absolute;
  right: 10px;
  bottom: max(26px, calc(env(safe-area-inset-bottom, 0px) + 14px));
  z-index: 5;
  display: flex;
  flex-direction: column;
  min-width: 168px;
  max-width: min(280px, 62vw);
  /* Never let a long layer list grow taller than the viewport (the mobile bug
   * where the expanded panel filled the whole screen). The header stays put and
   * the list scrolls inside. */
  max-height: min(64dvh, 560px);
  background: var(--gis-paper);
  border: 1px solid var(--gis-line);
  border-top: 3px solid var(--gis-gold);
  border-radius: 12px;
  box-shadow: 0 10px 24px rgba(20, 23, 20, 0.18);
  font-family: var(--gis-font);
  overflow: hidden;
}

.gis-legend-toggle {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 14px;
  border: 0;
  background: transparent;
  color: var(--gis-green);
  font-family: var(--gis-font);
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
}

.gis-legend-caret {
  width: 9px;
  height: 9px;
  border-right: 2px solid var(--gis-muted);
  border-bottom: 2px solid var(--gis-muted);
  transform: rotate(45deg);
  transition: transform 0.15s ease;
}

.gis-legend.collapsed .gis-legend-caret {
  transform: rotate(-135deg);
}

.gis-legend-items {
  display: grid;
  gap: 6px;
  padding: 0 14px 14px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.gis-legend.collapsed .gis-legend-items {
  display: none;
}

.gis-legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
  padding: 5px 6px;
  border-radius: 8px;
}

.gis-legend-item:hover {
  background: var(--gis-ivory);
}

.gis-legend-check {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  margin: 0;
  accent-color: var(--gis-gold);
  cursor: pointer;
}

.gis-legend-icon {
  width: 24px;
  height: 24px;
  flex: 0 0 24px;
  object-fit: contain;
}

.gis-legend-label {
  color: var(--gis-ink);
  font-size: 13px;
  font-weight: 800;
  line-height: 1.2;
  letter-spacing: -0.01em;
}

@media (prefers-reduced-motion: reduce) {
  .gis-popup { animation: none; }
  .gis-legend-caret { transition: none; }
}

@media (pointer: coarse) {
  .gis-popup-close {
    min-width: 44px;
    min-height: 44px;
    top: 6px;
    right: 6px;
  }

  .gis-legend-toggle {
    min-height: 48px;
    padding: 12px 14px;
  }

  .gis-legend-item {
    min-height: 44px;
    padding: 8px 6px;
  }
}

@media (max-width: 560px) {
  .gis-popup {
    padding: 14px 15px 13px;
    max-width: 86vw;
  }

  .gis-popup-title {
    font-size: 16px;
    margin-right: 22px;
  }

  .gis-popup-facts dd {
    font-size: 13px;
  }

  .gis-legend {
    right: 8px;
    bottom: max(20px, calc(env(safe-area-inset-bottom, 0px) + 10px));
    min-width: 0;
    max-width: min(232px, 66vw);
    max-height: min(58dvh, 460px);
  }

  .gis-legend-label {
    font-size: 12px;
  }
}
