PARIS/WASHINGTON/BEIRUT (Reuters) - The ceasefire deal that ended a relentless barrage of Israeli airstrikes and led Lebanon into a shaky peace took shape over weeks of talks and was uncertain until the final hours.

U.S. envoy Amos Hochstein shuttled repeatedly to Beirut and Jerusalem despite the ructions of an election at home to secure a deal that required help from France -- and that was nearly derailed by international arrest warrants for Israel's leaders.

Israel had signalled last month that it had achieved its main war goals in Lebanon by dealing Iran-backed Hezbollah a series of stunning blows, but an agreed truce remained some way off.

A football match, intense shuttle diplomacy and pressure from the United States all helped get it over the line on Tuesday night, officials and diplomats said.

Longstanding enemies, Israel and Hezbollah have been fighting for 14 months since the Lebanese group began firing rockets at Israeli military targets in support of the Palestinian militant group Hamas. 

Escalations over the summer drew in Hezbollah's main patron Iran and threatened a regional conflagration, as Israel refocused its military from the urban ruins of Gaza to the rugged border hills of Lebanon.

Israel stepped up its campaign suddenly in September with its pager attack and targeted airstrikes that killed Hezbollah's leader and many in its command structure. Tanks crossed the border late on Sept. 30. 

With swathes of southern Lebanon in ruins, more than a million Lebanese driven from their homes and Hezbollah under pressure, Israeli Prime Minister Benjamin Netanyahu indicated in October there was "a window" for a deal, a senior U.S. administration official said. 

Although some in Israel sought a more comprehensive victory and an uninhabited buffer zone in Lebanon, the country was strained by a two-front war that had required many people to leave their jobs to fight as reservists. 

DIPLOMACY

"You sometimes get a sense when things get into the final lane, where the parties are not only close, but that the will is there and the desire is there and the stars are aligned," the senior U.S. administration official said in a briefing. 

Officials of the governments of Israel, Lebanon, France and the U.S. who described to Reuters how the agreement came together declined to be identified for this story, citing the sensitivity of the matter.

Hezbollah did not immediately respond to a request for comment about how the deal was negotiated.

In Lebanon, Hezbollah was still fighting but under intense pressure, and newly open to a ceasefire that was not dependent on a truce in Gaza -- in effect dropping a demand it had made early in the war.

The Shi'ite group had in early October endorsed Lebanon's veteran Parliament Speaker Nabih Berri, its longtime ally, to lead negotiations. 

With Hochstein shuttling between the countries, meeting Israeli negotiators under Strategic Affairs Minister Ron Dermer and reporting back daily to U.S. National Security Adviser Jake Sullivan, France was also in the picture. 

Paris had been working with Hochstein on a failed attempt for a truce in September and was still working in parallel to the U.S.

Lebanese officials had made it clear to the U.S. that Lebanon had little trust in either Washington or Netanyahu, two European diplomats said.

France had been increasingly critical of Israel's military campaigns, and Lebanese officials regarded it as a counterweight in talks to the U.S., the Western diplomat said.

French Foreign Minister Jean-Noel Barrot visited the region in early November at Israel's request despite tensions between the countries. 

He held long talks with Dermer on the mechanics of a ceasefire with a phased approach to redeployments, with the two delegations poring over maps, two sources aware of the matter said. 

As things worsened for Lebanon, there was frustration at the pace of talks. "(Hochstein) told us he needed 10 days to get to a ceasefire but the Israelis dragged it out to a month to finish up military operations," a Lebanese official said. 

VIOLATIONS 

The deal was to be based on better implementation of U.N. Security Council Resolution 1701, which ended a 2006 war between Israel and Hezbollah. Both sides complained of repeated violations of that deal and wanted reassurances. 

The main sticking point was Israel's insistence on a free hand to strike if Hezbollah violated 1701. That was not acceptable to Lebanon. 

Eventually Israel and the U.S. agreed a side-deal - verbal assurances according to a Western diplomat - that Israel would be able to respond to threats. 

"The two sides keep their right to defend themselves, but we want to do everything to avoid them exercising that right," a European diplomat said.

Israel was also worried about Hezbollah weapons supplies through Syria. It sent messages to Syrian President Bashar al-Assad via intermediaries to prevent this, three diplomatic sources said. 

It reinforced the message by ramping up air strikes in Syria, including near Russian forces in Latakia province where there is a major port, the three sources said.

"Israel can almost dictate the terms. Hezbollah is massively weakened. Hezbollah wants and needs a ceasefire more than Israel does. This is finishing not due to American diplomacy but because Israel feels it has done what it needs to do," said a senior Western diplomat. 

OBSTACLES

The talks intensified as the Nov. 5 U.S. presidential election loomed and reached a turning point after Donald Trump won the vote. 

U.S. mediators briefed the Trump team, telling them the deal was good for Israel, good for Lebanon and good for U.S. national security, the senior U.S. administration official said. 

A potential new flashpoint endangering the critical role of Paris in the negotiations emerged as an Israeli soccer team travelled to France after violence had engulfed Israeli fans in Amsterdam.  

However, with French authorities averting trouble, French President Emmanuel Macron sat next to the Israeli ambassador in the stadium. "The match was so boring that the two spent an hour talking about how to calm tensions between the two allies and move forward," the source aware of the matter said.

At this key moment the International Criminal Court issued arrest warrants for Netanyahu and former Israeli defence minister Yoav Gallant. 

Netanyahu threatened to cut France out of any deal if Paris abided by its Rome Statute obligation to arrest him if he went there, three sources said. That could in turn torpedo Lebanese agreement to the truce. 

U.S. President Joe Biden phoned Macron, who in turn phoned Netanyahu before Biden and Macron spoke again, the U.S. official said. The Elysee eventually settled on a statement accepting the ICC's authority but shying away from threats of an arrest. 

Over the weekend U.S. officials then ramped up pressure on Israel, with Hochstein warning that if a deal was not agreed within days, he would pull the plug on mediation, two Israeli officials said. 

By Tuesday it all came together and on Wednesday the bombs stopped falling.    

(Reporting by Humeyra Pamuk and Trevor Hunnicutt in Washington, John Irish in Paris, Maya Gebeily in Beirut and Maayan Lubell in Jerusalem; Writing by Angus McDowall and Suzanne Goldenberg, Editing by William Maclean)

By John Irish, Humeyra Pamuk and Maya Gebeily