Match ends, Croatia 2, Spain 1.
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.
Man of the match - Ivan Perisic

'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).
Line-ups
Croatia
Formation 4-2-3-1
- 23Subasic
- 11SrnaBooked at 70mins
- 5Corluka
- 6Jedvaj
- 2VrsaljkoBooked at 70mins
- 15RogBooked at 29minsSubstituted forKovacicat 82'minutes
- 19Badelj
- 4PerisicBooked at 88minsSubstituted forKramaricat 90+4'minutes
- 7Rakitic
- 20PjacaSubstituted forCopat 90+2'minutes
- 16N Kalinic
Substitutes
- 1Vargic
- 3Strinic
- 8Kovacic
- 9Kramaric
- 10Modric
- 12L Kalinic
- 13Schildenfeld
- 14Brozovic
- 17Mandzukic
- 18Coric
- 21Vida
- 22Cop
Spain
Formation 4-3-3
- 13de Gea
- 16Torres Belén
- 3Piqué
- 15Ramos
- 18Alba
- 10FàbregasSubstituted forThiago Alcántaraat 84'minutes
- 5Busquets
- 6Iniesta
- 21Silva
- 7MorataSubstituted forAdurizat 67'minutes
- 22NolitoSubstituted forBrunoat 60'minutes
Substitutes
- 1Casillas
- 2Azpilicueta
- 4Bartra
- 8Koke
- 9Vázquez
- 11Pedro
- 12Bellerín
- 14Thiago Alcántara
- 17San José
- 19Bruno
- 20Aduriz
- 23Rico
- Referee:
- Björn Kuipers
- Attendance:
- 37,245
Match Stats
- Possession
- Home32%
- Away68%
- Shots
- Home13
- Away15
- Shots on Target
- Home4
- Away3
- Corners
- Home3
- Away7
- Fouls
- Home10
- Away11
Live Text
Post update
Full Time
Second Half ends, Croatia 2, Spain 1.
Post update
Offside, Croatia. Ivan Rakitic tries a through ball, but Duje Cop is caught offside.
Substitution
Substitution, Croatia. Andrej Kramaric replaces Ivan Perisic.
Substitution
Substitution, Croatia. Duje Cop replaces Marko Pjaca.
Post update
Foul by Sergio Busquets (Spain).
Post update
Sime Vrsaljko (Croatia) wins a free kick in the defensive half.
Post update
Sergio Ramos (Spain) wins a free kick in the defensive half.
Post update
Foul by Nikola Kalinic (Croatia).
Booking
Ivan Perisic (Croatia) is shown the yellow card for excessive celebration.
Goal!
Goal! Croatia 2, Spain 1. Ivan Perisic (Croatia) left footed shot from the left side of the box to the centre of the goal. Assisted by Nikola Kalinic following a fast break.
Post update
Attempt blocked. Aduriz (Spain) right footed shot from outside the box is blocked. Assisted by Sergio Busquets.
Substitution
Substitution, Spain. Thiago Alcántara replaces Cesc Fàbregas.
Post update
Foul by Cesc Fàbregas (Spain).
Post update
Darijo Srna (Croatia) wins a free kick in the defensive half.
Substitution
Substitution, Croatia. Mateo Kovacic replaces Marko Rog.
Post update
Delay over. They are ready to continue.
Post update
Delay in match Vedran Corluka (Croatia) because of an injury.
Post update
Attempt missed. Sergio Ramos (Spain) header from the centre of the box is too high. Assisted by David Silva with a cross following a corner.
Post update
Corner, Spain. Conceded by Darijo Srna.