On June 10, 2014, the day the black flag of the Islamic State militant group went up over the city of Mosul in northern Iraq, life for an ISIS fighter was good.

The seizure of a city of nearly two million showed that ISIS leader Abu Bakr al-Baghdadi was making good on his claim to set up a “caliphate” across a vast swath of the Middle East.

Foreign fighters flocked to a group once famously mocked by US President Barack Obama as the “JV (Junior Varsity) team.” And why not? After taking Mosul, ISIS fighters were paid $500 a month and given a mobile and a car. Amid the deepening chaos in the Middle East, Daesh, as the group is called in Arabic, had emerged as the strong horse.

Today, the battle of Mosul, Round Two, is underway. The United States, the Iraqi army and Kurdish peshmerga fighters have started an offensive to retake the city. And ISIS, undeniably, is now weakened, its caliphate vastly reduced in size.

The Iraqi government, backed by US airpower and special operations forces, has methodically retaken cities in Iraq’s Sunni heartland that had fallen to ISIS: Tikrit, Fallujah, Ramadi and, soon, most analysts believe, Mosul.

ISIS today controls nearly 50% less territory in Iraq than it did two years ago. The flow of foreign fighters going there has dwindled, and ISIS now conscripts locals for $50 a month — but it has fallen three months behind, former fighters say, in paying even that amount.

It is not only Iraq where ISIS is now in retreat. In Libya, where it had established an important foothold in the central coastal city of Sirte — demonstrating that it could take and hold territory far from Raqqa, its so-called capital in Syria — militia fighters, backed by at least 170 American airstrikes, have nearly retaken the city.

ISIS forces are now fleeing to the south. Soon, the “caliphate’’ will extend not much beyond its stronghold in Syria, and even there, the Syrian Democratic Forces, backed by US special operations forces and airstrikes, have begun to surround Raqqa, cutting off supply lines and preparing the battlefield for an offensive likely to come sometime next year. All of this, to be sure, is a humiliation for Baghdadi.

But demonstrating that ISIS could not create and then hold its “caliphate” indefinitely does not mean it is defeated. The idea that it could sustain its territorial holdings without possessing significant anti-aircraft weaponry to deter US airstrikes was always “fanciful,” says a Western military intelligence official.

Dislodging ISIS from its strongholds was a necessary first step, but it was the easy part in what will be a long struggle. Military and counterintelligence officials and diplomats in the United States, Europe and the Middle East acknowledge that the fight now becomes more difficult for the West — and, many contend, more dangerous.

ISIS needs to adapt to a rapidly deteriorating military situation, and there is already evidence that it is doing so. Consider, as but one example, what has happened in Mosul — and in particular underneath Mosul — in the two-plus years since it fell to ISIS control.

Former ISIS fighters who have recently left the battlefield say their former comrades in arms have painstakingly constructed their own version of the famous Cu Chi tunnels in Vietnam - tunnels outside of Saigon that during the war there gave North Vietnamese fighters freedom of movement, the ability to protect weapons and ammunition from heavy American bombardment and – critically - an escape route that ensured they would be able to move on to the next battlefield.

The former fighters describe a long struggle. Military and counterintelligence officials and diplomats in the United States, Europe and the Middle East acknowledge that the fight now becomes more difficult for the West — and, many contend, more dangerous.

ISIS needs to adapt to a rapidly deteriorating military situation, and there is already evidence that it is doing so. Consider, as but one example, what has happened in Mosul — and in particular underneath Mosul — in the two-plus years since it fell to ISIS control.

Former ISIS fighters who have recently left the battlefield say their former comrades in arms have painstakingly constructed their own version of the famous Cu Chi tunnels in Vietnam - tunnels outside of Saigon that during the war there gave North Vietnamese fighters freedom of movement, the ability to protect weapons and ammunition from heavy American bombardment and – critically - an escape route that ensured they would be able to move on to the next battlefield.

The former fighters describe a similarly intricate network of tunnels with rooms, toilets, medical facilities and enough food to sustain a long fight.

Chillingly, the fighters say the roughly 11,000 men set to defend Mosul have chemical weapons - “chlorine and mustard gas,” according to Waleed Abdullah, a 23-year-old Iraqi who left the battlefield last month. Abdullah believes about 3,000 ISIS soldiers will fight to the death.

The rest, he says, will escape — via the tunnels — and, as the North Vietnamese did nearly half a century ago, continue the fight. And contrary to numerous reports of deteriorating morale among the ISIS rank and file, Waleed says that among the hardcore fighters, “morale is still very high. They will stay underground for a long time since the tunnels are deep and provide the means of life.”

But where will they fight? Current and former intelligence officials in the Middle East, Europe and the United States say their primary fear is that the pace of attacks on soft targets in their regions is likely to increase as the resources and manpower necessary to maintain the “caliphate” shrink.

ISIS, in fact, has been planning for exactly this for years. Mubin Shaikh, a former jihadi who has worked with the Canadian government on counterintelligence matters, says: “When the caliphate was establishing, they were talking about the inevitability of Western forces attacking them because they were overrunning territory in Iraq and that had already triggered a U.S. response.”

The flow of migrants from Syria, Libya and Iraq to Europe - and the certainty among some security officials that ISIS has slipped its members into that flow - already has Western counterterrorism units stretched to nearly the breaking point.

It’s about to get worse. Richard Barrett, a former head of counter-extremism at MI6 in the UK who now works for security consultancy the Soufan Group, says flatly that “there will a greater threat as ISIS continues to lose territory, as numbers [of fighters flowing back to Europe] will increase and security services will be even more hard-pressed, making an attack more likely to slip through the net. This will lead to greater public anger, more pressure on security services, and it will become a spiral.”

The inability to hold territory creates “ISIS 2.0,” as a senior Middle Eastern intelligence official puts it. “It means that ISIS adapts and begins to look more like Al-Qaeda,’’ whose top leadership all along warned Baghdadi that he couldn’t sustain a caliphate across several countries and was foolish to try.

“They may not be able to maintain the physical caliphate, but they canbuild and maintain networks,” the intelligence official adds.

How effective will ISIS 2.0 be? Not everyone shares the grim view that MI6 alumnus Barrett sketches out. Some posit that the loss of territory will diminish ISIS’s ability to pull off high-profile, mass-casualty attacks - just as Al-Qaeda’s loss of its sanctuary in Afghanistan after 9/11 did. That, as Obama administration officials say, is no small thing. Two key questions going forward will confront the ISIS leadership.

The first is financial. The methodical rolling up of the caliphate that has been Obama’s strategy for the past two years has undeniably made it more difficult for ISIS to make money. It has degraded ISIS’s access to oil money in both Iraq and Syria,

forcing it to turn more to extortion.

That, in turn, further alienates populations living under ISIS’s thumb. In Al-Qayyarah, Iraq, a town that Baghdad’s forces liberated from the group in late August, smoke from a burning, bombed-out oil well that used to generate income for ISIS now blackens the daytime sky.

But Abu Ahmad, who lives just 100 yards away from the burning well, says, “I prefer the [smoke] from the well to life under Daesh. Everything was bad and dark under them.”

The second issue for Baghdadi and his top lieutenants is one of face: How much does the loss of territory damage the ISIS brand in the eyes of would-be jihadis all over the world?

When ISIS was the strong horse, recruiting fighters was easy. During its rise, its motto, repeated endlessly by Abu Mohammad al-Adnani, the group’s chief propagandist killed in an airstrike in August, was “Remaining and expanding.”

Now that they’re neither remaining nor expanding, optimists believe the loss of territory “will hurt them, disrupt them and decrease the flow of foreign fighters attracted to the group,” says Nada Bakos, a former CIA officer now at the Foreign Policy Research Institute. According to this view, we likely have seen peak ISIS, its ability to pull off significant attacks damaged and the prestige that attended fighting for it seriously eroded.

Many US and European counterterrorism officials doubt it will be that simple. Officials concede they are deeply concerned not so much that the numbers of fighters flowing back into the West will increase but about the type of fighters likely to return.

According to Rob Wainwright, director of Europol, the European Union’s law enforcement agency, at least 5,000 European passport holders went to Syria and Iraq to fight, and only about a third of them have returned. The working assumption in Europe is that many will eventually try to come to Europe.

Gilles de Kerchove, the EU’s counterterrorism coordinator, said he’s particularly concerned about the skills the returnees have learned fighting in the Middle East.

“One of the risks is that these guys have learned a lot in terms of weaponised chemical stuff, in terms of using car bombs. I hope this knowledge will not be used here, but we have to be vigilant.”

Those concerns are legitimate, say fighters who have recently left ISIS. The flow back to the West has already begun. “It’s true that they are losing areas in Iraq and Syria,” says Waleed, “but they have other options.”

He says that in the past two years, ISIS has sent more than 300 “sleepers” from the region to different Western nations.

“They first go to Turkey, where they get a fake passport,” and then slip out individually via varying routes to the West. One such fighter is from the same town in Iraq as Waleed — Hawijah, south of Mosul — and he had plastic surgery in Turkey “in order to be unrecognised.”

U.S. counterterrorism officials argue that military success against ISIS will make this type of transit more difficult, and thus ISIS’s ability to carry out even smaller-scale attacks abroad will be more limited.

Turkish forces entered Syria in late August, ostensibly to combat ISIS but also, officials in Ankara have acknowledged, to prevent the Syrian Kurdish People’s Protection Units (the militia known as YPG) from setting up an enclave on the Syrian border with Turkey.

But if the result is that Turkey shuts down its border — as it appears to have done so far — and stops turning a blind eye to ISIS fighters and weaponry slipping in and out of Syria, that’s a win for the anti-ISIS coalition.

“We’ll see,” says one senior Middle East intelligence source about whether Ankara’s newfound vigilance lasts. The official’s wariness is rooted in other facts on the ground that may work to ISIS’s benefit, despite the loss of “face” associated with battlefield defeats.

As former Defense Intelligence Agency official Michael Pregent says, the strategy to defeat ISIS amounts to “rubble-ising” predominantly Sunni cities in Iraq and Syria.

“Eighty percent of Ramadi was destroyed. Half of Fallujah was destroyed. Now the UN and other nongovernmental organizations are already fretting about the massive number of refugees that the Mosul offensive will create,” says Pregent. (Some 1.2 million Iraqis remain in Mosul.)

“Are we really defeating ISIS? There are 20 million Arab Sunnis in the northern Middle East — 350,000 military-age young men in Mosul alone — all asking [the United States], ‘What are you doing ?’”

ISIS, remember, arose in Iraq largely in reaction to the strident sectarian leadership of former Prime Minister Nouri al-Maliki, a Shiite who, once the US left in 2009, set about purging Sunnis from the military and senior positions in the Iraqi government.

Many Sunnis believe the US has effectively thrown in with Iran and the Shiite militias in Iraq to defeat ISIS. The former Iraqi Baathist intelligence and military officials who make up ISIS’s leadership under Baghdadi know that despite the loss of territory, those disaffected young men are ripe for recruiting.

The same is true in Syria, where some 400,000 Sunnis (and counting) have died in that country’s civil war, for which there is no end in sight now that the ceasefire negotiated by the US and Russia has quickly fallen apart.

The sectarian chaos only increases. Shiite militias from Iraq have been deployed in Syria to fight Sunni rebel groups, including ISIS but also including Jabhat Fateh al-Sham (formerly the Nusra Front) and Ahrar al-Sham — both of which have been active in defending the besieged city of Aleppo from Russian- and Iranian-backed Syrian forces. Through such actions, the Sunni groups have “secured [their] place in the hearts and minds of the Syrian people,” according to a recent intelligence report.

Why are those groups significant? They are both affiliates of Al-Qaeda. In the public mind in the United States — and, for a time, within the US government — Al-Qaeda had become an afterthought.

Osama bin Laden was dead; his second-in-command, Ayman al-Zawahiri, was said to be a doddering, uncharismatic bean counter; and, by 2014, it became clear ISIS was not the “JVs.”

But even though ISIS undeniably cut into “Al-Qaeda’s share of the jihadi market,” as Thomas Joscelyn, senior fellow at the Foundation for Defense of Democracies, puts it, under Zawahiri’s stewardship, Al-Qaeda also grew its largest paramilitary force ever — in Syria alone, Jabhat Fateh al-Sham has 10,000 fighters loyal to it.

A senior US intelligence official acknowledges as much and says the tempo of airstrikes targeting fighters with Al-Qaeda affiliates has increased in recent months.

The defining feature of the West’s war against radical Islamic militants has been its constant shape-shifting; at times, it can seem like whack-a-mole — enemies are killed, others pop up.

The fortunes of the opposition wax and — in the case of ISIS now — wane, as they figure out new ways to attack. ISIS, now degraded, as Obama vowed, will necessarily be in transition but perhaps no less lethal. Al-Qaeda is expanding, patiently, and still has ambitions for large-scale attacks in the West, at a time and place of its choosing. The war grinds on.

In early 2013, on his last day as acting head of the CIA, Michael Morell told Obama, who came to office wanting to end wars, “My children’s generation and my grandchildren’s generation will still be fighting this fight.”

Additional reporting by Mahmoud Shikh Ibrahim in Erbil, Iraq, and Jack Moore and Mirren Gidda in London.