RIO DE JANEIRO — Dark clouds hovered over Maracana Stadium at dusk, with a sudden wind tugging at flags and whipping the Olympic flame.
Then a drenching rain began to fall.
The storm that intruded on Sunday night’s Closing Ceremony at the Summer Olympics befit a mega-sporting event that, over the last few weeks, had blended spectacle with more than a few glitches and negative headlines.
“A Games in the middle of reality,” Thomas Bach, president of the International Olympic Committee, had called them.
There were historic performances by some of the world’s greatest athletes, cheered on — and sometimes booed — by crowds exhibiting Brazil’s characteristic exuberance.
There were also reports of street crime, logistical gaffes and venue failures suggesting Brazil was not quite ready for prime time.
It was all part of the IOC’s gamble in coming to South America for the first time. The samba dancers and pop musicians who soldiered through Sunday’s inclement weather, and the fireworks that illuminated the gloomy sky at the end, embodied the host city’s determination.
“Having the Games in Rio was a challenge,” Carlos Nuzman, president of the Rio 2016 organizing committee, told the crowd. “But the challenge was a success.”
Olympic leaders often speak of the power of sport, as if the Games can cure all ills — or, at least, spur economic development — for any city willing to spend the billions required to stage them.
In Rio’s case, optimism had waned in recent months, overshadowed by concerns over political upheaval, the Zika virus and raw sewage flowing into the ocean where sailors, rowers and open-water swimmers would compete.
IOC vice president John Coates called them “the most difficult Games we have ever encountered.”
The Russian doping scandal created another distraction, as IOC officials wrestled over whether to ban the entire contingent, then chose to let the international federations that govern each sport decide on a case-by-case basis.
Not until the day before the Opening Ceremony was the Russian team finalized, with more than 100 athletes excluded.
Yet, as often happens, the start of competition elbowed much of the bad news aside.
Michael Phelps secured a place among history’s greatest Olympians by raising his career medal count to 28, then handed the stage to Jamaican sprinter Usain Bolt, who swept the 100, 200 and 400-meter relay for an unprecedented third straight Games.
The Americans dominated overall with 121 medals, their biggest haul ever, not counting the boycotted 1984 Los Angeles Games or the 1904 St. Louis Games, when hardly anyone else showed up.
The U.S. total was boosted by breakout stars such as Simone Manuel — the first African-American to win an individual swimming gold — and gymnast Laurie Hernandez.
“Every so often I’ll stop and look at my coach and be like, ‘Oh my goodness, we’re at the Olympics. This is insane,’ ” Hernandez marveled on her way to gold and silver.
The stories that reached beyond sport were equally moving as a team of refugee athletes, sponsored by the IOC, made it to the world stage from violence-stricken countries such as Syria, South Sudan and the Democratic Republic of the Congo.
In the women’s 5,000 meters, two runners collided and fell at the rear of the pack. Abbey D’Agostino of the U.S., too injured to continue, helped Nikki Hamblin of New Zealand to her feet and urged her to finish.
“I’m so grateful for Abbey doing that for me,” Hamblin said. “That girl is the Olympic spirit right there.”
Even some of the larger pre-Games issues quickly fell by the wayside.
The World Health Organization proved correct in predicting that Zika would subside with a dwindling mosquito population in the Southern Hemisphere’s winter. It was a non-factor in Rio.
So was the pollution — at least for athletes who shrugged off the mention of water-borne superbugs. But reality intruded in other ways.
The Australian team initially balked at moving into the Olympic Village when they found exposed wiring and leaky pipes in their rooms. Next came reports of athletes and coaches robbed and an IOC member from Ireland arrested in an alleged ticket-scalping scandal.
A steady flow of logistic and venue hiccups suggested the Brazilians could not fully accommodate the flood of athletes, officials and tourists.
Stray bullets whizzed through the equestrian venue in the Deodoro competition cluster, which overlaps a military base. Fans waited in long lines outside arenas, the concession stands often ran out of food and a bus took swimmers to the wrong venue.
Apologies became a recurring theme at the daily news update, where Rio 2016 spokesman Mario Andrada found himself scrambling to explain some of the more quizzical missteps.
When the diving pool turned bright green from algae, he said: “Chemistry is not an exact science.”
All of this played out against the backdrop of a nation mired in recession and a populace lacking basic services. The economy forced Rio 2016 into steep budget cuts, which explained at least some of the operational hiccups.
Yet the people of Rio displayed passion over the last few weeks, making enough noise to compensate for arenas left half full by sluggish ticket sales. As Bach said: “They turned the great competition into a party for everyone.”
In the end, the host country got a gold-medal victory over rival Germany in soccer and a sense of accomplishment at having staged the Games — warts and all — amid political and economic crisis.
At the Closing Ceremony, after the athletes marched and a woman in an elaborate headdress performed a tribute to Carmen Miranda, Bach stepped to the podium to describe Rio as “the marvelous Games in the marvelous city.”
A day earlier, pressed by reporters, his assessment had been more tempered.
“This was very good for everybody … to face the challenges and put sport into perspective,” he said. “To see where sport can help and unite people but also to understand that there are limitations to the power of sport.”