/* ============================================================
   Genforce EMS - Application Stylesheet
   Corporate palette: #6AB0E3 (primary), #85868A (secondary), #000000 (dark)
   ============================================================ */

:root {
    --ems-primary:   #6AB0E3;
    --ems-secondary: #85868A;
    --ems-dark:      #000000;
    --ems-primary-dark: #4a90c3;
    --ems-primary-light: #a8d0f0;
    --ems-bg:        #f4f6f9;
    --ems-card-bg:   #ffffff;
    --ems-border:    #dee2e6;
    --ems-text:      #212529;
    --ems-muted:     #6c757d;
}

/* ---- Layout ---- */
html, body {
    height: 100%;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background-color: var(--ems-bg);
    color: var(--ems-text);
}

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

.ems-main {
    flex: 1;
}

/* ---- Navbar ---- */
.ems-navbar {
    background: linear-gradient(135deg, var(--ems-dark) 0%, #1a1a2e 60%, #1e3a5f 100%);
    border-bottom: 3px solid var(--ems-primary);
    padding: 0.5rem 1rem;
}

.ems-navbar .navbar-brand {
    font-weight: 700;
    font-size: 1.25rem;
    letter-spacing: 0.5px;
}

.navbar-logo {
    height: 36px;
    width: auto;
    max-width: 144px; /* 4:1 ratio at 36px height */
    object-fit: contain;
}

.ems-navbar .nav-link {
    color: rgba(255,255,255,0.85) !important;
    transition: color 0.2s;
}

.ems-navbar .nav-link:hover,
.ems-navbar .nav-link.active {
    color: var(--ems-primary) !important;
}

.ems-navbar .dropdown-menu {
    border: 1px solid rgba(106,176,227,0.3);
    background: #1a1a2e;
    z-index: 2000;
}

.ems-navbar .dropdown-item {
    color: rgba(255,255,255,0.85);
    transition: background 0.2s, color 0.2s;
}

.ems-navbar .dropdown-item:hover {
    background: var(--ems-primary);
    color: #fff;
}

.ems-navbar .dropdown-menu .dropdown-item-text.text-muted {
    color: #ffffff !important;
}

/* ---- Footer ---- */
.ems-footer {
    background: var(--ems-dark);
    border-top: 2px solid var(--ems-primary);
    color: #ffffff;
}

.ems-footer span.text-muted {
    color: #ffffff !important;
}

/* ---- Cards ---- */
.ems-card {
    background: var(--ems-card-bg);
    border: 1px solid var(--ems-border);
    border-radius: 0.5rem;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    transition: box-shadow 0.2s;
}

.ems-card:hover {
    box-shadow: 0 4px 16px rgba(106,176,227,0.2);
}

.ems-card .card-body {
    padding: 1.25rem;
}

.ems-card-header {
    background: var(--ems-primary);
    color: #fff;
    border-radius: 0.5rem 0.5rem 0 0 !important;
    padding: 0.75rem 1.25rem;
    font-weight: 600;
}

/* ---- Map View ---- */
#map-container {
    height: 100%;
    position: relative;
}

#main-map {
    height: 100%;
    width: 100%;
    z-index: 1;
}

.map-sidebar {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1000;
    width: 320px;
    max-height: calc(100vh - 120px);
    overflow-y: auto;
}

.map-sidebar .ems-card {
    margin-bottom: 10px;
}

/* ---- Device Markers ---- */
.device-marker-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 3px solid var(--ems-primary);
    background: var(--ems-dark);
    color: var(--ems-primary);
    font-weight: bold;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
    transition: all 0.2s;
}

.device-marker-icon.stale {
    border-color: var(--ems-secondary);
    color: var(--ems-secondary);
}

.device-marker-icon.high-load {
    border-color: #dc3545;
    color: #dc3545;
}

/* Leaflet popup customisation */
.leaflet-popup-content-wrapper {
    background: var(--ems-dark);
    color: #fff;
    border: 1px solid var(--ems-primary);
    border-radius: 0.5rem;
}

.leaflet-popup-tip {
    background: var(--ems-primary);
}

.leaflet-popup-content {
    margin: 12px 16px;
    min-width: 200px;
}

.device-popup-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--ems-primary);
    margin-bottom: 6px;
}

.device-popup-power {
    font-size: 1.5rem;
    font-weight: 700;
    color: #fff;
}

.device-popup-unit {
    font-size: 0.85rem;
    color: var(--ems-secondary);
}

.device-popup-link {
    display: block;
    margin-top: 10px;
    color: var(--ems-primary);
    text-decoration: none;
    font-size: 0.85rem;
}

.device-popup-link:hover {
    color: var(--ems-primary-light);
    text-decoration: underline;
}

/* ---- Live Reading Widgets ---- */
.reading-widget {
    background: var(--ems-card-bg);
    border: 1px solid var(--ems-border);
    border-radius: 0.5rem;
    padding: 1rem;
    text-align: center;
    transition: all 0.3s;
    position: relative;
    overflow: hidden;
}

.reading-widget::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: var(--ems-primary);
}

.reading-widget.phase-a::before { background: #6AB0E3; }
.reading-widget.phase-b::before { background: #f59e0b; }
.reading-widget.phase-c::before { background: #10b981; }

.reading-value {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--ems-dark);
    line-height: 1;
    margin-bottom: 0.25rem;
}

.reading-unit {
    font-size: 0.8rem;
    color: var(--ems-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.reading-label {
    font-size: 0.85rem;
    color: var(--ems-secondary);
    margin-top: 0.25rem;
    font-weight: 500;
}

.reading-widget.updating {
    animation: pulse-border 0.5s ease;
}

@keyframes pulse-border {
    0%   { border-color: var(--ems-primary); box-shadow: 0 0 0 0 rgba(106,176,227,0.4); }
    50%  { border-color: var(--ems-primary); box-shadow: 0 0 0 8px rgba(106,176,227,0); }
    100% { border-color: var(--ems-border);  box-shadow: none; }
}

/* Phase tabs */
.phase-tabs .nav-link {
    color: var(--ems-secondary);
    border-bottom: 2px solid transparent;
}

.phase-tabs .nav-link.active {
    color: var(--ems-primary);
    border-bottom-color: var(--ems-primary);
    background: transparent;
}

/* Phase indicators */
.phase-indicator {
    display: inline-block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-right: 4px;
}

.phase-a-indicator { background: #6AB0E3; }
.phase-b-indicator { background: #f59e0b; }
.phase-c-indicator { background: #10b981; }

/* ---- Status badges ---- */
.status-live {
    color: #28a745;
    font-size: 0.75rem;
}

.status-stale {
    color: var(--ems-secondary);
    font-size: 0.75rem;
}

.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 4px;
}

.status-dot.live {
    background: #28a745;
    animation: blink 1.5s infinite;
}

.status-dot.stale {
    background: var(--ems-secondary);
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.3; }
}

/* ---- Charts ---- */
.chart-container {
    position: relative;
    min-height: 280px;
    background: var(--ems-card-bg);
    border-radius: 0.5rem;
    padding: 1rem;
    border: 1px solid var(--ems-border);
}

.chart-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

/* ---- Tables ---- */
.ems-table {
    border-radius: 0.5rem;
    overflow: hidden;
}

.ems-table thead th {
    background: var(--ems-dark);
    color: var(--ems-primary);
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border: none;
    padding: 0.75rem 1rem;
}

.ems-table tbody tr:hover {
    background: rgba(106,176,227,0.05);
}

/* ---- Buttons ---- */
.btn-ems {
    background: var(--ems-primary);
    border-color: var(--ems-primary);
    color: #fff;
    font-weight: 500;
}

.btn-ems:hover {
    background: var(--ems-primary-dark);
    border-color: var(--ems-primary-dark);
    color: #fff;
}

.btn-ems-outline {
    background: transparent;
    border-color: var(--ems-primary);
    color: var(--ems-primary);
    font-weight: 500;
}

.btn-ems-outline:hover {
    background: var(--ems-primary);
    color: #fff;
}

/* ---- Login Page ---- */
.login-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--ems-dark) 0%, #1a1a2e 60%, #1e3a5f 100%);
}

.login-card {
    background: #fff;
    border-radius: 1rem;
    padding: 2.5rem;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.login-logo {
    max-width: 200px;
    height: auto;
    margin-bottom: 1.5rem;
}

.login-logo-icon {
    width: 80px;
    height: 64px; /* 5:4 ratio */
    object-fit: contain;
}

/* ---- Admin Panel ---- */
.admin-sidebar {
    min-height: calc(100vh - 80px);
    background: var(--ems-dark);
    border-right: 1px solid rgba(106,176,227,0.2);
    padding: 1rem 0;
}

.admin-sidebar .nav-link {
    color: rgba(255,255,255,0.7);
    padding: 0.6rem 1.25rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}

.admin-sidebar .nav-link:hover,
.admin-sidebar .nav-link.active {
    color: var(--ems-primary);
    background: rgba(106,176,227,0.1);
    border-left-color: var(--ems-primary);
}

.admin-sidebar .nav-link i {
    width: 20px;
    text-align: center;
}

/* ---- Device Detail ---- */
.device-header-banner {
    background: linear-gradient(135deg, var(--ems-dark), #1e3a5f);
    color: #fff;
    padding: 1.5rem;
    border-radius: 0.5rem;
    margin-bottom: 1.5rem;
    border-left: 5px solid var(--ems-primary);
}

.device-id-badge {
    font-size: 0.75rem;
    background: rgba(106,176,227,0.2);
    color: var(--ems-primary);
    border: 1px solid rgba(106,176,227,0.3);
    padding: 0.2rem 0.5rem;
    border-radius: 0.25rem;
    font-family: monospace;
}

/* ---- Totals Bar ---- */
.totals-bar {
    background: linear-gradient(135deg, #1e3a5f, var(--ems-dark));
    color: #fff;
    border-radius: 0.5rem;
    padding: 1rem 1.5rem;
    display: flex;
    gap: 2rem;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 1.5rem;
}

.total-item {
    text-align: center;
}

.total-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--ems-primary);
    line-height: 1;
}

.total-label {
    font-size: 0.75rem;
    color: var(--ems-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

/* ---- Responsive ---- */
@media (max-width: 768px) {
    #map-container {
        height: 60vh;
    }

    .map-sidebar {
        position: static;
        width: 100%;
        max-height: none;
        padding: 0.5rem;
    }

    .totals-bar {
        gap: 1rem;
    }

    .total-value {
        font-size: 1.5rem;
    }
}

/* ---- Utilities ---- */
.text-ems-primary { color: var(--ems-primary) !important; }
.text-ems-secondary { color: var(--ems-secondary) !important; }
.bg-ems-primary { background-color: var(--ems-primary) !important; }
.bg-ems-dark { background-color: var(--ems-dark) !important; }
.border-ems { border-color: var(--ems-primary) !important; }

/* Loading spinner overlay */
.loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    border-radius: inherit;
}

.spinner-ems {
    width: 3rem;
    height: 3rem;
    border: 4px solid var(--ems-border);
    border-top-color: var(--ems-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Refresh counter */
.refresh-badge {
    font-size: 0.7rem;
    color: var(--ems-secondary);
}

/* SAML login button */
.btn-saml {
    background: #0078d4;
    border-color: #0078d4;
    color: #fff;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    justify-content: center;
    font-weight: 500;
}

.btn-saml:hover {
    background: #005fa3;
    border-color: #005fa3;
    color: #fff;
}

/* Table actions */
.table-actions .btn {
    padding: 0.2rem 0.5rem;
    font-size: 0.8rem;
}