“首相” looks like the prime minister of a royal country, while “总理” is the premier of a republic, but in China's history, there is no official post of ”首相“,but of "丞相“or "宰相", and in the case of countries like Thailand and Malaysia, both of which are kingdoms with king or sultan as the head of state, but the heads of government are named in Chinese (mainland China) as "总理",but not as "首相" like in the UK, Japan, Denmark, Sweden, Saudi Arabia, Kuwait, etc.
By the way, if you have seen pictures of prime minister of Japan, the official name in Kanji of Japanese is "内阁总理大臣", so it should be "总理" but not "首相".
It is very confusing.