But the chances did not turn into goals. Gabriel Jesus clanked a header of the crossbar. The rebound ended up at the feet of Coutinho near the 18-yard line. His shot was headed for the open left side of the net, but defender Cristian Gamboa got his shin in the way at the last moment. Minutes later, Neymar was handed a sitter from seven yards, but sent it sailing above the crossbar, and Coutinho hit another open strike from in front of the goal right into at the arms of Costa Rica goalkeeper Keylor Navas.

As the minutes ticked toward the end of regulation, Costa Rica, disciplined and organized in a five-man back line, held firm. For long stretches, the Ticos kept every player behind the ball, frustrating the surging Brazilians

“I don’t know what else we could have done,” Costa Rica Manager Oscar Ramirez said. “We were playing against the second-best team in the world. They have great players. Considering what I have, it was reasonable what we did.”

It nearly worked. Kuipers scolded Neymar to stop yapping at him about uncalled fouls, then delivered a yellow card when Neymar slammed the ball with his hand after yet another didn’t go his way. After one open shot from 20 yards sailed into the stands, Neymar hid his face in his shirt, devoid of the usual swagger he carries on the world’s biggest stage.

Then, just in the nick of time, Brazil did turn into some version of Brazil: not the fancy one but the one that nearly always gets the results it needs. Marcelo’s pass in from the right in the first minute of stoppage time found Roberto Firmino’s head and then Gabriel Jesus’s foot, and when it dropped into an open patch of grass right in front of Navas, Coutinho dashed into the space and slammed the ball past him.

In the final minute, Neymar got his revenge, turning home a pass from the substitute Douglas Costa behind a helpless Navas. When the final whistle blew moments later, he collapsed to his knees in tears at the center of the field, covering his eyes again, this time overcome by the emotion of the narrow escape.

Tite didn’t escape unscathed, either. Rushing the field to celebrate the first goal, he got tripped up and tumbled to the grass.