Even as it launches waves of terrorist attacks around the globe, the Islamic State is quietly preparing its followers for the eventual collapse of the caliphate it proclaimed with great fanfare two years ago.

In public messages and in recent actions in Syria, the group’s leaders are acknowledging the terrorist organization’s declining fortunes on the battlefield while bracing for the possibility that its remaining strongholds could fall.

At the same time, the group is vowing to press on with its recent campaign of violence, even if the terrorists themselves are driven underground. U.S. counterterrorism experts believe the mass-­casualty attacks in Istanbul and Baghdad in the past month were largely a response to military reversals in Iraq and Syria.

[Turkey and the Islamic State appear to be headed toward outright war]

Such terrorist acts are likely to continue and even intensify, at least initially, analysts say, as the group evolves from a quasi-state with territorial holdings to a shadowy and diffuse network with branches and cells on at least three continents.

Iraqi forces are advancing toward Mosul, the largest city held by the militant group. (Reuters)

Indeed, while the loss of a physical sanctuary would constitute a major blow to the Islamic State — severely limiting, for example, its ability to raise money, train recruits or plan complex terrorist operations — the group’s highly decentralized nature ensures that it will remain dangerous for some time to come, according to current and former U.S. officials and terrorism experts.

“Where al-Qaeda was hierarchical and somewhat controlled, these guys are not. They have all the energy and unpredictability of a populist movement,” said Michael Hayden, the retired Air Force general who headed the CIA from 2006 to 2009.

[ISIS’s allure increases as its territory shrinks]

Islamic State officials, in public statements and in interviews, insist that the group’s “caliphate” project remains viable while also acknowledging that military setbacks have forced a change in strategy.

“While we see our core structure in Iraq and Syria under attack, we have been able to expand and have shifted some of our command, media and wealth structure to different countries,” a longtime Islamic State operative, speaking through an Internet-based audio service, said in an interview.

“We do have, every day, people reaching out and telling us they want to come to the caliphate,” said the operative, who agreed to speak to a Western journalist on the condition that his name and physical location not be revealed. “But we tell them to stay in their countries and rather wait to do something there.”

[ISIS releases a new video of captive British journalist John Cantlie in Iraq]

Libyan forces allied with the U.N.-backed government fire artillery toward ISIS fighters’ positions in Sirte, Libya, July 12, 2016. (Goran Tomasevic/Reuters)

But signs of desperation are mounting weekly inside the caliphate, which shrank by another 12 percent in the first six months of 2016, according to a report last week by IHS Inc., an analysis and consulting firm.

A series of communiques issued in the Islamic State’s Syrian enclave last month closed down Internet cafes in one province and ordered the destruction of TVs and satellite dishes in another.

The orders, billed as an effort to eliminate a tool for “disseminating infidel beliefs,” effectively cut off access to news from the outside world.

‘Without any city or land’

More signals of a coming downfall are contained in statements issued by Islamic State officials over the past six weeks, a period that saw the group’s fighters retreating across multiple fronts, from Fallujah in central Iraq to the Syrian-Turkish border.

A remarkable editorial last month in al-Naba, the Islamic State’s weekly Arabic newsletter, offered a gloomy assessment of the caliphate’s prospects, acknowledging the possibility that all its territorial holdings could ultimately be lost. Just two years ago, jihadist leaders heralded the start of a glorious new epoch in the world’s history with the establishment of their Islamic “caliphate,” which at the time encompassed most of eastern Syria and a vast swath of northern and western Iraq, a combined territory roughly the size of Great Britain.

[How ISIS tries to thwart attacks on its oil infrastructure]

The editorial, titled, “The Crusaders’ Illusions in the Age of the Caliphate,” sought to rally the group’s followers by insisting that the Islamic State would continue to survive, even if all its cities fell to the advancing “crusaders” — the separate Western- and Russian-backed forces arrayed against them.

“The crusaders and their apostate clients are under the illusion that . . . they will be able to eliminate all of the Islamic State’s provinces at once, such that it will be completely wiped out and no trace of it will be left,” the article states. In reality, the group’s foes “will not be able to eliminate it by destroying one of its cities or besieging another of them, or by killing a soldier, an emir or an imam,” it says.

The editorial asserts that the “whole world­ . . . has changed” with the creation of a theocratic enclave that has “shown all of mankind what the true Islamic state is like.”

“If they want to achieve true victory — they will not, God willing — they will have to wait a long time: until an entire generation of Muslims that was witness to the establishment of the Islamic State and the return of the caliphate . . . is wiped out.”

The same themes were repeated in an otherwise upbeat sermon by the Islamic State’s official spokesman, Abu Muhammad al-Adnani, marking the start of Ramadan observances. Adnani’s missive attracted international attention because of its call for a global terrorism campaign during the Muslim holy month. But Adnani also appeared to be preparing his followers for heavy losses.

[In a deadly Ramadan, ISIS terrorism exposes the failures of others]

At one point he evoked one of the darkest chapters in the Islamic State’s history, when the group — then known as the Islamic State of Iraq — was all but destroyed in 2008 by a combination of forces, including the U.S. troop surge and the “Anbar Awakening,” a revolt against the Islamists by Sunni Arab tribes.

“Were we defeated when we lost the cities in Iraq and were in the desert without any city or land? And would we be defeated and you be victorious if you were to take Mosul, Sirte or Raqqa, or even take all the cities?” asked Adnani, referring to the Islamic State’s primary strongholds in Iraq, Libya and Syria. “Certainly not!”

Echoes of an inglorious past

The group’s near-defeat in 2008 also has been cited multiple times in recent weeks in social-media accounts, suggesting to some analysts that its leaders are trying to limit the inevitable damage to the Islamic State’s reputation among jihadists as an unstoppable military and moral force.

[CIA director: ISIS not yet close to being restrained]

“They don’t want to lose territory,” said Cole Bunzel, a doctoral candidate at Princeton University’s Near Eastern studies department who provided a translation and commentary on the al-Naba editorial in the blog Jihadica. “But they’re trying to remind people that the group has a long history and they’re going to persist, just as they did in earlier times.”

The deadly attacks against Istanbul’s Ataturk Airport and Baghdad’s Karrada shopping district — both relatively easy targets for terrorists concerned only with massive numbers of civilian casualties — were probably also part of the same effort to reassure followers of the Islamic State’s vitality, said Will McCants, a Brookings Institution researcher and author of the 2015 book “ISIS Apocalypse: The History, Strategy and Doomsday Vision of the Islamic State.”

“The successful attacks abroad are an indication of deep worry at home,” McCants said. After years of boasting of the group’s invincibility, leaders such as Adnani are beginning to acknowledge battlefield losses while attempting to depict them in the most positive light, he said. Absent from the group’s statements is any acknowledgment of strategic and tactical errors that contributed to the Islamic State’s current predicament, fighting alone against a broad array of forces that includes the major Western powers, Sunni and Shiite Muslims, Russians and Kurds, McCants said.

“They’re not trying to be clever about it,” he said, “but they’re really trying to prepare their followers to cope with a ‘caliphate’ that is no longer a caliphate.”

The Islamic State operative who consented to an interview acknowledged that some of the group’s followers had become disillusioned because of mistakes by individual commanders. He said he didn’t care whether the organization’s headquarters remained in Raqqa or moved to North Africa or elsewhere — although he added that the loss of Raqqa would certainly be avenged.

“There is a message to all members of the coalition against us: We will not forget, and we will come into your countries and hit you,” he said, “one way or the other.”

European intelligence officials fear that the new phase is already underway. “They are . . . challenged as we adapt our strategy to their initial one, in order to start ‘de-sanctuarizing’ them,” said a senior French security official, speaking on the condition of anonymity to discuss counterterrorism strategy. “But they will now expand to other tactics and start executing much more insidious and covert ops, in big cities.

“The next step,” he said, “has begun.”

Mekhennet reported from Frankfurt, Germany. Liz Sly in Beirut contributed to this report.

Read more:

How ISIS inspires home-grown terrorist attacks

In tiny Albania, worries about ISIS’s emergence

Three suicide attacks in Saudi Arabia extend global wave of bombings and a bloody week

Iraqis trapped in Fallujah face twin peril of Islamic State and militia fighters