/* ==========================================
   Global Mobile Responsiveness - Havana System
   ========================================== */

/* --------------------------------------
   1. Global Container & Layout Adjustments
   -------------------------------------- */
/* Ensure proper box sizing and scrolling globally */
html,
body {
    overflow-x: hidden;
    width: 100%;
    position: relative;
    -webkit-text-size-adjust: none;
    -moz-text-size-adjust: none;
    text-size-adjust: none;
    /* Prevent font scaling in landscape */
}

/* Fix Container Widths on Mobile */
@media (max-width: 992px) {

    .container,
    .hero-container,
    .features-grid,
    .footer-container,
    .nav-container,
    .filter-container,
    .workers-grid,
    .stats-container,
    .steps-container,
    .testimonials-grid,
    .faq-container,
    .cta-container,
    .track-header-container,
    .track-search-container,
    .track-result-container {
        padding-left: 20px !important;
        padding-right: 20px !important;
        width: 100% !important;
        max-width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
    }
}

/* --------------------------------------
   2. Navigation & Header
   -------------------------------------- */
@media (max-width: 992px) {

    /* --- CORE: إخفاء ديسكتوب، إظهار موبايل --- */
    .desktop-nav,
    .nav-actions,
    .nav-menu-desktop {
        display: none !important;
    }


    /* إظهار زر الهامبرغر */
    .mobile-toggle {
        display: flex !important;
        flex-direction: column;
        justify-content: space-between;
        width: 32px;
        height: 20px;
        cursor: pointer;
        background: transparent;
        border: none;
        padding: 0;
        z-index: 1002;
    }

    .mobile-toggle span {
        display: block;
        height: 3px;
        width: 100%;
        background: var(--primary-gold);
        border-radius: 4px;
        transition: all 0.3s ease;
    }

    /* Reset all navigation related lists */
    .sidebar-nav,
    .sidebar-nav ul,
    .sidebar-nav li {
        list-style: none !important;
        margin: 0 !important;
        padding: 0 !important;
        text-decoration: none !important;
    }

    .sidebar-nav {
        display: block !important;
        /* Ensure sidebar-nav renders vertically */
    }

    /* --- MOBILE HEADER CUSTOMIZATION --- */
    .header {
        background: #ffffff !important;
        height: 85px !important;
        /* Increased from 70px */
        box-shadow: 0 2px 15px rgba(0, 0, 0, 0.05) !important;
        padding: 0 !important;
        display: flex !important;
        align-items: center !important;
    }

    .nav-container {
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        width: 100% !important;
        height: 100% !important;
        padding: 0 20px !important;
    }

    [dir="rtl"] .logo {
        margin-left: auto !important;
        margin-right: 0 !important;
    }

    [dir="ltr"] .logo {
        margin-right: auto !important;
        margin-left: 0 !important;
    }

    .logo-text {
        font-size: 27px !important;
    }

    .logo-icon img {
        width: 51px !important;
        height: auto !important;
    }


    /* --- القائمة الجانبية على الموبايل --- */
    .mobile-sidebar {
        display: block !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100% !important;
        height: 100% !important;
        z-index: 10000 !important;
        visibility: hidden;
        pointer-events: none;
        transition: visibility 0.3s ease;
    }

    .mobile-sidebar.active {
        visibility: visible;
        pointer-events: auto;
    }

    .sidebar-backdrop {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(6px);
        opacity: 0;
        transition: opacity 0.3s ease;
    }

    .mobile-sidebar.active .sidebar-backdrop {
        opacity: 1;
    }

    .sidebar-drawer {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 320px;
        max-width: 85%;
        background: #ffffff;
        display: flex;
        flex-direction: column;
        box-shadow: 0 0 50px rgba(0, 0, 0, 0.3);
        transition: transform 0.4s cubic-bezier(0.19, 1, 0.22, 1);
        z-index: 10001;
    }

    [dir="rtl"] .sidebar-drawer {
        left: 0;
        transform: translateX(-105%);
    }

    [dir="ltr"] .sidebar-drawer {
        right: 0;
        transform: translateX(105%);
    }

    .mobile-sidebar.active .sidebar-drawer {
        transform: translateX(0);
    }

    /* Sidebar Header */
    .sidebar-header {
        padding: 25px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #f0f0f0;
    }

    .sidebar-logo {
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .sidebar-logo img {
        width: 38px;
        height: auto;
    }

    .sidebar-logo span {
        font-weight: 800;
        font-size: 20px;
        color: var(--primary-gold);
    }

    .sidebar-close {
        width: 44px;
        height: 44px;
        border-radius: 50%;
        border: none;
        background: #f8f8f8;
        color: #e74c3c;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        cursor: pointer;
    }

    /* Sidebar Navigation */
    .sidebar-nav {
        flex: 1;
        padding: 20px 0;
        overflow-y: auto;
    }

    .sidebar-link {
        display: flex !important;
        align-items: center;
        padding: 16px 25px !important;
        font-size: 17px !important;
        font-weight: 600 !important;
        color: #333 !important;
        text-decoration: none !important;
        border-bottom: 1px solid #f9f9f9;
        transition: all 0.2s ease;
        text-align: inherit !important;
    }

    .sidebar-link:active,
    .sidebar-link.active {
        background: rgba(212, 175, 55, 0.05);
        color: var(--primary-gold) !important;
        border-right: 4px solid var(--primary-gold);
    }

    [dir="ltr"] .sidebar-link:active,
    [dir="ltr"] .sidebar-link.active {
        border-right: none;
        border-left: 4px solid var(--primary-gold);
    }

    /* Sidebar Footer */
    .sidebar-footer {
        padding: 25px;
        background: #fdfdfd;
        border-top: 1px solid #f0f0f0;
        display: flex;
        flex-direction: column;
        gap: 15px;
    }

    .sidebar-lang-btn {
        display: flex;
        align-items: center;
        gap: 8px;
        padding: 14px 20px;
        background: #ffffff;
        border: 1px solid #e0e0e0;
        border-radius: 12px;
        color: #333;
        font-weight: 700;
        cursor: pointer;
        width: 100%;
        font-size: 15px;
    }

    .sidebar-lang-btn .lang-label-ar,
    .sidebar-lang-btn .lang-label-en {
        font-weight: 700;
        opacity: 0.45;
        transition: opacity 0.2s, color 0.2s;
    }

    .sidebar-lang-btn .lang-divider {
        opacity: 0.3;
        font-weight: 300;
    }

    /* Highlight active language */
    html[lang="ar"] .sidebar-lang-btn .lang-label-ar,
    html:not([lang="en"]) .sidebar-lang-btn .lang-label-ar {
        opacity: 1;
        color: var(--color-primary, #c9a84c);
    }

    html[lang="en"] .sidebar-lang-btn .lang-label-en {
        opacity: 1;
        color: var(--color-primary, #c9a84c);
    }

    .sidebar-btns {
        display: flex;
        flex-direction: column;
        gap: 12px;
    }

    .sidebar-btns .btn {
        width: 100% !important;
        justify-content: center !important;
        padding: 14px !important;
        border-radius: 12px !important;
        font-weight: 700 !important;
    }

    body.menu-open {
        overflow: hidden !important;
        position: fixed !important;
        width: 100% !important;
    }
}


/* --------------------------------------
   3. Hero Section
   -------------------------------------- */
@media (max-width: 992px) {
    .hero {
        min-height: auto;
        padding-top: 140px;
        /* Increased to prevent overlap with multi-row header if any */
        padding-bottom: 50px;
        text-align: center;
        background-position: center;
    }

    .hero-container {
        grid-template-columns: 1fr !important;
        gap: 40px;
        /* Reverse order: Text first, then Image/Stats */
        display: flex !important;
        flex-direction: column;
    }

    .hero-content {
        order: 1;
        text-align: center !important;
        padding: 0 10px;
    }

    /* Typography Scaling */
    .hero-title {
        font-size: 2rem !important;
        /* using rem for better scaling */
        line-height: 1.3;
        margin-bottom: 20px;
    }

    .hero-description {
        font-size: 1rem !important;
        margin-bottom: 30px;
        padding: 0 10px;
    }

    .hero-buttons {
        flex-direction: column;
        gap: 15px;
        width: 100%;
        max-width: 300px;
        margin: 0 auto 30px;
    }

    .hero-buttons .btn {
        width: 100%;
        justify-content: center;
    }

    /* Stats in Hero */
    .hero-stats {
        flex-direction: row;
        justify-content: center;
        gap: 20px;
        flex-wrap: wrap;
        margin-top: 10px;
    }

    .stat-item {
        min-width: 80px;
    }

    .stat-number {
        font-size: 24px;
    }

    .hero-image {
        order: 2;
        display: none;
        /* Hide for cleaner mobile view */
    }
}

/* --------------------------------------
   4. Filter Section
   -------------------------------------- */
@media (max-width: 992px) {
    .filter-section {
        padding: 30px 0;
    }

    .filter-grid {
        grid-template-columns: 1fr !important;
        gap: 15px;
    }

    .filter-actions {
        flex-direction: column;
        gap: 10px;
    }

    .filter-actions button {
        width: 100%;
    }
}

/* --------------------------------------
   5. Workers & Cards Grid (Universal)
   -------------------------------------- */
@media (max-width: 576px) {

    .workers-grid,
    .features-grid,
    .testimonials-grid,
    .stats-container {
        grid-template-columns: 1fr !important;
        gap: 20px;
    }

    .worker-card {
        margin: 0 auto;
        max-width: 100%;
    }
}

@media (min-width: 577px) and (max-width: 992px) {

    .workers-grid,
    .features-grid,
    .stats-container {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 20px;
    }
}

/* --------------------------------------
   6. Steps Timeline
   -------------------------------------- */
@media (max-width: 992px) {
    .steps-timeline {
        flex-direction: column;
        gap: 30px;
        position: relative;
        padding-left: 20px;
        /* Space for connecting line */
    }

    [dir="rtl"] .steps-timeline {
        padding-left: 0;
        padding-right: 20px;
    }

    .steps-timeline::before {
        /* Vertical connecting line on mobile */
        content: '';
        position: absolute;
        left: 20px;
        /* Align with padding */
        top: 0;
        bottom: 0;
        width: 2px;
        background: rgba(0, 0, 0, 0.1);
        display: block;
    }

    [dir="rtl"] .steps-timeline::before {
        left: auto;
        right: 20px;
    }

    .step-item {
        width: 100%;
        margin-bottom: 20px;
        display: flex;
        align-items: flex-start;
        gap: 15px;
        text-align: inherit;
    }

    .step-number {
        flex-shrink: 0;
        z-index: 1;
        /* Above timeline line */
        background: white;
        /* Cover line behind */
    }

    .step-content {
        padding-top: 10px;
    }
}


/* --------------------------------------
   7. Footer
   -------------------------------------- */
@media (max-width: 992px) {
    .footer-grid {
        grid-template-columns: 1fr !important;
        gap: 40px;
        text-align: center;
    }

    .footer-logo,
    .footer-social,
    .footer-contact li {
        justify-content: center;
    }

    .footer-contact li {
        display: flex;
        flex-direction: column;
        gap: 5px;
    }
}


/* --------------------------------------
   8. Admin Dashboard Specifics
   -------------------------------------- */
@media (max-width: 992px) {
    .sidebar {
        transform: translateX(100%);
        /* RTL: Hide to right */
        right: 0;
        width: 280px !important;
        z-index: 1100;
    }

    [dir="ltr"] .sidebar {
        transform: translateX(-100%);
        left: 0;
        right: auto;
    }

    .sidebar.active {
        transform: translateX(0) !important;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
    }

    .main-content {
        margin-right: 0 !important;
        margin-left: 0 !important;
        width: 100% !important;
        overflow-x: hidden !important;
    }

    .top-header {
        padding: 10px 15px !important;
        height: auto !important;
        flex-wrap: nowrap !important;
        /* Keep items side-by-side but remove padding */
        justify-content: space-between !important;
        gap: 10px !important;
    }

    .header-search {
        display: none !important;
        /* Hide search on mobile to save space and prevent overflow */
    }

    .header-actions {
        order: 2 !important;
        margin-left: auto !important;
        margin-right: 0 !important;
        gap: 10px !important;
    }

    [dir="rtl"] .header-actions {
        margin-right: auto !important;
        margin-left: 0 !important;
    }

    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -15px;
        padding: 0 10px;
    }

    .stats-row {
        grid-template-columns: 1fr !important;
        /* Single column stats on mobile */
        gap: 15px;
    }

    /* Full Width Cards */
    .hero-container,
    .filter-grid,
    .workers-grid,
    .features-grid,
    .stats-container,
    .testimonials-grid,
    .footer-grid,
    .tracking-info-grid {
        grid-template-columns: 1fr !important;
        gap: 20px;
    }

    /* Modal responsive adjustments */
    .modal-dialog {
        margin: 10px;
        width: auto !important;
    }

    /* Modals */
    .modal,
    .tracking-modal {
        width: 95% !important;
        margin: 10px auto;
        max-height: 90vh;
    }
}

/* Tablet Optimizations for Admin */
@media (min-width: 600px) and (max-width: 992px) {
    .stats-row {
        grid-template-columns: repeat(2, 1fr) !important;
        /* Two column stats on tablet */
    }
}

/* --------------------------------------
   9. Safe Area Insets (iPhone X+)
   -------------------------------------- */
/* Safe-area insets — only for non-dashboard pages */
body:not(.dashboard-body) {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

/* Dashboard top-header already handles its own safe area */
.top-header {
    padding-top: max(15px, env(safe-area-inset-top));
}

/* --------------------------------------
   10. Tables to Cards Transformation (Mobile)
   -------------------------------------- */
@media (max-width: 768px) {

    /* Target data-tables specifically */
    .table-container {
        overflow-x: visible !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .data-table,
    .data-table tbody,
    .data-table tr,
    .data-table td {
        display: block;
        width: 100%;
    }

    /* Hide the header row completely */
    .data-table thead {
        display: none;
    }

    /* Card Style for Rows */
    .data-table tr {
        background: white;
        margin-bottom: 20px;
        border-radius: 15px;
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
        border: 1px solid rgba(0, 0, 0, 0.05);
        overflow: hidden;
        padding-bottom: 15px;
        position: relative;
    }

    /* Cell Styles */
    .data-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        text-align: left;
        padding: 12px 15px;
        border-bottom: 1px solid #f8f9fa;
        min-height: 50px;
    }

    [dir="rtl"] .data-table td {
        text-align: right;
    }

    .data-table td:last-child {
        border-bottom: none;
        justify-content: center;
        padding-top: 15px;
    }

    /* The Label (pseudo-element) */
    .data-table td::before {
        content: attr(data-label);
        font-weight: 700;
        font-size: 13px;
        color: var(--text-secondary, #666);
        /* Fallback color */
        flex-shrink: 0;
        margin-left: 15px;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        /* max-width: 40%; Remove max-width to let label grow if needed */
    }

    [dir="rtl"] .data-table td::before {
        margin-right: 0;
        margin-left: 20px;
    }

    [dir="ltr"] .data-table td::before {
        margin-right: 20px;
        margin-left: 0;
    }

    /* Specific adjustments for buttons in card view */
    .receipt-badge {
        width: auto;
        padding: 5px 12px;
        font-size: 11px;
    }

    .client-workers-list {
        justify-content: flex-end;
    }
}

@media (max-width: 992px) {
    /* 11. Dashboard & Settings Mobile Overhaul */

    /* Main Layout Fixes */
    .main-content {
        margin-right: 0 !important;
        margin-left: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
    }

    .dashboard-content {
        padding: 15px 10px !important;
        width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Page Header */
    .page-header {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 15px !important;
        margin-bottom: 20px !important;
    }

    .page-title {
        text-align: center;
    }

    .page-title h2 {
        font-size: 22px !important;
        margin-bottom: 5px !important;
    }

    .page-actions {
        display: flex;
        justify-content: center;
        width: 100%;
    }

    .page-actions .btn {
        width: 100% !important;
        justify-content: center;
    }

    /* Settings Tabs Scrollability */
    .settings-tabs-container,
    .client-tabs-container {
        display: flex !important;
        width: auto !important;
        margin: 0 -10px 20px -10px !important;
        overflow-x: auto !important;
        white-space: nowrap !important;
        background: white !important;
        padding: 10px !important;
        border-radius: 0 !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05) !important;
        -webkit-overflow-scrolling: touch;
    }

    .settings-tabs,
    .client-tabs {
        display: flex !important;
        gap: 0 !important;
        padding: 0 5px !important;
        overflow-x: auto !important;
        max-width: 100% !important;
        -webkit-overflow-scrolling: touch;
    }

    .settings-tab,
    .client-tab {
        flex: 0 0 auto !important;
        padding: 8px 16px !important;
        font-size: 14px !important;
    }

    /* Content Cards */
    .content-card {
        margin-bottom: 20px !important;
        border-radius: 12px !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    .card-header {
        padding: 15px !important;
    }

    .card-body {
        padding: 15px !important;
    }

    /* Form Layouts */
    .form-row {
        grid-template-columns: 1fr !important;
        gap: 15px !important;
        display: flex !important;
        flex-direction: column !important;
    }

    .form-group {
        width: 100% !important;
        margin-bottom: 5px !important;
    }

    .form-label {
        display: block !important;
        margin-bottom: 8px !important;
        font-size: 14px !important;
    }

    .form-input,
    .form-select,
    .form-textarea {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        box-sizing: border-box !important;
        height: 46px !important;
        font-size: 16px !important;
        /* Better for mobile typing */
    }

    /* WhatsApp API Section Specifics */
    #settings-tab-whatsapp .alert {
        white-space: normal !important;
        word-wrap: break-word !important;
        font-size: 13px !important;
        padding: 12px !important;
        line-height: 1.5 !important;
    }

    #api-sid,
    #api-token,
    #api-number {
        font-size: 14px !important;
        /* Keep monospace slightly smaller */
    }

    /* Ensure modals are also responsive */
    .hp-modal-content {
        width: 95% !important;
        margin: 20px auto !important;
        padding: 0 !important;
    }

    .modal-body {
        padding: 15px !important;
    }

    /* Scrollbar styling for tabs */
    .settings-tabs-container::-webkit-scrollbar,
    .settings-tabs::-webkit-scrollbar {
        height: 4px !important;
    }

    .settings-tabs-container::-webkit-scrollbar-thumb,
    .settings-tabs::-webkit-scrollbar-thumb {
        background: rgba(212, 175, 55, 0.4) !important;
        border-radius: 4px !important;
    }

    /* Dropdown Menus on Mobile (Notifications/Messages) */
    .dropdown-menu {
        width: 280px !important;
        max-width: calc(100vw - 30px) !important;
        position: absolute !important;
        top: 100% !important;
        z-index: 1001 !important;
        box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15) !important;
    }

    /* Fixed: Dropdown alignment in RTL mobile to prevent cutting off */
    [dir="rtl"] .dropdown-menu {
        right: 0 !important;
        left: auto !important;
        transform-origin: top right !important;
    }

    [dir="ltr"] .dropdown-menu {
        left: 0 !important;
        right: auto !important;
        transform-origin: top left !important;
    }

    .notification-item {
        padding: 10px !important;
    }
}

/* --------------------------------------
   12. Filter Bar Adjustments (Mobile)
   -------------------------------------- */
@media (max-width: 992px) {
    .filters-bar {
        display: flex !important;
        flex-direction: column !important;
        gap: 15px !important;
        margin-bottom: 20px !important;
        width: 100% !important;
    }

    .filter-item {
        width: 100% !important;
        margin: 0 !important;
    }

    .filter-item input,
    .filter-item select {
        width: 100% !important;
        min-width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        height: 48px !important;
        font-size: 16px !important;
        /* Prevent iOS zoom */
        margin: 0 !important;
        padding: 0 15px !important;
        border-radius: 8px !important;
        border: 1px solid #e2e8f0 !important;
        background-color: white !important;
        -webkit-appearance: none;
        /* Attempt to reset UI */
        -moz-appearance: none;
        appearance: none;
        background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23333' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e") !important;
        background-repeat: no-repeat !important;
        background-position: left 15px center !important;
        /* Since RTL */
        background-size: 16px !important;
    }

    /* RTL specific background position */
    [dir="ltr"] .filter-item input,
    [dir="ltr"] .filter-item select {
        background-position: right 15px center !important;
    }

    /* Input type text shouldn't have the arrow */
    .filter-item input {
        background-image: none !important;
    }

    /* ==========================================
       13. Aggressive Failsafe Overrides (Modals, Forms, Tables)
       ========================================== */
    .modal-content, .modal-dialog, .hp-modal-content, .modal {
        width: 98% !important;
        max-width: 100vw !important;
        margin: 5px auto !important;
        box-sizing: border-box !important;
    }
    
    .modal-body, .modal-header, .modal-footer {
        padding: 15px !important;
        width: 100% !important;
        box-sizing: border-box !important;
        overflow-x: hidden !important;
    }

    /* Force tables inside modals to horizontal scroll instead of breaking layout */
    .table-responsive, .table-container, .modal-body .table-wrapper {
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch !important;
        display: block !important;
    }

    /* Ensure text in tables doesn't stretch cells infinitely */
    table td {
        word-wrap: break-word !important;
        white-space: normal !important;
    }

    form, .form-container, .form-row {
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* Prevent inputs/textareas from bypassing boundaries */
    input, select, textarea, .btn {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
    
    /* Wrap buttons and action items */
    .actions, .btn-group, .modal-footer {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 10px !important;
    }
}