A goalscoring double in a record-breaking display from Cristiano Ronaldo kept Portugal's Euro 2016 dream alive after a draw with Hungary.
Much discussed in the build-up to the final Group F encounter in Lyon on Wednesday - having blasted Iceland's "small mentality" and then missed a penalty in the goalless draw with Austria - the Real Madrid superstar was already at the centre of attention well before kick-off - even more so after a video emerged of him throwing a television reporter's microphone into a lake earlier in the day.
By being named in Fernando Santos' starting XI, the 31-year-old completed a record 17th appearance in the competition, and his first strike on the night made him the only player to score in four separate Euros.
That effort, a brilliant flick with his trailing foot, hauled Portugal level after Balazs Dzsudzsak's deflected free-kick had given Bernd Storck's team the lead in a pulsating affair.
Earlier, in the first half, Zoltan Gera and Nani had exchanged goals to send the teams in level at 1-1.
Another deflected Dzsudzsak strike then put Hungary in control once again in the 47th minute, before Ronaldo finally took centre stage.
The Portugal captain latched on to Joao Mario's ball to produce his landmark, record-breaking finish before thumping a header home just past the hour mark to seal 3-3 draw.
That saw Portugal finish third in the group, setting up a meeting with Croatia in Lens on Saturday.
Hungary, meanwhile - for whom Akos Elek was denied a winner by the post - advance as section winners.
On a night when he was always likely to be the headline attraction, Ronaldo's frustration - which had been building throughout the tournament - was evident as early as the eighth minute.
When Nani failed to pick him out at the back post after hurtling down the left wing towards the byline, the captain wheeled away and made no effort to hide his displeasure.
His mood worsened 11 minutes later when a brilliant strike from Gera put Hungary ahead.
Headers from Ronaldo and Nani were not enough to clear away a corner, with Gera controlling the ball on his chest before driving a venomous half-volley low to Portugal goalkeeper Rui Patricio's left.
Elek could have doubled Hungary's lead when a tackle on Dzsudzsak resulted in a kind break of the ball, but Rui Patricio rushed off his line to make an important block.
Gabor Kiraly scurried across his line to stop a 30-yard free-kick from Ronaldo bouncing into the bottom-right corner, but the Hungary goalkeeper was beaten three minutes before half-time.
Ronaldo threaded a brilliant pass in behind the defence for Nani, who lashed home a powerful finish at the near post.
That gave Portugal a boost, but they found themselves behind again two minutes after the restart when Dzsudzsak's free-kick deflected off Andre Gomes and beat Rui Patricio.
Once again, though, Ronaldo came to the rescue. He brilliantly used his trailing leg to flick a cross from Joao Mario behind Adam Lang and into the far corner.
Hungary's resolve still was not broken and, after seeing his free-kick blocked by the wall, Dzsudzsak collected the rebound and picked out the far corner of the net to restore his side's shock lead - with the help of a deflection off Nani.
As things stood at that stage, Portugal were heading out, but in the 62nd minute that changed again.
Ronaldo - now in full stride - got away from Roland Juhasz to head substitute Ricardo Quaresma's left-wing cross into the net.
The topsy-turvy second period continued when Gergo Lovrencsics picked out Elek in the box, but the midfielder's first-time strike crashed back off the post.
And so Ronaldo's double proved enough to send Portugal through, although in the end that came as the third-placed side in Group F after Iceland's late winner in a 2-1 victory over Austria ensured the Nordic nation of the runner-up spot.
Key Opta stats:
- Cristiano Ronaldo made his 17th European Championship finals appearance - an outright record.
- Ronaldo also became the first player to score in four different European Championship finals tournaments (2004, 2008, 2012 and 2016).
- Only Michel Platini (9) has scored more goals in European Championship Finals than Ronaldo (8).
- After failing to score with his first 23 shots at Euro 2016, Ronaldo scored with his 24th and 26th in the tournament.
- Zoltan Gera became the second oldest goalscorer in a European Championship finals match (37 years and 61 days old) – behind Ivica Vastic (Austria v Poland, June 2008 - at 38 years and 256 days).
- Nani has scored two goals in his three appearances for Portugal at Euro 2016 - after failing to score in eight appearances at Euro 2008 and Euro 2012 combined.
- Balazs Dzsudzsak is the first player to score two goals from outside the box in the same game at a European Championship finals since Wayne Rooney (England v Croatia in 2004).
- Hungary were the first team to take the lead three times in a single European Championship finals game since Yugoslavia (v Spain in 2000).