:root {
  --sos-orange: #ff6a00;
  --sos-dark: rgba(8, 12, 16, .78);
  --sos-panel: rgba(10, 15, 20, .82);
  --sos-border: rgba(255,255,255,.14);
  --sos-text: #f4f4f4;
  --sos-muted: rgba(255,255,255,.68);
}

* {
  box-sizing: border-box;
}

html,
body,
.map4-shell,
#map {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #05080b;
  color: var(--sos-text);
  overflow: hidden;
}

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

.map4-topbar {
  position: fixed;
  z-index: 500;
  top: 12px;
  left: 12px;
  right: 12px;
  height: 54px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px;
  background: var(--sos-dark);
  border: 1px solid var(--sos-border);
  border-radius: 18px;
  backdrop-filter: blur(14px);
  box-shadow: 0 16px 40px rgba(0,0,0,.32);
}

.topbar-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--sos-border);
  border-radius: 12px;
  background: rgba(255,255,255,.06);
  color: white;
  cursor: pointer;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-weight: 800;
  letter-spacing: .02em;
}

.brand img {
  width: 42px;
  height: auto;
}

.live-badge {
  margin-left: auto;
  padding: 6px 10px;
  border-radius: 999px;
  background: rgba(255,106,0,.16);
  border: 1px solid rgba(255,106,0,.55);
  color: #ffd4b5;
  font-size: 12px;
  font-weight: 900;
}

.map4-sidebar {
  position: fixed;
  z-index: 450;
  top: 78px;
  left: 12px;
  width: 326px;
  max-height: calc(100vh - 96px);
  overflow: auto;
  padding: 14px;
  background: var(--sos-panel);
  border: 1px solid var(--sos-border);
  border-radius: 20px;
  backdrop-filter: blur(16px);
  box-shadow: 0 18px 48px rgba(0,0,0,.36);
  transition: transform .25s ease, opacity .25s ease;
}

.map4-sidebar:not(.open) {
  transform: translateX(-112%);
  opacity: 0;
  pointer-events: none;
}

.panel-section {
  padding: 14px 0;
  border-bottom: 1px solid rgba(255,255,255,.1);
}

.panel-section:last-child {
  border-bottom: 0;
}

.section-title {
  margin-bottom: 10px;
  color: #fff;
  font-size: 13px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: .08em;
}

.tech-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 5px 0;
  color: var(--sos-muted);
  font-size: 13px;
}

.tech-row strong {
  color: #fff;
  font-weight: 800;
  text-align: right;
}

.map4-select,
.map4-input {
  width: 100%;
  height: 40px;
  border: 1px solid var(--sos-border);
  border-radius: 12px;
  background: rgba(255,255,255,.08);
  color: white;
  padding: 0 12px;
  outline: none;
}

.map4-select option {
  color: #111;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 0;
  color: var(--sos-text);
  font-size: 14px;
}

.switch-row input {
  display: none;
}

.switch-row i {
  width: 48px;
  height: 26px;
  border-radius: 999px;
  background: rgba(255,255,255,.14);
  position: relative;
  border: 1px solid var(--sos-border);
}

.switch-row i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: white;
  transition: transform .2s ease;
}

.switch-row input:checked + i {
  background: rgba(255,106,0,.58);
}

.switch-row input:checked + i::after {
  transform: translateX(22px);
}

.legend p {
  margin: 8px 0;
  color: var(--sos-muted);
  font-size: 13px;
}

.dot {
  display: inline-block;
  width: 11px;
  height: 11px;
  margin-right: 8px;
  border-radius: 50%;
}

.dot.live { background: #ff6a00; box-shadow: 0 0 14px rgba(255,106,0,.9); }
.dot.stale { background: #d9b36c; }
.dot.offline { background: #7f8790; }

.map-controls {
  position: fixed;
  right: 14px;
  bottom: 18px;
  z-index: 470;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.map-controls button,
.map-controls select {
  min-width: 44px;
  min-height: 42px;
  border: 1px solid var(--sos-border);
  border-radius: 14px;
  background: var(--sos-panel);
  color: white;
  backdrop-filter: blur(12px);
  box-shadow: 0 10px 28px rgba(0,0,0,.28);
  cursor: pointer;
}

.map-controls select {
  padding: 0 10px;
}

.map-controls select option {
  color: #111;
}

.map4-marker {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.9);
  box-shadow: 0 0 0 4px rgba(255,106,0,.18), 0 0 18px rgba(255,106,0,.8);
  background: var(--sos-orange);
}

.map4-marker.mediumfast {
  background: #00bcd4;
  box-shadow: 0 0 0 4px rgba(0,188,212,.16), 0 0 18px rgba(0,188,212,.65);
}

.map4-marker.stale {
  filter: grayscale(.35);
  opacity: .68;
  box-shadow: none;
}

.map4-marker.offline {
  background: #717b84;
  opacity: .42;
  box-shadow: none;
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: rgba(8, 12, 16, .94);
  color: white;
}

.leaflet-popup-content {
  margin: 12px 14px;
  font-size: 13px;
}

.popup-title {
  font-weight: 900;
  font-size: 16px;
  margin-bottom: 4px;
}

.popup-muted {
  color: rgba(255,255,255,.66);
}

/* ============================================================
   MAP4 v0.1 — Mobile optimization
   ============================================================ */

@media (max-width: 720px) {
  .map4-topbar {
    height: 58px;
    top: 10px;
    left: 10px;
    right: 10px;
    padding: 0 12px;
    border-radius: 22px;
    gap: 10px;
  }

  .topbar-btn {
    width: 42px;
    height: 42px;
    border-radius: 16px;
    font-size: 20px;
  }

  .brand {
    gap: 10px;
    min-width: 0;
  }

  .brand img {
    width: 46px;
  }

  .brand span {
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .live-badge {
    padding: 7px 10px;
    font-size: 11px;
    white-space: nowrap;
  }

  .map4-sidebar {
    top: 78px;
    left: 10px;
    width: calc(100vw - 20px);
    max-height: calc(100vh - 96px);
    border-radius: 22px;
  }

  .map-controls {
    right: 10px;
    bottom: 16px;
    gap: 7px;
    align-items: flex-end;
  }

  .map-controls button {
    width: 54px;
    min-width: 54px;
    height: 44px;
    min-height: 44px;
    border-radius: 16px;
    font-size: 18px;
  }

  .map-controls select {
    width: 158px;
    min-width: 158px;
    height: 44px;
    min-height: 44px;
    border-radius: 16px;
    font-size: 14px;
    padding: 0 10px;
  }
}

@media (max-width: 420px) {
  .brand img {
    width: 42px;
  }

  .brand span {
    font-size: 14px;
  }

  .live-badge {
    font-size: 10px;
    padding: 6px 8px;
  }

  .map-controls button {
    width: 48px;
    min-width: 48px;
    height: 42px;
    min-height: 42px;
  }

  .map-controls select {
    width: 148px;
    min-width: 148px;
    height: 42px;
    min-height: 42px;
    font-size: 13px;
  }
}

/* ============================================================
   MAP4 v0.1 — Basemap + marker logic update
   ============================================================ */

.map4-marker {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.9);
  background: var(--sos-orange);
  box-shadow:
    0 0 0 4px rgba(255,106,0,.16),
    0 0 18px rgba(255,106,0,.7);
}

.map4-marker.sos-node {
  background: var(--sos-orange);
}

.map4-marker.external-node {
  background: #00bcd4;
  box-shadow:
    0 0 0 4px rgba(0,188,212,.14),
    0 0 18px rgba(0,188,212,.62);
}

.map4-marker.infra-node {
  border: 3px solid #fff2e6;
  outline: 3px solid rgba(255,106,0,.85);
  outline-offset: 2px;
}

.map4-marker.central-broker {
  box-shadow:
    0 0 0 5px rgba(255,106,0,.18),
    0 0 22px rgba(255,106,0,.78);
}

.map4-marker.age-live {
  opacity: 1;
}

.map4-marker.age-stale {
  opacity: .84;
}

.map4-marker.age-old {
  opacity: .66;
}

.map4-marker.age-zombie {
  opacity: .34;
  filter: saturate(.75);
}

.age-badge {
  display: inline-flex;
  align-items: center;
  margin-left: 6px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .04em;
  vertical-align: middle;
  border: 1px solid rgba(255,255,255,.22);
}

.age-badge.live {
  color: #d8ffe8;
  background: rgba(0,180,90,.24);
  border-color: rgba(0,220,110,.55);
}

.age-badge.stale {
  color: #fff2bd;
  background: rgba(230,170,0,.24);
  border-color: rgba(255,200,40,.55);
}

.age-badge.old {
  color: #ffd8b8;
  background: rgba(255,106,0,.20);
  border-color: rgba(255,106,0,.55);
}

.age-badge.zombie {
  color: #e3dcff;
  background: rgba(120,100,180,.24);
  border-color: rgba(160,140,230,.48);
}

@keyframes map4PulseUpdate {
  0%, 100% {
    transform: scale(1);
  }
  35% {
    transform: scale(1.65);
  }
  65% {
    transform: scale(1.18);
  }
}

.map4-marker.pulse-update {
  animation: map4PulseUpdate .42s ease-in-out 3;
}

/* ============================================================
   MAP4 v0.1.2 — SOS gateway semantic fix
   ============================================================ */

.popup-gateway-badge {
  display: inline-flex;
  margin-top: 8px;
  padding: 4px 8px;
  border-radius: 999px;
  color: #ffd8b8;
  background: rgba(255,106,0,.18);
  border: 1px solid rgba(255,106,0,.55);
  font-size: 11px;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.map4-marker.sos-gateway-node.central-broker {
  box-shadow:
    0 0 0 5px rgba(255,106,0,.22),
    0 0 26px rgba(255,106,0,.92);
}

/* ============================================================
   MAP4 v0.1.3 — Popup node hex id
   ============================================================ */

.popup-title {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.popup-nodehex {
  display: inline-flex;
  align-items: center;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.12);
  color: rgba(255,255,255,.72);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .03em;
}

/* ============================================================
   MAP4 v0.1.4 — Marker hover mini badge
   ============================================================ */

.map4-marker-tooltip {
  padding: 4px 8px;
  border-radius: 999px;
  background: rgba(8, 12, 16, .88);
  border: 1px solid rgba(255,255,255,.18);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .02em;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 22px rgba(0,0,0,.30);
}

.leaflet-tooltip-top.map4-marker-tooltip::before {
  border-top-color: rgba(8, 12, 16, .88);
}

/* ============================================================
   MAP4 v0.1.5 — Sidebar legend alignment
   ============================================================ */

.legend .dot.sos {
  background: var(--sos-orange);
  box-shadow: 0 0 12px rgba(255,106,0,.75);
}

.legend .dot.external {
  background: #00bcd4;
  box-shadow: 0 0 12px rgba(0,188,212,.65);
}

.legend .dot.infra {
  background: var(--sos-orange);
  border: 2px solid #fff2e6;
  outline: 2px solid rgba(255,106,0,.75);
  outline-offset: 1px;
}

.legend .age-badge {
  margin-left: 0;
  margin-right: 8px;
}

.legend-separator {
  height: 1px;
  margin: 12px 0;
  background: rgba(255,255,255,.10);
}

/* ============================================================
   MAP4 v0.1.7 — Hide map controls when mobile sidebar is open
   ============================================================ */

@media (max-width: 720px) {
  body.map4-sidebar-open .map-controls {
    opacity: 0;
    pointer-events: none;
    transform: translateY(12px);
  }

  .map-controls {
    transition: opacity .18s ease, transform .18s ease;
  }
}

/* ============================================================
   MAP4 v0.1.8 — Mobile sidebar viewport-safe height
   ============================================================ */

@media (max-width: 720px) {
  .map4-sidebar {
    top: 86px;
    bottom: max(12px, env(safe-area-inset-bottom));
    max-height: none;
    height: auto;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(18px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 420px) {
  .map4-sidebar {
    top: 82px;
    bottom: max(10px, env(safe-area-inset-bottom));
  }
}


/* ============================================================
   MAP4 v0.2 — Sidebar search results
   ============================================================ */

.search-results {
  margin-top: 10px;
  display: grid;
  gap: 7px;
}

.search-summary,
.search-empty {
  color: var(--sos-muted);
  font-size: 12px;
}

.search-empty {
  padding: 8px 10px;
  border: 1px solid rgba(255,255,255,.10);
  border-radius: 12px;
  background: rgba(255,255,255,.05);
}

.search-result {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 10px;
  border: 1px solid rgba(255,255,255,.11);
  border-radius: 14px;
  background: rgba(255,255,255,.055);
  color: var(--sos-text);
  text-align: left;
  cursor: pointer;
  transition: border-color .16s ease, background .16s ease, transform .16s ease;
}

.search-result:hover {
  transform: translateY(-1px);
  border-color: rgba(255,106,0,.45);
  background: rgba(255,106,0,.10);
}

.search-result span {
  min-width: 0;
  display: grid;
  gap: 2px;
}

.search-result strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.search-result small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--sos-muted);
  font-size: 11px;
}

.search-result .age-badge {
  flex: 0 0 auto;
  margin-left: 0;
}

/* ============================================================
   MAP4 v0.2.1 — Mobile polish / viewport + popup safety
   ============================================================ */

@supports (height: 100dvh) {
  html,
  body,
  .map4-shell,
  #map {
    height: 100dvh;
  }
}

@media (max-width: 720px) {
  .map4-sidebar {
    max-height: none;
    height: auto;
    max-width: calc(100vw - 20px);
  }

  .leaflet-popup {
    max-width: calc(100vw - 28px);
  }

  .leaflet-popup-content-wrapper {
    max-width: calc(100vw - 28px);
    border-radius: 16px;
  }

  .leaflet-popup-content {
    max-width: calc(100vw - 56px);
    margin: 12px 13px;
    line-height: 1.42;
    word-break: break-word;
  }

  .popup-title {
    line-height: 1.2;
  }

  .popup-nodehex {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .search-results {
    padding-bottom: 4px;
  }
}

@media (max-width: 420px) {
  .leaflet-popup-content {
    font-size: 12.5px;
  }

  .popup-title {
    font-size: 15px;
  }
}

/* MAP4 v0.3 — Popup node detail action */
.popup-actions {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid rgba(255,255,255,.12);
}

.map4-popup-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid rgba(255,106,0,.55);
  background: rgba(255,106,0,.14);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  text-decoration: none;
  box-shadow: 0 0 0 rgba(255,106,0,0);
  transition: transform .18s ease, background .18s ease, border-color .18s ease, box-shadow .18s ease;
}

.map4-popup-action:hover {
  transform: translateY(-1px);
  background: rgba(255,106,0,.24);
  border-color: rgba(255,106,0,.9);
  box-shadow: 0 0 18px rgba(255,106,0,.28);
}

/* MAP4 v0.3.1 — Leaflet popup action color fix */
.leaflet-popup-content .map4-popup-action,
.leaflet-popup-content .map4-popup-action:visited {
  color: #fff !important;
}

.leaflet-popup-content .map4-popup-action:hover {
  color: #fff !important;
}

/* MAP4 embed mode — used inside regional mini maps */
body.map4-embed .map4-topbar,
body.map4-embed .map4-sidebar,
body.map4-embed .map-controls {
  display: none !important;
}

body.map4-embed #map {
  inset: 0;
}

body.map4-embed .leaflet-control-container {
  display: none;
}

/* MAP4 embed mode v2 — compact regional mini map */
body.map4-embed .map4-sidebar,
body.map4-embed .map-controls,
body.map4-embed .leaflet-control-container {
  display: none !important;
}

body.map4-embed .map4-topbar {
  top: 12px;
  left: 12px;
  right: auto;
  width: auto;
  max-width: calc(100% - 24px);
  padding: 8px 10px;
  border-radius: 14px;
  background: rgba(25, 31, 36, .82);
}

body.map4-embed .brand span {
  font-size: 14px;
}

body.map4-embed .live-pill {
  display: none;
}

body.map4-embed #map {
  inset: 0;
}

/* =========================================================
   MAP4 context-aware per Web App SOS Italia
   Attivo con: /map4/?context=app
   ========================================================= */

.map4-app-back {
  position: fixed;
  top: 104px;
  left: 22px;
  z-index: 1400;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 8px 13px;
  border-radius: 999px;
  background: rgba(7, 28, 47, 0.94);
  color: #ffffff;
  border: 1px solid rgba(255, 139, 44, 0.72);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.32);
  font-size: 0.88rem;
  font-weight: 800;
  text-decoration: none;
  letter-spacing: 0.01em;
  backdrop-filter: blur(10px);
}

.map4-app-back:hover {
  background: #0b2945;
  color: #ffffff;
  border-color: #ff8b2c;
}

.map4-context-app .map4-shell,
.map4-context-app .map4-layout,
.map4-context-app #map {
  min-height: 100dvh;
}

@media (max-width: 720px) {
  .map4-context-app .map4-app-back {
    top: 112px;
    left: 16px;
    min-height: 34px;
    padding: 7px 11px;
    font-size: 0.8rem;
  }

  .map4-context-app .map4-sidebar {
    max-height: 44dvh;
  }
}



/* =========================================================
   MAP4 context=app — UI lite per Web App / PWA
   ========================================================= */

.map4-context-app .tech-row,
.map4-context-app .map4-panel small,
.map4-context-app .map4-source-note {
  display: none !important;
}

.map4-context-app .map4-sidebar {
  border-radius: 18px;
}

.map4-context-app .map4-panel {
  padding: 12px;
}

.map4-context-app .map4-panel h1,
.map4-context-app .map4-panel h2,
.map4-context-app .map4-panel h3 {
  margin-bottom: 8px;
}

@media (max-width: 720px) {
  .map4-context-app .map4-sidebar {
    max-height: 38dvh;
    overflow-y: auto;
  }

  .map4-context-app .map4-panel {
    font-size: 0.9rem;
  }

  .map4-context-app .map4-input,
  .map4-context-app select,
  .map4-context-app button {
    min-height: 38px;
  }
}
