In the summer, Democrats were privately debating how to handle a jolt that had been sent to the more than 1 million U.S. households with same-sex couples.
The Supreme Court had just overturned its own rulings on abortion rights, and conservative Justice Clarence Thomas openly mused in his concurring opinion whether the court's landmark 2015 decision on gay marriage -- which he said was based on "legal fiction" -- should be next.
According to several aides on Capitol Hill, a top staffer to New York Rep. Jerry Nadler, chairman of the House Judiciary Committee, approached the office of Wisconsin Sen. Tammy Baldwin, the first openly gay senator and a longtime LGBTQ rights advocate.
Should the House push through a party-line gay rights bill to send a political message ahead of the midterm elections? Nadler's staff asked. Or was there a chance Democrats could peel off enough Republican support for a bill that would guarantee same-sex marriage?
According to several aides familiar with the account, Baldwin had already told her staff she thought such a bill was doable.
"Let's do this for real," responded Baldwin's chief of staff, Ken Reidy, kicking off a monthslong negotiation on what would become the biggest civil rights deal to pass Congress in years.
The House is expected to finalize the Respect for Marriage Act sometime this week, approving the Senate version that passed last week by a 61-35 vote. An earlier House vote on the proposal drew 47 Republican yeses. President Joe Biden could sign the bill into law within days.
Douglas Laycock, a top religious liberty scholar who supports the legislation, said he never thought it would happen in his lifetime.
"This is the first significant compromise between gay rights groups and religious liberty groups," said Laycock, a professor of constitutional law at the University of Virginia. "Both sides killed every previous attempted compromise."
Laying down a 'yellow brick road'
Onlookers have been quick to credit the bill's success -- drawing Republicans as well as Democrats -- to changing public opinion. Indeed, according to a Gallup poll in May, 71% of Americans say they support legal same-sex marriage. That is more than 40 points higher than in 1996, when the question was first asked.
But people involved in the bill's passage said it also was the product of a hard-fought negotiation years in the making.
"This shows there was a bipartisan way for LGBTQ freedom and religious freedom," said Tim Schultz, president of the 1st Amendment Partnership, who worked on the bill representing faith-based groups.
"Lawmakers do want to find a way forward on these issues. And I hope that this breaks the ice for bigger things," he added.
To understand that breakthrough, ABC News interviewed Hill staffers and lobbyists, several of whom spoke on condition of anonymity to be candid on how such a bill survived.
According to those involved, much of the compromise unfolded behind closed doors, including private phone calls by wealthy GOP donors who wanted Congress to resolve the issue.
Of particular concern, some Republicans said, was that any moves by the Supreme Court could alienate independent and moderate voters ahead of the 2024 election -- much as the debate on abortion rights hurt Republicans in the midterms.
There also were unexpected alliances. The Church of Jesus Christ of Latter-day Saints, which considers same-sex relationships against God's commandments, emerged as an early architect of the bill and worked closely with Arizona Sen. Kyrsten Sinema, the first openly bisexual member of the chamber, who grew up Mormon.
Under an amendment backed by the church, the Respect for Marriage Act guaranteed that faith-based groups wouldn't lose their tax-exempt status or risk federal grant money because of their objections to same-sex marriages.
In exchange, the Mormon church publicly endorsed the proposal as one that protects "religious freedom," paving the way for a dozen Senate Republicans to swing behind it.
"We knew who we could get to 'yes,'" said one GOP lobbyist. "But we needed to lay down a yellow brick road they could walk on."
What the marriage bill does and doesn't do
The Respect for Marriage Act is intended as kind of an insurance policy in case the Supreme Court were to overturn its own landmark ruling Obergefell v. Hodges, which requires every state to allow same-sex marriage.
If that ruling is reversed by the court, as Justice Thomas suggested it could be, the Respect for Marriage Act would kick in to require that all states recognize unions legally performed elsewhere in the country.
The bill is narrow in scope, disappointing many progressives. It wouldn't force states to issue same-sex marriage licenses, for example, as is required now under the Obergefell ruling.
The bill also doesn't say whether businesses or faith-based organization can deny working with same-sex couples, citing either freedom of religion or speech. Such a case is being considered this week in the Supreme Court.
Still, LGTBQ advocates who worked on the bill said the compromise is a historic step forward that provides legal certainty to the 710,000 households in the U.S. with married same-sex couples.
"It's not easy to sit down and actually have a conversation. I would say there is skepticism on both sides," said Shannon Minter, legal director of the National Center for Lesbian Rights, who was involved in negotiations on the bill.
"But I think by the end of the process, there was a recognition that people were actually negotiating in good faith there," Minter said.
Congressional aides said the legislation was closely negotiated by Democratic Sens. Baldwin and Sinema along with three Republicans: Susan Collins of Maine, who was known to cross party lines; Rob Portman of Ohio, who a decade ago changed his mind on same-sex marriage after learning his son was gay; and Thom Tillis of North Carolina, who had backed this year's bipartisan package on gun safety.
Several people said the job fell to Tillis to convince enough Republican colleagues to vote in favor of the bill so no one person had to be the final deciding vote. Under Senate rules, 60 votes are needed to advance most legislation.
Other key moments included a decision by Democrats to hold off on a Senate vote until after the midterm elections were settled and a decision by Senate Republican leader Mitch McConnell and other prominent GOP senators not to speak out against the bill even though they personally voted against it.
The Catholic Church and several conservatives publicly criticized the legislation, including Sens. Mike Lee of Utah and James Lankford of Oklahoma, who contended it would set a national policy on gay marriage that uniquely exposes churches and organizations to lawsuits.
But after the Church of Jesus Christ and several religious liberty scholars publicly signed on, the dozen Senate Republicans joined in support and guaranteed its passage.
In the end, both LGBTQ rights groups and religious freedom advocates claimed victory -- an achievement Sinema noted at a news conference celebrating the bill's passage in the Senate.
"What could fundamentally be more American than balancing those two issues?" she asked.
ABC News' Allison Pecorin and Trish Turner contributed to this report.