/**
 * Mobile Responsiveness Fixes
 * Fixes whitespace/overflow and hamburger menu visibility
 */

/* ====================================================================================
   FIX 1: PREVENT HORIZONTAL OVERFLOW (Whitespace on edges)
   Note: body overflow-y must remain auto/visible for reCAPTCHA challenge popup
   ==================================================================================== */

/* Prevent any element from causing horizontal scroll */
html {
    overflow-x: hidden !important;
    max-width: 100vw !important;
}

body {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    /* MUST be auto for reCAPTCHA popup */
    max-width: 100vw !important;
    position: relative;
}

/* Ensure all sections stay within viewport - but not volunteer content */
.site-header,
.header-top,
.header-main,
.header-content,
.hero-section,
.tp-section,
.site-footer,
.footer-main {
    max-width: 100% !important;
    overflow-x: hidden !important;
}

/* Allow volunteer containers to center properly */
.tp-container {
    max-width: 100% !important;
}

.volunteer-content,
.volunteer-content .tp-container {
    overflow-x: visible !important;
}

/* Exception: Allow forms to overflow for proper display */
.volunteer-application,
.application-card,
.application-form,
.wpforms-container,
.wpforms-form {
    overflow-x: visible !important;
    overflow-y: visible !important;
}

/* Fix container padding on mobile */
@media (max-width: 768px) {

    /* General container padding - except volunteer section */
    .tp-container:not(.volunteer-content .tp-container) {
        padding-left: 15px !important;
        padding-right: 15px !important;
        margin-left: auto !important;
        margin-right: auto !important;
    }

    /* Fix header content wrapping */
    .header-content {
        padding-left: 15px !important;
        padding-right: 15px !important;
    }

    /* Fix hero section overflow */
    .hero-section {
        padding-left: 15px !important;
        padding-right: 15px !important;
    }

    /* Ensure footer doesn't overflow */
    .site-footer {
        padding-left: 15px !important;
        padding-right: 15px !important;
    }

    /* Fix green header/sections full-width issue */
    .site-header,
    .header-top,
    .header-main {
        width: 100vw !important;
        max-width: 100vw !important;
        margin-left: calc(-50vw + 50%) !important;
        margin-right: calc(-50vw + 50%) !important;
        padding-left: calc(50vw - 50%) !important;
        padding-right: calc(50vw - 50%) !important;
    }

    /* Ensure all direct children respect viewport */
    * {
        max-width: 100%;
    }

    img,
    video,
    iframe {
        max-width: 100% !important;
        height: auto !important;
    }
}

/* ====================================================================================
   FIX 2: HAMBURGER MENU VISIBILITY & FUNCTIONALITY
   Enhanced for Edge Mobile compatibility
   ==================================================================================== */

/* Ensure hamburger button is ALWAYS visible on mobile */
.menu-toggle {
    display: none !important;
    /* Hidden on desktop */
    position: relative;
    z-index: 1001;
    background: rgba(255, 255, 255, 0.15);
    border: 2px solid rgba(255, 255, 255, 0.4);
    padding: 8px 10px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 48px;
    height: 48px;
    flex-shrink: 0;
    /* Cross-browser touch fixes - including Edge */
    -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
    -ms-touch-action: manipulation;
    touch-action: manipulation;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    /* Edge-specific fixes */
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    outline: none;
}

.menu-toggle:active,
.menu-toggle:focus {
    background: rgba(255, 255, 255, 0.3);
    outline: none;
}

.menu-toggle:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: scale(1.05);
}

/* Hamburger icon lines */
.menu-toggle span:not(.menu-label) {
    display: block;
    width: 24px;
    height: 3px;
    background: white;
    margin: 4px auto;
    transition: all 0.3s ease;
    border-radius: 2px;
    pointer-events: none;
}

/* Animated hamburger to X - support both classes */
.menu-toggle.is-active span:nth-child(1),
.menu-toggle[aria-expanded="true"] span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}

.menu-toggle.is-active span:nth-child(2),
.menu-toggle[aria-expanded="true"] span:nth-child(2) {
    opacity: 0;
}

.menu-toggle.is-active span:nth-child(3),
.menu-toggle[aria-expanded="true"] span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

/* Show hamburger menu on mobile and tablet */
@media (max-width: 1024px) {

    /* SHOW THE HAMBURGER BUTTON */
    .menu-toggle {
        display: flex !important;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        order: 3;
        /* Position at end of flex container */
        margin-left: auto;
        /* Edge-specific: ensure button is clickable */
        pointer-events: auto !important;
        cursor: pointer !important;
    }

    /* Hide menu label text on mobile */
    .menu-toggle .menu-label {
        display: none !important;
    }

    /* Hide desktop navigation */
    .main-navigation {
        position: fixed;
        top: 0;
        right: -100%;
        width: 85%;
        max-width: 350px;
        height: 100vh;
        height: 100dvh;
        background: white;
        box-shadow: -5px 0 25px rgba(0, 0, 0, 0.3);
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        z-index: 10001;
        transition: right 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        padding: 0;
    }

    /* Show menu when active */
    .main-navigation.active {
        right: 0 !important;
    }

    /* Mobile menu overlay */
    .menu-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        height: 100dvh;
        background: rgba(0, 0, 0, 0.6);
        z-index: 10000;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
        /* Safari touch fixes */
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
        cursor: pointer;
    }

    .menu-overlay.active {
        opacity: 1;
        visibility: visible;
    }

    /* Mobile menu close button */
    .mobile-menu-close {
        position: absolute;
        top: 50%;
        right: 20px;
        transform: translateY(-50%);
        width: 40px;
        height: 40px;
        background: rgba(255, 255, 255, 0.2);
        border: 2px solid rgba(255, 255, 255, 0.5);
        border-radius: 50%;
        color: white;
        font-size: 24px;
        font-weight: 300;
        cursor: pointer;
        z-index: 1000;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s ease;
        /* Safari touch fixes */
        -webkit-tap-highlight-color: rgba(255, 255, 255, 0.2);
        touch-action: manipulation;
        -webkit-user-select: none;
        user-select: none;
    }

    .mobile-menu-close:hover,
    .mobile-menu-close:active {
        background: rgba(255, 255, 255, 0.3);
        transform: translateY(-50%) scale(1.1);
    }

    /* Vertical menu layout */
    .nav-menu {
        flex-direction: column !important;
        padding: 20px !important;
        width: 100%;
        gap: 0 !important;
    }

    .nav-menu li {
        width: 100%;
        border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    }

    .nav-menu li:last-child {
        border-bottom: none;
    }

    .nav-menu a {
        padding: 15px 10px !important;
        color: #1a472a !important;
        font-size: 16px;
        width: 100%;
        border-radius: 0 !important;
        display: flex;
        align-items: center;
        justify-content: space-between;
        /* Safari touch fixes */
        -webkit-tap-highlight-color: rgba(45, 90, 61, 0.15);
        touch-action: manipulation;
        cursor: pointer;
    }

    .nav-menu a:hover,
    .nav-menu a:active {
        background: rgba(45, 90, 61, 0.08) !important;
    }

    /* Sub-menu styling for mobile */
    .nav-menu .sub-menu {
        position: static !important;
        opacity: 1 !important;
        visibility: visible !important;
        transform: none !important;
        box-shadow: none !important;
        background: rgba(0, 0, 0, 0.03) !important;
        padding-left: 20px !important;
        margin-top: 0 !important;
        display: none;
        /* Hidden by default, shown via JS */
    }

    .menu-item-has-children.active>.sub-menu {
        display: block !important;
    }

    /* Arrow indicator for sub-menus */
    .menu-item-has-children>a::after {
        content: "▼";
        font-size: 12px;
        transition: transform 0.3s ease;
        margin-left: auto;
    }

    .menu-item-has-children.active>a::after {
        transform: rotate(180deg);
    }

    /* Hide desktop header CTA buttons on mobile */
    .header-cta {
        display: none !important;
    }

    /* Ensure site branding displays logo + title properly on mobile */
    .site-branding {
        flex: 1 1 auto;
        min-width: 0;
        /* Allow shrinking */
        overflow: visible;
        display: flex;
        align-items: center;
        gap: 10px;
    }

    /* Better mobile logo sizing - balanced for visibility */
    .site-logo {
        flex-shrink: 0;
    }

    .site-logo img,
    .custom-logo {
        width: auto !important;
        height: 55px !important;
        max-height: 55px !important;
        min-height: 40px;
        object-fit: contain;
    }

    /* Site title - allow wrapping on mobile for long names */
    .site-logo-text {
        flex: 1 1 auto;
        min-width: 0;
        overflow: hidden;
    }

    .site-title {
        font-size: clamp(0.85rem, 3.5vw, 1.1rem) !important;
        line-height: 1.2 !important;
        margin: 0 !important;
        white-space: normal !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
        -webkit-hyphens: auto;
        -ms-hyphens: auto;
        hyphens: auto;
        max-width: 100%;
    }

    .site-title a {
        display: block;
    }

    .site-tagline {
        font-size: 0.65rem !important;
        margin: 2px 0 0 0 !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* Fix header content layout */
    .header-content {
        display: flex !important;
        align-items: center !important;
        justify-content: space-between !important;
        gap: 12px !important;
        flex-wrap: nowrap !important;
        padding: 8px 15px !important;
    }

    /* Prevent body scroll when menu open */
    body.menu-open {
        overflow: hidden !important;
        position: fixed;
        width: 100%;
        height: 100%;
    }
}

/* Extra small mobile devices (iPhone SE, small Android) */
@media (max-width: 480px) {
    .main-navigation {
        width: 92%;
        max-width: none;
    }

    .site-logo img,
    .custom-logo {
        height: 45px !important;
        max-height: 45px !important;
    }

    .site-title {
        font-size: clamp(0.75rem, 3vw, 0.95rem) !important;
        line-height: 1.15 !important;
    }

    .site-tagline {
        display: none !important;
    }

    .header-content {
        gap: 8px !important;
        padding: 6px 12px !important;
    }

    .menu-toggle {
        width: 44px;
        height: 44px;
    }
}

/* ====================================================================================
   FIX 3: ENSURE SECTIONS ARE VISIBLE IN MENU
   ==================================================================================== */

/* Add section navigation items if using anchor links */
.nav-menu .menu-item-section a {
    font-weight: 600;
    color: #2d5a3d !important;
}

.nav-menu .menu-item-section a:hover {
    background: rgba(45, 90, 61, 0.15) !important;
}

/* Ensure all menu items are tappable */
@media (max-width: 1024px) {
    .nav-menu a {
        min-height: 44px !important;
        /* iOS minimum touch target */
        display: flex !important;
        align-items: center !important;
    }

    /* Make sure icons/text don't prevent tap, but allow on links */
    .nav-menu a * {
        pointer-events: none;
    }

    /* Ensure menu toggle children don't block clicks */
    .menu-toggle * {
        pointer-events: none;
    }

    /* Ensure close button is clickable */
    .mobile-menu-close {
        pointer-events: auto !important;
    }

    /* Ensure overlay is clickable */
    .menu-overlay {
        pointer-events: auto !important;
    }
}

/* ====================================================================================
   FIX 4: FORM CENTERING AND MOBILE RESPONSIVENESS
   ==================================================================================== */

@media (max-width: 768px) {

    /* Remove conflicting padding from volunteer sections */
    .volunteer-content,
    .volunteer-grid {
        padding-left: 0 !important;
        padding-right: 0 !important;
    }

    /* Ensure form containers are properly centered */
    .volunteer-application,
    .application-card,
    .application-form {
        width: calc(100% - 30px) !important;
        max-width: 100% !important;
        margin-left: 15px !important;
        margin-right: 15px !important;
        padding: 15px !important;
        box-sizing: border-box !important;
    }

    /* WPForms specific mobile centering - shift left to center */
    .wpforms-container,
    .wpforms-form {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 auto !important;
        padding: 0 !important;
        box-sizing: border-box !important;
        transform: translateX(-25px);
    }

    .wpforms-field-container {
        width: 100% !important;
        padding: 0 !important;
        margin: 0 auto !important;
    }

    /* Ensure form fields are centered and properly sized */
    .wpforms-field {
        width: 100% !important;
        margin-left: auto !important;
        margin-right: auto !important;
        margin-bottom: 1.25rem !important;
        padding: 0 10px !important;
        box-sizing: border-box !important;
    }

    .wpforms-field-label {
        font-size: 16px;
        margin-bottom: 0.5rem;
        display: block;
    }

    /* All form inputs get proper mobile sizing */
    .wpforms-field input[type="text"],
    .wpforms-field input[type="email"],
    .wpforms-field input[type="tel"],
    .wpforms-field input[type="number"],
    .wpforms-field input[type="password"],
    .wpforms-field input[type="url"],
    .wpforms-field textarea,
    .wpforms-field select {
        width: 100%;
        max-width: 100%;
        font-size: 16px;
        min-height: 48px;
        padding: 12px 16px;
        box-sizing: border-box;
        border-radius: 6px;
        border: 1px solid #ddd;
        transition: border-color 0.3s ease;
    }

    .wpforms-field textarea {
        min-height: 120px;
        resize: vertical;
    }

    /* Submit buttons centered and full-width */
    .wpforms-submit-container {
        width: 100%;
        text-align: center;
        margin-top: 1.5rem;
        padding: 0;
    }

    .wpforms-submit,
    button[type="submit"],
    input[type="submit"],
    .btn-submit {
        width: 100%;
        max-width: 100%;
        font-size: 16px;
        min-height: 48px;
        padding: 14px 24px;
        box-sizing: border-box;
        cursor: pointer;
        border-radius: 6px;
        transition: background-color 0.3s ease;
    }

    /* Fix any text overflow in form cards */
    .application-header h2,
    .application-header p,
    .application-card h2,
    .application-card p {
        width: 100%;
        word-wrap: break-word;
        overflow-wrap: break-word;
        margin-left: 0;
        margin-right: 0;
    }

    /* Remove excess padding that might conflict */
    .application-header,
    .application-info {
        padding-left: 0;
        padding-right: 0;
    }
}

/* ====================================================================================
   FIX: reCAPTCHA MOBILE RENDERING
   Google reCAPTCHA is 304px wide - needs scaling on narrow screens
   The challenge popup is a separate iframe that needs special handling
   ==================================================================================== */

/* reCAPTCHA challenge popup container - this is injected by Google at body level */
/* These selectors target the popup overlay that appears when clicking "I'm not a robot" */
body>div[style*="position: fixed"],
body>div[style*="z-index: 2000000000"],
div[style*="z-index: 2000000000"],
iframe[title="recaptcha challenge expires in two minutes"],
iframe[src*="recaptcha/api2/bframe"],
iframe[src*="google.com/recaptcha"] {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    height: 100vh !important;
    max-width: 100vw !important;
    max-height: 100vh !important;
    z-index: 2147483647 !important;
    overflow: visible !important;
}

/* The inner challenge content needs to be scrollable */
iframe[title="recaptcha challenge expires in two minutes"],
iframe[src*="recaptcha/api2/bframe"] {
    width: 100% !important;
    height: 100% !important;
    border: none !important;
}

@media (max-width: 768px) {

    /* reCAPTCHA container - ensure it's visible and scrollable if needed */
    .toad-captcha-container {
        width: 100% !important;
        max-width: 100% !important;
        overflow: visible !important;
        padding: 10px 5px !important;
        box-sizing: border-box !important;
        display: block !important;
        text-align: center !important;
    }

    /* Scale down reCAPTCHA on very small screens */
    .g-recaptcha,
    .lz-recaptcha,
    [class*="recaptcha"],
    .h-captcha,
    .cf-turnstile {
        transform-origin: center center !important;
        display: block !important;
        margin: 0 auto !important;
        max-width: 100% !important;
        overflow: visible !important;
    }

    /* For screens smaller than reCAPTCHA's native 304px width */
    @media (max-width: 320px) {

        .g-recaptcha,
        .lz-recaptcha,
        [class*="recaptcha"] {
            transform: scale(0.9) !important;
            transform-origin: center center !important;
        }
    }

    /* reCAPTCHA iframe needs to be visible - both checkbox and challenge */
    .g-recaptcha iframe,
    .lz-recaptcha iframe,
    [class*="recaptcha"] iframe {
        max-width: 100% !important;
        overflow: visible !important;
    }

    /* Ensure the outer reCAPTCHA div doesn't get clipped */
    .toad-form-section,
    .toad-form-group {
        overflow: visible !important;
    }

    /* Form container should allow captcha to display */
    .toad-patrol-form-container,
    .toad-patrol-form-box,
    .toad-patrol-form-box.toad-form-wide {
        overflow: visible !important;
    }

    /* Ensure all parent elements allow overflow for reCAPTCHA */
    .volunteer-application,
    .application-card,
    .volunteer-content,
    .site-main,
    #content {
        overflow: visible !important;
    }

    /* Loginizer specific fixes */
    .lz_g-recaptcha,
    .loginizer_captcha {
        width: 100% !important;
        display: flex !important;
        justify-content: center !important;
        overflow: visible !important;
    }

    /* Math captcha should also be visible */
    .toad-captcha-container input[name="loginizer_cap_math"] {
        width: 100% !important;
        max-width: 280px !important;
        margin: 0 auto !important;
    }
}
