import glob
import re

def update_solid_header():
    html_files = glob.glob('*.html')
    
    old_override_pattern = re.compile(
        r'<style>\s*/\*\s*Internal page solid header override\s*\*/\s*\.fs-header\s*\{\s*background:\s*#[0-9a-fA-F]+\s*!important;\s*\}\s*</style>',
        re.MULTILINE
    )
    
    new_override = """<style>
        /* Internal page solid header override */
        .fs-header,
        .fs-header.scrolled,
        .fs-header.mobile-sticky {
            background: #0c1727 !important;
            backdrop-filter: none !important;
            -webkit-backdrop-filter: none !important;
        }
    </style>"""

    for file_path in html_files:
        if file_path == 'index.html':
            continue
            
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
            
        if '/* Internal page solid header override */' in content:
            # Replace the old pattern
            new_content = old_override_pattern.sub(new_override, content)
            
            # If for some reason the regex didn't match perfectly but the string is there,
            # we might need to handle it. Let's just do a simpler string replace if regex fails.
            if new_content == content and '.fs-header.scrolled' not in content:
                # manual replace
                start_idx = content.find('/* Internal page solid header override */')
                style_start = content.rfind('<style>', 0, start_idx)
                style_end = content.find('</style>', start_idx) + 8
                if style_start != -1 and style_end != -1:
                    new_content = content[:style_start] + new_override + content[style_end:]
        else:
            # Insert before </head>
            new_content = content.replace('</head>', '\n    ' + new_override + '\n</head>')
            
        if new_content != content:
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(new_content)
            print(f"Updated solid header override in {file_path}")

if __name__ == '__main__':
    update_solid_header()
