/* ===== CSS VARIABLES FOR LAYOUT ===== */
:root {
    --sidebar-width: 260px;
    --sidebar-collapsed-width: 72px;
    --navbar-height: 64px;
    --transition-speed: 0.25s;
    --transition-timing: cubic-bezier(0.4, 0, 0.2, 1);
}

/* ===== FOUC PREVENTION ON LOAD ===== */
html.sidebar-collapsed-init .sidebar {
    width: var(--sidebar-collapsed-width) !important;
    transition: none !important;
}
html.sidebar-collapsed-init .sidebar .nav-group-label,
html.sidebar-collapsed-init .sidebar .nav-link span,
html.sidebar-collapsed-init .sidebar .sidebar-text,
html.sidebar-collapsed-init .sidebar .sidebar-chevron {
    display: none !important;
}
html.sidebar-collapsed-init .sidebar .sidebar-header {
    justify-content: center !important;
}
html.sidebar-collapsed-init .sidebar .sidebar-toggle .fa-chevron-left {
    transform: rotate(180deg) !important;
}
html.sidebar-collapsed-init .sidebar .nav-link {
    justify-content: center !important;
    padding: 0.625rem !important;
    margin: 0.125rem 0.375rem !important;
}
html.sidebar-collapsed-init .main-content {
    margin-left: var(--sidebar-collapsed-width) !important;
    transition: none !important;
}

/* ===== TOP PROGRESS BAR ===== */
#top-progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0%;
    background: linear-gradient(90deg, #6366f1, #3b82f6, #10b981);
    box-shadow: 0 0 10px rgba(99, 102, 241, 0.7), 0 0 5px rgba(59, 130, 246, 0.5);
    z-index: 10000;
    pointer-events: none;
    opacity: 0;
    transition: width 0.25s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
}

#top-progress-bar.active {
    opacity: 1;
}

/* ===== GLOBAL BRANDED LOADING SCREEN ===== */
#global-loading {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(15, 23, 42, 0.4);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    display: flex !important;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease, visibility 0.25s ease;
}

.dark #global-loading {
    background: rgba(15, 23, 42, 0.65);
}

#global-loading.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

.loading-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    padding: 1.75rem 2.5rem;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 1.25rem;
    box-shadow: 0 25px 50px -12px rgba(15, 23, 42, 0.25), 0 0 0 1px rgba(0, 0, 0, 0.03);
    transform: scale(0.92);
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.dark .loading-card {
    background: #1e293b;
    border-color: #334155;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.05);
}

#global-loading.active .loading-card {
    transform: scale(1);
}

.loading-brand {
    position: relative;
    width: 52px;
    height: 52px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.loading-brand .loading-ring {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    border: 3px solid transparent;
    border-top-color: #6366f1;
    border-right-color: #3b82f6;
    animation: spin 0.9s cubic-bezier(0.55, 0.15, 0.45, 0.85) infinite;
}

.loading-brand .loading-ring-inner {
    position: absolute;
    inset: 6px;
    border-radius: 50%;
    border: 2.5px solid transparent;
    border-bottom-color: #10b981;
    border-left-color: #06b6d4;
    animation: spin 1.4s linear infinite reverse;
}

.loading-brand .loading-icon {
    font-size: 1.25rem;
    color: #6366f1;
    animation: pulse-glow 2s ease-in-out infinite;
}

.dark .loading-brand .loading-icon {
    color: #818cf8;
}

.loading-text {
    font-size: 0.875rem;
    font-weight: 600;
    color: #475569;
    letter-spacing: 0.02em;
    animation: text-pulse 1.8s ease-in-out infinite;
}

.dark .loading-text {
    color: #cbd5e1;
}

/* Container loading overlay */
.card .loading-overlay,
.table-container .loading-overlay {
    position: absolute;
    inset: 0;
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(3px);
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0.2s ease;
    border-radius: inherit;
}

.dark .card .loading-overlay,
.dark .table-container .loading-overlay {
    background: rgba(15, 23, 42, 0.8);
}

.card .loading-overlay.active,
.table-container .loading-overlay.active {
    opacity: 1;
    visibility: visible;
}

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

@keyframes pulse-glow {
    0%, 100% { transform: scale(1); opacity: 0.9; }
    50% { transform: scale(1.15); opacity: 1; }
}

@keyframes text-pulse {
    0%, 100% { opacity: 0.6; }
    50% { opacity: 1; }
}

/* ===== SIDEBAR MOBILE SCROLL LOCK ===== */
body.sidebar-mobile-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
    height: 100vh;
    height: 100dvh;
}

/* ===== THEMED SIDEBAR & MAIN CONTENT BASE TRANSITIONS ===== */
.sidebar,
.sidebar.collapsed {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    height: 100dvh;
    width: var(--sidebar-width);
    z-index: 40;
    background: var(--bg-sidebar, linear-gradient(180deg, #303891 0%, #1e40af 100%)) !important;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
    transition: width var(--transition-speed) var(--transition-timing),
                transform var(--transition-speed) var(--transition-timing);
    will-change: width, transform;
    display: flex;
    flex-direction: column;
}

.dark .sidebar,
.dark .sidebar.collapsed {
    background: var(--bg-sidebar, #0f172a) !important;
    border-right-color: #334155;
}

/* ===== SIDEBAR BACKGROUND SHAPES (lighter/darker shades of the sidebar colour) ===== */
.sidebar-shapes {
    position: absolute;
    inset: 0;
    overflow: hidden;
    pointer-events: none;
    z-index: 0;
}

.sidebar-shape {
    position: absolute;
    border-radius: 50%;
}

.sidebar-shape-1 {
    width: 300px;
    height: 300px;
    top: -110px;
    right: -130px;
    background: rgba(255, 255, 255, 0.07);
}

.sidebar-shape-2 {
    width: 340px;
    height: 340px;
    top: 38%;
    left: -190px;
    background: rgba(0, 0, 0, 0.16);
}

.sidebar-shape-3 {
    width: 280px;
    height: 280px;
    bottom: -120px;
    right: -90px;
    background: rgba(255, 255, 255, 0.11);
}

.sidebar > .sidebar-header,
.sidebar > .sidebar-nav,
.sidebar > .sidebar-footer {
    position: relative;
    z-index: 1;
}

.sidebar-header {
    position: relative;
    flex-shrink: 0;
    background: transparent;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
}

.dark .sidebar-header {
    border-bottom-color: #334155 !important;
}

.sidebar .sidebar-brand span {
    color: #ffffff !important;
}

.sidebar-nav {
    background: transparent;
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
    overflow-x: hidden;
    overflow-y: scroll;
}

.sidebar-nav::-webkit-scrollbar {
    width: 5px;
}

.sidebar-nav::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar-nav::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 9999px;
}

.sidebar-nav:hover::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.4);
}

.dark .sidebar-nav {
    scrollbar-color: rgba(51, 65, 85, 0.5) transparent;
}

.dark .sidebar-nav::-webkit-scrollbar-thumb {
    background: rgba(51, 65, 85, 0.5);
}

.dark .sidebar-nav:hover::-webkit-scrollbar-thumb {
    background: rgba(100, 116, 139, 0.7);
}

.sidebar-footer {
    background: transparent;
    border-top: 1px solid rgba(255, 255, 255, 0.1) !important;
}

.dark .sidebar-footer {
    border-top-color: #334155 !important;
}

.main-content {
    position: relative;
    min-height: 100vh;
    transition: margin-left var(--transition-speed) var(--transition-timing);
    will-change: margin-left;
}

/* ===== SIDEBAR OVERLAY ===== */
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 48;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity var(--transition-speed) ease, visibility var(--transition-speed) ease;
}

.sidebar-overlay.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* ===== SIDEBAR TOGGLE BUTTONS ===== */
#sidebar-toggle:focus-visible,
#sidebar-toggle-mobile:focus-visible {
    outline: 2px solid var(--primary-500, #6366f1);
    outline-offset: 2px;
}

/* Floating boundary rail toggle button */
.sidebar-toggle {
    display: flex;
    width: 26px;
    height: 26px;
    border-radius: 9999px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #ffffff;
    background: #1e3a8a;
    border: 1px solid rgba(255, 255, 255, 0.25);
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease, transform var(--transition-speed) var(--transition-timing);
    flex-shrink: 0;
    position: absolute;
    right: -13px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 50;
}

.dark .sidebar-toggle {
    color: #94a3b8;
    background: #1e293b;
    border-color: #334155;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
}

.sidebar-toggle:hover {
    background: #2563eb;
    color: #ffffff;
    border-color: rgba(255, 255, 255, 0.5);
    transform: translateY(-50%) scale(1.15);
}

.dark .sidebar-toggle:hover {
    background: #334155;
    color: #818cf8;
    border-color: #6366f1;
    transform: translateY(-50%) scale(1.15);
}

.sidebar-toggle .fa-chevron-left {
    font-size: 0.6875rem;
    transition: transform var(--transition-speed) var(--transition-timing);
}

/* ===== NAV LINKS & GROUP LABELS ===== */
.sidebar .nav-group-label {
    padding: 0.75rem 1rem 0.25rem 1rem;
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: rgba(255, 255, 255, 0.55) !important;
    transition: opacity var(--transition-speed) ease;
}

.dark .sidebar .nav-group-label {
    color: #64748b !important;
}

.sidebar .nav-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.625rem 0.875rem;
    margin: 0.125rem 0.5rem;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.85) !important;
    transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
    touch-action: manipulation;
    text-decoration: none;
}

.sidebar .nav-link i {
    color: rgba(255, 255, 255, 0.75);
    transition: color 0.15s ease;
}

.sidebar .nav-link:hover {
    background: rgba(255, 255, 255, 0.12) !important;
    color: #ffffff !important;
}

.sidebar .nav-link:hover i {
    color: #ffffff;
}

.dark .sidebar .nav-link {
    color: #cbd5e1 !important;
}

.dark .sidebar .nav-link:hover {
    background: rgba(99, 102, 241, 0.15) !important;
    color: #818cf8 !important;
}

.sidebar .nav-link.active {
    background: rgba(255, 255, 255, 0.22) !important;
    color: #ffffff !important;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(8px);
}

.sidebar .nav-link.active i {
    color: #ffffff !important;
}

.dark .sidebar .nav-link.active {
    background: #6366f1 !important;
    color: #ffffff !important;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
}

/* Sidebar Plan link & Footer styling */
.sidebar .sidebar-plan-link {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    transition: background 0.15s ease, border-color 0.15s ease;
}

.sidebar .sidebar-plan-link:hover {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(255, 255, 255, 0.25);
}

.sidebar .sidebar-plan-link span {
    color: #ffffff !important;
}

.sidebar .sidebar-plan-link span.text-slate-400 {
    color: rgba(255, 255, 255, 0.6) !important;
}

.sidebar .sidebar-chevron {
    color: rgba(255, 255, 255, 0.5) !important;
}

/* ===== PC SIDEBAR (> 1024px) - COLLAPSIBLE RAIL ===== */
@media (min-width: 1025px) {
    .main-content {
        margin-left: var(--sidebar-width);
    }

    .sidebar.collapsed ~ .main-content {
        margin-left: var(--sidebar-collapsed-width) !important;
    }

    /* Navbar toggle button on PC */
    .sidebar-toggle-navbar {
        display: flex;
        cursor: pointer;
    }

    /* Desktop toggle button inside sidebar header */
    .sidebar-toggle {
        display: flex !important;
    }

    /* Collapsed sidebar state */
    .sidebar.collapsed {
        width: var(--sidebar-collapsed-width);
    }

    .sidebar.collapsed .sidebar-header {
        padding-left: 0.5rem;
        padding-right: 0.5rem;
        justify-content: center;
    }

    .sidebar.collapsed .sidebar-brand {
        justify-content: center;
        gap: 0;
    }

    /* Keep toggle button visible in collapsed state with flipped chevron */
    .sidebar.collapsed .sidebar-toggle {
        display: flex !important;
    }

    .sidebar.collapsed .sidebar-toggle .fa-chevron-left {
        transform: rotate(180deg);
    }

    .sidebar.collapsed .nav-group-label,
    .sidebar.collapsed .nav-link span,
    .sidebar.collapsed .sidebar-text,
    .sidebar.collapsed .sidebar-chevron {
        display: none !important;
    }

    .sidebar.collapsed .nav-link {
        justify-content: center;
        padding: 0.625rem 0.5rem;
        margin: 0.25rem 0.5rem;
        position: relative;
    }

    .sidebar.collapsed .sidebar-footer {
        padding: 0.75rem 0.375rem;
    }

    .sidebar.collapsed .sidebar-footer a,
    .sidebar.collapsed .sidebar-footer button {
        justify-content: center;
        padding: 0.625rem 0.25rem;
        position: relative;
    }

    /* Tooltip badges for collapsed rail nav items */
    .sidebar.collapsed .nav-link[data-tooltip]::before,
    .sidebar.collapsed .sidebar-plan-link[data-tooltip]::before {
        content: attr(data-tooltip);
        position: absolute;
        left: calc(100% + 12px);
        top: 50%;
        transform: translateY(-50%) scale(0.92);
        background: #0f172a;
        color: #f8fafc;
        padding: 0.4rem 0.75rem;
        font-size: 0.75rem;
        font-weight: 500;
        border-radius: 0.375rem;
        white-space: nowrap;
        box-shadow: 0 10px 25px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.2);
        border: 1px solid rgba(255, 255, 255, 0.15);
        pointer-events: none;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s ease;
        z-index: 999;
    }

    .sidebar.collapsed .nav-link[data-tooltip]::after,
    .sidebar.collapsed .sidebar-plan-link[data-tooltip]::after {
        content: '';
        position: absolute;
        left: calc(100% + 4px);
        top: 50%;
        transform: translateY(-50%);
        border-width: 5px;
        border-style: solid;
        border-color: transparent #0f172a transparent transparent;
        pointer-events: none;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.15s ease, visibility 0.15s ease;
        z-index: 999;
    }

    .sidebar.collapsed .nav-link[data-tooltip]:hover::before,
    .sidebar.collapsed .nav-link[data-tooltip]:hover::after,
    .sidebar.collapsed .sidebar-plan-link[data-tooltip]:hover::before,
    .sidebar.collapsed .sidebar-plan-link[data-tooltip]:hover::after {
        opacity: 1;
        visibility: visible;
        transform: translateY(-50%) scale(1);
    }
}

/* ===== DRAWER VIEW (<= 1024px) - AUTO-HIDE DRAWER ===== */
@media (max-width: 1024px) {
    .main-content {
        margin-left: 0 !important;
    }

    /* Sidebar is hidden offscreen by default */
    .sidebar {
        position: fixed;
        top: 0;
        left: 0;
        width: min(var(--sidebar-width), 85vw);
        height: 100vh;
        height: 100dvh;
        z-index: 50;
        transform: translateX(-100%);
        -webkit-transform: translateX(-100%);
        transition: transform var(--transition-speed) var(--transition-timing);
        will-change: transform;
        backface-visibility: hidden;
        box-shadow: none;
    }

    /* Show sidebar when mobile-open */
    .sidebar.mobile-open {
        transform: translateX(0);
        -webkit-transform: translateX(0);
        box-shadow: 8px 0 30px rgba(15, 23, 42, 0.25);
    }

    /* Show mobile toggle button in navbar */
    #sidebar-toggle-mobile {
        display: flex;
        cursor: pointer;
    }

    /* Hide desktop floating boundary toggle button in drawer view */
    .sidebar-toggle {
        display: none !important;
    }

    /* Ensure nav items are visible in drawer */
    .sidebar .nav-group-label,
    .sidebar .nav-link span,
    .sidebar .sidebar-text,
    .sidebar .sidebar-chevron {
        display: block !important;
    }

    .sidebar .nav-link {
        justify-content: flex-start !important;
        padding: 0.625rem 1rem !important;
        margin: 0.125rem 0.5rem !important;
    }

    /* Navbar z-index to be above sidebar overlay */
    .navbar {
        z-index: 45;
    }
}

/* ===== SMALL MOBILE (<= 380px) ===== */
@media (max-width: 380px) {
    .sidebar {
        width: 270px;
        max-width: 88vw;
    }
}

/* ===== LIST PAGINATION (public/js/pagination.js) ===== */
.pg-hidden { display: none !important; }
.pg-bar { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .5rem; padding: .75rem 1rem; border-top: 1px solid var(--border-color); }
.pg-info { font-size: .8125rem; color: var(--text-muted); }
.pg-buttons { display: flex; flex-wrap: wrap; gap: .25rem; }
.pg-btn { padding: .25rem .625rem; font-size: .8125rem; border-radius: .375rem; border: 1px solid var(--border-color); background: var(--bg-card); color: var(--text-secondary); cursor: pointer; }
.pg-btn:hover:not(:disabled):not(.pg-active) { background: var(--hover-bg); }
.pg-btn.pg-active { background: var(--primary-500); border-color: var(--primary-500); color: #fff; }
.pg-btn:disabled { opacity: .5; cursor: not-allowed; }
.pg-gap { padding: .25rem .25rem; color: var(--text-muted); }
