The government’s top lawyer has resigned as it prepares for a High Court battle over the under-16 social media ban.