Last night, Terry McAuliffe beat Ken Cuccinelli in the Virginia governor’s race by a margin of 55,000 votes. The race turned out to be closer than many expected, but a loss is a loss—the voters’ verdict will likely be viewed in part as a rejection of Mr. Cuccinelli’s extreme social conservative agenda.

What may be more disappointing for conservatives is a potentially razor-thin defeat in the Virginia Attorney General’s race. Republican candidate Mark Obenshain had appeared to be a more palatable option than Cuccinelli throughout much of the campaign. In the final polls leading up to yesterday’s voting, Obenshain was outperforming Cuccinelli by 7 points . It was thought that if the margin between McAuliffe and Cucinelli was under 5 points—which it was—then Obenshain would win. But this morning Obenshain woke up to find himself about 540 votes behind Democrat Mark Herring. (That margin continues to shift as the final precincts report in; by noon Wednesday, Obenshain appeared to be in the lead by several hundred votes. But weeks of recounts seem likely.)

So, what got in Mark Obenshain’s way? In a word, guns—both his own extreme record in the issue and the intervention of national gun-control advocates like New York City Mayor Michael Bloomberg and former Rep. Gabrielle Giffords.

Let’s walk through how the Obenshain campaign faltered in the final 10 days.

Obenshain had run a better campaign the Cuccinelli for months. Though Obenshain shared Cuccinelli’s extreme cultural conservative agenda on guns and women’s issues, he instead put his biography and family in the spotlight as he tried to appeal to voters. Obenshain ran a series of ads where his daughter introduced him in an appealing, compelling light .

The image he’d cultivated and the stronger poll numbers inspired a wave of late contributions to Obenshain’s campaign. One group alone, the D.C.-based Republican State Leadership Conference, poured in more than $1 million between October 17 and election day . The funds allowed Obenshain to spend almost $700,000 on broadcast TV ads in the final nine days—far more ads than Cuccinelli could afford.

But, in those final nine days, one huge obstacle was in Obenshain’s path: his out-of-the-mainstream record on guns.

Although he tried to separate himself from Cuccinelli and extreme positions that were obviously faring poorly with Virginia voters, Obenshain had a similar record: he voted 12 times against expanding background checks for gun sales. And, in the final days of the election, Mayor Bloomberg’s Independence USA Super PAC nearly doubled the Obenshain campaign’s broadcast spending with a $1,292,415 ad campaign focusing on northern Virginia . These ads focused on Obenshain’s opposition to universal gun background checks and his other extreme positions on issues like abortion. Mark Herring, Obenshain’s Democratic opponent, in contrast campaigned on expanding background checks and protecting women’s health choices. The Bloomberg ads charged that Obenshain was just another member of the Cuccinelli’s “Tea Party Ticket.”

Bloomberg’s efforts were supplemented by Gabby Giffords’s group, Americans for Responsible Solutions, which targeted Obenshain with a six-figure mail campaign with a similar message focusing on 66,000 Virginia households headed by Independent women.

And the result? This election remains too close to call. There will undoubtedly be a recount and the race for attorney general may not be called for weeks. (An aside: Bob McDonnell, the outgoing Virginia governor, ultimately won a 2005 Virginia attorney general’s race by 360 votes when counting was completed… in December of that year.)

But, regardless of who may ultimately prevail, one lesson of this race is already clear: the tide is turning in swing states like Virginia. Even in Virginia, where the NRA is headquartered, candidates cannot fall back on extreme positions on issues like guns and expect to easily coast to victory. This race, as well as the decisive defeat of Cuccinelli, in which Giffords and Bloomberg’s PACs spent more than $2 million, demonstrates that candidates who oppose background checks will pay a price at the ballot box—even in purple states with a vibrant gun heritage.

A poll released Monday by Public Policy Polling showed that in the Cuccinelli race, 47 percent of Virginia voters reported that his background checks position made them less likely to vote for him—only 18 percent said they were more likely to vote for Cuccinelli based on that position. And in the official exit polls, while nearly half of voters live in gun-owning households, McAuliffe did better in non-gun households than Cuccinelli did in gun households. Being for background checks helps more than it hurts.

We may never know exactly how much damage Mark Obenshain’s gun extremism and the ad campaign against him did—but if Herring’s lead of several hundred votes holds up, it’s a question that Mark Obenshain may be wondering about for a long time.