Mikel Merino's last-gasp header sent Spain into the semi-finals of Euro 2024 as La Roja beat hosts Germany 2-1 after extra time.
Florian Wirtz's 89th-minute strike had cancelled out Dani Olmo's calm finish in a frantic encounter in Stuttgart on Friday.
Yet having had the best chances of extra time, Germany became the first Euros hosts to lose a quarter-final when Merino brilliantly headed home Spain's latest ever goal in the competition in the 119th minute.
Niclas Fullkrug's header was just inches away from rescuing Germany and setting up penalties, but Luis de la Fuente's team, who had Dani Carvajal sent off late on, held on to tee up a clash with either Portugal or France.
Pedri's early injury saw Olmo come on, and the substitute sparked a slow-burner of a contest into life in the 51st minute.
After seeing Alvaro Morata lash over from close range, Olmo made no mistake when he side-footed home from Lamine Yamal's cutback.
Simon made a fantastic save from Robert Andrich's drive, before the post came to Spain's salvation when Niclas Fullkrug's effort clipped off the right-hand upright.
Germany should have been level when Simon made a calamitous clearance soon after, but with the goal at his mercy, Havertz chipped over.
But Havertz's blushes were spared by Wirtz, who drilled in off the post after latching onto Joshua Kimmich's knockdown.
Wirtz had the best chance of the first half of extra time, sending a low effort just wide, before chaos broke out.
Germany saw a penalty appeal turned down after Jamal Musiala hit Marc Cucurella's arm, and Simon saved a fantastic Fullkrug header.
With penalties beckoning, Spain broke clear, and La Roja were in ecstasy as Merino planted in from Olmo's inch-perfect cross.
Ferran Torres' poor finish moments later could have proved costly had Fullkrug's attempt been half a yard to the left, but it was not to be for Germany, even though Spain were reduced to 10 for the final minute after Carvajal picked up his second booking.
Spain call on the super-subs as Lamal sparkles again
Olmo is now the first Spain player to score two goals as a substitute at a single edition of the Euros, and third at any major tournament, after Fernando Morientes at the 2002 World Cup and Morata at the 2022 edition of that competition.
The RB Leipzig attacker is also the second player to have scored in two consecutive knockout stage appearances for Spain at the European Championships, after Chus Pereda way back in 1964.
And Merino's winner meant Spain have now scored two goals from substitutes in a single Euros match for just the second time, after the Euro 2012 final against Italy.
There has been no such scoring luck so far for Yamal at Euro 2024, with the youngster having had 13 shots without finding the back of the net (only Cristiano Ronaldo, with 20, has had more attempts and not scored at this tournament).
However, he again showcased his creative talents, laying on three chances and taking his tally of assists for the tournament to three. He is the first-ever teenager to record three assists at a single edition of the Euros or World Cup.
Musiala cannot find the magic touch
Jamal Musiala went into Friday's tie having scored in three of Germany's four previous games, and aiming to move clear at the top of the Euro 2024 scoring charts and match Wayne Rooney (at Euro 2004) as the only player to score four goals at a single edition of the tournament while aged 21 or younger.
Yet the Bayern Munich playmaker struggled to wield his usual influence on proceedings, creating only one chance, and though his fellow youngster Wirtz shined, it was not enough for the hosts.
There will be more major tournament opportunities for Musiala and Wirtz, but at the other end of the age spectrum, Toni Kroos made his final appearance before retirement. The 34-year-old finished the match with 83 passes attempted, the most on the pitch.