When Thursday began, Elon Musk had a deal on the table.

After days of tense negotiations, the Securities and Exchange Commission and lawyers for Musk, Tesla’s chief executive, had agreed on a settlement that would bring to a close a drama that has riveted Wall Street and Silicon Valley for the past two months.

Under the terms of the agreement, what started on Aug. 7 — when Musk posted on Twitter that he had “funding secured” to take Tesla private for $420 a share — would end with some modest penalties and Musk staying on as chief executive, according to two people briefed on the talks.

The plan, as it was negotiated by lawyers, was for Musk to step down as chairman of Tesla within 45 days and not resume that post for two years. The company, also a party to the proposed agreement, would add two new directors to its board. Musk and the company would pay tens of millions of dollars in fines, according to the people, who requested anonymity because they were not authorized to speak publicly. The negotiators planned to announce the agreement Thursday after the markets closed.

But for Musk — an emotional, volatile and cocksure billionaire — the deal was unacceptable.

The settlement with the SEC was a “neither admit nor deny” deal, meaning that Musk would not have acknowledged knowingly committing a crime. Musk, however, would not have been allowed to publicly state that he had done nothing wrong — and that was something he couldn’t accept, according to three people familiar with the talks.

So on Thursday morning, as settlement papers were being drawn up and news releases were being drafted, Musk walked away. Lawyers, Tesla executives and advisers to the company were stunned that he would turn away from such a favorable settlement.

And the SEC, taken aback, quickly changed course and upped the ante significantly. On Thursday afternoon, the agency sued Musk, seeking to bar him from serving as an executive or a director of a public company. If it wins, Musk will lose the company he co-founded. Tesla stock fell 14 percent on Friday.

“The company’s brand and stock will suffer if he leaves,” said Mike Ramsey, an auto analyst at Gartner. “But I hate to say it, they might be better off.”

Tesla has lurched from crisis to crisis over the past year, and has been scrambling to contain the fallout from Musk’s tweet, which touched off a market frenzy that sent Tesla’s shares soaring, and prompted federal regulators to examine whether Musk had misled investors with a surprise declaration that vastly overstated reality.

On Friday, the SEC set a date of Feb. 1 for a preliminary hearing on the case, leaving plenty of time for Musk to change his mind and agree to a settlement, albeit a potentially less favorable one. But the lawsuit, which could take years to come to trial, will cast a cloud over the company as long as the matter remains unresolved. In recent weeks, the SEC was preparing to send Tesla a Wells Notice, signaling that it intended to bring civil charges against the company and Musk. But by Thursday, after the settlement talks fell apart, the SEC narrowed its focus. Instead of looking to settle with the company and Musk, it sued Musk alone, according to a person close the company.

After the commission began to investigate Musk’s assertion on Twitter, his lawyers sent two lengthy letters to regulators making their case that he had done nothing wrong, according to that person.

The letters outlined meetings Musk had had with officials from a Saudi Arabian sovereign wealth fund, which had led him to believe he had financial support to take Tesla private, the person said.

On an evening in March 2017, for example, Musk and Tesla’s chief financial officer dined at the Tesla factory in Fremont, California, with Larry Ellison, the chairman of Oracle, and Yasir Al Rumayyan, the managing director of the Saudi Public Investment Fund. During the meal, the letters said, Rumayyan raised the idea of taking Tesla private and increasing the Saudi fund’s stake in it.

More than a year later, the lawyers said, Musk and Rumayyan met at the Tesla factory on July 31. When Rumayyan spoke again of taking the company private, Musk asked him whether anyone else at the fund needed to approve of such a significant deal. Rumayyan said no, according to the person familiar with the letters.

Representatives for Ellison and the Saudi investment fund did not immediately respond to messages seeking comment Friday evening. People familiar with the workings of the Saudi fund previously said it had taken none of the steps that such an ambitious transaction would entail, like preparing a term sheet or hiring a financial adviser to work on the deal.

Musk and other Tesla executives told Tesla’s board about the talks with the Saudis, the lawyers wrote, according to the person familiar with the letters. On Aug. 3, Musk shared his idea for the $420 share price with the board.

It was an Aug. 7 Financial Times story that spurred Musk to action, the lawyers said in the letters to the commission. An alarm bell went off when he saw the newspaper’s report that the Saudis had built up a significant stake in Tesla. He feared that word would get out that a deal to take Tesla private was possible. So he began to tweet, the lawyers said.

His tweets, the lawyers wrote, were sent in good faith. He believed that the Saudis were capable of doing a deal and interested in doing one, and that what remained was a matter of details, according to the person familiar with the letters.

One sticking point for Musk in the tentative SEC settlement was the particular statute which he was said to have violated.

That statute contains language about misleading investors. Musk’s lawyers wanted the commission to change its claim to say he was merely negligent in his statement, according to a person familiar with the details of the negotiations.

Musk was concerned about what those terms might mean for his other businesses, SpaceX and the Boring Co. He was worried the agreement could jeopardize those companies’ ability to continue working for the government, the person said.

In a statement after the commission filed its suit Thursday, Musk called the agency’s enforcement effort “unjustified.”

“I have always taken action in the best interests of truth, transparency and investors,” he said. “Integrity is the most important value in my life, and the facts will show I never compromised this in any way.” Musk’s decision to back away from the settlement could complicate Tesla’s future. He has said the company will be consistently profitable by the end of this year, propelled by sales of its newest car, the midsize Model 3 sedan. But Tesla has struggled to meet its production targets for the Model 3, and has continued to burn through cash while two bond payments totaling more than $1 billion will come due in the next six months.

Tesla had $2.2 billion in cash at the end of the second quarter, but has been using up nearly a $1 billion every three months. It also has about $11 billion in debt, and owed its suppliers $3 billion as of June 30.

Musk has said Tesla won’t seek additional capital. But Garrett Nelson, an analyst at CFRA Research, said he believed Tesla will have do so in the first half of 2019.

Musk’s legal troubles will only make it more difficult for the company to issue bonds or secure other financing.

“The best case is they can get access to capital but it’s more expensive than they would like,” Nelson said. “The worst case is they won’t be able to raise capital.”

Another uncertainty for investors is who will ultimately be at the helm of Tesla. Musk is Tesla’s visionary, much like Steve Jobs was at Apple, Nelson said, and belief in him is one of the reasons investors have bet on Tesla shares.

“He’s critical, in our opinion,” Nelson said.