/* Runo — element defaults and accessibility primitives.
 *
 * Everything here applies to bare elements. Components live in
 * components.css; nothing in this file may use a class except the small set
 * of a11y utilities at the bottom, which have no visual design of their own.
 */

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

  /* Author display rules must never defeat the hidden attribute. */
  [hidden] { display: none !important; }

  html {
    /* Anchors and scripted scrolls land below the sticky topbar instead of
     * underneath it. */
    scroll-padding-top: calc(var(--topbar-h) + var(--space-4));
    -webkit-text-size-adjust: 100%;
  }

  body {
    margin: 0;
    /* Wide content scrolls inside its own container; the page never goes
     * sideways. `clip` rather than `hidden` because `hidden` would make the
     * body a scroll container and break the sticky topbar. A backstop, not
     * a licence — an overflowing child is still a bug to fix at source. */
    overflow-x: clip;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: var(--leading-normal);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
  }

  h1, h2, h3, h4 {
    margin: 0;
    font-weight: 600;
    line-height: var(--leading-tight);
    text-wrap: balance;
  }

  h1 { font-size: var(--text-2xl); font-family: var(--font-serif); }
  h2 { font-size: var(--text-xl); font-family: var(--font-serif); }
  h3 { font-size: var(--text-md); }
  h4 { font-size: var(--text-base); }

  p { margin: 0 0 var(--space-3); }
  p:last-child { margin-bottom: 0; }

  a {
    color: var(--accent);
    text-decoration-thickness: 1px;
    text-underline-offset: 0.15em;
  }
  a:hover { color: var(--accent-hover); }

  small { font-size: var(--text-sm); }

  code, kbd, samp, pre { font-family: var(--font-mono); font-size: 0.9em; }

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

  /* Inherit rather than reset to the UA's 13px Arial. */
  button, input, select, textarea {
    font: inherit;
    color: inherit;
  }

  /* --- focus ----------------------------------------------------------
   * The old stylesheet had two :focus rules in 821 lines, so a keyboard
   * user effectively could not tell where they were (WCAG 2.4.7). This is
   * a global default rather than something each component opts into —
   * forgetting it is the failure mode.
   *
   * :focus-visible, not :focus, so a mouse click on a button does not leave
   * a ring behind; keyboard and programmatic focus still show one.
   */
  :focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
  }
  /* Only safe because the rule above restores it for keyboard users. */
  :focus:not(:focus-visible) { outline: none; }

  ::selection {
    background: var(--accent-subtle);
    color: var(--text);
  }

  hr {
    border: 0;
    border-top: 1px solid var(--border);
    margin: var(--space-5) 0;
  }

  fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
  legend { padding: 0; }

  /* --- scrollbars ------------------------------------------------------ */
  * {
    scrollbar-width: thin;
    scrollbar-color: var(--border-strong) transparent;
  }
  *::-webkit-scrollbar { width: 10px; height: 10px; }
  *::-webkit-scrollbar-track { background: transparent; }
  *::-webkit-scrollbar-thumb {
    background-color: var(--border-strong);
    border-radius: var(--radius-full);
    border: 2px solid var(--bg);
  }
  *::-webkit-scrollbar-thumb:hover { background-color: var(--text-faint); }
  *::-webkit-scrollbar-corner { background: transparent; }

  /* --- motion ---------------------------------------------------------
   * Respected globally rather than per-animation. Not zero: a 1ms duration
   * still fires transitionend, so JS that waits on it keeps working.
   */
  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 1ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 1ms !important;
      scroll-behavior: auto !important;
    }
  }

  /* --- forced colours --------------------------------------------------
   * Windows High Contrast replaces our palette wholesale. Borders that were
   * drawn with background colour vanish, so ask for a real one.
   */
  @media (forced-colors: active) {
    .btn,
    .card,
    .badge { border: 1px solid; }
  }
}

@layer utilities {
  /* Visible only to assistive technology. The clip-path pair is the
   * modern-safe version; height/width of 1px keeps it focusable. */
  .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  /* A skip link: hidden until focused, then a real, visible control.
   * `.sr-only-focusable` must come after `.sr-only` to win. */
  .sr-only-focusable:focus-visible {
    position: fixed;
    top: var(--space-3);
    left: var(--space-3);
    z-index: 100;
    width: auto;
    height: auto;
    margin: 0;
    padding: var(--space-2) var(--space-4);
    clip-path: none;
    background: var(--surface-raised);
    color: var(--text);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    text-decoration: none;
  }
}
