Massachusetts became the first state to elect an openly gay attorney general, while the gay candidate in Maine's gubernatorial race narrowly lost his chance to make history. Nationally, gay-rights activists worried that conservative gains in Congress would hamper their bid for federal anti-bias legislation.

In all, it was a sobering election for the gay-rights movement, contrasting with its recent court victories that have nearly doubled the number of states allowing same-sex marriage. In U.S. Senate races, for example, several Democrats who supported gay marriage - including Mark Udall of Colorado and Kay Hagan of North Carolina - lost to Republicans who oppose it.

Among the highlights for gay-rights activists was Democrat Maura Healey's election as Massachusetts attorney general. Healey, a former chief of the civil rights division in the attorney general's office, was captain of the women's basketball team at Harvard and played pro basketball in Austria before launching her law career.

In Maine, Democrat Mike Michaud fell short in his bid to become the first openly gay candidate in the U.S. to be elected governor. The 59-year-old former mill worker and six-term congressman, who made his sexual orientation public last year, had said it would be a boost for the gay community to have a voice in discussions among governors on equality issues.

In near-complete returns in a three-way race, Michaud had about 44 percent of the votes, while incumbent Republican Paul LePage was re-elected with 48 percent. An independent candidate, who may have undercut Michaud's chances, got 8 percent.

All five of the openly gay members of the U.S. House of Representatives won re-election, as did Kyrsten Sinema of Arizona, who is bisexual.

But several gay candidates seeking to join their ranks were defeated - including Democrat Sean Eldridge, husband of Facebook co-founder Chris Hughes, in eastern New York, and Republican Richard Tisei in a hard-fought race against Democrat Seth Moulton, an Iraq War veteran, in Massachusetts.

Another gay Republican, Carl DeMaio, was in a virtual dead with Democratic Rep. Scott Peters in a House district in San Diego. DeMaio confronted allegations from a former staffer of sexual harassment in the campaign's final weeks.

There have been gay GOP congressmen previously, but none who was out of the closet at the time they first won their seat.

Looking ahead, gay-rights leaders acknowledged that the new Congress, with an infusion of conservative Republicans, would be unlikely to support a federal bill outlawing a broad range of discrimination against lesbians, gays, bisexuals and transgender people. That has become a top priority for activists now that same-sex marriage seems on the path to nationwide legalization.

"The road will be hard and rocky," said Heather Cronk of the LGBT-rights group GetEQUAL. "We must offer up forward-thinking legislation to secure much-needed legal protections, rather than wallowing in self-loathing pity."

Fred Sainz, a vice president of the Human Rights Campaign, said the short-term goal would be "to change the hearts and minds of those not yet there with us."

"We'll be putting on our walking shoes and walking the halls of Congress - a lot - over the next few years to make sure that the new members become allies," Sainz said in an email.

He noted that the Republican caucus in the Senate - which will become the majority party in January - includes several supporters of same-sex marriage as well as several outspoken opponents.

Sainz also noted that nearly two-thirds of Americans now live in states where gay marriage is allowed, including several states which still do not ban discrimination by employers or public accommodations based on sexual orientation.

"It's incomprehensible that you can get married at 10 a.m. and be legally fired, thrown out of your apartment or denied a hotel room at 2 p.m., simply because you got married," Sainz said. "It's not something that Republicans will be able to ignore."

Rea Carey, executive director the National LGBTQ Task Force, said that Congress, even it took a more conservative turn, would be unable to stop the advances of same-sex marriage.

"As much as the results in Congress are challenging, our work continues at the state and local level," she said.

Some activists took heart from the fact that several Republicans who won election as governor either support the same-sex marriage laws in their state or accept the laws as reality. Among them are governors-elect Bruce Rauner in Illinois, Charlie Baker in Massachusetts and Larry Hogan in Maryland.

"Things that would have been hot button issues before have become less hot button," said Kevin Cathcart, executive director of the LGBT-rights group Lambda Legal. "We're still going to make progress this year and make progress next year, even if not on Capitol Hill."

A different tone was sounded in Idaho, where Republican Gov. C.L. "Butch" Otter won a third term and vowed to continue fighting the legalization of same-sex marriage even though the state's ban has been overturned in federal court.

"I believe we are the last chance to stand up and fight for traditional marriage," Otter said in his victory speech. "If they want to change the rest of the 49 states, go ahead. Why should we change? That's not what our voters want, that's not what our creator wants, and that's not what Idaho wants."