The suicide blast tore open the gates of the compound. One worker inside saw a shower of glass shards and jagged pieces of the explosives-rigged truck.

Then seconds later Tuesday — as an ash-gray cloud rose over Kabul — Taliban gunmen opened fire on Afghanistan’s equivalent of the Secret Service. For three hours, a gun battle raged even as some agents were trapped under collapsed walls and ceilings.

Just a week earlier, the Taliban vowed to escalate attacks as the Afghan weather warms.

The militants now appear to have followed through with the warnings in a shattering tally: at least 28 people killed, more than 325 injured and authorities left struggling over how Taliban attackers outwitted security patrols to carry out one of the most devastating attacks in Kabul in years.

The target — the main training ground for an Afghan intelligence unit tasked with protecting senior officials — represented a direct strike against the Western-aided government as it takes the lead role in the fight against the Islamist militants.

The raid also was a message that the reach of Taliban fighters — and their ability to stage major coordinated attacks — appears undimmed despite rifts within the militant group’s ranks and pressures from the rival Islamic State as it seeks to expand its influence in Afghanistan.

For leaders in Kabul, it may shatter for now any hope of reviving stalled peace talks with the Taliban, and puts President Ashraf Ghani under growing pressure from rivals over his efforts to reach out to the Islamist insurgent group.

The attack ended several weeks of relative calm in the Afghan capital. It began when a suicide bomber detonated a truck packed with explosives next to the security compound, said Kabul police spokesman Basir Mujahid. The blast was so powerful that it shattered windows and cracked building facades up to two miles away.

After the explosion shredded part of the security compound, at least one gunman entered the compound, touching off a three-hour gun battle less than a mile from the presidential palace and the Defense Ministry in a densely populated part of the city.

One gunman was killed, said Sediq Sediqqi, a spokesman for the Interior Ministry. Full details about the number of attackers and their tactics, however, remained unclear as the investigation unfolded.

“No doubt there was a security vacuum, and that needs to be investigated. It is too early to comment on that right now,” he told reporters.

The Taliban claimed responsibility, even as the casualties were still being counted.

Ghani said the attack showed that insurgents cannot defeat Afghan forces­ in a “face-to-face battle.”

The Health Ministry said at least 327 people were wounded, many of them civilian passersby. Mujahid, the police spokesman, said by phone that “the death toll is between 28 and 30.”

One worker at the intelligence agency, who goes by the single name Mehrabuddin, said he was resting on a bed when the truck bomb exploded.

“I rushed out of the room and was hit by debris, bricks, shrapnel and flying glass in the yard of the compound,” he said at Wazir Akbar Khan Hospital, where he was treated for injuries to his head and stomach.

He said about 50 officers were studying in a room where the ceiling caved in from the explosion.

“I do not know what happened to them,” he said.

A truck of the International Committee of the Red Cross later brought medical assistance to the hospital. Volunteers rushed to donate blood.

The Taliban announced the start of its spring offensive on April 12. Fighting has since flared around the northern city of Kunduz, Afghanistan’s fifth-largest city, but Kabul had remained relatively quiet.

Kunduz fell briefly to the Taliban in September. That marked the biggest setback to Ghani’s government since NATO-led ­forces­ ceased their combat operations in Afghanistan at the end of 2014.

The coming months are seen as a critical test of the Taliban’s strength and unity.

In last week’s announcement, the Taliban vowed to carry out “large-scale” attacks as the weather warms.

Yet the group has been racked by internal splits after the public acknowledgment last year of the death of its longtime leader, Mohammad Omar. Some factions favored exploring peace efforts with the Afghan government. Others, though, called for bolstered offen­sives to regain territory and counter moves by the Islamic State to find new footholds in Afghanistan.

At the same time, the U.S.-led coalition withdrew all but 13,500 troops last year, leaving the Afghan military at the forefront of the fight against the militants. The Taliban, in turn, has made steady gains in southern, eastern and northern Afghanistan.

Tuesday’s attack in Kabul was a stinging blow to Afghan forces­ amid efforts to enhance security in the capital after an earlier string of high-profile Taliban incursions. In November 2014, militants carried out back-to-back suicide blasts, including one in a Kabul district that houses many embassies and foreign compounds.

In a statement Tuesday, the United Nations called on the Taliban to stop attacks in civilian areas.

“The use of high explosives in civilian populated areas, in circumstances almost certain to cause immense suffering to civilians, may amount to war crimes,” said Tadamichi Yamamoto, the secretary general’s deputy special representative for Afghanistan.

The U.S. military’s top commander in Afghanistan, Gen. John W. Nicholson, portrayed the attack as a sign of the insurgents’ “weakness.”

“Today’s attack shows the insurgents are unable to meet Afghan forces­ on the battlefield and must resort to these terrorist attacks,” Nicholson said in a statement.

Daniela Deane in London and Brian Murphy in Washington contributed to this report.