When you are under attack, the game will automatically lock the portion of the resource that can be raided by the attacker. In the meantime, the game allows you to use the protected resource to do building upgrades and summon troops while you are under attack.
Let's say you have total 100,000 Crystal in your city. Your storage protects 20,000, and the resources that can be looted during an attack is 80,000.
When you were under attack, the loot-able resources (80,000 Crystal) would be locked immediately. Let's say during the attack, your enemy took away all loot-able 80,000 Crystals, and in the meantime, you spent all protected resources (20,000 Crystal) in your city that were not locked to build troops or repair buildings.
As a result, when the attack ended, you found out that not only the 80,000 Crystal were raided by the attacker, the *protected* 20,000 Crystal disappeared too and your crystal balance turns into zero. This is because the 20,000 Crystal have already been used during the attack.