President Barack Obama boards Air Force One during his departure at Jorge Chavez International Airport in Lima, Peru, Sunday, Nov. 20, 2016. Obama is heading back to Washington after traveling to South America to attend the annual Asia-Pacific Economic Cooperation (APEC) forum. (AP Photo/Pablo Martinez Monsivais)

President Barack Obama boards Air Force One during his departure at Jorge Chavez International Airport in Lima, Peru, Sunday, Nov. 20, 2016. Obama is heading back to Washington after traveling to South America

LIMA, Peru (AP) — On the surface, President Barack Obama was cool-headed, cerebral and confident, doling out parting reassurances to world leaders on his final global jaunt. Behind the scenes, his aides and Democrats back at home were a deflated and weary bunch, grimly ending Obama's tenure in a way they never imagined.

The jarring gap between Obama's public face and the prevailing Democratic sentiment about Donald Trump's election was an unexpected element to Obama's final presidential trip, which included visits to Greece, Germany and Peru. Time after time, Obama opted for optimism over any sense of foreboding, and diplomatic dodges over criticism. The man whose legacy risks being devastated by Trump's election appeared to be the one Democrat who wouldn't publicly fret about its impact.

As Obama hopped from capital to capital, news of the emerging Trump administration followed him, dominating news conferences and private meetings with leaders. The president answered nearly every question about Trump, his new hires and his plan to dismantle Obama's legacy, with a version of the same answer.

"Reality will force him to adjust how he approaches many of these issues," Obama said Sunday as he closed out his trip in Lima. "That's just how this office works."

It was far from clear that other Democrats shared that optimism.

Each time Air Force One landed in another foreign capital, cellphones buzzed and White House officials' faces fell as the latest news came in about Trump's team-in-waiting: First, Steve Bannon, former head of the far-right outlet Breitbart News, as chief White House strategist, then retired Lt. Gen. Michael Flynn, who has said Islam is a "cancer," to be national security adviser.

And as Obama was somewhere over the Atlantic Ocean, flying to Lima, Trump picked immigration hard-liner Sen. Jeff Sessions for attorney general and national security hawk Rep. Mike Pompeo for CIA chief.

Taken together, the selections deflated the hopes in the White House that Trump, faced with the awesome duty of running the nation, might tone it down after the campaign

Of all the Trump's choices, White House officials said it was the selection of Flynn that felt like the most devastating blow, given the immense authority the national security adviser has over matters of war and peace.

By the time Obama arrived in Peru, the creeping sense of despair among his aides was palpable.

Even still, Obama told a young student from Peru the next day that she shouldn't assume the worst. When she asked whether anxiety about Trump was media-driven "global paranoia," Obama's advice was to "wait until the administration is in place."

Though he acknowledged that Trump's vehement opposition to free trade deals would create tensions, he stood firm that Trump, despite his campaign rhetoric, was committed to NATO, echoing his message to leaders in Berlin and Athens. He didn't dwell on Trump's tough talk about immigration, a key concern in Latin America.

"With respect to Latin America, I don't anticipate major changes in policy from the new administration," Obama said.

If Obama seems more assured than fellow Democrats about how Trump will govern, that's because he is, said individuals familiar with his thinking.

In their Oval Office meeting after Trump's victory, Obama discussed his upcoming trip and said he'd be quizzed on what to expect from Trump. Informed by their conversation, Obama felt confident enough to tell foreign leaders that Trump, for example, would stand behind NATO and wouldn't cut ties to Latin America, said the individuals, who weren't authorized to comment publicly and requested anonymity.

Whether world leaders are taking Obama — and therefore, Trump — at his word remains to be seen. After all, there may be a credibility gap for Obama, who spent the last year reassuring anxious allies that Trump would never win.

Since Election Day, Obama has refused to entertain any wallowing in despair, nor has he engaged in the kind of warts-and-all diagnosis of what went wrong that many Democrats crave, in hopes of ensuring their devastating loss isn't repeated. Instead, his message has been to buck up and keep calm.

It started within hours of Trump's victory, when Obama called shell-shocked aides in for a morning meeting and told them, essentially, to get it together, keep their heads up, and go about their work, aides said. This is politics, stuff happens, and this is how the game is played, the president told his team.

For Obama's aides, many of whom sobbed openly in the Rose Garden hours later when he addressed the nation, it was the slap in the face needed to be able to focus on a task they never imagined having to perform: preparing to hand over the White House to Donald Trump.

That process started immediately — before the nation had a chance to truly grasp what happened — and by the next day Trump was there, in the Oval Office, sitting down with Obama while his aides toured the West Wing.

___

EDITOR'S NOTE — Josh Lederman has covered the White House and national politics for The Associated Press since 2012.