/* ==================== HISTORICAL OVERLAY ==================== */

.historical-overlay {
    position: fixed;
    inset: 0;
    z-index: 2000;
    background: rgba(10, 14, 26, 0.98);
    display: flex;
    flex-direction: column;
    animation: fadeIn 0.2s ease;
}

/* ---- Header ---- */
.hist-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border-bottom: 1px solid rgba(0, 212, 170, 0.25);
    flex-shrink: 0;
}

.hist-header-title {
    font-size: 18px;
    font-weight: 700;
    color: var(--primary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.hist-header-title span {
    font-size: 11px;
    font-weight: 400;
    color: rgba(255,255,255,0.4);
}

.hist-close {
    background: rgba(255,255,255,0.07);
    border: 1px solid rgba(255,255,255,0.12);
    color: rgba(255,255,255,0.7);
    width: 34px;
    height: 34px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.hist-close:hover { background: rgba(255,255,255,0.15); color: white; }

/* ---- Layout ---- */
.hist-body {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ---- Sidebar ---- */
.hist-sidebar {
    width: 220px;
    flex-shrink: 0;
    border-right: 1px solid rgba(255,255,255,0.08);
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.hist-section-title {
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--primary);
    margin-bottom: 8px;
}

.hist-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    margin-bottom: 8px;
}

.hist-field label {
    font-size: 10px;
    color: rgba(255,255,255,0.5);
    font-weight: 600;
}

.hist-field input[type="date"] {
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 8px;
    color: white;
    padding: 7px 10px;
    font-size: 12px;
    font-family: inherit;
    width: 100%;
    cursor: pointer;
    color-scheme: dark;
}
.hist-field input[type="date"]:focus { outline: none; border-color: var(--primary); }

.hist-select {
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 8px;
    color: white;
    padding: 7px 10px;
    font-size: 12px;
    font-family: inherit;
    width: 100%;
    cursor: pointer;
    color-scheme: dark;
}
.hist-select:focus { outline: none; border-color: var(--primary); }
.hist-select option { background: #1a1f2e; }

.hist-buffer-info {
    margin-top: 8px;
    font-size: 10px;
    color: rgba(255,255,255,0.35);
    line-height: 1.4;
}

.hist-btn {
    width: 100%;
    background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
    border: none;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #0a0e1a;
    font-weight: 700;
    font-size: 13px;
    padding: 10px;
    cursor: pointer;
    transition: opacity 0.2s;
    font-family: inherit;
}
.hist-btn:hover    { opacity: 0.88; }
.hist-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.hist-btn-secondary {
    width: 100%;
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.15);
    border-radius: 10px;
    color: rgba(255,255,255,0.75);
    font-size: 12px;
    padding: 8px;
    cursor: pointer;
    transition: background 0.2s;
    font-family: inherit;
}
.hist-btn-secondary:hover { background: rgba(255,255,255,0.12); }

.hist-divider {
    border: none;
    border-top: 1px solid rgba(255,255,255,0.08);
    margin: 0;
}

.ai-disclaimer {
    margin-top: 12px;
    padding: 10px 12px;
    background: rgba(251, 191, 36, 0.08);
    border: 1px solid rgba(251, 191, 36, 0.3);
    border-left: 3px solid #fbbf24;
    border-radius: 8px;
    font-size: 11px;
    color: rgba(251, 191, 36, 0.9);
    line-height: 1.5;
}

/* Step badge inside buttons */
.hist-step-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: rgba(255,255,255,0.25);
    font-size: 11px;
    font-weight: 800;
    line-height: 1;
    margin-right: 7px;
    flex-shrink: 0;
}
.hist-btn .hist-step-badge {
    background: rgba(10,14,26,0.35);
}
.hist-btn:disabled .hist-step-badge {
    background: rgba(255,255,255,0.15);
}

/* Climate history shortcut button in sidebar */
.hist-climate-link {
    width: 100%;
    background: transparent;
    border: 1px solid rgba(0, 212, 170, 0.35);
    border-radius: 10px;
    color: rgba(0, 212, 170, 0.85);
    font-size: 12.5px;
    font-weight: 500;
    padding: 9px 14px;
    text-align: left;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 8px;
}
.hist-climate-link:hover {
    background: rgba(0, 212, 170, 0.08);
    border-color: rgba(0, 212, 170, 0.6);
}

/* ---- Location picker button & display (climate sidebar) ---- */
.climate-pick-map-btn {
    width: 100%;
    background: linear-gradient(135deg, rgba(0, 212, 170, 0.15) 0%, rgba(0, 168, 255, 0.15) 100%);
    border: 1px solid rgba(0, 212, 170, 0.5);
    border-radius: 10px;
    color: var(--primary);
    font-size: 13px;
    font-weight: 600;
    padding: 10px 14px;
    cursor: pointer;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background 0.2s, border-color 0.2s;
    margin-bottom: 8px;
}
.climate-pick-map-btn:hover {
    background: linear-gradient(135deg, rgba(0, 212, 170, 0.25) 0%, rgba(0, 168, 255, 0.25) 100%);
    border-color: var(--primary);
}

.climate-loc-display {
    background: rgba(0, 212, 170, 0.07);
    border: 1px solid rgba(0, 212, 170, 0.2);
    border-radius: 8px;
    padding: 7px 10px;
    font-size: 11px;
    font-family: 'JetBrains Mono', monospace;
    color: rgba(255, 255, 255, 0.7);
    margin-bottom: 4px;
    text-align: center;
    min-height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ---- Location Picker Modal ---- */
.loc-picker-overlay {
    position: fixed;
    inset: 0;
    z-index: 3000;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.15s ease;
}

.loc-picker-modal {
    background: rgba(10, 14, 26, 0.99);
    border: 1px solid rgba(0, 212, 170, 0.4);
    border-radius: 18px;
    width: min(92vw, 620px);
    height: min(85vh, 580px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.7);
}

.loc-picker-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    flex-shrink: 0;
    font-size: 15px;
    font-weight: 700;
    color: var(--primary);
}

.loc-picker-search {
    display: flex;
    gap: 8px;
    padding: 10px 14px;
    flex-shrink: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.loc-picker-search-input {
    flex: 1;
    background: rgba(255, 255, 255, 0.06);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 8px;
    color: white;
    padding: 8px 12px;
    font-size: 13px;
    font-family: inherit;
    color-scheme: dark;
    outline: none;
    transition: border-color 0.2s;
}
.loc-picker-search-input:focus { border-color: var(--primary); }
.loc-picker-search-input::placeholder { color: rgba(255, 255, 255, 0.35); }

.loc-picker-search-btn {
    background: rgba(0, 212, 170, 0.15);
    border: 1px solid rgba(0, 212, 170, 0.4);
    color: var(--primary);
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    font-family: inherit;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: background 0.2s;
}
.loc-picker-search-btn:hover { background: rgba(0, 212, 170, 0.28); }

.loc-picker-results {
    max-height: 180px;
    overflow-y: auto;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    flex-shrink: 0;
}
.loc-picker-results.hidden { display: none; }

.loc-picker-result-item {
    padding: 10px 16px;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.75);
    cursor: pointer;
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
    transition: background 0.15s;
    line-height: 1.4;
}
.loc-picker-result-item:hover { background: rgba(0, 212, 170, 0.1); color: white; }
.loc-picker-result-item:last-child { border-bottom: none; }
.loc-picker-result-empty { color: rgba(255, 255, 255, 0.35); cursor: default; }
.loc-picker-result-empty:hover { background: none; }

.loc-picker-map-wrap {
    flex: 1;
    position: relative;
    min-height: 0;
}
#locPickerMap { width: 100%; height: 100%; }

.loc-picker-crosshair {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 10;
    color: var(--primary);
    filter: drop-shadow(0 0 6px rgba(0, 0, 0, 0.9)) drop-shadow(0 0 2px rgba(0, 212, 170, 0.5));
}

.loc-picker-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    flex-shrink: 0;
    gap: 12px;
}

.loc-picker-coords {
    font-size: 12px;
    font-family: 'JetBrains Mono', monospace;
    color: var(--primary);
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.loc-picker-footer-btns {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}
.loc-picker-footer-btns .hist-btn { padding: 8px 20px; font-size: 12px; }
.loc-picker-footer-btns .hist-btn-secondary { padding: 8px 14px; font-size: 12px; }

@media (max-width: 480px) {
    .loc-picker-modal { width: 100%; height: 100%; border-radius: 0; }
    .loc-picker-footer { flex-wrap: wrap; }
    .loc-picker-coords { flex: 0 0 100%; text-align: center; }
    .loc-picker-footer-btns { flex: 1; justify-content: flex-end; }
}

/* Variable checkboxes */
.hist-var-group { display: flex; flex-direction: column; gap: 6px; }

.hist-var-item {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
    color: rgba(255,255,255,0.75);
    cursor: pointer;
    padding: 3px 0;
}
.hist-var-item:hover { color: white; }

.hist-var-item input[type="checkbox"] {
    accent-color: var(--primary);
    width: 14px;
    height: 14px;
    cursor: pointer;
    flex-shrink: 0;
}

.hist-var-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* ---- Main area ---- */
.hist-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 20px 24px;
    gap: 12px;
}

.hist-status {
    font-size: 12px;
    color: rgba(255,255,255,0.5);
    min-height: 18px;
}
.hist-status.hist-status-ok    { color: var(--primary); }
.hist-status.hist-status-warn  { color: #fbbf24; }
.hist-status.hist-status-error { color: #ff6b6b; }

.hist-chart-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 0 6px;
    flex-shrink: 0;
}

.hist-chart-hint {
    font-size: 10px;
    color: rgba(255,255,255,0.3);
}

.hist-chart-wrap {
    flex: 1;
    position: relative;
    min-height: 0;
}

#historicalChart { width: 100% !important; height: 100% !important; }

/* Buffer count tag next to sensor name */
.hist-buf-tag {
    font-size: 9px;
    color: rgba(255,255,255,0.3);
    font-weight: 400;
    margin-left: 2px;
}

/* Source badge: OM = OpenMeteo full history, buf = session buffer only */
.hist-src-tag {
    font-size: 8px;
    font-weight: 700;
    padding: 1px 4px;
    border-radius: 3px;
    margin-left: auto;
    background: rgba(0, 212, 170, 0.18);
    color: #00d4aa;
    letter-spacing: 0.03em;
    flex-shrink: 0;
}
.hist-src-tag.buf {
    background: rgba(255, 204, 0, 0.15);
    color: #ffcc00;
}

/* Wind Rose section — absolute overlay on top-right of chart wrap */
.hist-windrose-section {
    position: absolute;
    top: 0;
    right: 0;
    width: 300px;
    bottom: 0;
    display: flex;
    flex-direction: column;
    padding: 12px 12px 12px 16px;
    background: rgba(10, 14, 26, 0.92);
    border-left: 1px solid rgba(255,255,255,0.1);
    border-radius: 0 0 0 12px;
    z-index: 10;
    transition: transform 0.25s ease;
}

.hist-windrose-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
    flex-shrink: 0;
}

.hist-windrose-toggle {
    background: rgba(255,255,255,0.08);
    border: 1px solid rgba(255,255,255,0.15);
    color: rgba(255,255,255,0.7);
    border-radius: 6px;
    width: 24px;
    height: 24px;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.hist-windrose-toggle:hover { background: rgba(255,255,255,0.18); color: white; }

/* Collapsed tab — small button on the right edge */
.hist-windrose-tab {
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    writing-mode: vertical-rl;
    text-orientation: mixed;
    background: rgba(10, 14, 26, 0.92);
    border: 1px solid rgba(0, 212, 170, 0.25);
    border-right: none;
    border-radius: 8px 0 0 8px;
    color: var(--primary);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 14px 7px;
    cursor: pointer;
    z-index: 10;
    transition: background 0.2s, color 0.2s;
}
.hist-windrose-tab:hover { background: rgba(0,212,170,0.15); color: white; }

.hist-windrose-section.hist-windrose-collapsed {
    transform: translateX(100%);
}

.hist-rose-plot {
    flex: 1;
    min-height: 0;
}


/* ==================== CLIMATE HISTORY ==================== */

.climate-roses-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
}

.climate-rose-card {
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 14px;
}

.climate-rose-plot {
    width: 100%;
    height: 280px;
}

.climate-rose-legend {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.climate-rose-legend-item {
    display: flex;
    align-items: center;
    gap: 7px;
    font-size: 10px;
    color: rgba(255,255,255,0.6);
}

.climate-rose-legend-dot {
    width: 10px;
    height: 10px;
    border-radius: 2px;
    flex-shrink: 0;
}

.climate-table-wrap {
    overflow-x: auto;
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 10px;
}

.climate-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 11px;
    font-family: 'JetBrains Mono', monospace;
}

.climate-table th,
.climate-table td {
    padding: 6px 10px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
    text-align: center;
    white-space: nowrap;
}

.climate-table th {
    background: rgba(0,212,170,0.1);
    color: var(--primary);
    font-weight: 700;
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    position: sticky;
    top: 0;
}

.climate-table td { color: rgba(255,255,255,0.75); }
.climate-table tr:last-child td { border-bottom: none; }
.climate-table tr:hover td { background: rgba(255,255,255,0.03); }

@media (max-width: 900px) {
    .climate-roses-grid { grid-template-columns: 1fr 1fr; }
}

/* ---- Responsive ---- */
@media (max-width: 900px) {
    /* Historical header: stack title and actions on small screens */
    .hist-header { padding: 12px 16px; gap: 8px; }
    .hist-header-title { font-size: 15px; }
    .hist-header-title span { display: none; }
}

@media (max-width: 640px) {
    .climate-roses-grid { grid-template-columns: 1fr; }

    /* Historical overlay: sidebar becomes a compact scrollable top strip.
       Keep overflow:hidden on hist-body so flex children get proper height
       constraints — without it the canvas height:100% resolves to 0 and
       the climate main area can't scroll. */
    .hist-body { flex-direction: column; overflow: hidden; }

    .hist-sidebar {
        width: 100%;
        flex-shrink: 0;          /* do not steal height from hist-main */
        border-right: none;
        border-bottom: 1px solid rgba(255,255,255,0.08);
        flex-direction: row;
        flex-wrap: wrap;
        padding: 10px 12px;
        gap: 8px;
        max-height: 200px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    /* Variable/config sections: two per row */
    .hist-sidebar > div { flex: 1 1 130px; min-width: 120px; }
    /* Dividers: full width */
    .hist-sidebar > .hist-divider { flex: 0 0 100%; min-width: 0; margin: 2px 0; }
    /* Action buttons: full width */
    .hist-sidebar > .hist-btn { flex: 0 0 100%; }
    .hist-sidebar > .hist-climate-link { flex: 1 1 calc(50% - 8px); min-width: 0; }
    /* Disclaimer: full width, smaller */
    .hist-sidebar > .ai-disclaimer {
        flex: 0 0 100%;
        font-size: 10px;
        padding: 7px 10px;
        margin-top: 0;
    }

    /* hist-main must be flex:1 with min-height:0 so the chart-wrap can fill it */
    .hist-main { flex: 1; min-height: 0; padding: 8px 12px; gap: 6px; overflow: hidden; }

    /* Give the chart a minimum height so it always renders */
    .hist-chart-wrap { min-height: 180px; }

    /* Hide wind rose completely on mobile — position:relative inside chart-wrap
       would collapse the canvas; the toggle tab is also hidden */
    .hist-windrose-section,
    .hist-windrose-tab { display: none !important; }

    /* Climate roses: single column already handled above */
    .climate-rose-plot { height: 200px; }

    /* Historical header actions: smaller */
    .hist-header > div { gap: 6px; }
    .hist-btn-secondary { font-size: 11px; padding: 6px 10px; }
}
