<!-- skill: CSS Design | version: 1.0.0 | description: Modern CSS layout, responsive design, variables, motion and BEM method | category: Web -->

## Instructions
- Organize CSS in layers: reset → tokens (`:root` variables) → base → layout → components → utilities; use `@layer` to control cascade explicitly.
- Master specificity order: elements < classes < attributes/pseudo-classes < IDs (avoid) < `!important` (never); keep selectors flat, max 2-3 levels deep.
- Default to Flexbox for 1-D rows/columns, Grid for 2-D pages/cards; never float-layout or absolute-position whole pages.
- Build mobile-first with `min-width` breakpoints (e.g. 640/768/1024px); add container queries (`@container`) for truly reusable components.
- Define design tokens as CSS custom properties: colors, spacing scale, radii, shadows, font stacks; dark mode via `[data-theme]` overrides.
- Use BEM naming (`.card`, `.card__title`, `.card--featured`) for vanilla projects; keep utility-class sprawl out unless Tailwind is adopted project-wide.
- Animations: prefer `transform` + `opacity` only (GPU-safe); use `transition` for state changes, `@keyframes` for loops; always honor `prefers-reduced-motion`.
- Responsive media: `max-width:100%` on all media, `aspect-ratio` for frames, `clamp()` for fluid type/spacing instead of many breakpoints.
- Avoid magic numbers: derive spacing from scale (`--space-1..8`), z-index scale, and line-height ratios; document any exception inline.
- Tailwind-vs-vanilla rule: solo landing/small site → vanilla + BEM; fast-iterating app/design-system team → Tailwind; never mix both on the same component.
- Debug visually: outline grid (`* { outline: 1px solid red }` temporarily), check computed specificity, then remove debug code.
- Ship lean CSS: purge unused rules, minify, single critical `<style>` for above-fold + deferred stylesheet for the rest.

## Examples
- Pricing grid: 3-column Grid (`repeat(auto-fit, minmax(240px, 1fr))`) collapsing to 1 column, featured card with `--modifier` highlight and fluid `clamp()` pricing type.
- Sticky navbar + hero: Flexbox nav with wrap-safe links, hero using Grid centering, gradient tokens, reduced-motion-safe fade-in.
- Tailwind conversion: legacy BEM card (`.card/.card__body`) rewritten as composable utilities, extracting repeated patterns into `@component` classes.

## Workflows
1. Define tokens first: palette, type scale with `clamp()`, spacing scale, breakpoints — get sign-off before components.
2. Lay out page skeleton (header/main/sidebar/footer) with Grid areas, then Flexbox inside each region.
3. Build components mobile-first: base style → `min-width` enhancements → container-query variants → dark-mode overrides.
4. Add motion last: transitions on hover/focus, keyframe entrances guarded by `prefers-reduced-motion: no-preference`.
5. Audit: check contrast, 320px/768px/1440px widths, print stylesheet sanity, then minify and measure CLS/LCP impact.

## Templates
```css
/* tokens + fluid type + grid */
:root {
  --bg: #0f172a; --surface: #1e293b; --text: #f1f5f9;
  --accent: #38bdf8; --space-4: 1rem; --space-8: 2rem;
  --radius: 12px; --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
img { max-width: 100%; display: block; }
body { margin: 0; font-family: var(--font); background: var(--bg); color: var(--text); }
h1 { font-size: clamp(2rem, 5vw, 3.5rem); line-height: 1.1; }
.grid { display: grid; gap: var(--space-4); grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.card { background: var(--surface); border-radius: var(--radius); padding: var(--space-4); }
.card--featured { outline: 2px solid var(--accent); }
/* container query */
.cards { container-type: inline-size; }
@container (min-width: 600px) { .card { display: grid; grid-template-columns: 1fr 1fr; } }
/* safe motion */
@media (prefers-reduced-motion: no-preference) {
  .fade-in { animation: rise .5s ease both; }
  @keyframes rise { from { opacity: 0; transform: translateY(12px); } }
}
```
```html
<!-- BEM card -->
<article class="card card--featured">
  <h2 class="card__title">Pro plan</h2>
  <p class="card__price">$19/mo</p>
  <button class="card__btn" type="button">Choose Pro</button>
</article>
```

## Tools
- SARAH `file_write`: keep `styles.css` (tokens/layout) + `components.css` split; link with `<link rel="stylesheet" href="styles.css">`.
- SARAH `shell_run`: `npx stylelint "**/*.css"` to lint; `npx lightningcss --minify styles.css -o styles.min.css` to minify.
- SARAH preview: screenshot 390px and 1280px widths; verify no horizontal scroll and focus rings visible.

## Advanced
- Specificity traps: `#id` beats ten classes — refactor to classes; `:where()` adds zero specificity for resets, `:is()` takes strongest argument.
- Grid pro moves: `grid-template-areas` for page shells, `subgrid` for aligned card rows, `minmax(0,1fr)` to stop blowout from long content.
- Container queries: set `container-type: inline-size` on wrapper; style components by their own width, not viewport — ideal for sidebars/cards.
- Troubleshooting: collapsing margins (add padding/flow-root), flex overflow (add `min-width:0`), z-index wars (single scale + `isolation:isolate`).
- Pro tip: `clamp(1rem, 2vw + 0.5rem, 1.5rem)` fluid spacing beats 5 breakpoints; test dark mode by flipping `[data-theme]` on `<html>`.

## Documentation
- MDN CSS: https://developer.mozilla.org/en-US/docs/Web/CSS
- web.dev Learn CSS: https://web.dev/learn/css
- Tailwind docs: https://tailwindcss.com/docs
