/* Mobile Optimization Styles for AMREST COMPANION */
/* This file contains all mobile-specific styles and responsive design improvements */

/* ===================================
   MOBILE NAVIGATION
   =================================== */

/* Hamburger menu improvements */
@media (max-width: 991px) {
    .navbar {
        padding: 0.7rem 0.95rem !important;
    }

    .navbar .container-fluid {
        align-items: center;
    }

    .navbar-toggler {
        border: 2px solid rgba(255, 255, 255, 0.3);
        padding: 0.5rem 0.75rem;
        border-radius: 8px;
        transition: all 0.3s ease;
    }
    
    .navbar-toggler:focus {
        box-shadow: 0 0 0 0.25rem rgba(240, 165, 0, 0.25);
        border-color: var(--secondary-color);
    }
    
    .navbar-toggler:hover {
        background-color: rgba(255, 255, 255, 0.1);
        border-color: rgba(255, 255, 255, 0.5);
    }

    .navbar-brand {
        max-width: calc(100vw - 108px);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    
    /* Drawer-style collapse menu */
    #navbarNav.navbar-collapse {
        position: fixed;
        top: calc(58px + env(safe-area-inset-top, 0px));
        left: 0.75rem;
        right: 0.75rem;
        background: linear-gradient(180deg, rgba(16, 49, 107, 0.98) 0%, rgba(16, 49, 107, 0.95) 100%);
        margin-top: 0;
        border-radius: 14px;
        border: 1px solid rgba(255, 255, 255, 0.14);
        padding: 0.85rem;
        box-shadow: 0 18px 40px rgba(0, 0, 0, 0.35);
        max-height: calc(100dvh - 74px - env(safe-area-inset-bottom, 0px));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    #navbarNav.navbar-collapse.show {
        animation: mobileNavReveal 0.18s ease-out;
    }

    .navbar-nav {
        gap: 0.15rem;
    }

    .navbar-nav > .nav-item {
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        padding-bottom: 0.15rem;
        margin-bottom: 0.15rem;
    }

    .navbar-nav > .nav-item:last-child {
        border-bottom: none;
        margin-bottom: 0;
        padding-bottom: 0;
    }
    
    /* Larger touch targets for nav items */
    .navbar-nav .nav-link {
        padding: 0.76rem 0.9rem !important;
        font-size: 1rem !important;
        border-radius: 10px;
        margin-bottom: 0;
        min-height: 46px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 0.7rem;
        line-height: 1.2;
        color: #ffffff !important;
    }

    .navbar-nav .nav-link i {
        flex-shrink: 0;
    }
    
    .navbar-nav .nav-link:hover,
    .navbar-nav .nav-link:focus-visible {
        background-color: rgba(255, 255, 255, 0.1);
        color: var(--secondary-color) !important;
    }

    .navbar-nav .nav-link.dropdown-toggle::after {
        margin-left: auto;
        transition: transform 0.18s ease;
    }

    .navbar-nav .show > .nav-link.dropdown-toggle::after {
        transform: rotate(180deg);
    }
    
    /* Dropdown menus on mobile */
    .navbar-nav .dropdown-menu {
        position: static !important;
        float: none;
        width: 100%;
        background-color: rgba(255, 255, 255, 0.1) !important;
        border: 1px solid rgba(255, 255, 255, 0.1) !important;
        border-left: 3px solid rgba(240, 165, 0, 0.45) !important;
        margin-top: 0.35rem !important;
        margin-bottom: 0.35rem !important;
        padding: 0.45rem !important;
        box-shadow: none !important;
        backdrop-filter: none !important;
        transform: none !important;
        border-radius: 10px !important;
    }
    
    .navbar-nav .dropdown-item {
        padding: 0.62rem 0.8rem !important;
        font-size: 0.92rem !important;
        min-height: 44px;
        display: flex;
        align-items: center;
        border-radius: 8px;
        margin-bottom: 0.15rem;
        color: #ffffff !important;
    }
    
    .navbar-nav .dropdown-item:hover,
    .navbar-nav .dropdown-item:focus {
        background-color: rgba(240, 165, 0, 0.2) !important;
        color: var(--secondary-color) !important;
    }
    
    .navbar-nav .dropdown-item.active,
    .navbar-nav .dropdown-item:active {
        background-color: rgba(240, 165, 0, 0.3) !important;
        color: var(--secondary-color) !important;
    }
    
    .navbar-nav .dropdown-item i {
        color: rgba(255, 255, 255, 0.7);
        margin-right: 0.55rem;
    }
    
    .navbar-nav .dropdown-header {
        padding: 0.68rem 0.8rem;
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: var(--secondary-color) !important;
        border-bottom-color: rgba(240, 165, 0, 0.4) !important;
        margin-bottom: 0.35rem;
    }
    
    .navbar .dropdown-menu h6.dropdown-header {
        color: var(--secondary-color) !important;
        border-bottom-color: rgba(240, 165, 0, 0.4) !important;
    }
    
    .dropdown-divider {
        border-color: rgba(255, 255, 255, 0.15);
        margin: 0.45rem 0;
    }
    
    /* Mega menu mobile fixes */
    .mega-menu {
        min-width: 100% !important;
        max-width: 100% !important;
        padding: 0.5rem !important;
    }
    
    .mega-menu-column {
        border-bottom-color: rgba(255, 255, 255, 0.1) !important;
    }
    
    /* Recently Viewed mobile optimization */
    #recently-viewed-menu {
        position: fixed !important;
        top: auto !important;
        left: 50% !important;
        transform: translateX(-50%) !important;
        bottom: 10px !important;
        max-width: 95vw !important;
        min-width: 95vw !important;
        max-height: 70vh !important;
        overflow-y: auto !important;
    }
    
    #recently-viewed-content {
        max-height: 50vh !important;
    }
}

@keyframes mobileNavReveal {
    from {
        opacity: 0;
        transform: translateY(-6px) scale(0.995);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.mobile-nav-overlay {
    display: none;
}

@media (max-width: 991px) {
    .mobile-nav-overlay {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(2, 10, 24, 0.45);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.2s ease;
        z-index: 980;
    }

    body.mobile-nav-open .mobile-nav-overlay {
        opacity: 1;
        pointer-events: auto;
    }

    body.mobile-nav-open {
        overflow: hidden;
    }
}

/* Extra small devices */
@media (max-width: 576px) {
    .navbar-brand {
        font-size: 0.9rem !important;
    }
    
    .navbar-brand img {
        max-height: 30px !important;
    }
    
    #task-counter {
        font-size: 0.7rem !important;
        padding: 0.25em 0.5em !important;
    }
}

/* ===================================
   MOBILE FORMS
   =================================== */

@media (max-width: 768px) {
    /* Single column layout for all forms */
    .row {
        margin-right: 0 !important;
        margin-left: 0 !important;
    }
    
    .col, [class*="col-"] {
        padding-right: 0.5rem !important;
        padding-left: 0.5rem !important;
    }
    
    /* Make form inputs larger and touch-friendly */
    .form-control,
    .form-select,
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    textarea,
    select {
        font-size: 16px !important; /* Prevents iOS zoom */
        padding: 0.75rem 1rem !important;
        min-height: 48px !important;
        border-radius: 8px !important;
    }
    
    /* Larger labels */
    .form-label,
    label {
        font-size: 1rem !important;
        font-weight: 600 !important;
        margin-bottom: 0.5rem !important;
    }
    
    /* Touch-friendly buttons */
    .btn {
        font-size: 1rem !important;
        padding: 0.75rem 1.5rem !important;
        min-height: 48px !important;
        border-radius: 8px !important;
        font-weight: 600 !important;
    }
    
    .btn-sm {
        font-size: 0.9rem !important;
        padding: 0.5rem 1rem !important;
        min-height: 40px !important;
    }
    
    .btn-lg {
        font-size: 1.2rem !important;
        padding: 1rem 2rem !important;
        min-height: 56px !important;
    }
    
    /* Checkbox and radio button sizing */
    .form-check-input {
        width: 1.5rem !important;
        height: 1.5rem !important;
        margin-top: 0.1rem !important;
        cursor: pointer;
    }
    
    .form-check-label {
        padding-left: 0.5rem;
        cursor: pointer;
        font-size: 1rem !important;
    }
    
    /* File input styling */
    input[type="file"] {
        padding: 0.5rem !important;
    }
    
    /* Textarea mobile optimization */
    textarea.form-control {
        min-height: 120px !important;
    }
    
    /* Form groups spacing */
    .mb-3, .form-group {
        margin-bottom: 1.5rem !important;
    }
}

/* ===================================
   MOBILE TABLES
   =================================== */

@media (max-width: 992px) {
    /* Enable horizontal scrolling for tables */
    .table-responsive {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .table {
        font-size: 0.85rem;
        margin-bottom: 0;
    }
    
    .table thead {
        position: sticky;
        top: 0;
        background-color: var(--primary-color);
        z-index: 10;
    }
    
    .table th {
        white-space: nowrap;
        padding: 0.75rem 0.5rem !important;
        font-size: 0.8rem;
    }
    
    .table td {
        padding: 0.75rem 0.5rem !important;
        vertical-align: middle;
    }
    
    /* First column sticky on scroll */
    .table th:first-child,
    .table td:first-child {
        position: sticky;
        left: 0;
        background-color: var(--card-bg);
        z-index: 5;
        box-shadow: 2px 0 5px rgba(0,0,0,0.1);
    }
    
    .table thead th:first-child {
        z-index: 15;
        background-color: var(--primary-color);
    }
    
    /* Make action buttons stack vertically */
    .table .btn-group-vertical {
        display: flex !important;
        flex-direction: column;
        gap: 0.25rem;
    }
    
    .table .btn-group-vertical .btn {
        width: 100%;
        margin-bottom: 0.25rem;
    }
}

@media (max-width: 768px) {
    /* Card-based view for complex tables */
    .table-mobile-cards {
        display: block;
    }
    
    .table-mobile-cards thead {
        display: none;
    }
    
    .table-mobile-cards tbody {
        display: block;
    }
    
    .table-mobile-cards tr {
        display: block;
        margin-bottom: 1rem;
        border: 1px solid rgba(0,0,0,0.125);
        border-radius: 10px;
        padding: 1rem;
        background-color: var(--card-bg);
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    
    .table-mobile-cards td {
        display: flex;
        justify-content: space-between;
        padding: 0.5rem 0 !important;
        border: none !important;
        align-items: center;
    }
    
    .table-mobile-cards td:before {
        content: attr(data-label);
        font-weight: 600;
        margin-right: 1rem;
        color: var(--primary-color);
        flex-shrink: 0;
    }
    
    .table-mobile-cards td:first-child {
        position: static;
        box-shadow: none;
    }
}

/* ===================================
   MOBILE PAGE LAYOUTS
   =================================== */

@media (max-width: 768px) {
    /* Adjust main content padding */
    .main-content {
        padding-top: 100px !important;
        padding-bottom: 2rem;
    }
    
    /* Container adjustments */
    .container,
    .container-fluid,
    .container-wide {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }
    
    /* Card styling for mobile */
    .card {
        margin-bottom: 1rem;
        border-radius: 12px;
    }
    
    .card-body {
        padding: 1rem !important;
    }
    
    .card-header {
        padding: 0.75rem 1rem !important;
        font-size: 1.1rem !important;
    }
    
    /* Hero section on home page */
    .hero {
        padding: 2rem 1.5rem !important;
        margin: 1rem 0 !important;
    }
    
    .hero h1 {
        font-size: 2rem !important;
    }
    
    .hero p {
        font-size: 1rem !important;
    }
    
    /* Footer mobile optimization */
    footer {
        padding: 2rem 0 !important;
    }
    
    .footer-col {
        margin-bottom: 2rem;
        text-align: center;
    }
    
    .footer-col h4 {
        font-size: 1.2rem;
    }
}

/* ===================================
   MOBILE UTILITIES
   =================================== */

@media (max-width: 768px) {
    /* Hide on mobile */
    .d-mobile-none {
        display: none !important;
    }
    
    /* Show only on mobile */
    .d-mobile-block {
        display: block !important;
    }
    
    /* Mobile text sizes */
    .mobile-text-sm {
        font-size: 0.875rem !important;
    }
    
    .mobile-text-lg {
        font-size: 1.25rem !important;
    }
    
    /* Mobile spacing */
    .mobile-mt-3 {
        margin-top: 1rem !important;
    }
    
    .mobile-mb-3 {
        margin-bottom: 1rem !important;
    }
    
    /* Flash messages mobile */
    .alert {
        font-size: 0.95rem;
        padding: 1rem;
        border-radius: 10px;
        margin-bottom: 1rem;
    }
    
    /* Modal mobile optimization */
    .modal-dialog {
        margin: 0.5rem;
        max-width: calc(100% - 1rem);
    }
    
    .modal-content {
        border-radius: 12px;
    }
    
    .modal-header {
        padding: 1rem;
    }
    
    .modal-body {
        padding: 1rem;
    }
    
    .modal-footer {
        padding: 1rem;
        flex-direction: column;
        gap: 0.5rem;
    }
    
    .modal-footer .btn {
        width: 100%;
    }
}

/* ===================================
   TOUCH OPTIMIZATIONS
   =================================== */

/* Remove hover effects on touch devices */
@media (hover: none) and (pointer: coarse) {
    .btn:hover,
    .nav-link:hover,
    .dropdown-item:hover,
    .card:hover {
        transform: none !important;
    }
    
    /* But keep active/pressed states */
    .btn:active,
    .nav-link:active,
    .dropdown-item:active {
        transform: scale(0.98);
        opacity: 0.8;
    }
}

/* Tap highlight color */
* {
    -webkit-tap-highlight-color: rgba(240, 165, 0, 0.3);
}

/* Prevent text selection on buttons */
button, .btn, .nav-link, .dropdown-item {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* ===================================
   LANDSCAPE MOBILE OPTIMIZATION
   =================================== */

@media (max-width: 992px) and (orientation: landscape) {
    .main-content {
        padding-top: 80px !important;
    }
    
    .navbar {
        min-height: 60px;
    }
    
    .hero {
        padding: 1.5rem !important;
    }
    
    .hero h1 {
        font-size: 1.75rem !important;
    }
}

/* ===================================
   ACCESSIBILITY IMPROVEMENTS
   =================================== */

@media (max-width: 768px) {
    /* Focus visible for keyboard navigation */
    *:focus-visible {
        outline: 3px solid var(--secondary-color);
        outline-offset: 2px;
    }
    
    /* Skip to content link */
    .skip-to-content {
        position: absolute;
        top: -40px;
        left: 0;
        background: var(--primary-color);
        color: white;
        padding: 8px;
        text-decoration: none;
        z-index: 100;
    }
    
    .skip-to-content:focus {
        top: 0;
    }
}

/* ===================================
   PERFORMANCE OPTIMIZATIONS
   =================================== */

/* Reduce animations on mobile to improve performance */
@media (max-width: 768px) and (prefers-reduced-motion: no-preference) {
    * {
        animation-duration: 0.5s !important;
        transition-duration: 0.2s !important;
    }
}

/* Disable animations if user prefers reduced motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ===================================
   MOBILE TABLE ENHANCEMENTS
   =================================== */

/* Force all tables to be scrollable on mobile even without .table-responsive */
@media (max-width: 768px) {
    table:not(.table-no-scroll) {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Scroll indicator shadow on scrollable tables */
    .table-responsive {
        position: relative;
    }
    
    .table-responsive::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        width: 20px;
        background: linear-gradient(to left, rgba(0,0,0,0.08), transparent);
        pointer-events: none;
        z-index: 1;
        opacity: 1;
        transition: opacity 0.3s;
    }
    
    /* Improve table readability on mobile */
    .table td, .table th {
        min-width: 80px;
    }
    
    /* Prevent tiny unreadable tables */
    .table {
        font-size: max(0.8rem, 13px) !important;
    }
}

/* ===================================
   MOBILE COLUMN FALLBACK FIX
   =================================== */

/* Ensure columns that only specify md/lg/xl stack properly on small screens */
@media (max-width: 575.98px) {
    /* Force full-width on extra-small when no col-* or col-sm-* is specified */
    .row > [class*="col-md-"]:not([class*="col-sm-"]):not([class*="col-"]):not([class*="col-xs-"]),
    .row > [class*="col-lg-"]:not([class*="col-sm-"]):not([class*="col-md-"]):not([class*="col-"]):not([class*="col-xs-"]),
    .row > [class*="col-xl-"]:not([class*="col-sm-"]):not([class*="col-md-"]):not([class*="col-"]):not([class*="col-xs-"]) {
        flex: 0 0 100%;
        max-width: 100%;
    }
}

/* ===================================
   MOBILE BUTTON GROUP FIX
   =================================== */

@media (max-width: 576px) {
    /* Auto-wrap button groups and flex containers with buttons */
    .d-flex:has(.btn) {
        flex-wrap: wrap !important;
    }
    
    .btn-group:not(.btn-group-vertical) {
        flex-wrap: wrap;
    }
    
    /* Ensure btn-sm meets touch target on mobile */
    .btn-sm {
        min-height: 40px !important;
        min-width: 40px !important;
        padding: 0.5rem 0.75rem !important;
        font-size: 0.875rem !important;
    }
}

/* ===================================
   MOBILE MODAL FIX
   =================================== */

@media (max-width: 576px) {
    .modal-xl, .modal-lg {
        max-width: calc(100vw - 1rem) !important;
        margin: 0.5rem auto !important;
    }
}

/* ===================================
   MOBILE PROGRESS BAR FIX
   =================================== */

@media (max-width: 576px) {
    .progress[style*="height: 25px"],
    .progress[style*="height:25px"] {
        height: 20px !important;
    }
}

/* ===================================
   MOBILE CARD BODY SCROLL FIX
   =================================== */

@media (max-width: 576px) {
    .card-body[style*="max-height: 300px"] {
        max-height: 50vh !important;
    }
}

/* ===================================
   MOBILE GRID OVERFLOW PROTECTION
   =================================== */

@media (max-width: 576px) {
    /* Prevent CSS Grid items from overflowing on small screens */
    [style*="grid-template-columns"],
    .tabs-grid {
        grid-template-columns: 1fr !important;
    }
    
    /* Prevent fixed min-width grids from overflowing */
    *[style*="minmax(350px"],
    *[style*="minmax(300px"] {
        grid-template-columns: 1fr !important;
    }
}

/* ===================================
   MOBILE STICKY POSITION FIX
   =================================== */

@media (max-width: 576px) {
    /* Reduce sticky element footprint on small screens */
    .page-nav {
        position: relative !important;
        top: auto !important;
    }
    
    .key-metrics-bar {
        position: relative !important;
        top: auto !important;
        flex-wrap: wrap !important;
    }
}

/* ===================================
   MOBILE INLINE WIDTH OVERRIDES
   =================================== */

@media (max-width: 576px) {
    /* Fix inline style fixed widths */
    input[style*="width: 100px"],
    input[style*="width: 120px"],
    input[style*="width:100px"],
    input[style*="width:120px"] {
        width: 100% !important;
        max-width: 100% !important;
    }
    
    /* Ensure source amount inputs scale */
    .source-amount-input {
        width: 100% !important;
    }
    
    /* Calendar container mobile fix */
    .cal-container {
        max-width: 100% !important;
    }
}

/* ===================================
   MOBILE DATA ENTRY FORMS
   =================================== */

@media (max-width: 576px) {
    /* Stack form sections fully */
    .form-section {
        padding: 1rem !important;
    }
    
    /* Ensure currency inputs don't clip */
    .currency-input::before {
        left: 8px !important;
    }
    
    /* Datepicker icon mobile positioning */
    .datepicker-icon {
        right: 10px !important;
    }
}

/* ===================================
   MOBILE FLEX LAYOUT HELPERS
   =================================== */

@media (max-width: 576px) {
    /* Stack d-flex button rows on mobile */
    .d-flex.gap-2:has(.btn-sm),
    .d-flex.gap-1:has(.btn) {
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
    }
    
    /* Info banner mobile optimization */
    .info-banner {
        flex-direction: column !important;
        text-align: center !important;
        padding: 16px 20px !important;
        gap: 8px !important;
    }
}

/* ===================================
   MOBILE MEGA-COLUMN TABLES
   =================================== */

/* For tables with 10+ columns, provide better UX hints */
@media (max-width: 768px) {
    .data-table {
        font-size: max(0.78rem, 12px) !important;
    }
    
    .data-table th,
    .data-table td {
        padding: 0.5rem 0.4rem !important;
    }
}

/* ===================================
   MOBILE FOOTER OPTIMIZATION
   =================================== */

@media (max-width: 576px) {
    .footer-col h5::after {
        left: 50%;
        transform: translateX(-50%);
    }
    
    .footer-links a {
        justify-content: center;
    }
}
