import os
import re

mobile_menu_css = """
/* ============================================================
   MOBILE NAVIGATION DRAWER — Comprehensive Bulletproof Styles
   ============================================================ */
.fs-mobile-nav {
    display: none;
    position: fixed !important;
    top: 0 !important;
    right: -100% !important;
    width: 100% !important;
    height: 100vh !important;
    height: 100dvh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    background: #0c1727 !important;
    background: rgba(12, 23, 39, 0.98) !important;
    color: #ffffff !important;
    z-index: 999999 !important;
    padding: 1.5rem 1.5rem 6rem 1.5rem !important;
    transition: right 0.35s ease !important;
    box-sizing: border-box !important;
}

.fs-mobile-nav.active {
    display: flex !important;
    flex-direction: column !important;
    right: 0 !important;
    gap: 0.5rem !important;
}

.fs-mobile-close {
    align-self: flex-end !important;
    background: rgba(255, 255, 255, 0.1) !important;
    border: 1px solid rgba(255, 255, 255, 0.2) !important;
    color: #ffffff !important;
    width: 42px !important;
    height: 42px !important;
    border-radius: 50% !important;
    font-size: 1.6rem !important;
    font-weight: 300 !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    margin-bottom: 1rem !important;
    transition: background 0.2s ease, color 0.2s ease !important;
    outline: none !important;
}

.fs-mobile-close:hover,
.fs-mobile-close:focus {
    background: #e6c888 !important;
    color: #0c1727 !important;
}

/* Primary Top-level Items */
.fs-mobile-nav > a,
.fs-mobile-submenu-toggle {
    color: #ffffff !important;
    font-family: 'Instrument Sans', sans-serif !important;
    font-size: 1.15rem !important;
    font-weight: 600 !important;
    text-decoration: none !important;
    background: transparent !important;
    border: none !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08) !important;
    text-align: left !important;
    cursor: pointer !important;
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    width: 100% !important;
    padding: 0.85rem 0.2rem !important;
    margin: 0 !important;
    box-sizing: border-box !important;
    transition: color 0.2s ease !important;
    outline: none !important;
    box-shadow: none !important;
}

.fs-mobile-nav > a:hover,
.fs-mobile-nav > a:focus,
.fs-mobile-nav > a:active,
.fs-mobile-submenu-toggle:hover,
.fs-mobile-submenu-toggle:focus,
.fs-mobile-submenu-toggle:active {
    color: #e6c888 !important;
    background: transparent !important;
    border-color: rgba(255, 255, 255, 0.15) !important;
}

.fs-mobile-submenu-toggle.active {
    color: #e6c888 !important;
    border-bottom-color: transparent !important;
}

.fs-mobile-submenu-arrow {
    display: inline-block !important;
    font-size: 0.8rem !important;
    margin-left: 0.5rem !important;
    color: rgba(255, 255, 255, 0.7) !important;
    transition: transform 0.3s ease, color 0.3s ease !important;
}

.fs-mobile-submenu-toggle.active .fs-mobile-submenu-arrow {
    transform: rotate(90deg) !important;
    color: #e6c888 !important;
}

/* Submenu container */
.fs-mobile-submenu {
    display: none;
    flex-direction: column !important;
    gap: 0.4rem !important;
    padding: 0.5rem 0.8rem 0.8rem 1.2rem !important;
    margin: 0 0 0.5rem 0 !important;
    background: rgba(255, 255, 255, 0.04) !important;
    border-left: 2px solid #e6c888 !important;
    border-radius: 0 8px 8px 0 !important;
}

.fs-mobile-submenu.open {
    display: flex !important;
}

.fs-mobile-submenu a {
    font-family: 'Instrument Sans', sans-serif !important;
    font-size: 1rem !important;
    color: #d6d3d1 !important;
    font-weight: 500 !important;
    text-decoration: none !important;
    padding: 0.45rem 0 !important;
    margin: 0 !important;
    display: block !important;
    border: none !important;
    background: transparent !important;
    transition: color 0.2s ease, padding-left 0.2s ease !important;
}

.fs-mobile-submenu a:hover,
.fs-mobile-submenu a:focus,
.fs-mobile-submenu a.active {
    color: #e6c888 !important;
    padding-left: 0.3rem !important;
}
"""

def update_css_files():
    for css_path in [os.path.join('css', 'styles.css'), os.path.join('css', 'design-system.css')]:
        if os.path.exists(css_path):
            with open(css_path, 'r', encoding='utf-8') as f:
                content = f.read()
            content += "\n" + mobile_menu_css
            with open(css_path, 'w', encoding='utf-8') as f:
                f.write(content)
            print(f"Updated {css_path}")

def update_all_html_files():
    html_files = [f for f in os.listdir('.') if f.endswith('.html')]
    
    style_replacement = """    <style id="custom-menu-active-styles">
        /* Keep toggle highlighted when mega menu is open and highlight active page */
        .fs-desktop-nav a.active,
        .fs-desktop-nav button.active,
        .fs-desktop-nav button.hover-active {
            color: #e6c888 !important;
            font-weight: 800 !important;
        }
        .fs-desktop-nav button.active::after,
        .fs-desktop-nav button.hover-active::after {
            color: #e6c888 !important;
        }
        /* Mobile hamburger toggle: disable all hover fills and color shifts */
        .fs-mobile-toggle,
        .fs-mobile-toggle:hover,
        .fs-mobile-toggle:focus,
        .fs-mobile-toggle:active {
            background: transparent !important;
            background-color: transparent !important;
            border: none !important;
            outline: none !important;
            box-shadow: none !important;
            transform: none !important;
            -webkit-tap-highlight-color: transparent !important;
        }
        .fs-mobile-toggle::before,
        .fs-mobile-toggle::after,
        .fs-mobile-toggle:hover::before,
        .fs-mobile-toggle:hover::after,
        .fs-mobile-toggle:active::before,
        .fs-mobile-toggle:active::after,
        .fs-mobile-toggle:focus::before,
        .fs-mobile-toggle:focus::after {
            display: none !important;
            content: none !important;
            opacity: 0 !important;
        }
        .fs-mobile-toggle span span {
            background: #ffffff !important;
        }
        /* Centered small logo on mobile header */
        @media (max-width: 991px) {
            .fs-header {
                padding: 0.5rem 1rem !important;
            }
            .fs-header-inner {
                position: relative !important;
                display: flex !important;
                align-items: center !important;
                justify-content: space-between !important;
                width: 100% !important;
                min-height: 48px !important;
            }
            .fs-logo {
                position: absolute !important;
                left: 50% !important;
                top: 50% !important;
                transform: translate(-50%, -50%) !important;
                display: flex !important;
                align-items: center !important;
                justify-content: center !important;
                gap: 0.45rem !important;
                margin: 0 !important;
                text-align: left !important;
                z-index: 102 !important;
                max-width: calc(100% - 90px) !important;
                text-decoration: none !important;
            }
            .fs-logo img {
                display: block !important;
                height: 32px !important;
                width: auto !important;
                flex-shrink: 0 !important;
            }
            .fs-logo-text {
                display: flex !important;
                flex-direction: column !important;
                text-align: left !important;
                justify-content: center !important;
            }
            .fs-logo-name {
                font-family: 'Instrument Sans', sans-serif !important;
                font-size: 0.6rem !important;
                font-weight: 600 !important;
                line-height: 1.15 !important;
                color: #ffffff !important;
                white-space: nowrap !important;
                overflow: hidden !important;
                text-overflow: ellipsis !important;
            }
            .fs-logo-sub {
                font-family: 'Instrument Sans', sans-serif !important;
                font-size: 0.46rem !important;
                font-weight: 400 !important;
                line-height: 1.15 !important;
                color: rgba(255, 255, 255, 0.85) !important;
                white-space: nowrap !important;
                overflow: hidden !important;
                text-overflow: ellipsis !important;
            }
            .fs-header-actions {
                display: flex !important;
                align-items: center !important;
                z-index: 103 !important;
            }
        }
        @media (max-width: 440px) {
            .fs-logo img {
                height: 28px !important;
            }
            .fs-logo-name {
                font-size: 0.52rem !important;
            }
            .fs-logo-sub {
                font-size: 0.4rem !important;
            }
        }
        """ + mobile_menu_css + """
    </style>"""

    for fname in html_files:
        with open(fname, 'r', encoding='utf-8') as f:
            c = f.read()

        c = re.sub(r'<style id="custom-menu-active-styles">[\s\S]*?</style>', style_replacement, c)

        with open(fname, 'w', encoding='utf-8') as f:
            f.write(c)

    print(f"Updated custom-menu-active-styles in all {len(html_files)} HTML files.")

if __name__ == '__main__':
    update_css_files()
    update_all_html_files()
