/* ── components/math-renderer/math-renderer.css ── */
/*
 * Formula Rendering (math-renderer): a formula typeset by KaTeX. The TeX source
 * stays in the DOM - readable code until the typeset output arrives, hidden
 * after. KaTeX's own stylesheet (loaded with it) sets the math fonts and
 * metrics; this sheet places the formula, its states and its term selection.
 */
@layer components {
  .math-renderer {
    display: block;
    margin: 0;
    min-inline-size: 0;
    color: inherit;

    /* the source: readable TeX before (and without) KaTeX */
    & > .math-renderer-source {
      display: block;
      font-family: var(--font-mono);
      font-size: 0.875rem;
      white-space: pre-wrap;
      overflow-wrap: anywhere;
      color: var(--muted-foreground);
    }
    &[data-state="pending"] > .math-renderer-source {
      opacity: 0.6;
    }

    /* KaTeX's output: a display formula scrolls sideways instead of overflowing its column */
    & > .math-renderer-output {
      display: block;
      overflow-x: auto;
      overflow-y: hidden;
      overscroll-behavior-x: contain;
      padding-block: 0.125rem;
    }
    & .katex-display {
      margin: 0.25em 0;
    }

    & > figcaption {
      margin-block-start: 0.5rem;
      color: var(--muted-foreground);
      font-family: var(--font-sans);
      font-size: 0.8125rem;
      text-align: center;
    }

    /* selectable terms: a hover outline, the selection outlined and underlined (not color alone) */
    & [data-df-math-id] {
      border-radius: var(--radius-sm);
      cursor: pointer;
      transition: background-color 120ms ease;
    }
    & [data-df-math-id]:hover {
      outline: 1px dashed var(--ring);
      outline-offset: 1px;
    }
    & [data-df-math-id][data-selected] {
      background: var(--accent);
      color: var(--accent-foreground);
      outline: 2px solid var(--ring);
      outline-offset: 1px;
      text-decoration: underline;
      text-underline-offset: 0.2em;
    }

    /* the term buttons: one tab stop, the pressed term marked */
    & > .math-renderer-terms {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      gap: 0.375rem;
      margin-block-start: 0.75rem;
    }
    & .math-renderer-term {
      appearance: none;
      cursor: pointer;
      border: 1px solid var(--border);
      border-radius: var(--radius-md);
      background: var(--background);
      color: var(--foreground);
      padding: 0.25rem 0.625rem;
      font-family: var(--font-sans);
      font-size: 0.75rem;
      line-height: 1.25rem;
      &:hover {
        background: var(--accent);
        color: var(--accent-foreground);
      }
      &[aria-pressed="true"] {
        border-color: var(--ring);
        background: var(--accent);
        color: var(--accent-foreground);
        text-decoration: underline;
      }
      &:focus-visible {
        outline: 2px solid var(--ring);
        outline-offset: 2px;
      }
    }
    & > .math-renderer-description {
      display: block;
      margin-block-start: 0.5rem;
      font-family: var(--font-serif);
      font-size: 0.9375rem;
      line-height: 1.5;
      text-align: center;
      &:empty {
        display: none;
      }
    }

    & > .math-renderer-error {
      display: block;
      margin-block-start: 0.5rem;
      color: var(--destructive);
      font-family: var(--font-sans);
      font-size: 0.8125rem;
      line-height: 1.5;
      white-space: pre-wrap;
    }

    /* card: the formula on a panel (demos, teaching) */
    &[data-variant="card"] {
      padding: 1rem 1.25rem;
      border: 1px solid var(--border);
      border-radius: var(--radius-lg);
      background: var(--card);
      color: var(--card-foreground);
      box-shadow: var(--shadow-xs);
    }

    /* inline: a formula inside a sentence, on its baseline */
    &[data-variant="inline"] {
      display: inline;
      & > .math-renderer-source {
        display: inline;
        font-size: 0.9em;
      }
      & > .math-renderer-output {
        display: inline;
        overflow: visible;
        padding: 0;
      }
      & > .math-renderer-error {
        display: inline;
        margin-inline-start: 0.375rem;
      }
    }

    /* typeset: the source steps aside - after the variants, so it wins over their display */
    &:is([data-state="rendered"], [data-state="highlighted"]) > .math-renderer-source {
      display: none;
    }

    @media (prefers-reduced-motion: reduce) {
      & [data-df-math-id] {
        transition: none;
      }
    }
    @media (prefers-contrast: more) {
      & > .math-renderer-source {
        color: var(--foreground);
      }
      & .math-renderer-term {
        border-color: var(--foreground);
      }
    }
    @media (forced-colors: active) {
      & [data-df-math-id][data-selected] {
        outline-color: Highlight;
        background: Highlight;
        color: HighlightText;
      }
      & .math-renderer-term[aria-pressed="true"] {
        border-color: Highlight;
      }
    }
    @media print {
      & > .math-renderer-terms,
      & > .math-renderer-description {
        display: none;
      }
      & > .math-renderer-output {
        overflow: visible;
      }
      & [data-df-math-id][data-selected] {
        background: none;
        outline: none;
        text-decoration: none;
      }
    }
  }

  /* outside elements linked to a formula's terms (data-math-for + data-math-term): the linked term is marked */
  [data-math-for][data-math-term] {
    cursor: pointer;
    &[data-selected] {
      background: var(--accent);
      color: var(--accent-foreground);
      box-shadow: inset 3px 0 0 var(--ring);
    }
    @media (forced-colors: active) {
      &[data-selected] {
        outline: 2px solid Highlight;
      }
    }
  }
}

/* ── components/math-editor/math-editor.css ── */
/*
 * Formula Editing (math-editor): a MathLive <math-field> over a native
 * textarea. The field is themed through MathLive's documented custom
 * properties and ::part() hooks; its virtual keyboard through the keyboard's
 * documented --keyboard-* / --keycap-* properties. One private hook is pinned
 * to mathlive@0.111.0: .MLK__keycap's radius (MathLive hard-codes it).
 */
@layer components {
  .math-editor {
    display: grid;
    gap: 0.375rem;
    min-inline-size: 0;

    /* the textarea: the editor without JavaScript (or when MathLive cannot load) */
    & > .math-editor-source {
      inline-size: 100%;
      min-block-size: 2.5rem;
      field-sizing: content;
      padding: 0.5rem 0.75rem;
      border: 1px solid var(--input);
      border-radius: var(--radius-md);
      background: transparent;
      color: var(--foreground);
      font-family: var(--font-mono);
      font-size: 0.875rem;
      resize: vertical;
      &:focus-visible {
        outline: 2px solid var(--ring);
        outline-offset: 2px;
      }
    }
    /* MathLive took over: the textarea steps aside but stays the form control */
    &[data-ready] > .math-editor-source {
      display: none;
    }

    /* the field: an input of the system, math inside */
    & > .math-editor-field {
      display: block;
      inline-size: 100%;
      min-block-size: 2.75rem;
      box-sizing: border-box;
      padding: 0.25rem 0.75rem;
      border: 1px solid var(--input);
      border-radius: var(--radius-md);
      background: var(--background);
      color: var(--foreground);
      font-size: 1.25rem;
      --caret-color: var(--foreground);
      --primary-color: var(--primary);
      --selection-background-color: color-mix(in oklch, var(--ring) 28%, transparent);
      --selection-color: var(--foreground);
      --contains-highlight-background-color: var(--muted);
      --placeholder-color: var(--muted-foreground);
      --smart-fence-color: var(--muted-foreground);
      --latex-color: var(--muted-foreground);
      --text-font-family: var(--font-sans);
      --ui-font-family: var(--font-sans);
      transition:
        border-color 120ms ease,
        box-shadow 120ms ease;
      &:focus-within {
        border-color: var(--ring);
        box-shadow: 0 0 0 3px color-mix(in oklch, var(--ring) 30%, transparent);
        outline: none;
      }
      /* the keyboard and the menu are the component's (toolbar, states), not MathLive's toggles */
      &::part(virtual-keyboard-toggle),
      &::part(menu-toggle) {
        display: none;
      }
    }

    /* the toolbar: data-math-command buttons */
    & > .math-editor-tools {
      display: flex;
      flex-wrap: wrap;
      gap: 0.25rem;
    }

    & > .math-editor-message {
      color: var(--destructive);
      font-size: 0.8125rem;
      line-height: 1.4;
    }

    /* states */
    &[data-state="invalid"] > .math-editor-field,
    &[data-state="invalid"] > .math-editor-source {
      border-color: var(--destructive);
      &:focus-within,
      &:focus-visible {
        box-shadow: 0 0 0 3px color-mix(in oklch, var(--destructive) 25%, transparent);
      }
    }
    &[data-state="readonly"] > .math-editor-field {
      background: var(--muted);
    }
    &[data-state="disabled"] > .math-editor-field,
    &[data-state="disabled"] > .math-editor-source {
      opacity: 0.5;
      cursor: not-allowed;
    }

    /* the virtual keyboard, leased into the editor (MathLive's documented properties) */
    & > .math-editor-keyboard {
      position: relative;
      /* in a container MathLive takes its backdrop padding out of the measured height - the bottom row was clipped */
      --keyboard-padding-top: 0px;
      --keyboard-padding-bottom: 0px;
      --keyboard-background: var(--muted);
      --keyboard-border: var(--border);
      --keyboard-accent-color: var(--primary);
      --keyboard-horizontal-rule: var(--border);
      --keyboard-toolbar-text: var(--muted-foreground);
      --keyboard-toolbar-text-active: var(--foreground);
      --keyboard-toolbar-background-hover: var(--accent);
      --keyboard-toolbar-background-selected: var(--background);
      --keycap-background: var(--background);
      --keycap-background-hover: var(--accent);
      --keycap-background-active: var(--primary);
      --keycap-background-pressed: var(--primary);
      --keycap-border: var(--border);
      --keycap-border-bottom: var(--border);
      --keycap-text: var(--foreground);
      --keycap-text-active: var(--primary-foreground);
      --keycap-text-pressed: var(--primary-foreground);
      --keycap-secondary-background: var(--secondary);
      --keycap-secondary-background-hover: var(--accent);
      --keycap-secondary-text: var(--secondary-foreground);
      --keycap-secondary-border: var(--border);
      --keycap-secondary-border-bottom: var(--border);
      --keycap-primary-background: var(--primary);
      --keycap-primary-background-hover: color-mix(in oklch, var(--primary) 88%, var(--background));
      --keycap-primary-text: var(--primary-foreground);
      border-radius: var(--radius-lg);
      overflow: hidden;
      &:empty {
        display: none;
      }
      /* private, pinned to mathlive@0.111.0 (tests/e2e/math/math-editor.e2e.ts reads it). MathLive injects its
         sheet unlayered, which beats any normal declaration in a layer - only !important wins from here */
      & .MLK__keycap {
        border-radius: var(--radius-sm) !important;
      }
    }

    @media (prefers-reduced-motion: reduce) {
      & > .math-editor-field {
        transition: none;
      }
    }
    @media (prefers-contrast: more) {
      & > .math-editor-field,
      & > .math-editor-source {
        border-color: var(--foreground);
      }
    }
    @media (forced-colors: active) {
      & > .math-editor-field {
        border-color: CanvasText;
        &:focus-within {
          outline: 2px solid Highlight;
        }
      }
      &[data-state="invalid"] > .math-editor-field {
        border-color: Mark;
      }
    }
    @media print {
      & > .math-editor-tools,
      & > .math-editor-keyboard {
        display: none;
      }
    }
  }
}

/* ── components/math-compute/math-compute.css ── */
/*
 * Compute & Explain (math-compute): an input, an operation and the engine's
 * record - input and result side by side, the conditions they hold under, a
 * solver's candidates, the steps the engine reports and its notices. Formulas
 * are Formula Rendering elements; statuses are Badges. Explanation prose is
 * set in the serif face, the record and its controls in the sans, the JSON in
 * the monospace.
 */
@layer components {
  .math-compute {
    display: grid;
    gap: 0.75rem;
    min-inline-size: 0;
    padding: 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--card);
    color: var(--card-foreground);

    & .math-compute-source {
      inline-size: 100%;
      min-block-size: 2.5rem;
      field-sizing: content;
      padding: 0.5rem 0.75rem;
      border: 1px solid var(--input);
      border-radius: var(--radius-md);
      background: var(--background);
      color: var(--foreground);
      font-family: var(--font-mono);
      font-size: 0.875rem;
      resize: vertical;
      &:focus-visible {
        outline: 2px solid var(--ring);
        outline-offset: 2px;
      }
    }

    & .math-compute-tools {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      gap: 0.5rem;
    }

    & > .math-compute-status {
      color: var(--muted-foreground);
      font-size: 0.8125rem;
      &:empty {
        display: none;
      }
    }
    &:is([data-state="error"], [data-state="unsupported"]) > .math-compute-status {
      padding: 0.5rem 0.75rem;
      border-radius: var(--radius-md);
      border: 1px solid var(--border);
      color: var(--foreground);
      font-size: 0.875rem;
    }
    &[data-state="error"] > .math-compute-status {
      border-color: var(--destructive);
      color: var(--destructive);
    }
    &[data-state="computing"] > .math-compute-status::before {
      content: "";
      display: inline-block;
      inline-size: 0.75em;
      block-size: 0.75em;
      margin-inline-end: 0.5em;
      border: 2px solid currentColor;
      border-inline-end-color: transparent;
      border-radius: 50%;
      vertical-align: -0.1em;
      animation: math-compute-spin 0.8s linear infinite;
    }

    /* the record */
    & > .math-compute-view {
      display: grid;
      gap: 0.75rem;
      padding-block-start: 0.75rem;
      border-block-start: 1px solid var(--border);
      font-family: var(--font-sans);
      line-height: 1.55;
    }
    & .math-compute-pair {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
      gap: 0.75rem;
      & > .math-renderer {
        padding: 0.75rem;
        border: 1px solid var(--border);
        border-radius: var(--radius-md);
        background: var(--background);
      }
    }
    & h4 {
      margin: 0 0 0.25rem;
      color: var(--muted-foreground);
      font-family: var(--font-sans);
      font-size: 0.75rem;
      font-weight: 600;
      letter-spacing: 0.04em;
      text-transform: uppercase;
    }
    & :is(.math-compute-conditions, .math-compute-candidates-box) ul {
      display: flex;
      flex-wrap: wrap;
      gap: 0.375rem 1rem;
      margin: 0;
      padding: 0;
      list-style: none;
      & li {
        display: inline-flex;
        align-items: baseline;
        gap: 0.375rem;
      }
    }
    & .math-compute-origin,
    & .math-compute-coverage,
    & .math-compute-rule,
    & .math-compute-recorded {
      color: var(--muted-foreground);
      font-family: var(--font-sans);
      font-size: 0.8125rem;
    }
    & .math-compute-coverage,
    & .math-compute-explanation,
    & .math-compute-verdict,
    & .math-compute-recorded {
      margin: 0;
    }
    /* explanation prose in the serif face; the record around it (lists, badges, controls) stays sans */
    & .math-compute-explanation,
    & .math-compute-verdict {
      font-family: var(--font-serif);
    }

    /* the steps the engine reports */
    & .math-compute-steps {
      display: grid;
      gap: 0.5rem;
      font-family: var(--font-sans);
      & ol {
        display: grid;
        gap: 0.125rem;
        margin: 0;
        padding-inline-start: 1.5rem;
      }
    }
    & .math-compute-step {
      appearance: none;
      padding: 0.125rem 0.375rem;
      border: 0;
      border-radius: var(--radius-sm);
      background: transparent;
      color: inherit;
      font: inherit;
      font-size: 0.875rem;
      text-align: start;
      cursor: pointer;
      &:hover {
        background: var(--accent);
        color: var(--accent-foreground);
      }
      &[aria-current="step"] {
        background: var(--accent);
        color: var(--accent-foreground);
        font-weight: 600;
        box-shadow: inset 3px 0 0 var(--ring);
      }
      &:focus-visible {
        outline: 2px solid var(--ring);
        outline-offset: 1px;
      }
    }
    & .math-compute-step-tools {
      display: flex;
      flex-wrap: wrap;
      gap: 0.375rem;
    }

    & .math-compute-notices {
      margin: 0;
      padding-inline-start: 1.25rem;
      color: var(--muted-foreground);
      font-family: var(--font-sans);
      font-size: 0.8125rem;
    }
    & .math-compute-record {
      font-family: var(--font-sans);
      font-size: 0.8125rem;
      & > summary {
        cursor: pointer;
        color: var(--muted-foreground);
      }
      & > pre {
        max-block-size: 16rem;
        overflow: auto;
        margin: 0.5rem 0 0;
        padding: 0.75rem;
        border-radius: var(--radius-md);
        background: var(--muted);
        color: var(--foreground);
        font-family: var(--font-mono);
        font-size: 0.75rem;
      }
    }

    @media (prefers-reduced-motion: reduce) {
      &[data-state="computing"] > .math-compute-status::before {
        animation: none;
      }
    }
    @media (prefers-contrast: more) {
      border-color: var(--foreground);
      & .math-compute-source {
        border-color: var(--foreground);
      }
    }
    @media (forced-colors: active) {
      & .math-compute-step[aria-current="step"] {
        outline: 2px solid Highlight;
      }
    }
    @media print {
      & .math-compute-tools,
      & .math-compute-step-tools,
      & .math-compute-record {
        display: none;
      }
    }
  }

  @keyframes math-compute-spin {
    to {
      rotate: 1turn;
    }
  }
}
