What time of year are you planning to take this trip? Southern Spain is oppressively hot in the summer, and Madrid is often unpleasant at that time of year as well.
Your itinerary is tight, but I've seen (much) worse. With four large cities full of sights and/or great day-trip opportunities (Barcelona, Madrid, Seville, Lisbon), you won't have a great deal of time left over for all the other destinations. You have two city shifts requiring flights (into and out of Ibiza) and six other city changes; you should roughly estimate each hotel change will take 1/2 day, so you'll lose about 4 full days for your many relocations. That leaves 21 full days, not counting the probably-jetlagged arrival day. Nine cities are a lot for 21 days.
With this list of target cities and 26 nights on the ground in Spain, I think you should forget any idea of a train between the two countries. There's very, very little rail service between Spain and Portugal--a link north from Porto to a very nice part of Spain you don't have time for on this trip, or a route between Madrid and Lisbon that doesn't show up on most (any?) rail websites, I presume because half of it is run by Renfe and half by CP, the Portuguese rail company.
Including Ibiza requires two fights, unless you want to alter your itinerary and travel from Barcelona to Ibiza and back to Barcelona by ferry. Are you sure Ibiza is where you want to go? Are you very young? From what I gather, Ibiza is a crowded party island these days. The Balearic island with the most varied scenery and historic sights seems to be Mallorca, which is six times the size of Ibiza. On the other hand, switching to Mallorca would make your time problem worse, because Mallorca is a lot larger, so you'd really need to spend more time there.
Rough suggestions for nights spent in each place:
Barcelona: 4 (minimum). Barcelona is tricky because there are six popular sights (not that you have to go to all of them) for which timed entry tickets need to be purchased in advance, because the ticket lines are unbearably long or (in one case) the English-language tour tends to sell out. You simply cannot create an optimum, compressed sightseeing schedule when you have to guess how much time to leave between each pair of entries.
Madrid: 3 to 5 (depending on how you feel about art museums and how many day-trips you want to take (Segovia, Cuenca, Alcala de Henares, etc.)
Toledo: There's easily two full days' worth of sights here (which would require 3 days or an early start from Madrid), but most visitors probably day trip from Madrid, so this is up to you. I'm not sure decamping to Toledo for just one night adds a lot of value if you're going to head out early the next morning without doing additional sightseeing; might as well keep your hotel room in Madrid so you don't have to drag luggage to Toledo. Toledo's a fabulous, very historic city with a large medieval district to explore, in addition to the various sights enumerated in guidebooks.
Cordoba: 2 nights, or 3 if you can spare them. To get from Toledo to Cordoba by train, you must first return to Madrid. It's not a big deal, but it means the trip by express train won't be as fast as you might guess by looking at a map. In other words, you won't arrive in Cordoba super early. Train time will be about 3 hours, which doesn't include moving between hotels and train stations. Pay attention to the calendar: On weekends, the first train from Toledo to Madrid doesn't depart until 9:30 AM.
Granada: 3 nights. With 2 nights you wouldn't have time for very much beyond the Alhambra. Train service into and out of Granada isn't super-frequent, so it's a good plan to check the schedule on renfe.com to see when you can reasonably get to Granada and to your next stop.
Seville: 4 nights or more. Seville has a lot of sights, and they're not concentrated in a small area.
Out of space. To be continued.