import os
import re

css_inject = """
/* ============================================================
   NAV MENU & DROPDOWN HOVER BRIGHTNESS & VISIBILITY FIX
   ============================================================ */
.fs-desktop-nav a,
.fs-desktop-nav button,
.fs-desktop-nav .fs-mega-toggle,
button.fs-mega-toggle {
    color: #ffffff !important;
    background: transparent !important;
    background-color: transparent !important;
    border: none !important;
    outline: none !important;
    box-shadow: none !important;
    font-family: 'DM Sans', sans-serif !important;
    font-size: 0.88rem !important;
    font-weight: 600 !important;
    text-decoration: none !important;
    cursor: pointer !important;
    transition: color 0.2s ease !important;
    opacity: 1 !important;
}

.fs-desktop-nav a:hover,
.fs-desktop-nav button:hover,
.fs-desktop-nav .fs-mega-toggle:hover,
.fs-desktop-nav .fs-mega-toggle:focus,
.fs-desktop-nav .fs-mega-toggle.active,
button.fs-mega-toggle:hover,
button.fs-mega-toggle:focus {
    color: #e6c888 !important;
    background: transparent !important;
    background-color: transparent !important;
    opacity: 1 !important;
}

.fs-desktop-nav .fs-mega-toggle::after,
button.fs-mega-toggle::after {
    content: ' ▼' !important;
    font-size: 0.6rem !important;
    margin-left: 0.4rem !important;
    color: #ffffff !important;
    transition: color 0.2s ease, transform 0.2s ease !important;
    display: inline-block !important;
}

.fs-desktop-nav .fs-mega-toggle:hover::after,
.fs-desktop-nav .fs-mega-toggle:focus::after,
button.fs-mega-toggle:hover::after,
button.fs-mega-toggle:focus::after {
    color: #e6c888 !important;
}

/* Dropdown menu item styling */
.fs-mega-menu {
    background: #0c1727 !important;
    border: 1px solid rgba(230, 200, 136, 0.25) !important;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4) !important;
    border-radius: 8px !important;
}

.fs-mega-col a {
    color: #d6d3d1 !important;
    font-size: 0.92rem !important;
    font-weight: 500 !important;
}

.fs-mega-col a:hover {
    color: #e6c888 !important;
    background: rgba(230, 200, 136, 0.12) !important;
}

/* ============================================================
   LOGO & LOGO TITLE HOVER LOCK (NO HOVER COLOR CHANGE OR ANIMATION)
   ============================================================ */
.fs-logo,
.fs-logo:hover,
.fs-logo:focus,
.fs-logo:active {
    text-decoration: none !important;
    background: transparent !important;
    background-color: transparent !important;
    transform: none !important;
    box-shadow: none !important;
    opacity: 1 !important;
    cursor: pointer !important;
}

.fs-logo *,
.fs-logo:hover *,
.fs-logo:focus *,
.fs-logo:active *,
.fs-logo span,
.fs-logo:hover span,
.fs-logo-name,
.fs-logo-sub,
.fs-logo:hover .fs-logo-name,
.fs-logo:hover .fs-logo-sub {
    color: #ffffff !important;
    transform: none !important;
    text-decoration: none !important;
    transition: none !important;
}

.fs-logo img,
.fs-logo:hover img,
.fs-logo:focus img {
    transform: none !important;
    transition: none !important;
    filter: none !important;
    animation: none !important;
}
"""

# 1. Update css/styles.css
styles_css_path = r'css\styles.css'
if os.path.exists(styles_css_path):
    with open(styles_css_path, 'r', encoding='utf-8') as f:
        c = f.read()
    
    # Fix line 3003:
    c = c.replace(
        'a:not(.footer-social-btn):not([style*="color: #a8a29e"]):not([style*="color: #d6d3d1"]):not([href^="mailto"]):not([href^="tel"]):hover span',
        'a:not(.footer-social-btn):not(.fs-logo):not([style*="color: #a8a29e"]):not([style*="color: #d6d3d1"]):not([href^="mailto"]):not([href^="tel"]):hover span'
    )
    # Fix line 3010:
    c = c.replace(
        'a.btn-chocolate, a.btn-outline-dark, .header-cta-btn, button {',
        'a.btn-chocolate, a.btn-outline-dark, .header-cta-btn, button:not(.fs-mega-toggle):not(.slider-btn):not(.footer-accordion-toggle):not(.fs-mobile-toggle):not(.fs-mobile-close) {'
    )
    
    c += "\n" + css_inject
    with open(styles_css_path, 'w', encoding='utf-8') as f:
        f.write(c)
    print("Updated css/styles.css")

# 2. Update css/design-system.css
ds_css_path = r'css\design-system.css'
if os.path.exists(ds_css_path):
    with open(ds_css_path, 'r', encoding='utf-8') as f:
        c = f.read()
    c += "\n" + css_inject
    with open(ds_css_path, 'w', encoding='utf-8') as f:
        f.write(c)
    print("Updated css/design-system.css")

# 3. Update all 33 HTML files in <style id="custom-menu-active-styles"> or <style>
html_files = [f for f in os.listdir('.') if f.endswith('.html')]
for fname in html_files:
    with open(fname, 'r', encoding='utf-8') as f:
        c = f.read()
        
    if 'id="custom-menu-active-styles"' in c:
        c = re.sub(
            r'(<style id="custom-menu-active-styles">)',
            r'\1\n' + css_inject + '\n',
            c
        )
    elif '<head>' in c:
        c = c.replace('</head>', f'<style>\n{css_inject}\n</style>\n</head>', 1)
        
    with open(fname, 'w', encoding='utf-8') as f:
        f.write(c)

print(f"Injected nav & logo hover rules across all {len(html_files)} HTML files.")
