body, html { 
    margin: 0; 
    padding: 0; 
    height: 100%; 
    width: 100%; 
    font-family: 'Sarabun', sans-serif; 
    overflow: hidden; 
}

.leaflet-popup-content-wrapper, .leaflet-popup-tip {
    font-family: 'Sarabun', sans-serif;
}

#map { 
    height: 100%; 
    width: 100%; 
}

.leaflet-tile { 
    image-rendering: -webkit-optimize-contrast; 
}

.leaflet-top.leaflet-left {
    top: 65px;
}

.menu-btn {
    position: absolute;
    top: 15px;
    left: 15px;
    z-index: 1002;
    background: #ffffff;
    color: #0284c7;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 8px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    font-family: 'Sarabun', sans-serif;
}
.menu-btn:hover { background: #f0f9ff; }

.sidebar {
    position: absolute;
    top: 0;
    left: -260px;
    width: 250px;
    height: 100%;
    background: #ffffff;
    box-shadow: 4px 0 15px rgba(0,0,0,0.2);
    z-index: 2000;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    font-family: 'Sarabun', sans-serif;
}
.sidebar.open {
    left: 0;
}
.sidebar-header {
    padding: 15px;
    background: #0284c7;
    color: white;
    font-size: 14px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.sidebar-close {
    background: none;
    border: none;
    color: white;
    font-size: 16px;
    cursor: pointer;
}
.sidebar-content {
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow-y: auto;
    flex-grow: 1;
    min-height: 0;
}
.sidebar-section-title {
    font-size: 11px;
    font-weight: bold;
    color: #64748b;
    margin-top: 5px;
    margin-bottom: 2px;
}
.region-btn {
    background: #f1f5f9;
    color: #334155;
    border: none;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-family: 'Sarabun', sans-serif;
    text-align: left;
    font-weight: 500;
    transition: background 0.2s;
}
.region-btn:hover, .region-btn.active {
    background: #0284c7;
    color: white;
}

.sidebar-footer {
    padding: 12px 15px;
    background: #f8fafc;
    border-top: 1px solid #e2e8f0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.sidebar-footer-title {
    font-size: 11px;
    font-weight: bold;
    color: #94a3b8;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.brand-logos {
    display: flex;
    align-items: center;
    justify-content: space-around;
    gap: 12px;
}
.brand-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    color: #475569;
    font-size: 11px;
    font-weight: bold;
    text-align: center;
    cursor: default;
}
.brand-item img, .brand-item svg {
    height: 28px;
    width: auto;
    object-fit: contain;
}

.sidebar-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    z-index: 1999;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s;
}
.sidebar-overlay.show {
    opacity: 1;
    visibility: visible;
}

.locate-btn {
    position: absolute;
    bottom: 85px;
    right: 15px;
    z-index: 2001;
    background: #ffffff;
    color: #0284c7;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
}
.locate-btn:hover { background: #f0f9ff; }

.clouds-toggle-btn {
    position: absolute;
    bottom: 135px;
    right: 15px;
    z-index: 2001;
    background: #f1f5f9;
    color: #94a3b8;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s;
}
.clouds-toggle-btn.active {
    background: #ffffff;
    color: #0284c7;
}

.weather-toggle-btn {
    position: absolute;
    bottom: 185px;
    right: 15px;
    z-index: 2001;
    background: #f1f5f9;
    color: #94a3b8;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s;
}
.weather-toggle-btn.active {
    background: #ffffff;
    color: #0284c7;
}

.storm-btn-wrapper {
    position: absolute;
    bottom: 235px;
    right: 15px;
    z-index: 2001;
}

.storm-toggle-btn {
    background: #ffffff;
    color: #64748b;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    transition: all 0.2s;
}
.storm-toggle-btn.active {
    background: #fee2e2;
    color: #dc2626;
}

.storm-bubble {
    position: absolute;
    right: 50px;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(15, 23, 42, 0.9);
    color: #ffffff;
    padding: 6px 10px;
    border-radius: 6px;
    font-size: 12px;
    font-family: 'Sarabun', sans-serif;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0,0,0,0.25);
    display: none;
    pointer-events: none;
}
.storm-bubble::after {
    content: '';
    position: absolute;
    top: 50%;
    right: -5px;
    transform: translateY(-50%);
    border-width: 5px 0 5px 5px;
    border-style: solid;
    border-color: transparent transparent transparent rgba(15, 23, 42, 0.9);
}

.settings-toggle-btn {
    position: absolute;
    bottom: 335px;
    right: 15px;
    z-index: 2001;
    background: #f1f5f9;
    color: #94a3b8;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: all 0.2s;
}
.settings-toggle-btn.active {
    background: #ffffff;
    color: #0284c7;
}

.settings-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9998;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}
.settings-overlay.show {
    opacity: 1;
    visibility: visible;
}

.settings-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.9);
    background: #ffffff;
    width: 90%;
    max-width: 320px;
    border-radius: 12px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    z-index: 9999;
    font-family: 'Sarabun', sans-serif;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-sizing: border-box;
}
.settings-modal.show {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
    visibility: visible;
}
.settings-modal-header {
    padding: 12px 16px;
    background: #0284c7;
    color: white;
    font-size: 14px;
    font-weight: bold;
    border-top-left-radius: 12px;
    border-top-right-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.settings-close-btn {
    background: none;
    border: none;
    color: white;
    font-size: 16px;
    cursor: pointer;
}
.settings-modal-content {
    padding: 16px;
    font-size: 13px;
}
.settings-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #334155;
}

.legend-container {
    position: absolute;
    top: 15px;
    right: 15px;
    z-index: 1000;
    display: flex;
    align-items: flex-start;
    flex-direction: row-reverse;
}
.legend-toggle-btn {
    background: #f1f5f9;
    color: #94a3b8;
    border: none;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
    z-index: 1001;
    transition: all 0.2s;
}
.legend-toggle-btn.active {
    background: #ffffff;
    color: #0284c7;
}

.legend-box {
    position: absolute;
    right: 52px;
    top: 0;
    background: rgba(255, 255, 255, 0.95);
    padding: 10px 12px;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    font-size: 12px;
    font-family: 'Sarabun', sans-serif;
    max-height: 80vh;
    overflow-y: auto;
    width: 210px;
    box-sizing: border-box;
    transform-origin: top right;
    transform: scale(0);
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
.legend-box.show {
    transform: scale(1);
    opacity: 1;
    visibility: visible;
}
.legend-title {
    font-weight: bold;
    margin-bottom: 8px;
    color: #1e293b;
    font-size: 13px;
}
.legend-content {
    display: flex;
    gap: 10px;
    align-items: stretch;
}
.legend-bar {
    width: 10px;
    background: linear-gradient(to bottom, 
        #cccccc 0%, 
        #dcd6bc 14%, 
        #00b4d8 28%, 
        #03045e 42%, 
        #ffb703 57%, 
        #ae2012 71%, 
        #ff70a6 85%, 
        #ff477e 100%
    );
    border-radius: 5px;
    flex-shrink: 0;
}
.legend-items {
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-grow: 1;
}
.legend-item {
    display: flex;
    align-items: flex-start;
    gap: 6px;
}
.legend-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    margin-top: 3px;
    flex-shrink: 0;
}
.legend-text {
    line-height: 1.2;
    color: #334155;
}
.legend-text b {
    display: block;
    font-size: 12px;
    color: #0f172a;
}
.legend-text span {
    font-size: 10px;
    color: #64748b;
}

.time-control {
    position: absolute;
    bottom: 15px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    background: rgba(255, 255, 255, 0.95);
    padding: 8px 12px;
    border-radius: 8px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-family: 'Sarabun', sans-serif;
    width: 92%;
    max-width: 420px;
    box-sizing: border-box;
}
.time-control button {
    background: #0284c7;
    color: white;
    border: none;
    padding: 6px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
}
.time-control input[type=range] {
    cursor: pointer;
    flex-grow: 1;
}
.time-text-container {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    min-width: 125px;
    line-height: 1.2;
}
.time-scope {
    font-size: 10px;
    color: #475569;
    white-space: nowrap;
}
.time-date {
    font-size: 10px;
    color: #64748b;
    font-weight: bold;
}
.time-value {
    font-size: 13px;
    font-weight: bold;
    color: #0284c7;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}
.storm-icon-spin {
    animation: spin 3s linear infinite;
}

.device-marker-icon {
    background: #2563eb;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 3px solid #ffffff;
    box-shadow: 0 0 8px rgba(37, 99, 235, 0.8);
}
.measure-control { position: absolute; right: 15px; bottom: 335px; z-index: 2001; display: flex; flex-direction: row-reverse; align-items: center; gap: 8px; }
#measureBtn { width: 42px; height: 42px; border: 0; border-radius: 50%; background: white; color: #475569; box-shadow: 0 4px 10px rgba(0,0,0,.3); cursor: pointer; font-size: 18px; }
#measureBtn[aria-pressed="true"] { background: #fff; color: #0369a1; }
body .settings-toggle-btn { bottom: min(385px, calc(100% - 60px)); }
#measureBar { position: absolute; bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); width: max-content; max-width: 100%; box-sizing: border-box; text-align: center; padding: 5px 10px; background: white; color: #164e63; border-radius: 8px; font-size: 13px; box-shadow: 0 2px 6px rgba(0,0,0,.2); }
#map.measuring, #map.measuring .leaflet-grab { cursor: crosshair; }
#map.measuring .leaflet-interactive { pointer-events: none; }
@media (max-height: 520px) { .measure-control { right: 66px; bottom: 85px; } }
#measureBar:not([hidden]), #measureActions:not([hidden]) { display: flex; align-items: center; justify-content: center; gap: 5px; flex-wrap: wrap; }
#measureActions button { font: inherit; padding: 5px 8px; }
#measureActions button:disabled { background: #e2e8f0; color: #64748b; cursor: default; }
#lightningToggleBtn { position: fixed; right: 15px; bottom: 285px; z-index: 2001; width: 42px; height: 42px; padding: 0; border: 0; border-radius: 50%; box-shadow: 0 4px 10px rgba(0,0,0,.3); cursor: pointer; display: grid; place-items: center; }
body :is(.locate-btn, #cloudsToggleBtn, #weatherToggleBtn, #stormToggleBtn, #lightningToggleBtn, #measureBtn, #settingsToggleBtn, #legendToggleBtn) { background: #fff; }
#measureBtn[aria-pressed="true"] { color: #0369a1; }
body :is(#cloudsToggleBtn.active, #weatherToggleBtn.active, #stormToggleBtn.active, #measureBtn[aria-pressed="true"]) { outline: 2px solid #0284c7; outline-offset: -2px; }
@media (max-width: 768px) and (min-height: 581px) {
    :is(.locate-btn, .clouds-toggle-btn, .weather-toggle-btn, .storm-btn-wrapper, #lightningToggleBtn, .measure-control, .settings-toggle-btn) { translate: 0 -48px; }
}
@media (max-height: 580px) {
    body .settings-toggle-btn { bottom: 244px; right: 15px; }
    .measure-control { bottom: 194px; right: 15px; }
    .locate-btn { bottom: 144px; right: 15px; }
    .storm-btn-wrapper { bottom: 244px; right: 65px; }
    .weather-toggle-btn { bottom: 194px; right: 65px; }
    .clouds-toggle-btn { bottom: 144px; right: 65px; }
    #lightningToggleBtn { bottom: 194px; right: 115px; }
}
.sidebar-logo {
    display: block;
    width: min(64px, 12vh, 45%);
    height: auto;
    margin: 0 auto 2px;
}
.rainviewer-item i { color: #0284c7; }
#reloadData {
    position: absolute;
    top: 15px;
    right: 65px;
    z-index: 1001;
    width: 42px;
    height: 42px;
    border: 0;
    border-radius: 50%;
    background: white;
    color: #0284c7;
    box-shadow: 0 4px 10px #0004;
    cursor: pointer;
}
#dataLoading {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: grid;
    place-items: center;
    padding: 24px;
    box-sizing: border-box;
    color: #fff;
    background: radial-gradient(ellipse at 50% 38%, #1b4860 0%, #10283c 48%, #071725 100%);
    opacity: 1;
    visibility: visible;
    transition: opacity .65s ease, visibility .65s ease;
}
#dataLoading.is-leaving { opacity: 0; visibility: hidden; pointer-events: none; }
#dataLoading[hidden] { display: none; }
.loading-card { width: min(340px, 100%); display: flex; flex-direction: column; align-items: center; text-align: center; animation: loadingRise .7s cubic-bezier(.2,.8,.2,1) both; }
.loading-card img { width: clamp(112px, 28vw, 164px); height: auto; filter: drop-shadow(0 10px 28px #0008); animation: logoFloat 3.2s ease-in-out infinite; }
.loading-kicker { margin-top: 18px; color: #e3c47f; font: 600 10px/1.4 system-ui,sans-serif; letter-spacing: 2.7px; }
.loading-title { margin-top: 9px; color: #fff; font-size: 20px; font-weight: 700; }
.loading-detail { margin-top: 5px; color: #b6cad7; font-size: 13px; }
.loading-track { width: 100%; height: 5px; margin-top: 26px; overflow: hidden; border-radius: 8px; background: #ffffff24; }
.loading-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg,#1fc8f4,#efc665); box-shadow: 0 0 14px #1fc8f488; transition: width .35s ease; }
.loading-percent { margin-top: 10px; color: #f3d58e; font: 600 12px system-ui,sans-serif; font-variant-numeric: tabular-nums; }
@keyframes loadingRise { from { opacity: 0; transform: translateY(16px) scale(.98); } to { opacity: 1; transform: translateY(0) scale(1); } }
@keyframes logoFloat { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-5px); } }
@media (prefers-reduced-motion: reduce) { .loading-card,.loading-card img { animation: none; } #dataLoading,#loadingProgress { transition-duration: .01ms; } }
