Spain ended a 21-year wait to reach the men's Olympic soccer semi-finals as substitute Rafa Mir scored a hat-trick to secure a 5-2 victory over Ivory Coast in extra time on Saturday.
Winger Max Gradel appeared to have secured Ivory Coast's place in the final four when he made it 2-1 with a stoppage-time strike, only for Mir to score almost immediately at the other end and force extra time.
Ivory Coast's fate was sealed when defender Eric Bailly, who scored their opening goal inside 10 minutes, turned from hero to villain as he conceded a penalty with a bizarre handball inside the box.
Mikel Oyarzabal slotted the ball home from the spot in the 98th minute before Mir added two more goals past an exhausted Ivory Coast defence to put the result beyond doubt.
Victory ensures Spain return to the semi-finals for the first time since claiming silver at the 2000 Sydney Games.
Spain, the last European team to win a men's football Olympic gold in 1992, will face the winner of the quarter-final between hosts Japan and New Zealand.