/* Spatial analysis extension for the existing data-analysis studio. */
.map-analysis-section {
  --map-accent: #316c72;
  --map-accent-rgb: 49, 108, 114;
  --map-soft: #e8f1f0;
  position: relative;
  padding: clamp(72px, 8vw, 112px) 0;
  border-top: 1px solid var(--line);
  background:
    linear-gradient(90deg, transparent 0 67%, rgba(var(--map-accent-rgb), .045) 67% 100%),
    repeating-linear-gradient(0deg, transparent 0 55px, rgba(var(--map-accent-rgb), .045) 55px 56px),
    var(--paper);
}

html[data-theme="dark"] .map-analysis-section {
  --map-accent: #74c6c4;
  --map-accent-rgb: 116, 198, 196;
  --map-soft: rgba(116, 198, 196, .12);
}

.map-section-heading { margin-bottom: 30px; }
.map-section-heading > div > p { color: var(--map-accent); }

.map-mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border: 1px solid var(--line-strong);
  border-bottom: 0;
  background: var(--surface);
}

.map-mode-tabs button {
  position: relative;
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr);
  grid-template-rows: auto auto;
  gap: 2px 14px;
  min-height: 86px;
  border: 0;
  border-right: 1px solid var(--line);
  padding: 18px 20px;
  color: var(--text);
  text-align: left;
  background: var(--surface);
  transition: color var(--motion-fast) ease, background var(--motion-fast) ease;
}

.map-mode-tabs button:last-child { border-right: 0; }
.map-mode-tabs button::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  height: 3px;
  background: transparent;
}
.map-mode-tabs button > span {
  grid-row: 1 / 3;
  align-self: center;
  color: var(--map-accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .14em;
}
.map-mode-tabs b { color: var(--heading); font-size: 17px; letter-spacing: -.015em; }
.map-mode-tabs small { color: var(--muted); font-size: 13px; font-weight: 520; }
.map-mode-tabs button:hover,
.map-mode-tabs button:focus-visible { background: var(--surface-soft); }
.map-mode-tabs button[aria-selected="true"] { color: var(--map-accent); background: var(--map-soft); }
.map-mode-tabs button[aria-selected="true"]::after { background: var(--map-accent); }

.map-workbench {
  display: grid;
  grid-template-columns: minmax(360px, 430px) minmax(0, 1fr);
  align-items: stretch;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
}

.map-control-panel {
  min-width: 0;
  border-right: 1px solid var(--line-strong);
  background: var(--surface);
}

.map-control-heading,
.map-result-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 78px;
  padding: 17px 20px;
  border-bottom: 1px solid var(--line);
}
.map-control-heading span,
.map-result-heading span {
  display: block;
  margin-bottom: 3px;
  color: var(--map-accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .12em;
}
.map-control-heading h3,
.map-result-heading h3 { margin: 0; color: var(--heading); font-size: 20px; letter-spacing: -.025em; }
.map-control-heading > b {
  flex: none;
  border: 1px solid rgba(var(--map-accent-rgb), .35);
  padding: 5px 9px;
  color: var(--map-accent);
  background: var(--map-soft);
  font-size: 12px;
}

.map-control-group { padding: 20px; border-bottom: 1px solid var(--line); }
.map-control-label { display: flex; align-items: baseline; gap: 10px; margin-bottom: 15px; }
.map-control-label > span { color: var(--map-accent); font-size: 11px; font-weight: 800; letter-spacing: .12em; }
.map-control-label h4 { margin: 0; color: var(--heading); font-size: 15px; letter-spacing: -.01em; }

.map-source-actions { display: grid; grid-template-columns: 1.15fr 1fr .72fr; gap: 8px; margin-bottom: 14px; }
.map-source-actions :where(button, .map-file-button),
.map-export-actions button,
.map-copy-actions button {
  min-height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  padding: 8px 10px;
  color: var(--heading);
  background: var(--surface-soft);
  font-size: 13px;
  font-weight: 680;
  text-align: center;
  transition: border-color var(--motion-fast) ease, color var(--motion-fast) ease, background var(--motion-fast) ease;
}
.map-source-actions :where(button, .map-file-button):hover,
.map-source-actions :where(button, .map-file-button):focus-within,
.map-export-actions button:hover:not(:disabled),
.map-export-actions button:focus-visible:not(:disabled),
.map-copy-actions button:hover:not(:disabled),
.map-copy-actions button:focus-visible:not(:disabled) {
  border-color: var(--map-accent);
  color: var(--map-accent);
  background: var(--map-soft);
}
.map-file-button { display: grid; place-items: center; cursor: pointer; }
.map-file-button input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

.map-field { display: grid; gap: 7px; min-width: 0; color: var(--heading); font-weight: 650; }
.map-field > span { font-size: 13px; }
.map-field :where(input, textarea, select) {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: 2px;
  padding: 9px 11px;
  color: var(--text);
  background: var(--surface-soft);
  font: inherit;
  font-size: 13px;
  font-weight: 520;
  outline: none;
}
.map-field textarea {
  min-height: 155px;
  resize: vertical;
  font-family: ui-monospace, "SFMono-Regular", Consolas, monospace;
  line-height: 1.55;
  tab-size: 2;
}
.map-field :where(input, textarea, select):focus {
  border-color: var(--map-accent);
  background: var(--surface);
  box-shadow: 0 0 0 3px rgba(var(--map-accent-rgb), .12);
}
.map-field :where(input, textarea, select)[aria-invalid="true"] { border-color: var(--danger); }
.map-field small { color: var(--muted); font-size: 12px; font-weight: 480; line-height: 1.55; }

.map-field-grid,
.map-filter-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px 10px; }
.map-field-wide { grid-column: 1 / -1; }

.map-filter-builder { margin-top: 13px; border-top: 1px solid var(--line); padding-top: 12px; }
.map-filter-builder summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  color: var(--map-accent);
  font-size: 13px;
  font-weight: 720;
  cursor: pointer;
}
.map-filter-builder summary span { color: var(--muted); font-size: 11px; font-weight: 600; }
.map-filter-builder[open] summary { margin-bottom: 13px; }
.map-filter-grid { grid-template-columns: 1fr 1fr; }
.map-filter-grid .map-field:first-child { grid-column: 1 / -1; }

.map-toggle-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-top: 14px; }
.map-toggle-grid label {
  display: flex;
  align-items: center;
  min-height: 38px;
  border: 1px solid var(--line);
  padding: 8px 10px;
  color: var(--muted);
  background: var(--surface-soft);
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}
.map-toggle-grid input { width: 15px; height: 15px; margin: 0 8px 0 0; accent-color: var(--map-accent); }
.map-toggle-grid label:has(input:checked) { border-color: rgba(var(--map-accent-rgb), .45); color: var(--map-accent); background: var(--map-soft); }

.map-form-actions { display: flex; gap: 9px; padding: 20px 20px 0; }
.map-form-actions .button { min-height: 44px; border-radius: 2px; font-size: 13px; }
.map-form-actions .button:first-child { border-color: var(--map-accent); color: #fff; background: var(--map-accent); }
.map-control-panel > .studio-message { min-height: 46px; margin: 0; padding: 12px 20px 20px; font-size: 12px; }

.map-result-board { min-width: 0; background: var(--surface-soft); }
.map-result-heading { background: var(--surface); }
.map-export-actions { display: flex; flex-wrap: wrap; gap: 7px; }
.map-export-actions button { min-height: 35px; }
.map-export-actions button:disabled,
.map-copy-actions button:disabled { opacity: .45; cursor: not-allowed; }

.map-result-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border-bottom: 1px solid var(--line); background: var(--surface); }
.map-result-metrics article { min-width: 0; min-height: 102px; border-right: 1px solid var(--line); padding: 16px; }
.map-result-metrics article:last-child { border-right: 0; }
.map-result-metrics span,
.map-result-metrics strong,
.map-result-metrics small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.map-result-metrics span { color: var(--muted); font-size: 11px; font-weight: 700; }
.map-result-metrics strong { margin-top: 7px; color: var(--heading); font-size: 22px; letter-spacing: -.03em; }
.map-result-metrics small { margin-top: 4px; color: var(--muted); font-size: 11px; }

.map-result-tabs { display: flex; min-height: 47px; border-bottom: 1px solid var(--line); background: var(--surface); }
.map-result-tabs button {
  position: relative;
  min-width: 88px;
  border: 0;
  padding: 13px 16px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  font-weight: 680;
}
.map-result-tabs button::after { content: ""; position: absolute; right: 16px; bottom: -1px; left: 16px; height: 2px; background: transparent; }
.map-result-tabs button[aria-selected="true"] { color: var(--map-accent); }
.map-result-tabs button[aria-selected="true"]::after { background: var(--map-accent); }

.map-result-panel { min-height: 585px; padding: 18px; }
.map-canvas-shell {
  position: relative;
  min-height: 520px;
  border: 1px solid var(--line-strong);
  overflow: hidden;
  background:
    linear-gradient(90deg, transparent 49.9%, rgba(var(--map-accent-rgb), .045) 50%, transparent 50.1%),
    linear-gradient(0deg, transparent 49.9%, rgba(var(--map-accent-rgb), .045) 50%, transparent 50.1%),
    var(--surface);
}
#spatialMap { display: block; width: 100%; height: auto; min-height: 520px; font-family: var(--font); }
#spatialMap .map-region { vector-effect: non-scaling-stroke; transition: fill 150ms ease, opacity 150ms ease; }
#spatialMap .map-region:hover { stroke-width: 1.65; opacity: .84; }
#spatialMap .map-point { vector-effect: non-scaling-stroke; transition: r 150ms ease, opacity 150ms ease; }
#spatialMap .map-point:hover { opacity: .78; }
#spatialMap .map-title { fill: var(--heading); font-size: 22px; font-weight: 680; letter-spacing: -.02em; }
#spatialMap .map-subtitle { fill: var(--muted); font-size: 11px; font-weight: 620; letter-spacing: .08em; }
#spatialMap .map-coordinate { fill: var(--muted); font-size: 9px; }
#spatialMap .map-graticule { fill: none; stroke: var(--line); stroke-width: .7; vector-effect: non-scaling-stroke; }
#spatialMap .map-compass-text { fill: var(--heading); font-size: 12px; font-weight: 800; text-anchor: middle; }
#spatialMap .map-scale-text { fill: var(--muted); font-size: 10px; }

.map-empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  max-width: 420px;
  margin: auto;
  padding: 30px;
  text-align: center;
}
.map-empty-state span { color: var(--map-accent); font-size: 11px; font-weight: 800; letter-spacing: .14em; }
.map-empty-state h4 { margin: 10px 0 8px; color: var(--heading); font-size: 24px; letter-spacing: -.03em; }
.map-empty-state p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.7; }

.map-tooltip {
  position: absolute;
  z-index: 5;
  max-width: 240px;
  border: 1px solid var(--line-strong);
  padding: 10px 12px;
  color: var(--text);
  background: var(--surface-alpha);
  box-shadow: var(--shadow-soft);
  backdrop-filter: blur(12px);
  pointer-events: none;
  font-size: 12px;
  line-height: 1.55;
}
.map-tooltip strong { display: block; color: var(--heading); font-size: 13px; }
.map-legend { position: absolute; right: 18px; bottom: 18px; min-width: 170px; border: 1px solid var(--line); padding: 10px; background: var(--surface-alpha); backdrop-filter: blur(10px); }
.map-legend-gradient { height: 8px; background: linear-gradient(90deg, #edf2ef, #b7d7cf, #609e94, #1f5f60); }
.map-legend-scale { display: flex; justify-content: space-between; margin-top: 5px; color: var(--muted); font-size: 10px; }
.map-legend-clusters { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 9px; color: var(--muted); font-size: 10px; }
.map-legend-clusters i { display: inline-block; width: 9px; height: 9px; margin-right: 5px; vertical-align: -1px; }
.map-source-note { margin: 10px 0 0; color: var(--muted); font-size: 11px; line-height: 1.55; }

.map-table-wrap { max-height: 540px; overflow: auto; border: 1px solid var(--line); background: var(--surface); }
.map-table-wrap th { position: sticky; top: 0; z-index: 1; background: var(--surface-soft); }
.map-log { min-height: 520px; margin: 0; border: 1px solid var(--line); padding: 18px; overflow: auto; color: #dce9e2; background: #10231b; font: 12px/1.75 ui-monospace, "SFMono-Regular", Consolas, monospace; white-space: pre-wrap; }
.map-interpretation-card { min-height: 520px; border: 1px solid var(--line); padding: clamp(24px, 4vw, 44px); background: var(--surface); }
.map-interpretation-card > span { color: var(--map-accent); font-size: 11px; font-weight: 800; letter-spacing: .14em; }
.map-interpretation-card h4 { margin: 10px 0 22px; color: var(--heading); font-size: 27px; letter-spacing: -.035em; }
#mapInterpretation { color: var(--text); font-size: 14px; line-height: 1.85; }
#mapInterpretation p { margin: 0 0 16px; }
#mapInterpretation dl { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1px; margin: 24px 0; background: var(--line); }
#mapInterpretation dl div { padding: 14px; background: var(--surface-soft); }
#mapInterpretation dt { color: var(--muted); font-size: 11px; font-weight: 720; }
#mapInterpretation dd { margin: 5px 0 0; color: var(--heading); font-size: 15px; font-weight: 680; }
.map-copy-actions { display: flex; flex-wrap: wrap; gap: 9px; margin-top: 25px; }

.map-method-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); margin-top: 24px; border: 1px solid var(--line-strong); background: var(--surface); }
.map-method-grid article { min-height: 205px; border-right: 1px solid var(--line); padding: 24px; }
.map-method-grid article:last-child { border-right: 0; }
.map-method-grid span { color: var(--map-accent); font-size: 11px; font-weight: 800; letter-spacing: .13em; }
.map-method-grid h3 { margin: 13px 0 10px; color: var(--heading); font-size: 19px; letter-spacing: -.025em; }
.map-method-grid p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.7; }

@media (max-width: 1100px) {
  .map-workbench { grid-template-columns: 380px minmax(0, 1fr); }
  .map-result-metrics { grid-template-columns: 1fr 1fr; }
  .map-result-metrics article:nth-child(2) { border-right: 0; }
  .map-result-metrics article:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
}

@media (max-width: 900px) {
  .map-workbench { grid-template-columns: 1fr; }
  .map-control-panel { border-right: 0; border-bottom: 1px solid var(--line-strong); }
  .map-field-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .map-field-wide { grid-column: auto; }
  .map-result-panel { min-height: 0; }
  .map-method-grid { grid-template-columns: 1fr; }
  .map-method-grid article { min-height: 0; border-right: 0; border-bottom: 1px solid var(--line); }
  .map-method-grid article:last-child { border-bottom: 0; }
}

@media (max-width: 680px) {
  .map-analysis-section { padding: 58px 0 72px; }
  .map-mode-tabs { grid-template-columns: 1fr; border-bottom: 1px solid var(--line-strong); }
  .map-mode-tabs button { min-height: 72px; border-right: 0; border-bottom: 1px solid var(--line); padding: 13px 16px; }
  .map-mode-tabs button:last-child { border-bottom: 0; }
  .map-mode-tabs button::after { top: 0; right: auto; width: 3px; height: auto; }
  .map-workbench { border-top: 0; }
  .map-field-grid { grid-template-columns: 1fr 1fr; }
  .map-control-heading,
  .map-result-heading { align-items: flex-start; flex-direction: column; min-height: 0; }
  .map-export-actions { width: 100%; }
  .map-export-actions button { flex: 1 1 0; }
  .map-result-tabs { overflow-x: auto; }
  .map-result-tabs button { min-width: 82px; padding-inline: 12px; }
  .map-result-panel { padding: 12px; }
  .map-canvas-shell,
  #spatialMap { min-height: 420px; }
  .map-source-note { padding-inline: 2px; }
}

@media (max-width: 440px) {
  .map-control-group { padding: 17px 15px; }
  .map-control-heading,
  .map-result-heading { padding: 16px 15px; }
  .map-source-actions { grid-template-columns: 1fr 1fr; }
  .map-source-actions > :first-child { grid-column: 1 / -1; }
  .map-field-grid,
  .map-filter-grid,
  .map-toggle-grid { grid-template-columns: 1fr; }
  .map-field-wide,
  .map-filter-grid .map-field:first-child { grid-column: auto; }
  .map-form-actions { padding-inline: 15px; }
  .map-form-actions .button { flex: 1 1 0; }
  .map-result-metrics { grid-template-columns: 1fr; }
  .map-result-metrics article,
  .map-result-metrics article:nth-child(2) { border-right: 0; border-bottom: 1px solid var(--line); }
  .map-result-metrics article:last-child { border-bottom: 0; }
  .map-canvas-shell,
  #spatialMap { min-height: 360px; }
  .map-legend { right: 9px; bottom: 9px; }
  #mapInterpretation dl { grid-template-columns: 1fr; }
}
