– The downpour that drenched Sunday’s Closing Ceremony of the Rio Olympics didn’t stop the show. Thousands still filled Maracana Stadium to sing along with Brazilian balao music, shimmy in their seats to samba rhythms and applaud thousands of athletes who put on ponchos and splashed onto the arena floor.

In that respect, the weather had much in common with the empty seats, the hazy green water in the diving and water polo pools, the constant traffic jams, the booing by Brazilian fans and a string of organizational glitches. It threw some cold water on the proceedings, but it couldn’t douse the Olympic flame. The cauldron was extinguished as planned near the end of Sunday’s program, after International Olympic Committee President Thomas Bach declared, “These were marvelous Olympic Games, in the Marvelous City.”

Bach said Saturday he had no regrets about bringing the Olympics to Rio, despite problems that also included several armed robberies and assaults and the smashing of windows on a media bus. Sunday, at the close of the first Olympics in South America, he again emphasized the importance of diversity to the Olympic movement.

When the ceremony ended with a Carnival-style dance number, the crowd once again demonstrated the best side of the Rio Games: the unabashed joy of Brazil’s people. Nearly everyone in an announced crowd of 41,000 leapt out of their seats, dancing and singing along to Rio’s anthem, one that Bach referenced in his farewell: Cidade Maravilhosa, the marvelous city.

“You will have a place in our hearts forever,” Bach told the city. “History will talk about Rio before, and a much better Rio after the Olympic Games. Brazil, we love you.”

The joyful crowd at Maracana radiated pride in Rio’s ability to pull off the Games. The Olympics managed to avoid any major disasters, but they will be remembered as the most trouble-plagued Games since the 1996 Olympics in Atlanta.

Those Games were marred by a bombing in the city’s Olympic Park and massive organizational dysfunction. Rio could not tamp down the street crime that scared off many visitors, and the budget cuts spurred by Brazil’s financial crisis led to reductions in the services and volunteer forces that make the Games run smoothly. The main Olympic Park was mostly colorless concrete, and only 15 percent of the banners and signs that dress up the Olympics were ever installed.

Bach said these Olympics were “a Games in the middle of reality,” citing the deep social and economic divisions that have not improved despite its status as an Olympic host. The Rio Games, Bach said, brought the Olympics out of their bubble, forcing them to coexist for 16 days amid poverty and political unrest.

While the Olympics retain the power to unite people, Bach said there are limitations to its power, a point underscored in Rio. Holding the Olympics did not narrow Rio’s class divide, nor did it clean up its waters or significantly reduce its crime. The political tensions did not ebb, either; Sunday, Rio Mayor Eduardo Paes was introduced in three languages at the Closing Ceremony and booed all three times.

Bach glossed over the Games’ problems at a news conference Saturday, saying the Brazilians were “great hosts” who made the Summer Games “a great party.” But the party was not always well-attended, and the hosts weren’t always on good behavior.

Rio never solved the problem of empty seats at venues, which dulled the atmosphere at many events. The track stadium often was half-full, and sports such as field hockey and shooting drew less than that. With 75 percent of tickets sold to Brazilians, their penchant for booing left a sour taste at an event that is supposed to exemplify good sportsmanship.

That didn’t affect the athletic performances. A total of 87 Olympic nations won medals, a record, and 10 won the first gold medals in their countries’ histories. The U.S. led the medal table with 121 — the most it has ever won at an Olympics outside its borders — and 46 golds.

American swimmer Michael Phelps finished with five gold medals and a silver, increasing his record totals to 23 golds and 28 total medals. Jamaican sprinter Usain Bolt won three more golds, giving him nine for his career and tying him for second place behind Phelps.

In the handoff ceremony, Rio passed the Olympic torch to Tokyo, host of the 2020 Summer Games. Tokyo’s turn on the stage included an inventive light show and an appearance by Prime Minister Shinzo Abe, channeling video game character Super Mario. Its technological dazzle, abstract modernism and cheeky humor reflected how different those Olympics are likely to be compared with the past 16 days in Rio.

Rio still has one more party to throw: the Paralympics, which begin Sept. 7. Only 12 percent of tickets have been sold, and a government-funded grant for athlete travel has not been released. Sir Philip Craven, head of the International Paralympic Committee, said the Paralympics have never faced such dire circumstances in their 56-year history.

That means Rio will face more doubts, more questions, more skepticism. Sunday, its citizens didn’t want to think about that.

“I am the happiest man alive,” shouted Carlos Nuzman, president of Rio’s Olympic organizing committee. “I am proud of my country, my city, my people. All Brazilians are Olympic heroes.”