/* ==============================================
   Responsive Design - جميع أحجام الشاشات
   ============================================== */

/* ========== Mobile First Approach ========== */

/* Base Styles - Mobile (320px+) */
:root {
    --content-max-width: 100%;
    --sidebar-width: 260px;
    --topbar-height: 68px;
    --container-padding: 1rem;
}

/* General Responsive Utilities */
.container-fluid {
    width: 100%;
    max-width: 100%;
    padding-right: var(--container-padding);
    padding-left: var(--container-padding);
    margin-right: auto;
    margin-left: auto;
}

.row {
    display: flex;
    flex-wrap: wrap;
    margin-right: calc(var(--container-padding) * -0.5);
    margin-left: calc(var(--container-padding) * -0.5);
}

.col {
    flex: 1 0 0%;
    padding-right: calc(var(--container-padding) * 0.5);
    padding-left: calc(var(--container-padding) * 0.5);
}

/* Responsive Images */
img {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Responsive Tables */
.table-responsive {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

table {
    width: 100%;
    border-collapse: collapse;
}

/* ========== Extra Small Devices (320px - 480px) ========== */
@media (max-width: 480px) {
    :root {
        --container-padding: 0.75rem;
        --spacing-xs: 0.2rem;
        --spacing-sm: 0.4rem;
        --spacing-md: 0.75rem;
        --spacing-lg: 1rem;
        --spacing-xl: 1.5rem;
    }
    
    html {
        font-size: 14px;
    }
    
    /* App Container */
    .app-container {
        grid-template-columns: 1fr !important;
        grid-template-areas: "topbar" "main" !important;
    }
    
    /* Topbar */
    .topbar {
        height: 56px;
        padding: 0 0.75rem;
        flex-wrap: wrap;
    }
    
    .topbar-right,
    .topbar-left {
        gap: 0.5rem;
    }
    
    .brand {
        gap: 0.5rem;
    }
    
    .brand-logo {
        width: 32px !important;
        height: 32px !important;
    }
    
    .brand-name {
        font-size: 1rem !important;
    }
    
    .brand-subtitle {
        font-size: 0.75rem !important;
    }
    
    .company-header-name {
        font-size: 1rem !important;
    }
    
    /* User Menu & Dropdown */
    .user-info {
        display: none !important;
    }
    
    .user-btn {
        padding: 0.375rem !important;
        min-width: auto !important;
    }
    
    .user-avatar {
        width: 32px !important;
        height: 32px !important;
    }
    
    .user-avatar svg {
        width: 18px !important;
        height: 18px !important;
    }
    
    .chevron {
        display: none !important;
    }
    
    .dropdown-menu {
        position: fixed !important;
        top: 56px !important;
        right: 0 !important;
        left: 0 !important;
        width: 100% !important;
        border-radius: 0 !important;
        min-width: 100% !important;
        max-width: 100% !important;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5) !important;
        padding: 1rem !important;
        margin: 0 !important;
        border-top: 1px solid #2D3748 !important;
        border-left: none !important;
        border-right: none !important;
        border-bottom: 1px solid #2D3748 !important;
        z-index: 1001 !important;
    }
    
    body[dir="ltr"] .dropdown-menu {
        right: 0 !important;
        left: 0 !important;
    }
    
    .dropdown-item {
        padding: 0.875rem 1rem !important;
        font-size: 0.9375rem !important;
        border-radius: 0.5rem !important;
        margin-bottom: 0.25rem !important;
    }
    
    .dropdown-item:last-child {
        margin-bottom: 0 !important;
    }
    
    .dropdown-item svg {
        width: 20px !important;
        height: 20px !important;
    }
    
    .dropdown-divider {
        margin: 0.75rem 0 !important;
    }
    
    /* Sidebar */
    .sidebar {
        position: fixed;
        left: -100%;
        top: 56px;
        height: calc(100vh - 56px);
        width: 80vw;
        max-width: 280px;
        z-index: 999;
        transition: left 0.3s ease;
    }
    
    .sidebar.open {
        left: 0;
    }
    
    /* Main Content */
    .main-content {
        padding: 0.75rem;
        margin-top: 56px;
    }
    
    /* Cards */
    .card {
        padding: 0.75rem;
        margin-bottom: 0.75rem;
    }
    
    .card-header {
        padding: 0.75rem;
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
    }
    
    .card-body {
        padding: 0.75rem;
    }
    
    .card-title {
        font-size: 1rem;
    }
    
    /* Buttons */
    .btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.875rem;
        min-width: auto;
    }
    
    .btn-group {
        flex-direction: column;
        width: 100%;
    }
    
    .btn-group .btn {
        width: 100%;
        margin-bottom: 0.5rem;
    }
    
    /* Forms */
    .form-group {
        margin-bottom: 0.75rem;
    }
    
    .form-control {
        padding: 0.5rem 0.75rem;
        font-size: 0.875rem;
    }
    
    .form-row {
        flex-direction: column;
    }
    
    .form-row .form-group {
        width: 100%;
    }
    
    /* Tables */
    .table-wrapper {
        overflow-x: auto;
    }
    
    table {
        font-size: 0.75rem;
    }
    
    th, td {
        padding: 0.5rem;
        white-space: nowrap;
    }
    
    /* Modals */
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100vw - 1rem);
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 0.75rem;
    }
    
    /* Dashboard Stats */
    .stats-grid {
        grid-template-columns: 1fr !important;
        gap: 0.75rem;
    }
    
    .stat-card {
        padding: 0.75rem;
    }
    
    /* Charts */
    .chart-container {
        height: 250px !important;
    }
    
    /* Navigation Tabs */
    .nav-tabs {
        overflow-x: auto;
        flex-wrap: nowrap;
        -webkit-overflow-scrolling: touch;
    }
    
    .nav-tabs .nav-link {
        white-space: nowrap;
        padding: 0.5rem 0.75rem;
        font-size: 0.875rem;
    }
}

/* ========== Small Devices (481px - 767px) ========== */
@media (min-width: 481px) and (max-width: 767px) {
    :root {
        --container-padding: 1rem;
    }
    
    html {
        font-size: 15px;
    }
    
    .app-container {
        grid-template-columns: 1fr !important;
        grid-template-areas: "topbar" "main" !important;
    }
    
    .topbar {
        height: 60px;
        padding: 0 1rem;
    }
    
    .brand-logo {
        width: 36px !important;
        height: 36px !important;
    }
    
    /* User Menu & Dropdown for Small Devices */
    .user-info {
        display: none !important;
    }
    
    .user-avatar {
        width: 34px !important;
        height: 34px !important;
    }
    
    .chevron {
        display: none !important;
    }
    
    .dropdown-menu {
        position: fixed !important;
        top: 60px !important;
        right: 0 !important;
        left: 0 !important;
        width: 100% !important;
        border-radius: 0 !important;
        min-width: 100% !important;
        max-width: 100% !important;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5) !important;
        padding: 1rem !important;
        margin: 0 !important;
        border-top: 1px solid #2D3748 !important;
        border-left: none !important;
        border-right: none !important;
        border-bottom: 1px solid #2D3748 !important;
        z-index: 1001 !important;
    }
    
    body[dir="ltr"] .dropdown-menu {
        right: 0 !important;
        left: 0 !important;
    }
    
    .dropdown-item {
        padding: 0.875rem 1rem !important;
        font-size: 0.9375rem !important;
        border-radius: 0.5rem !important;
        margin-bottom: 0.25rem !important;
    }
    
    .dropdown-item:last-child {
        margin-bottom: 0 !important;
    }
    
    .dropdown-divider {
        margin: 0.75rem 0 !important;
    }
    
    /* Sidebar */
    .sidebar {
        position: fixed;
        left: -100%;
        top: 60px;
        height: calc(100vh - 60px);
        width: 70vw;
        max-width: 300px;
        z-index: 999;
        transition: left 0.3s ease;
    }
    
    .sidebar.open {
        left: 0;
    }
    
    .main-content {
        padding: 1rem;
        margin-top: 60px;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 1rem;
    }
    
    .chart-container {
        height: 300px !important;
    }
    
    .btn-group {
        flex-wrap: wrap;
    }
    
    .form-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 1rem;
    }
}

/* ========== Tablets (768px - 1023px) ========== */
@media (min-width: 768px) and (max-width: 1023px) {
    :root {
        --container-padding: 1.5rem;
        --sidebar-width: 220px;
    }
    
    html {
        font-size: 15px;
    }
    
    .app-container {
        grid-template-columns: 220px 1fr;
    }
    
    .topbar {
        height: 64px;
        padding: 0 1.5rem;
    }
    
    /* User Menu & Dropdown for Tablets */
    .user-info {
        display: flex !important;
    }
    
    .user-name {
        font-size: 0.8125rem !important;
    }
    
    .user-role {
        font-size: 0.6875rem !important;
    }
    
    .dropdown-menu {
        position: absolute !important;
        top: calc(100% + 0.5rem) !important;
        right: 0 !important;
        left: auto !important;
        border-radius: 0.75rem !important;
        min-width: 240px !important;
        max-width: 320px !important;
        width: auto !important;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6) !important;
        padding: 0.875rem !important;
        margin: 0 !important;
        border: 1px solid #2D3748 !important;
        z-index: 1001 !important;
    }
    
    body[dir="ltr"] .dropdown-menu {
        right: 0 !important;
        left: auto !important;
    }
    
    body[dir="rtl"] .dropdown-menu {
        left: 0 !important;
        right: auto !important;
    }
    
    .dropdown-item {
        padding: 0.75rem 1rem !important;
        font-size: 0.875rem !important;
        border-radius: 0.5rem !important;
        margin-bottom: 0.25rem !important;
        white-space: nowrap !important;
    }
    
    .dropdown-item:last-child {
        margin-bottom: 0 !important;
    }
    
    .dropdown-item svg {
        flex-shrink: 0 !important;
    }
    
    .dropdown-divider {
        margin: 0.625rem 0 !important;
    }
    
    /* Main Content */
    .main-content {
        padding: 1.5rem;
    }
    
    .stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 1rem;
    }
    
    .chart-container {
        height: 350px !important;
    }
    
    .form-row {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 1rem;
    }
    
    .card-header {
        flex-direction: row;
        align-items: center;
    }
}

/* ========== Small Laptops (1024px - 1365px) ========== */
@media (min-width: 1024px) and (max-width: 1365px) {
    :root {
        --container-padding: 1.5rem;
        --sidebar-width: 240px;
    }
    
    html {
        font-size: 16px;
    }
    
    .app-container {
        grid-template-columns: 240px 1fr;
    }
    
    .topbar {
        height: 68px;
    }
    
    /* User Menu - Full Display */
    .user-info {
        display: flex !important;
    }
    
    .dropdown-menu {
        position: absolute !important;
        top: calc(100% + 0.5rem) !important;
        right: 0 !important;
        left: auto !important;
        border-radius: 0.75rem !important;
        min-width: 260px !important;
        max-width: 340px !important;
        width: auto !important;
        box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6) !important;
        padding: 1rem !important;
        margin: 0 !important;
        border: 1px solid #2D3748 !important;
        z-index: 1001 !important;
    }
    
    body[dir="ltr"] .dropdown-menu {
        right: 0 !important;
        left: auto !important;
    }
    
    body[dir="rtl"] .dropdown-menu {
        left: 0 !important;
        right: auto !important;
    }
    
    .dropdown-item {
        padding: 0.75rem 1rem !important;
        font-size: 0.9375rem !important;
        border-radius: 0.5rem !important;
        margin-bottom: 0.25rem !important;
        white-space: nowrap !important;
    }
    
    .dropdown-item:last-child {
        margin-bottom: 0 !important;
    }
    
    .dropdown-divider {
        margin: 0.75rem 0 !important;
    }
    
    .main-content {
        padding: 1.5rem;
    }
    
    .stats-grid {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 1.5rem;
    }
    
    .chart-container {
        height: 400px !important;
    }
    
    .form-row {
        display: grid;
        grid-template-columns: repeat(2, 1fr);
        gap: 1.5rem;
    }
}

/* ========== Large Laptops & Desktops (1366px - 1919px) ========== */
@media (min-width: 1366px) and (max-width: 1919px) {
    :root {
        --container-padding: 2rem;
        --sidebar-width: 260px;
    }
    
    html {
        font-size: 16px;
    }
    
    .app-container {
        grid-template-columns: 260px 1fr;
    }
    
    .main-content {
        padding: 2rem;
    }
    
    .stats-grid {
        grid-template-columns: repeat(4, 1fr) !important;
        gap: 1.5rem;
    }
    
    .chart-container {
        height: 450px !important;
    }
    
    .form-row {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 1.5rem;
    }
}

/* ========== Extra Large Screens (1920px - 2559px) ========== */
@media (min-width: 1920px) and (max-width: 2559px) {
    :root {
        --container-padding: 2.5rem;
        --sidebar-width: 280px;
    }
    
    html {
        font-size: 17px;
    }
    
    .app-container {
        grid-template-columns: 280px 1fr;
    }
    
    .main-content {
        padding: 2.5rem;
        max-width: 1800px;
        margin: 0 auto;
    }
    
    .stats-grid {
        grid-template-columns: repeat(4, 1fr) !important;
        gap: 2rem;
    }
    
    .chart-container {
        height: 500px !important;
    }
    
    .form-row {
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        gap: 2rem;
    }
    
    .card {
        padding: 2rem;
    }
}

/* ========== 4K & Ultra Wide (2560px+) ========== */
@media (min-width: 2560px) {
    :root {
        --container-padding: 3rem;
        --sidebar-width: 320px;
    }
    
    html {
        font-size: 18px;
    }
    
    .app-container {
        grid-template-columns: 320px 1fr;
    }
    
    .main-content {
        padding: 3rem;
        max-width: 2400px;
        margin: 0 auto;
    }
    
    .stats-grid {
        grid-template-columns: repeat(6, 1fr) !important;
        gap: 2.5rem;
    }
    
    .chart-container {
        height: 600px !important;
    }
    
    .form-row {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 2.5rem;
    }
    
    .card {
        padding: 2.5rem;
    }
    
    .btn {
        padding: 0.875rem 1.75rem;
        font-size: 1.125rem;
    }
}

/* ========== Sidebar Overlay & Backdrop ========== */
.sidebar-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 998;
    opacity: 0;
    transition: opacity 0.3s ease;
    backdrop-filter: blur(2px);
}

.sidebar-backdrop.show {
    display: block;
    opacity: 1;
}

@media (max-width: 767px) {
    .sidebar-backdrop {
        top: var(--topbar-height);
    }
}

/* ========== Additional Responsive Helpers ========== */
.table-responsive::after {
    content: '→';
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    background: linear-gradient(to left, rgba(255,255,255,0.9), transparent);
    padding: 0.5rem 1rem;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.table-responsive.has-scroll::after {
    opacity: 1;
}

@media (max-width: 767px) {
    .table-responsive::after {
        display: block;
    }
}

/* ========== Print Styles ========== */
@media print {
    .sidebar,
    .topbar,
    .btn,
    .sidebar-toggle,
    .modal-backdrop {
        display: none !important;
    }
    
    .main-content {
        margin: 0 !important;
        padding: 0 !important;
    }
    
    .app-container {
        display: block !important;
    }
    
    .card {
        page-break-inside: avoid;
        break-inside: avoid;
    }
    
    table {
        page-break-inside: auto;
    }
    
    tr {
        page-break-inside: avoid;
        break-inside: avoid;
    }
}

/* ========== Landscape Orientation ========== */
@media (max-width: 767px) and (orientation: landscape) {
    .topbar {
        height: 48px;
    }
    
    .sidebar {
        top: 48px;
        height: calc(100vh - 48px);
    }
    
    .main-content {
        margin-top: 48px;
    }
    
    .brand-logo {
        width: 28px !important;
        height: 28px !important;
    }
}

/* ========== High DPI Screens ========== */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    img {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* ========== Touch Device Optimizations ========== */
@media (hover: none) and (pointer: coarse) {
    .btn,
    .nav-link,
    .sidebar-nav-link {
        min-height: 44px;
        min-width: 44px;
    }
    
    input,
    select,
    textarea {
        font-size: 16px; /* Prevents zoom on iOS */
    }
}

/* ========== Flexbox Utilities ========== */
.d-flex {
    display: flex !important;
}

.flex-column {
    flex-direction: column !important;
}

.flex-row {
    flex-direction: row !important;
}

.flex-wrap {
    flex-wrap: wrap !important;
}

.justify-content-center {
    justify-content: center !important;
}

.justify-content-between {
    justify-content: space-between !important;
}

.align-items-center {
    align-items: center !important;
}

.align-items-start {
    align-items: flex-start !important;
}

/* ========== Spacing Utilities ========== */
.m-0 { margin: 0 !important; }
.mt-1 { margin-top: 0.25rem !important; }
.mt-2 { margin-top: 0.5rem !important; }
.mt-3 { margin-top: 1rem !important; }
.mt-4 { margin-top: 1.5rem !important; }
.mb-1 { margin-bottom: 0.25rem !important; }
.mb-2 { margin-bottom: 0.5rem !important; }
.mb-3 { margin-bottom: 1rem !important; }
.mb-4 { margin-bottom: 1.5rem !important; }

.p-0 { padding: 0 !important; }
.pt-1 { padding-top: 0.25rem !important; }
.pt-2 { padding-top: 0.5rem !important; }
.pt-3 { padding-top: 1rem !important; }
.pb-1 { padding-bottom: 0.25rem !important; }
.pb-2 { padding-bottom: 0.5rem !important; }
.pb-3 { padding-bottom: 1rem !important; }

/* ========== Display Utilities ========== */
.d-none { display: none !important; }
.d-block { display: block !important; }
.d-inline-block { display: inline-block !important; }

@media (max-width: 480px) {
    .d-xs-none { display: none !important; }
    .d-xs-block { display: block !important; }
}

@media (max-width: 767px) {
    .d-sm-none { display: none !important; }
    .d-sm-block { display: block !important; }
}

@media (max-width: 1023px) {
    .d-md-none { display: none !important; }
    .d-md-block { display: block !important; }
}

@media (min-width: 1024px) {
    .d-lg-none { display: none !important; }
    .d-lg-block { display: block !important; }
}

/* ========== Text Utilities ========== */
.text-center { text-align: center !important; }
.text-left { text-align: left !important; }
.text-right { text-align: right !important; }

@media (max-width: 767px) {
    .text-sm-center { text-align: center !important; }
}

/* ========== Width Utilities ========== */
.w-100 { width: 100% !important; }
.w-75 { width: 75% !important; }
.w-50 { width: 50% !important; }
.w-25 { width: 25% !important; }

@media (max-width: 767px) {
    .w-sm-100 { width: 100% !important; }
}

/* ========== Max Width Container ========== */
.container {
    width: 100%;
    padding-right: var(--container-padding);
    padding-left: var(--container-padding);
    margin-right: auto;
    margin-left: auto;
}

@media (min-width: 576px) {
    .container { max-width: 540px; }
}

@media (min-width: 768px) {
    .container { max-width: 720px; }
}

@media (min-width: 992px) {
    .container { max-width: 960px; }
}

@media (min-width: 1200px) {
    .container { max-width: 1140px; }
}

@media (min-width: 1400px) {
    .container { max-width: 1320px; }
}

/* ========== Grid System ========== */
.grid {
    display: grid;
    gap: 1rem;
}

.grid-cols-1 { grid-template-columns: repeat(1, 1fr); }
.grid-cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid-cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid-cols-4 { grid-template-columns: repeat(4, 1fr); }

@media (max-width: 767px) {
    .grid-cols-2,
    .grid-cols-3,
    .grid-cols-4 {
        grid-template-columns: 1fr;
    }
}

@media (min-width: 768px) and (max-width: 1023px) {
    .grid-cols-3,
    .grid-cols-4 {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ========== Responsive Typography ========== */
h1, .h1 {
    font-size: clamp(1.75rem, 4vw, 2.5rem);
    line-height: 1.2;
}

h2, .h2 {
    font-size: clamp(1.5rem, 3.5vw, 2rem);
    line-height: 1.3;
}

h3, .h3 {
    font-size: clamp(1.25rem, 3vw, 1.75rem);
    line-height: 1.4;
}

h4, .h4 {
    font-size: clamp(1.125rem, 2.5vw, 1.5rem);
    line-height: 1.4;
}

h5, .h5 {
    font-size: clamp(1rem, 2vw, 1.25rem);
    line-height: 1.5;
}

h6, .h6 {
    font-size: clamp(0.875rem, 1.5vw, 1rem);
    line-height: 1.5;
}

/* ========== Smooth Scrolling ========== */
html {
    scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }
}

/* ========== Overflow Handling ========== */
.overflow-auto {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

.overflow-x-auto {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}

.overflow-y-auto {
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}
