In a monumental upset fueled by a Donald Trump tweet, U.S. Rep. Mark Sanford lost his Republican primary to Katie Arrington, a one-term state lawmaker who made loyalty to the president the centerpiece of her campaign.

The defeat, which carries national implications, marks the first time Sanford has lost an election, which began with his first congressional bid in this very district in 1994.

President Trump — making a move unprecedented in South Carolina history — in a late afternoon tweet asked state voters to replace Sanford with Arrington, going so far as to say Sanford is "better off in Argentina," a reference to his adulterous affair when he was governor in 2009.

The message was delivered less than three hours before the polls closed, illustrating Trump's willingness to take out one of the few Republican congressmen in recent months willing to call out the president for his sometimes crude behavior.

Arrington, a Summerville Republican, led by 3,000 votes with 80 percent of the vote in. Dimitri Cherny, the third candidate on the Republican ballot, pulled in about 3 percent despite running as a Democrat in previous elections. The final results will be certified later this week.

Arrington will face Democrat Joe Cunningham in the Nov. 6 general election. Cunningham, a 35-year-old construction attorney, handily defeated Democrat Toby Smith.

"We are the party of Donald J. Trump... and now, congresswoman Katie Arrington," she said at her victory party across town in North Charleston at the Doubletree Hotel, where the crowd chanted her name in unison.

The dynamics Sanford faced Tuesday in the GOP contest mirrors the one-man political litmus test many Republicans face in midterms nationwide: Do they support — and will they support — the president?

Sanford addressed that ideological challenge head-on in his concession speech in Mount Pleasant. 

"A lot of people have come up to me in the course of this campaign and asked me, 'Are you for or against Trump?' And I'd say, 'I'm neither for nor against Trump. I'm for ideas that I've long stood for in my entirety of my time in office,'" he said.

Glancing down occasionally at his handwritten speech, Sanford accepted it was his Trump stances that did him in, but said he would not have changed how he conducted himself.

"It may have cost me an election, but I stand by every one of those decisions to disagree with the president because I didn't think they would be concurrent with the promises I made when I first ran for office and for the very voices of the people of the 1st District that I represent."

The loss also parallels the defeat of Republican Bob Inglis in his 2010 conservative Upstate congressional primary at the hands of another political wildcard, the tea party movement.

Sanford's trepidation showed throughout the day Tuesday. He drove himself around the district, making his final pitch to voters in his run for a third term in his second stint in Washington. He served three terms in Congress in the 1990s.

"Whatever I've believed has always been what I really believed. It's not the flavor of the month or poll-tested," Sanford said, gripping the wheel of his black Chevy Suburban during his nearly 77-mile round-trip throughout the district on Election Day.

Trump's tweet, delivered after his return from his Singapore and a meeting with North Korean leader Kim Jong-un, additionally called Sanford "nothing but trouble."

"Mark Sanford has been very unhelpful to me in my campaign to MAGA. He is MIA and nothing but trouble. He is better off in Argentina. I fully endorse Katie Arrington for Congress in SC, a state I love. She is tough on crime and will continue our fight to lower taxes. VOTE Katie!" he said.

Sanford admitted the tweet proved troublesome for him, a conservative trying to navigate an administration that demands all-or-nothing loyalty.

"It's fairly unusual for a president to tweet in a congressional race. I don't know the last time it's happened in a Republican primary but it happened today," he said.

Arrington, meanwhile, looked forward to her odds in a GOP-leaning district and emerging as a congressional candidate with only two years of experience in the S.C. Statehouse.

“It’s amazing, but it really is about people wanting representation and getting a citizen legislature out there," she said. "It’s a phenomenal feeling.”

Sanford said he did not plan to stick around the Lowcountry in the days following the primary. He has a Wednesday morning flight to Washington to make sure he's back for a House vote, even knowing his days in office are numbered.

Abigail Darlington contributed to this story.

Reach Caitlin Byrd at 843-937-5590 and follow her on Twitter @MaryCaitlinByrd.

Caitlin Byrd is a political reporter at The Post and Courier and author of the Palmetto Politics newsletter. Before moving to Charleston in 2016, her byline appeared in the Asheville Citizen-Times. To date, Byrd has won 17 awards for her work.