Cristiano Ronaldo refused to let his World Cup dreams slip away as the Portugal captain dragged his side level from the penalty spot, before substitute Goncalo Ramos struck in the fourth minute of stoppage time to complete a stunning 2-1 comeback win over Croatia in the Round of 32 in Toronto on Friday.
It capped an extraordinary night at BMO Field, one that swung from disallowed goals to VAR drama and finally delivered Portugal a Round of 16 date with Spain.
After a barren first half, which saw Portugal dominate possession and squander a number of half-chances, Croatia broke the deadlock in the 53rd minute.
Kovacic found space and spread play to Sucic, who worked the ball to Stanisic. His cross found Perisic at the far post, and the winger drilled a low finish into the bottom corner.
Portugal responded through Ronaldo. Nuno Mendes won a penalty after Vlasic tugged Veiga's shirt at a corner, and Ronaldo stepped up to send Livakovic the wrong way in the 68th minute.
It was Ronaldo's first goal in a World Cup knockout match, ending a wait that had begun in 2006.
Minutes before the qualiser, Portugal coach Roberto Martinez had taken off four players. And after Ronaldo found the back of the net, he made the bold call of withdrawing his captain in the 81st minute.
Croatia sensed an opportunity and pushed forward, hitting the post through Kovacic and forcing saves from Diogo Costa.
Portugal held firm, and Martinez's substitution paid off in the fourth minute of stoppage time. Rafa Leao floated a cross from the left, and Ramos rose between two defenders to head Portugal in front.
Croatia thought they had responded immediately. Perisic's ball into the box eventually fell to Gvardiol, who turned it home, but the goal was ruled out after a VAR review found Matanovic offside in the build-up.
The final whistle confirmed Portugal's passage to the last 16, ending Croatia's run at this World Cup. Luka Modric, appearing in his fifth tournament, brought the curtain down on his international career at the finals.
Both sides had earlier seen goals chalked off. Ronaldo thought he had scored in the 60th minute, chipping Livakovic after controlling a diagonal ball, but the offside flag denied him. Sucic also had an effort ruled out for offside in the 80th minute.
Portugal dominated possession for long spells but struggled to create clear openings against a well-organised Croatia side. Diogo Costa was called into action just once in the first half, saving from Bruno Fernandes' follow-up after his own initial effort was blocked.
Ronaldo, at 41, became the oldest player to appear in a World Cup knockout match. Croatia's starting XI, with an average age of over 30, were the oldest in a non-group-stage World Cup fixture since their own side in 1998.
Portugal will now turn their attention to Spain in the Round of 16, while Croatia's remarkable run of reaching the last four in three successive tournaments comes to an end.