Here's a problem I've struggled with for years that just came back.
I exported a few hundred notes from Evernote, these have about 300k of CSS and SVG. I am displaying these through a web application that can compose HTML documents at the DOM level, so I can just insert some elements into the <head> and <body>. I am inserting a toolbar into the page that also pops up modal dialogs to let the user annotate text. Right now I am keeping the native CSS but I know there's some risk of namespace collisions between my CSS and their CSS particularly because I am using Bootstrap which uses bog common words for names.
I am facing something similar to work where we have a React application that sometimes gets loaded on our partners web pages so we always have some concern we might break each others CSS.
The pages from Evernote have no JS so I'd imagine in principle I could rewrite names in the style sheet and HTML. My UI is mostly HTMX but the back end has to know what things are called on the front end and there are small amounts of Javascript.
I think of Ted Nelson's ideas of "transclusion" and how it's a bit difficult to cut and paste a piece of one HTML page into another HTML page because of these namespace issues.
What solutions are out there for this sort of problem?
Also, it may be worth looking into why you're writing so much CSS in the first place. I found that 80% of my CSS addressed layout concerns (display: flex; direction: 'column'; margin: var(--space-2);), so using specialised components allowed me to write less coupled components in fewer lines of code. You can either write them yourself, or check out something like https://www.radix-ui.com/themes/docs/overview/layout.