Gdansk - Substitute Jesus Navas tapped home two minutes from time to snatch a 1-0 victory over Croatia and send holders Spain through to the last eight of Euro 2012 as Group C winners.
Croatia, who battled hard against the world and European champions, went out as Italy finished second thanks to a 2-0 win over Ireland. Spain will now face the runners-up in a Group D featuring France, England, Ukraine and eliminated Sweden.
Spain topped the group on seven points, with Italy on five and Croatia on four, while Ireland finished with zero.
Spain's late winner was tough on Croatia and they probably had the best chance of the game before Spain's goal when Iker Casillas brilliantly blocked Ivan Rakitic's close-range header in the 59th minute.
However, as the game opened up in the latter stages, Spain substitute Cesc Fabregas and fellow midfielder Andres Iniesta were able to carve open the Croatian defence and Iniesta squared the ball for Navas to slid home into an empty net.
On a warm and breezy night at Gdansk's amber-coloured PGE Arena, Spain quickly seized control of possession in the first competitive meeting between the two sides.
The Croats, with in-form Mario Mandzukic operating as a lone target man up front, looked to catch Spain on the break but had to wait until the 25th minute for their first goal attempt.
Playing in unfamiliar light-blue shirts, Spain struggled to create any clear chances and centre backs Sergio Ramos and Gerard Pique had probably their best two efforts of the first half with long-range strikes.
Spain coach Vicente del Bosque fielded an unchanged side from last week's 4-0 drubbing of Ireland, with Fernando Torres leading the line and Iniesta and David Silva operating on either flank.
Torres had a quiet game and was replaced by Fabregas, who operated as a roving forward in Spain's 1-1 draw with Italy and had netted the equaliser.
Del Bosque's Croatian counterpart Slaven Bilic, who is joining Lokomotiv Moscow after the finals, switched to a more defensive formation, with in-form striker Mario Mandzukic ahead of an offensive midfield trio led by playmaker Luka Modric.
It was Modric who conjured a superb pass with the outside of his foot to create the chance for Rakitic but he was otherwise well shackled by the Spanish midfield and defence. - Reuters