It's prohibited.
The problem with medicine is a gzera to avoid grinding medicines (see SA OC 328.1). Pills are not food.
See shulchan_aruch_oc_328_37
Regarding recreational pill, It’s prohibited despite that from the first glance in SA OC 328.37, we may mistakingly think that it’s permitted. Here is the text of the SA. He says that an entirely healthy person can take a food that healthy persons don't take usually.
כל אוכלים ומשקין שהם מאכל בריאים מותר לאכלן ולשתותן לרפואה אע"פ שהם קשים לקצת בריאים ומוכחא מילתא דלרפואה עביד אפילו הכי שרי וכל שאינו מאכל ומשקה בריאים אסור לאכלו ולשתותו לרפואה ודוקא מי שיש לו מיחוש בעלמא והוא מתחזק והולך כבריא אבל אם אין לו שום מיחוש מותר.
It’s from the Tur. And underanding the Tur, we can understand more clearly the SA.
כל שאינו מאכל ומשקה בריאין אסור לאוכלו ולשתותו לרפואה. אבל אם אוכל ושותה אותו לרעבו ולצמאו ואין לו חולי, שרי
A food or a drink that is not usual for healthy persons… But if he eats and drinks it because he is hungry or thirsty and he is not ill, it’s permitted. The Magen Avraham understands from this Tur that it’s permitted only if he eat it with an alimentary intention only.
Recreational purpose is not necessarily similar to drinking Alcohol, wine and alcohols have a good taste, they are a part of meals and aren't at all used as medicines.
The Chovot Halevavot advises anxious persons to drink wine, see Shaar Haperishut chapter 5, following the Mishle.
אבל משתך יהיה מים אלא אם תהיה כוונתך בשתית היין לתועלת גופך או להעביר דאגה מלבך כמ"ש (שם לא) תנו שכר לאובד ויין למרי נפש
Mishle 43.6
תְּנוּ שֵׁכָר לְאוֹבֵד וְיַיִן לְמָרֵי נָפֶשׁ
Give strong drink to him who is ready to perish, and wine to those whose hearts are heavy.
We can ask, is it permitted to drink Whiskey during a very mild anxiety state? For a moderate to severe psychiatric episode all medicines are permitted.
So, Alcohol is viewed as a psychiatric drug. It's permitted even in Shabbat, the same Sayf of SA says
כל אוכלים ומשקין שהם מאכל בריאים מותר לאכלן ולשתותן לרפואה אע"פ שהם קשים לקצת בריאים ומוכחא מילתא דלרפואה עביד אפילו הכי שרי
All foods and drinks that are food for healthy persons can be used for a medicinal purpose, despite that they are not healthy for some healthy people, and that it's obvious that he use them for a medicinal purpose, it's permitted.
We need to add that for someone who is dependent to opioids and is at risk to develop a withdrawal syndrome, it's permitted to take his drug.