/* ===============================================
   LỘC THIÊN ERP — MOBILE & PWA ENHANCEMENTS
   Fixes: touch targets, safe areas, scroll, performance
   =============================================== */

/* === iOS Safe Area Support === */
@supports (padding: env(safe-area-inset-top)) {
    .main-header {
        padding-top: env(safe-area-inset-top) !important;
        /* Extend header background into safe area */
        margin-top: 0;
    }

    /* Safe-area background color is handled by the header itself */

    .sidebar {
        padding-top: env(safe-area-inset-top) !important;
        padding-bottom: env(safe-area-inset-bottom);
    }

    .page-content {
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }
}

/* === iPhone-specific Touch Target Fixes === */
@media (max-width: 767px) {

    /* Minimum 44px touch targets (Apple HIG guideline) */
    .nav-link {
        min-height: 44px !important;
        padding: 12px 16px !important;
    }

    .nav-submenu .nav-link {
        min-height: 44px !important;
        padding: 12px 16px !important;
    }

    .btn {
        min-height: 44px !important;
        padding: 12px 16px !important;
    }

    .btn-sm {
        min-height: 36px !important;
        padding: 8px 12px !important;
    }

    .tab-btn {
        min-height: 44px !important;
        padding: 10px 16px !important;
        font-size: 13px !important;
    }

    /* Form inputs — bigger touch targets */
    .form-control,
    .form-select,
    input[type="text"],
    input[type="password"],
    input[type="number"],
    input[type="date"],
    input[type="tel"],
    select,
    textarea {
        min-height: 44px !important;
        font-size: 16px !important; /* Prevents iOS zoom on focus */
        padding: 10px 14px !important;
    }

    /* Prevent iOS double-tap zoom on interactive elements */
    * {
        touch-action: manipulation;
    }

    /* Smooth scrolling for momentum scroll on iOS */
    .page-content,
    .sidebar-nav,
    .modal-body,
    .card-body {
        -webkit-overflow-scrolling: touch;
    }

    /* Fix iOS bounce overscroll */
    body {
        overscroll-behavior-y: contain;
    }

    /* Stat cards: 2x2 grid on mobile */
    .stat-cards {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
    }

    .stat-card {
        padding: 14px !important;
    }

    .stat-card-value {
        font-size: 1.4rem !important;
    }

    .stat-card-label {
        font-size: 11px !important;
    }

    /* Dashboard charts: full width on mobile */
    div[style*="grid-template-columns:1fr 1fr"] {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    /* Analytics row: stack on mobile */
    .analytics-row,
    div[style*="grid-template-columns:1fr 1fr 1fr"] {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    /* Create order form: stack grid */
    div[style*="grid-template-columns:1fr 2fr 2fr"],
    div[style*="grid-template-columns:1fr 1fr"] {
        grid-template-columns: 1fr !important;
    }

    /* Order cards: tighter padding */
    .order-card {
        padding: 14px !important;
        margin-bottom: 10px !important;
    }

    /* Order footer buttons: Wrap properly */
    .order-card-footer {
        gap: 6px !important;
    }

    .order-card-footer .btn {
        font-size: 12px !important;
        padding: 10px 12px !important;
        flex: 1 1 auto;
    }

    /* Header actions: tighter spacing */
    .header-actions {
        gap: 12px !important;
    }

    /* Hide page title on mobile to make room for actions */
    .page-title {
        display: none !important;
    }

    /* Header: left-align menu, right-align actions */
    .main-header {
        padding: 20px 12px 0 12px !important;
        padding-top: calc(20px + env(safe-area-inset-top, 0px)) !important;
        gap: 8px;
    }

    /* Notification bell: bigger touch target + more visible */
    .notification-bell {
        width: 44px !important;
        height: 44px !important;
        font-size: 22px !important;
        background: rgba(124, 92, 255, 0.08) !important;
        border-radius: 12px !important;
    }

    .notification-count {
        top: -2px !important;
        right: -2px !important;
        min-width: 20px !important;
        height: 20px !important;
        font-size: 11px !important;
    }

    /* User avatar: smaller on mobile */
    .user-avatar {
        padding: 0 10px !important;
        min-width: 36px !important;
        height: 36px !important;
        font-size: 12px !important;
    }

    /* Notification panel: slide-up sheet starting at 50% screen */
    .notification-panel {
        position: fixed !important;
        top: 40% !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        width: 100% !important;
        max-height: 60vh !important;
        border-radius: 20px 20px 0 0 !important;
        z-index: 2000 !important;
        box-shadow: 0 -4px 30px rgba(0,0,0,0.25) !important;
        animation: slideUpPanel 0.3s ease-out !important;
    }

    .notification-panel.hidden {
        animation: none !important;
    }

    /* Handle bar (drag indicator) */
    .notification-header::before {
        content: '' !important;
        display: block !important;
        width: 40px;
        height: 4px;
        background: #ccc;
        border-radius: 2px;
        margin: 0 auto 8px;
    }

    .notification-header {
        flex-wrap: wrap !important;
        padding: 10px 16px 8px !important;
        position: sticky;
        top: 0;
        background: white;
        z-index: 1;
    }

    .notification-header span {
        font-size: 16px !important;
        font-weight: 700 !important;
    }

    /* Bigger notification items on mobile */
    .notification-item {
        padding: 14px 16px !important;
        gap: 12px !important;
        min-height: 56px !important;
        position: relative !important;
    }

    .notification-icon {
        width: 40px !important;
        height: 40px !important;
        font-size: 18px !important;
    }

    .notification-title {
        font-size: 14px !important;
        font-weight: 600 !important;
    }

    .notification-body {
        font-size: 13px !important;
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: unset !important;
        line-height: 1.4 !important;
    }

    .notification-time {
        font-size: 11px !important;
        margin-top: 4px !important;
    }

    .notification-list {
        max-height: calc(60vh - 50px) !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    /* Backdrop overlay for notification panel */
    .notification-backdrop {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.4);
        z-index: 1999;
        animation: fadeInBackdrop 0.2s ease-out;
    }

    /* Toast: below header bar, not at very top */
    .notification-container {
        top: calc(var(--header-height, 60px) + env(safe-area-inset-top, 0px) + 4px) !important;
    }

    .notification-toast {
        margin: 4px 10px !important;
        border-radius: 14px !important;
        padding: 12px 14px !important;
        box-shadow: 0 4px 20px rgba(0,0,0,0.2) !important;
    }

    .toast-title {
        font-size: 14px !important;
    }

    .toast-body {
        font-size: 13px !important;
        white-space: normal !important;
        overflow: visible !important;
        text-overflow: unset !important;
        line-height: 1.4 !important;
    }

    .toast-close {
        font-size: 22px !important;
        padding: 2px 4px !important;
    }

    /* User menu dropdown: position properly on mobile */
    .user-menu-dropdown {
        right: 0 !important;
        left: auto !important;
        min-width: 160px;
    }

    /* Search bar: full width on mobile */
    .search-input {
        width: 100% !important;
    }

    /* Modal improvements */
    .modal-content {
        width: 95% !important;
        max-width: 95vw !important;
        max-height: 92vh !important;
        margin: 4vh auto !important;
        border-radius: 12px !important;
    }

    .modal-body {
        max-height: 70vh !important;
        overflow-y: auto !important;
    }

    /* Detail rows stack on mobile */
    .detail-row {
        flex-direction: column !important;
        gap: 4px !important;
    }

    /* Dispatch filter area — stack vertically */
    section[id="section-dispatch"] > div:first-child {
        flex-direction: column !important;
        gap: 8px !important;
    }

    /* Date range filters — stack */
    #dashboard-date-range {
        flex-direction: column !important;
        width: 100% !important;
    }

    /* Support contacts — smaller padding */
    .support-contacts {
        padding: 10px !important;
        font-size: 0.75rem !important;
    }

    /* ========================================
       CONFIRM ORDERS — Mobile Responsive
       ======================================== */

    /* Review panel: stack image + details vertically on mobile */
    #review-split-container {
        grid-template-columns: 1fr !important;
        overflow-y: auto !important;
        overflow-x: hidden !important;
    }

    /* Remove right border on image section when stacked */
    #review-split-container > div:first-child {
        border-right: none !important;
        border-bottom: 1px solid #E5E7EB;
        padding: 14px !important;
    }

    /* Details section — scrollable */
    #review-split-container > div:last-child {
        padding: 14px !important;
        overflow-y: visible !important;
    }

    /* Review modal card — full-screen on mobile for better product visibility */
    #confirm-review-panel > div:nth-child(2) {
        padding: 0 !important;
        align-items: stretch !important;
    }

    #confirm-review-panel > div:nth-child(2) > div {
        width: 100vw !important;
        max-width: 100vw !important;
        max-height: 100vh !important;
        border-radius: 0 !important;
        margin: 0 !important;
    }

    /* Review panel header: smaller on mobile */
    #confirm-review-panel > div:nth-child(2) > div > div:first-child {
        padding: 10px 14px !important;
        border-radius: 0 !important;
    }

    /* Confirm order cards: stack layout for product visibility */
    #confirm-order-list > div {
        padding: 12px !important;
    }

    /* Product text: make sure it wraps and is readable */
    #confirm-order-list > div div[style*="font-size:11px"][style*="4B5563"],
    #confirm-order-list > div div[style*="font-size:11px"][style*="6B7280"] {
        font-size: 12px !important;
        line-height: 1.5 !important;
        word-break: break-word !important;
        white-space: normal !important;
    }

    /* Confirm order card: stack buttons below info on mobile */
    #confirm-order-list > div > div[style*="display:flex"][style*="justify-content:space-between"] {
        flex-direction: column !important;
        gap: 10px !important;
    }

    /* Action buttons in confirm cards: row layout, full width */
    #confirm-order-list > div > div > div[style*="flex-shrink:0"] {
        flex-direction: row !important;
        width: 100% !important;
        min-width: unset !important;
    }

    #confirm-order-list > div > div > div[style*="flex-shrink:0"] .btn {
        flex: 1 !important;
    }

    /* Review products — editable inputs bigger on mobile */
    #review-products input[type="number"] {
        min-width: 70px !important;
        font-size: 15px !important;
    }

    /* Confirm footer buttons — stack if needed */
    #confirm-review-panel button[id="review-confirm-btn"],
    #confirm-review-panel button[id="review-reject-btn"] {
        font-size: 14px !important;
        padding: 12px 16px !important;
    }
}

/* === Small iPhone (iPhone SE, 5S) === */
@media (max-width: 375px) {
    .page-content {
        padding: 8px !important;
    }

    .stat-cards {
        gap: 8px !important;
    }

    .stat-card {
        padding: 10px !important;
    }

    .stat-card-value {
        font-size: 1.2rem !important;
    }

    .page-heading {
        font-size: 16px !important;
    }

    .tab-btn {
        padding: 8px 10px !important;
        font-size: 11px !important;
    }

    /* Modal takes full width */
    .modal-content {
        width: 100% !important;
        max-width: 100vw !important;
        border-radius: 12px 12px 0 0 !important;
        position: fixed !important;
        bottom: 0 !important;
        margin: 0 !important;
        max-height: 90vh !important;
    }
}

/* === PWA Standalone Mode — remove browser chrome padding === */
@media (display-mode: standalone) {
    body {
        /* Prevent text selection outside inputs */
        -webkit-user-select: none;
        user-select: none;
    }

    input, textarea, [contenteditable] {
        -webkit-user-select: text;
        user-select: text;
    }

    /* Status bar area padding */
    .main-header {
        padding-top: env(safe-area-inset-top, 20px);
    }
}

/* === Performance: Hardware acceleration for animations === */
/* NOTE: Do NOT add .sidebar here — it uses translateX for show/hide */
.modal,
.loading-overlay,
.notification-panel {
    will-change: transform;
    transform: translateZ(0);
}

/* === Scroll performance === */
.page-content {
    contain: content;
}

#dispatch-order-list {
    contain: layout style;
}

/* === Prevent layout shift === */
.stat-card-value {
    min-height: 1.2em;
}

.loading-spinner {
    contain: strict;
}

/* === Mobile Notification Animations === */
@keyframes slideUpPanel {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

@keyframes fadeInBackdrop {
    from { opacity: 0; }
    to { opacity: 1; }
}
