/* ==================== MAP CONTAINER ==================== */
.map-container {
    flex: 1;
    position: relative;
}

#map { height: 100%; width: 100%; }

/* ==================== TOP CONTROLS ==================== */
.top-controls {
    position: absolute;
    top: 24px;
    right: 24px;
    z-index: 100;
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.map-control {
    background: rgba(10, 14, 26, 0.95);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 212, 170, 0.4);
    border-radius: 10px;
    padding: 10px 16px;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-family: inherit;
}

.map-control:hover {
    background: rgba(0, 212, 170, 0.2);
    border-color: var(--primary);
    transform: translateY(-2px);
}

.map-control-icon {
    padding: 0;
    width: 40px;
    height: 40px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.map-control-var {
    padding: 0 10px;
    height: 40px;
    min-width: 52px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.mc-icon {
    width: 18px;
    height: 18px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex-shrink: 0;
    pointer-events: none;
}

.map-control-active {
    background: rgba(0, 212, 170, 0.15);
    border-color: var(--primary);
}

.map-control.epa-control { border-color: rgba(59, 130, 246, 0.4); }
.map-control.epa-control:hover {
    background: rgba(59, 130, 246, 0.2);
    border-color: var(--epa-blue);
}

/* ==================== REFERENCES PANEL ==================== */
.refs-panel {
    position: absolute;
    left: 24px;
    bottom: 84px;
    z-index: 100;
    background: rgba(10, 14, 26, 0.95);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 212, 170, 0.3);
    border-radius: 12px;
    min-width: 170px;
    overflow: hidden;
    display: none;
}
.refs-panel.refs-panel-visible { display: block; }

.refs-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 9px 14px;
    font-size: 11px;
    font-weight: 700;
    color: var(--primary);
    cursor: pointer;
    user-select: none;
}
.refs-header:hover { background: rgba(0, 212, 170, 0.06); }
.refs-toggle-icon { font-size: 10px; color: rgba(255,255,255,0.5); }

.refs-body {
    display: none;
    padding: 0 14px 12px;
}
.refs-body.open { display: block; }

.refs-section {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid rgba(255,255,255,0.08);
}
.refs-section:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.refs-section.refs-section-hidden { display: none; }

.refs-section-title {
    font-size: 10px;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 7px;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.refs-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 11px;
    color: var(--text-secondary);
    margin-bottom: 5px;
}
.refs-item:last-child { margin-bottom: 0; }

.refs-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}
.refs-line {
    width: 18px;
    height: 4px;
    border-radius: 2px;
    flex-shrink: 0;
}

/* ==================== GRIDVIEW ==================== */
.sensors-gridview-container {
    position: absolute;
    left: 16px;
    right: 16px;
    bottom: 16px;
    z-index: 101;
    background: linear-gradient(180deg, rgba(10, 14, 26, 0.98) 0%, rgba(20, 25, 35, 0.98) 100%);
    backdrop-filter: blur(25px);
    border: 1px solid rgba(0, 212, 170, 0.3);
    border-radius: 16px;
    overflow: hidden;
    transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    max-height: 350px;
}

.sensors-gridview-container.collapsed { max-height: 52px; }

.gridview-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    background: linear-gradient(135deg, rgba(0, 212, 170, 0.1) 0%, rgba(0, 168, 255, 0.1) 100%);
    border-bottom: 1px solid rgba(0, 212, 170, 0.2);
    cursor: pointer;
}

.gridview-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
    font-weight: 700;
    color: var(--primary);
}

.epa-count-badge {
    background: linear-gradient(135deg, var(--epa-blue-dark) 0%, var(--epa-blue) 100%);
    color: white;
    padding: 3px 8px;
    border-radius: 10px;
    font-size: 10px;
    font-weight: 600;
}

.gridview-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.action-button {
    background: linear-gradient(135deg, rgba(0, 212, 170, 0.1) 0%, rgba(0, 168, 255, 0.1) 100%);
    border: 1px solid rgba(0, 212, 170, 0.3);
    color: var(--primary);
    padding: 5px 10px;
    border-radius: 6px;
    font-size: 11px;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s ease;
}

.action-button:hover {
    background: rgba(0, 212, 170, 0.2);
    border-color: var(--primary);
}

.toggle-button {
    background: none;
    border: none;
    color: var(--primary);
    font-size: 18px;
    cursor: pointer;
    padding: 6px;
    transition: transform 0.3s ease;
}

.toggle-button.collapsed { transform: rotate(180deg); }

.gridview-content {
    max-height: 280px;
    overflow-y: auto;
}

.sensors-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.sensors-table th {
    background: linear-gradient(135deg, rgba(0, 212, 170, 0.15) 0%, rgba(0, 168, 255, 0.15) 100%);
    padding: 10px 12px;
    text-align: left;
    font-weight: 600;
    color: var(--primary);
    border-bottom: 1px solid rgba(0, 212, 170, 0.3);
    position: sticky;
    top: 0;
    cursor: pointer;
    white-space: nowrap;
}

.sensors-table td {
    padding: 10px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    white-space: nowrap;
}

.sensors-table tr { cursor: pointer; transition: all 0.2s ease; }

.sensors-table tr:hover {
    background: linear-gradient(135deg, rgba(0, 212, 170, 0.1) 0%, rgba(0, 168, 255, 0.1) 100%);
}

.sensors-table tr.epa-compliant {
    background: linear-gradient(135deg, rgba(30, 58, 138, 0.1) 0%, rgba(59, 130, 246, 0.1) 100%);
}

.sensor-name-cell { font-weight: 600; color: var(--primary); }
.sensor-name-cell.epa { color: #60a5fa; }

.spray-indicator {
    font-size: 11px;
    cursor: default;
    vertical-align: middle;
    filter: drop-shadow(0 0 3px rgba(0,168,255,0.7));
}

.sensors-table tr.sensor-row-offline { opacity: 0.6; }
.sensors-table tr.sensor-row-offline td { color: rgba(255,255,255,0.5); }
.sensor-offline-badge {
    display: inline-block;
    font-size: 9px;
    font-weight: 700;
    text-transform: uppercase;
    background: rgba(255, 107, 107, 0.2);
    color: #ff6b6b;
    border: 1px solid rgba(255, 107, 107, 0.4);
    border-radius: 3px;
    padding: 1px 4px;
    vertical-align: middle;
    letter-spacing: 0.5px;
}

.aqi-cell {
    font-weight: 800;
    padding: 4px 8px;
    border-radius: 6px;
    text-align: center;
    font-family: 'JetBrains Mono', monospace;
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
    margin-right: 6px;
}

.status-active  { background: var(--primary); }
.status-recent  { background: #fbbf24; }
.status-offline { background: #ff6b6b; }

/* ==================== SENSOR MODAL ==================== */
.sensor-modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    z-index: 1000;
    animation: fadeIn 0.2s ease;
}

.sensor-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1001;
    width: 400px;
    max-width: calc(100vw - 32px);
    max-height: 88vh;
    background: rgba(10, 14, 26, 0.98);
    border: 1px solid rgba(0, 212, 170, 0.4);
    border-radius: 18px;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
    display: flex;
    flex-direction: column;
    animation: modalSlideIn 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
    overflow: hidden;
}

@keyframes modalSlideIn {
    from { opacity: 0; transform: translate(-50%, -48%) scale(0.96); }
    to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

.sensor-modal-close {
    position: absolute;
    top: 10px;
    right: 12px;
    background: rgba(255,255,255,0.08);
    border: none;
    color: rgba(255,255,255,0.7);
    font-size: 14px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.sensor-modal-close:hover { background: rgba(255,255,255,0.18); color: white; }

.sensor-modal-body {
    overflow-y: auto;
    flex: 1;
    overscroll-behavior: contain;
}

.mapboxgl-popup-content {
    background: #1a1f2e !important;
    border-radius: 12px !important;
    padding: 0 !important;
    box-shadow: 0 8px 32px rgba(0,0,0,0.6) !important;
    border: 1px solid rgba(255,255,255,0.08) !important;
}
.mapboxgl-popup-tip { border-top-color: #1a1f2e !important; border-bottom-color: #1a1f2e !important; }
.mapboxgl-popup-close-button { color: rgba(255,255,255,0.5) !important; font-size: 18px; padding: 6px 10px; }
.mapboxgl-popup-close-button:hover { color: white !important; background: rgba(255,255,255,0.1) !important; border-radius: 0 12px 0 0; }

.popup-content { padding: 14px 16px; color: white; }

.popup-header {
    font-weight: 700;
    font-size: 16px;
    color: var(--primary);
    display: flex;
    align-items: center;
    gap: 8px;
    padding-bottom: 12px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    margin-bottom: 12px;
}

.popup-header.epa-compliant { color: #60a5fa; }

.popup-epa-badge {
    background: linear-gradient(135deg, var(--epa-blue-dark) 0%, var(--epa-blue) 100%);
    color: white;
    padding: 2px 8px;
    border-radius: 8px;
    font-size: 10px;
    font-weight: 600;
}

.popup-aqi-section {
    background: linear-gradient(135deg, rgba(30, 58, 138, 0.15) 0%, rgba(59, 130, 246, 0.15) 100%);
    border: 1px solid rgba(59, 130, 246, 0.3);
    border-radius: 10px;
    padding: 10px;
    margin-bottom: 10px;
    text-align: center;
}

.popup-aqi-value   { font-size: 28px; font-weight: 900; font-family: 'JetBrains Mono', monospace; }
.popup-aqi-category { font-size: 14px; font-weight: 700; text-transform: uppercase; margin-top: 4px; }
.popup-aqi-english { font-size: 10px; color: rgba(255,255,255,0.6); margin-top: 2px; }
.popup-main-pollutant { font-size: 11px; color: rgba(255,255,255,0.7); margin-top: 8px; padding-top: 8px; border-top: 1px solid rgba(255,255,255,0.1); }

.popup-epa-info { background: rgba(0,0,0,0.2); border-radius: 8px; padding: 10px; margin-bottom: 12px; font-size: 11px; }
.popup-epa-info-title { color: #60a5fa; font-weight: 600; margin-bottom: 6px; }
.popup-epa-recommendation { color: rgba(255,255,255,0.8); font-style: italic; line-height: 1.3; }

.popup-data { display: flex; justify-content: space-between; padding: 6px 0; font-size: 13px; border-bottom: 1px solid rgba(255,255,255,0.06); }
.popup-data-label { color: rgba(255,255,255,0.7); }
.popup-data-value { font-weight: 600; }

.popup-pm-section { margin-top: 12px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,0.15); }
.popup-pm-title { font-size: 12px; font-weight: 600; margin-bottom: 10px; color: var(--text-secondary); }
.popup-pm-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.popup-pm-item { background: rgba(0, 168, 255, 0.1); padding: 8px; border-radius: 8px; text-align: center; border: 1px solid rgba(0, 168, 255, 0.2); }
.popup-pm-item.epa-pollutant { background: rgba(59, 130, 246, 0.15); border-color: rgba(59, 130, 246, 0.3); }
.popup-pm-label { font-size: 10px; color: rgba(255,255,255,0.6); }
.popup-pm-value { font-size: 14px; font-weight: 700; color: var(--secondary); font-family: 'JetBrains Mono', monospace; }
.popup-pm-value.epa-pollutant { color: #60a5fa; }
.popup-pm-unit { font-size: 9px; color: rgba(255,255,255,0.5); }

.popup-footer { margin-top: 12px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,0.15); font-size: 11px; color: rgba(255,255,255,0.6); }
.popup-footer-row { display: flex; justify-content: space-between; margin-bottom: 4px; }

/* Cigarette equivalent */
.popup-cig-equiv {
    background: linear-gradient(135deg, rgba(255,107,107,0.12) 0%, rgba(255,159,67,0.12) 100%);
    border: 1px solid rgba(255,107,107,0.3);
    border-radius: 8px;
    padding: 7px 12px;
    font-size: 12px;
    color: rgba(255,255,255,0.85);
    margin-bottom: 10px;
    text-align: center;
}
.popup-cig-equiv strong { color: #ffb347; }
.popup-cig-source { font-size: 10px; color: rgba(255,255,255,0.4); }

/* History chart in popup */
.popup-history-section {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid rgba(255,255,255,0.12);
}

.popup-history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
}

.popup-period-btns { display: flex; gap: 4px; }

.popup-period-btn {
    background: rgba(255,255,255,0.07);
    border: 1px solid rgba(255,255,255,0.15);
    color: rgba(255,255,255,0.5);
    padding: 2px 8px;
    border-radius: 6px;
    font-size: 10px;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s ease;
}

.popup-period-btn:hover,
.popup-period-btn.active {
    background: rgba(0, 212, 170, 0.2);
    border-color: var(--primary);
    color: var(--primary);
}

.popup-chart-wrap {
    height: 110px;
    position: relative;
}

/* ==================== RESPONSIVE ==================== */
@media (max-width: 768px) {
    /* Top controls: horizontal scrollable strip */
    .top-controls {
        top: 16px;
        left: 72px;          /* clear the sidebar toggle button */
        right: 12px;
        flex-direction: row;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        gap: 6px;
        padding-bottom: 2px;
    }
    .top-controls::-webkit-scrollbar { display: none; }
    .map-control {
        padding: 8px 10px;
        font-size: 11px;
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Sensors gridview: add bottom safe area for mobile browsers */
    .sensors-gridview-container {
        left: 8px;
        right: 8px;
        bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    }
    .sensors-gridview-container.collapsed { max-height: 48px; }
    .gridview-header { padding: 12px 14px; }
    .gridview-title { font-size: 12px; }

    /* Reduce table cell padding on mobile */
    .sensors-table th,
    .sensors-table td { padding: 8px 8px; font-size: 11px; }

    /* Hide lower-priority table columns on mobile */
    .sensors-table th:nth-child(n+5),
    .sensors-table td:nth-child(n+5) { display: none; }

    /* Refs panel */
    .refs-panel { left: 8px; bottom: 68px; }

    /* Mapbox popup */
    .mapboxgl-popup-content { min-width: 260px !important; max-width: calc(100vw - 24px) !important; }
}

@media (max-width: 540px) {
    /* On small phones wrap the buttons into two rows instead of scrolling */
    .top-controls {
        flex-wrap: wrap;
        overflow-x: visible;
        gap: 4px;
    }
    .map-control {
        padding: 6px 8px;
        font-size: 10px;
        letter-spacing: 0;
        white-space: nowrap;
        flex-shrink: 0;
    }
}

@media (max-width: 480px) {
    /* Sensor modal becomes a bottom sheet on very small phones */
    .sensor-modal {
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        transform: none !important;
        width: 100%;
        max-width: 100%;
        max-height: 85vh;
        border-radius: 18px 18px 0 0;
        animation: modalSlideUp 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
    }
}

@keyframes modalSlideUp {
    from { opacity: 0; transform: translateY(40px); }
    to   { opacity: 1; transform: translateY(0); }
}
