/* Atticlet base layer. Requires dist/tokens.css, which declares the layer order.
 *
 * Split across two layers on purpose. Element rules sit BELOW a component library
 * so it can override them — an unlayered `a { color }` beats every MUI style and
 * silently repaints anchors, which is how a contained button once shipped with a
 * blue label at 2.2:1. The focus ring and reduced-motion sit ABOVE it, because
 * MUI's ButtonBase and InputBase reset `outline: 0` and would otherwise erase the
 * ring with no error and no visible symptom until someone tabs through the app.
 */

@layer atticlet-reset {
  *,
  *::before,
  *::after { box-sizing: border-box; }

  html { scroll-behavior: smooth; }

  body {
    min-width: 320px;
    margin: 0;
    background: var(--at-bg);
    color: var(--at-text);
    font-family: var(--at-font-sans);
    font-size: var(--at-text-body-size);
    line-height: var(--at-text-body-line);
    font-weight: var(--at-text-body-weight);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  a { color: var(--at-link); text-underline-offset: 0.18em; }
  a:hover { color: var(--at-link-hover); }

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

  img, svg, video { max-width: 100%; }

  /* Type scale ----------------------------------------------------------- */

  .at-display-lg, .at-display, .at-h1, .at-h2, .at-h3 { margin: 0; color: var(--at-text); }

  .at-display-lg { font-size: var(--at-text-display-lg-size); line-height: var(--at-text-display-lg-line);
                   font-weight: var(--at-text-display-lg-weight); letter-spacing: var(--at-text-display-lg-tracking); }
  .at-display { font-size: var(--at-text-display-size); line-height: var(--at-text-display-line);
                font-weight: var(--at-text-display-weight); letter-spacing: var(--at-text-display-tracking); }
  .at-h1      { font-size: var(--at-text-h1-size); line-height: var(--at-text-h1-line);
                font-weight: var(--at-text-h1-weight); letter-spacing: var(--at-text-h1-tracking); }
  .at-h2      { font-size: var(--at-text-h2-size); line-height: var(--at-text-h2-line);
                font-weight: var(--at-text-h2-weight); letter-spacing: var(--at-text-h2-tracking); }
  .at-h3      { font-size: var(--at-text-h3-size); line-height: var(--at-text-h3-line);
                font-weight: var(--at-text-h3-weight); letter-spacing: var(--at-text-h3-tracking); }
  .at-body-lg { font-size: var(--at-text-body-lg-size); line-height: var(--at-text-body-lg-line); }
  .at-body    { font-size: var(--at-text-body-size); line-height: var(--at-text-body-line); }
  .at-body-sm { font-size: var(--at-text-body-sm-size); line-height: var(--at-text-body-sm-line);
                color: var(--at-text-secondary); }
  .at-caption { font-size: var(--at-text-caption-size); line-height: var(--at-text-caption-line);
                color: var(--at-text-secondary); }
  .at-label   { font-size: var(--at-text-label-size); line-height: var(--at-text-label-line);
                font-weight: var(--at-text-label-weight); letter-spacing: var(--at-text-label-tracking);
                text-transform: uppercase; color: var(--at-text-muted); }

  /* A lede under a display heading. Every migration reimplemented this. */
  .at-lede { margin: 0; max-width: var(--at-measure);
             font-size: var(--at-text-body-lg-size); line-height: var(--at-text-body-lg-line);
             color: var(--at-text-secondary); }

  .at-measure { max-width: var(--at-measure); }

  /* Money and any figure that must not shift column to column as values change. */
  .at-numeric { font-variant-numeric: var(--at-font-numeric); }

  /* Layout --------------------------------------------------------------- */

  .at-shell {
    width: min(var(--at-shell-max), calc(100% - var(--at-shell-gutter) * 2));
    margin-inline: auto;
  }
  @media (max-width: 600px) {
    .at-shell { width: min(var(--at-shell-max), calc(100% - var(--at-shell-gutter-sm) * 2)); }
  }

  .at-shell-narrow { width: min(var(--at-shell-narrow), calc(100% - var(--at-shell-gutter) * 2));
                     margin-inline: auto; }

  .at-stack > * + * { margin-top: var(--at-space-4); }

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

  /* Reduced motion lives in the FIRST layer, not with the focus ring, because
     layer precedence reverses for !important declarations: the earliest layer
     wins. Here it outranks a component library's own !important; in the guards
     layer it would lose to one. */
  @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;
    }
  }
}

/* Above the component library: nothing may erase these. --------------------- */

@layer atticlet-guards {
  /* No border-radius here. Setting it would change the ELEMENT's corners, not the
     outline's — a pill button squares to 4px on keyboard focus — and browsers
     already draw the outline following the element's own radius. It also beat
     `border-radius` coming from a component library, which is a property this rule
     has no business touching. */
  :focus-visible {
    outline: var(--at-focus-width) solid var(--at-focus);
    outline-offset: var(--at-focus-offset);
  }

  /* On a BRAND fill the ring borrows that fill's foreground: brand blue on a
     brand-blue band is invisible, and 2.2:1 on the orange one.
     Only brand fills. On `--at-surface` the default ring is already correct, and in
     dark `--at-focus-on-fill` and `--at-surface` are both Ink — an Ink ring on an Ink
     button is invisible. Scope the attribute to action/success/warning tones. */
  [data-on-fill]:focus-visible { outline-color: var(--at-focus-on-fill); }
  [data-on-fill='dark']:focus-visible { outline-color: var(--at-focus-on-dark-fill); }

  /* On the inverse surface of a toast the default ring measures 1.11:1 in dark (3.29
     light, barely). Here and not in the component layer, because a ring colour set below
     this layer loses to the rule above. Both spellings of a toast: the .at-toast
     class and MUI's SnackbarContent. */
  .at-toast :focus-visible,
  .MuiSnackbarContent-root :focus-visible { outline-color: var(--at-link-on-inverse); }

  /* A row (.at-row) fills its card edge to edge and the card clips it to its radius,
     so the ring goes inside the row rather than being cut off. Here for the same
     reason as above: an offset set in the component layer loses to the default. */
  .at-row:focus-visible { outline-offset: calc(-1 * var(--at-focus-width)); }
}
