I can't talk too much to you about Morocco.
And you have decided to rent a car.
And I think it's very good if you rent a car. It is not very complicated to drive on Spanish roads. But I recommend that when entering cities, leave your car as soon as you arrive and use public transportation for moving through the city. Driving on Spanish roads is a joy. On the other hand, traveling through cities can be a nightmare because it is not easy to park and traffic can become very chaotic. the cost of public transportation is cheap (taxi or uber)
And since you are going to take the car, I recommend several changes in your tour of Spain.
Madrid 2 days. Great. (You do not need a car when in Madrid)
Córdoba 2 days
I would spend a day in Córdoba and see Medina Zahara (Medina Zahara is a now half-ruined city where the most powerful person in the world lived in the year 950. There are companies that take you by bus in the morning to see it from Cordoba 15 minutes (it is very important to be with somebody who tells you the story of Medina Zahara). Then you can go to eat at the half a day to Córdoba (Try restaurant Casa Pepe, very closed to the Mosque of Cordoba), hire a private visit (the same company that brought you to Medina Zahara can provide you the local guide) to see the Mosque of Córdoba, the Patios, the synagogue and the alcázar and sleep in Córdoba.
Seville 2 days.
I would recommend taking the high-speed train to Cordoba and Seville (AVE) because that way you will enjoy this country much more. And then hire the car in Seville (on the fourth or fifth day) (You do not need a car when in Madrid)
The AVE would take you from Madrid to Córdoba 1,30 hours instead of 4 and a half hours by car.
And from Seville to Córdoba by AVE again, which will take an hour and is a joy.
Spain is the country with the most miles of high-speed lines in which if you arrive at your destination more than 30 minutes late you can request a refund.
This country is much more attractive if you use the fast-speed train.
On the first day in Seville, visit the Cathedral/Giralda, Los Alcázares, and eat tapas in the center. Take a horse-drawn carriage to go from the center to the Plaza de España. Visit the Torre del Oro. Get on the panoramic bus to be shown the city with an audio guide, enjoy a flamenco show in the town, and have a tapas dinner in the city area of tapas.
Go to sleep in Seville.
And then rent the car. (the fourth, the fifth day?)
Go with the rented car to Jerez de la Frontera. Spend a day and a night in Jerez de la Frontera.
Gibraltar is fine to stay for 5 or 6 hours but no more.
To get to Gibraltar, I suggest you go down from Jerez, pass through the city of Cadiz, and sign up for a guided tour of the city. And from Cadiz to Gibraltar, stop at one or more of the fantastic beaches along the way. The best beaches in Spain are in Cádiz.
If you go to Valdevaqueros beach you will see hundreds of kitesurf sails in the sky. Tarifa beaches are the best place of Europe for doing wind-surf.
Maybe you can stay a few hours because you were not coming to see beaches in Spain.
In Algeciras there is a ferry that takes you to Morocco.
On your way back from Morocco, try to go to Ronda, which is an unforgettable city, and from there go up to Granada to spend at least one night in Granada.
In Granada it is important that you buy the entrance tickets to the Alhambra in advance. It would be unforgivable to go to Granada and not be able to enter the Alhambra.
And then you can come back to Madrid to take the plane to the USA.
If you decide to take the car from Madrid to Córdoba, I recommend that you stop halfway in Consuegra where you will see a wonderful view of some 16th century windmills of Don Quixote and a castle from the year 950.
Stopping in Consuegra means detouring just 6 miles on your car trip to Córdoba.
If you want more detail please let me know.
Have a good trip