I lived in Madrid and it was truly the best experience!
As for where to stay, I don't think Madrid has good hostels, I've never been in one because I had my own apartment. So, try to find a hotel somewhere in Sol, along Gran Via, or Plaza Major. That's where you'll find all the attractions.
In Madrid, everything is accessible by metro, which is very easy to navigate and pretty cheap and safe! I'm pretty sure the Madrid metro system is one of the best in Europe too - so please take the metro when you can! Save the euros and make use of the metro. The metro closes pretty late, so take the metro to go to the clubs and bars. If you stay in Sol, you'll pretty much walk to the cafes, bars, and clubs. If you're lost, take the taxi, its pretty affordable too!
The gay part of Madrid is Chueca, which I actually lived there before because my university had a residential contract there. It's kinda dirty, but some of the best cafes are there (for coffee). At night, it can be fun there too. But, realistic everywhere in Madrid is fun, where there is a bar, theres enjoyment. But if you enjoy drag shows and karaoke - spend time in Chueca, just stay safe!
In terms of food, try all the tapas that you can. Enjoy them with some sangria ! and definitely find chocolate con churros if you're a chocolate lover.
In terms of touristy stuff:
Visit the Prado Museum
Rein de Sofia Museum
Theres so much I want to tell you to do, but I don't know how else to continue. Feel free to message me and I'll guide you as much as I can.
PLEASE FEEL FREE TO USE ME AS A TOUR GUIDE, I LOVE MADRID SO MUCH AND I WANT YOU TO BE ABLE TO ENJOY IT AS MUCH AS I DID.