:root {
    --sidebar-width: 260px;
    --sidebar-bg: #1e40af;
    --sidebar-hover: #1e3a8a;
    --header-height: 60px;
    --primary-color: #2563eb;
    --secondary-color: #f43f5e;
    --bg-light: #f1f5f9;
    --text-dark: #0f172a;
    --text-muted: #64748b;
    --card-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1);
    --input-focus: #3b82f6;
    --transition-speed: 0.2s;
    --border-radius-lg: 12px;
    --border-radius-md: 8px;
    --border-radius-sm: 4px;
}

html {
    scroll-behavior: smooth;
}

body {
    background-color: var(--bg-light);
    background-image: none;
    font-family: 'Cairo', sans-serif;
    color: var(--text-dark);
    overflow-x: hidden;
    margin: 0;
    line-height: 1.5;
    font-size: 14px;
    /* Momentum scrolling for touch devices */
    -webkit-overflow-scrolling: touch;
}

@media (min-width: 1400px) {
    body {
        font-size: 15px;
    }
}

.container-fluid {
    max-width: 100%;
    padding-left: 4px;
    padding-right: 4px;
}

@media (min-width: 1200px) {
    .container-fluid {
        padding-left: 2rem;
        padding-right: 2rem;
    }
}

@media (min-width: 1600px) {
    #page-content-wrapper .container-fluid,
    .top-navbar {
        max-width: 1500px;
        margin-left: auto;
        margin-right: auto;
        left: 0;
        right: 0;
    }
    
    [dir="rtl"] #wrapper:not(.toggled) .top-navbar {
        right: 320px;
        left: 0;
    }
}

/* Scrollbar Customization */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { 
    background: rgba(0, 0, 0, 0.1); 
    border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover { background: rgba(0, 0, 0, 0.2); }

#sidebar-wrapper {
    height: 100vh; /* Fixed height for scrolling */
    width: var(--sidebar-width);
    background: #0f172a; /* Darker Slate */
    box-shadow: 4px 0 25px rgba(0,0,0,0.15);
    position: fixed;
    right: 0;
    top: 0;
    z-index: 1050;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: overlay; /* Scroll over content */
    overflow-x: hidden;
}

/* Sidebar Scrollbar - More visible for the user */
#sidebar-wrapper::-webkit-scrollbar {
    width: 6px;
}
#sidebar-wrapper::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 10px;
}
#sidebar-wrapper::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.4);
}

#sidebar-wrapper .list-group {
    padding-bottom: 80px; /* Space at the bottom */
}

#sidebar-wrapper .sidebar-heading {
    padding: 2.5rem 1.5rem;
    font-size: 1.6rem;
    color: #fff;
    font-weight: 800;
    text-align: center;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    background: linear-gradient(45deg, #1e293b, #0f172a);
}

#sidebar-wrapper .list-group-item {
    background: transparent;
    color: rgba(255,255,255,0.65);
    border: none;
    padding: 0.9rem 1.5rem;
    margin: 0.3rem 1.2rem;
    border-radius: var(--border-radius-md);
    font-size: 0.95rem;
    font-weight: 500;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    gap: 12px;
}

#sidebar-wrapper .list-group-item i {
    width: 20px;
    text-align: center;
    font-size: 1.1rem;
    opacity: 0.8;
}

#sidebar-wrapper .list-group-item:hover {
    background: rgba(255,255,255,0.08);
    color: #fff;
    transform: translateX(-8px);
}

#sidebar-wrapper .list-group-item.active {
    background: rgba(255,255,255,0.15);
    color: #fff;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    font-weight: 700;
}

/* Main Content Styling */
#page-content-wrapper {
    min-height: 100vh;
    margin-right: var(--sidebar-width);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Navbar Enhancement */
.top-navbar {
    height: var(--header-height);
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(0,0,0,0.05);
    padding: 0 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: sticky;
    top: 0;
    z-index: 1000;
    box-shadow: 0 2px 10px rgba(0,0,0,0.02);
}

@media (min-width: 992px) {
    .top-navbar {
        padding: 0 2rem;
    }
}

[dir="rtl"] .top-navbar {
    flex-direction: row;
}

@media (min-width: 1400px) {
    #sidebar-wrapper {
        width: 320px;
        margin-right: -320px;
    }
    
    #wrapper.toggled #sidebar-wrapper {
        margin-right: -320px;
    }
    
    #wrapper:not(.toggled) #sidebar-wrapper {
        margin-right: 0;
    }
    
    #page-content-wrapper {
        padding-right: 0;
        transition: padding 0.25s ease-out;
    }

    #wrapper:not(.toggled) #page-content-wrapper {
        padding-right: 320px;
    }
}

/* Sidebar Toggle State for Desktop */
#wrapper.toggled #sidebar-wrapper {
    margin-right: calc(var(--sidebar-width) * -1);
}

#wrapper.toggled #page-content-wrapper {
    margin-right: 0;
}

/* Mobile Overlay */
#sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.4);
    backdrop-filter: blur(2px);
    z-index: 1000;
    transition: opacity 0.3s;
}

/* Card Enhancements */
.card {
    border: none;
    border-radius: var(--border-radius-lg);
    background: #fff;
    box-shadow: var(--card-shadow);
    overflow: hidden;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    margin-bottom: 1.5rem;
}

@media (min-width: 1400px) {
    .card-body {
        padding: 2rem !important;
    }
    
    .stat-card .h3 {
        font-size: 2.2rem;
    }
    
    .stat-card .stat-icon {
        width: 60px;
        height: 60px;
        font-size: 1.2rem;
    }
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 35px rgba(0,0,0,0.12);
}

.card-header {
    background: #fff;
    border-bottom: 1px solid #f8f9fa;
    padding: 1.5rem;
    font-weight: 700;
}

/* Global Container and Form Scroll Improvements */
.container-fluid, .container {
    max-width: 100%;
    overflow-x: auto; /* Enable horizontal scroll if content overflows */
    -webkit-overflow-scrolling: touch;
}

/* Ensure cards and forms don't break layout on small screens */
.card, .form-container, .report-container {
    min-width: 320px; /* Minimum usable width */
}

/* Custom Scrollbar for all horizontal scrolling elements */
.container-fluid::-webkit-scrollbar, 
.container::-webkit-scrollbar,
.table-responsive::-webkit-scrollbar {
    height: 6px;
}

.container-fluid::-webkit-scrollbar-thumb,
.container::-webkit-scrollbar-thumb,
.table-responsive::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.1);
    border-radius: 10px;
}

/* Table Responsive Improvements */
.table-responsive {
    border-radius: var(--border-radius-md);
    overflow-x: auto;
    position: relative;
    /* Enable smooth momentum scrolling on mobile */
    -webkit-overflow-scrolling: touch;
}

/* Custom Scrollbar for tables */
.table-responsive::-webkit-scrollbar {
    height: 6px;
}

.table-responsive::-webkit-scrollbar-thumb {
    background: #cbd5e0;
    border-radius: 10px;
}

.table {
    margin-bottom: 0;
}

.table thead th {
    background-color: #f8f9fa;
    border-bottom: 2px solid #edf2f7;
    color: var(--text-dark);
    font-weight: 700;
    padding: 1.2rem 1rem;
    white-space: nowrap;
}

.table tbody td {
    padding: 1.2rem 1rem;
    vertical-align: middle;
    border-bottom: 1px solid #edf2f7;
}

/* Form Styling */
.form-control, .form-select {
    border-radius: var(--border-radius-md);
    padding: 0.75rem 1.2rem;
    border: 1.5px solid #e2e8f0;
    background-color: #fff;
    transition: all 0.2s ease;
}

.form-control:focus, .form-select:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 4px rgba(63, 81, 181, 0.1);
}

/* Buttons */
.btn {
    border-radius: var(--border-radius-md);
    padding: 0.7rem 1.5rem;
    font-weight: 600;
    transition: all 0.3s;
}

.btn-primary {
    background: linear-gradient(135deg, #3f51b5 0%, #303f9f 100%);
    border: none;
    box-shadow: 0 4px 12px rgba(63, 81, 181, 0.25);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(63, 81, 181, 0.35);
}

/* Media Queries for Responsiveness */
@media (max-width: 991.98px) {
    #sidebar-wrapper {
        margin-right: calc(var(--sidebar-width) * -1);
    }
    #page-content-wrapper {
        margin-right: 0 !important;
    }
    #wrapper.toggled #sidebar-wrapper {
        margin-right: 0;
    }
    #wrapper.toggled #sidebar-overlay {
        display: block;
    }
    
    .top-navbar {
        padding: 0 1rem;
    }
    
    .card-header {
        padding: 1.25rem;
    }
    
    .hero-gradient {
        padding: 2rem !important;
    }
}

@media (max-width: 767.98px) {
    .container-fluid {
        padding: 1rem !important;
    }
    
    .row > * {
        padding-left: 0.75rem;
        padding-right: 0.75rem;
    }
    
    h2, .h2 { font-size: 1.5rem; }
    h3, .h3 { font-size: 1.3rem; }
    
    .display-4 { font-size: 2.5rem; }
    
    .btn {
        padding: 0.6rem 1.2rem;
        font-size: 0.9rem;
    }
    
    /* Table stacked layout for very small screens if needed */
    .table-responsive-stack .table tbody td {
        display: block;
        width: 100%;
        text-align: right !important;
        border-bottom: none;
    }
}

@media (max-width: 575.98px) {
    :root {
        --sidebar-width: 260px;
    }
    
    .top-navbar h5 {
        font-size: 1rem;
    }
}

/* Global Loading Progress Bar */
#global-progress-bar {
    position: fixed;
    top: 0;
    right: 0;
    height: 3px;
    background: linear-gradient(90deg, #3b82f6, #60a5fa, #3b82f6);
    background-size: 200% 100%;
    animation: loading-bar-animation 2s linear infinite;
    z-index: 9999;
    width: 0;
    transition: width 0.3s ease;
}

@keyframes loading-bar-animation {
    0% { background-position: 100% 0; }
    100% { background-position: -100% 0; }
}

/* Back to Top Button */
#back-to-top {
    position: fixed;
    bottom: 30px;
    left: 30px;
    width: 45px;
    height: 45px;
    display: none;
    z-index: 999;
    border: none;
    outline: none;
    cursor: pointer;
    transition: all 0.3s ease;
    align-items: center;
    justify-content: center;
}

#back-to-top:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(37, 99, 235, 0.4);
}

/* Page Transition Fade */
.page-fade-in {
    animation: pageFadeIn 0.4s ease-out forwards;
}


/* Global Loading Overlay */
#global-loader {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(4px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10000;
}

.loader-content {
    text-align: center;
    background: white;
    padding: 2rem;
    border-radius: 20px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.1);
}

.spinner-custom {
    width: 50px;
    height: 50px;
    border: 4px solid #f3f3f3;
    border-top: 4px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 1rem;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Button Loading State */
.btn-loading {
    position: relative;
    color: transparent !important;
    pointer-events: none;
}

.btn-loading::after {
    content: "";
    position: absolute;
    width: 1.2rem;
    height: 1.2rem;
    top: 50%;
    left: 50%;
    margin-top: -0.6rem;
    margin-left: -0.6rem;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Smooth Navigation Hover */
.list-group-item {
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.list-group-item:active {
    transform: scale(0.98);
}

/* Status Badges */
.badge {
    padding: 0.5em 0.8em;
    border-radius: 6px;
    font-weight: 600;
}

/* Dropdown Submenu */
.dropdown-submenu {
    position: relative;
}

.dropdown-submenu .dropdown-menu {
    top: 0;
    right: 100%;
    margin-top: -1px;
    margin-right: 0.5rem;
}

.dropdown-submenu:hover > .dropdown-menu {
    display: block;
}

/* RTL support for submenu */
[dir="rtl"] .dropdown-submenu .dropdown-menu {
    right: auto;
    left: 100%;
    margin-left: 0.5rem;
    margin-right: 0;
}
