/* ============================================================================
   Meteo Online — карта
   Один экземпляр карты; контейнер меняет слот средствами CSS:
     телефон  — вкладка «Карта» на весь экран под шапкой;
     планшет  — секция внутри потока (56 vh), разворачивается;
     десктоп  — липкая колонка справа, разворачивается на весь экран.
   ========================================================================== */

/* --- Хост (телефон) ------------------------------------------------------- */
.map-host {
  display: none;
  position: fixed;
  inset: var(--header-h) 0 0 0;
  z-index: var(--z-view);
  background: var(--map-water);
}

body[data-view="map"] .map-host {
  display: block;
}

/* Изоляция держит внутренние z-index Leaflet (до 1000) внутри рамки */
.map-frame {
  position: absolute;
  inset: 0;
  isolation: isolate;
  overflow: hidden;
}

.map {
  position: absolute;
  inset: 0;
  background: var(--map-water);
  font-family: var(--font-sans);
  touch-action: none;
}

/* Тёмная тема: фильтр только на слое тайлов, подписи и наложения не трогаем */
.leaflet-tile-pane {
  filter: var(--tile-filter);
}

.leaflet-container {
  font-family: var(--font-sans);
  outline: none;
}

.leaflet-container a {
  color: inherit;
}

/* --- Чип места ------------------------------------------------------------ */
.map-chip {
  position: absolute;
  top: var(--sp-3);
  left: var(--sp-3);
  z-index: 450;
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  max-width: calc(100% - 72px);
  min-height: 44px;
  padding: 5px var(--sp-3) 5px 6px;
  border-radius: var(--r-pill);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-3);
  color: var(--ink);
  text-align: left;
  -webkit-tap-highlight-color: transparent;
}

.map-chip-icon {
  width: 30px;
  height: 30px;
}

.map-chip-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  line-height: var(--leading-snug);
}

.map-chip-line {
  display: flex;
  align-items: baseline;
  gap: var(--sp-2);
  min-width: 0;
}

.map-chip-name {
  font-weight: var(--weight-bold);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.map-chip-temp {
  font-weight: var(--weight-heavy);
}

.map-chip-meta {
  display: none;
  color: var(--ink-3);
  font-size: var(--text-xs);
  white-space: nowrap;
}

.map-chip-arrow {
  width: 16px;
  height: 16px;
  color: var(--ink-3);
}

/* --- Инструменты справа сверху -------------------------------------------- */
.map-tools {
  position: absolute;
  top: var(--sp-3);
  right: var(--sp-3);
  z-index: 450;
  display: grid;
  justify-items: end;
  gap: var(--sp-2);
}

.map-btn-group {
  display: grid;
  overflow: hidden;
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-3);
}

.map-btn-group .map-btn + .map-btn {
  border-top: 1px solid var(--line);
}

.map-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  color: var(--ink);
  background: var(--surface);
  transition: background-color var(--dur-fast) var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}

.map-btn:hover {
  background: var(--surface-2);
}

.map-btn:active {
  background: var(--surface-3);
}

.map-btn--single {
  border-radius: var(--r-md);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-3);
}

.map-btn .icon {
  width: 20px;
  height: 20px;
}

.map-btn .icon-collapse,
body.map-expanded .map-btn-expand .icon-expand {
  display: none;
}

body.map-expanded .map-btn-expand .icon-collapse {
  display: block;
}

.map-attrib {
  padding: 3px 10px;
  border-radius: var(--r-pill);
  background: var(--surface-translucent);
  color: var(--ink-3);
  font-size: 10px;
  font-weight: var(--weight-medium);
  white-space: nowrap;
}

.map-attrib a {
  color: var(--ink-2);
  text-underline-offset: 2px;
}

/* --- Док: слои и легенда --------------------------------------------------- */
.map-dock {
  position: absolute;
  left: var(--sp-3);
  right: var(--sp-3);
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px) + var(--sp-3));
  z-index: 450;
  display: grid;
  /* minmax(0, …): несжимаемая полоса слоёв не должна распирать док */
  grid-template-columns: minmax(0, 1fr);
  gap: var(--sp-2);
  pointer-events: none;
}

.layer-bar {
  pointer-events: auto;
  display: flex;
  gap: 2px;
  width: max-content;
  min-width: 0;
  max-width: 100%;
  padding: 4px;
  border-radius: var(--r-pill);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-3);
  scroll-snap-type: x proximity;
}

.layer-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex: none;
  min-height: 40px;
  padding: 0 var(--sp-3);
  border-radius: var(--r-pill);
  color: var(--ink-2);
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  white-space: nowrap;
  scroll-snap-align: start;
  scroll-snap-stop: always;
  transition: background-color var(--dur-fast) var(--ease-out), color var(--dur-fast) var(--ease-out);
  -webkit-tap-highlight-color: transparent;
}

.layer-btn:hover {
  background: var(--surface-2);
  color: var(--ink);
}

.layer-btn[aria-checked="true"] {
  background: var(--ink);
  color: var(--ink-inverse);
}

.layer-btn .icon {
  width: 16px;
  height: 16px;
}

.legend {
  pointer-events: auto;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: var(--sp-2) var(--sp-4);
  padding: var(--sp-3) var(--sp-4);
  border-radius: var(--r-md);
  background: var(--surface);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-3);
  transition: opacity var(--dur) var(--ease-out);
}

.legend[data-state="loading"] {
  opacity: 0.7;
}

/* min-width: auto — колонка не уже заголовка, иначе он обрезается */
.legend-readout {
  display: flex;
  flex-direction: column;
  min-width: auto;
  line-height: var(--leading-snug);
}

.legend-title {
  font-size: var(--text-sm);
  font-weight: var(--weight-bold);
  white-space: nowrap;
}

.legend-value {
  color: var(--ink-2);
  font-size: var(--text-xs);
  font-weight: var(--weight-bold);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.legend-scale-wrap {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.legend-scale {
  height: 8px;
  border-radius: var(--r-pill);
  background: var(--surface-3);
}

.legend-ticks {
  display: flex;
  justify-content: space-between;
  gap: var(--sp-2);
  color: var(--ink-3);
  font-size: 11px;
  font-weight: var(--weight-bold);
  white-space: nowrap;
}

.legend-note {
  display: none;
}

/* --- Метка точки: таблетка с температурой --------------------------------- */
.temp-pin {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, -100%);
}

.temp-marker {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 46px;
  min-height: 32px;
  padding: 2px 12px;
  border-radius: var(--r-pill);
  background: var(--ink);
  color: var(--ink-inverse);
  font-family: var(--font-sans);
  font-size: var(--text-sm);
  font-weight: var(--weight-heavy);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  box-shadow: var(--shadow-2);
}

.temp-marker::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -4px;
  width: 10px;
  height: 10px;
  border-radius: 2px;
  background: inherit;
  transform: translateX(-50%) rotate(45deg);
}

/* Точка на карте — центр нижнего края метки: от неё расходится пульс */
.temp-pulse {
  position: absolute;
  left: 50%;
  bottom: -7px;
  width: 14px;
  height: 14px;
  margin-left: -7px;
  margin-bottom: -7px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0;
  animation: pin-pulse 2.6s var(--ease-out) infinite;
}

@keyframes pin-pulse {
  0% {
    opacity: 0.5;
    transform: scale(0.6);
  }
  70%,
  100% {
    opacity: 0;
    transform: scale(3.4);
  }
}

.temp-pin.is-pending .temp-marker {
  color: transparent;
}

.temp-pin.is-pending .temp-marker::before {
  content: "";
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid var(--line-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 700ms linear infinite;
}

/* --- Поле слоя ------------------------------------------------------------ */
.field-overlay {
  opacity: 0;
  transition: opacity var(--dur-slow) var(--ease-in-out);
  will-change: opacity;
}

.field-overlay.is-shown {
  opacity: var(--field-opacity, 0.66);
}

/* Подписи значений в узлах сетки */
.field-label {
  position: absolute;
  left: 0;
  top: 0;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 1px 7px;
  border-radius: var(--r-sm);
  background: var(--surface-translucent);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: var(--weight-bold);
  font-variant-numeric: tabular-nums;
  line-height: 1.5;
  white-space: nowrap;
  box-shadow: var(--shadow-1);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.field-arrow {
  width: 13px;
  height: 13px;
  color: var(--accent);
}

/* ============================================================================
   Планшет (≥768): секция в потоке + полноэкранный режим
   ========================================================================== */
@media (min-width: 768px) {
  .map-host {
    display: block;
    position: relative;
    inset: auto;
    z-index: auto;
    height: 56vh;
    min-height: 340px;
    border-radius: var(--r-lg);
    border: 1px solid var(--line);
    box-shadow: var(--shadow-2);
    overflow: hidden;
  }

  .map-frame {
    border-radius: inherit;
  }

  .map-chip {
    max-width: min(380px, calc(100% - 72px));
  }

  .map-chip-arrow {
    display: none;
  }

  .map-chip-meta {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .map-dock {
    bottom: var(--sp-4);
  }

  .legend {
    grid-template-columns: auto minmax(120px, 220px) auto;
  }

  .legend-note {
    display: block;
    color: var(--ink-3);
    font-size: var(--text-xs);
    font-weight: var(--weight-bold);
    white-space: nowrap;
  }

  body.map-expanded .map-host {
    position: fixed;
    inset: var(--header-h) 0 0 0;
    z-index: calc(var(--z-view) + 5);
    height: auto;
    min-height: 0;
    border-radius: 0;
    border: 0;
    box-shadow: none;
  }

  body.map-expanded {
    overflow: hidden;
  }

  .map-btn-expand {
    display: grid;
  }
}

/* ============================================================================
   Десктоп (≥1024): липкая колонка
   ========================================================================== */
@media (min-width: 1024px) {
  .map-host {
    position: sticky;
    top: calc(var(--header-h) + var(--sp-4));
    height: calc(100vh - var(--header-h) - var(--sp-4) * 2);
    height: calc(100dvh - var(--header-h) - var(--sp-4) * 2);
    min-height: 440px;
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-1);
  }

  body.map-expanded .map-host {
    position: fixed;
    top: var(--header-h);
    height: auto;
    min-height: 0;
    border-radius: 0;
  }
}

/* Телефон: кнопки разворачивания нет — вкладка и есть полный экран */
@media (max-width: 767px) {
  .map-btn-expand {
    display: none;
  }
}

/* Тайл не загрузился ни напрямую, ни через релей: не показывать «битую» картинку */
.leaflet-tile.tile-failed {
  visibility: hidden;
}

.map-btn:disabled {
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
}
