/* ============================================================================
   base.css — reset, defaults and layout primitives. Load after tokens.css.
   Everything here reads from tokens; there are no raw values below.
   ========================================================================= */

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

* { margin: 0; }

html {
  -webkit-text-size-adjust: 100%;   /* stop iOS Safari inflating text in landscape */
  scroll-behavior: smooth;
  scrollbar-gutter: stable;         /* no layout jump when a scrollbar appears */
  tab-size: 2;
}

body {
  min-height: 100svh;               /* svh, not vh: vh is wrong under mobile browser chrome */
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-text);
  font-size: var(--t-base);
  line-height: var(--lh-base);
  letter-spacing: var(--track-normal);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-synthesis-weight: none;      /* never let the browser fake a weight */
}

/* Media defaults. `max-width:100%` on replaced elements prevents the single
   most common cause of horizontal overflow. */
img, picture, video, canvas, svg, iframe {
  display: block;
  max-width: 100%;
  height: auto;
}
img { background-color: var(--n-100); }   /* so a slow/broken image isn't a white hole */

/* Long strings — URLs, IDs, part numbers — are the second most common cause. */
p, li, h1, h2, h3, h4, h5, h6, dd, dt, td, th, figcaption {
  overflow-wrap: break-word;
  hyphens: auto;
}

input, button, textarea, select { font: inherit; color: inherit; }

/* ---- Headings ------------------------------------------------------------
   Display sizes get negative tracking and tight leading. Large type set at
   body-text tracking is one of the clearest "nobody adjusted this" signals. */

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-display);
  color: var(--ink-strong);
  line-height: var(--lh-snug);
  text-wrap: balance;               /* no orphaned single word on the last line */
  font-weight: 600;
}
h1 { font-size: var(--t-3xl); line-height: var(--lh-tight); letter-spacing: var(--track-display); }
h2 { font-size: var(--t-2xl); line-height: var(--lh-tight); letter-spacing: var(--track-display); }
h3 { font-size: var(--t-xl);  letter-spacing: var(--track-tight); }
h4 { font-size: var(--t-lg);  letter-spacing: var(--track-tight); }
h5 { font-size: var(--t-md); }
h6 { font-size: var(--t-base); text-transform: uppercase; letter-spacing: var(--track-wide); color: var(--ink-muted); }

p { text-wrap: pretty; }            /* avoids orphans/short last lines in prose */

/* Prose gets a measure. Without this, text runs to container width and becomes
   physically hard to read — the eye loses the line return past ~75 characters. */
.prose { max-width: var(--measure); }
.prose > * + * { margin-block-start: var(--s-md); }
.prose > * + :is(h2, h3) { margin-block-start: var(--s-2xl); }

a { color: var(--ink-brand); text-underline-offset: 0.18em; text-decoration-thickness: 0.07em; }
a:hover { text-decoration-thickness: 0.12em; }

hr { border: 0; border-block-start: 1px solid var(--line); margin-block: var(--s-2xl); }

code, kbd, samp, pre { font-family: var(--font-mono); font-size: 0.9em; }
code { background: var(--surface-sunken); padding: 0.15em 0.35em; border-radius: var(--r-xs); }
pre  { overflow-x: auto; padding: var(--s-md); background: var(--surface-sunken);
       border: 1px solid var(--line); border-radius: var(--r-md); }
pre code { background: none; padding: 0; }

::selection { background: var(--brand-200); color: var(--ink-strong); }

/* ---- Focus ---------------------------------------------------------------
   :focus-visible, not :focus — so a mouse click doesn't leave a ring, but a
   keyboard user always sees one. The offset keeps the ring legible against
   the element's own background. WCAG 2.2 (2.4.13) requires the indicator to
   be both large enough and contrasting; 2px at 2px offset satisfies it. */

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: var(--r-xs);
}
:focus:not(:focus-visible) { outline: none; }

/* ---- Skip link — first focusable element in the document ----------------- */
.skip-link {
  position: absolute; inset-inline-start: var(--s-md); inset-block-start: var(--s-md);
  z-index: 999; padding: var(--s-xs) var(--s-md);
  background: var(--surface-raised); color: var(--ink-strong);
  border-radius: var(--r-md); box-shadow: var(--e3);
  transform: translateY(-200%);
  transition: transform var(--d-fast) var(--ease-out);
}
.skip-link:focus { transform: translateY(0); }

/* ---- Screen-reader-only (still focusable) -------------------------------- */
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0;
  margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}

/* ============================================================================
   LAYOUT PRIMITIVES
   Compose pages from these instead of writing bespoke flex/grid every time.
   ========================================================================= */

/* Stack — vertical rhythm via the owl selector, so the first and last child
   never carry stray margin. */
.stack { display: flex; flex-direction: column; }
.stack > * + * { margin-block-start: var(--space, var(--s-md)); }

/* Cluster — things that sit in a row and wrap gracefully. Nav, tags, buttons. */
.cluster {
  display: flex; flex-wrap: wrap;
  gap: var(--space, var(--s-sm));
  align-items: var(--align, center);
}

/* Center — the page container. min() guards against overflow on narrow
   viewports; a bare max-width plus padding can still exceed the viewport. */
.center {
  box-sizing: content-box;
  max-width: var(--container);
  margin-inline: auto;
  padding-inline: var(--gutter);
}
.center--narrow { max-width: var(--container-narrow); }

/* Switcher — a row that becomes a column below a threshold, with NO media
   query. Each child claims at least --threshold or the row breaks. */
.switcher { display: flex; flex-wrap: wrap; gap: var(--space, var(--s-lg)); }
.switcher > * { flex-grow: 1; flex-basis: calc((var(--threshold, 30rem) - 100%) * 999); }

/* Grid — responsive columns without breakpoints. The inner min() is required:
   without it, minmax() forces the track to --min even when the viewport is
   narrower, producing horizontal overflow on phones. */
.grid {
  display: grid;
  gap: var(--space, var(--s-lg));
  grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--min, 18rem)), 1fr));
}

/* Sidebar — fixed-ish aside plus fluid main, collapsing when main gets tight. */
.with-sidebar { display: flex; flex-wrap: wrap; gap: var(--space, var(--s-xl)); }
.with-sidebar > :first-child { flex-basis: var(--side, 18rem); flex-grow: 1; }
.with-sidebar > :last-child  { flex-basis: 0; flex-grow: 999; min-inline-size: var(--content-min, 50%); }

/* Frame — enforce an aspect ratio on media. Kills the CLS from unsized images. */
.frame { aspect-ratio: var(--ratio, 16 / 9); overflow: hidden; }
.frame > * { inline-size: 100%; block-size: 100%; object-fit: cover; }

/* Reel — horizontal scroller that doesn't break the page width. */
.reel {
  display: flex; gap: var(--space, var(--s-md));
  overflow-x: auto; overscroll-behavior-inline: contain;
  scroll-snap-type: inline mandatory;
}
.reel > * { flex: 0 0 var(--item, 18rem); scroll-snap-align: start; }

/* Section rhythm — vary it. Every section sharing identical padding is one of
   the clearest template signals; use --s-3xl/--s-4xl deliberately. */
.section { padding-block: var(--s-4xl); }
.section--tight { padding-block: var(--s-2xl); }
.section--sunken { background: var(--surface-sunken); }

/* ============================================================================
   COMPONENTS — minimum viable, with all states present.
   ========================================================================= */

.card {
  background: var(--surface-raised);
  border-radius: var(--r-lg);
  box-shadow: var(--e2);
  padding: var(--s-lg);
  transition: box-shadow var(--d-base) var(--ease-out),
              transform var(--d-base) var(--ease-out);
}
.card--interactive:hover { box-shadow: var(--e3); transform: translateY(-2px); }

.btn {
  --btn-bg: var(--brand-600);
  --btn-ink: var(--n-0);
  display: inline-flex; align-items: center; justify-content: center;
  gap: var(--s-xs);
  min-block-size: 2.75rem;          /* comfortably past the 24px WCAG 2.2 floor */
  padding-inline: var(--s-lg);
  border: 0; border-radius: var(--r-md);
  background: var(--btn-bg); color: var(--btn-ink);
  font-weight: 560; letter-spacing: var(--track-tight);
  box-shadow: var(--e1), var(--e-bevel);
  cursor: pointer;
  transition: background var(--d-instant) var(--ease-out),
              box-shadow var(--d-fast) var(--ease-out),
              transform var(--d-instant) var(--ease-out);
}
.btn:hover   { --btn-bg: var(--brand-500); box-shadow: var(--e2), var(--e-bevel); }
.btn:active  { transform: translateY(1px); box-shadow: var(--e-inset); }
.btn[disabled], .btn[aria-disabled="true"] {
  --btn-bg: var(--n-300); --btn-ink: var(--n-600);
  cursor: not-allowed; box-shadow: none; transform: none;
}
.btn[data-loading] { cursor: progress; }

.btn--secondary {
  --btn-bg: transparent; --btn-ink: var(--ink-strong);
  box-shadow: inset 0 0 0 1px var(--line-strong);
}
.btn--secondary:hover { --btn-bg: var(--surface-sunken); }

.field { display: flex; flex-direction: column; gap: var(--s-2xs); }
.field label { font-size: var(--t-sm); font-weight: 550; color: var(--ink); }
.field input, .field textarea, .field select {
  min-block-size: 2.75rem;
  padding: var(--s-xs) var(--s-sm);
  background: var(--field);
  color: var(--ink);
  border: 1px solid var(--field-line);
  border-radius: var(--r-md);
  box-shadow: var(--e-inset);
  transition: border-color var(--d-instant), box-shadow var(--d-instant);
}
.field input:hover { border-color: var(--line-strong); }
.field [aria-invalid="true"] { border-color: var(--err-500); }
.field .hint  { font-size: var(--t-sm); color: var(--ink-muted); }
.field .error { font-size: var(--t-sm); color: var(--err-500); }

/* Empty and loading states — their absence is what makes an app feel unfinished. */
.empty-state {
  display: grid; place-items: center; gap: var(--s-sm);
  padding: var(--s-3xl) var(--s-lg);
  text-align: center; color: var(--ink-muted);
  border: 1px dashed var(--line-strong); border-radius: var(--r-lg);
}

.skeleton {
  background: linear-gradient(90deg, var(--n-100) 25%, var(--n-200) 37%, var(--n-100) 63%);
  background-size: 400% 100%;
  animation: skeleton-sweep 1.4s ease-in-out infinite;
  border-radius: var(--r-sm);
}
@keyframes skeleton-sweep { from { background-position: 100% 50%; } to { background-position: 0 50%; } }

/* Staggered reveal. ~50ms between items reads as intentional sequencing;
   0ms reads as a dump and >120ms reads as sluggish. */
.reveal { animation: reveal var(--d-slow) var(--ease-out) backwards; }
.reveal:nth-child(1) { animation-delay: 0ms; }
.reveal:nth-child(2) { animation-delay: 50ms; }
.reveal:nth-child(3) { animation-delay: 100ms; }
.reveal:nth-child(4) { animation-delay: 150ms; }
.reveal:nth-child(5) { animation-delay: 200ms; }
@keyframes reveal { from { opacity: 0; transform: translateY(12px); } }

/* ---- Reduced motion ------------------------------------------------------
   Not a blanket kill. The vestibular triggers are large-area movement, parallax
   and zoom; a crossfade is fine and keeps the interface legible. So: remove
   TRANSFORMS, shorten durations, and leave opacity alone. */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .reveal { animation: none; }
  .card--interactive:hover { transform: none; }
  .skeleton { animation: none; background: var(--n-100); }
}

/* ---- Forced colors (Windows High Contrast) -------------------------------
   Shadows and background-image vanish in forced-colors mode, so anything whose
   boundary is drawn ONLY by a shadow becomes invisible. Restore with borders. */
@media (forced-colors: active) {
  .card, .btn, .field input { border: 1px solid; }
  .btn { forced-color-adjust: none; }
  :focus-visible { outline: 2px solid CanvasText; }
}

/* ---- Print ---------------------------------------------------------------- */
@media print {
  body { background: #fff; color: #000; }
  .skip-link, nav, .no-print { display: none; }
  a[href^="http"]::after { content: " (" attr(href) ")"; font-size: 0.85em; }
  .card { box-shadow: none; border: 1px solid #999; break-inside: avoid; }
}
