Wow sounds like you failed to play the office politics game effectively. Good performance is never enough. You don't play the game and you lose, 100% of the time if you are in management.
Ok now you have to salvage what is left. First, and I know you are going to hate this one, you have to make friends with your new boss. You have to get him to mentor you. You have to help him and not show your resentment. Yeah I know you resent him, you wouldn't be human if you didn't. But this is the time to take the high road. This guy clearly has the office politics down pat, you need to learn from him.
You need to make sure that if team performance drops it can't be blamed on your attitude. So you have to be seen as cooperating with this guy. You also have to let your team know that they will have to cooperate with him too. Whether they like him or not.
They didn't take your management title away, but it may be a matter of time if this guy takes a dislike to you. Consider if now might not be the best time to find another job. You still have the title, so it might look better on resume and in interviews if you move while you still are officially a manager.
In response to the question by the OP in the comments
You said you saw it coming that he was being groomed for this. I think you should have addressed that right off. What allies did you have organizationalllyorganizationally? Did you think that because he didn't know what he was doing ("didn't even understand the function") and he was unpopular with the team that he wasn't a threat? Good political players (espciallyespecially the snake in the grass kind) are often unpopular with the team because they care more about being well thought of by people higher than them. That shoudlshould have been a clue that he had something going on to make that unpoulatrityunpopularity unimportant or that management wasn't aware of it.
Did you rely too much on your good performance and not enough on your network?
Why did they want him? What is it he brings to the table that they think you do not? What is he promising to deliver that he claims you cannot? Did he talk disparinglydisparagingly of you or your team and let it go becasuebecause you knew management thought you were doing a good job.
Sometimes it is hard to win over someone's cousin or college roommate no matter what moves you make.
Are they facing a financial challenge they think he can handle that you can't? Did they like your team management but ultimately, you didn't sell your strategic vision to them? If he is taking over strategically, he almost certainly has sold them some version of the future that they liked more than whatever strategic direction you wanted to go in.
There are many subtle and not so subtle things that could have gone on.