House Majority Leader M. Kirkland Cox (R-Colonial Heights) would be speaker if Republicans maintain control. (Steve Helber/Associated Press)

Whether Virginia's deep-red House of Delegates turns blue, or an awkward purple, comes down to a few dozen votes and potential handshake deals.

Republicans, who held 66 of 100 seats in the lower house of the state legislature, saw their majority melt away Tuesday in a Democratic wave that felled at least 12 GOP incumbents and flipped three open seats to the Democrats — an unprecedented shift.

With four races still too close to call, both parties are bracing for the messiest of all outcomes: a dead-even 50-50 split that requires power-sharing and a potentially ugly fight for the speakership.

That would be triggered if Democrats pick up one of the four races that are close enough for a state-funded recount. Republicans have leads difficult to overcome in three of them, including Del. Timothy D. Hugo (R-Fairfax), who narrowly pulled ahead of his challenger after unofficial results were tallied. Del. David Yancey (R-Newport News) is just 12 votes ahead of Democratic challenger Shelly Simonds, with provisional ballots still being counted through Monday.

Recounts would probably take place around Thanksgiving when elections are certified.

Control of the chamber has implications for policy decisions affecting large swaths of Virginians, including whether Medicaid is expanded to an estimated 400,000 low-income residents and new gun control measures are implemented.

Even if Democrats fall short, they have other options to take control of the legislature. They can lure Republican lawmakers to their side with the promise of plush committee assignments, or government jobs or Cabinet positions offered by Governor-elect Ralph Northam (D).

"There's always a degree of horse-trading and politics, and people often have discussions about what committees they want to be on and where they want to be," said House Minority Leader David J. Toscano (D-Charlottesville), who would probably be the chamber's speaker next year if his party prevails. "The first thing we need to do is count the votes and know exactly where we are."

But the GOP House leadership said their majority is secure because they are confident Yancey will hold his seat.

"Our majority will be smaller, but our resolve and commitment to good governance based on conservative principles remains strong and unwavering," said Del. M. Kirkland Cox (R-Colonial Heights), who is set to be speaker if the GOP majority holds. "We also plan to serve as a check and balance to any effort to push an extreme liberal agenda, as our constituents elected us to do."

Earlier in the day, his spokesman Parker Slaybaugh declined "to speculate on how the chamber might be organized in January" if Democrats are able to block a Republican majority.

Unlike the state Senate, where the lieutenant governor breaks ties, there is no such mechanism in the House. Virginia last saw an even split in the House of Delegates in 1999. Democrats held on to the speakership with a parliamentary maneuver, while committees that serve as gatekeepers for legislation were led by two chairs, one from each party.

"It's not that it can't work, but it's just a total mess," said Del. David B. Albo (R-Fairfax), a retiring Republican who served through those years.

Political observers say that managing the battle for control of the legislature will be Northam's first chance to show leadership, with an opportunity for bipartisanship and to strengthen his negotiating hand.

"This is territory we've seen once, and it wasn't pretty," said Bob Holsworth, a veteran observer of Virginia politics. "It was extremely bitter. It will be a test of Northam to make sure that doesn't happen."

Unlike the man he is replacing, Gov. Terry McAuliffe (D), Northam has relationships with Republicans in the legislature stretching back a decade, to when he first became a lawmaker. He was so well-regarded by Republicans, in fact, that they tried to coax him into changing parties as part of a plot to take control of the Democratic state Senate.

Democrats don't have all the leverage if control of the House of Delegates is in play, strategists said. There are regional and ideological fissures within the Democratic caucus, and some of the incoming members were elected without the party's support — increasing the chances for defections. Under one scenario, the Republicans could delay seating Simonds, if the Democrat prevails in a recount against Yancey, in time to elect a speaker of their own.

"If Northam could broker a deal, it would be a major, major accomplishment for him," said Ben Tribbett, a Democratic strategist closely monitoring the House of Delegates races. "But if he tries to get involved and fails, it would be a big loss to start off his term."

A spokesman for Northam declined to tip his hand on how the future governor would maneuver to turn the General Assembly blue, but said he would be true to his campaign promise of working across the aisle.

"His talk about bipartisanship isn't just happy talk, it's because people want to see government be effective and work for them again," spokesman David Turner said. "Republicans would be wise to understand that bipartisanship and having effective government was one of the things we ran on — and won on by a historic margin."

The upper chamber of the state legislature could also be in play.

Republicans hold a 21 to 19 edge in the state Senate, with Lt. Gov.-elect Justin Fairfax (D) set to preside over the body and break ties. Some strategists speculated that Northam would try to bring some of his old colleagues from the state Senate — who are Republican — into his administration.

That happened in 1998, when then-Gov. Jim Gilmore (R) appointed a Democratic senator as his deputy transportation secretary, handing control of the Senate to Republicans as a result. And in 2014, Virginia Republicans took control of the Senate after Sen. Phillip P. Puckett (D-Russell) resigned amid talks of a job with a state tobacco commission.

Regardless of the parliamentary maneuvering and who ends up in power, Virginia Republicans are entering a new administration with far less clout than they had before Election Day.

"It's going to be a new day over there in the House of Delegates," said McAuliffe, who set a record for vetoing legislation passed by the GOP-controlled General Assembly.

Albo, the retiring Republican lawmaker who will be replaced by a Democrat, lamented the vanishing breed of districts with voters willing to split their ballots.

"This state is not purple," he said. "It's like dark red and dark blue spots."

Gregory S. Schneider contributed to this report.