/* ============================================================
   Antique Heat Index Monitoring System — Design System
   Light Theme Default + Dark Mode Toggle
   ============================================================ */

/* --- CSS Custom Properties: Light Theme (Default) --- */
:root {
    --bg-primary: #F4F6F9;
    --bg-secondary: #FFFFFF;
    --bg-card: #FFFFFF;
    --bg-glass: rgba(0, 0, 0, 0.02);
    --bg-input: #F0F2F5;
    --border-color: #E0E4EA;
    --border-glass: rgba(0, 0, 0, 0.08);
    --text-primary: #1A1D23;
    --text-secondary: #4A5568;
    --text-muted: #8896A6;
    --accent: #0066CC;
    --accent-dim: rgba(0, 102, 204, 0.1);
    --accent-hover: #0052A3;
    --cat-normal: #22C55E;
    --cat-caution: #EAB308;
    --cat-extreme-caution: #F97316;
    --cat-danger: #EF4444;
    --cat-extreme-danger: #DC2626;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.1);
    --transition: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --header-bg: #FFFFFF;
    --header-border: #E0E4EA;
    --chart-grid: rgba(0,0,0,0.06);
    --chart-text: #4A5568;
    --popup-bg: #FFFFFF;
    --popup-shadow: 0 8px 30px rgba(0,0,0,0.15);
    --footer-bg: #1A1D23;
    --footer-text: #CBD5E1;
    --tile-url: 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png';
}

/* --- Dark Theme Override --- */
[data-theme="dark"] {
    --bg-primary: #0B0F19;
    --bg-secondary: #141824;
    --bg-card: rgba(20, 24, 36, 0.95);
    --bg-glass: rgba(255, 255, 255, 0.03);
    --bg-input: #1C2030;
    --border-color: rgba(255, 255, 255, 0.08);
    --border-glass: rgba(255, 255, 255, 0.06);
    --text-primary: #F1F5F9;
    --text-secondary: #94A3B8;
    --text-muted: #64748B;
    --accent: #38BDF8;
    --accent-dim: rgba(56, 189, 248, 0.12);
    --accent-hover: #7DD3FC;
    --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
    --header-bg: rgba(20, 24, 36, 0.92);
    --header-border: rgba(255,255,255,0.06);
    --chart-grid: rgba(255,255,255,0.06);
    --chart-text: #94A3B8;
    --popup-bg: rgba(20, 24, 36, 0.95);
    --popup-shadow: 0 8px 30px rgba(0,0,0,0.5);
    --footer-bg: #0B0F19;
    --footer-text: #94A3B8;
}

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
    font-family: var(--font);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    overflow-x: hidden;
    transition: background 0.4s ease, color 0.4s ease;
}

/* --- Loading Overlay --- */
.loading-overlay {
    position: fixed; inset: 0; z-index: 9999;
    background: var(--bg-primary);
    display: flex; align-items: center; justify-content: center;
    flex-direction: column; gap: 16px;
}
.loading-overlay h2 { font-weight: 600; color: var(--accent); }
.loading-overlay p { color: var(--text-secondary); font-size: 0.9rem; }
.loading-spinner {
    width: 48px; height: 48px;
    border: 4px solid var(--border-color);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
.loading-spinner-small {
    width: 20px; height: 20px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --- Header / Banner --- */
.app-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 28px;
    background: var(--header-bg);
    border-bottom: 1px solid var(--header-border);
    position: sticky; top: 0; z-index: 100;
    backdrop-filter: blur(12px);
    transition: background 0.4s ease, border-color 0.4s ease;
}
.header-left { display: flex; align-items: center; gap: 14px; }
.header-logo { display: flex; align-items: center; gap: 14px; }
.logo-seal { width: 48px; height: 48px; border-radius: 50%; object-fit: cover; border: 2px solid var(--border-color); }
.logo-text h1 { font-size: 1.2rem; font-weight: 700; letter-spacing: -0.02em; line-height: 1.2; color: var(--text-primary); }
.logo-subtitle { font-size: 0.78rem; color: var(--text-secondary); font-weight: 400; }
.header-right { display: flex; align-items: center; gap: 16px; }
.header-live { display: flex; align-items: center; gap: 6px; }
.live-dot { width: 10px; height: 10px; border-radius: 50%; background: #aaa; }
.live-dot.live-active { background: #22C55E; animation: pulse-live 2s ease-in-out infinite; }
@keyframes pulse-live {
    0%, 100% { box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.4); }
    50% { box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
}
.live-text { font-size: 0.72rem; font-weight: 700; color: #22C55E; letter-spacing: 0.1em; }
.header-meta { display: flex; gap: 14px; }
.meta-item { display: flex; flex-direction: column; align-items: flex-end; }
.meta-label { font-size: 0.62rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.meta-value { font-size: 0.82rem; font-weight: 600; color: var(--text-primary); }
.countdown { font-variant-numeric: tabular-nums; color: var(--accent) !important; }

/* --- Theme Toggle --- */
.theme-toggle {
    width: 44px; height: 24px; border-radius: 12px;
    background: var(--bg-input); border: 1px solid var(--border-color);
    position: relative; cursor: pointer; transition: var(--transition);
    flex-shrink: 0;
}
.theme-toggle:hover { border-color: var(--accent); }
.theme-toggle-knob {
    width: 18px; height: 18px; border-radius: 50%;
    background: var(--accent);
    position: absolute; top: 2px; left: 3px;
    transition: transform 0.3s ease;
    display: flex; align-items: center; justify-content: center;
    font-size: 0.6rem;
}
[data-theme="dark"] .theme-toggle-knob { transform: translateX(19px); }
.theme-toggle-label { font-size: 0.7rem; color: var(--text-muted); margin-right: 4px; white-space: nowrap; }

.refresh-indicator { display: none; }
.refresh-indicator.refreshing { display: block; }
.refresh-spinner { width: 20px; height: 20px; border: 2px solid var(--border-color); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }

/* --- Main Content --- */
.app-main { max-width: 1440px; margin: 0 auto; padding: 20px 28px 40px; }

/* --- Summary Stats Bar --- */
.summary-bar { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 16px; }
.stat-card {
    background: var(--bg-card); border: 1px solid var(--border-color);
    border-radius: var(--radius-md); padding: 18px 20px;
    display: flex; flex-direction: column; gap: 4px;
    transition: var(--transition); box-shadow: var(--shadow-sm);
}
.stat-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: var(--shadow-md); }
.stat-value { font-size: 1.5rem; font-weight: 700; line-height: 1.2; }
.stat-label { font-size: 0.73rem; color: var(--text-secondary); font-weight: 500; }

/* --- Distribution Bar --- */
.distribution-section { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.dist-label { font-size: 0.68rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; white-space: nowrap; }
.distribution-bar { flex: 1; display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--bg-input); }
.dist-segment {
    display: flex; align-items: center; justify-content: center;
    font-size: 0.6rem; font-weight: 700; color: #fff;
    transition: var(--transition);
}

/* --- Main Grid --- */
.main-grid { display: grid; grid-template-columns: 1fr 400px; gap: 20px; margin-bottom: 24px; }

/* --- Panel Header --- */
.panel-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.panel-title { font-size: 1.05rem; font-weight: 600; color: var(--text-primary); }

/* --- Map Panel --- */
.map-panel {
    background: var(--bg-card); border: 1px solid var(--border-color);
    border-radius: var(--radius-lg); padding: 16px;
    box-shadow: var(--shadow-sm); min-height: 520px;
}
.map-container { height: 490px; border-radius: var(--radius-md); overflow: hidden; }
.btn-reset {
    background: var(--bg-input); border: 1px solid var(--border-color);
    color: var(--text-secondary); padding: 6px 14px; border-radius: var(--radius-sm);
    font-family: var(--font); font-size: 0.78rem; cursor: pointer; transition: var(--transition);
}
.btn-reset:hover { background: var(--accent-dim); color: var(--accent); border-color: var(--accent); }

/* --- Map Legend --- */
.map-legend {
    background: var(--popup-bg) !important; backdrop-filter: blur(12px);
    border: 1px solid var(--border-color) !important;
    border-radius: var(--radius-md) !important; padding: 12px 16px !important;
    color: var(--text-primary); font-family: var(--font);
    box-shadow: var(--shadow-md);
}
.map-legend h4 { font-size: 0.85rem; margin-bottom: 8px; font-weight: 600; }
.legend-item { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.legend-color { width: 14px; height: 14px; border-radius: 3px; flex-shrink: 0; }
.legend-label { font-size: 0.72rem; color: var(--text-secondary); }

.hi-label { background: transparent !important; border: none !important; }
.hi-label-inner {
    padding: 2px 8px; border-radius: 6px;
    font-family: var(--font); font-size: 0.72rem; font-weight: 700;
    text-align: center; white-space: nowrap; box-shadow: var(--shadow-sm);
}

/* --- Map Popup --- */
.custom-popup .leaflet-popup-content-wrapper {
    background: var(--popup-bg) !important; backdrop-filter: blur(16px);
    border: 1px solid var(--border-color) !important;
    border-radius: var(--radius-md) !important; color: var(--text-primary);
    font-family: var(--font); box-shadow: var(--popup-shadow);
}
.custom-popup .leaflet-popup-tip { background: var(--popup-bg) !important; }
.map-popup { padding: 4px; }
.popup-title { font-size: 1rem; font-weight: 700; margin-bottom: 8px; }
.popup-badge { display: inline-block; padding: 4px 12px; border-radius: 20px; font-size: 0.75rem; font-weight: 600; margin-bottom: 10px; color: #fff; }
.popup-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.popup-stat { display: flex; flex-direction: column; }
.popup-stat-label { font-size: 0.65rem; color: var(--text-muted); text-transform: uppercase; }
.popup-stat-value { font-size: 0.95rem; font-weight: 600; }
.popup-forecast-btn {
    width: 100%; padding: 8px; border: 1px solid var(--accent);
    background: var(--accent-dim); color: var(--accent);
    border-radius: var(--radius-sm); font-family: var(--font);
    font-size: 0.8rem; font-weight: 600; cursor: pointer; transition: var(--transition);
}
.popup-forecast-btn:hover { background: var(--accent); color: #fff; }

/* --- Sidebar Panel --- */
.sidebar-panel { display: flex; flex-direction: column; gap: 12px; max-height: 580px; }

/* --- Filter Controls --- */
.filter-controls { display: flex; flex-direction: column; gap: 10px; }
.search-wrapper { position: relative; display: flex; align-items: center; }
.search-icon { position: absolute; left: 12px; font-size: 0.9rem; z-index: 1; }
.search-input {
    width: 100%; padding: 10px 12px 10px 36px;
    background: var(--bg-input); border: 1px solid var(--border-color);
    border-radius: var(--radius-sm); color: var(--text-primary);
    font-family: var(--font); font-size: 0.85rem;
    transition: var(--transition); outline: none;
}
.search-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-dim); }
.search-input::placeholder { color: var(--text-muted); }

.filter-row { display: flex; flex-direction: column; gap: 8px; }
.filter-buttons { display: flex; flex-wrap: wrap; gap: 6px; }
.filter-btn {
    padding: 5px 10px; border-radius: 20px; border: 1px solid var(--border-color);
    background: var(--bg-input); color: var(--text-secondary);
    font-family: var(--font); font-size: 0.7rem; font-weight: 500;
    cursor: pointer; transition: var(--transition); display: flex; align-items: center; gap: 4px;
}
.filter-btn:hover { border-color: var(--accent); color: var(--accent); }
.filter-btn.active { background: var(--accent-dim); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.filter-normal.active { background: rgba(34,197,94,0.1); border-color: var(--cat-normal); color: var(--cat-normal); }
.filter-caution.active { background: rgba(234,179,8,0.1); border-color: var(--cat-caution); color: #a16207; }
.filter-extreme-caution.active { background: rgba(249,115,22,0.1); border-color: var(--cat-extreme-caution); color: var(--cat-extreme-caution); }
.filter-danger.active { background: rgba(239,68,68,0.1); border-color: var(--cat-danger); color: var(--cat-danger); }
.filter-extreme-danger.active { background: rgba(220,38,38,0.1); border-color: var(--cat-extreme-danger); color: var(--cat-extreme-danger); }
.badge-count { background: rgba(0,0,0,0.06); padding: 1px 6px; border-radius: 10px; font-size: 0.65rem; font-weight: 700; }
[data-theme="dark"] .badge-count { background: rgba(255,255,255,0.08); }
.sort-select {
    padding: 7px 12px; background: var(--bg-input); border: 1px solid var(--border-color);
    border-radius: var(--radius-sm); color: var(--text-secondary);
    font-family: var(--font); font-size: 0.78rem; cursor: pointer; outline: none;
}

/* --- Municipality Cards --- */
.municipality-cards {
    display: flex; flex-direction: column; gap: 8px;
    overflow-y: auto; flex: 1; padding-right: 4px;
    scrollbar-width: thin; scrollbar-color: rgba(0,0,0,0.1) transparent;
}
[data-theme="dark"] .municipality-cards { scrollbar-color: rgba(255,255,255,0.08) transparent; }
.municipality-cards::-webkit-scrollbar { width: 5px; }
.municipality-cards::-webkit-scrollbar-track { background: transparent; }
.municipality-cards::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.1); border-radius: 3px; }

.muni-card {
    background: var(--bg-card); border: 1px solid var(--border-color);
    border-radius: var(--radius-md); padding: 14px;
    cursor: pointer; transition: var(--transition); box-shadow: var(--shadow-sm);
}
.muni-card:hover { border-color: var(--accent); transform: translateX(4px); box-shadow: var(--shadow-md); }
.muni-card.selected { border-color: var(--accent); background: var(--accent-dim); box-shadow: 0 0 0 1px var(--accent), var(--shadow-md); }
.muni-card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.muni-card-name { font-size: 0.88rem; font-weight: 600; }
.muni-card-badge { padding: 3px 10px; border-radius: 16px; font-size: 0.68rem; font-weight: 600; white-space: nowrap; color: #fff; }
.muni-card-body { display: flex; gap: 12px; }
.muni-stat { display: flex; align-items: center; gap: 6px; flex: 1; }
.muni-stat-icon { font-size: 0.9rem; }
.muni-stat-content { display: flex; flex-direction: column; }
.muni-stat-value { font-size: 0.85rem; font-weight: 600; }
.muni-stat-label { font-size: 0.6rem; color: var(--text-muted); text-transform: uppercase; }
.muni-card-footer { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--border-color); }
.muni-updated { font-size: 0.65rem; color: var(--text-muted); }

.skeleton-card {
    height: 100px; border-radius: var(--radius-md);
    background: linear-gradient(90deg, var(--bg-input) 25%, rgba(0,0,0,0.04) 50%, var(--bg-input) 75%);
    background-size: 200% 100%; animation: shimmer 1.5s ease-in-out infinite;
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.no-results { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.no-results-icon { font-size: 2rem; display: block; margin-bottom: 8px; }

/* --- Charts Section Grid --- */
.charts-grid { display: grid; grid-template-columns: 1fr; gap: 20px; margin-bottom: 24px; }

/* --- Forecast / Chart Sections --- */
.chart-section {
    background: var(--bg-card); border: 1px solid var(--border-color);
    border-radius: var(--radius-lg); padding: 20px; box-shadow: var(--shadow-sm);
}
.forecast-loading { display: flex; align-items: center; gap: 8px; color: var(--text-muted); font-size: 0.8rem; }
.forecast-summary { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.forecast-stat {
    background: var(--bg-input); border: 1px solid var(--border-color);
    border-radius: var(--radius-sm); padding: 14px;
    display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center;
}
.forecast-value { font-size: 1.2rem; font-weight: 700; }
.forecast-label { font-size: 0.68rem; color: var(--text-muted); }
.chart-container { height: 280px; position: relative; }
.chart-row { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

/* --- Recommendations Section --- */
.recommendations-section {
    background: var(--bg-card); border: 1px solid var(--border-color);
    border-radius: var(--radius-lg); padding: 20px; box-shadow: var(--shadow-sm);
    margin-bottom: 24px;
}
.recommendations-panel { min-height: 80px; }
.rec-placeholder { text-align: center; padding: 32px; color: var(--text-muted); }
.rec-placeholder-icon { font-size: 2rem; display: block; margin-bottom: 8px; }
.rec-header { padding: 16px; margin-bottom: 16px; background: var(--bg-input); border-radius: var(--radius-md); }
.rec-level { display: inline-block; padding: 4px 14px; border-radius: 20px; font-size: 0.78rem; font-weight: 700; margin-bottom: 8px; color: #fff; }
.rec-summary { font-size: 0.88rem; color: var(--text-secondary); line-height: 1.5; }
.rec-sections { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 16px; }
.rec-section { background: var(--bg-input); border: 1px solid var(--border-color); border-radius: var(--radius-md); padding: 16px; }
.rec-section-title { font-size: 0.88rem; font-weight: 600; margin-bottom: 10px; }
.rec-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.rec-list li { font-size: 0.82rem; color: var(--text-secondary); line-height: 1.5; padding-left: 16px; position: relative; }
.rec-list li::before { content: '›'; position: absolute; left: 0; color: var(--accent); font-weight: 700; }
.rec-firstaid { border-color: rgba(220,38,38,0.15); }
.rec-firstaid .rec-section-title { color: var(--cat-extreme-danger); }
.rec-animate { animation: fadeIn 0.4s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

/* --- Footer --- */
.app-footer {
    background: var(--footer-bg); border-top: 1px solid var(--border-color);
    padding: 24px 28px; transition: background 0.4s ease;
}
.footer-content {
    max-width: 1440px; margin: 0 auto;
    display: flex; justify-content: space-between; align-items: center;
}
.footer-content p { font-size: 0.8rem; color: var(--footer-text); }
.footer-sub { font-size: 0.7rem; color: var(--text-muted); margin-top: 2px; }
.footer-content a { color: var(--accent); text-decoration: none; }
.footer-content a:hover { text-decoration: underline; }
.footer-author { text-align: right; }
.footer-author p { line-height: 1.5; }

/* --- Error Toast --- */
.error-toast {
    position: fixed; bottom: 24px; right: 24px; z-index: 9000;
    background: rgba(220, 38, 38, 0.9); color: #fff;
    padding: 12px 20px; border-radius: var(--radius-sm);
    font-size: 0.85rem; font-weight: 500;
    animation: slideInUp 0.4s ease, fadeOut 0.4s ease 4.5s forwards;
    box-shadow: var(--shadow-lg);
}
@keyframes slideInUp { from { transform: translateY(20px); opacity: 0; } }
@keyframes fadeOut { to { opacity: 0; transform: translateY(10px); } }

/* --- Responsive --- */
@media (max-width: 1024px) {
    .main-grid { grid-template-columns: 1fr; }
    .sidebar-panel { max-height: none; }
    .municipality-cards { max-height: 400px; }
    .summary-bar { grid-template-columns: repeat(2, 1fr); }
    .forecast-summary { grid-template-columns: repeat(2, 1fr); }
    .chart-row { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
    .app-header { flex-direction: column; gap: 12px; align-items: flex-start; }
    .header-right { width: 100%; justify-content: space-between; flex-wrap: wrap; }
    .summary-bar { grid-template-columns: 1fr 1fr; }
    .forecast-summary { grid-template-columns: 1fr 1fr; }
    .map-container { height: 350px; }
    .chart-container { height: 220px; }
    .rec-sections { grid-template-columns: 1fr; }
    .footer-content { flex-direction: column; gap: 12px; text-align: center; }
    .footer-author { text-align: center; }
    .muni-card-body { flex-wrap: wrap; }
}
