IT HAS been a long war, with many horrors. But three years after Abu Bakr al-Baghdadi ascended the pulpit of the Nuri mosque in Mosul to call on all Muslims to flock to his “caliphate”, Islamic State (IS) is suffering two crushing blows. In Iraq, the jihadists have all but lost Mosul; they blew up the Nuri mosque in their last stand in a pocket of alleyways (see article). Mr Baghdadi, if he is alive, may have fled to Syria. But IS is faring badly there, too. American-backed fighters have pushed into the old city of Raqqa; IS’s Syrian stronghold will fall soon. With the loss of its biggest cities, the mystique of the jihadist “state” is being shattered. IS is turning into a nasty militia, and may yet become a nebulous terrorist group, like al-Qaeda. The world should rejoice at its decline. IS committed untold barbarity, from mass beheadings to sexual enslavement, and inspired jihadists to kill the innocent in Europe and elsewhere. America has led the campaign ably, giving training and air support, while letting local allies do the ground fighting.

But what will come after the caliphate? No one knows, least of all Donald Trump. He has yet to fill nearly 200 foreign and national-security jobs, and policymaking in his administration is dysfunctional. That is alarming. Adrift, America faces two perils: one is a vacuum that allows jihadists to regroup, as they did repeatedly in Iraq; the other is being sucked into a regional war that would make everything worse.

Conflict, wrapped in strife, inside a war

The bloodbath in the Middle East consists of many conflicts: revolts against oppressive rulers who failed to earn legitimacy or foster prosperity; struggles over competing forms of Islam; regional contests between Saudi Arabia, Iran and Turkey; and rivalry between America and Russia. At its heart is Sunni Arabs’ sense of dispossession, of which IS is the most grotesque manifestation. So America needs a strategy with many dimensions, blending military, political and economic means, and taking account of the complex geopolitics that feeds chaos.

There is no sign that Mr Trump’s administration is engaged in such thinking. On his first trip abroad, to Saudi Arabia, he was feted by Arab and Muslim leaders, and regaled with juicy defence contracts. In return, he seemed to embrace the Saudi worldview—and thus precipitated a crisis among America’s Gulf allies. Emboldened, the Saudis and Emiratis (along with the Egyptians and Bahrainis) cut off land, sea and air links with Qatar. They accuse the upstart gas-rich kingdom, which hosts America’s biggest air base in the region, of supporting terrorism (a charge often levelled at the Saudis). Mr Trump denounced Qatar on Twitter. But Rex Tillerson, America’s secretary of state, instead chastised the boycotters; James Mattis, the defence secretary, signed a deal to sell F-15 jets to Qatar, and sent two warships to call at Doha. Amid such incoherence, a strategy may fail, even if one existed.

Mr Trump seems to have three vague aims: destroy IS, roll back Iran’s growing power and reduce America’s involvement in the Middle East. These are inconsistent, because sustained diplomatic and military engagement will be needed both to prevent a new IS rising from the ashes of the old and also to contain Iran, which is using its proxies and its own forces dotted around the region to extend its influence.

Start with IS. To lock in its victories against the jihadists, America must back pluralist and decentralised politics (don’t call it democracy) that gives voice to the region’s disparate groups, especially Sunni Arabs. In Iraq, America must urge the government to woo Sunnis and to include them in running the country; and it should use its air power, special forces and diplomats as a counterweight to Iran.

Syria will be even harder. The end of the civil war is a long way off. Mr Tillerson has put out feelers to Russia about co-operating to stabilise the country, but what he wants is unclear. For now, America can help local allies push down the Euphrates valley, not just to defeat the remnants of IS but also to ensure that border crossings are in friendly hands. That would also secure an American voice in future peace talks.

This will mean redoubling the effort to train more moderate Sunni Arabs. The alliance led by Kurds is unlikely to want to go beyond Raqqa. The priority will thus be to foster a southern Arab force to fight and draw away support from IS.

Inevitably, that will bring America into further contact with Iran. America’s forces and their allies are already rubbing against Syrians and others pushing eastward, seeking to link up with Iranian-backed Shia militias in Iraq and create a land corridor from Iran to Lebanon. America shot down a Syrian jet and bombed Iranian-backed fighters that threatened American and allied forces at al-Tanf, near the southern border with Jordan and Iraq. As if that were not complex enough, Mr Trump says that Iran “has been put on notice”, although he has for now kept to Barack Obama’s deal that froze Iran’s nuclear programme. As the administration debates a new Iran policy, there is much talk of fresh sanctions, with some arguing for “coerced democratisation”: ie, toppling the mullahs.

America could thus end up at war with Iran by military accident, or by being goaded into it by Gulf allies or because it succumbs again to the delusion that overthrowing foes is easy and painless. A conflict with Iran is unlikely to unseat the clerics; but it will undoubtedly spread instability and Shia radicalism. Iran is too big to be excluded from the region’s affairs; but it can be contained by a mixture of pressure and dialogue. America should therefore work to end the row among its Gulf allies, which benefits Iran, just as it should act to halt the war in Yemen, which creates space for al-Qaeda.

American power, vital but limited

Whatever America does will be messy, and its aims ought to be limited. The wars in Afghanistan and Iraq after the 9/11 attacks show that it is good at defeating foes but poor at building lasting institutions; grand plans to remake countries are usually doomed. Yet the collapse of Syria and the rise of IS since 2011 show that neglect poses grave dangers, too. America has no choice but to contain the worst of the tumult. Mr Trump needs to hire good experts and draw up a plan—fast.