Ivan Perisic's late goal helped Croatia stun Spain 2-1 and tip their opponents to the top spot in Euro 2016 Group D on Tuesday.
After Alvaro Morata gave Spain an early lead, Croatia hit back through Nikola Kalinic before the break, and Sergio Ramos had a penalty saved for Spain in the second half before Perisic scored three minutes from time.
The result means Spain finished second and will have to play Italy in the round of 16 on Monday at the Stade de France.
Spain scored with their first opportunity of the game in the seventh minute as David Silva played in Cesc Fabregas, who chipped Croatia keeper Danijel Subasic.
The ball appeared to be heading into the back of the net anyway, but Morata got a touch to it on the goal line to record his third of the tournament.
Spain nearly doubled their lead within two minutes but Nolita's volleyed effort following a Silva cross went just wide.
But Croatia held their own against the defending champions and soon had chances of their own, with Kalinic's shot from distance forcing David De Gea to make a good save.
They then came within inches of an equaliser when De Gea gave the ball away when off his goal line, and Ivan Rakitic's lofted shot toward the back post struck both the underside of the bar and the post. Goal-line technology showed a sliver of the ball had crossed the line, but it stayed out.
Croatia kept up the pressure throughout the first half, though Spain had a good chance in the 44th minute when Fabregas found an open Morata at the top of the box, but the striker's first touch was too heavy and Subasic gathered.
And before the break Croatia were finally rewarded for their efforts when Perisic's cross from the wing flew just over Rakitic's head but fell perfectly onto the outside of Kalinic's foot to flick in at the near post for the equaliser.
In the second half, De Gea nearly gifted Croatia a goal when he came for a cross but barely got a touch to it, though he recovered in time to block Tin Jedvaj's shot, and Marko Pjaca's overhead attempt sailed wide.
Spain looked for a winner and nearly found it from a corner, but Ramos' flicked header was beyond the post.
The crucial moment came when Spain were awarded a penalty in the 70th minute after Sime Vrsaljko clipped Silva in the box as they chased a lobbed Iniesta pass.
It was a debatable call and the furious Croatian defenders were vindicated when Ramos hit his penalty too close to Subasic and the keeper saved.
And Croatia made Spain pay with a winner in the 87th minute.
Croatia broke out on the counter-attack after Aritz Aduriz's shot was blocked at the top of the box, Kalinic carried the ball upfield and laid off for Perisic, whose shot from the angle beat De Gea at his near post.
The Group D winners is still to be determined, as Croatia will next play on Saturday in Lens against either Slovakia or the third-place team from Group E or Group F.