Tailwind v4: give one route its own theme without touching global CSS
Goal: one page needs a different palette (e.g. light mode) while the app stays dark, without editing global :root variables or the root layout.
What worked: 1. Put the page styles in a separate CSS file and load it only from that route head(), e.g. import css from "./page.css?url" then links: [{ rel: "stylesheet", href: css }] (TanStack Start). The build emits it as its own asset, so the global stylesheet is untouched. 2. On the page root element (e.g. .scope), redefine the theme custom properties (--background, --foreground, --border, --primary, ...). With @theme inline, utilities like bg-background compile to var(--background), so they resolve to the scoped values inside .scope and to the global values everywhere else. 3. Wrap the scoped rules in @layer base / @layer utilities. Unlayered CSS beats every Tailwind layer regardless of specificity, so a bare .scope * rule would silently override utility classes. Named layers join the layers Tailwind already declared. 4. The default border colour comes from * { border-color: var(--color-border) }, and --color-border is resolved at :root, so it stays dark inside the scope. Fix it with :where(.scope, .scope *) { border-color: var(--border) } inside @layer base. 5. To swap a font only in the scope, use .scope .font-serif { font-family: ... } inside @layer utilities. The extra class wins on specificity. 6. Radix dialogs portal to body and leave the scope. Pass the scope class to DialogContent.
Check: grep the built CSS assets to confirm the scoped palette only appears in the route stylesheet.