/* ============================================
   MOBILE RESPONSIVE STYLES - FLUID & UNIVERSAL
   Uses responsive units (%, vw, vh, rem, em, clamp)
   NO hardcoded pixel sizes for device-specific dimensions
   ============================================ */

/* Mobile Breakpoint: Tablets and Phones */
@media (max-width: 768px) {

    /* Add padding to body to account for bottom nav */
    body {
        padding-bottom: 5rem;
    }

    /* Header - Fluid */
    header {
        padding: 0.75rem 1rem;
        flex-wrap: nowrap;
        align-items: center;
        z-index: 1001 !important;
        position: relative;
    }

    .header-brand {
        flex: 1;
    }

    .header-brand h1 {
        font-size: clamp(1rem, 4vw, 1.25rem);
    }

    /* Hamburger Menu Button */
    .hamburger-menu {
        display: flex;
        z-index: 1003 !important;
        position: relative;
    }

    /* Mobile Navigation Menu Panel */
    nav#mainNav {
        position: fixed;
        top: 0;
        bottom: 0;
        /* Anchor to bottom for proper viewport fit */
        right: -100%;
        width: min(85vw, 22rem);
        /* Height auto allows top/bottom anchors to define size */
        height: auto;
        background: var(--bg-card);
        box-shadow: -2px 0 10px rgba(0, 0, 0, 0.2);
        flex-direction: column;
        align-items: flex-start;
        gap: 0;
        padding: 3.5rem 0 5rem 0;
        /* Reduced top padding, standard bottom padding */
        transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        overflow-y: auto;
        /* Internal scrolling */
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        z-index: 1002 !important;
        pointer-events: auto !important;
    }

    nav#mainNav.active {
        right: 0 !important;
    }

    /* BACKDROP OVERLAY */
    body.menu-open::before {
        content: '';
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(2px);
        z-index: 1000 !important;
        pointer-events: auto;
    }

    /* Prevent body scroll when menu open */
    body.menu-open {
        overflow: hidden !important;
    }

    /* Navigation Links */
    nav#mainNav .nav-link,
    nav#mainNav .nav-dropdown {
        width: 100%;
        padding: 1rem 1.5rem;
        border-radius: 0;
        justify-content: flex-start;
        font-size: clamp(0.9rem, 3.5vw, 1rem);
        border-bottom: 1px solid var(--border);
    }

    nav#mainNav .nav-link:hover {
        background: #f8fafc;
    }

    /* Upgrade Button in Mobile Menu */
    nav#mainNav .upgrade-btn {
        width: 100%;
        padding: 1rem 1.5rem;
        border-radius: 0;
        justify-content: flex-start;
        font-size: clamp(0.9rem, 3.5vw, 1rem);
        border-bottom: 1px solid var(--border);
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white !important;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        text-decoration: none;
    }

    nav#mainNav .upgrade-btn:hover {
        background: linear-gradient(135deg, #5568d3 0%, #653a8a 100%);
    }

    /* Support Link - visible in natural order */
    nav#mainNav a[href*="support"] {
        display: flex !important;
        width: 100% !important;
    }

    /* Dropdown Menus */
    nav#mainNav .dropdown-menu {
        position: static;
        box-shadow: none;
        border: none;
        border-radius: 0;
        margin-top: 0;
        padding: 0;
        background: #f8fafc;
        display: none;
        /* Hidden by default */
        overflow: hidden;
        transition: max-height 0.3s ease;
    }

    /* FORCE DISPLAY WHEN CLASS IS ADDED */
    nav#mainNav .dropdown-menu.show,
    nav#mainNav .user-dropdown.show {
        display: block !important;
    }

    nav#mainNav .dropdown-menu a {
        padding: 0.75rem 2.5rem;
        font-size: clamp(0.85rem, 3vw, 0.9rem);
        display: block;
    }

    nav#mainNav .dropdown-trigger .dropdown-arrow {
        transition: transform 0.3s ease;
    }

    nav#mainNav .nav-dropdown.open .dropdown-arrow {
        transform: rotate(180deg);
    }

    /* User Menu - At top of sidebar */
    nav#mainNav .user-menu {
        width: 100%;
        margin: 0;
        padding: 0;
        border: none;
        order: -1;
        background: #f1f5f9;
        border-bottom: 1px solid var(--border);
    }

    nav#mainNav .user-menu-btn {
        width: 100%;
        padding: 1.25rem 1.5rem;
        /* Larger touch target */
        border-radius: 0;
        justify-content: flex-start;
        border-bottom: none;
        font-weight: 600;
        color: var(--text-heading);
    }

    nav#mainNav .user-dropdown {
        position: static;
        width: 100%;
        box-shadow: none;
        border: none;
        background: #e2e8f0;
        /* Darker nested background */
        display: none;
        /* Hidden by default */
    }

    nav#mainNav .user-name {
        display: inline;
    }

    /* Bottom Navigation */
    .bottom-nav {
        display: flex !important;
        font-size: clamp(0.7rem, 2.5vw, 0.875rem);
        z-index: 900;
        background: var(--bg-card);
    }

    .bottom-nav-items {
        padding: 0.5rem 0;
    }

    .bottom-nav-item {
        min-width: 0;
        padding: 0.5rem clamp(0.5rem, 2vw, 1rem);
    }

    .bottom-nav-icon {
        font-size: clamp(1.25rem, 5vw, 1.5rem);
    }

    /* Container */
    .container {
        padding: 1rem clamp(0.75rem, 3vw, 1.5rem);
        max-width: 100%;
    }

    /* Cards */
    .card {
        padding: clamp(1rem, 4vw, 1.5rem);
        margin-bottom: 1rem;
    }

    /* Fix AI Upload History Cards on Mobile */
    .history-item {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 0.75rem !important;
    }

    .history-info {
        width: 100% !important;
    }

    .history-filename {
        word-break: break-all !important;
        /* Prevent long filenames from overflowing */
        white-space: normal !important;
    }

    /* Wrap badges if they get too long */
    .history-item>div:last-child {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 0.5rem !important;
        width: 100% !important;
        justify-content: flex-start !important;
    }

    /* TABS - NO SCROLL, JUST FIT */
    .tabs,
    .dashboard-tabs-container {
        gap: 2px !important;
        overflow: hidden !important;
        flex-wrap: nowrap !important;
        padding-bottom: 0 !important;
        display: flex !important;
        width: 100% !important;
    }

    /* Target BOTH class types */
    .tab-button,
    .dashboard-tab {
        padding: 0.75rem 2px !important;
        font-size: 0.75rem !important;
        white-space: nowrap !important;
        flex: 1 1 0px !important;
        min-width: 0 !important;
        text-align: center !important;
        justify-content: center !important;
    }

    @media (min-width: 380px) {

        .tab-button,
        .dashboard-tab {
            font-size: 0.85rem !important;
            padding: 0.75rem 5px !important;
        }
    }

    /* Forms */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="date"],
    select,
    textarea {
        font-size: max(16px, 1rem);
        padding: 0.75rem;
    }

    /* Buttons */
    .btn,
    .btn-primary,
    .btn-secondary,
    button {
        padding: 0.75rem clamp(1rem, 4vw, 1.5rem);
        font-size: clamp(0.9rem, 3.5vw, 1rem);
        min-height: 2.75rem;
    }

    /* Transaction Type Buttons */
    .transaction-type-buttons,
    .type-buttons {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
        gap: 0.5rem;
        width: 100%;
    }

    .transaction-type-buttons button,
    .type-buttons button {
        padding: 0.75rem 0.5rem;
        font-size: clamp(0.8rem, 3vw, 0.9rem);
        min-height: 2.75rem;
    }

    /* Charts */
    canvas {
        max-width: 100% !important;
        height: auto !important;
    }

    .chart-container {
        position: relative;
        width: 100%;
        max-width: 100%;
        margin: 0 auto;
    }

    /* Tables */
    .table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    table {
        min-width: 100%;
        font-size: clamp(0.8rem, 3vw, 0.875rem);
    }

    /* Utility Classes */
    .desktop-only {
        display: none !important;
    }

    .row {
        flex-direction: column;
    }

    .col {
        width: 100%;
        margin-bottom: 1rem;
    }

    /* Headings */
    h1 {
        font-size: clamp(1.5rem, 5vw, 2rem);
    }

    h2 {
        font-size: clamp(1.25rem, 4vw, 1.75rem);
    }

    h3 {
        font-size: clamp(1.1rem, 3.5vw, 1.5rem);
    }

    /* Page Title */
    .page-title {
        font-size: clamp(1.25rem, 4.5vw, 1.75rem);
        margin-bottom: 1rem;
    }
}

/* Extra Small Phones */
@media (max-width: 360px) {

    .tab-button,
    .dashboard-tab {
        font-size: 0.7rem !important;
        padding: 0.5rem 2px !important;
    }

    .bottom-nav-item span:not(.bottom-nav-icon) {
        font-size: 0.65rem;
    }
}