Except calories are the unit of temperature raise in a CC of water whesomething
Food isn't burned in the body under a beaker of water and a thermometer in it
But yes, eat less to lose fat and eat more to gain muscle
If diet used to be very, very bad, you will need an extended period of low carb dieting to get your insulin sensitivity back to a normal level
Starches are also better than sugars for muscular gains and glycogen synthesis, look it up. So diet should be "clean" unless you're having real trouble increasing calories