Spain will play Italy in the last 16 of Euro 2016 after a late Croatia winner condemned them to second in Group D.
Spain took the lead when Cesc Fabregas dinked the ball over keeper Danijel Subasic and Alvaro Morata tapped in.
An Ivan Rakitic lob hit the woodwork for Croatia, who equalised when Nikola Kalinic turned in Ivan Perisic's cross.
Subasic saved a Sergio Ramos penalty and Perisic's late winner gave Croatia, who finished top of the group, a tie against a third-placed qualifier.
Perisic lashed in an 87th-minute shot in a thrilling match to inflict Spain's first defeat in 15 games at the European Championship since losing to Portugal in Euro 2004.
Croatia will now play on Saturday in Lens, while Spain's mouth-watering tie against Italy - a repeat of the 2012 final - will take place on Monday in Paris.
Spain's defence looks suspect
This was an eagerly anticipated game because of the quality at Croatia's disposal and the type of test they were expected to give the reigning champions.
From that point of view, Croatia pressed and pushed further up than Spain's previous opponents - Turkey and the Czech Republic - and, despite going behind, caused plenty of moments of uncertainty at the back for Vicente del Bosque's side.
Spain centre-back Ramos gave the ball away early on and Perisic had a shot palmed away by keeper David de Gea.
De Gea was then dispossessed by Kalinic and the ball fell to Rakitic, whose chip hit the crossbar and the post before bouncing the wrong side of the post for Croatia.
Spain again dominated possession but their resistance was finally broken when they conceded their first goal in 735 minutes at the Euros as Kalinic stole in front of Ramos to turn in Perisic's left-wing cross.
A weak punch from De Gea led to him having to save Tin Jedvaj's shot before Marko Pjaca sent an overhead kick wide.
Spain's keeper and their defence looked vulnerable when exposed before they were caught out on the counter-attack, with Perisic's strike beating De Gea at his near post.
Croatia's strength in depth
Croatia coach Ante Cacic made five changes while his team were without key midfielder Luka Modric and striker Mario Mandzukic through injury.
Nevertheless, they managed to provide further evidence they pose a danger to any team in the tournament by not only showing character to come back from a goal down but discipline in defence and a threat when going forward.
Perisic and captain Darijo Srna are key to their play with their energetic running on the flanks and ability to whip in dangerous crosses, like the one which led to the equaliser.
Croatia, who left Ramos unmarked twice from crosses only for him to fail to get his headers on target, were angered by the penalty award for Sime Vrsaljko's challenge on David Silva and will have felt justice was done when Subasic, who was three yards off his goal-line, saved.
In another plus for Croatia, there was no trouble from their fans following the crowd unrest and throwing of flares on to the pitch which marred their draw with Czech Republic.
'We have to rise to the situation'
Spain manager Vicente del Bosque: "The players were sat in silence. They don't enjoy losing. We had the game under control, I'm not going to defend our performance excessively but I think we played quite well.
"We didn't hold on to the ball as well as we should have [before the winning goal] and weren't ready for the counter attack. But we're still in the competition, we are not on the path we wanted to be but we have to rise to the situation."
Croatia boss Ante Cacic: "This team is like our nation. The players have shown how to represent your country and how to behave.
"I wanted to play the next match with really fresh players. It's a huge achievement, and now we can be even more self-confident."
Pundit analysis
Former Wales forward and BBC Radio 5 live summariser John Hartson: "I think Croatia have deserved it, Spain have played some good football, easy on the eye, but they were not clinical enough in front of goal."
The stats you need to know
This is the first time Spain have ever lost a game in a Euro finals tournament after being ahead.
Sergio Ramos is the first Spanish player to miss a penalty at a European Championship since Raul v France in 2000.
Cesc Fabregas has been involved in more goals than any other Spaniard at Euro finals (three goals, five assists).
(BBC)
www.ann.az
Follow us !