/* ========================================
   NRW Stromnetz Dashboard – Styling
   Dark/Light Theme, responsive Layout
   ======================================== */

/* --- CSS Variablen (Themes) --- */

:root {
    --sidebar-width: 320px;
    --transition: 0.3s ease;
}

[data-theme="dark"] {
    --bg-primary: #1a1a2e;
    --bg-secondary: #16213e;
    --bg-card: #1e2a45;
    --bg-input: #253552;
    --text-primary: #e0e0e0;
    --text-secondary: #a0a8b8;
    --text-muted: #6b7280;
    --accent: #4fc3f7;
    --accent-hover: #29b6f6;
    --border: #2d3a54;
    --error: #ef5350;
    --success: #66bb6a;
    --shadow: rgba(0, 0, 0, 0.4);
}

[data-theme="light"] {
    --bg-primary: #f5f5f5;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --bg-input: #f0f0f0;
    --text-primary: #1a1a2e;
    --text-secondary: #4a4a6a;
    --text-muted: #9ca3af;
    --accent: #1976d2;
    --accent-hover: #1565c0;
    --border: #e0e0e0;
    --error: #d32f2f;
    --success: #388e3c;
    --shadow: rgba(0, 0, 0, 0.1);
}

/* --- Reset & Basis --- */

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
}

/* --- Layout --- */

.dashboard {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* --- Sidebar --- */

#sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    z-index: 1000;
    transition: transform var(--transition);
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.sidebar-header h1 {
    font-size: 18px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar-header .logo-icon {
    font-size: 24px;
}

#theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 10px;
    font-size: 18px;
    cursor: pointer;
    color: var(--text-primary);
    transition: background var(--transition);
}

#theme-toggle:hover {
    background: var(--bg-card);
}

/* --- Sidebar-Sektionen --- */

.sidebar-section {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.sidebar-section h2 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 12px;
}

/* --- Adresssuche --- */

.search-form {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.search-row {
    display: flex;
    gap: 6px;
}

.search-input {
    flex: 1;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 13px;
    outline: none;
    transition: border-color var(--transition);
}

.search-input:focus {
    border-color: var(--accent);
}

.search-input::placeholder {
    color: var(--text-muted);
}

#search-btn {
    padding: 10px 14px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 16px;
    cursor: pointer;
    transition: background var(--transition);
    flex-shrink: 0;
}

#search-btn:hover {
    background: var(--accent-hover);
}

#search-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

#search-error {
    display: none;
    color: var(--error);
    font-size: 12px;
}

.search-hints {
    font-size: 11px;
    color: var(--text-muted);
}

.search-history {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 6px;
    overflow: hidden;
}

.history-item {
    padding: 8px 12px;
    font-size: 12px;
    color: var(--text-primary);
    cursor: pointer;
    transition: background var(--transition);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-item:hover {
    background: var(--bg-input);
}

.history-item + .history-item {
    border-top: 1px solid var(--border);
}

/* --- Layer-Toggles --- */

.layer-toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 0;
    cursor: pointer;
    font-size: 13px;
    user-select: none;
}

.layer-toggle input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    cursor: pointer;
    flex-shrink: 0;
}

.layer-indicator {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    flex-shrink: 0;
}

.layer-label {
    flex: 1;
    color: var(--text-primary);
}

.layer-count {
    font-size: 11px;
    color: var(--text-muted);
    white-space: nowrap;
}

/* --- Refresh --- */

#refresh-btn {
    width: 100%;
    padding: 8px 16px;
    background: transparent;
    color: var(--accent);
    border: 1px solid var(--accent);
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    margin-bottom: 8px;
}

#refresh-btn:hover {
    background: var(--accent);
    color: #fff;
}

#refresh-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

#refresh-status {
    font-size: 11px;
    color: var(--text-muted);
}

.progress-bar {
    width: 100%;
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    overflow: hidden;
    margin-bottom: 8px;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    width: 0%;
    transition: width 0.4s ease;
}

.export-btn {
    width: 100%;
    padding: 8px 16px;
    margin-top: 8px;
    background: transparent;
    color: var(--success);
    border: 1px solid var(--success);
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
}

.export-btn:hover {
    background: var(--success);
    color: #fff;
}

.export-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* ding0 Abschnitt */
.section-hint {
    font-size: 11px;
    color: var(--text-secondary);
    margin: 0 0 8px 0;
    line-height: 1.4;
}

.ding0-options {
    margin-bottom: 8px;
}

.ding0-option {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: var(--text-secondary);
    cursor: pointer;
}

.ding0-option input[type="checkbox"] {
    margin: 0;
}

.refresh-status-text {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 4px;
    min-height: 16px;
}

/* ding0 Stat-Bar Farbe */
.stat-bar-fill[data-color="ding0"] {
    background: #FF9800;
}

.last-update {
    font-size: 12px;
    color: var(--text-secondary);
    margin-top: 4px;
}

.last-update span {
    color: var(--text-primary);
}

/* --- Statistiken --- */

.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
}

.stat-item {
    background: var(--bg-card);
    border-radius: 8px;
    padding: 12px;
    text-align: center;
}

.stat-value {
    font-size: 20px;
    font-weight: 700;
    color: var(--accent);
}

.stat-label {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 2px;
}

/* --- Statistik-Aufschluesselung --- */

.stat-breakdown {
    margin-top: 12px;
    padding: 10px;
    background: var(--bg-card);
    border-radius: 8px;
}

.stat-breakdown-title {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-breakdown-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
}

.stat-breakdown-row:last-child {
    margin-bottom: 0;
}

.stat-breakdown-label {
    font-size: 12px;
    color: var(--text-secondary);
    min-width: 70px;
    flex-shrink: 0;
}

.stat-breakdown-bar {
    flex: 1;
    height: 6px;
    background: var(--bg-input);
    border-radius: 3px;
    overflow: hidden;
}

.stat-bar-fill {
    display: block;
    height: 100%;
    border-radius: 3px;
    width: 0%;
    transition: width 0.6s ease;
}

/* Farben fuer Kategorien */
.stat-bar-fill[data-color="substations"] { background: #FFEB3B; }
.stat-bar-fill[data-color="lines"] { background: #AB47BC; }
.stat-bar-fill[data-color="plants"] { background: #76FF03; }

/* Farben fuer Spannungsebenen */
.stat-bar-fill[data-color="hv"] { background: #AB47BC; }
.stat-bar-fill[data-color="mv"] { background: #FF7043; }
.stat-bar-fill[data-color="lv"] { background: #66BB6A; }

.stat-breakdown-value {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-primary);
    min-width: 50px;
    text-align: right;
    flex-shrink: 0;
}

/* --- Karte --- */

#map {
    flex: 1;
    height: 100vh;
    z-index: 1;
}

/* --- Karten-Legende --- */

.map-legend {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 12px;
    line-height: 1.6;
    box-shadow: 0 2px 8px var(--shadow);
    max-height: 260px;
    overflow-y: auto;
}

.map-legend h4 {
    margin: 0 0 6px 0;
    font-size: 12px;
    font-weight: 700;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.legend-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 2px 0;
    color: var(--text-primary);
}

.legend-line {
    display: inline-block;
    width: 20px;
    border-radius: 1px;
    flex-shrink: 0;
}

.legend-dot {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
}

@media (max-width: 768px) {
    .map-legend {
        font-size: 11px;
        padding: 8px 10px;
    }
}

/* --- SVG Marker --- */

.svg-marker {
    background: none !important;
    border: none !important;
}

/* --- Leaflet Cluster Styling --- */

.marker-cluster-small {
    background-color: rgba(79, 195, 247, 0.4) !important;
}
.marker-cluster-small div {
    background-color: rgba(79, 195, 247, 0.7) !important;
    color: #fff !important;
    font-weight: 600;
}
.marker-cluster-medium {
    background-color: rgba(255, 152, 0, 0.4) !important;
}
.marker-cluster-medium div {
    background-color: rgba(255, 152, 0, 0.7) !important;
    color: #fff !important;
    font-weight: 600;
}
.marker-cluster-large {
    background-color: rgba(244, 67, 54, 0.4) !important;
}
.marker-cluster-large div {
    background-color: rgba(244, 67, 54, 0.7) !important;
    color: #fff !important;
    font-weight: 600;
}

/* --- Leaflet Popup Styling --- */

.feature-popup h3 {
    font-size: 14px;
    font-weight: 700;
    margin-bottom: 8px;
    color: #1a1a2e;
}

.popup-row {
    font-size: 12px;
    padding: 2px 0;
    color: #333;
}

.popup-label {
    font-weight: 600;
    color: #555;
}

.popup-value {
    color: #1a1a2e;
}

.popup-link {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid #eee;
}

.popup-link a {
    color: #1976d2;
    text-decoration: none;
    font-weight: 600;
}

.popup-link a:hover {
    text-decoration: underline;
}

/* Kapazitaets-Popup */

.popup-section {
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid #eee;
}

.popup-section-title {
    font-size: 11px;
    font-weight: 700;
    color: #555;
    margin-bottom: 4px;
    text-transform: uppercase;
}

.capacity-bar {
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    margin: 4px 0 6px;
    overflow: hidden;
}

.capacity-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
}

.popup-source {
    margin-top: 6px;
    font-size: 11px;
    color: #999;
}

.popup-notes {
    font-size: 11px;
    color: #888;
    margin-top: 4px;
}

.popup-edit-btn {
    margin-top: 8px;
    padding: 4px 12px;
    background: #1976d2;
    color: white;
    border: none;
    border-radius: 4px;
    font-size: 12px;
    cursor: pointer;
}

.popup-edit-btn:hover {
    background: #1565c0;
}

.popup-synthetic {
    font-size: 11px;
    color: #ff9800;
    font-style: italic;
}

.popup-synthetic {
    background: rgba(255, 152, 0, 0.15);
    color: #FF9800;
    font-size: 11px;
    padding: 2px 6px;
    border-radius: 3px;
    margin-bottom: 4px;
    font-style: italic;
}

/* --- Ladeindikator --- */

#loader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.loader-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.1);
    border-left-color: var(--accent, #4fc3f7);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 20px;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

#loader-text {
    color: #e0e0e0;
    font-size: 14px;
    text-align: center;
    max-width: 300px;
}

/* --- Verbindungsfehler --- */

#connection-error {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9998;
}

.error-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 32px;
    text-align: center;
    max-width: 360px;
}

.error-icon {
    font-size: 40px;
    margin-bottom: 12px;
}

.error-message {
    color: var(--text-primary);
    font-size: 14px;
    margin-bottom: 20px;
    white-space: pre-line;
    line-height: 1.6;
}

#retry-btn {
    padding: 10px 24px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background var(--transition);
}

#retry-btn:hover {
    background: var(--accent-hover);
}

#retry-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* --- Mobile Hamburger --- */

#sidebar-toggle {
    display: none;
    position: fixed;
    top: 12px;
    left: 12px;
    z-index: 2000;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 20px;
    cursor: pointer;
    color: var(--text-primary);
    line-height: 1;
}

/* --- Overlay (Mobile Sidebar-Hintergrund) --- */

#sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
}

#sidebar-overlay.active {
    display: block;
}

/* --- Responsive --- */

@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: 0;
        top: 0;
        height: 100vh;
        transform: translateX(-100%);
        box-shadow: 4px 0 20px var(--shadow);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    #sidebar-toggle {
        display: block;
    }

    #map {
        width: 100vw;
    }

    .sidebar-section {
        padding: 12px 16px;
    }
}

@media (max-width: 480px) {
    #sidebar {
        width: 280px;
        min-width: 280px;
    }

    .sidebar-header h1 {
        font-size: 16px;
    }

    .stats-grid {
        grid-template-columns: 1fr 1fr;
    }

    .stat-breakdown-label {
        min-width: 55px;
        font-size: 11px;
    }

    #sidebar-toggle {
        top: 8px;
        left: 8px;
        padding: 6px 10px;
        font-size: 18px;
    }
}

/* Modal */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-radius: 12px;
    width: 90%;
    max-width: 640px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    margin: 0;
    font-size: 18px;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 24px;
    cursor: pointer;
    padding: 0 4px;
}

.modal-close:hover {
    color: var(--text-primary);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
    font-size: 13px;
    line-height: 1.6;
}

.modal-body h3 {
    margin: 16px 0 8px;
    font-size: 14px;
    color: var(--accent);
}

.modal-body h3:first-child {
    margin-top: 0;
}

.modal-body ul {
    margin: 0;
    padding-left: 20px;
}

.modal-body li {
    margin-bottom: 4px;
}

.modal-body .tag {
    display: inline-block;
    padding: 2px 8px;
    margin: 2px;
    border-radius: 4px;
    background: var(--bg-tertiary);
    font-size: 12px;
}

/* Energiemix Chart */
#energiemix-container {
    text-align: center;
}

#energiemix-chart {
    max-height: 220px;
    margin-bottom: 8px;
}

#energiemix-summary {
    font-size: 13px;
    color: var(--text-secondary);
    line-height: 1.8;
}

.energiemix-total {
    font-size: 18px;
    font-weight: 700;
    color: var(--accent);
}

.energiemix-renewable {
    color: #4CAF50;
    font-weight: 600;
}

#energiemix-timestamp {
    font-size: 11px;
    color: var(--text-muted);
    margin-top: 6px;
}
