/* kit — reusable components
   Load AFTER tokens.css: it depends on the tokens there, including
   interpolate-size: allow-keywords for the accordion reveal.

   Conventions (details in README.md):
   - BEM-lite: .block__element--modifier; utilities prefixed .u-.
   - Components reference composite tokens only — never --*-raw-* values.
   - Component-level responsive is done with container queries; @media is
     reserved for user preferences (reduced motion) and forced-colors (spec §3).
   - Component boundaries are drawn with borders, never box-shadow alone,
     so forced-colors mode keeps every edge visible (spec §1/§8).
   - Motion animates transform/opacity only and respects reduced motion
     (spec §5). One documented exception: the accordion reveal animates
     block-size, which the ::details-content pattern requires by design.

   ============================== base ============================== */

*,
*::before,
*::after { box-sizing: border-box; }

html {
  scrollbar-gutter: stable;   /* no layout shift when a page gains a scrollbar */
}

body {
  margin: 0;
  font-family: system-ui, sans-serif;
  font-size: 1rem;            /* rem base; form controls inherit ≥1rem */
  line-height: 1.6;           /* unitless */
  color: var(--color-text);
  background: var(--color-bg);
}

h1, h2, h3, h4, h5, h6 {
  line-height: 1.25;
  text-wrap: balance;         /* headings only — never apply text-wrap via * (spec §2) */
  margin-block: var(--space-5) var(--space-3);
}
h1 {
  margin-block: var(--space-6) var(--space-4);
  font-size: clamp(1.75rem, 1.2rem + 1.5vw, 2.5rem);   /* mixed units, ratio 1.43 ≤ 2.5 */
}
h2 { font-size: clamp(1.375rem, 1.1rem + 0.8vw, 1.75rem); }
h3 { font-size: 1.125rem; }
h4, h5, h6 { font-size: 1rem; }

p {
  text-wrap: pretty;          /* running text only (spec §2) */
  margin-block: 0 var(--space-3);
}

a {
  color: var(--color-brand);
  text-underline-offset: 0.2em;
}
a:hover { color: color-mix(in oklab, var(--color-brand), black 15%); }

/* brand focus ring, everywhere; never remove outlines without an equal
   or better replacement (spec §8) */
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

img, video {
  max-inline-size: 100%;
  block-size: auto;
  /* pair with an explicit aspect-ratio at the call site to prevent CLS */
}

pre {
  overflow: auto;             /* a scroll region, not clipped content (spec §3) */
  overscroll-behavior: contain;
  padding: var(--space-3);
}

code {
  overflow-wrap: anywhere;    /* long URLs/tokens break instead of overflowing */
}

fieldset {
  margin: 0;
  padding: var(--space-3) var(--space-4) var(--space-4);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-s);
  min-inline-size: 0;         /* fieldsets default to min-content width — reset it */
}
legend {
  padding-inline: var(--space-1);
  font-weight: 600;
}

/* Safety net: kit animations that matter sit in their own
   prefers-reduced-motion guard; this catches everything else (spec §5/§8). */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
}

/* ---- page-level helpers (plain CSS; no @media needed for these) ---- */

.container {
  inline-size: 100%;
  max-inline-size: var(--content-width);
  margin-inline: auto;
  padding-inline: var(--space-4);
}

.prose { max-inline-size: 80ch; }   /* measure for running text (spec §2) */

.u-visually-hidden {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link {
  position: fixed;
  inset-block-start: var(--space-2);
  inset-inline-start: var(--space-2);
  z-index: 10;
  padding: var(--space-2) var(--space-3);
  font-weight: 600;
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);
  border-radius: var(--radius-s);
  text-decoration: none;
  transform: translateY(-300%);   /* parked off-canvas, transform-only motion */
}
.skip-link:focus-visible { transform: none; }

/* ============================== .btn ============================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  min-block-size: 2.5rem;     /* ≥40px touch target (spec §6) */
  padding-block: var(--space-2);
  padding-inline: var(--space-4);
  font: inherit;
  font-weight: 600;
  line-height: 1.2;
  text-decoration: none;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: var(--radius-s);
  transition: transform 120ms ease;   /* transform/opacity only (spec §5) */
}

.btn:active { transform: translateY(1px); }

/* hover shades are computed at the element from the composite token, so
   scoped re-theming (see starter.html) keeps hovers coherent */
.btn--primary {
  background: var(--color-brand);
  border-color: var(--color-brand);
  color: var(--color-on-brand);
}
.btn--primary:hover {
  background: color-mix(in oklab, var(--color-brand), black 15%);
  border-color: color-mix(in oklab, var(--color-brand), black 15%);
}

.btn--ghost {
  background: transparent;
  border-color: var(--color-border-strong);
  color: var(--color-text);
}
.btn--ghost:hover {
  background: color-mix(in oklab, var(--color-brand) 12%, transparent);
  border-color: var(--color-brand);
  color: var(--color-brand);
}

/* ============================== .badge ============================== */

.badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding-block: 0.2em;
  padding-inline: 0.7em;
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.4;
  border: 1px solid transparent;
  border-radius: 999px;
  background: var(--color-brand-soft);
  color: var(--color-brand);
}

.badge--neutral {
  background: var(--color-raised);
  color: var(--color-muted);
  border-color: var(--color-border);
}

/* ============================== .card ============================== */

.card {
  container-type: inline-size;   /* cards adapt to their own box, not the viewport */
  display: grid;
  gap: var(--space-3);
  grid-template-rows: auto 1fr auto;   /* explicit-rows fallback: body stretches,
                                          actions pin to the card bottom */
  padding: var(--space-5);
  background: var(--color-surface);
  border: 1px solid var(--color-border);   /* boundary = border, never box-shadow alone */
  border-radius: var(--radius-m);
  min-inline-size: 0;
}

.card__title {
  margin: 0;
  font-size: 1.25rem;         /* static fallback */
  line-height: 1.3;
  text-wrap: balance;
}
@supports (font-size: 1cqi) {
  .card__title {
    font-size: clamp(1.125rem, 5cqi, 1.625rem);   /* fluid against the CARD's width */
  }
}

.card__body {
  margin: 0;
  color: var(--color-muted);
}

.card__actions {
  display: flex;
  flex-wrap: wrap;            /* MUST: wrap instead of overflowing (spec §3) */
  align-items: center;
  align-items: safe center;   /* safe alignment never clips taller items */
  gap: var(--space-2);
  margin: 0;
}

/* container query: the card reacts to the space it is given — roomier
   padding rhythm once the card itself is wide, whatever the viewport is */
@container (min-width: 24rem) {
  .card__actions { margin-block-start: var(--space-2); }
}

.card-grid {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));   /* unknown item count (spec §3) */
}

@supports (grid-template-rows: subgrid) {
  .card-grid > .card {
    grid-row: span 3;
    grid-template-rows: subgrid;   /* title/body/actions align across cards,
                                      single axis only (spec §3) */
  }
}

/* ============================== .navbar ============================== */

.navbar {
  display: flex;
  flex-wrap: wrap;            /* MUST: wrap instead of overflowing (spec §3) */
  align-items: center;
  align-items: safe center;
  gap: var(--space-3);
  padding-block: var(--space-2);
  border-block-end: 1px solid var(--color-border);
}

.navbar__brand {
  display: inline-flex;
  align-items: center;
  min-block-size: 2.5rem;
  font-weight: 700;
  font-size: 1.125rem;
  color: var(--color-text);
  text-decoration: none;
}

.navbar__links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-items: safe center;
  gap: var(--space-1);
  margin: 0;
  margin-inline-start: auto;  /* push the link group to the row end — no order/*-reverse tricks */
  padding: 0;
  list-style: none;
}

.navbar__link {
  display: inline-flex;
  align-items: center;
  min-block-size: 2.5rem;
  padding-inline: var(--space-2);
  border-radius: var(--radius-s);
  color: var(--color-muted);
  font-weight: 500;
  text-decoration: none;
}

.navbar__link:hover {
  color: var(--color-text);
  background: var(--color-raised);
}

/* current page: semantic attribute + a non-color cue (underline) (spec §4/§8) */
.navbar__link[aria-current="page"] {
  color: var(--color-brand);
  font-weight: 600;
  text-decoration: underline;
  text-decoration-color: var(--color-brand);
  text-underline-offset: 0.3em;
}

/* ============================== .field ============================== */

.field {
  display: flex;
  flex-direction: column;     /* single-column forms (spec §6) */
  gap: var(--space-1);
  min-inline-size: 0;
}

.field__label { font-weight: 600; }   /* visible label — placeholder is never the label */

.field__hint {
  margin: 0;
  font-size: 0.875rem;
  color: var(--color-muted);
}

/* Error message lives ABOVE the input in the DOM (spec §6) and pairs a pure-CSS
   icon with text, so color is never the only indicator (spec §8).
   Hidden until the field turns invalid. */
.field__error {
  display: none;
  align-items: center;
  gap: var(--space-2);
  margin: 0;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-danger);
}
.field__error::before {
  content: "";
  flex: none;
  inline-size: 0;
  block-size: 0;
  border-inline: 0.45em solid transparent;
  border-block-end: 0.7em solid currentColor;   /* CSS warning triangle */
}

.field__input {
  inline-size: 100%;
  min-inline-size: 0;
  min-block-size: 2.5rem;     /* ≥40px touch target (spec §6) */
  padding-block: var(--space-2);
  padding-inline: var(--space-3);
  font: inherit;              /* ≥1rem — no zoom-on-focus on mobile */
  color: var(--color-text);
  background: var(--color-surface);
  border: 1px solid var(--color-border-strong);   /* ≥3:1 component boundary (spec §8) */
  border-radius: var(--radius-s);
  accent-color: var(--color-brand);   /* branded controls; :root already sets it globally */
}

/* doubles as the accent-color fallback: focus is always self-drawn (spec §6) */
.field__input:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

textarea.field__input {
  field-sizing: content;      /* grows with typing where supported; keep rows="" as fallback */
  min-block-size: 6rem;
}

/* Invalid states. :user-invalid only matches AFTER a real interaction
   (blur / submit attempt) — the spec's validation timing, for free.
   [aria-invalid="true"] gives scripted flows the same styling. */
.field__input:user-invalid,
.field__input[aria-invalid="true"] {
  border-color: var(--color-danger);
  background: color-mix(in oklab, var(--color-danger) 7%, var(--color-surface));
}

/* parent tinting + reveal of the message placed above the input (spec §6) */
.field:has(:user-invalid) .field__error,
.field:has(.field__input[aria-invalid="true"]) .field__error {
  display: flex;
}
.field:has(:user-invalid) .field__label,
.field:has(.field__input[aria-invalid="true"]) .field__label {
  color: var(--color-danger);
}

/* checkbox / radio row — accent-color does the branding, size does the a11y */
.choice {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-block-size: 2.5rem;     /* ≥40px touch target */
  cursor: pointer;
}
.choice input {
  inline-size: 1rem;
  block-size: 1rem;
  margin: 0;
  accent-color: var(--color-brand);
}

/* ============================== .accordion ============================== */
/* <details class="accordion"> + <summary> + <div class="accordion__body">.
   Relies on interpolate-size: allow-keywords from tokens.css. */

.accordion {
  min-inline-size: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-s);
}

.accordion + .accordion { margin-block-start: var(--space-2); }

.accordion > summary {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  min-block-size: 2.5rem;
  padding: var(--space-3) var(--space-4);
  font-weight: 600;
  cursor: pointer;
  list-style: none;           /* custom marker below */
}
.accordion > summary::-webkit-details-marker { display: none; }

.accordion > summary::after {
  content: "";
  flex: none;
  inline-size: 0.5em;
  block-size: 0.5em;
  margin-inline-start: auto;  /* chevron pinned to the row end */
  border-inline-end: 2px solid currentColor;
  border-block-end: 2px solid currentColor;
  transform: rotate(45deg);   /* closed: points down */
}
.accordion[open] > summary::after { transform: rotate(-135deg); }   /* open: points up */

@media (prefers-reduced-motion: no-preference) {
  .accordion > summary::after { transition: transform 200ms ease; }
}

/* Expand/collapse reveal — double-guarded:
   1. @media (prefers-reduced-motion: no-preference)  (spec §5/§8)
   2. @supports for ::details-content + interpolate-size (else: instant open,
      still fully correct).
   block-size is the one sanctioned exception to "transform/opacity only":
   an auto-height reveal has no transform equivalent, and the pattern is
   spec-endorsed for <details> (spec §4 accordion row). */
@media (prefers-reduced-motion: no-preference) {
  @supports (interpolate-size: allow-keywords) and (selector(::details-content)) {
    .accordion::details-content {
      block-size: 0;
      opacity: 0;
      overflow: clip;          /* pure clipping — never a hidden scroll container (spec §3) */
      transition:
        block-size 250ms ease,
        opacity 200ms ease,
        content-visibility 250ms;
      transition-behavior: allow-discrete;   /* longhand only — never in the shorthand (spec §5) */
    }
    .accordion[open]::details-content {
      block-size: auto;        /* interpolates to natural height via interpolate-size */
      opacity: 1;
    }
  }
}

.accordion__body {
  padding: 0 var(--space-4) var(--space-4);
  color: var(--color-muted);
}
.accordion__body > :last-child { margin-block-end: 0; }

/* ============================== .tool-shell ============================== */
/* Two-pane shell for tool pages: control panel | live preview. The section
   carries container-type, so the SAME shell collapses in narrow contexts
   (dialogs, sidebars) without any @media — the component reacts to the space
   it is actually given (spec §3: container queries for components). */

.tool-shell { container-type: inline-size; }

.tool-shell__layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);   /* stacked = the narrow default */
  gap: var(--space-5);
  align-items: start;
}

@container (min-width: 44rem) {
  .tool-shell__layout {
    grid-template-columns: minmax(16rem, 22rem) minmax(0, 1fr);   /* panel | preview */
  }
}

.tool-shell__panel,
.tool-shell__preview {
  min-inline-size: 0;          /* long content can never blow out the track (spec §3) */
  padding: var(--space-5);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-m);
}

.tool-shell__panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.tool-shell__title { margin: 0; }

.tool-shell__preview {
  overflow: auto;              /* the pane is a scroll region */
  overscroll-behavior: contain;   /* nested scrolling never chains to the page (spec §3) */
  scrollbar-gutter: stable;    /* no shift when output grows a scrollbar */
  overflow-wrap: anywhere;     /* long URLs/IDs break instead of overflowing */
  background: var(--color-raised);
}

/* ===================== forced-colors fallback (spec §1/§8 MUST) ===================== */
/* The kit draws boundaries with borders, which forced-colors mode maps to
   system colors automatically. What needs explicit help: filled/soft surfaces
   that would lose their edge, and the focus ring color. */
@media (forced-colors: active) {
  .btn,
  .badge {
    border: 1px solid ButtonBorder;
  }
  .btn--primary {
    border-color: ButtonText;   /* keep the filled CTA distinguishable from ghost */
  }
  :focus-visible {
    outline-color: Highlight;
  }
}
