/* 
    Project: Kings School Admin
    Purpose: Global Mobile Responsiveness Fixes
    Author: Antigravity AI
*/

/* ============================================================
   GLOBAL UTILITIES
   ============================================================ */
   @media (max-width: 768px) {
    .page-wrapper {
        margin-left: 0 !important;
        padding-top: 60px;
    }
    .page-content {
        padding: 1rem !important;
    }
    .topbar {
        left: 0 !important;
    }
    .mobile-toggle-menu {
        display: block !important;
    }

    /* Fix for content being hidden behind sidebar when toggled on tablets/small laptops */
    .wrapper.toggled .page-wrapper {
        margin-left: 250px !important;
    }
    .wrapper.toggled .topbar {
        left: 250px !important;
    }
    .wrapper.toggled .modal-dialog {
        margin-left: calc(250px + 1rem) !important;
        max-width: calc(100% - 270px) !important;
    }
}

/* Specific fix for screens above mobile but still in responsive range */
@media screen and (min-width: 769px) and (max-width: 1280px) {
    .wrapper.toggled .page-wrapper {
        margin-left: 250px !important;
    }
    .wrapper.toggled .topbar {
        left: 250px !important;
    }
    .wrapper.toggled .modal-dialog {
        margin-left: calc(250px + 1rem) !important;
        max-width: calc(100% - 270px) !important;
    }
}

/* ============================================================
   GLOBAL Z-INDEX FIXES
   ============================================================ */
.sidebar-wrapper {
    z-index: 1100 !important;
}
.sticky-top {
    z-index: 1010 !important;
}
.flyout-submenu {
    z-index: 1200 !important;
}
/* Ensure SweetAlert2 popups are always on top of modals (which are at 1401) */
.swal2-container {
    z-index: 10000 !important;
}

.sub-flyout-submenu, .sub-bookview-submenu {
    z-index: 1210 !important;
}

/* ============================================================
   MODALS POSITION & Z-INDEX FIX
   ============================================================ */
/* Ensure modals and backdrops are always on top of the sidebar (1100-1300) */
.modal {
    z-index: 1401 !important;
}
.modal-backdrop {
    z-index: 1400 !important;
}



/* ============================================================
   SIDEBAR & OVERLAY
   ============================================================ */
@media (max-width: 1024px) {
    .sidebar-wrapper {
        left: -300px;
        transition: all 0.3s ease;
    }
    .wrapper.toggled .sidebar-wrapper {
        left: 0;
        box-shadow: 5px 0 15px rgba(0,0,0,0.2);
        z-index: 1300;
    }
    .wrapper.toggled .overlay {
        display: block;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 1200;
    }
}

/* ============================================================
   TABLES RESPONSIVENESS
   ============================================================ */
@media (max-width: 768px) {
    .table-responsive {
        border: 0;
        margin-bottom: 1rem;
        overflow-y: hidden;
        -ms-overflow-style: -ms-autohiding-scrollbar;
        border-radius: 8px;
    }
    .table-responsive .table {
        margin-bottom: 0;
        min-width: 800px; /* Ensure table doesn't squish too much */
    }
    .table-responsive .table th, 
    .table-responsive .table td {
        white-space: nowrap;
        padding: 10px 8px !important;
        font-size: 13px;
    }
    
    /* Pagination adjustments */
    #pagination-wrapper {
        display: flex;
        justify-content: center;
        width: 100%;
        overflow-x: auto;
        padding: 10px 0;
    }
    .pagination {
        margin-bottom: 0;
    }
}

/* ============================================================
   HEADER & NAVBAR
   ============================================================ */
@media (max-width: 768px) {
    .topbar .navbar {
        padding-left: 10px;
        padding-right: 10px;
    }
    .user-box .user-info {
        display: none; /* Hide name, keep profile pic */
    }
    /* ============================================================
   RECEIPT & PRINT VIEW
   ============================================================ */
@media (max-width: 768px) {
    .receipt-container {
        padding: 10px !important;
        margin: 10px auto !important;
    }
    .receipt-page {
        padding: 15px !important;
    }
    .header .logo-col img {
        max-height: 40px !important;
    }
    .title-col h2 {
        font-size: 16px !important;
    }
    .details-section {
        flex-direction: column !important;
    }
    .details-section div {
        width: 100% !important;
        margin-bottom: 5px;
        text-align: left !important;
    }
}
    .user-img {
        width: 35px;
        height: 35px;
    }
    .top-menu .navbar-nav .nav-link {
        padding-right: 0.5rem;
        padding-left: 0.5rem;
    }
    .page-title {
        font-size: 1.5rem !important;
    }
}

/* ============================================================
   CARDS & DASHBOARD
   ============================================================ */
@media (max-width: 768px) {
    /* Dashboard adjustments */
    .row-cols-md-4 > .col {
        width: 50% !important;
        margin-bottom: 15px;
    }
    .widgets-icons {
        width: 40px;
        height: 40px;
        font-size: 20px;
    }
    .card-body {
        padding: 1rem !important;
    }

    /* Filters & Action Buttons */
    .d-flex.justify-content-end.mb-3.gap-2,
    .d-flex.justify-content-start.mb-3.gap-4 {
        flex-wrap: wrap !important;
        gap: 10px !important;
    }
    .d-flex.justify-content-end.mb-3.gap-2 > a,
    .d-flex.justify-content-end.mb-3.gap-2 > button,
    .d-flex.justify-content-start.mb-3.gap-4 > div {
        flex: 1 1 auto;
        min-width: 140px;
    }
}

@media (max-width: 480px) {
    .row-cols-md-4 > .col {
        width: 100% !important;
    }
    /* Stacking header elements */
    .d-flex.justify-content-between.align-items-center {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 10px;
    }
    .d-flex.justify-content-between.align-items-center > * {
        width: 100%;
    }
    
    /* Ensure action buttons are easy to tap */
    .d-flex.justify-content-end.mb-3.gap-2 > a,
    .d-flex.justify-content-end.mb-3.gap-2 > button {
        width: 100%;
        text-align: center;
    }
}

/* ============================================================
   FORMS & BUTTONS
   ============================================================ */
@media (max-width: 768px) {
    .btn {
        padding: 8px 12px;
        font-size: 13px;
    }
    .form-control, .form-select {
        font-size: 14px;
    }
    .filter-section .d-flex {
        flex-wrap: wrap;
        gap: 10px;
    }
    .daterange-input {
        width: 100% !important;
    }
}

/* ============================================================
   MODALS
   ============================================================ */
@media (max-width: 768px) {
    .modal-dialog {
        margin: 0.5rem;
        max-width: none !important;
    }
    .modal-content {
        height: auto;
        max-height: 100%;
    }
    .modal-header {
        padding: 10px 15px;
    }
    .modal-body {
        padding: 15px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }
    .modal-footer {
        padding: 10px 15px;
        flex-direction: row; /* Changed to row for better button layout */
        flex-wrap: wrap;
        gap: 8px;
        justify-content: flex-end;
    }
    .modal-footer > .btn {
        flex: 1 1 auto; /* Allow buttons to grow/shrink but not necessarily take full width if they are small */
        min-width: 80px;
        margin: 0 !important;
    }
    .modal-footer .d-flex.gap-2 {
        width: 100%;
        justify-content: center;
        margin-bottom: 5px;
    }
    
    /* Specific fix for tables inside modals on mobile */
    .modal-body .table-responsive {
        max-height: none !important; /* Allow full height within scrollable modal body */
        overflow-y: visible !important;
    }
}

/* ============================================================
   REQUIREMENT PAGE SPECIFIC
   ============================================================ */
@media (max-width: 768px) {
    #createOrderForm .row > div {
        margin-bottom: 10px;
    }
    .align-self-end {
        align-self: flex-start !important;
    }
    #openSelectCategoriesModalBtn, #openItemsModalBtn {
        width: 100%;
    }
    
    /* Qty input width fix */
    .qty-input, .item-qty {
        min-width: 70px !important;
        text-align: center;
    }
    
    /* Table header sticky fix already handled globally */
    
    /* Specific column widths for Requirement tables to prevent squishing */
    #itemsTable th, #itemsTable td,
    #categoryItemsTable th, #categoryItemsTable td,
    #editItemsTable th, #editItemsTable td,
    #editCategoryItemsTable th, #editCategoryItemsTable td,
    #requirementItemsTable th, #requirementItemsTable td {
        min-width: 100px;
    }
    #itemsTable th:first-child, #itemsTable td:first-child,
    #categoryItemsTable th:first-child, #categoryItemsTable td:first-child,
    #editItemsTable th:first-child, #editItemsTable td:first-child,
    #editCategoryItemsTable th:first-child, #editCategoryItemsTable td:first-child,
    #requirementItemsTable th:first-child, #requirementItemsTable td:first-child {
        min-width: 40px !important;
    }
    /* Item Name column should be wider */
    #itemsTable th:nth-child(3), #itemsTable td:nth-child(3),
    #categoryItemsTable th:nth-child(3), #categoryItemsTable td:nth-child(3),
    #editItemsTable th:nth-child(3), #editItemsTable td:nth-child(3),
    #editCategoryItemsTable th:nth-child(3), #editCategoryItemsTable td:nth-child(3),
    #requirementItemsTable th:nth-child(5), #requirementItemsTable td:nth-child(5) {
        min-width: 200px !important;
    }
}

/* ============================================================
   Z-INDEX & FIXES
   ============================================================ */
.mobile-toggle-menu {
    cursor: pointer;
    font-size: 24px;
    color: #008cff;
}

/* ============================================================
   GENERAL TYPOGRAPHY & SPACING
   ============================================================ */
@media (max-width: 768px) {
    h1 { font-size: 1.8rem !important; }
    h2 { font-size: 1.5rem !important; }
    h3 { font-size: 1.3rem !important; }
    h4 { font-size: 1.1rem !important; }
    h5 { font-size: 1rem !important; }
    
    .mb-5 { margin-bottom: 2rem !important; }
    .mt-5 { margin-top: 2rem !important; }
    .p-5 { padding: 1.5rem !important; }
    
    /* Center text on very small screens for headings */
    @media (max-width: 480px) {
        .page-title { text-align: center; }
    }
}

/* Fix for Select2 dropdown positioning on mobile */
.select2-dropdown {
    z-index: 10005 !important;
}

/* Ensure images don't overflow */
img {
    max-width: 100%;
    height: auto;
}

/* Fix for Select2 on mobile */
.select2-container {
    width: 100% !important;
}

/* ============================================================
   LOGIN PAGE SPECIFIC
   ============================================================ */
@media (max-width: 1024px) {
    .login-container .graph { width: 350px; }
    .login-container .stock, .login-container .expenses { width: 200px; }
    .login-container .characters { width: 90%; }
}
@media (max-width: 768px) {
    .login-container { flex-direction: column !important; height: auto !important; }
    .login-container .left, .login-container .right { width: 100% !important; flex: unset !important; }
    .login-container .left {
        height: 300px !important;
        padding: 10px !important;
    }
    .login-container .graph { width: 280px !important; bottom: 40px !important; }
    .login-container .stock, .login-container .expenses { width: 150px !important; }
    .login-container .characters { width: 90% !important; top: 180px !important; }
    .login-form { max-width: 100% !important; padding: 20px !important; }

/* ============================================================
   DATATABLES RESPONSIVENESS
   ============================================================ */
@media (max-width: 768px) {
    .dataTables_wrapper .row {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 10px;
    }
    .dataTables_wrapper .dataTables_filter {
        width: 100%;
        text-align: left !important;
    }
    .dataTables_wrapper .dataTables_filter input {
        width: 100% !important;
        margin-left: 0 !important;
    }
    .dataTables_wrapper .dataTables_info {
        padding-top: 0 !important;
    }
    .dataTables_wrapper .dataTables_paginate {
        width: 100%;
        text-align: center !important;
        display: flex;
        justify-content: center;
    }
    .dataTables_wrapper .dataTables_length {
        width: 100%;
        text-align: left !important;
    }
}

/* Damaged/Wastage Page Title */
@media (max-width: 768px) {
    .page-content h2 {
        font-size: 1.4rem !important;
        margin-bottom: 1rem !important;
    }
}
}
@media (max-width: 480px) {
    .login-form { padding: 15px !important; }
    .login-form h2 { font-size: 22px !important; }
    .login-form button { font-size: 14px !important; }
}
