Hafsid gold coin
In the early 1200s AD, the Almohad empire fell apart into Marinid, Christian, and Hafsid kingdoms.The middle of North Africa (modern Tunisia, western Libya, and eastern Algeria) broke away from the Almohad dynasty in 1229 AD under the leadership of Abu Zakariya. People called his descendants the Hafsids.
The mausoleum (tomb) of Sidi Qasim, in Tunis
The Hafsid kingdom, already wealthy, soon became even more wealthy because many Islamic and Jewish refugees who had fled from the Christians invading Spain came to the Hafsid kingdom to live. (Among these was the family of Ibn Khaldun, for example.) These refugees brought with them money and education and a lot of energy, which they put to work building new houses and new mosques all over the Hafsid kingdom, especially in Tunis. It became common in Tunis for new buildings to have the horseshoe arches and tiles of medieval Spanish architecture, to remind the refugees of the home they had lost.
All through the 1200s the Hafsid kingdom remained strong. They had no trouble in 1270 fighting off the Eighth Crusade, when Louis IX of France invaded Tunis.
During the 1300s the Hafsids were weaker. Many people living in the Hafsid kingdom (like Ibn Khaldun's parents) died of the Black Death. Between 1347 and 1357, the Marinids pretty much controlled the Hafsids, but thanks to the help of the Berber people, the Hafsids were able to fight off the Marinids and get their kingdom back. The Hafsids began to attack European ships in the Mediterranean Sea as a way of getting rich. Of course this made the Italian and Spanish cities angry, and during the 1400s the armies of Venice and Aragon sometimes attacked the Hafsid kingdom, though they did not destroy it. During the 1400s, the Hafsids also traded south across the Sahara with the people of West Africa, and east across the Sahara with the Mamluks in Egypt.
But by the 1500s the Hafsids really could not control their kingdom anymore. They only ruled the city of Tunis, and the rest of their kingdom was pretty much independent cities and towns. The Berber people, who were nomads, were also independent. Even Tunis sometimes fell under the control of the king of Spain. In 1574, the Ottoman Empire finally conquered Tunis and ended the Hafsid kingdom.