/**
 * SATUN DISASTER — Map Styles
 * Path: public/assets/css/map.css
 */

#map {
  width: 100%;
  height: 100%;
  background: #0a1628;
  z-index: 1;
}

/* Custom markers */
.satun-marker {
  background: transparent !important;
  border: none !important;
}

.marker-pin {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
  border: 2px solid rgba(255, 255, 255, 0.85);
}

.marker-emoji {
  transform: rotate(45deg);
  font-size: 16px;
  line-height: 1;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.3));
}

/* Marker clusters */
.marker-cluster {
  background-clip: padding-box;
  border-radius: 50%;
}

.marker-cluster div {
  width: 30px;
  height: 30px;
  margin-left: 5px;
  margin-top: 5px;
  text-align: center;
  border-radius: 50%;
  font: 12px/30px Sarabun, sans-serif;
  font-weight: 600;
  color: #fff;
}

.marker-cluster-small {
  background-color: rgba(37, 99, 235, 0.55);
}
.marker-cluster-small div {
  background-color: rgba(37, 99, 235, 0.9);
}

.marker-cluster-medium {
  background-color: rgba(249, 115, 22, 0.55);
}
.marker-cluster-medium div {
  background-color: rgba(249, 115, 22, 0.9);
}

.marker-cluster-large {
  background-color: rgba(239, 68, 68, 0.55);
}
.marker-cluster-large div {
  background-color: rgba(239, 68, 68, 0.95);
}

/* Controls */
.satun-ctrl-btn {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #0f2137;
  color: #e2e8f0;
  border: 1px solid #1e3a5f;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.15s, color 0.15s;
}

.satun-ctrl-btn:hover {
  background: #1e3a5f;
  color: #60a5fa;
}

.satun-toolbar {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-radius: 4px;
}

.satun-coord-display {
  background: rgba(15, 33, 55, 0.9);
  color: #94a3b8;
  padding: 4px 10px;
  font-size: 11px;
  font-family: ui-monospace, monospace;
  border-radius: 4px;
  border: 1px solid #1e3a5f;
  margin: 0 0 8px 8px;
  pointer-events: none;
}

/* Leaflet overrides for dark theme */
.leaflet-container {
  font-family: Sarabun, sans-serif;
  background: #0a1628;
}

.leaflet-control-layers,
.leaflet-bar {
  border: 1px solid #1e3a5f !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4) !important;
}

.leaflet-control-layers {
  background: #0f2137 !important;
  color: #e2e8f0 !important;
  border-radius: 8px !important;
}

.leaflet-control-layers-expanded {
  padding: 10px 12px !important;
  min-width: 180px;
}

.leaflet-control-layers label {
  color: #cbd5e1 !important;
  font-size: 13px;
}

.leaflet-control-layers-separator {
  border-top-color: #1e3a5f !important;
}

.leaflet-bar a {
  background: #0f2137 !important;
  color: #e2e8f0 !important;
  border-bottom-color: #1e3a5f !important;
}

.leaflet-bar a:hover {
  background: #1e3a5f !important;
  color: #60a5fa !important;
}

.leaflet-popup-content-wrapper {
  background: #0f2137;
  color: #e2e8f0;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  border: 1px solid #1e3a5f;
}

.leaflet-popup-tip {
  background: #0f2137;
  border: 1px solid #1e3a5f;
}

.leaflet-popup-close-button {
  color: #94a3b8 !important;
}

.leaflet-popup-content {
  margin: 12px 14px;
  font-size: 13px;
  line-height: 1.45;
  min-width: 180px;
}

.popup-title {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 4px;
  color: #fff;
}

.popup-meta {
  font-size: 11px;
  color: #94a3b8;
  margin-bottom: 4px;
}

.popup-section {
  margin: 8px 0;
  padding: 6px 0;
  border-top: 1px solid #1e3a5f;
  font-size: 12px;
}

.popup-status {
  display: inline-block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  margin: 4px 0;
}

.popup-btn {
  display: inline-block;
  margin-top: 8px;
  padding: 6px 12px;
  background: #2563eb;
  color: #fff !important;
  border-radius: 6px;
  text-decoration: none;
  font-size: 12px;
  font-weight: 500;
}

.popup-btn:hover {
  background: #3b82f6;
}

.badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  margin-left: 4px;
}

.badge-normal, .badge-NORMAL { background: #166534; color: #bbf7d0; }
.badge-watch, .badge-WATCH { background: #854d0e; color: #fef08a; }
.badge-warning, .badge-WARNING { background: #9a3412; color: #fed7aa; }
.badge-critical, .badge-CRITICAL { background: #991b1b; color: #fecaca; }

.status-online { color: #4ade80; }
.status-delayed { color: #facc15; }
.status-offline { color: #f87171; }
.status-unknown { color: #94a3b8; }

/* Custom layer panel (right sidebar) */
.layer-panel {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 260px;
  max-height: calc(100% - 24px);
  background: rgba(15, 33, 55, 0.95);
  backdrop-filter: blur(8px);
  border: 1px solid #1e3a5f;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  z-index: 1000;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.layer-panel-header {
  padding: 10px 14px;
  border-bottom: 1px solid #1e3a5f;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  gap: 8px;
  cursor: default;
}

.layer-panel-header .layer-panel-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.layer-panel-header button {
  background: transparent;
  border: 0;
  color: #94a3b8;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1;
}

.layer-panel-header button:hover {
  color: #fff;
  background: rgba(30, 58, 95, 0.8);
}

.layer-panel-body {
  padding: 10px 14px;
  overflow-y: auto;
  font-size: 13px;
  transition: max-height 0.25s ease, opacity 0.2s ease, padding 0.2s ease;
  max-height: 70vh;
  opacity: 1;
}

/* Desktop collapse: hide body, keep header */
.layer-panel.collapsed .layer-panel-body {
  max-height: 0;
  opacity: 0;
  padding-top: 0;
  padding-bottom: 0;
  overflow: hidden;
  pointer-events: none;
}

.layer-panel.collapsed .layer-panel-header {
  border-bottom-color: transparent;
}

.layer-panel.collapsed #btn-collapse-layers i {
  transform: rotate(-90deg);
}

#btn-collapse-layers i {
  transition: transform 0.2s ease;
  display: inline-block;
}

.layer-group-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #64748b;
  margin: 10px 0 6px;
}

.layer-group-title:first-child {
  margin-top: 0;
}

.layer-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  color: #cbd5e1;
  cursor: pointer;
  user-select: none;
}

.layer-item:hover {
  color: #fff;
}

.layer-item input[type="checkbox"] {
  width: 15px;
  height: 15px;
  accent-color: #2563eb;
  cursor: pointer;
}

/* Legend */
.map-legend {
  position: absolute;
  bottom: 28px;
  left: 12px;
  background: rgba(15, 33, 55, 0.92);
  border: 1px solid #1e3a5f;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 11px;
  color: #94a3b8;
  z-index: 900;
  line-height: 1.6;
}

.map-legend .lg-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.map-legend .lg-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Map container in dashboard */
.map-wrapper {
  position: relative;
  width: 100%;
  height: calc(100vh - 180px);
  min-height: 480px;
  overflow: hidden;
}

.map-wrapper #map {
  width: 100%;
  height: 100%;
  min-height: 480px;
  touch-action: pan-x pan-y;
}

/* Tablet / mobile shared */
@media (max-width: 1023px) {
  .layer-panel {
    display: none;
  }
  .layer-panel.open {
    display: flex;
    width: calc(100% - 24px);
    max-width: 320px;
  }
  .map-legend {
    bottom: calc(72px + env(safe-area-inset-bottom, 0px));
    left: 8px;
    right: auto;
    font-size: 10px;
    padding: 6px 8px;
    max-width: 42vw;
  }
  .map-wrapper {
    height: calc(100dvh - 200px - env(safe-area-inset-bottom, 0px));
    min-height: 360px;
  }
  .map-wrapper #map {
    min-height: 360px;
  }
}

@media (max-width: 768px) {
  .map-wrapper {
    height: calc(100dvh - 210px - env(safe-area-inset-bottom, 0px));
    min-height: 320px;
  }
  .map-wrapper #map {
    min-height: 320px;
  }
  .leaflet-control-layers-expanded {
    max-height: 50vh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    min-width: 160px !important;
    max-width: min(280px, 80vw) !important;
    font-size: 13px !important;
  }
  .leaflet-top .leaflet-control {
    margin-left: 8px !important;
  }
  .leaflet-bottom.leaflet-right {
    margin-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  }
  .leaflet-bottom.leaflet-left {
    margin-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
  }
  .satun-coord-display {
    font-size: 10px;
    padding: 3px 8px;
    margin-bottom: calc(56px + env(safe-area-inset-bottom, 0px));
  }
  .leaflet-popup-content {
    margin: 10px 12px !important;
    font-size: 13px;
    min-width: 180px;
    max-width: min(280px, 78vw);
  }
  .leaflet-popup-content-wrapper {
    border-radius: 12px;
  }
}


/* Leaflet layers control (only layers UI on map) */
.leaflet-control-layers {
  background: rgba(15, 33, 55, 0.96) !important;
  color: #e2e8f0 !important;
  border: 1px solid #1e3a5f !important;
  border-radius: 10px !important;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35) !important;
}

.leaflet-control-layers-toggle {
  background-color: #0f2137 !important;
  border-radius: 8px !important;
  width: 36px !important;
  height: 36px !important;
}

.leaflet-control-layers-expanded {
  padding: 10px 12px !important;
  min-width: 180px;
}

.leaflet-control-layers label {
  color: #cbd5e1 !important;
  font-size: 13px;
  margin: 2px 0;
}

.leaflet-control-layers-separator {
  border-top-color: #1e3a5f !important;
}




/* =========================================================
   Layer search — matches Leaflet control stack (top-left)
   ========================================================= */
.leaflet-top.leaflet-left {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
}

/* Search sits in Leaflet control flow under zoom/locate */
.satun-search-control.leaflet-control {
  margin: 10px 0 0 10px !important;
  float: none !important;
  clear: both !important;
  position: relative;
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
}

/* Icon matches .leaflet-bar / .satun-ctrl-btn */
.satun-search-toggle {
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  border: 2px solid rgba(0, 0, 0, 0.2);
  border-radius: 4px;
  color: #333;
  cursor: pointer;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
  font-size: 14px;
  padding: 0;
  line-height: 1;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease, transform 0.12s ease;
}
.satun-search-toggle:hover {
  background: #f4f4f4;
}
.satun-search-toggle:active {
  transform: scale(0.96);
}
.satun-search-toggle.is-open {
  background: #2563eb;
  color: #fff;
  border-color: #1d4ed8;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.45);
}

/* Panel opens to the right of the icon, aligned top */
.satun-search-panel {
  position: absolute;
  left: calc(100% + 8px);
  top: 0;
  z-index: 1200;
  animation: satun-search-in 0.15s ease-out;
}
.satun-search-panel.hidden {
  display: none !important;
}
@keyframes satun-search-in {
  from { opacity: 0; transform: translateX(-6px); }
  to   { opacity: 1; transform: translateX(0); }
}

.satun-search-box {
  display: flex;
  align-items: center;
  gap: 6px;
  background: #0f2137;
  border: 1px solid #1e3a5f;
  border-radius: 10px;
  padding: 7px 10px;
  width: min(280px, calc(100vw - 90px));
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.satun-search-icon {
  color: #94a3b8;
  font-size: 12px;
  flex-shrink: 0;
}
.satun-search-box input {
  flex: 1;
  background: transparent;
  border: 0;
  outline: none;
  color: #e2e8f0;
  font-size: 13px;
  font-family: Sarabun, sans-serif;
  min-width: 0;
}
.satun-search-box input::placeholder {
  color: #64748b;
}
#satun-search-clear,
#satun-search-close {
  background: transparent;
  border: 0;
  color: #94a3b8;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  padding: 3px 5px;
  flex-shrink: 0;
  border-radius: 6px;
}
#satun-search-clear:hover,
#satun-search-close:hover {
  color: #e2e8f0;
  background: #1e3a5f;
}

.satun-search-results {
  margin-top: 6px;
  background: #0f2137;
  border: 1px solid #1e3a5f;
  border-radius: 10px;
  max-height: min(280px, 40vh);
  overflow-y: auto;
  width: min(280px, calc(100vw - 90px));
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
}
.satun-search-results.hidden {
  display: none;
}
.satun-search-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  border-bottom: 1px solid #1e3a5f;
  padding: 8px 10px;
  cursor: pointer;
  color: #e2e8f0;
  font-family: Sarabun, sans-serif;
}
.satun-search-item:last-child {
  border-bottom: 0;
}
.satun-search-item:hover {
  background: #153047;
}
.satun-search-item-title {
  display: block;
  font-size: 13px;
  font-weight: 600;
}
.satun-search-item-meta {
  display: block;
  font-size: 11px;
  color: #94a3b8;
  margin-top: 2px;
}
.satun-search-empty {
  padding: 10px;
  font-size: 12px;
  color: #94a3b8;
}

/* Mobile search */
@media (max-width: 640px) {
  .satun-search-control.leaflet-control {
    margin: 8px 0 0 8px !important;
  }
  .satun-search-toggle {
    width: 36px;
    height: 36px;
    font-size: 15px;
  }
  .satun-search-panel {
    left: 0;
    top: calc(100% + 8px);
    right: auto;
  }
  .satun-search-box,
  .satun-search-results {
    width: min(300px, calc(100vw - 20px));
  }
  .satun-search-box {
    padding: 8px 10px;
  }
  .satun-search-box input {
    font-size: 16px; /* prevents iOS zoom on focus */
  }
  .satun-search-results {
    max-height: min(240px, 35vh);
  }
  .satun-search-item {
    padding: 10px 12px;
  }
}

/* Light theme */
html[data-theme="light"] .satun-search-box,
html[data-theme="light"] .satun-search-results {
  background: #fff;
  border-color: #cbd5e1;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.12);
}
html[data-theme="light"] .satun-search-box input {
  color: #0f172a;
}
html[data-theme="light"] .satun-search-item {
  color: #0f172a;
  border-bottom-color: #e2e8f0;
}
html[data-theme="light"] .satun-search-item:hover {
  background: #f1f5f9;
}
html[data-theme="light"] .satun-search-item-meta {
  color: #64748b;
}
html[data-theme="light"] #satun-search-clear:hover,
html[data-theme="light"] #satun-search-close:hover {
  color: #0f172a;
  background: #e2e8f0;
}


/* =========================================================
   Mobile app chrome (bottom nav + safe areas)
   ========================================================= */
.satun-mobile-nav {
  padding-bottom: calc(0.625rem + env(safe-area-inset-bottom, 0px)) !important;
  padding-left: max(0.5rem, env(safe-area-inset-left, 0px));
  padding-right: max(0.5rem, env(safe-area-inset-right, 0px));
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.satun-mobile-nav a {
  min-width: 56px;
  padding: 4px 2px;
  border-radius: 10px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.satun-mobile-nav a:active {
  background: rgba(255, 255, 255, 0.06);
}

/* Stats / toolbar row on small screens */
@media (max-width: 768px) {
  .map-stats-bar,
  section.bg-command-800 .flex.flex-wrap {
    row-gap: 6px;
  }
  /* Larger touch targets for map tools */
  .leaflet-bar a,
  .satun-ctrl-btn {
    width: 36px !important;
    height: 36px !important;
    line-height: 36px !important;
    font-size: 15px !important;
  }
  .leaflet-control-zoom-in,
  .leaflet-control-zoom-out {
    font-size: 18px !important;
  }
}

/* Very small phones */
@media (max-width: 380px) {
  .map-legend {
    display: none; /* avoid clutter; risk colors still on markers */
  }
  .satun-search-box,
  .satun-search-results {
    width: calc(100vw - 16px);
  }
}

/* Landscape phone: give map more height, shrink bottom nav padding */
@media (max-width: 900px) and (orientation: landscape) {
  .map-wrapper {
    height: calc(100dvh - 120px - env(safe-area-inset-bottom, 0px));
    min-height: 240px;
  }
  .map-legend {
    bottom: calc(52px + env(safe-area-inset-bottom, 0px));
  }
  .satun-mobile-nav {
    padding-top: 0.25rem;
    padding-bottom: calc(0.25rem + env(safe-area-inset-bottom, 0px)) !important;
  }
  .satun-mobile-nav a {
    font-size: 10px;
  }
  .satun-mobile-nav a i {
    font-size: 14px !important;
  }
}

/* Prevent double-tap zoom on controls */
.leaflet-control,
.satun-search-control,
.satun-mobile-nav {
  touch-action: manipulation;
}

/* iOS rubber-band / overscroll inside map */
.map-wrapper,
#map {
  overscroll-behavior: none;
}


/* =========================================================
   Map controls theme: light / dark
   ========================================================= */

/* ---- Dark (default) ---- */
html[data-theme="dark"] .leaflet-container,
html:not([data-theme]) .leaflet-container {
  background: #0a1628;
}

html[data-theme="dark"] .leaflet-bar,
html:not([data-theme]) .leaflet-bar,
html[data-theme="dark"] .leaflet-control-layers,
html:not([data-theme]) .leaflet-control-layers {
  border-color: #1e3a5f !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45) !important;
}

html[data-theme="dark"] .leaflet-bar a,
html:not([data-theme]) .leaflet-bar a {
  background: #0f2137 !important;
  color: #e2e8f0 !important;
  border-bottom-color: #1e3a5f !important;
}
html[data-theme="dark"] .leaflet-bar a:hover,
html:not([data-theme]) .leaflet-bar a:hover {
  background: #1e3a5f !important;
  color: #60a5fa !important;
}

html[data-theme="dark"] .leaflet-control-layers,
html:not([data-theme]) .leaflet-control-layers {
  background: #0f2137 !important;
  color: #e2e8f0 !important;
}
html[data-theme="dark"] .leaflet-control-layers label,
html:not([data-theme]) .leaflet-control-layers label {
  color: #cbd5e1 !important;
}
html[data-theme="dark"] .leaflet-control-layers-separator,
html:not([data-theme]) .leaflet-control-layers-separator {
  border-top-color: #1e3a5f !important;
}

html[data-theme="dark"] .satun-ctrl-btn,
html:not([data-theme]) .satun-ctrl-btn {
  background: #0f2137;
  color: #e2e8f0;
  border-color: #1e3a5f;
}
html[data-theme="dark"] .satun-ctrl-btn:hover,
html:not([data-theme]) .satun-ctrl-btn:hover {
  background: #1e3a5f;
  color: #60a5fa;
}

html[data-theme="dark"] .satun-search-toggle,
html:not([data-theme]) .satun-search-toggle {
  background: #0f2137;
  color: #e2e8f0;
  border: 1px solid #1e3a5f;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
}
html[data-theme="dark"] .satun-search-toggle:hover,
html:not([data-theme]) .satun-search-toggle:hover {
  background: #1e3a5f;
  color: #60a5fa;
}
html[data-theme="dark"] .satun-search-toggle.is-open,
html:not([data-theme]) .satun-search-toggle.is-open {
  background: #2563eb;
  color: #fff;
  border-color: #1d4ed8;
}

html[data-theme="dark"] .satun-coord-display,
html:not([data-theme]) .satun-coord-display {
  background: rgba(15, 33, 55, 0.92);
  color: #94a3b8;
  border-color: #1e3a5f;
}

html[data-theme="dark"] .map-legend,
html:not([data-theme]) .map-legend {
  background: rgba(15, 33, 55, 0.92);
  color: #cbd5e1;
  border: 1px solid #1e3a5f;
}

html[data-theme="dark"] .leaflet-popup-content-wrapper,
html:not([data-theme]) .leaflet-popup-content-wrapper,
html[data-theme="dark"] .leaflet-popup-tip,
html:not([data-theme]) .leaflet-popup-tip {
  background: #0f2137;
  color: #e2e8f0;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

html[data-theme="dark"] .leaflet-control-attribution,
html:not([data-theme]) .leaflet-control-attribution {
  background: rgba(10, 22, 40, 0.75);
  color: #64748b;
}

/* ---- Light ---- */
html[data-theme="light"] .leaflet-container {
  background: #e8eef5;
}

html[data-theme="light"] .leaflet-bar,
html[data-theme="light"] .leaflet-control-layers {
  border: 1px solid #cbd5e1 !important;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.12) !important;
}

html[data-theme="light"] .leaflet-bar a {
  background: #ffffff !important;
  color: #1e293b !important;
  border-bottom-color: #e2e8f0 !important;
}
html[data-theme="light"] .leaflet-bar a:hover {
  background: #f1f5f9 !important;
  color: #2563eb !important;
}
html[data-theme="light"] .leaflet-bar a.leaflet-disabled {
  color: #94a3b8 !important;
  background: #f8fafc !important;
}

html[data-theme="light"] .leaflet-control-layers {
  background: #ffffff !important;
  color: #0f172a !important;
}
html[data-theme="light"] .leaflet-control-layers label {
  color: #334155 !important;
}
html[data-theme="light"] .leaflet-control-layers-separator {
  border-top-color: #e2e8f0 !important;
}
html[data-theme="light"] .leaflet-control-layers-toggle {
  background-color: #fff !important;
  /* keep default layers icon readable on light */
  filter: none;
}

html[data-theme="light"] .satun-ctrl-btn {
  background: #ffffff;
  color: #1e293b;
  border: 1px solid #cbd5e1;
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.1);
}
html[data-theme="light"] .satun-ctrl-btn:hover {
  background: #f1f5f9;
  color: #2563eb;
}

html[data-theme="light"] .satun-search-toggle {
  background: #ffffff;
  color: #1e293b;
  border: 1px solid #cbd5e1;
  box-shadow: 0 1px 5px rgba(15, 23, 42, 0.12);
}
html[data-theme="light"] .satun-search-toggle:hover {
  background: #f1f5f9;
  color: #2563eb;
}
html[data-theme="light"] .satun-search-toggle.is-open {
  background: #2563eb;
  color: #ffffff;
  border-color: #1d4ed8;
  box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
}

html[data-theme="light"] .satun-coord-display {
  background: rgba(255, 255, 255, 0.92);
  color: #475569;
  border: 1px solid #cbd5e1;
}

html[data-theme="light"] .map-legend {
  background: rgba(255, 255, 255, 0.94);
  color: #334155;
  border: 1px solid #cbd5e1;
  box-shadow: 0 2px 10px rgba(15, 23, 42, 0.08);
}
html[data-theme="light"] .map-legend .font-semibold,
html[data-theme="light"] .map-legend .text-gray-300 {
  color: #334155 !important;
}

html[data-theme="light"] .leaflet-popup-content-wrapper,
html[data-theme="light"] .leaflet-popup-tip {
  background: #ffffff;
  color: #0f172a;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.12);
}
html[data-theme="light"] .leaflet-popup-content-wrapper .popup-title {
  color: #0f172a;
}
html[data-theme="light"] .leaflet-popup-content-wrapper .popup-meta {
  color: #64748b;
}
html[data-theme="light"] .leaflet-popup-content-wrapper .popup-btn {
  background: #2563eb;
  color: #fff;
}

html[data-theme="light"] .leaflet-control-attribution {
  background: rgba(255, 255, 255, 0.8);
  color: #64748b;
}

/* Zoom +/- icons already text; ensure SVG/path icons if any */
html[data-theme="light"] .leaflet-control-zoom-in,
html[data-theme="light"] .leaflet-control-zoom-out {
  color: #1e293b !important;
  font-weight: 600;
}




/* =========================================================
   Control icon spacing — 8px between each left control
   ซูม · ตำแหน่ง · ค้นหา · เครื่องมือ · รีเฟรช · โฮม
   ========================================================= */
.leaflet-top.leaflet-left {
  display: flex !important;
  flex-direction: column !important;
  align-items: flex-start !important;
  gap: 8px !important;
  padding-top: 10px;
  padding-left: 0;
}

.leaflet-top.leaflet-left > .leaflet-control {
  margin: 0 0 0 10px !important;
  float: none !important;
  clear: none !important;
}

/* Zoom group: internal + / - stay stacked, outer gap via parent */
.leaflet-top.leaflet-left .leaflet-control-zoom.leaflet-bar {
  margin: 0 0 0 10px !important;
}

.leaflet-top.leaflet-left .leaflet-control-zoom a {
  width: 34px !important;
  height: 34px !important;
  line-height: 34px !important;
}

/* Single icon buttons (locate / fullscreen / refresh / home) */
.satun-icon-btn.satun-ctrl-btn,
button.satun-icon-btn {
  width: 34px !important;
  height: 34px !important;
  display: flex !important;
  align-items: center;
  justify-content: center;
  padding: 0 !important;
  border-radius: 4px !important;
  cursor: pointer;
  line-height: 1 !important;
}

/* Search toggle same footprint */
.satun-search-control.leaflet-control {
  margin: 0 0 0 10px !important;
}
.satun-search-toggle {
  width: 34px !important;
  height: 34px !important;
  border-radius: 4px !important;
}

/* Right controls */
.leaflet-top.leaflet-right > .leaflet-control {
  margin: 10px 10px 0 0 !important;
}
.leaflet-bottom.leaflet-right > .leaflet-control {
  margin: 0 10px 10px 0 !important;
}
.leaflet-bottom.leaflet-left > .leaflet-control {
  margin: 0 0 10px 10px !important;
}

@media (max-width: 768px) {
  .leaflet-top.leaflet-left {
    gap: 8px !important;
    padding-top: 8px;
  }
  .leaflet-top.leaflet-left > .leaflet-control {
    margin-left: 8px !important;
  }
  .leaflet-top.leaflet-left .leaflet-control-zoom a,
  .satun-icon-btn.satun-ctrl-btn,
  .satun-search-toggle {
    width: 36px !important;
    height: 36px !important;
  }
  .leaflet-top.leaflet-left .leaflet-control-zoom a {
    line-height: 36px !important;
  }
}


/* Risk legend only when floods layer is allowed */
.map-legend[data-layer-floods='0'] {
  display: none !important;
}


/* Navigation button in popups */
.popup-nav {
  margin-top: 8px;
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}
.popup-btn-nav {
  display: inline-flex !important;
  align-items: center;
  gap: 6px;
  background: #059669 !important;
  color: #fff !important;
  border: none !important;
  border-radius: 8px !important;
  padding: 8px 12px !important;
  font-size: 13px !important;
  font-weight: 600 !important;
  text-decoration: none !important;
  cursor: pointer;
}
.popup-btn-nav:hover {
  background: #047857 !important;
  color: #fff !important;
}
html[data-theme="light"] .popup-btn-nav {
  background: #059669 !important;
  color: #fff !important;
}


/* Facebook share button in popups */
.popup-btn-fb {
  display: inline-flex !important;
  align-items: center;
  gap: 6px;
  background: #1877f2 !important;
  color: #fff !important;
  border: none !important;
  border-radius: 8px !important;
  padding: 8px 12px !important;
  font-size: 13px !important;
  font-weight: 600 !important;
  text-decoration: none !important;
  cursor: pointer;
}
.popup-btn-fb:hover {
  background: #166fe5 !important;
  color: #fff !important;
}
html[data-theme="light"] .popup-btn-fb {
  background: #1877f2 !important;
  color: #fff !important;
}


/* =========================================================
   Officer mode FAB — bottom-right, clear of layers/legend
   ========================================================= */


/* ===== Officer FAB — bottom-right on map ===== */
.map-wrapper {
  position: relative;
}

.officer-map-fab-wrap {
  position: absolute;
  z-index: 1100;
  right: 14px;
  bottom: 44px; /* above Leaflet attribution */
  left: auto;
  top: auto;
  display: flex;
  flex-direction: column-reverse; /* FAB bottom, panel opens upward */
  align-items: flex-end;
  gap: 10px;
  max-width: min(320px, calc(100% - 28px));
  pointer-events: none;
}
.officer-map-fab-wrap > * {
  pointer-events: auto;
}

.officer-map-fab {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 2px solid rgba(56, 189, 248, 0.6);
  background: #0f2137;
  color: #38bdf8;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  cursor: pointer;
  position: relative;
  flex-shrink: 0;
  transition: transform 0.12s ease, background 0.15s ease, color 0.15s ease;
}
.officer-map-fab:hover {
  background: #1e3a5f;
  color: #fff;
  transform: scale(1.06);
}
.officer-map-fab[aria-expanded="true"] {
  background: #0284c7;
  color: #fff;
  border-color: #7dd3fc;
}

.officer-map-fab-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: #f97316;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}
.officer-map-fab-badge.hidden {
  display: none !important;
}

.officer-field-panel.officer-field-dropdown {
  position: relative;
  width: min(300px, calc(100vw - 28px));
  max-height: min(45vh, 360px);
  display: none;
  flex-direction: column;
  background: rgba(15, 33, 55, 0.98);
  border: 1px solid #1e3a5f;
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}
.officer-field-panel.officer-field-dropdown[data-open="1"] {
  display: flex !important;
}
.officer-field-panel.officer-field-dropdown[data-open="0"],
.officer-field-panel.officer-field-dropdown.hidden {
  display: none !important;
}
.officer-nearby-list {
  max-height: min(32vh, 240px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.officer-sos-card:last-child {
  border-bottom: 0 !important;
}

html[data-theme="light"] .officer-map-fab {
  background: #fff;
  color: #0284c7;
  border-color: #7dd3fc;
  box-shadow: 0 4px 14px rgba(15, 23, 42, 0.15);
}
html[data-theme="light"] .officer-map-fab[aria-expanded="true"] {
  background: #0284c7;
  color: #fff;
}
html[data-theme="light"] .officer-field-panel.officer-field-dropdown {
  background: rgba(255, 255, 255, 0.98);
  border-color: #cbd5e1;
  box-shadow: 0 12px 32px rgba(15, 23, 42, 0.15);
}
html[data-theme="light"] .officer-field-panel .text-white {
  color: #0f172a !important;
}
html[data-theme="light"] .officer-sos-card {
  border-color: #e2e8f0 !important;
}

@media (max-width: 768px) {
  .officer-map-fab-wrap {
    right: 10px;
    bottom: 16px;
  }
  .officer-map-fab {
    width: 48px;
    height: 48px;
    font-size: 18px;
  }
  .officer-field-panel.officer-field-dropdown {
    width: min(280px, calc(100vw - 20px));
    max-height: min(38vh, 300px);
  }
}

/* ========== โหมดขอความช่วยเหลือ (สมาชิก) ========== */
.help-seeker-fab-wrap {
  position: absolute;
  right: 16px;
  bottom: 96px;
  z-index: 1050;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0;
  pointer-events: none;
}
.officer-map-fab-wrap {
  /* เจ้าหน้าที่อยู่ล่างกว่า / ขวา */
  bottom: 24px;
}
.help-seeker-fab-wrap > * {
  pointer-events: auto;
}
.help-seeker-fab {
  width: 48px;
  height: 48px;
  border-radius: 9999px;
  border: 2px solid rgba(249, 115, 22, 0.7);
  background: linear-gradient(145deg, #ea580c, #c2410c);
  color: #fff;
  box-shadow: 0 4px 14px rgba(234, 88, 12, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  cursor: pointer;
  position: relative;
}
.help-seeker-fab:hover {
  filter: brightness(1.08);
}
.help-seeker-fab[aria-expanded="true"] {
  box-shadow: 0 0 0 3px rgba(249, 115, 22, 0.35);
}
.help-seeker-fab-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 9999px;
  background: #ef4444;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}
.help-seeker-fab-badge.hidden {
  display: none !important;
}
.help-seeker-panel.help-seeker-dropdown {
  position: absolute;
  right: 0;
  bottom: calc(100% + 10px);
  width: min(300px, 88vw);
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}
.help-seeker-panel.help-seeker-dropdown[data-open="0"],
.help-seeker-panel.help-seeker-dropdown.hidden {
  display: none !important;
}
.hs-officer-pin {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #0284c7;
  border: 3px solid #fff;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  font-size: 14px;
}
html[data-theme="light"] .help-seeker-fab {
  background: linear-gradient(145deg, #fb923c, #ea580c);
}
html[data-theme="light"] .help-seeker-panel.help-seeker-dropdown {
  background: #fff;
  border-color: #e2e8f0;
}
html[data-theme="light"] .help-seeker-panel .text-white {
  color: #0f172a !important;
}
@media (max-width: 640px) {
  .help-seeker-fab-wrap {
    right: max(12px, env(safe-area-inset-right));
    bottom: 110px;
  }
}


/* เส้นนำทาง ขอช่วย ↔ เจ้าหน้าที่ */
.help-seeker-route-label-wrap {
  background: transparent !important;
  border: none !important;
}
.hs-route-label {
  background: rgba(15, 23, 42, 0.88);
  color: #86efac;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 9999px;
  border: 1px solid rgba(34, 197, 94, 0.55);
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
  text-align: center;
}
html[data-theme="light"] .hs-route-label {
  background: rgba(255, 255, 255, 0.95);
  color: #15803d;
  border-color: rgba(234, 88, 12, 0.4);
}


.help-seeker-route-road {
  filter: drop-shadow(0 1px 2px rgba(22, 163, 74, 0.55));
}
.hs-nav-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  padding: 3px 8px;
  border-radius: 6px;
  background: #0369a1;
  color: #e0f2fe !important;
  text-decoration: none !important;
}
.hs-nav-btn:hover {
  background: #0284c7;
}


/* อยู่เหนือ legend "ระดับความเสี่ยง" ซ้ายล่าง — ไม่ทับกัน */
.offline-map-btn {
  position: absolute;
  left: 12px;
  bottom: 148px; /* เหนือ .map-legend (bottom: 28px + ความสูง legend) */
  z-index: 950;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 9999px;
  border: 1px solid rgba(34, 197, 94, 0.45);
  background: rgba(15, 23, 42, 0.92);
  color: #86efac;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0,0,0,0.35);
  max-width: calc(100% - 24px);
}
.offline-map-btn:hover { filter: brightness(1.1); }
.offline-map-btn.is-loading { opacity: 0.7; pointer-events: none; }
html[data-theme="light"] .offline-map-btn {
  background: rgba(255,255,255,0.95);
  color: #15803d;
  border-color: rgba(22, 163, 74, 0.4);
}
@media (max-width: 1023px) {
  .offline-map-btn {
    left: 8px;
    /* เหนือ legend มือถือ (legend bottom ~72px + safe) */
    bottom: calc(72px + 120px + env(safe-area-inset-bottom, 0px));
  }
}
@media (max-width: 640px) {
  .offline-map-btn-label { display: none; }
  .offline-map-btn {
    left: max(10px, env(safe-area-inset-left));
    bottom: calc(72px + 100px + env(safe-area-inset-bottom, 0px));
    padding: 10px;
  }
}
/* เมื่อ legend ถูกซ่อน — ลดปุ่มลงใกล้ขอบล่าง */
.map-wrapper:has(.map-legend[data-layer-floods="0"]) .offline-map-btn,
.map-wrapper:has(.map-legend[style*="display: none"]) .offline-map-btn {
  bottom: 28px;
}
@media (max-width: 1023px) {
  .map-wrapper:has(.map-legend[data-layer-floods="0"]) .offline-map-btn,
  .map-wrapper:has(.map-legend[style*="display: none"]) .offline-map-btn {
    bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  }
}

/* =========================================================
   RESPONSIVE SYSTEM — all devices (overrides)
   Breakpoints: xs≤380 · sm≤640 · md≤768 · lg≤1023 · xl≥1024
   ========================================================= */
:root {
  --satun-safe-top: env(safe-area-inset-top, 0px);
  --satun-safe-right: env(safe-area-inset-right, 0px);
  --satun-safe-bottom: env(safe-area-inset-bottom, 0px);
  --satun-safe-left: env(safe-area-inset-left, 0px);
  --satun-header-h: 56px;
  --satun-mobile-nav-h: 56px;
  --satun-map-chrome: 180px;
  --satun-fab-size: 52px;
  --satun-control-gap: 8px;
  --satun-touch-min: 44px;
}

/* —— Desktop / large —— */
.map-wrapper {
  position: relative;
  width: 100%;
  height: calc(100vh - var(--satun-map-chrome));
  height: calc(100dvh - var(--satun-map-chrome));
  min-height: 420px;
  overflow: hidden;
}
.map-wrapper #map {
  width: 100%;
  height: 100%;
  min-height: 420px;
  touch-action: pan-x pan-y;
}

/* Leaflet controls touch-friendly */
.leaflet-control-zoom a {
  width: var(--satun-touch-min) !important;
  height: var(--satun-touch-min) !important;
  line-height: var(--satun-touch-min) !important;
  font-size: 18px !important;
}
.leaflet-top.leaflet-left {
  top: max(10px, var(--satun-safe-top));
  left: max(10px, var(--satun-safe-left));
}
.leaflet-top.leaflet-right {
  top: max(10px, var(--satun-safe-top));
  right: max(10px, var(--satun-safe-right));
}
.leaflet-bottom.leaflet-right {
  bottom: max(8px, var(--satun-safe-bottom));
  right: max(8px, var(--satun-safe-right));
}

/* Legend — left bottom */
.map-legend {
  left: max(12px, var(--satun-safe-left));
  bottom: max(28px, calc(12px + var(--satun-safe-bottom)));
  max-width: min(200px, 46vw);
  z-index: 900;
}

/* Offline map — above legend, never over FABs */
.offline-map-btn {
  left: max(12px, var(--satun-safe-left));
  right: auto;
  bottom: max(148px, calc(130px + var(--satun-safe-bottom)));
  z-index: 950;
  min-height: var(--satun-touch-min);
}

/* Officer FAB stack — right bottom */
.officer-map-fab-wrap {
  right: max(14px, var(--satun-safe-right));
  bottom: max(44px, calc(28px + var(--satun-safe-bottom)));
  max-width: min(320px, calc(100vw - 28px - var(--satun-safe-left) - var(--satun-safe-right)));
}
.officer-map-fab {
  width: var(--satun-fab-size);
  height: var(--satun-fab-size);
  min-width: var(--satun-touch-min);
  min-height: var(--satun-touch-min);
}

/* Help-seeker FAB — above officer FAB */
.help-seeker-fab-wrap {
  right: max(14px, var(--satun-safe-right));
  bottom: max(108px, calc(90px + var(--satun-safe-bottom)));
  max-width: min(300px, calc(100vw - 28px));
}
.help-seeker-fab {
  width: 48px;
  height: 48px;
  min-width: var(--satun-touch-min);
  min-height: var(--satun-touch-min);
}

.officer-field-panel.officer-field-dropdown,
.help-seeker-panel.help-seeker-dropdown {
  width: min(300px, calc(100vw - 28px - var(--satun-safe-left) - var(--satun-safe-right)));
  max-height: min(50vh, 380px);
}

/* —— Tablet ≤1023 —— */
@media (max-width: 1023px) {
  :root {
    --satun-map-chrome: 200px;
    --satun-fab-size: 48px;
  }
  .map-wrapper {
    min-height: 360px;
  }
  .map-legend {
    bottom: calc(var(--satun-mobile-nav-h) + 16px + var(--satun-safe-bottom));
    font-size: 10px;
    padding: 6px 8px;
    max-width: min(160px, 40vw);
  }
  .offline-map-btn {
    bottom: calc(var(--satun-mobile-nav-h) + 110px + var(--satun-safe-bottom));
  }
  .officer-map-fab-wrap {
    bottom: calc(var(--satun-mobile-nav-h) + 12px + var(--satun-safe-bottom));
  }
  .help-seeker-fab-wrap {
    bottom: calc(var(--satun-mobile-nav-h) + 72px + var(--satun-safe-bottom));
  }
  .map-wrapper:has(.map-legend[data-layer-floods="0"]) .offline-map-btn,
  .map-wrapper:has(.map-legend[style*="display: none"]) .offline-map-btn {
    bottom: calc(var(--satun-mobile-nav-h) + 16px + var(--satun-safe-bottom));
  }
}

/* —— Phone ≤768 —— */
@media (max-width: 768px) {
  :root {
    --satun-map-chrome: 210px;
    --satun-control-gap: 6px;
  }
  .map-wrapper,
  .map-wrapper #map {
    min-height: 320px;
  }
  .leaflet-control-layers-expanded {
    max-width: min(260px, calc(100vw - 24px));
    max-height: 50vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .satun-search-control.leaflet-control {
    max-width: calc(100vw - 70px);
  }
  .officer-field-panel.officer-field-dropdown,
  .help-seeker-panel.help-seeker-dropdown {
    max-height: min(42vh, 320px);
    font-size: 13px;
  }
  .leaflet-bottom.leaflet-left .leaflet-control-attribution {
    max-width: 55vw;
    font-size: 9px;
    margin: 0 0 2px 2px !important;
  }
}

/* —— Small phone ≤640 —— */
@media (max-width: 640px) {
  .offline-map-btn-label {
    display: none;
  }
  .offline-map-btn {
    padding: 10px;
    border-radius: 50%;
    width: var(--satun-touch-min);
    height: var(--satun-touch-min);
    justify-content: center;
  }
  .help-seeker-fab-wrap {
    right: max(12px, var(--satun-safe-right));
  }
  .officer-map-fab-wrap {
    right: max(12px, var(--satun-safe-right));
  }
  .map-legend {
    max-width: min(140px, 38vw);
    line-height: 1.45;
  }
  .ui-illust {
    width: 88px;
  }
}

/* —— Tiny ≤380 —— */
@media (max-width: 380px) {
  :root {
    --satun-fab-size: 44px;
  }
  .map-legend {
    font-size: 9px;
    padding: 4px 6px;
    max-width: 34vw;
  }
  .map-legend .lg-dot {
    width: 8px;
    height: 8px;
  }
  .offline-map-btn {
    bottom: calc(var(--satun-mobile-nav-h) + 96px + var(--satun-safe-bottom));
  }
}

/* —— Landscape phone/tablet —— */
@media (max-width: 900px) and (orientation: landscape) {
  :root {
    --satun-map-chrome: 110px;
    --satun-mobile-nav-h: 48px;
  }
  .map-wrapper {
    min-height: 240px;
  }
  .map-legend {
    bottom: calc(8px + var(--satun-safe-bottom));
    max-width: 28vw;
  }
  .offline-map-btn {
    bottom: calc(100px + var(--satun-safe-bottom));
  }
  .officer-map-fab-wrap {
    bottom: calc(8px + var(--satun-safe-bottom));
  }
  .help-seeker-fab-wrap {
    bottom: calc(64px + var(--satun-safe-bottom));
  }
  .satun-mobile-nav a {
    font-size: 10px;
    padding-top: 4px;
    padding-bottom: 4px;
  }
  .satun-mobile-nav a i {
    font-size: 1rem;
  }
}

/* —— Large desktop ≥1280 —— */
@media (min-width: 1280px) {
  .map-wrapper {
    min-height: 520px;
  }
  .map-legend {
    font-size: 12px;
    padding: 10px 14px;
  }
}

/* —— Prefers reduced motion —— */
@media (prefers-reduced-motion: reduce) {
  .officer-map-fab,
  .help-seeker-fab,
  .offline-map-btn {
    transition: none !important;
  }
}

/* —— Print —— */
@media print {
  .officer-map-fab-wrap,
  .help-seeker-fab-wrap,
  .offline-map-btn,
  .satun-mobile-nav,
  .leaflet-control-zoom {
    display: none !important;
  }
  .map-wrapper {
    height: 70vh !important;
  }
}


/* ===== Pin form (SweetAlert) — light/dark ===== */
/* ===== Pin form (SweetAlert) — mobile + dark/light ===== */
.pin-form-swal-container.swal2-container {
  padding: env(safe-area-inset-top, 8px) 8px env(safe-area-inset-bottom, 8px) !important;
  box-sizing: border-box !important;
  overflow-x: hidden !important;
}
.pin-form-swal.swal2-popup,
.pin-form-swal {
  border-radius: 16px !important;
  box-sizing: border-box !important;
  max-width: calc(100vw - 16px) !important;
  width: min(480px, calc(100vw - 16px)) !important;
  margin: 0 auto !important;
  padding: 0 !important;
  overflow-x: hidden !important;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45) !important;
}
.pin-form-swal--dark {
  background: linear-gradient(165deg, #12233a 0%, #0b1628 55%, #0a1422 100%) !important;
  color: #e2e8f0 !important;
  border: 1px solid rgba(56, 189, 248, 0.18) !important;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55), 0 0 0 1px rgba(15, 23, 42, 0.4) !important;
}
.pin-form-swal--light {
  background: linear-gradient(165deg, #ffffff 0%, #f1f5f9 60%, #e8eef5 100%) !important;
  color: #0f172a !important;
  border: 1px solid #cbd5e1 !important;
  box-shadow: 0 16px 48px rgba(15, 23, 42, 0.14), 0 0 0 1px rgba(255,255,255,0.6) inset !important;
}
.pin-form-swal .swal2-title {
  font-size: 1.1rem !important;
  padding: 1rem 0.85rem 0.35rem !important;
  line-height: 1.35 !important;
  color: inherit !important;
}
.pin-form-swal-html,
.pin-form-swal .swal2-html-container {
  margin: 0 !important;
  padding: 0.35rem 0.85rem 0.25rem !important;
  max-height: min(60vh, 480px) !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  box-sizing: border-box !important;
  width: 100% !important;
  color: inherit !important;
  -webkit-overflow-scrolling: touch;
}
.pin-form-swal-actions,
.pin-form-swal .swal2-actions {
  flex-wrap: wrap !important;
  gap: 8px !important;
  padding: 0.55rem 0.85rem calc(0.85rem + env(safe-area-inset-bottom, 0px)) !important;
  margin: 0 !important;
  width: 100% !important;
  box-sizing: border-box !important;
}
.pin-form-swal .swal2-actions .swal2-styled {
  margin: 0 !important;
  flex: 1 1 auto !important;
  min-width: 120px !important;
}
.pin-form {
  text-align: left;
  font-size: 13px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  overflow-x: hidden;
}
.pin-form-row {
  margin-bottom: 10px;
  min-width: 0;
}
.pin-form-row label {
  display: block;
  margin-bottom: 4px;
  font-size: 12px;
  font-weight: 600;
  opacity: 0.9;
}
.pin-form-row .req { color: #f87171; }
.pin-form-input {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  border-radius: 10px;
  border: 1px solid #334155;
  background: #0b1220;
  color: #e2e8f0;
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
.pin-form-input:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
}
.pin-form-textarea {
  min-height: 72px;
  resize: vertical;
  font-family: inherit;
}
.pin-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.pin-form-grid > .pin-form-row {
  min-width: 0;
}
.pin-form-upload {
  border: 1px dashed #475569;
  border-radius: 12px;
  padding: 12px;
  background: rgba(15, 23, 42, 0.55);
  position: relative;
  box-sizing: border-box;
  max-width: 100%;
}
.pin-form-upload input[type="file"] {
  width: 100%;
  max-width: 100%;
  font-size: 12px;
  color: #94a3b8;
  box-sizing: border-box;
}
.pin-form-upload-hint {
  margin-top: 6px;
  font-size: 11px;
  color: #94a3b8;
}
.pin-form-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.pin-form-preview-item {
  width: 64px;
  height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #334155;
  flex-shrink: 0;
}
.pin-form-preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.pin-form-coords {
  margin-top: 8px;
  font-size: 11px;
  color: #94a3b8;
  word-break: break-all;
}
.pin-img-del {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 4px;
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 8px;
  background: #7f1d1d;
  color: #fecaca;
  cursor: pointer;
}
.pin-img-del:hover { background: #991b1b; }
.pin-add-images-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #93c5fd;
  cursor: pointer;
}

/* Dark mode explicit (default) */
.pin-form-swal--dark .pin-form-input,
html[data-theme="dark"] .pin-form-input,
:root:not([data-theme="light"]) .pin-form-input {
  background: rgba(8, 15, 28, 0.92);
  color: #e2e8f0;
  border-color: #1e3a5f;
}
.pin-form-swal--dark .pin-form-upload,
html[data-theme="dark"] .pin-form-upload,
:root:not([data-theme="light"]) .pin-form-upload {
  background: rgba(8, 15, 28, 0.65);
  border-color: #1e3a5f;
}
.pin-form-swal--dark .pin-form-upload-hint,
.pin-form-swal--dark .pin-form-coords,
html[data-theme="dark"] .pin-form-upload-hint,
html[data-theme="dark"] .pin-form-coords {
  color: #94a3b8;
}
.pin-form-swal--dark select.pin-form-input option {
  background: #0f2137;
  color: #e2e8f0;
}

/* Light mode */
.pin-form-swal--light .pin-form-input,
html[data-theme="light"] .pin-form-input {
  background: #ffffff;
  color: #0f172a;
  border-color: #cbd5e1;
}
.pin-form-swal--light .pin-form-upload,
html[data-theme="light"] .pin-form-upload {
  background: rgba(255, 255, 255, 0.75);
  border-color: #94a3b8;
}
.pin-form-swal--light .pin-form-upload-hint,
.pin-form-swal--light .pin-form-coords,
html[data-theme="light"] .pin-form-upload-hint,
html[data-theme="light"] .pin-form-coords {
  color: #64748b;
}
.pin-form-swal--light .pin-form-preview-item,
html[data-theme="light"] .pin-form-preview-item {
  border-color: #cbd5e1;
}
.pin-form-swal--light .pin-add-images-btn,
html[data-theme="light"] .pin-add-images-btn {
  color: #1d4ed8;
}
.pin-form-swal--light select.pin-form-input option {
  background: #ffffff;
  color: #0f172a;
}
.pin-form-swal--light .pin-form-row label {
  color: #334155;
}

@media (max-width: 640px) {
  .pin-form-grid { grid-template-columns: 1fr; }
  .pin-form-swal.swal2-popup,
  .pin-form-swal {
    max-width: calc(100vw - 12px) !important;
    width: calc(100vw - 12px) !important;
    border-radius: 12px !important;
  }
  .pin-form-swal .swal2-title {
    font-size: 1rem !important;
    padding: 0.75rem 0.65rem 0.2rem !important;
  }
  .pin-form-swal-html,
  .pin-form-swal .swal2-html-container {
    max-height: min(55vh, 400px) !important;
    padding: 0.35rem 0.65rem !important;
  }
  .pin-form-input {
    font-size: 16px !important; /* กัน iOS zoom */
    padding: 10px !important;
  }
  .pin-form-upload { padding: 10px; }
  .pin-form-preview-item { width: 52px; height: 52px; }
  .pin-form-swal .swal2-actions .swal2-styled {
    width: 100%;
    min-width: 0 !important;
  }
}



/* ===== Pin form open/close animation ===== */
@keyframes pinFormIn {
  0% {
    opacity: 0;
    transform: translate3d(0, 18px, 0) scale(0.94);
  }
  100% {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(1);
  }
}
@keyframes pinFormOut {
  0% {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(1);
  }
  100% {
    opacity: 0;
    transform: translate3d(0, 12px, 0) scale(0.96);
  }
}
@keyframes pinBackdropIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pinBackdropOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

.pin-form-anim-in {
  animation: pinFormIn 0.32s cubic-bezier(0.22, 1, 0.36, 1) both !important;
}
.pin-form-anim-out {
  animation: pinFormOut 0.2s ease-in both !important;
}
.pin-form-backdrop-in {
  animation: pinBackdropIn 0.28s ease-out both !important;
}
.pin-form-backdrop-out {
  animation: pinBackdropOut 0.18s ease-in both !important;
}

/* Backdrop เข้ากับธีม */
.pin-form-swal-backdrop.swal2-backdrop-show,
div.swal2-container.pin-form-swal-container {
  background: rgba(2, 8, 20, 0.72) !important;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
html[data-theme="light"] .pin-form-swal-container.swal2-container,
html[data-theme="light"] div.swal2-container.pin-form-swal-container {
  background: rgba(15, 23, 42, 0.35) !important;
}

@media (prefers-reduced-motion: reduce) {
  .pin-form-anim-in,
  .pin-form-anim-out,
  .pin-form-backdrop-in,
  .pin-form-backdrop-out {
    animation-duration: 0.01ms !important;
  }
}


/* Offline map download progress */
.offline-map-progress {
  position: absolute;
  left: 12px;
  bottom: 88px;
  z-index: 1000;
  width: min(260px, calc(100vw - 24px));
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid rgba(148, 163, 184, 0.25);
  box-shadow: 0 4px 16px rgba(0,0,0,0.35);
  backdrop-filter: blur(6px);
}
.offline-map-progress.hidden { display: none !important; }
.offline-map-progress-label {
  font-size: 11px;
  color: #e2e8f0;
  margin-bottom: 6px;
  line-height: 1.3;
}
.offline-map-progress-track {
  height: 6px;
  border-radius: 999px;
  background: rgba(51, 65, 85, 0.9);
  overflow: hidden;
}
.offline-map-progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #0ea5e9, #22c55e);
  transition: width 0.2s ease;
}
html[data-theme="light"] .offline-map-progress {
  background: rgba(255,255,255,0.95);
  border-color: #cbd5e1;
}
html[data-theme="light"] .offline-map-progress-label { color: #0f172a; }
@media (max-width: 640px) {
  .offline-map-progress {
    left: 8px;
    bottom: 72px;
    width: min(220px, calc(100vw - 16px));
  }
}


/* ==========================================================================
   SATUN 360° — แถบหน้าแรก + แผนที่ออฟไลน์
   --------------------------------------------------------------------------
   ส่วนนี้ควบคุม:
   1) แถบทางลัดหน้าแรก (.home-toolbar)
   2) ไอคอนหมวดหมู่ (#category-icon-bar / .category-icon-btn)
   3) ปุ่มและแผงแผนที่ออฟไลน์ (.offline-map-btn / .offline-map-panel)
   ========================================================================== */


/* --------------------------------------------------------------------------
   1) แถบทางลัดหน้าแรก (.home-toolbar)
   -------------------------------------------------------------------------- */
.home-toolbar {
  display: flex;                 /* จัดเรียงลูกแบบ flex */
  flex-direction: column;        /* มือถือ: เรียงจากบนลงล่าง (หมวด → ปุ่ม) */
  gap: 6px;                      /* ระยะห่างระหว่างแถวลูก 6px */
}

@media (min-width: 640px) {
  .home-toolbar {
    flex-direction: row;         /* จอใหญ่: เรียงซ้าย→ขวา */
    flex-wrap: wrap;             /* ถ้าแคบเกินให้ขึ้นบรรทัดใหม่ได้ */
    align-items: center;         /* จัดกึ่งกลางแนวตั้ง */
    justify-content: space-between; /* หมวดชิดซ้าย ปุ่มชิดขวา */
  }
}

.home-toolbar-actions {
  width: 100%;                   /* มือถือ: กว้างเต็มแถบ */
}

@media (min-width: 640px) {
  .home-toolbar-actions {
    width: auto;                 /* จอใหญ่: กว้างตามเนื้อหาปุ่ม */
    flex-shrink: 0;              /* ไม่ให้ถูกบีบจนปุ่มหาย */
  }
}


/* --------------------------------------------------------------------------
   2) ไอคอนหมวดหมู่บนแถบ
   -------------------------------------------------------------------------- */

.category-icon-bar-wrap {
  width: 100%;                   /* มือถือ: กว้างเต็มแถว */
  min-width: 0;                  /* อนุญาตให้หดได้ → เปิดทางให้ overflow-x ทำงาน */
}

@media (min-width: 640px) {
  .category-icon-bar-wrap {
    width: auto;                 /* กว้างตามเนื้อหา / flex */
    flex: 1 1 auto;              /* ขยายได้ หดได้ พื้นฐานตามเนื้อหา */
    max-width: calc(100% - 12rem); /* กันล้ำพื้นที่ปุ่มทางลัด */
  }
}

#category-icon-bar {
  display: flex;                 /* เรียงไอคอนเป็นแถว */
  flex-wrap: nowrap;             /* ไม่ขึ้นบรรทัดใหม่ — เลื่อนแนวนอนแทน */
  align-items: center;           /* กึ่งกลางแนวตั้ง */
  gap: 8px;                      /* ช่องว่างระหว่างไอคอน */
  width: 100%;                   /* กว้างเต็มกล่องห่อ */
  max-width: 100%;               /* ไม่ล้นพาเรนต์ */
  overflow-x: auto;              /* เลื่อนซ้าย–ขวาเมื่อไอคอนเยอะ */
  overflow-y: hidden;            /* ไม่เลื่อนแนวตั้ง */
  -webkit-overflow-scrolling: touch; /* โมเมนตัมสกรอลล์บน iOS */
  overscroll-behavior-x: contain; /* ไม่ดึงหน้าทั้งหน้าตอนเลื่อนสุดขอบ */
  touch-action: pan-x;           /* นิ้วเลื่อนแนวนอนในแถบนี้ได้ */
  scrollbar-width: none;         /* Firefox: ซ่อน scrollbar */
  padding-bottom: 2px;           /* กันเงา/ขอบล่างถูกตัด */
}

#category-icon-bar::-webkit-scrollbar {
  display: none;                 /* Chrome/Safari: ซ่อน scrollbar */
}

.category-icon-btn {
  flex: 0 0 auto;                /* ไม่ยืด ไม่หด — ขนาดคงที่ */
  box-sizing: border-box;        /* width รวม border/padding */
  width: 40px;                   /* ขนาดสัมผัสประมาณมาตรฐานมือถือ */
  height: 40px;
  min-width: 40px;               /* กัน flex ย่อเล็กกว่านี้ */
  min-height: 40px;
  padding: 0;                    /* ไม่มีช่องว่างในปุ่ม — ไอคอนเต็มพื้นที่ */
  border-radius: 10px;           /* มุมโค้ง */
  /* เส้นขอบผสมสีหมวด (--cat-color จาก inline style) กับสีพื้น */
  border: 1px solid color-mix(in srgb, var(--cat-color, #6366f1) 45%, #334155);
  /* พื้นหลังโปร่งสีหมวดบนพื้นเข้ม */
  background: color-mix(in srgb, var(--cat-color, #6366f1) 18%, #0f172a);
  cursor: pointer;               /* แสดงเป็นคลิกได้ */
  transition: transform 0.15s ease, filter 0.15s ease; /* อนิเมชันตอน hover */
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15); /* เงาบาง ๆ ให้นูน */
}

.category-icon-btn:hover {
  transform: translateY(-1px);   /* ยกขึ้น 1px ตอนชี้ */
  filter: brightness(1.12);      /* สว่างขึ้นเล็กน้อย */
}

.category-icon-face {
  position: relative;            /* เป็นจุดอ้างอิงให้ .category-icon-count ซ้อนทับ */
  display: flex;
  align-items: center;           /* จัด emoji/icon กึ่งกลาง */
  justify-content: center;
  width: 100%;
  height: 100%;
}

.category-icon-emoji {
  font-size: 1.15rem;            /* ขนาด emoji */
  line-height: 1;                /* ไม่มีช่องว่างบรรทัดเกิน */
  opacity: 0.85;                 /* จางลงนิดให้ตัวเลขอ่านง่าย */
}

.category-icon-face > i {
  font-size: 0.95rem;            /* ขนาด Font Awesome */
  color: var(--cat-color, #a5b4fc); /* สีตามหมวด */
  opacity: 0.9;
}

.category-icon-count {
  position: absolute;            /* ซ้อนทับกึ่งกลางไอคอน */
  inset: 0;                      /* top/right/bottom/left = 0 ครบ */
  display: flex;
  align-items: center;           /* ตัวเลขกึ่งกลางแนวตั้ง */
  justify-content: center;       /* ตัวเลขกึ่งกลางแนวนอน */
  font-size: 11px;
  font-weight: 800;              /* ตัวหนาชัด */
  color: #fff;
  line-height: 1;
  pointer-events: none;          /* คลิกทะลุไปที่ปุ่ม ไม่ถูกตัวเลขบัง */
  text-shadow:
    0 0 3px rgba(0, 0, 0, 0.9),  /* เงาดำรอบตัวอักษรให้อ่านบน emoji */
    0 1px 2px rgba(0, 0, 0, 0.85);
}

html[data-theme="light"] .category-icon-btn {
  background: color-mix(in srgb, var(--cat-color, #6366f1) 12%, #fff); /* พื้นสว่าง */
  border-color: color-mix(in srgb, var(--cat-color, #6366f1) 35%, #cbd5e1);
}

html[data-theme="light"] .category-icon-count {
  color: #0f172a;                /* ตัวเลขสีเข้มบนพื้นสว่าง */
  text-shadow:
    0 0 2px rgba(255, 255, 255, 0.9),
    0 1px 1px rgba(255, 255, 255, 0.8);
}

@media (max-width: 639px) {
  .category-icon-btn {
    width: 38px;                 /* มือถือ: เล็กลงเล็กน้อยประหยัดพื้นที่ */
    height: 38px;
    min-width: 38px;
    min-height: 38px;
  }

  .category-icon-emoji {
    font-size: 1.05rem;
  }

  .category-icon-count {
    font-size: 10px;
  }

  .home-toolbar-actions {
    justify-content: flex-start !important; /* ปุ่มทางลัดชิดซ้ายบนมือถือ */
  }
}


/* --------------------------------------------------------------------------
   3) ปุ่มแผนที่ออฟไลน์ (ไอคอนบนแผนที่)
   ตำแหน่ง: มุมขวาล่าง แนวเดียวกับ Layers
   ขนาด: 34×34 ≈ ปุ่มซูม/ค้นหา Leaflet
   -------------------------------------------------------------------------- */
.offline-map-btn,
.offline-map-btn.satun-icon-btn {
  position: absolute !important; /* วางอิสระบน .map-wrapper */
  left: auto !important;         /* ไม่ยึดซ้าย */
  /* ระยะจากขอบขวา (รวม safe-area มือถือที่มี notch) */
  right: max(30px, calc(20px + var(--satun-safe-right, 10px))) !important;
  top: auto !important;          /* ไม่ยึดบน */
  /* ระยะจากขอบล่าง */
  bottom: max(58px, calc(54px + var(--satun-safe-bottom, 0px))) !important;
  z-index: 1000 !important;      /* อยู่เหนือแผนที่/ไทล์ */

  width: 34px !important;        /* ขนาดเท่าปุ่มควบคุม Leaflet โดยประมาณ */
  height: 34px !important;
  min-width: 34px !important;
  min-height: 34px !important;
  padding: 0 !important;         /* ไอคอนอยู่กลางปุ่ม */
  margin: 0 !important;

  display: inline-flex !important;
  align-items: center !important;    /* กึ่งกลางแนวตั้ง */
  justify-content: center !important; /* กึ่งกลางแนวนอน */
  gap: 0 !important;

  border-radius: 4px !important; /* มุมแบบ Leaflet control */
  border: 2px solid rgba(0, 0, 0, 0.2) !important;
  background: #fff !important;   /* พื้นขาวโหมดสว่างเริ่มต้น */
  color: #333 !important;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.4) !important; /* เงาแบบ leaflet-bar */
  font-size: 14px !important;
  line-height: 1 !important;
  cursor: pointer;
  max-width: none !important;    /* ไม่จำกัดความกว้างจากกฎเก่า */
}

.offline-map-btn i {
  color: #15803d;                /* ไอคอนสีเขียว */
  font-size: 15px;
}

.offline-map-btn:hover {
  background: #f4f4f4 !important; /* เทาอ่อนตอนชี้ */
  filter: none !important;       /* ยกเลิก filter จากกฎเก่า */
}

.offline-map-btn.is-loading {
  opacity: 0.75;                 /* จางลงตอนกำลังโหลด */
  pointer-events: none;          /* กันกดซ้ำ */
}

.offline-map-btn.is-loading i {
  animation: offlineMapSpin 0.9s linear infinite; /* หมุนไอคอน */
}

@keyframes offlineMapSpin {
  to { transform: rotate(360deg); } /* หมุนครบ 1 รอบ */
}

/* ซ่อนข้อความป้าย — อ่านผ่าน title / screen reader แทน */
.offline-map-btn-label,
.offline-map-btn .offline-map-btn-label {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important; /* เทคนิค sr-only */
  white-space: nowrap !important;
  border: 0 !important;
}

html[data-theme="dark"] .offline-map-btn,
html[data-theme="dark"] .offline-map-btn.satun-icon-btn {
  background: #1e293b !important; /* พื้นเข้มโหมดมืด */
  border-color: #334155 !important;
  color: #e2e8f0 !important;
}

html[data-theme="dark"] .offline-map-btn i {
  color: #4ade80;                /* เขียวสว่างบนพื้นเข้ม */
}

/* แถบ progress ตอนดาวน์โหลดไทล์ */
.offline-map-progress {
  position: absolute;
  left: auto !important;
  right: max(30px, calc(20px + var(--satun-safe-right, 10px))) !important; /* แนวเดียวกับปุ่ม */
  top: auto !important;
  bottom: max(100px, calc(96px + var(--satun-safe-bottom, 0px))) !important; /* เหนือปุ่ม */
  z-index: 1000;
  width: min(200px, calc(100vw - 56px)) !important; /* ไม่ล้นจอแคบ */
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.92); /* พื้นโปร่งมืด */
  border: 1px solid rgba(148, 163, 184, 0.25);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

.offline-map-progress.hidden {
  display: none !important;      /* ซ่อนเมื่อไม่ได้ดาวน์โหลด */
}

.offline-map-progress-label {
  font-size: 11px;
  color: #e2e8f0;
  margin-bottom: 6px;            /* ห่างจากแถบ progress */
  line-height: 1.3;
}

.offline-map-progress-track {
  height: 6px;                   /* ความสูงราง */
  border-radius: 999px;          /* โค้งหัวท้าย */
  background: rgba(51, 65, 85, 0.9); /* สีรางว่าง */
  overflow: hidden;              /* ตัดมุม fill */
}

.offline-map-progress-fill {
  height: 100%;
  width: 0%;                     /* JS ปรับตามเปอร์เซ็นต์ */
  border-radius: 999px;
  background: linear-gradient(90deg, #0ea5e9, #22c55e); /* ฟ้า→เขียว */
  transition: width 0.2s ease;   /* เลื่อนแถบสมูธ */
}

html[data-theme="light"] .offline-map-progress {
  background: rgba(255, 255, 255, 0.95);
  border-color: #cbd5e1;
}

html[data-theme="light"] .offline-map-progress-label {
  color: #0f172a;
}

@media (max-width: 640px) {
  .offline-map-btn,
  .offline-map-btn.satun-icon-btn {
    right: max(30px, calc(20px + var(--satun-safe-right, 10px))) !important;
    bottom: max(110px, calc(102px + var(--satun-safe-bottom, 0px))) !important; /* ยกขึ้นกันเมนูล่าง */
  }

  .offline-map-progress {
    right: max(30px, calc(20px + var(--satun-safe-right, 10px))) !important;
    bottom: max(152px, calc(144px + var(--satun-safe-bottom, 0px))) !important;
  }
}


/* --------------------------------------------------------------------------
   4) แผงจัดการไฟล์แผนที่ออฟไลน์
   -------------------------------------------------------------------------- */
.offline-map-panel {
  position: absolute;            /* ลอยบนแผนที่ */
  right: max(30px, calc(20px + var(--satun-safe-right, 10px))); /* ชิดขวาแนวเดียวปุ่ม */
  bottom: max(100px, calc(96px + var(--satun-safe-bottom, 0px))); /* เหนือปุ่มไอคอน */
  z-index: 1100;                 /* สูงกว่าปุ่ม/progress */
  width: min(280px, calc(100vw - 24px)); /* กว้างสุด 280px หรือจอ−ขอบ */
  background: rgba(15, 23, 42, 0.96);
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);    /* เบลอพื้นหลังแผนที่ */
  overflow: hidden;              /* ตัดมุมหัวแผง */
}

.offline-map-panel.hidden {
  display: none !important;      /* ปิดแผง */
}

.offline-map-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between; /* ชื่อซ้าย ปุ่มปิดขวา */
  padding: 10px 12px;
  border-bottom: 1px solid rgba(51, 65, 85, 0.8);
  color: #f1f5f9;
  font-size: 13px;
  font-weight: 600;
}

.offline-map-panel-close {
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #94a3b8;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}

.offline-map-panel-close:hover {
  background: rgba(51, 65, 85, 0.6);
  color: #fff;
}

.offline-map-panel-body {
  padding: 12px;                 /* ระยะในเนื้อหาแผง */
}

.offline-map-panel-stats {
  font-size: 12px;
  color: #e2e8f0;
  margin: 0 0 8px;               /* ห่างจากข้อความถัดไป */
  line-height: 1.45;
}

.offline-map-panel-stats .text-muted {
  color: #94a3b8;                /* รายละเอียดแคชสีจาง */
  font-size: 11px;
}

.offline-map-panel-hint {
  font-size: 11px;
  color: #94a3b8;
  margin: 0 0 12px;
  line-height: 1.4;
}

.offline-map-panel-actions {
  display: flex;
  flex-direction: column;        /* ปุ่มเรียงแนวตั้ง */
  gap: 8px;                      /* ห่างระหว่างปุ่ม */
}

.offline-map-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;                      /* ห่างไอคอนกับข้อความ */
  width: 100%;                   /* กว้างเต็มแผง */
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid rgba(71, 85, 105, 0.8);
  background: rgba(30, 41, 59, 0.9);
  color: #e2e8f0;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.offline-map-action:hover {
  filter: brightness(1.08);      /* สว่างขึ้นตอนชี้ */
}

.offline-map-action.primary {
  background: rgba(22, 163, 74, 0.25); /* โทนเขียว = ดาวน์โหลด */
  border-color: rgba(34, 197, 94, 0.45);
  color: #86efac;
}

.offline-map-action.danger {
  background: rgba(185, 28, 28, 0.2); /* โทนแดง = ลบ */
  border-color: rgba(248, 113, 113, 0.35);
  color: #fca5a5;
}

html[data-theme="light"] .offline-map-panel {
  background: rgba(255, 255, 255, 0.98);
  border-color: #cbd5e1;
}

html[data-theme="light"] .offline-map-panel-head {
  color: #0f172a;
  border-bottom-color: #e2e8f0;
}

html[data-theme="light"] .offline-map-panel-stats {
  color: #0f172a;
}

html[data-theme="light"] .offline-map-action {
  background: #f8fafc;
  color: #0f172a;
  border-color: #cbd5e1;
}

@media (max-width: 640px) {
  .offline-map-panel {
    right: max(12px, var(--satun-safe-right, 10px)); /* ชิดขอบจอมากขึ้น */
    bottom: max(150px, calc(140px + var(--satun-safe-bottom, 0px))); /* สูงขึ้นกันทับ UI ล่าง */
  }
}


/* --------------------------------------------------------------------------
   เลือกวิธีปักหมุดตำแหน่ง (GPS / แผนที่)
   -------------------------------------------------------------------------- */
.pin-loc-choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
}
.pin-loc-btn {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(71, 85, 105, 0.85);
  background: rgba(30, 41, 59, 0.95);
  color: #e2e8f0;
  cursor: pointer;
  transition: border-color 0.15s ease, filter 0.15s ease;
}
.pin-loc-btn i {
  font-size: 1.15rem;
  color: #38bdf8;
  margin-bottom: 4px;
}
.pin-loc-btn:hover {
  filter: brightness(1.08);
  border-color: rgba(56, 189, 248, 0.55);
}
.pin-loc-title {
  font-size: 14px;
  font-weight: 700;
}
.pin-loc-desc {
  font-size: 12px;
  color: #94a3b8;
}
html[data-theme="light"] .pin-loc-btn {
  background: #f8fafc;
  color: #0f172a;
  border-color: #cbd5e1;
}
html[data-theme="light"] .pin-loc-desc {
  color: #64748b;
}


/* --------------------------------------------------------------------------
   Popup เลือกหมวดหมู่ปักหมุด — อ่านชัดใน Dark / Light mode
   -------------------------------------------------------------------------- */
.pin-cat-swal {
  border: 1px solid rgba(71, 85, 105, 0.6) !important;
}
.pin-cat-title {
  color: inherit !important;
  font-weight: 700 !important;
}
/* ช่อง select ต้องมีพื้นและตัวอักษรตัดกันชัด */
.swal2-popup.pin-cat-swal .swal2-select,
.pin-cat-select,
select.pin-cat-select,
.swal2-popup .swal2-select.pin-cat-select {
  width: 100% !important;
  margin: 0.5rem 0 0 !important;
  padding: 10px 12px !important;
  border-radius: 10px !important;
  border: 1px solid #475569 !important;
  background: #0b1220 !important;
  color: #f1f5f9 !important;
  font-size: 15px !important;
  font-weight: 500 !important;
  line-height: 1.4 !important;
  -webkit-appearance: auto !important;
  appearance: auto !important;
}
.swal2-popup.pin-cat-swal .swal2-select option,
.pin-cat-select option {
  background: #0b1220 !important;
  color: #f1f5f9 !important;
}
/* Light mode */
html[data-theme="light"] .swal2-popup.pin-cat-swal .swal2-select,
html[data-theme="light"] .pin-cat-select,
html[data-theme="light"] select.pin-cat-select {
  background: #ffffff !important;
  color: #0f172a !important;
  border-color: #94a3b8 !important;
}
html[data-theme="light"] .swal2-popup.pin-cat-swal .swal2-select option,
html[data-theme="light"] .pin-cat-select option {
  background: #ffffff !important;
  color: #0f172a !important;
}
/* ข้อความรอง / placeholder */
.swal2-popup.pin-cat-swal .swal2-html-container {
  color: inherit !important;
}


/* --------------------------------------------------------------------------
   แผนที่ออฟไลน์ — ย้ายไปกลุ่มซ้ายบน (คู่กับรีเฟรช)
   ปุ่มจริงเป็น Leaflet control; #btn-offline-map ซ่อนไว้เป็นตัวจับ event
   -------------------------------------------------------------------------- */
#btn-offline-map.offline-map-btn,
.offline-map-btn.satun-icon-btn {
  /* ซ่อนปุ่มลอยเดิม (มุมขวาล่าง) ไม่ให้ทับโหมดเจ้าหน้าที่ */
  position: absolute !important;
  left: -9999px !important;
  top: auto !important;
  bottom: auto !important;
  right: auto !important;
  width: 1px !important;
  height: 1px !important;
  opacity: 0 !important;
  pointer-events: none !important;
  overflow: hidden !important;
}

/* แผงจัดการ + progress อยู่ซ้าย ใต้กลุ่มปุ่มซูม/รีเฟรช */
.offline-map-panel {
  left: max(10px, var(--satun-safe-left, 10px)) !important;
  right: auto !important;
  top: 190px !important;
  bottom: auto !important;
  z-index: 1100 !important;
}
.offline-map-progress {
  left: max(10px, var(--satun-safe-left, 10px)) !important;
  right: auto !important;
  top: 150px !important;
  bottom: auto !important;
}
@media (max-width: 640px) {
  .offline-map-panel {
    left: max(10px, var(--satun-safe-left, 10px)) !important;
    right: auto !important;
    top: 210px !important;
    bottom: auto !important;
    width: min(280px, calc(100vw - 20px)) !important;
  }
  .offline-map-progress {
    left: max(10px, var(--satun-safe-left, 10px)) !important;
    right: auto !important;
    top: 170px !important;
    bottom: auto !important;
  }
}


/* ระดับความเสี่ยง — ขยับขึ้น 10px ไม่ทับแถบพิกัด lat/lng */
.map-legend {
  bottom: 38px !important;
}
@media (max-width: 1023px) {
  .map-legend {
    bottom: calc(82px + env(safe-area-inset-bottom, 0px)) !important;
  }
}
@media (max-width: 640px) {
  .map-legend {
    bottom: calc(62px + env(safe-area-inset-bottom, 0px)) !important;
  }
}


/* ปุ่ม Street View ใน popup */
.popup-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.popup-btn-sv {
  background: #0f766e !important;
  color: #ecfdf5 !important;
  border: 1px solid rgba(45, 212, 191, 0.35) !important;
}
.popup-btn-sv:hover {
  filter: brightness(1.08);
}


/* รายการค้นหา Layers — แสดงคำอธิบายย่อ */
.satun-search-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: 0;
  border-bottom: 1px solid rgba(51, 65, 85, 0.6);
  background: transparent;
  color: inherit;
  cursor: pointer;
}
.satun-search-item:hover,
.satun-search-item:focus {
  background: rgba(56, 189, 248, 0.12);
}
.satun-search-item-title {
  font-size: 13px;
  font-weight: 600;
  color: #f1f5f9;
}
.satun-search-item-meta {
  font-size: 11px;
  color: #94a3b8;
}
.satun-search-item-desc {
  font-size: 11px;
  color: #cbd5e1;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
html[data-theme="light"] .satun-search-item-title { color: #0f172a; }
html[data-theme="light"] .satun-search-item-desc { color: #475569; }
html[data-theme="light"] .satun-search-item:hover {
  background: rgba(14, 165, 233, 0.1);
}


/* ==========================================================================
   Leaflet Popup — ปรับให้สวยงาม (Dark / Light)
   ========================================================================== */

.leaflet-popup {
  margin-bottom: 16px;
}

.leaflet-popup-content-wrapper {
  background: linear-gradient(165deg, #132337 0%, #0f2137 55%, #0c1a2c 100%) !important;
  color: #e2e8f0 !important;
  border-radius: 14px !important;
  border: 1px solid rgba(56, 189, 248, 0.18) !important;
  box-shadow:
    0 12px 40px rgba(0, 0, 0, 0.45),
    0 0 0 1px rgba(15, 23, 42, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.04) !important;
  padding: 0 !important;
  overflow: hidden;
  backdrop-filter: blur(10px);
}

.leaflet-popup-tip {
  background: #0f2137 !important;
  border: none !important;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

.leaflet-popup-content {
  margin: 12px 14px 14px !important;
  min-width: 200px;
  max-width: min(320px, 86vw);
  font-size: 13px;
  line-height: 1.45;
  font-family: 'Sarabun', system-ui, sans-serif;
}

.leaflet-popup-close-button {
  top: 8px !important;
  right: 8px !important;
  width: 28px !important;
  height: 28px !important;
  padding: 0 !important;
  border-radius: 8px !important;
  background: rgba(30, 41, 59, 0.85) !important;
  color: #94a3b8 !important;
  font-size: 18px !important;
  font-weight: 500 !important;
  line-height: 26px !important;
  text-align: center !important;
  transition: background 0.15s ease, color 0.15s ease;
}

.leaflet-popup-close-button:hover {
  background: rgba(239, 68, 68, 0.25) !important;
  color: #fca5a5 !important;
}

/* หัวข้อ */
.popup-title {
  font-size: 15px;
  font-weight: 700;
  color: #f8fafc;
  margin: 0 28px 6px 0;
  line-height: 1.35;
  letter-spacing: 0.01em;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.popup-title .pin-pop-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05em;
}

/* เมตา / รหัส / หน่วยงาน */
.popup-meta {
  font-size: 11.5px;
  color: #94a3b8;
  margin-bottom: 8px;
  line-height: 1.4;
}

/* บล็อครายละเอียด */
.popup-section {
  margin: 8px 0;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid rgba(51, 65, 85, 0.7);
  color: #cbd5e1;
  font-size: 12.5px;
  line-height: 1.45;
}

.popup-section strong {
  color: #e2e8f0;
  font-weight: 600;
}

.popup-section img {
  border-radius: 8px;
  margin-top: 4px;
}

/* สถานะ */
.popup-status {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  margin-bottom: 8px;
  background: rgba(51, 65, 85, 0.6);
  color: #cbd5e1;
}

.popup-status.status-online,
.popup-status.status-approved {
  background: rgba(22, 163, 74, 0.2);
  color: #86efac;
}
.popup-status.status-delayed,
.popup-status.status-pending {
  background: rgba(202, 138, 4, 0.2);
  color: #fde047;
}
.popup-status.status-offline,
.popup-status.status-rejected {
  background: rgba(220, 38, 38, 0.2);
  color: #fca5a5;
}

/* กลุ่มปุ่ม */
.popup-nav {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(51, 65, 85, 0.75);
}

.popup-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  border-radius: 9px;
  border: 1px solid rgba(71, 85, 105, 0.8);
  background: rgba(30, 41, 59, 0.95);
  color: #e2e8f0 !important;
  font-size: 12px;
  font-weight: 600;
  text-decoration: none !important;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease, border-color 0.12s ease;
  line-height: 1.2;
  white-space: nowrap;
}

.popup-btn:hover {
  filter: brightness(1.1);
  transform: translateY(-1px);
}

.popup-btn-nav {
  background: linear-gradient(135deg, #1d4ed8, #2563eb) !important;
  border-color: rgba(96, 165, 250, 0.45) !important;
  color: #eff6ff !important;
}

.popup-btn-sv {
  background: linear-gradient(135deg, #0f766e, #14b8a6) !important;
  border-color: rgba(45, 212, 191, 0.4) !important;
  color: #ecfdf5 !important;
}

.popup-btn-fb {
  background: linear-gradient(135deg, #1e3a8a, #1d4ed8) !important;
  border-color: rgba(96, 165, 250, 0.35) !important;
  color: #dbeafe !important;
}

.pin-detail-btn {
  width: 100%;
  margin-top: 8px;
  background: rgba(124, 58, 237, 0.25) !important;
  border-color: rgba(167, 139, 250, 0.4) !important;
  color: #ddd6fe !important;
}

.pin-detail-box {
  margin-top: 8px;
  max-height: 220px;
  overflow: auto;
  border-radius: 10px;
}

/* Light mode */
html[data-theme="light"] .leaflet-popup-content-wrapper {
  background: linear-gradient(165deg, #ffffff 0%, #f8fafc 100%) !important;
  color: #0f172a !important;
  border-color: rgba(148, 163, 184, 0.45) !important;
  box-shadow:
    0 12px 32px rgba(15, 23, 42, 0.12),
    0 0 0 1px rgba(226, 232, 240, 0.9) !important;
}

html[data-theme="light"] .leaflet-popup-tip {
  background: #ffffff !important;
}

html[data-theme="light"] .leaflet-popup-close-button {
  background: #f1f5f9 !important;
  color: #64748b !important;
}

html[data-theme="light"] .leaflet-popup-close-button:hover {
  background: #fee2e2 !important;
  color: #dc2626 !important;
}

html[data-theme="light"] .popup-title {
  color: #0f172a;
}

html[data-theme="light"] .popup-meta {
  color: #64748b;
}

html[data-theme="light"] .popup-section {
  background: #f8fafc;
  border-color: #e2e8f0;
  color: #334155;
}

html[data-theme="light"] .popup-section strong {
  color: #0f172a;
}

html[data-theme="light"] .popup-nav {
  border-top-color: #e2e8f0;
}

html[data-theme="light"] .popup-btn {
  background: #f1f5f9;
  border-color: #cbd5e1;
  color: #0f172a !important;
}

html[data-theme="light"] .pin-detail-btn {
  background: rgba(124, 58, 237, 0.12) !important;
  color: #5b21b6 !important;
  border-color: rgba(167, 139, 250, 0.45) !important;
}

/* มือถือ */
@media (max-width: 640px) {
  .leaflet-popup-content {
    margin: 10px 12px 12px !important;
    max-width: min(300px, 88vw);
    font-size: 12.5px;
  }
  .popup-title {
    font-size: 14px;
  }
  .popup-btn {
    padding: 9px 8px;
    font-size: 11.5px;
  }
}



/* ==========================================================================
   Leaflet Popup — Animation ลื่นไหล (ไม่ทำให้ popup หาย)
   ========================================================================== */

@keyframes satunPopupIn {
  from {
    opacity: 0;
    transform: translate3d(0, 10px, 0) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(1);
  }
}

.leaflet-popup {
  opacity: 1 !important; /* กัน animation ค้างแล้วหาย */
}

.leaflet-popup.satun-popup-anim {
  animation: satunPopupIn 0.25s cubic-bezier(0.22, 1, 0.36, 1);
  will-change: transform, opacity;
}

/* หลัง animation จบ ต้องมองเห็นเสมอ */
.leaflet-popup.satun-popup-anim,
.leaflet-popup:not(.satun-popup-anim) {
  opacity: 1;
}

.leaflet-popup .leaflet-popup-content-wrapper {
  opacity: 1 !important;
  visibility: visible !important;
}

.leaflet-popup .popup-btn {
  transition:
    transform 0.18s cubic-bezier(0.22, 1, 0.36, 1),
    filter 0.18s ease,
    box-shadow 0.18s ease;
}

.leaflet-popup .popup-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.leaflet-popup .popup-btn:active {
  transform: translateY(0);
}

.leaflet-popup-close-button {
  transition: background 0.18s ease, color 0.18s ease, transform 0.18s ease !important;
}

.leaflet-popup-close-button:hover {
  transform: scale(1.05);
}

@media (prefers-reduced-motion: reduce) {
  .leaflet-popup.satun-popup-anim {
    animation: none !important;
  }
}


/* การ์ดรายละเอียดข้างช่องค้นหา (hover) */
.satun-search-hover-card {
  width: min(300px, calc(100vw - 24px));
  max-height: min(360px, 70vh);
  overflow: auto;
  border-radius: 14px;
  border: 1px solid rgba(56, 189, 248, 0.22);
  background: linear-gradient(165deg, #132337 0%, #0f2137 100%);
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.45);
  color: #e2e8f0;
  font-size: 13px;
  line-height: 1.45;
  animation: satunPopupIn 0.2s cubic-bezier(0.22, 1, 0.36, 1);
}
.satun-search-hover-card.hidden {
  display: none !important;
}
.satun-shc-inner {
  padding: 12px 14px 14px;
}
.satun-shc-title {
  font-size: 15px;
  font-weight: 700;
  color: #f8fafc;
  margin-bottom: 4px;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.satun-shc-icon {
  display: inline-flex;
}
.satun-shc-meta {
  font-size: 11.5px;
  color: #94a3b8;
  margin-bottom: 6px;
}
.satun-shc-section {
  margin: 8px 0;
  padding: 8px 10px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid rgba(51, 65, 85, 0.7);
  color: #cbd5e1;
  font-size: 12.5px;
}
.satun-shc-coord {
  font-size: 11px;
  color: #7dd3fc;
  font-family: ui-monospace, monospace;
  margin-top: 6px;
}
.satun-shc-hint {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid rgba(51, 65, 85, 0.7);
  font-size: 11px;
  color: #64748b;
}
html[data-theme="light"] .satun-search-hover-card {
  background: linear-gradient(165deg, #fff, #f8fafc);
  border-color: #cbd5e1;
  color: #0f172a;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.12);
}
html[data-theme="light"] .satun-shc-title { color: #0f172a; }
html[data-theme="light"] .satun-shc-meta { color: #64748b; }
html[data-theme="light"] .satun-shc-section {
  background: #f8fafc;
  border-color: #e2e8f0;
  color: #334155;
}
html[data-theme="light"] .satun-shc-coord { color: #0369a1; }
html[data-theme="light"] .satun-shc-hint {
  border-top-color: #e2e8f0;
  color: #94a3b8;
}
@media (max-width: 640px) {
  .satun-search-hover-card {
    width: min(280px, calc(100vw - 16px));
  }
}
