$map='gallery_replacements.txt'; $js='js\main.js'; if(-not (Test-Path $map)){ Write-Error "Missing $map"; exit 1 }
$text = Get-Content $js -Raw
$lines = Get-Content $map | Where-Object { $_ -and $_ -notmatch '^\s*$' -and $_ -notmatch '^```' }
$changes = 0
foreach($l in $lines){ $parts = $l -split '\|'; if($parts.Count -lt 4){ continue }
    $id=$parts[0]; $idx=$parts[1]; $old=$parts[2]; $new=$parts[3];
    if([string]::IsNullOrWhiteSpace($new)){ continue }
    $esc = [regex]::Escape($old)
    if($text -match [regex]::Escape($old)){
        $text = [regex]::Replace($text,$esc,$new)
        $changes++
    }
}
Set-Content -Path $js -Value $text -Force
Write-Output "Applied $changes replacements to $js"