Hi,
As you already know,, every Moodle course, category and sub-category are issued with a unique ID number
These ID numbers are converted into body classes which only appear in body section of the page source when that page is viewed in the users browser window.
Top level category body class is listed before the course body class
so the CSS needs to be category first course last
For Example: category > course
.category-2.course-5
However the sub level category body class is listed after the course body class
so the CSS needs to be course first sub-category last
For example: course -> sub-category
.course-3.category-3
I hope this helps?
Mary