5 Nights Barcelona
4 Nights Granada
4 Nights Madrid
I would say that 4nights is too much for Granada. There are some very nice places near Barcelona which you could visit for 2 of those, like Girona or the Costa Brava.
Looks like a nice trip! I would suggest you consider Sevilla.
Granada is of course very nice. But Sevilla is also nice and has more to see. Below are the 3 and 2 triangle attractions in both cities (per Rick Steves):
Granada
https://www.ricksteves.com/europe/spain/granada
▲▲▲ The Alhambra The last and finest Moorish palace, highlighting the splendor of that civilization in the 13th and 14th centuries. Reservations are a must for a daytime visit.
▲▲ Royal Chapel Lavish 16th-century chapel with the tombs of Queen Isabel and King Ferdinand.
▲▲ San Nicolás Viewpoint Breathtaking vista over the Alhambra and the Albayzín.
Sevilla
https://www.ricksteves.com/europe/spain/sevilla
▲▲▲ Flamenco Flamboyant, riveting music-and-dance performances, offered at clubs throughout town.
▲▲▲ Cathedral and Giralda Bell Tower The world's largest Gothic church, with Columbus' tomb, treasury, and climbable tower.
▲▲▲ Royal Alcázar Palace built by the Moors in the 10th century, revamped in the 14th century, and still serving as royal digs.
▲▲ Hospital de la Caridad Former charity hospital (funded by the likely inspiration for Don Juan) with gorgeously decorated chapel.
▲▲ Church of the Savior Sevilla's second-biggest church, bristling with Baroque altarpieces.
▲▲ Basílica de la Macarena Church and museum with the much-venerated Weeping Virgin statue and two significant floats from Sevilla's Holy Week celebrations.
▲▲ Triana Energetic, colorful neighborhood on the west bank of the river.
▲▲ Bullfight Museum Spain's largest bullring, considered the mecca of bullfighting; also houses an impressive museum.
▲▲ Evening Paseo Locals strolling in the cool of the evening, mainly along Avenida de la Constitución, Barrio Santa Cruz, the Calle Sierpes and Tetuán shopping pedestrian zone, and the Guadalquivir River.
Have a great trip!
Be sure and visit the Prado art museum in Madrid. I also loved the adjoining Jardín Botánico, and the nearby naval museum was a treat for me also (though it's more of a particular taste).
The three major art museums in Madrid are all excellent, but the Prado is transcendent.
Consider six nights in Barcelona and three in Granada.
Barcelona offers an excellent public transport system connecting the subway directly to train stations. The result is ease of taking day trips to a variety of interesting locales. Recommend four days exploring Barcelona with two day trips.
Three nights in Granada offers a relaxing exploration of a wonderful destination.
Four nights in Madrid allows thorough examination, but also the option of a day trip.
Stick to three destinations and you have the makings of enjoying exploration of three vastly different areas of Spain..
Enjoy!