@import url('tokens.css');
@import url('miv.css');
@import url('responsive.css');

/* ============================================
   SISTEMA DE CAPTAÇÃO (Mapa) — Overrides & Extras
   (Tokens vêm de tokens.css / MIV)
   ============================================ */

:root {
  --miv-shell-header-height: 64px;
  --miv-bottom-nav-height: 52px;
  /* Variáveis específicas deste dash (não duplicam MIV) */
  --map-header-height: 56px;
  --map-brand: #145f6b;
  --map-brand-strong: #0d4650;
  --map-blue: #2563eb;
  --map-blue-dark: #1748b5;
  --map-red: #d72828;
  --map-teal: #0f766e;
  --map-teal-fill: #14b8a6;
  --map-amber: #d97706;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: var(--surface-muted);
  color: var(--text-primary);
  font-family: var(--font-family);
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
  letter-spacing: 0;
}

button {
  color: inherit;
}

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

/* ── Header ── */
.app-header {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  height: var(--map-header-height);
  justify-content: space-between;
  padding: 0 18px;
  position: relative;
  z-index: 1200;
}

.brand {
  align-items: center;
  display: flex;
  gap: 12px;
  min-width: 0;
}

.brand-logo {
  display: block;
  height: 40px;
  object-fit: contain;
  width: 104px;
}

.brand-copy {
  min-width: 0;
}

.brand-kicker,
.panel-kicker {
  color: var(--map-brand);
  display: block;
  font-size: var(--font-size-xs);
  font-weight: 700;
  line-height: 12px;
  text-transform: uppercase;
}

.brand h1 {
  color: var(--map-brand-strong);
  font-size: 15px;
  font-weight: 650;
  line-height: 20px;
  margin: 0;
  white-space: nowrap;
}

.title-mobile {
  display: none;
}

.mobile-actions {
  display: none;
}

.icon-button {
  align-items: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  cursor: pointer;
  display: inline-flex;
  height: 36px;
  justify-content: center;
  padding: 0;
  width: 36px;
}

.icon-button:hover {
  background: var(--surface-muted);
  border-color: var(--border);
}

.icon-button svg {
  height: 18px;
  width: 18px;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.28);
  outline-offset: 2px;
}

/* ── Map Shell ── */
.map-shell {
  height: calc(100dvh - var(--miv-shell-header-height) - var(--map-header-height));
  min-height: 300px;
  position: relative;
  width: 100%;
}

#map {
  inset: 0;
  position: absolute;
  z-index: 0;
}

/* ── Map Panels ── */
.map-panel {
  background: rgba(255, 255, 255, 0.97);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  position: absolute;
  z-index: 850;
}

.toolbar-panel {
  left: 16px;
  padding: 14px;
  top: 16px;
  width: 340px;
}

.summary-panel {
  max-height: calc(100% - 32px);
  overflow: auto;
  padding: 14px;
  right: 16px;
  top: 16px;
  width: 248px;
}

.legend-panel {
  bottom: 16px;
  left: 16px;
  padding: 12px 14px;
  width: 212px;
}

.panel-heading {
  align-items: center;
  display: flex;
  justify-content: space-between;
  margin-bottom: 12px;
  min-height: 28px;
}

.panel-heading h2 {
  color: var(--text-primary);
  font-size: 15px;
  font-weight: 700;
  line-height: 20px;
  margin: 0;
}

.panel-close {
  display: none;
}

/* ── Search Control ── */
.search-control {
  align-items: center;
  display: flex;
  position: relative;
}

.search-control > svg {
  color: var(--text-secondary);
  height: 17px;
  left: 11px;
  pointer-events: none;
  position: absolute;
  width: 17px;
  z-index: 1;
}

.search-control input {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  height: 40px;
  padding: 0 12px 0 36px;
  width: 100%;
}

.search-control input::placeholder {
  color: #74838a;
}

.search-results {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  left: 0;
  list-style: none;
  margin: 4px 0 0;
  max-height: 260px;
  overflow-y: auto;
  padding: 4px;
  position: absolute;
  right: 0;
  top: 100%;
  z-index: 1100;
}

.search-results button {
  background: transparent;
  border: 0;
  border-radius: 5px;
  cursor: pointer;
  display: grid;
  gap: 2px;
  padding: 9px 10px;
  text-align: left;
  width: 100%;
}

.search-results button:hover,
.search-results button:focus-visible {
  background: #edf4f6;
}

.search-result-name {
  color: var(--text-primary);
  font-size: 13px;
  font-weight: 650;
}

.search-result-meta {
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
}

/* ── Filter Row ── */
.filter-row {
  align-items: center;
  display: grid;
  gap: 8px;
  grid-template-columns: auto minmax(0, 1fr) 36px;
  margin-top: 10px;
}

.filter-row label {
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  font-weight: 650;
}

.filter-row select {
  appearance: none;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%235c6b73' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 10px center;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  height: 36px;
  padding: 0 34px 0 10px;
  width: 100%;
}

.reset-button {
  border-color: var(--border);
  color: var(--map-brand);
}

/* ── Segmented Control ── */
.segmented-control {
  background: #eaf0f2;
  border-radius: 7px;
  display: grid;
  gap: 2px;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 10px;
  padding: 3px;
}

.segmented-control button {
  background: transparent;
  border: 0;
  border-radius: 5px;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: var(--font-size-sm);
  font-weight: 650;
  height: 32px;
  padding: 0 8px;
}

.segmented-control button.is-active {
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(17, 33, 39, 0.15);
  color: var(--map-brand-strong);
}

/* ── Metrics Grid ── */
.metrics-grid {
  display: grid;
  gap: 8px;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin: 0 0 12px;
}

.metrics-grid > div {
  border-left: 2px solid var(--border);
  min-width: 0;
  padding-left: 8px;
}

.metrics-grid > div:first-child {
  border-left-color: var(--map-brand);
}

.metrics-grid > div:nth-child(2) {
  border-left-color: var(--map-blue);
}

.metrics-grid > div:nth-child(3) {
  border-left-color: var(--map-red);
}

.metrics-grid dt {
  color: var(--text-secondary);
  font-size: 9px;
  font-weight: 650;
  line-height: 12px;
}

.metrics-grid dd {
  color: var(--text-primary);
  font-size: var(--font-size-md);
  font-weight: 750;
  line-height: 22px;
  margin: 0;
}

/* ── Summary Table ── */
.summary-table-wrap {
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.summary-table {
  border-collapse: collapse;
  font-size: var(--font-size-sm);
  width: 100%;
}

.summary-table th,
.summary-table td {
  padding: 4px 2px;
}

.summary-table th {
  font-weight: 650;
  text-align: left;
}

.summary-table th:last-child,
.summary-table td:last-child {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.summary-table thead {
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
  text-transform: uppercase;
}

.summary-table tfoot {
  border-top: 1px solid var(--border);
  color: var(--map-brand-strong);
}

/* ── Legend ── */
.legend-panel .panel-heading {
  margin-bottom: 6px;
}

.legend-list {
  display: grid;
  gap: 7px;
  list-style: none;
  margin: 0;
  padding: 0;
}

.legend-list li {
  align-items: center;
  color: var(--text-secondary);
  display: flex;
  font-size: var(--font-size-sm);
  gap: 9px;
  min-height: 18px;
}

.legend-symbol {
  display: inline-flex;
  flex: 0 0 auto;
}

.legend-symbol.point {
  border: 2px solid var(--surface);
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

.legend-symbol.point.top3 {
  background: var(--map-red);
  height: 13px;
  width: 13px;
}

.legend-symbol.point.municipality {
  background: var(--map-blue);
  height: 11px;
  width: 11px;
}

.legend-symbol.cluster {
  align-items: center;
  background: var(--map-blue-dark);
  border: 2px solid rgba(255, 255, 255, 0.95);
  border-radius: 50%;
  color: var(--text-inverse);
  font-size: 8px;
  font-weight: 700;
  height: 18px;
  justify-content: center;
  width: 18px;
}

.legend-symbol.sector {
  background: rgba(20, 184, 166, 0.12);
  border: 1px solid var(--map-teal);
  height: 13px;
  width: 13px;
}

/* ── Sector Progress ── */
.sector-progress {
  align-items: center;
  background: rgba(255, 255, 255, 0.97);
  border: 1px solid var(--border);
  border-radius: 7px;
  bottom: 82px;
  box-shadow: var(--shadow-md);
  color: var(--text-secondary);
  display: flex;
  font-size: var(--font-size-xs);
  gap: 8px;
  min-height: 34px;
  padding: 7px 10px;
  position: absolute;
  right: 16px;
  transition: opacity 180ms ease;
  z-index: 840;
}

.sector-progress.is-complete {
  opacity: 0;
  pointer-events: none;
}

.progress-spinner {
  animation: spinner 850ms linear infinite;
  border: 2px solid #c8d5d9;
  border-radius: 50%;
  border-top-color: var(--map-teal);
  height: 14px;
  width: 14px;
}

@keyframes spinner {
  to { transform: rotate(360deg); }
}

/* ── Toast ── */
.app-toast {
  background: #fff8e8;
  border: 1px solid #e9c67c;
  border-radius: 7px;
  bottom: 16px;
  box-shadow: var(--shadow-lg);
  color: #704609;
  font-size: var(--font-size-sm);
  left: 50%;
  max-width: min(420px, calc(100% - 32px));
  padding: 10px 12px;
  position: absolute;
  transform: translateX(-50%);
  z-index: 1000;
}

/* ── Point Icons ── */
.point-icon {
  background: transparent;
  border: 0;
}

.point-dot {
  border: 2px solid var(--surface);
  border-radius: 50%;
  box-shadow: 0 1px 5px rgba(9, 25, 33, 0.42);
  display: block;
  height: 100%;
  width: 100%;
}

.point-dot.blue {
  background: var(--map-blue);
}

.point-dot.red {
  background: var(--map-red);
  box-shadow: 0 1px 6px rgba(122, 12, 12, 0.46);
}

/* ── Cluster Markers ── */
.municipality-cluster {
  background: transparent;
}

.cluster-marker {
  align-items: center;
  background: var(--map-blue-dark);
  border: 3px solid rgba(255, 255, 255, 0.94);
  border-radius: 50%;
  box-shadow: 0 3px 10px rgba(18, 56, 137, 0.32);
  color: var(--text-inverse);
  display: flex;
  font-size: var(--font-size-xs);
  font-weight: 750;
  height: 100%;
  justify-content: center;
  width: 100%;
}

.cluster-marker.medium {
  background: #1e55c7;
  font-size: var(--font-size-sm);
}

.cluster-marker.large {
  background: #17459f;
  font-size: 13px;
}

/* ── Info Popover ── */
.info-popover {
  color: var(--text-primary);
  font-family: var(--font-family);
  min-width: 220px;
}

.info-popover .info-eyebrow {
  color: var(--map-teal);
  font-size: var(--font-size-xs);
  font-weight: 750;
  margin-bottom: 2px;
  text-transform: uppercase;
}

.info-popover.municipality .info-eyebrow {
  color: var(--map-blue-dark);
}

.info-popover h3 {
  color: var(--text-primary);
  font-size: var(--font-size-base);
  line-height: 18px;
  margin: 0 0 8px;
}

.info-popover dl {
  display: grid;
  font-size: var(--font-size-sm);
  gap: 4px 10px;
  grid-template-columns: auto minmax(0, 1fr);
  line-height: 16px;
  margin: 0;
}

.info-popover dt {
  color: var(--text-secondary);
}

.info-popover dd {
  color: var(--text-primary);
  font-weight: 650;
  margin: 0;
  overflow-wrap: anywhere;
}

/* ── Leaflet Overrides ── */
.leaflet-container {
  background: #dce8ea;
  font-family: var(--font-family);
}

.leaflet-control-zoom {
  border: 0 !important;
  box-shadow: var(--shadow-lg) !important;
}

.leaflet-control-zoom a {
  border-color: var(--border) !important;
  color: var(--map-brand-strong) !important;
}

.leaflet-tooltip,
.leaflet-popup-content-wrapper {
  border: 1px solid var(--border);
  border-radius: 7px;
  box-shadow: var(--shadow-lg);
}

.leaflet-tooltip {
  padding: 10px 12px;
}

.leaflet-popup-content {
  margin: 13px 15px;
}

.leaflet-popup-close-button {
  color: var(--text-secondary) !important;
}

/* ── Responsive ── */
@media (max-width: 767px) {
  .map-shell {
    height: calc(100dvh - var(--miv-shell-header-height) - var(--map-header-height) - var(--miv-bottom-nav-height));
  }

  .app-header {
    gap: 8px;
    padding: 0 8px;
  }

  .brand {
    gap: 7px;
    overflow: hidden;
  }

  .brand-logo {
    height: 32px;
    width: 72px;
  }

  .brand-kicker,
  .title-desktop {
    display: none;
  }

  .title-mobile {
    display: block;
  }

  .brand h1 {
    font-size: 13px;
    line-height: 16px;
    white-space: normal;
  }

  .mobile-actions {
    display: flex;
    flex: 0 0 auto;
    gap: 2px;
  }

  .mobile-actions .icon-button {
    height: 34px;
    width: 34px;
  }

  .mobile-actions .icon-button[aria-expanded="true"] {
    background: #e8f1f3;
    border-color: #bad0d5;
    color: var(--map-brand-strong);
  }

  .map-panel {
    bottom: auto;
    display: none;
    left: 10px;
    max-height: calc(100% - 20px);
    overflow-y: auto;
    right: 10px;
    top: 10px;
    width: auto;
    z-index: 950;
  }

  .map-panel.is-open {
    display: block;
  }

  .toolbar-panel,
  .summary-panel,
  .legend-panel {
    padding: 14px;
  }

  .panel-close {
    display: inline-flex;
  }

  .summary-panel,
  .legend-panel {
    max-width: 360px;
    width: calc(100% - 20px);
  }

  .summary-panel {
    left: auto;
  }

  .legend-panel {
    right: auto;
    width: 230px;
  }

  .sector-progress {
    bottom: 14px;
    left: 10px;
    right: auto;
  }

  .leaflet-control-zoom {
    margin-bottom: 12px !important;
    margin-right: 10px !important;
  }

  .leaflet-control-attribution {
    max-width: calc(100vw - 110px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .leaflet-tooltip {
    display: none;
  }
}

@media (max-width: 380px) {
  .brand-logo {
    height: 29px;
    width: 62px;
  }

  .brand-copy {
    max-width: 74px;
  }

  .mobile-actions .icon-button {
    height: 32px;
    width: 32px;
  }

  .mobile-actions .icon-button svg {
    height: 17px;
    width: 17px;
  }
}

@media (hover: none) {
  .leaflet-tooltip {
    display: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }

  .progress-spinner {
    animation-duration: 1.8s;
  }
}
