:root {
    /* Brand palette — matches the public landing page (4-color brand system) */
    --navy-950: #251d3a;
    --navy-900: #251d3a;
    --navy-800: #2a2550;
    --navy-700: #2a2550;
    --gold-500: #ffcc00;
    --gold-400: #ffcc00;
    --gold-300: #ffcc00;

    --primary: var(--navy-900);
    --primary-hover: var(--navy-700);
    --accent: var(--gold-500);
    --accent-hover: var(--gold-400);
    --accent-text: var(--navy-900); /* dark text reads best on both the bright-yellow accent and its orange hover state */
    --sidebar-bg: linear-gradient(160deg, var(--navy-900), var(--navy-700));
    --sidebar-text: rgba(255, 255, 255, 0.75);
    --bg-light: #f3f4f6;
    --sidebar-width: 260px;
    --card-trend-up: #10b981; /* Emeral Green for increases */

    /* --- Theme tokens (light) --- */
    --bg-main: #f3f4f6;
    --bg-card: #ffffff;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
    --navbar-bg: #ffffff;
}

html[data-bs-theme="dark"] {
    --bg-main: #0b1526;
    --bg-card: #141d30;
    --text-main: #e2e8f0;
    --text-muted: #94a3b8;
    --border-color: #263045;
    --navbar-bg: #101a2c;
    --bg-light: #0b1526;
}

body {
    font-family: 'Plus Jakarta Sans', 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 0.95rem;
    line-height: 1.6;
    background-color: var(--bg-main);
    color: var(--text-main);
    overflow-x: hidden;
    transition: background-color 0.25s ease, color 0.25s ease;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

h1, h2, h3, h4, h5, h6 {
    font-family: inherit;
    font-weight: 700;
    letter-spacing: -0.01em;
}

/* --- Dark mode overrides for shared components --- */
html[data-bs-theme="dark"] #content { background-color: var(--bg-main); }

html[data-bs-theme="dark"] .top-navbar {
    background: var(--navbar-bg);
    box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
html[data-bs-theme="dark"] .top-navbar #pageTitle,
html[data-bs-theme="dark"] .top-navbar .text-dark { color: var(--text-main) !important; }

html[data-bs-theme="dark"] .card-custom,
html[data-bs-theme="dark"] .stat-card {
    background: var(--bg-card);
    box-shadow: 0 4px 6px -1px rgba(0,0,0,0.35);
}
html[data-bs-theme="dark"] .stat-card-number { color: var(--text-main); }
html[data-bs-theme="dark"] .stat-card-title,
html[data-bs-theme="dark"] .stat-card-smalltext { color: var(--text-muted); }

html[data-bs-theme="dark"] .table-custom th {
    background: var(--bg-card);
    color: var(--text-muted);
    border-bottom-color: var(--border-color);
}
html[data-bs-theme="dark"] .table-custom td { color: var(--text-main); }

html[data-bs-theme="dark"] .modal-content,
html[data-bs-theme="dark"] .dropdown-menu {
    background-color: var(--bg-card);
    color: var(--text-main);
}
html[data-bs-theme="dark"] .text-dark { color: var(--text-main) !important; }
html[data-bs-theme="dark"] .form-select,
html[data-bs-theme="dark"] .form-control {
    background-color: var(--bg-card);
    color: var(--text-main);
    border-color: var(--border-color);
}

/* --- App shell ---
   #wrapper owns one fixed viewport-height box; #sidebar and #content are its
   two panes and #content is the ONLY one that scrolls. This replaces the
   previous position:sticky sidebar, which was silently unreliable: body's
   `overflow-x: hidden` computes (per spec) to `overflow: hidden auto`,
   making body itself a scroll-container-establishing ancestor even though
   it never visibly scrolled — which is enough to make it (rather than the
   real viewport) the sticky sidebar's containing block, so "stuck"
   positioning could disagree with what was actually being scrolled. Giving
   #wrapper an explicit height + overflow:hidden and letting #content scroll
   internally sidesteps that whole class of bug and guarantees exactly one
   scrollbar, ever.

   Sidebar link colors/hover/active/collapse states live entirely in
   layout/sidebar.ejs's own <style> block now — this file only owns the
   shell's structural geometry (width, height, background, stacking), so
   there is a single place that decides what a nav link looks like. */
#wrapper {
    display: flex;
    width: 100%;
    align-items: stretch;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}
#sidebar {
    min-width: var(--sidebar-width);
    max-width: var(--sidebar-width);
    flex-shrink: 0;
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 1000;
}
#sidebar.active { margin-left: calc(var(--sidebar-width) * -1); }

/* --- Content & Navbar --- */
#content {
    width: 100%;
    height: 100%;
    min-width: 0;
    padding: 20px;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-gutter: stable;
    transition: margin 0.3s ease, opacity 0.18s ease-out, transform 0.18s ease-out;
    animation: pageFadeIn 0.3s ease-out;
}
/* Matches the 180ms setTimeout in main.js exactly, so the outgoing page never
   gets cut off mid-transition (a shorter JS delay than this duration is what
   produced the visible "jump" when navigating between pages).
   will-change lives ONLY on this transient class, not on #content itself:
   `will-change: transform` makes an element behave as if it had a transform,
   which creates a new containing block for any position:fixed descendant —
   and Bootstrap modals (like #adminLogsModal, rendered inside #content via
   the navbar partial) use position:fixed. A permanent will-change on #content
   was hijacking that, so modals opened clipped/mispositioned inside the
   scrolling #content box instead of the viewport, while the backdrop (which
   Bootstrap appends straight to <body>) still covered the full screen —
   looking like a stuck dark screen the modal couldn't be clicked through. */
#content.page-fade-out { opacity: 0; transform: translateY(8px); will-change: opacity, transform; }

@keyframes pageFadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}
/* Breaks out of #content's own 20px padding (negative margin on three sides)
   so the sticky bar sits flush against the real top/left/right edges of the
   content pane instead of floating as a rounded, shadowed card 20px down —
   that gap was what let scrolled-past content (e.g. the dashboard's hero
   card) visually poke out above/behind it mid-scroll. */
.top-navbar {
    background: #fff;
    border-radius: 0;
    margin: -20px -20px 20px -20px;
    padding: 14px 24px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.06);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 900;
}

/* --- UI Elements & Animations --- */
.card-custom { border: none; border-radius: 0.75rem; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05); }
.btn-primary { background-color: var(--primary); border-color: var(--primary); }
.btn-primary:hover { background-color: var(--primary-hover); border-color: var(--primary-hover); }
.table-custom th { background: #f9fafb; font-weight: 600; color: #374151; border-bottom: 2px solid #e5e7eb; }
.table-custom td { vertical-align: middle; }

/* Dashboard-Specific Stats Cards */
.stat-card {
    background: #fff;
    border-radius: 0.75rem;
    padding: 24px;
    display: flex;
    align-items: center;
    position: relative;
    overflow: hidden;
    cursor: default;
    box-shadow: 0 4px 6px -1px rgba(0,0,0,0.05);
    transition: all 0.2s ease-in-out;
    opacity: 0;
    transform: translateY(20px);
    animation: fadeInUp 0.5s ease forwards;
}

.stat-card.animate-delayed-1 { animation-delay: 0.1s; }
.stat-card.animate-delayed-2 { animation-delay: 0.2s; }
.stat-card.animate-delayed-3 { animation-delay: 0.3s; }

.stat-card:hover {
    transform: translateY(-3px) scale(1.01);
    box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);
}

.stat-card-accent {
    position: absolute;
    top: 0;
    left: 0;
    height: 4px;
    width: 0%;
    background: linear-gradient(90deg, var(--primary), var(--primary-hover));
    border-radius: 0.75rem 0.75rem 0 0;
    transition: width 0.4s ease-out;
}
.stat-card:hover .stat-card-accent { width: 100%; }

.stat-card-icon {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--navy-900);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--accent);
    font-size: 1.5rem;
    margin-right: 20px;
}

.stat-card-info { flex-grow: 1; }
.stat-card-title { font-size: 0.85rem; color: #6b7280; font-weight: 500; text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 2px; }
.stat-card-number { font-size: 2rem; font-weight: 700; color: #111827; margin: 0; }
.stat-card-smalltext { font-size: 0.8rem; color: #9ca3af; margin: 0; }

.stat-card-trend {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    color: var(--card-trend-up);
    font-size: 0.85rem;
    font-weight: 600;
}
.trend-arrow { font-size: 0.9rem; margin-bottom: 2px; }

/* Subtle Animated Background Line Graph */
.stat-card-bg-graph {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 60%;
    height: 40%;
    opacity: 0.03;
    background-image: url("data:image/svg+xml,%3Csvg width='241' height='100' viewBox='0 0 241 100' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M0 90C40 90 80 10 120 10C160 10 200 90 241 90V100H0V90Z' fill='%23251D3A'/%3E%3C/svg%3E");
    background-size: cover;
    background-position: bottom right;
    pointer-events: none;
    filter: blur(1px);
}

/* Specific Recent Logs Styling */
.recent-log-list {
    opacity: 0;
    transform: translateY(20px);
    animation: fadeInUp 0.5s ease 0.4s forwards;
}
.list-group-item.log-item { display: flex; align-items: center; }
.log-item-icon { width: 40px; height: 40px; }

@keyframes fadeInUp {
    to { opacity: 1; transform: translateY(0); }
}

/* Mobile */
@media (max-width: 768px) {
    /* Off-canvas drawer: fixed escapes #wrapper's overflow:hidden (no
       transform/filter on any ancestor, so fixed positioning isn't
       reclipped), and top/height are set explicitly since a fixed element
       no longer participates in #wrapper's flex sizing. */
    /* transition lives in layout/sidebar.ejs (one authoritative declaration
       covering min-width/max-width for desktop collapse AND margin-left for
       this drawer) so there's no cross-file cascade tie to resolve. */
    #sidebar {
        margin-left: calc(var(--sidebar-width) * -1);
        position: fixed;
        top: 0;
        left: 0;
        height: 100vh;
        height: 100dvh;
    }
    #sidebar.active { margin-left: 0; box-shadow: 8px 0 24px rgba(0, 0, 0, 0.25); }

    /* Dim/overlay behind the drawer, click to close (wired in main.js) */
    #sidebar-overlay {
        display: none;
        position: fixed; inset: 0;
        background: rgba(0, 0, 0, 0.45);
        z-index: 999;
    }
    #sidebar-overlay.active { display: block; animation: overlayIn 0.2s ease-out; }
    @keyframes overlayIn { from { opacity: 0; } to { opacity: 1; } }

    body.sidebar-open { overflow: hidden; }
}
/* --- Cluster Report Accordion Styles --- */
.report-section {
    opacity: 0;
    transform: translateY(20px);
    animation: fadeInUp 0.6s ease 0.3s forwards;
}

.accordion-custom .accordion-item {
    border: none;
    border-radius: 0.75rem;
    margin-bottom: 1rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
    overflow: hidden;
}

.accordion-custom .accordion-button {
    background-color: #fff;
    color: #111827;
    font-weight: 600;
    padding: 1.25rem 1.5rem;
    box-shadow: none !important;
    border-bottom: 1px solid #f3f4f6;
}

.accordion-custom .accordion-button:not(.collapsed) {
    background-color: #f8fafc;
    color: var(--primary);
}

.accordion-custom .accordion-button::after {
    filter: invert(30%) sepia(85%) saturate(1805%) hue-rotate(228deg) brightness(96%) contrast(94%); /* Colors the icon to primary */
}

.accordion-custom .accordion-button.collapsed::after {
    filter: none;
}

.table-report th {
    background-color: #f8fafc;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.05em;
    color: #6b7280;
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e5e7eb;
}

.table-report td {
    padding: 1rem 1.5rem;
    vertical-align: middle;
    color: #374151;
    font-size: 0.9rem;
    border-bottom: 1px solid #f3f4f6;
}

.table-report tbody tr:hover {
    background-color: #f8fafc;
}

.badge-outstanding {
    background-color: #d1fae5;
    color: #065f46;
    padding: 0.4em 0.8em;
    font-weight: 600;
    border-radius: 0.5rem;
}

.overall-rating-card {
    background: linear-gradient(135deg, var(--primary), var(--primary-hover));
    color: white;
    border-radius: 0.75rem;
    box-shadow: 0 10px 15px -3px rgba(37, 29, 58, 0.35);
}

/* --- Global UI/UX Consistency (placeholders, spacing) --- */
::placeholder {
    color: #94a3b8;
    opacity: 1;
    font-weight: 400;
    font-style: normal;
}

.form-label {
    font-weight: 600;
    font-size: 0.85rem;
    color: #374151;
    margin-bottom: 0.35rem;
}

.form-control, .form-select {
    border-radius: 0.5rem;
}

.container-fluid.px-4 {
    padding-top: 0.5rem;
    padding-bottom: 0.5rem;
}

.card-custom, .content-card, .records-card {
    margin-bottom: 1.5rem;
}

/* Archived record styling shared across management pages */
.archived-row { opacity: 0.55; background-color: #f8fafc; }
.badge-archived { background-color: #fee2e2; color: #b91c1c; font-weight: 600; font-size: 0.7rem; letter-spacing: 0.5px; }

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}