Alvaro Morata grabbed a dramatic winner as Spain beat Portugal in Braga to qualify for the Nations League Finals.
The ex-Chelsea striker scored in the 88th minute from close range after Nico Williams headed the ball into his path.
Portugal only needed a point to advance and had chances to score but Liverpool’s Diogo Jota and Manchester United’s Cristiano Ronaldo both missed.
Spain will be joined in June’s finals by Croatia, Italy and the Netherlands, who are expected to host all the games.
Portugal began the night top of Group A2 and will be wondering just how they failed to book their place at next summer’s four-nation tournament.
They had won the inaugural competition in 2018-19 and were the better team for most of the match in northern Portugal.
An excellent pass from Manchester United’s Bruno Fernandes was collected by Jota, who shot at goal but was denied by a fine one-handed save by Spain goalkeeper Unai Simon.
The hosts continued to create the better opportunities with Fernandes hitting the side-netting and Ronaldo having a shot blocked by Simon early in the second half.
Substitute Williams, who plays for Athletic Bilbao, made a difference for Spain and twice had efforts saved by Diogo Costa.
But the Portugal goalkeeper was beaten with only two minutes remaining when Williams’ header went past him with Morata hooking the ball into the roof of the net from close range.
Spain were runners-up in the Nations League Finals in 2021 and now have a chance to win the tournament in June.