/* ============================================================
   Floodgate — Dark Theme UI
   ============================================================ */

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --bg-primary:    #0f1117;
    --bg-secondary:  #1a1d27;
    --bg-card:       #1e2230;
    --bg-card-hover: #252a3a;
    --bg-input:      #252a3a;
    --bg-sidebar:    #141720;

    --border:        #2a2f3e;
    --border-light:  #353b4e;

    --text-primary:  #e4e7f0;
    --text-secondary:#8b92a8;
    --text-muted:    #5c6378;

    --accent:        #22c55e;
    --accent-dim:    rgba(34, 197, 94, 0.15);
    --accent-hover:  #16a34a;

    --red:           #ef4444;
    --red-dim:       rgba(239, 68, 68, 0.15);
    --orange:        #f97316;
    --orange-dim:    rgba(249, 115, 22, 0.15);
    --yellow:        #eab308;
    --yellow-dim:    rgba(234, 179, 8, 0.15);
    --blue:          #3b82f6;
    --blue-dim:      rgba(59, 130, 246, 0.15);
    --purple:        #a855f7;

    --radius:        8px;
    --radius-lg:     12px;
    --shadow:        0 1px 3px rgba(0,0,0,0.4), 0 1px 2px rgba(0,0,0,0.3);
    --shadow-lg:     0 4px 12px rgba(0,0,0,0.5);

    --sidebar-width: 240px;
    --transition:    150ms ease;

    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    line-height: 1.6;
    color: var(--text-primary);
    background: var(--bg-primary);
}

body {
    display: flex;
    min-height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }

/* --- Sidebar --- */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform var(--transition);
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 20px 18px;
    border-bottom: 1px solid var(--border);
}

.sidebar-logo {
    width: 28px;
    height: 28px;
    color: var(--accent);
    flex-shrink: 0;
}

.sidebar-brand {
    font-size: 1.2rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: var(--text-primary);
}

.sidebar-nav {
    flex: 1;
    padding: 12px 10px;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.sidebar-footer {
    padding: 12px 10px;
    border-top: 1px solid var(--border);
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius);
    color: var(--text-secondary);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all var(--transition);
    text-decoration: none;
}
.nav-item svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
    opacity: 0.7;
}
.nav-item:hover {
    background: var(--bg-card);
    color: var(--text-primary);
}
.nav-item:hover svg { opacity: 1; }
.nav-item.active {
    background: var(--accent-dim);
    color: var(--accent);
}
.nav-item.active svg { opacity: 1; color: var(--accent); }

/* --- Mobile menu --- */
.mobile-menu-toggle {
    display: none;
    position: fixed;
    top: 14px;
    left: 14px;
    z-index: 200;
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-card);
    color: var(--text-primary);
    cursor: pointer;
    align-items: center;
    justify-content: center;
}
.mobile-menu-toggle svg { width: 20px; height: 20px; }

/* --- Main content --- */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    padding: 32px 40px;
    max-width: 1200px;
    width: 100%;
}

/* --- Typography --- */
h1 { font-size: 1.75rem; font-weight: 700; margin-bottom: 24px; letter-spacing: -0.02em; }
h2 { font-size: 1.25rem; font-weight: 600; margin: 28px 0 16px; letter-spacing: -0.01em; }
h3 { font-size: 1rem; font-weight: 600; margin: 20px 0 12px; color: var(--text-secondary); }
p { color: var(--text-secondary); margin-bottom: 12px; }

/* --- Cards --- */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow);
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
    margin-bottom: 24px;
}

.stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 20px 24px;
    box-shadow: var(--shadow);
    transition: border-color var(--transition);
}
.stat-card:hover { border-color: var(--border-light); }

.stat-label {
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    margin-bottom: 8px;
}
.stat-value {
    font-size: 1.75rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.2;
}
.stat-sub {
    font-size: 0.8rem;
    color: var(--text-secondary);
    margin-top: 6px;
}

.text-green  { color: var(--accent); }
.text-red    { color: var(--red); }
.text-yellow { color: var(--yellow); }
.text-blue   { color: var(--blue); }
.text-muted  { color: var(--text-muted); }

/* --- Badges --- */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 10px;
    border-radius: 100px;
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    line-height: 1.4;
}
.badge-green  { background: var(--accent-dim); color: var(--accent); }
.badge-red    { background: var(--red-dim);    color: var(--red); }
.badge-orange { background: var(--orange-dim); color: var(--orange); }
.badge-yellow { background: var(--yellow-dim); color: var(--yellow); }
.badge-blue   { background: var(--blue-dim);   color: var(--blue); }
.badge-muted  { background: var(--bg-input);   color: var(--text-muted); }

/* --- Tables --- */
.table-wrapper { overflow-x: auto; margin-bottom: 24px; }

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
}
thead th {
    text-align: left;
    padding: 10px 14px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
tbody td {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    color: var(--text-secondary);
    vertical-align: middle;
}
tbody tr { transition: background var(--transition); }
tbody tr:hover { background: var(--bg-card-hover); }
tbody tr:last-child td { border-bottom: none; }
tbody td a { font-weight: 500; }

/* --- Forms --- */
label {
    display: block;
    font-size: 0.82rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 6px;
    margin-top: 16px;
}
label:first-child { margin-top: 0; }

input[type="text"],
input[type="password"],
input[type="email"],
input[type="url"],
input[type="number"],
select,
textarea {
    width: 100%;
    padding: 10px 14px;
    background: var(--bg-input);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text-primary);
    font-size: 0.9rem;
    font-family: inherit;
    transition: border-color var(--transition);
    outline: none;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-dim);
}
input::placeholder, select::placeholder { color: var(--text-muted); }

/* Checkbox */
input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--accent);
    margin-right: 8px;
    vertical-align: middle;
}
label:has(input[type="checkbox"]) {
    display: flex;
    align-items: center;
    cursor: pointer;
    font-weight: 500;
}

small, .form-hint {
    display: block;
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-top: 4px;
}

fieldset {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin: 16px 0;
}
legend {
    font-weight: 600;
    font-size: 0.9rem;
    padding: 0 8px;
    color: var(--text-primary);
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 20px;
    border-radius: var(--radius);
    font-size: 0.88rem;
    font-weight: 600;
    font-family: inherit;
    border: 1px solid transparent;
    cursor: pointer;
    transition: all var(--transition);
    text-decoration: none;
    line-height: 1.2;
}

.btn-primary {
    background: var(--accent);
    color: #000;
    border-color: var(--accent);
}
.btn-primary:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    color: #000;
}

.btn-secondary {
    background: transparent;
    color: var(--text-secondary);
    border-color: var(--border);
}
.btn-secondary:hover {
    background: var(--bg-card);
    color: var(--text-primary);
    border-color: var(--border-light);
}

.btn-danger {
    background: transparent;
    color: var(--red);
    border-color: var(--border);
}
.btn-danger:hover {
    background: var(--red-dim);
    border-color: var(--red);
}

.btn-sm {
    padding: 5px 12px;
    font-size: 0.78rem;
}

.btn-group {
    display: flex;
    gap: 10px;
    margin-top: 20px;
    flex-wrap: wrap;
}

/* --- Decision Log --- */
.decision-log {
    max-height: 500px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--bg-card);
}

.decision-item {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
    transition: background var(--transition);
}
.decision-item:last-child { border-bottom: none; }
.decision-item:hover { background: var(--bg-card-hover); }
.decision-item .timestamp {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}
.decision-item .reason {
    font-weight: 500;
    color: var(--text-primary);
    margin: 4px 0;
}
.decision-item .details {
    font-size: 0.82rem;
    color: var(--text-secondary);
}
.decision-item .changes {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 4px;
}

/* --- Chart --- */
.chart-container {
    position: relative;
    width: 100%;
    height: 280px;
    margin: 16px 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 16px;
}

/* --- Chart tooltip --- */
.chart-tooltip {
    position: absolute;
    pointer-events: none;
    background: var(--bg-secondary);
    border: 1px solid var(--border-light);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: 0.78rem;
    line-height: 1.6;
    color: var(--text-primary);
    box-shadow: var(--shadow-lg);
    z-index: 50;
    white-space: nowrap;
    display: none;
}
.chart-tooltip-title {
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 4px;
    font-size: 0.72rem;
}
.chart-tooltip-row {
    display: flex;
    align-items: center;
    gap: 6px;
}
.chart-tooltip-swatch {
    width: 8px;
    height: 8px;
    border-radius: 2px;
    flex-shrink: 0;
}
.chart-crosshair {
    position: absolute;
    top: 0;
    width: 1px;
    height: 100%;
    background: var(--border-light);
    pointer-events: none;
    display: none;
}

/* --- Energy flow diagram --- */
.flow-particle {
    animation: flowMove var(--flow-duration, 2s) linear infinite;
    opacity: 0;
}
@keyframes flowMove {
    0%   { offset-distance: 0%;   opacity: 0; }
    5%   { opacity: 1; }
    95%  { opacity: 1; }
    100% { offset-distance: 100%; opacity: 0; }
}
.flow-path-inactive {
    stroke-dasharray: 6 4;
    opacity: 0.15;
}
.flow-label {
    font-family: inherit;
    font-variant-numeric: tabular-nums;
}

/* --- Chart legend --- */
.chart-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    padding: 8px 4px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}
.chart-legend-item {
    display: flex;
    align-items: center;
    gap: 6px;
}
.chart-legend-swatch {
    width: 12px;
    height: 12px;
    border-radius: 3px;
    flex-shrink: 0;
}

/* --- Flash messages --- */
.flash {
    padding: 12px 18px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: 0.88rem;
    font-weight: 500;
}
.flash-error {
    background: var(--red-dim);
    border: 1px solid var(--red);
    color: var(--red);
}
.flash-success {
    background: var(--accent-dim);
    border: 1px solid var(--accent);
    color: var(--accent);
}

/* --- Empty state --- */
.empty-state {
    text-align: center;
    padding: 48px 24px;
    color: var(--text-muted);
    border: 1px dashed var(--border);
    border-radius: var(--radius-lg);
}
.empty-state svg {
    width: 48px;
    height: 48px;
    margin-bottom: 12px;
    opacity: 0.4;
}
.empty-state p { margin-bottom: 16px; }

/* --- Header bar with title and action --- */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 24px;
    flex-wrap: wrap;
}
.page-header h1 { margin-bottom: 0; }

/* --- Detail grid (for inverter/meter detail pages) --- */
.detail-table {
    width: 100%;
    border-collapse: collapse;
}
.detail-table td {
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
}
.detail-table td:first-child {
    font-weight: 500;
    color: var(--text-muted);
    width: 140px;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.detail-table td:last-child {
    color: var(--text-primary);
}

/* --- Responsive --- */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: var(--shadow-lg);
    }
    .mobile-menu-toggle {
        display: flex;
    }
    .main-content {
        margin-left: 0;
        padding: 70px 18px 32px;
    }
    .card-grid {
        grid-template-columns: 1fr;
    }
    .page-header {
        flex-direction: column;
        align-items: flex-start;
    }
    .stat-value {
        font-size: 1.4rem;
    }
}

@media (max-width: 480px) {
    .main-content {
        padding: 60px 12px 24px;
    }
    h1 { font-size: 1.4rem; }
    .btn-group { flex-direction: column; }
    .btn { width: 100%; justify-content: center; }
}
