Almost a week after the midterm elections, Democrat Kyrsten Sinema won Arizona’s Senate race, after an extended vote count delivered Democrat an upset victory and a blow to Republicans and President Donald Trump. Democrats had not won a Senate seat in Arizona since 1988, but President Trump carried Arizona by less than 5 points in 2016, a closer margin than previous GOP presidential nominees.

Sinema defeated Republican Martha McSally in one of the most closely watched Senate races this cycle. Sinema led by a margin of 38,197 votes, or about 1.7 percentage points, out of more than 2.1 million votes cast, when the Associated Press called the race on Monday, replacing retiring GOP Sen. Jeff Flake.

With almost three-quarters of the state’s voters casting ballots by mail in the close race, it took Arizona officials six days to finish tabulating the results. The outcome leaves the party division in the Senate at 51-47 in favor of Republicans, with the Florida race in a recount and the contest in Mississippi set for a runoff.

The AP made the call after Sinema, who was first elected to Congress in 2012, increased her lead over McSally for the fifth straight day. Arizona still has about 200,000 ballots left to count, but McSally would have to win an improbable percentage of those remaining votes to overcome Sinema's edge.

Sinema - who becomes the first female senator elected from Arizona and the first openly bisexual senator - will replace Republican Senator Jeff Flake, an outspoken Trump critic who often attracted the president’s ire. Flake’s sparring with Trump all but assured that if the senator ran again, he would have faced a primary challenge from the GOP’s right wing that Flake decided he probably couldn’t win.

"I just called Kyrsten Sinema and congratulated her on becoming Arizona's first female senator after a hard-fought battle," McSally said in a video posted to Twitter on Monday evening.

Congrats to @kyrstensinema. I wish her success. I’m grateful to all those who supported me in this journey. I’m inspired by Arizonans’ spirit and our state’s best days are ahead of us. pic.twitter.com/tw0uKgi3oO — McSally For Senate (@MarthaMcSally) November 13, 2018

The race was one of the year’s most hotly contested, and each contender was ahead in two or more polls since mid-October.

Sinema, who fashioned herself as a moderate, kept the focus on health care and protections for pre-existing conditions as a wedge issue with McSally, who voted for the GOP’s ObamaCare repeal bill. More from Bloomberg:

Sinema is a former Green Party activist who over time became a moderate Democrat. Like many on the ballot from her party, she stressed her support for Obamacare and its popular protection for people with pre-existing health conditions. But she also distanced herself from more liberal Democrats by rejecting a push to expand Medicare to cover all Americans. In the House, Sinema backed Republican efforts to curb regulations and voted against Nancy Pelosi of California in the 2015 and 2017 speaker elections, backing civil rights icon John Lewis of Georgia instead.

That said, she is certainly not a republican, siding with Democrats in a number of key areas: she supports abortion rights, gun control, environmental protections and a vigorous government role in providing a social safety net, education, job training and infrastructure.

McSally's loss is a political setback for Trump, who carried Arizona in 2016 and spent two days in the state last month in an effort to shore up the candidacy of McSally, a former Air Force pilot. As the vote count dragged on, Trump asserted, without evidence, that there was corruption in the tally. Some state GOP officials pushed back against the assertion and settled a dispute over taking extra time to verify ballots according to Bloomberg.

During earlier years of service in the Arizona state legislature, Sinema was among about three dozen state lawmakers who served on a health-care task force that President Barack Obama used to develop his Affordable Care Act proposal in 2009.

Meanwhile McSally overcame two more conservative challengers in the Republican primary, and she aligned herself closely with Trump. She focused much of her campaign messaging on immigration and border security in an effort to boost Republican turnout. McSally also hammered Sinema over her past anti-war protesting and progressive roots.

Sinema benefited from the help of Latino voters, who make up nearly one-third of Arizona’s voting age population and who have heavily favored Democrats. Sinema’s win suggests potential for further gains by Arizona Democrats in 2020. The state has voted for a Democratic presidential candidate only once since 1948, though Hillary Clinton lost Arizona by just 3.4 percentage points in 2016.

With Arizona in the rearview mirror, a handful of other races around the country remain too close to call, including high-profile races in Florida and Georgia.

Recounts have been ordered in Florida’s hotly contested Senate and gubernatorial races, while in Georgia, the governor’s race hasn’t been called, as Democrats hold out hope that remaining ballots could push the race into a runoff, though that remains an uphill battle.