It does work, but now it also displays items from the pre-selected categories. With this logic the unselected checkboxes are not treated as exclusions.
With the previous logic everything unselected is treated as exclusion.
I believe you should perhaps use the current logic, but set all checkboxes to unselected by default – that way the search will ignore the category filter when nothing is checked. Once the user starts checking the checkboxes, it will start filtering.