You first choose a legal target according to your current available mana, the spell starts to resolve, after the target is chosen, you pay the cost of the card (upper right corner) but no additional cost yet (written in the textbox). Just afterwards you do everything written on the card possible. The card only checks for any restricting conditions while choosing a target, not upon resolution. So you empty your mana, and destroy the target - which was legal while checking for viability.
If the game could peek into the future, it would be able to see that you would not have enough mana upon resolution of the spell, but neither does it check for this condition, nor does it care about it after the initial targeting.