First, I'd stay in Barcelona for the simple reason that you can never be sure what time you'll arrive at the train station for the train to Madrid. If you buy in advance (huge savings), you may miss the train. If you wait till you're through the airport, you'll be paying a steep price for the train tickets. I also think that Barcelona is not as intense for sightseeing, so you'll get more out of your first day or two while you get over the jetlag.
Second, I think trying to go to Tangier wastes a lot of time. It's a long trip there and then a long trip to Granada. I considered it on my first trip to Spain and dropped it. My impression was it's like going to Tijuana to see Mexico. It's not Morocco, it's a border town. Better to spend the time in Jerez or Ronda - or even better, add to one of your other destinations.
If you start in Barcelona, then maybe fly to Granada from there, and end in Madrid. From there you can take the train to Barcelona/airport. Just reverse the plan.
Toledo and Cordoba are my two favorite cities. Toledo is more demanding because it is hilly, while Cordoba is flat. Cordoba has more to do in the evenings - Royal Stables horse show, flamenco, lots of restaurants, night tour of the Mezquita, etc. So I would spend 2 nights there and only one in Toledo.
I suggest you rewrite your itinerary, looking at nights instead of days. Then figure that for every x number of nights, you have x-1 number of days. 3 nights in Madrid = 2 days of sightseeing. It also gives you a better idea of how much hopping around you're doing. Allow a little down time to drink some wine, eat tapas, and people-watch and recharge your batteries.