Critical light-matter entanglement at cavity mediated phase transitions