/* ===================================
   MOBILE FIXES - ADMIN CONTRACT VIEW ONLY
   =================================== */

/* ========================================
   ADMIN CONTRACT VIEW - ALL SCREEN SIZES
   ======================================== */

/* Bring modal in front of header on ALL screen sizes */
#audit-detail-view {
    z-index: 10002 !important;
}

@media (max-width: 768px) {
    /* ========================================
       ADMIN CONTRACT VIEW (#audit-detail-view)
       ======================================== */

    /* 1. Hide system header when admin contract modal is open */
    body:has(#audit-detail-view:not(.hidden)) header,
    body:has(#audit-detail-view:not(.hidden)) .top-header {
        display: none !important;
    }

    /* 2. Admin Contract Modal Header - Fixed at top with close button */
    #audit-detail-view .bg-white.border-b.px-6.py-4,
    #audit-detail-view .bg-white.border-b {
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
        height: 60px !important;
        z-index: 10001 !important;
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
        padding: 0 1rem !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1) !important;
        box-sizing: border-box !important;
    }

    /* Header text - prevent overflow */
    #audit-detail-view .bg-white.border-b h2 {
        font-size: 1rem !important;
        max-width: 70% !important;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    /* Close button - always visible */
    #audit-detail-view .bg-white.border-b button {
        width: 40px !important;
        height: 40px !important;
        min-width: 40px !important;
        flex-shrink: 0 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        background: #f1f5f9 !important;
        border-radius: 50% !important;
        border: 1px solid #e2e8f0 !important;
    }

    /* 3. Action Buttons - Fixed at bottom, 2 equal columns */
    #audit-detail-view .bg-white.border-t.p-4.flex,
    #audit-detail-view .bg-white.border-t {
        position: fixed !important;
        bottom: 70px !important;
        /* Above bottom nav (60px) */
        left: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
        display: grid !important;
        grid-template-columns: 1fr 1fr !important;
        gap: 0.5rem !important;
        padding: 0.75rem !important;
        background: white !important;
        z-index: 10000 !important;
        box-shadow: 0 -4px 10px rgba(0, 0, 0, 0.1) !important;
        box-sizing: border-box !important;
    }

    /* Equal size buttons */
    #audit-detail-view .bg-white.border-t button {
        width: 100% !important;
        height: 50px !important;
        min-height: 50px !important;
        max-height: 50px !important;
        padding: 4px 8px !important;
        font-size: 0.75rem !important;
        line-height: 1.1 !important;
        text-align: center !important;
        display: flex !important;
        flex-direction: column !important;
        align-items: center !important;
        justify-content: center !important;
        white-space: normal !important;
        margin: 0 !important;
    }

    /* Button icons */
    #audit-detail-view .bg-white.border-t button i {
        margin-right: 0 !important;
        margin-bottom: 2px !important;
        font-size: 1rem !important;
    }

    /* 4. Content area - space for fixed header and buttons */
    #audit-detail-view #printable-contract-admin {
        padding-top: 70px !important;
        /* Space for sticky header */
        padding-bottom: 200px !important;
        /* Space for fixed bottom buttons */
    }

    /* 5. Text wrapping - prevent overflow */
    #audit-detail-view #printable-contract-admin *,
    #audit-detail-view .section-content span,
    #audit-detail-view .section-content p,
    #audit-detail-view .section-content div,
    #audit-detail-view .section-content td {
        word-wrap: break-word !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
    }

    /* Emails and long text - force break anywhere */
    #audit-detail-view [class*="email"],
    #audit-detail-view [class*="EMAIL"],
    #audit-detail-view a[href^="mailto"],
    #audit-detail-view #printable-contract-admin td {
        word-break: break-all !important;
        overflow-wrap: anywhere !important;
        font-size: 0.75rem !important;
        max-width: 100% !important;
    }

    /* 6. Contract view containers */
    #audit-detail-view {
        width: 100vw !important;
        max-width: 100vw !important;
        overflow-x: hidden !important;
    }

    /* ========================================
       CLIENT CONTRACT VIEW (#contract-view)
       NO INTERFERENCE - Let it work naturally
       ======================================== */

    /* Ensure client contract view is scrollable on mobile */
    #contract-view {
        /* Full-screen fixed positioning */
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        /* Constrain to viewport height to enable scroll */
        height: 100vh !important;
        width: 100vw !important;
        /* Enable internal scrolling */
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        /* Keep above other elements */
        z-index: 9999 !important;
        /* Just below alerts */
    }

    /* Client contract bottom button bar */
    #contract-view .fixed.bottom-0 {
        z-index: 30 !important;
        box-shadow: 0 -4px 10px rgba(0, 0, 0, 0.1);
    }

    /* ========================================
       SIGNATURE MODAL (Used by client view)
       ======================================== */

    /* Ensure signature modal works correctly on mobile */
    [id*="signature-modal"],
    .signature-modal {
        position: fixed !important;
        inset: 0 !important;
        z-index: 9999 !important;
        background: rgba(0, 0, 0, 0.8) !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        padding: 1rem !important;
    }

    /* Signature modal content */
    [id*="signature-modal"]>div,
    .signature-modal>div {
        width: 100% !important;
        max-width: 600px !important;
        background: white !important;
        border-radius: 1rem !important;
        overflow: hidden !important;
    }

    /* Signature canvas container */
    [id*="signature-canvas"],
    .signature-canvas {
        width: 100% !important;
        height: auto !important;
        min-height: 200px !important;
        max-height: 300px !important;
        touch-action: none !important;
    }
}

/* ===================================
   iOS Smooth Scrolling
   =================================== */
* {
    -webkit-overflow-scrolling: touch;
}

/* ===================================
   iOS PWA Safe Area Support
   =================================== */

/* iOS Safe Area Support - Only applies when viewport-fit=cover is set */
@supports (padding: max(0px)) {

    /* Body safe areas for iOS */
    body {
        /* padding-top: env(safe-area-inset-top); */
        /* REMOVIDO: Header já aplica safe-area, body padding causava espaço duplo */
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
        /* Bottom handled by bottom menu */
    }

    /* Fixed headers need safe area */
    header.top-header,
    .top-header {
        padding-top: max(0, env(safe-area-inset-top));
    }

    /* Bottom navigation safe area */
    .modern-mobile-menu,
    .fixed.bottom-0 {
        padding-bottom: max(0.5rem, env(safe-area-inset-bottom));
    }

    /* Admin contract view - adjust for safe areas */
    @media (max-width: 768px) {
        #audit-detail-view .bg-white.border-b {
            padding-top: max(1rem, env(safe-area-inset-top)) !important;
            height: calc(60px + env(safe-area-inset-top)) !important;
        }

        #audit-detail-view .bg-white.border-t {
            bottom: calc(70px + env(safe-area-inset-bottom)) !important;
            padding-bottom: max(0.75rem, env(safe-area-inset-bottom)) !important;
        }

        #audit-detail-view #printable-contract-admin {
            padding-top: calc(70px + env(safe-area-inset-top)) !important;
        }
    }

    /* Client contract view safe areas */
    #contract-view .fixed.bottom-0 {
        padding-bottom: max(1rem, env(safe-area-inset-bottom)) !important;
    }
}

/* iOS-specific webkit fixes */
@supports (-webkit-touch-callout: none) {

    /* Prevent bounce scrolling on body when menu is open */
    body.menu-open {
        position: fixed;
        width: 100%;
        overflow: hidden;
    }

    /* Fix iOS keyboard pushing content */
    .modal-open {
        position: fixed;
        width: 100%;
    }
}

/* ===================================
   MODERN MOBILE MENU RESTORATION
   =================================== */

/* Hide on Desktop by default - CRITICAL FIX */
.modern-mobile-menu {
    display: none !important;
}

@media (max-width: 1024px) {

    /* Hide the top hamburger menu since we have bottom nav */
    .mobile-menu-toggle {
        display: none !important;
    }

    /* Adjust header title position since hamburger is gone */
    .header-brand {
        margin-left: 0 !important;
    }

    /* RESTORED: Transparent Wrapper for the Floating Pill Menu */
    .modern-mobile-menu {
        display: flex !important;
        position: fixed;
        bottom: 20px;
        left: 0;
        width: 100%;
        height: auto;
        background: transparent;
        border: none;
        box-shadow: none;
        justify-content: center;
        /* Center the pill */
        align-items: flex-end;
        z-index: 900;
        pointer-events: none;
        /* Allow clicks to pass through emptiness */
        padding-bottom: env(safe-area-inset-bottom);
        animation: slideUpMenu 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    }

    @keyframes slideUpMenu {
        from {
            transform: translateY(100%);
            opacity: 0;
        }

        to {
            transform: translateY(0);
            opacity: 1;
        }
    }

    /* Menu Items (Bottom Nav) */
    .mobile-menu-item {
        flex: 1;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        height: 100%;
        color: #94a3b8;
        /* lighter gray for inactive */
        background: none;
        border: none;
        cursor: pointer;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        padding-bottom: 5px;
        position: relative;
        overflow: hidden;
    }

    .mobile-menu-item:active::after {
        content: '';
        position: absolute;
        width: 100%;
        height: 100%;
        background: radial-gradient(circle, rgba(37, 99, 235, 0.1) 0%, transparent 70%);
        transform: scale(1);
        transition: transform 0s;
    }

    .mobile-menu-item .icon-container {
        font-size: 1.35rem;
        margin-bottom: 4px;
        transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
        display: flex;
        align-items: center;
        justify-content: center;
        width: 48px;
        height: 36px;
        border-radius: 16px;
        position: relative;
    }

    .mobile-menu-item .menu-label {
        font-size: 0.65rem;
        font-weight: 600;
        letter-spacing: 0.02em;
        transition: color 0.3s;
    }

    /* Active State (Bottom Nav) */
    .mobile-menu-item.active {
        color: #2563eb;
    }

    .mobile-menu-item.active .icon-container {
        transform: translateY(-4px);
        background: rgba(37, 99, 235, 0.12);
        color: #2563eb;
        box-shadow: 0 4px 12px rgba(37, 99, 235, 0.15);
    }

    .mobile-menu-item.active .menu-label {
        font-weight: 700;
        color: #1e40af;
    }
}

/* ===================================
   MOBILE DRAWER IMPLEMENTATION (CORRECTED)
   =================================== */

/* HIDE ON DESKTOP */
#main-nav-menu {
    display: none !important;
}

@media (max-width: 1024px) {

    /* Drawer Container */
    #main-nav-menu {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100vh;
        background: #f8fafc;
        z-index: 2000;
        /* Above everything */
        display: flex !important;
        flex-direction: column;
        padding: 20px;
        transform: translateX(-100%);
        /* Start hidden to left */
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        visibility: hidden;
        overflow-y: auto;
    }

    #main-nav-menu.active {
        transform: translateX(0);
        visibility: visible;
    }

    /* Header */
    .mobile-nav-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 24px;
        padding-bottom: 16px;
        border-bottom: 1px solid #e2e8f0;
    }

    .mobile-close-btn {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        background: white;
        border: 1px solid #e2e8f0;
        color: #64748b;
        display: flex;
        align-items: center;
        justify-content: center;
        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
        cursor: pointer;
    }

    /* Link Groups */
    .mobile-nav-group {
        margin-bottom: 8px;
    }

    /* Main Items */
    .mobile-nav-item {
        width: 100%;
        padding: 16px;
        background: white;
        border: 1px solid #f1f5f9;
        border-radius: 12px;
        color: #334155;
        font-weight: 600;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 8px;
        box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
        transition: all 0.2s;
        text-align: left;
        /* Ensure text aligns left for buttons */
    }

    .mobile-nav-item i:first-child {
        width: 24px;
        margin-right: 12px;
        color: #64748b;
        text-align: center;
    }

    .mobile-nav-item.active,
    .mobile-nav-item:active {
        background: #eff6ff;
        border-color: #bfdbfe;
        color: #2563eb;
    }

    .mobile-nav-item.active i:first-child,
    .mobile-nav-item:active i:first-child {
        color: #2563eb;
    }

    .mobile-nav-item .fa-chevron-down {
        transition: transform 0.3s;
    }

    .mobile-nav-item.nav-expanded .fa-chevron-down {
        transform: rotate(180deg);
    }

    .mobile-nav-item.nav-expanded {
        background: #f8fafc;
        border-color: #cbd5e1;
    }

    /* Sub Menu */
    .mobile-sub-menu {
        display: none;
        padding: 8px 0 8px 16px;
        animation: slideDownSub 0.3s ease-out;
    }

    .mobile-nav-item.nav-expanded+.mobile-sub-menu {
        display: block;
    }

    @keyframes slideDownSub {
        from {
            opacity: 0;
            transform: translateY(-10px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    .mobile-sub-item {
        display: flex;
        align-items: center;
        padding: 12px 16px;
        color: #64748b;
        font-size: 0.95rem;
        border-left: 2px solid transparent;
        margin-bottom: 4px;
    }

    .mobile-sub-item:hover,
    .mobile-sub-item:active {
        color: #2563eb;
        background: #f1f5f9;
        border-left-color: #2563eb;
        border-radius: 0 8px 8px 0;
    }

    .mobile-sub-item i {
        width: 24px;
        text-align: center;
        margin-right: 10px;
        font-size: 0.9rem;
    }
}


/* ===================================
   INTERACTIVE MOBILE MENU (PORTED FROM REACT)
   =================================== */

:root {
    --component-inactive-color: #64748b;
    /* var(--muted-foreground) */
    --component-bg: #ffffff;
    /* var(--card) */
    --component-shadow: #e2e8f0;
    /* var(--border) */
    --component-active-bg: #eff6ff;
    /* var(--secondary) */
    --component-line-inactive-color: #e2e8f0;
    /* var(--border) */
    --component-active-color-default: #2563eb;
    /* var(--accent-foreground) */
}

@keyframes iconBounce {

    0%,
    100% {
        transform: translateY(0);
    }

    20% {
        transform: translateY(-0.3em);
    }

    40% {
        transform: translateY(0);
    }

    60% {
        transform: translateY(-0.1em);
    }

    80% {
        transform: translateY(0);
    }
}

/* OVERRIDE EXISTING .modern-mobile-menu */
.modern-mobile-menu {
    border-top: none !important;
    /* Remove old border */
    background: transparent !important;
    /* Let container handle bg */
    box-shadow: none !important;
    padding: 0 1rem 1rem 1rem !important;
    /* Float effect */
    height: auto !important;
    max-width: 500px;
    margin: 0 auto;
    left: 50% !important;
    transform: translateX(-50%) !important;
    bottom: 20px !important;
    /* Floating */
    animation: slideUpMenu 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    pointer-events: none;
    /* Let clicks pass through padding to body if needed */
}

/* The Container imitating the React Component */
.interactive-menu-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--component-bg);
    border-radius: 999px;
    /* Pill shape */
    padding: 0.5rem;
    box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
    position: relative;
    width: 100%;
    border: 1px solid var(--component-shadow);
    pointer-events: auto;
    /* Re-enable clicks */
}

.menu__item {
    background: transparent;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-radius: 999px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    color: var(--component-inactive-color);
    flex: 1;
    justify-content: center;
    /* Magic Line Logic via Grid/Flex trick or JS */
}

/* Active State */
.menu__item.active {
    background: var(--component-active-bg);
    color: var(--component-active-color-default);
    flex-grow: 2;
    /* Slight expansion */
}

.menu__item.active .menu__icon {
    animation: iconBounce 0.6s ease-out;
}

.menu__icon {
    font-size: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.menu__text {
    font-size: 0.85rem;
    font-weight: 600;
    max-width: 0;
    overflow: hidden;
    opacity: 0;
    transition: all 0.3s ease;
    white-space: nowrap;
}

/* Show text only on active item */
.menu__item.active .menu__text {
    max-width: 100px;
    /* Open up */
    opacity: 1;
    margin-left: 4px;
}

/* ===================================
   HAMBURGER ANIMATION & DRAWER REFINEMENTS
   =================================== */

/* Rotate Icon Animation */
.fa-bars,
.fa-xmark {
    transition: transform 0.3s ease-in-out;
}

.icon-rotate-active i {
    transform: rotate(180deg) scale(1.1);
}

/* Drawer Backdrop/Overlay */
#mobile-overlay {
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    transition: opacity 0.3s ease-in-out;
    opacity: 0;
    pointer-events: none;
}

#mobile-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

/* Smooth Accordion Slide */
@keyframes accordionSlideDown {
    from {
        opacity: 0;
        transform: translateY(-10px);
        max-height: 0;
    }

    to {
        opacity: 1;
        transform: translateY(0);
        max-height: 500px;
        /* Arbitrary large height */
    }
}

#main-nav-menu .nav-dropdown,
#main-nav-menu .nav-dropdown.mega-dropdown {
    transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s, transform 0.3s;
    max-height: 0;
    /* Starte collapsed */
    display: block !important;
    /* Always block for animation, control via height */
    opacity: 0;
    visibility: hidden;
    overflow: hidden;
}

/* Expanded State */
#main-nav-menu .nav-link.nav-expanded+.nav-dropdown,
#main-nav-menu .nav-link.nav-expanded .nav-dropdown {
    max-height: 1000px !important;
    opacity: 1 !important;
    visibility: visible !important;
    transform: none !important;
    margin-top: 8px;
    margin-bottom: 8px;
}

/* Chevron Rotation */
.nav-link .chevron {
    transition: transform 0.3s ease;
}

.nav-link.nav-expanded .chevron {
    transform: rotate(180deg);
}