Cristiano Ronaldo scored two penalties to equal the all-time international goalscoring record and help holders Portugal edge into the last 16 of Euro 2020 with a 2-2 draw against France in Budapest on Wednesday.
Portugal will face the world's top-ranked side Belgium in the first knockout round in Seville on Sunday.
Fernando Santos' men moved through as one of the four best third-placed sides, for the second straight Euro, from Group F behind France and Germany, who only just qualified after a dramatic 2-2 draw with Hungary in Munich.
"We did everything I wanted us to do (in the 4-2 defeat) against Germany," Portuguese coach Santos told RTP. "The result was clearly fair."
Ronaldo gave his side the lead just after the half-hour mark with his first spot-kick, before France hit back through Karim Benzema's two goals either side of half-time.
But, with Portugal heading out as it stood with Hungary leading Germany, Ronaldo helped win and then scored their second penalty with half an hour remaining.
The 36-year-old's fifth goal of the group stage extended his record tally of Euro goals to 14 and drew him level with former Iran striker Ali Daei, in his 178th international match.
World champions France will next take on Switzerland in Bucharest on Monday.
"It was a fight, it wasn't easy," France coach Didier Deschamps told TF1. "We tried to win and at the end of the match, obviously, knowing the result of Germany, that allowed us to be first, which is the best place.
"I know very well that a new competition will be starting from the last 16."
The first real chance of the game fell to France forward Kylian Mbappe after quarter of an hour, but the 22-year-old's wait for a Euro goal was extended by Rui Patricio's parried save.
But it was Portugal who made the breakthrough, as they were awarded a 31st-minute penalty as France goalkeeper Hugo Lloris tried to punch the ball away, but instead caught Danilo Pereira in the face, leaving the Portugal midfielder needing treatment on the pitch.
Ronaldo stepped up, sending Lloris the wrong way.
- VAR controversy -
France drew themselves level in controversial circumstances in first-half stoppage time.
Referee Antonio Mateu Lahoz gave a penalty after Mbappe collided with Nelson Semedo while trying to get on the end of a pass into the box.
After a lengthy VAR check, the decision stood despite vociferous protestations from the Portugal players.
Benzema, who had missed his last three penalties, made no mistake from the spot this time, scoring his first goal for France since October 2015, having been recalled for the tournament after a five-and-a-half year exile over his alleged role in a blackmail scandal.
It was also the Real Madrid striker's first goal at a European Championship in his ninth match.
Unsurprisingly, Danilo was substituted at half-time after the blow to his head.
France moved in front for the first time shortly after the restart as Paul Pogba's excellent pass released Benzema to slot in off the post.
The linesman flagged the Frenchman offside, but a VAR check overturned the decision.
France were left without a recognised left-back when Lucas Digne hobbled off six minutes after coming on at the interval for Lucas Hernandez, who was booked in the first half.
But it was right-back Jules Kounde who conceded the second spot-kick for a handball from Ronaldo's cross, and the Juventus star stepped up to level the all-time record, with Lloris guessing incorrectly again.
Portugal knew a defeat would send them out if Hungary beat Germany, and they needed Patricio to make a wonderful double save midway through the second half.
Pogba cleverly created space for a long-range strike which Patricio managed to claw onto the angle of post and crossbar, with the Wolves 'keeper getting up quickly to dive and push away Antoine Griezmann's follow-up effort.
France had a penalty shout turned down in injury time despite another long VAR consultation, after Kingsley Coman went down under a challenge by Bruno Fernandes, who was brought on with 18 minutes to play after being left out from the start.
But with the other match over in Germany, France knew a draw would put them through as group winners and that was how it finished.
© Agence France-Presse