Each rewrite here must return exactly the same rows as the slow version; the check compares results and then looks at the shape. Run EXPLAIN on both versions as you go, so you can see what changed in the plan.
Rules of thumb from the unit: keep columns bare in filters, filter and aggregate early, use EXISTS for "has any", and name only the columns you need.