The beaming smile Theresa May wore as she shook Jean Claude Junker’s hand on Friday morning bore out the relief she must have felt after finally locking in a settlement for the first phase of Brexit talks.

With a deadline having been set for midnight on Sunday, early progress was by no means certain and not even expected as midnight approached, with intense international calls being made from 10 Downing Street.

For most of the day Ms May’s Europe advisor Olly Robbins had been in constant contact with Sabine Weyand, the deputy to chief EU negotiator Michel Barnier, laying ground for progress.

Meanwhile, other Downing Street staffers prepared to have their work Christmas party, which continued during the night as cross-channel negotiations lit up the phone lines.

A Number 10 spokesperson said: “We made progress throughout the day yesterday.

“But it was late in the evening, when we felt everything was in the right place and we could move forward together.”

The first sign to the wider world that things were moving towards a conclusion was around 6pm on Thursday, when officials for the President of the European Council, Donald Tusk, announced that he would make a statement in the morning.

Brexit bonhomie as May finally seals agreement with EU leaders Show all 10 1 /10 Brexit bonhomie as May finally seals agreement with EU leaders Brexit bonhomie as May finally seals agreement with EU leaders Britain's Prime Minister Theresa May is welcomed by European Commission President Jean-Claude Juncker Reuters Brexit bonhomie as May finally seals agreement with EU leaders EU President Jean-Claude Juncker greeting Theresa May at the EU Commission in Brussels PA Brexit bonhomie as May finally seals agreement with EU leaders Britain's Prime Minister Theresa May poses for a picture with European Council President Donald Tusk REUTERS Brexit bonhomie as May finally seals agreement with EU leaders EU Commission President Jean-Claude Juncker walks behind British Prime Minister Theresa May EPA Brexit bonhomie as May finally seals agreement with EU leaders British Prime Minister Theresa May (L) and European Commission President Jean-Claude Juncker address a press conference at the European Commission in Brussels AFP/Getty Images Brexit bonhomie as May finally seals agreement with EU leaders British Prime Minister Theresa May speaks at a press conference with EU Commission President Jean-Claude Juncker EPA Brexit bonhomie as May finally seals agreement with EU leaders Britain's Secretary of State for Exiting the European Union, David Davis (L) and Michel Barnier (2-L), the European Chief Negotiator of the Task Force for the Preparation and Conduct of the Negotiations with the United Kingdom under Article 50 enter the room by the emergency exit to attend British Prime Minister Theresa May press briefing on Brexit Negotiations in Brussels. EPA Brexit bonhomie as May finally seals agreement with EU leaders Britain's Secretary of State for Exiting the European Union David Davis (L), Britain's Prime Minister Theresa May (2-L), European Commission President Jean-Claude Juncker (2-R) and European Union's chief Brexit negotiator Michel Barnier (R) in a meeting on Friday morning AFP/Getty Images Brexit bonhomie as May finally seals agreement with EU leaders European Union chief Brexit negotiator Michel Barnier speaks during a media conference at EU headquarters Brexit bonhomie as May finally seals agreement with EU leaders European Council President Donald Tusk addresses a media conference at the Europa building in Brussels AP

Around the same time sources at the European Parliament began to say a deal had been done, but even at this point it was not set in stone.

With Downing Street desperate to avoid another PR disaster following the collapse of Ms May’s first proposed withdrawal settlement, Number 10 officials warned journalists against jumping to conclusions.

At 7.35pm Mr Juncker’s spokesperson confirmed the commission President had spoken to both Ms May and Irish Prime Minister Leo Varadkar.

Mr Juncker and Mr Tusk had cleared their dinner diaries in case the deal was to be sealed early but doubt remained over whether an agreement was within reach.

The critical period appears to have come between 9pm and midnight, when the Prime Minister took two calls in quick succession with the DUP’s Arlene Foster.

It was the Northern Irish political leader’s eleventh-hour intervention on Monday that scuppered the last settlement. But with some changes to the text, she now gave her approval for things to move forward.

Her permission was only given on the continued understanding that there are still outstanding issues around the Irish border that need to be agreed, something the text of the settlement outlines and Downing Street confirmed on Friday morning.

Brexit: Theresa May agrees breakthrough Irish border deal with EU leaders

While Ms May was finessing the agreement with the EU, Dublin and the DUP, and with the staff party going on around her, the Chief Whip was trying to ensure the parliamentary party felt included in the process.

Julian Smith MP sent out a series of messages aimed at Tory backbenchers who may still baulk at how the settlement paves the way for the UK to remain in regulatory alignment in Europe, and who will see the Treasury handing over almost £40bn to Brussels as anything but a victory.

Number 10 officials said Ms May had not had phone calls with key cabinet Brexiteers Boris Johnson and Michael Gove last night, central figures in demanding the Prime Minister stand her ground against Brussels’ demands in negotiations.

Democratic Unionist Party leader Arlene Foster gives her leader’s speech during the annual DUP party conference earlier this year (Getty)

But Mr Smith did post a picture on social media showing Mr Johnson met Ms May for a “quick update” on progress during the evening.

The Downing Street spokesperson said: “The PM had a tough day. There was a lot of work to be done, a lot of conversations to be had. We are pleased we got to the place we are at this morning.”

Confirming that the PM only had a couple of hours sleep, he added: “It was a late finish and an early start.”

Ms May was up and ready to leave Downing Street around 3.30am, with her official car whisking her and her bleary-eyed team to RAF Northolt to catch one of the UK Government’s jets to Brussels.

Brexit: the deciders Show all 8 1 /8 Brexit: the deciders Brexit: the deciders European Union's chief Brexit negotiator, Michel Barnier Getty Brexit: the deciders French President Emmanuel Macron Getty Brexit: the deciders German Chancellor Angela Merkel Reuters Brexit: the deciders Commission President Jean-Claude Juncker EPA Brexit: the deciders The European Parliament's chief Brexit negotiator Guy Verhofstadt Getty Brexit: the deciders Britain's Prime Minister Theresa May Getty Images Brexit: the deciders Britain's Chancellor of the Exchequer, Philip Hammond PA Brexit: the deciders After the first and second appointed Brexit secretaries resigned (David Davis and Dominic Raab respectively), Stephen Barclay is currently heading up the position PA

In Belgium, journalists were told to expect her arrival around 6am, and around 30 minutes later Mr Juncker’s chief of staff Martin Selmayr tweeted out a picture of white smoke frothing out of a chimney – a possible reference to the Vatican’s practice of letting off smoke when a new pope has been selected.

The irony of referencing a Catholic tradition, when the key stumbling block to the earlier settlement had been the staunchly protestant Northern Irish DUP, was not lost on social media.

Moments later, pictures were published of Ms May, Brexit Secretary David Davis and Mr Robbins sitting opposite Mr Selmayr, Mr Juncker and Mr Barnier over a breakfast of coffee, pastries and orange juice.