/*
 * Eduport base styles (opt-in): import '@eduportdesign/tokens/css/base' after '@eduportdesign/tokens/css'.
 *
 * Styles bare elements (body, headings, text, links, code, focus) with the semantic tokens, so the parts of a page
 * that aren't components match the components. Everything sits in the `ep-base` cascade layer and uses :where(),
 * so any app CSS, layered or not, overrides it without specificity fights.
 *
 * Margins on headings, paragraphs and lists are reset to 0: lay out pages with `gap` (see docs/layout.md).
 * Wrap long-form text (help pages, descriptions, rich text) in `.ep-prose` to get reading spacing back.
 */
@layer ep-base {
  :where(*, *::before, *::after) {
    box-sizing: border-box;
  }

  /* color-scheme comes from tokens.css and theme-dark.css, so native UI follows the theme without this file. */
  :where(:root) {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  /* With <ep-app-shell>'s sticky top bar, anchor jumps and focused fields stop below it (WCAG 2.4.11). */
  :where(html:has(ep-app-shell)) {
    scroll-padding-block-start: calc(var(--ep-layout-header-height) + var(--ep-space-100));
  }

  :where(body) {
    margin: 0;
    background: var(--ep-color-background-default);
    color: var(--ep-color-text-primary);
    font: var(--ep-font-body-md);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }

  /*
   * Headings: one type scale for every app, from the font.heading.* type roles. h6 stays at label size.
   * Slotted headings (<h3 slot="heading"> in a card) are skipped: page styles beat ::slotted(), so the
   * component's own heading style would otherwise lose.
   */
  :where(h1, h2, h3, h4, h5, h6):where(:not([slot])) {
    margin: 0;
    color: var(--ep-color-text-primary);
    text-wrap: balance;
  }

  :where(h1:not([slot])) {
    font: var(--ep-font-heading-xl);
    letter-spacing: -0.03em;
  }

  :where(h2:not([slot])) {
    font: var(--ep-font-heading-lg);
    letter-spacing: -0.02em;
  }

  :where(h3:not([slot])) {
    font: var(--ep-font-heading-md);
    letter-spacing: -0.01em;
  }

  :where(h4:not([slot])) {
    font: var(--ep-font-heading-sm);
  }

  :where(h5:not([slot])) {
    font: var(--ep-font-heading-xs);
  }

  :where(h6:not([slot])) {
    font: var(--ep-font-label-md);
    font-weight: var(--ep-font-weight-semibold);
  }

  :where(p, ul, ol, dl, dd, figure, blockquote, pre) {
    margin: 0;
  }

  :where(ul, ol) {
    padding-inline-start: var(--ep-space-300);
  }

  :where(small) {
    font-size: var(--ep-font-size-200);
  }

  :where(strong, b) {
    font-weight: var(--ep-font-weight-semibold);
  }

  :where(a) {
    color: var(--ep-color-text-link);
    text-decoration-thickness: from-font;
    text-underline-offset: 0.15em;
  }

  :where(a:hover) {
    text-decoration-thickness: 2px;
  }

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

  :where(pre) {
    overflow-x: auto;
  }

  :where(hr) {
    margin: 0;
    border: 0;
    border-block-start: var(--ep-border-width-thin) solid var(--ep-color-border-default);
  }

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

  :where(button, input, select, textarea) {
    font: inherit;
  }

  /* Native focusable elements get the same ring as the components. */
  :where(:focus-visible) {
    outline: var(--ep-border-width-thick) solid var(--ep-color-border-focus);
    outline-offset: 2px;
  }

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

  /* Hides content visually but keeps it for screen readers, e.g. a label for an icon-only column. */
  :where(.ep-visually-hidden:not(:focus-within)) {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  /* Long-form text: restores reading spacing and caps the line length. */
  :where(.ep-prose) {
    max-width: var(--ep-layout-width-prose);
  }

  :where(.ep-prose) :where(p, ul, ol, dl, blockquote, pre, figure, table) {
    margin-block: 0 var(--ep-space-200);
  }

  /* Avoids a single short word on the last line of a paragraph. Browsers without support wrap as usual. */
  :where(.ep-prose) :where(p, li, blockquote) {
    text-wrap: pretty;
  }

  :where(.ep-prose) :where(h1, h2, h3, h4, h5, h6) {
    margin-block: var(--ep-space-400) var(--ep-space-100);
  }

  :where(.ep-prose) :where(li + li) {
    margin-block-start: var(--ep-space-50);
  }

  :where(.ep-prose) :where(:first-child) {
    margin-block-start: 0;
  }

  :where(.ep-prose) :where(:last-child) {
    margin-block-end: 0;
  }
}
