:root {
    --bg-dark: #eef7f1;
    --bg-gradient-start: #f7fbff;
    --bg-gradient-end: #dcefe5;
    --panel-bg: rgba(255, 255, 255, 0.88);
    --panel-bg-hover: rgba(255, 255, 255, 0.97);
    --text-main: #1e293b;
    --text-muted: #475569;
    --text-bright: #0f172a;
    --accent-green: #059669;
    --accent-green-glow: rgba(16, 185, 129, 0.2);
    --accent-red: #dc2626;
    --accent-red-glow: rgba(239, 68, 68, 0.2);
    --accent-blue: #2563eb;
    --accent-blue-glow: rgba(59, 130, 246, 0.2);
    --accent-yellow: #d97706;
    --accent-purple: #7c3aed;
    --accent-purple-glow: rgba(139, 92, 246, 0.2);
    --border-color: rgba(0, 0, 0, 0.08);
    --border-glow: rgba(0, 0, 0, 0.15);
}

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

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
    background-image: 
        linear-gradient(135deg, rgba(255,255,255,0.92), rgba(236, 253, 245, 0.82)),
        radial-gradient(ellipse at 20% 0%, rgba(34, 197, 94, 0.12) 0%, transparent 42%),
        radial-gradient(ellipse at 85% 100%, rgba(37, 99, 235, 0.09) 0%, transparent 48%);
}

.glass-panel {
    background: var(--panel-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-color);
    border-radius: 10px;
    box-shadow: 
        0 14px 38px rgba(15, 23, 42, 0.08),
        inset 0 1px 0 rgba(255, 255, 255, 1);
    transition: border-color 0.3s ease;
}
.glass-panel:hover {
    border-color: var(--border-glow);
}

.dashboard-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    padding: 12px;
    gap: 12px;
}

/* ============ HEADER ============ */
.top-nav {
    min-height: 64px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 18px;
    background: rgba(255, 255, 255, 0.92);
    position: relative;
    z-index: 100;
    box-shadow: 0 12px 30px rgba(15,23,42,0.08);
}

.logo {
    display: flex;
    align-items: center;
    gap: 12px;
}
.logo-icon {
    font-size: 1.6rem;
    filter: drop-shadow(0 0 8px rgba(16, 185, 129, 0.5));
}
.logo-text {
    display: flex;
    flex-direction: column;
}
.logo h1 {
    font-size: 1.2rem;
    font-weight: 700;
    background: linear-gradient(135deg, #047857, #2563eb);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    line-height: 1.2;
}
.logo-subtitle {
    font-size: 0.65rem;
    color: var(--text-muted);
    letter-spacing: 1px;
    text-transform: uppercase;
}

.header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    font-size: 0.82rem;
}
.report-menu {
    position: relative;
}
.mobile-node-link,
.header-action-btn,
.report-menu-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 36px;
    padding: 0 12px;
    border-radius: 8px;
    border: 1px solid rgba(37, 99, 235, 0.18);
    background: rgba(37, 99, 235, 0.08);
    color: var(--accent-blue);
    text-decoration: none;
    font-weight: 700;
    font-family: inherit;
    font-size: 0.82rem;
    cursor: pointer;
}
.report-menu-trigger {
    border-color: rgba(5, 150, 105, 0.22);
    background: linear-gradient(135deg, rgba(5, 150, 105, 0.12), rgba(37, 99, 235, 0.1));
    color: var(--accent-green);
}
.report-menu-trigger[aria-expanded="true"] {
    border-color: rgba(37, 99, 235, 0.34);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}
.header-action-btn {
    color: var(--accent-green);
    background: rgba(5, 150, 105, 0.08);
    border-color: rgba(5, 150, 105, 0.2);
}
.settings-icon-btn {
    width: 36px;
    padding: 0;
    font-size: 1.05rem;
    line-height: 1;
}
.report-generator {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    width: 260px;
    display: grid;
    gap: 10px;
    padding: 12px;
    border: 1px solid rgba(5, 150, 105, 0.2);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 18px 42px rgba(15, 23, 42, 0.14);
    z-index: 1000;
}
.report-generator[hidden] {
    display: none;
}
.report-control {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 5px;
    color: var(--text-muted);
    font-size: 0.7rem;
    font-weight: 700;
}
.range-input-group {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: 7px;
    min-height: 32px;
}
.report-control input {
    height: 32px;
    border: 1px solid rgba(15, 23, 42, 0.1);
    border-radius: 8px;
    background: rgba(255,255,255,0.9);
    color: var(--text-main);
    font-family: inherit;
    font-size: 0.78rem;
    font-weight: 700;
    outline: none;
}
.report-control input {
    width: 100%;
    padding: 0 8px;
}
.report-control input:focus {
    border-color: rgba(37, 99, 235, 0.42);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}
.report-generate-btn {
    min-height: 32px;
    padding: 0 12px;
    border: 0;
    border-radius: 8px;
    background: linear-gradient(135deg, #047857, #2563eb);
    color: white;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.78rem;
    font-weight: 800;
    white-space: nowrap;
    box-shadow: 0 10px 20px rgba(37, 99, 235, 0.16);
}
.report-generate-btn:disabled {
    cursor: wait;
    opacity: 0.68;
}

@keyframes pulse {
    0% { transform: scale(0.9); opacity: 1; }
    50% { transform: scale(1.3); opacity: 0.6; }
    100% { transform: scale(0.9); opacity: 1; }
}

/* ============ MAIN LAYOUT ============ */
.main-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.55fr) 420px;
    grid-template-rows: minmax(0, 1fr) 230px;
    grid-template-areas:
        "map side"
        "analytics analytics";
    gap: 12px;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.map-column,
.side-column {
    display: flex;
    flex-direction: column;
    gap: 12px;
    overflow: hidden;
}
.map-column {
    grid-area: map;
}
.side-column {
    grid-area: side;
}
.analytics-row {
    grid-area: analytics;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(320px, 0.9fr);
    gap: 12px;
    min-height: 0;
    overflow: hidden;
}

.panel-header {
    padding: 11px 14px;
    border-bottom: 1px solid var(--border-color);
}
.panel-header h2 {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-bright);
}

/* ============ MAP ============ */
.map-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}
#map {
    flex-grow: 1;
    border-bottom-left-radius: 10px;
    border-bottom-right-radius: 10px;
    z-index: 1;
}

/* ============ NODES TABLE ============ */
.nodes-table-wrapper {
    flex-grow: 1;
    overflow: auto;
    padding: 6px 14px;
}
.nodes-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
}
.nodes-table th, .nodes-table td {
    padding: 8px 6px;
    text-align: left;
    border-bottom: 1px solid rgba(255,255,255,0.04);
}
.nodes-table th {
    color: var(--text-muted);
    font-weight: 500;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.node-badge {
    background: rgba(255,255,255,0.06);
    padding: 3px 8px;
    border-radius: 6px;
    font-size: 0.78rem;
}
.battery-bar {
    width: 50px;
    height: 5px;
    background: rgba(255,255,255,0.08);
    border-radius: 3px;
    display: inline-block;
    margin-right: 6px;
    overflow: hidden;
}
.battery-bar .fill { height: 100%; background: var(--accent-green); border-radius: 3px; }
.battery-bar .fill.warning { background: var(--accent-yellow); }
.status-ok { color: var(--accent-green); font-weight: 600; font-size: 0.78rem; }
.status-wait { color: var(--text-muted); font-style: italic; font-size: 0.78rem; }

/* ============ PIPELINE ============ */
.pipeline-container {
    flex-shrink: 0;
}
.pipeline-flow {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 14px 10px;
    flex-wrap: nowrap;
}
.pipeline-stage {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 8px 10px;
    border-radius: 10px;
    background: rgba(255,255,255,0.04);
    border: 1px solid rgba(255,255,255,0.06);
    transition: all 0.4s ease;
    min-width: 70px;
}
.pipeline-stage.active {
    border-color: var(--accent-green);
    background: rgba(16, 185, 129, 0.1);
    box-shadow: 0 0 15px var(--accent-green-glow);
}
.pipeline-stage.alert-active {
    border-color: var(--accent-red);
    background: rgba(239, 68, 68, 0.1);
    box-shadow: 0 0 15px var(--accent-red-glow);
}
.stage-icon { font-size: 1.2rem; }
.stage-label { font-size: 0.68rem; font-weight: 600; color: var(--text-bright); }
.stage-detail { font-size: 0.58rem; color: var(--text-muted); }

.pipeline-arrow {
    color: var(--text-muted);
    font-size: 1.1rem;
    font-weight: 300;
    opacity: 0.4;
}
.pipeline-stats {
    display: flex;
    justify-content: center;
    gap: 24px;
    padding: 6px 10px 10px;
    font-size: 0.75rem;
    color: var(--text-muted);
}
.stat-item strong {
    color: var(--text-bright);
}

/* ============ SPECTROGRAM ============ */
.spectrogram-container {
    flex: 0.7;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.spectrogram-wrapper {
    background: rgba(0, 0, 0, 0.3);
    border-radius: 0 0 14px 14px;
}

/* ============ ALERTS ============ */
.alerts-sidebar {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid rgba(220, 38, 38, 0.16);
}

.alert-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.badge {
    background: var(--accent-red);
    padding: 2px 10px;
    border-radius: 12px;
    font-size: 0.75rem;
    font-weight: 700;
    box-shadow: 0 0 10px var(--accent-red-glow);
}

.alert-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.alert-empty-state {
    text-align: center;
    color: var(--text-muted);
    padding: 20px;
    font-size: 0.85rem;
}

.alert-item {
    background: rgba(255, 255, 255, 0.02);
    border: 1px solid var(--border-color);
    border-left: 3px solid var(--accent-green);
    border-radius: 8px;
    padding: 8px 10px;
    animation: slideIn 0.3s ease-out;
    transition: background 0.2s;
}
.alert-item:hover {
    background: rgba(255, 255, 255, 0.04);
}
.alert-item.danger {
    border-left-color: var(--accent-red);
    background: rgba(239, 68, 68, 0.06);
    border-color: rgba(239, 68, 68, 0.2);
}
.alert-item.danger:hover {
    background: rgba(239, 68, 68, 0.1);
}

.alert-meta {
    display: flex;
    justify-content: space-between;
    font-size: 0.68rem;
    color: var(--text-muted);
    margin-bottom: 3px;
}
.alert-title {
    font-weight: 600;
    font-size: 0.82rem;
}
.alert-desc {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 2px;
}
.confidence-bar {
    height: 3px;
    background: rgba(255,255,255,0.1);
    border-radius: 2px;
    margin-top: 4px;
    overflow: hidden;
}
.confidence-fill {
    height: 100%;
    border-radius: 2px;
    transition: width 0.5s;
}
.confidence-high { background: var(--accent-green); }
.confidence-medium { background: var(--accent-yellow); }
.confidence-low { background: var(--accent-red); }

/* AI Explainability (Top-K) */
.topk-explain {
    font-size: 0.65rem;
    color: var(--accent-blue);
    margin-top: 4px;
    padding: 3px 6px;
    background: rgba(59, 130, 246, 0.08);
    border-radius: 4px;
    border: 1px solid rgba(59, 130, 246, 0.15);
    line-height: 1.4;
}
.alert-latency {
    font-size: 0.62rem;
    color: var(--accent-purple);
    margin-top: 2px;
    font-weight: 500;
}

/* ============ CHARTS ============ */
.chart-container {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}
.canvas-wrapper {
    flex-grow: 1;
    padding: 10px;
    position: relative;
    min-height: 0;
}

/* ============ ECONOMICS PANEL ============ */
.economics-panel {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}
.economics-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 10px;
    min-height: 0;
    flex: 1;
}
.econ-card {
    background: rgba(255,255,255,0.72);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 9px;
    text-align: center;
    transition: all 0.3s;
    min-width: 0;
}
.econ-card:hover {
    background: rgba(255,255,255,0.95);
    transform: translateY(-2px);
}
.econ-value {
    font-size: 1.14rem;
    font-weight: 700;
    line-height: 1.2;
}
.econ-value.green { color: var(--accent-green); text-shadow: 0 0 20px var(--accent-green-glow); }
.econ-value.blue { color: var(--accent-blue); text-shadow: 0 0 20px var(--accent-blue-glow); }
.econ-value.purple { color: var(--accent-purple); text-shadow: 0 0 20px var(--accent-purple-glow); }
.econ-value.yellow { color: var(--accent-yellow); }
.econ-label {
    font-size: 0.68rem;
    font-weight: 600;
    color: var(--text-main);
    margin-top: 2px;
}
.econ-detail {
    font-size: 0.58rem;
    color: var(--text-muted);
    margin-top: 1px;
}

/* ============ MODAL ============ */
.modal { 
    display: none; 
    position: fixed; 
    z-index: 10030; 
    left: 0; top: 0; 
    width: 100%; height: 100%; 
    background-color: rgba(15, 23, 42, 0.34);
    backdrop-filter: blur(4px);
}
.modal-content { 
    margin: 5vh auto; 
    padding: 0; 
    width: min(860px, calc(100vw - 32px)); 
    max-height: 90vh;
    overflow: hidden;
}
.report-modal {
    display: flex;
    flex-direction: column;
}
.report-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 18px;
    border-bottom: 1px solid var(--border-color);
}
.report-modal-header h2 {
    font-size: 1.1rem;
    color: var(--text-bright);
}
.report-meta {
    margin-top: 4px;
    color: var(--text-muted);
    font-size: 0.78rem;
}
.report-modal-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}
.report-modal-actions button,
.close-btn {
    min-height: 32px;
    padding: 0 11px;
    border-radius: 8px;
    border: 1px solid rgba(15, 23, 42, 0.1);
    background: rgba(255,255,255,0.76);
    color: var(--text-main);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.76rem;
    font-weight: 800;
}
.report-modal-actions button:hover,
.close-btn:hover {
    border-color: rgba(37, 99, 235, 0.24);
    color: var(--accent-blue);
}
.report-text {
    min-height: 320px;
    padding: 18px;
    overflow-y: auto;
    background: linear-gradient(180deg, rgba(248, 250, 252, 0.76), rgba(255,255,255,0.94));
    color: var(--text-main);
    font-size: 0.9rem;
    line-height: 1.65;
    white-space: pre-wrap;
    word-break: break-word;
}
.instructions-modal {
    display: flex;
    flex-direction: column;
    width: min(720px, calc(100vw - 32px));
}
.instructions-content {
    padding: 18px;
    background: linear-gradient(180deg, rgba(248, 250, 252, 0.76), rgba(255,255,255,0.94));
    overflow-y: auto;
}
.instruction-steps {
    display: grid;
    gap: 12px;
    margin-left: 18px;
    color: var(--text-main);
    font-size: 0.88rem;
    line-height: 1.55;
}
.instruction-steps li {
    padding-left: 4px;
}
.instruction-steps strong {
    color: var(--text-bright);
}
.instruction-note {
    margin-top: 16px;
    padding: 12px;
    border: 1px solid rgba(37, 99, 235, 0.16);
    border-radius: 8px;
    background: rgba(219, 234, 254, 0.6);
    color: var(--text-muted);
    font-size: 0.82rem;
    line-height: 1.5;
}

/* ============ SETTINGS DRAWER ============ */
.settings-drawer {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 10020;
}
.settings-drawer.open {
    display: block;
}
.settings-backdrop {
    position: absolute;
    inset: 0;
    border: 0;
    background: rgba(15, 23, 42, 0.28);
    backdrop-filter: blur(2px);
    cursor: pointer;
}
.settings-panel {
    position: absolute;
    top: 12px;
    right: 12px;
    bottom: 12px;
    width: min(560px, calc(100vw - 24px));
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 18px;
    border-bottom: 1px solid var(--border-color);
}
.settings-header h2 {
    font-size: 1.05rem;
    color: var(--text-bright);
}
.settings-header p {
    margin-top: 4px;
    color: var(--text-muted);
    font-size: 0.78rem;
}
.settings-close {
    min-height: 34px;
    padding: 0 12px;
    border-radius: 8px;
    border: 1px solid rgba(15, 23, 42, 0.12);
    background: rgba(255,255,255,0.72);
    color: var(--text-main);
    cursor: pointer;
    font-family: inherit;
    font-weight: 700;
}
.settings-section {
    padding: 14px;
    overflow: auto;
}
.settings-section-title {
    margin-bottom: 12px;
    color: var(--text-bright);
    font-size: 0.86rem;
    font-weight: 700;
}
.settings-section .nodes-table-wrapper {
    padding: 0;
    overflow: auto;
}
.settings-section .nodes-table th,
.settings-section .nodes-table td {
    border-bottom-color: rgba(15, 23, 42, 0.08);
}
.settings-section .node-badge {
    background: rgba(5, 150, 105, 0.08);
    color: #047857;
}
.settings-section .battery-bar {
    background: rgba(15, 23, 42, 0.08);
}

/* ============ SCROLLBAR ============ */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.25); }

/* ============ LEAFLET THEME ============ */
.leaflet-container { background: #dbeafe; }
.leaflet-tile { filter: none; }
.leaflet-popup-content-wrapper { 
    background: var(--panel-bg); 
    color: var(--text-main); 
    backdrop-filter: blur(12px); 
    border: 1px solid var(--border-color); 
    border-radius: 10px;
}
.leaflet-popup-tip { background: var(--panel-bg); }

.blinking-marker {
    width: 20px; height: 20px;
    background-color: var(--accent-red);
    border-radius: 50%;
    animation: blinker 1s linear infinite;
    box-shadow: 0 0 20px var(--accent-red-glow);
}

@keyframes blinker { 50% { opacity: 0; transform: scale(0.5); } }
@keyframes slideIn { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* ============ BUTTONS ============ */
.btn-primary {
    background: var(--accent-blue);
    color: white;
    border: none;
    padding: 6px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    transition: all 0.2s;
    font-family: inherit;
}
.btn-primary:hover {
    filter: brightness(1.15);
    transform: translateY(-1px);
}
.btn-danger {
    background: var(--accent-red);
    color: white;
    border: none;
    padding: 6px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    font-family: inherit;
}
.glass-select {
    background: rgba(255, 255, 255, 0.08);
    color: white;
    border: 1px solid var(--border-color);
    padding: 6px 10px;
    border-radius: 6px;
    outline: none;
    font-family: inherit;
}
.glass-select option {
    background: var(--bg-dark);
}

/* ============ RESPONSIVE ============ */
@media (max-width: 1400px) {
    .main-grid {
        grid-template-columns: minmax(0, 1fr) 380px;
        grid-template-rows: minmax(0, 1fr) 240px;
    }
}

@media (max-width: 900px) {
    .main-grid {
        grid-template-columns: 1fr;
        grid-template-rows: auto auto auto;
        grid-template-areas:
            "map"
            "side"
            "analytics";
        overflow-y: auto;
    }
    body { overflow: auto; }
    .dashboard-container { height: auto; min-height: 100vh; }
    .top-nav { height: auto; flex-wrap: wrap; padding: 10px; gap: 8px; }
    .header-actions {
        width: 100%;
        justify-content: flex-end;
        gap: 8px;
    }
    .report-menu {
        flex: 1;
    }
    .report-menu-trigger {
        width: 100%;
    }
    .report-generator {
        left: 0;
        right: auto;
        width: min(320px, calc(100vw - 24px));
    }
    .map-column { min-height: 520px; }
    .side-column { overflow: visible; }
    .alerts-sidebar { min-height: 320px; }
    .analytics-row {
        grid-template-columns: 1fr;
        overflow: visible;
    }
    .chart-container,
    .economics-panel {
        min-height: 230px;
    }
    .report-modal-header {
        align-items: stretch;
        flex-direction: column;
    }
    .report-modal-actions {
        justify-content: flex-end;
        flex-wrap: wrap;
    }
    .settings-panel {
        top: 8px;
        right: 8px;
        bottom: 8px;
        width: calc(100vw - 16px);
    }
}
