RIO DE JANEIRO (Reuters) - A blustery storm, a touch of melancholy and a sense of pride converged at the closing ceremony of the 2016 Olympics on Sunday as Brazil breathed a collective sigh of relief at having pulled off South America's first Games.
After a grueling 17 days, Rio de Janeiro cast aside early struggles with empty venues, security scares and a mysterious green diving pool to throw a huge Carnival-like party.
Samba dancers, singers, drummers and a giant plumed macaw float mixed with hundreds of athletes in the storied Maracana stadium while a final volley of fireworks lit up the night sky.
Brazilians came to the closing ceremony happy, many wearing the canary yellow jersey of the nation's sports teams, having won two late gold medals in their two favorite sports, men's soccer and volleyball.
But Sunday served up tough weather conditions for such a big party. High winds buffeted the Maracana, power briefly went out in the upper part of the stadium, and rain drenched performers and athletes as they entered the ceremony, many with medals hanging around their necks.
To the beat of traditional Brazilian music, Olympians danced and waved their countries' flags to celebrate their place on the world's premier sporting stage.
In the last of 306 medal ceremonies, International Olympic Committee President Thomas Bach draped the gold around the neck of Kenya's Eliud Kipchoge, winner of the men's marathon earlier in the day.
The city handed over the Olympic flag to Tokyo, site of the 2020 Summer Games, and Japanese Prime Minister Shinzo Abe appeared in the stadium dressed as popular video game character Mario, tunneling from Tokyo to Rio.
Bach declared the Rio Games closed and expressed hope that they had left a lasting mark on the metropolitan area of 12 million people.
"These Olympic Games are leaving a unique legacy for generations to come," he said. "History will talk about a Rio de Janeiro before and a much better Rio de Janeiro after the Olympic Games."
In a final symbolic act, the Olympic flame that had burned since Aug. 5 was then extinguished in a downpour of artificial rain.
In the midst of it worst economic recession since the 1930s, Brazil's opening and closing ceremonies relied more on the country's unique talents and natural beauty and less on expensive technology.
On Sunday, there was an ode to the white-clad lacemaking ladies and the forro music of the Northeast that sparked waves of pride among Brazilians.
One of the more stunning moments of the ceremony focused on the ancient art found in the Serra da Capivara National Park - a UNESCO World Heritage site in northeastern Brazil featuring cave paintings, some more than 25,000 years old.
But the beauty was betrayed by Brazil's tough times.
Just this week, the foundation that maintains the park said it could no longer do so because of a lack of funding.
For all the troubles before and during the Games, Rio will surely be remembered for great sporting moments.
There was the remarkable comeback of American swimmer Michael Phelps, who won five golds to reinforce his distinction as the most decorated Olympian of all time.
Jamaica's Usain Bolt drew down the curtain on his brilliant Olympic career by securing a sweep of the sprint titles for a third successive Games. And American gymnast Simone Biles, the U.S. flag bearer in the closing ceremony, kicked off her Olympic run by tying the record of four gold medals in a single Games.
But at times it was hard to focus on the sporting triumphs taking place across the sprawling city.
A low point for Rio came when Ryan Lochte, one of America's most decorated swimmers, said he was robbed at gunpoint. That ignited further security concerns after a series of assaults against government ministers, athletes and tourists.
But Lochte's story quickly unraveled, enraging Brazilians and Americans alike.
Brazilians could nevertheless take heart in the fact that there were no major mishaps or breaches after deadly attacks in Europe and the United States had prompted the biggest security operation in Brazil's history with 85,000 troops.
"Even with all our problems we pulled off a good Olympics. Nothing too bad happened and I'd say it was better than expected," said Nivea Araujo, a Rio resident attending the closing ceremony.
For many in the soccer-mad nation, the best Olympic moments happened in the Maracana, where Brazil defeated Germany in soccer on Saturday and pieced together a widely hailed opening ceremony despite the tight budget.
Rio won the right to host the Games in 2009, when the economy was booming and millions were pushing into the middle class.
"We are in a difficult moment as a country right now, we can't hide that, but the Games were scheduled and I'm glad we could enjoy them," said Alessandro Freitas, also from Rio.
One of the major concerns for Brazilians is what will be the final cost of the Games for a country and how much they actually helped improve the city's infrastructure. Many Rio residents could not afford tickets to events, leaving them feeling on the sidelines of the city's biggest undertaking.
And come Monday, with the Games no longer a distraction, Brazil gets back to its dour reality of dueling political and economic crises. An impeachment vote in coming days could lead to the permanent ouster of suspended President Dilma Rousseff.
Interim President Michel Temer, who was booed at the opening ceremony, decided not to attend the closing event.
Editing by Meredith Mazzilli, Bill Rigby and Peter Rutherford