# Cortex
> Cortex is a theme engine. From a `theme-config.json` with three brand colours (primary, secondary, neutral) it generates the design tokens a product needs: OKLCH colour ramps, text and border colours measured for contrast, type, spacing, shape, elevation and motion, as CSS custom properties with light and dark values. It runs at build time and works with any framework, or none.
- Package: `@flexpixels/cortex` (Node, Bun, or the browser through `@flexpixels/cortex/browser`). MCP server for AI agents: `@flexpixels/cortex-mcp`.
- `generateThemeCSS(config)` returns the lean stylesheet (599 tokens for this site's theme, the repository's `theme-config.json`); `generateFullThemeCSS(config)` returns every token the engine computes (1,034 for the same theme).
- Every token name starts with `--fp-` (`settings.prefix` changes it). Light and dark values are `light-dark()` pairs; `data-theme="light"` or `"dark"` on `` pins one.
---
# Install Cortex
> Install @flexpixels/cortex, pick the right entry point, and see what the package gives you.
Source: https://cortex.flexpixels.com/docs/quick-start/install/
Install the package with your package manager:
```bash
npm install @flexpixels/cortex@beta
# or
pnpm add @flexpixels/cortex@beta
yarn add @flexpixels/cortex@beta
bun add @flexpixels/cortex@beta
```
Cortex runs on Node.js 20 or later, on Bun, and in the browser through a bundler. It needs no framework and touches no DOM. The package is ES modules only: `import` it. From CommonJS use `await import("@flexpixels/cortex")`; `require()` of an ES module works only from Node.js 20.19 and 22.12 on.
Runtime dependencies: `culori`, `apca-w3` and `zod`.
The package also lists `@types/culori`, so its type declarations compile under strict TypeScript; it holds types only and adds no code.
## In the browser
The usual choice is to generate the stylesheet at build time and ship only the CSS: the page then loads no JavaScript from Cortex. Run the engine in the browser only when the theme changes at runtime, such as a theme editor.
The browser build imports `zod` and `culori` by bare name, so a plain `
```
The service reads `localStorage`, which only exists in the browser. With server-side rendering, read it only when `isPlatformBrowser(inject(PLATFORM_ID))` is true.
## Next
- [Token usage](https://cortex.flexpixels.com/docs/theme/best-practices-tokens/): which token to reach for.
- [Build & Import](https://cortex.flexpixels.com/docs/theme/build-and-import/): the lean and full stylesheets, and other build tools.
- [Custom Themes](https://cortex.flexpixels.com/docs/theme/custom-themes/): shape, type, spacing and elevation from the same config.
---
# Hello world
> Generate your first cortex theme from a minimal config.
Source: https://cortex.flexpixels.com/docs/quick-start/hello-world/
A minimal `theme-config.json`:
```json
{
"name": "MyApp",
"seeds": {
"primary": "#3b82f6",
"secondary": "#22d3ee",
"neutral": "#475569"
}
}
```
Every field has a default, so `mergeThemeConfig({})` and a file holding `{}` give a theme; in practice you set at least the three brand seeds, and the JSON Schema an editor checks the file against asks for them. `success`, `danger`, `warning` and `info` are generated at fixed hues (145°, 25°, 85°, 250°) when you leave them out. `name` appears in the stylesheet's header comment and as the title of the DESIGN.md export.
## Generate the CSS — Node or Bun
`loadUserConfig` reads the file, merges it over the defaults and validates it. `generateThemeCSS` turns the result into a stylesheet:
```ts
// build-theme.ts
import { mkdirSync, writeFileSync } from "node:fs";
import { generateThemeCSS, loadUserConfig } from "@flexpixels/cortex";
const config = loadUserConfig("theme-config.json");
mkdirSync("public", { recursive: true });
writeFileSync("public/theme.css", generateThemeCSS(config));
```
Run it with `bun build-theme.ts`, or save it as `build-theme.mjs` and run `node build-theme.mjs`. Re-run it whenever `theme-config.json` changes.
`generateThemeCSS` writes the lean stylesheet (the tokens in use). For every token the engine computes, including the APCA contrast matrix, call `generateFullThemeCSS(config)` instead, or set `"output": { "tokens": "full" }` in the config.
If the file is missing, `loadUserConfig` returns the default theme without a message. If it exists but is invalid, it prints a `CONFIG_LOAD_FAILED` warning naming the problem and also returns the defaults. Use `loadConfigFromPath(absolutePath)` when you would rather it throw.
If you already have a partial config in memory (a request body, editor state), skip the file and use `mergeThemeConfig`, which runs the same merge and validation:
```ts
import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex";
const config = mergeThemeConfig({
seeds: { primary: "#3b82f6", secondary: "#22d3ee", neutral: "#475569" },
});
const css = generateThemeCSS(config);
```
## Generate the CSS — browser
The same functions work in the browser, through a bundler. Put an empty `
```
Then write the result into it to switch themes live:
```ts
import { generateThemeCSS, mergeThemeConfig } from "@flexpixels/cortex/browser";
const css = generateThemeCSS(mergeThemeConfig(userPartial));
document.getElementById("fp-theme")!.textContent = css;
```
The engine adds about 290 KB of minified JavaScript to the page, so a theme that does not change at runtime is better generated at build time (see [Install](https://cortex.flexpixels.com/docs/quick-start/install/)). With a Content Security Policy, writing CSS into a `