“I still have a few more months. Eight months and 52 days – not that I’m counting. I just made that up. I don’t know, it’s something like that.”

Stepping away from the podium bearing the presidential seal, padding languidly around the platform, it was a reflective, ruminative Barack Obama that an invited “town-hall” audience witnessed on Saturday.

The principal task of the trip – in all probability his last as US president to these shores – had been completed with some aplomb the previous day: a stop-the-presses, potentially slam-dunk intervention into Britain’s Leave/Remain EU referendum debate. Not that he was letting on to the young audience invited to the Edwardian Royal Horticultural Halls, in central London. “I guess you all know why I came this week. It’s no secret,” he said. “Nothing was going to stop me from wishing happy birthday to Her Majesty… I can’t tell you what we talked about. I can tell you that I hope I am such an engaging lunch partner when I am 90.”

His warning on Friday was that Brexit Britain would be “at the back of the queue” in any future trade deal with the US. It was a piece of hard-hitting realpolitik. Obama expounded the philosophy behind the intervention after a morning trip to Shakespeare’s Globe Theatre for a special performance of scenes from Hamlet to mark the 400th anniversary of the Bard’s death.

The EU received only a cursory mention. Certainly Boris Johnson couldn’t have been further from the president’s mind. But the mood music, played in a hall adorned with the stars and stripes and union jack, was clear enough. “We go back a pretty long way, the UK and the US,” he told the 500-strong audience in the tiered hall. “We’ve had our quarrels – there was that little tea incident.

“The British burned my house down. But we made up, ultimately we made up and ended up spilling blood together on the battlefield, side by side, against fascism, against tyranny, for freedom, for democracy.

“From the ashes of war we led the charge to create the institutions and initiatives that sustain a prosperous peace: Nato, the UN, the Marshall Plan, the EU. The joint efforts and sacrifices of previous generations of Americans and Brits are part of why we have known decades of relative peace and prosperity and that in turn has helped spread peace and prosperity around the world.”

Reject “isolationism and xenophobia”, urged Obama, along with “pessimism and cynicism” and the temptation to withdraw into “tribal” loyalties.

What gave the president hope, he said, was that the generation staring back at him in the hall today saw “immigration and globalisation not as threats but as opportunities”. Do not snap shut the drawbridge, he asked. “I want to ask you to embrace the view of one of my predecesors, president JFK, who once said: ‘Our problems are manmade. Therefore, they can be solved by man and man can be as big as he wants.’”

Obama’s opening remarks were followed by a lengthy question and answer session with the audience, most of whom were aged between 18 and 30, and selected by ballot from the US Embassy’s Young Leaders UK programme or invited from sixth-form colleges visited by the ambassador. Alongside them sat a smattering of famous faces, including Benedict Cumberbatch and his wife Sophie Hunter, Mark Carney, governor of the Bank of England, the actress Holly Valance, musician Annie Lennox and designer Ozwald Boateng. A few political aides had also grabbed a prized ticket and had front-row seats.

There were no questions on Europe. Instead Obama was quizzed on the Northern Ireland peace process, Somalia, the transatlantic trade agreement and racial profile screening at airports.

Maria Munir, a young woman from a Pakistani-Muslim background, came out to Obama as a “non-binary person” and spoke of her campaigns for greater respect when it came to using pronouns to describe people and an increase in the number of “gender neutral” toilets.

The reality was that this was never likely to be an audience to challenge the president. Instead, Obama was left to muse on how history would judge him. “I don’t think I’ll have a sense of my legacy until 10 years from now, when I can look back with some perspective and get a sense of what worked and what didn’t,” he said, jacket by now draped over a nearby chair, and shirt sleeves rolled up. “There are things I’m proud of: the basic principle that in a country as wealthy as the United States every person should have access to high-quality healthcare that they can afford, that’s something I’m proud of.

“Saving the world economy from a great depression, that was pretty good.”

He said he was confident that as president he had been true to himself and maintained his core integrity. He also spoke of the importance of compromise and trying to understand the other person’s point of view to make progress on the world’s problems – not least in coming to a deal with Iran over its nuclear programme without more war. “It will also help you, by the way, if you decide to get married,” he added, to laughter.

After an hour and a half, an evidently tiring Obama brought the event to a close, describing the audience as inspirational. After handshakes with the audience, amid a barrage of flashing cameras, he then walked into a 30-minute meeting with the Labour leader Jeremy Corbyn before the presidential motorcade moved on to The Grove golf course in Chandlers Cross, Herts, where David Cameron was waiting to play a round of golf with him.

But it was one of Obama’s final comments, as he talked about how he viewed the presidency, that may give Cameron some food for thought.

“I consider myself a runner,” Obama had told the crowd. “I run my leg of the race but then I have got a baton and I am passing it on to the next person; hopefully they are running in the right direction as opposed to the wrong direction and hopefully they don’t drop the baton.”

Obama did his thing last week. Can the prime minister now take the Remain baton over the line?