/* ── components/mockup-code/mockup-code.css ── */
/* -- Mockup Code component --------------------------------------------- */
/* A terminal / code window: a stack of <pre> lines under window chrome.
   Prompts, line numbers and the cursor are CSS-generated, so selecting and
   copying the text never picks them up. CSS only. */

@layer components {
  .mockup-code {
    /* surface + the line tones, retuned per variant for its contrast */
    --_bg: oklch(0.21 0.006 286);
    --_fg: oklch(0.94 0 0);
    --_dim: oklch(0.68 0.01 286);
    --_success: oklch(0.8 0.16 150);
    --_warning: oklch(0.84 0.15 80);
    --_info: oklch(0.76 0.13 245);
    --_destructive: oklch(0.72 0.18 25);
    --_px: 1.25rem;

    position: relative;
    box-sizing: border-box;
    min-width: 0;
    max-width: 100%;
    padding-block: 1.25rem 1rem;
    border-radius: var(--radius-xl);
    background: var(--_bg);
    color: var(--_fg);
    font-family: var(--font-mono);
    font-size: 0.875rem;
    line-height: 1.7;
    /* code shows what is typed: no => → ⇒ or <!-- → arrow ligatures */
    font-variant-ligatures: none;
    text-align: start;
    /* code reads left to right even inside an RTL page */
    direction: ltr;
    unicode-bidi: isolate;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    /* one column as wide as the longest line (at least the window): every
       line - and its highlight band - spans the same width while the
       window scrolls sideways */
    display: grid;
    grid-template-columns: minmax(100%, max-content);

    /* -- Window chrome: three dots; data-chrome="mac" colours them,
       data-chrome="none" drops the bar ---------------------------- */
    &::before {
      content: "";
      display: block;
      width: 0.75rem;
      height: 0.75rem;
      margin-inline-start: var(--_px);
      margin-block-end: 1rem;
      border-radius: 9999px;
      color: var(--_dim);
      background: currentColor;
      opacity: 0.45;
      box-shadow:
        1.25rem 0 0 currentColor,
        2.5rem 0 0 currentColor;
    }
    &[data-chrome="mac"]::before {
      opacity: 1;
      background: oklch(0.68 0.2 25);
      box-shadow:
        1.25rem 0 0 oklch(0.82 0.16 85),
        2.5rem 0 0 oklch(0.74 0.17 145);
    }
    &[data-chrome="none"] {
      padding-block-start: 1rem;
    }
    &[data-chrome="none"]::before {
      display: none;
    }

    /* -- A title in the bar: data-title="bash - 80x24" -------------- */
    &[data-title]:not([data-chrome="none"])::after {
      content: attr(data-title);
      position: absolute;
      inset-block-start: 0.75rem;
      inset-inline: 4.5rem;
      font-family: var(--font-sans);
      font-size: 0.75rem;
      line-height: 1.25rem;
      text-align: center;
      color: var(--_dim);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      pointer-events: none;
    }

    /* -- Lines -------------------------------------------------- */
    & > pre {
      box-sizing: border-box;
      margin: 0;
      padding: 0 var(--_px);
      border: 0;
      border-radius: 0;
      background: transparent;
      color: inherit;
      font: inherit;
      white-space: pre;
    }
    & > pre > code {
      padding: 0;
      border: 0;
      background: none;
      color: inherit;
      font: inherit;
    }

    /* the prompt: $, >, ~, a number - generated, never selected or copied */
    & > pre[data-prefix]::before,
    &[data-numbers] > pre::before {
      display: inline-block;
      width: 2ch;
      margin-inline-end: 1.25ch;
      text-align: end;
      color: var(--_dim);
      user-select: none;
    }
    & > pre[data-prefix]::before {
      content: attr(data-prefix);
    }

    /* data-numbers on the window: every line without its own prefix is
       numbered (CSS counters - nothing to keep in sync) */
    &[data-numbers] {
      counter-reset: mockup-line;
    }
    &[data-numbers] > pre {
      counter-increment: mockup-line;
    }
    &[data-numbers] > pre:not([data-prefix])::before {
      content: counter(mockup-line);
    }

    /* -- Tones: output lines --------------------------------------- */
    & > pre[data-tone] {
      color: var(--_tone);
    }
    & > pre[data-tone="success"] {
      --_tone: var(--_success);
    }
    & > pre[data-tone="warning"] {
      --_tone: var(--_warning);
    }
    & > pre[data-tone="info"] {
      --_tone: var(--_info);
    }
    & > pre[data-tone="destructive"] {
      --_tone: var(--_destructive);
    }
    & > pre[data-tone="muted"] {
      --_tone: var(--_dim);
    }

    /* -- Highlighted line: a tinted band in the line's tone (or the
       text colour); data-highlight="solid" fills it -------------- */
    & > pre[data-highlight] {
      background: color-mix(in oklch, var(--_tone, var(--_fg)) 16%, transparent);
    }
    & > pre[data-highlight="solid"] {
      background: var(--_tone, var(--_fg));
      color: var(--_bg);
      &::before {
        color: inherit;
        opacity: 0.7;
      }
    }

    /* -- Diff: data-diff="add" / "remove" lines ----------------------
       The sign sits in its own column before the code (generated, never
       copied), so numbers and prompts keep theirs; every line of a diff
       window gets the column, so context lines line up. Word changes
       inside a line are native <ins> / <del>. */
    &:where(:has(> pre[data-diff])) > pre > code::before {
      content: "";
      display: inline-block;
      width: 2ch;
      user-select: none;
    }
    & > pre[data-diff="add"] {
      --_tone: var(--_success);
    }
    & > pre[data-diff="remove"] {
      --_tone: var(--_destructive);
    }
    & > pre[data-diff] {
      color: var(--_tone);
      background: color-mix(in oklch, var(--_tone) 14%, transparent);
    }
    & > pre[data-diff="add"] > code::before {
      content: "+";
    }
    & > pre[data-diff="remove"] > code::before {
      content: "-";
    }
    & > pre > code :is(ins, del) {
      border-radius: 0.2em;
      color: inherit;
      text-decoration: none;
    }
    & > pre > code ins {
      background: color-mix(in oklch, var(--_success) 32%, transparent);
    }
    & > pre > code del {
      background: color-mix(in oklch, var(--_destructive) 32%, transparent);
      text-decoration: line-through;
    }
    /* data-strike (and the animated diff) strikes removed lines through */
    &:is([data-strike], [data-animate]) > pre[data-diff="remove"] {
      opacity: 0.8;
      & > code {
        text-decoration: line-through;
        text-decoration-color: color-mix(in oklch, var(--_tone) 70%, transparent);
      }
    }

    /* -- Animated diff: removed lines turn red and strike through, added
       lines slide in green, one after another; context lines stay.
       Replays whenever data-animate is set again. ------------------ */
    &[data-animate] > pre {
      --_stagger: var(--mockup-code-stagger, 140ms);
      --_delay: calc(var(--mockup-code-delay, 300ms) + var(--_i, 0) * var(--_stagger));
    }
    &[data-animate] > pre[data-diff="remove"] {
      animation: mockup-code-remove 450ms ease-out var(--_delay) both;
      & > code {
        animation: mockup-code-strike 450ms ease-out var(--_delay) both;
      }
    }
    &[data-animate] > pre[data-diff="add"] {
      animation: mockup-code-add 450ms cubic-bezier(0.2, 0.8, 0.2, 1) var(--_delay) both;
    }

    /* -- Cursor: a blinking block after the line --------------------- */
    & > pre[data-cursor] > code::after {
      content: "";
      display: inline-block;
      width: 0.6em;
      height: 1.15em;
      margin-inline-start: 0.1em;
      vertical-align: text-bottom;
      background: currentColor;
      animation: mockup-code-cursor 1s steps(1) infinite;
    }

    /* -- Wrap instead of scrolling --------------------------------- */
    &[data-wrap] {
      grid-template-columns: minmax(0, 1fr);
    }
    &[data-wrap] > pre {
      white-space: pre-wrap;
      overflow-wrap: anywhere;
      padding-inline-start: calc(var(--_px) + 3.25ch);
      text-indent: -3.25ch;
    }
    &[data-wrap] > pre:not([data-prefix]):not(:where([data-numbers] > pre)) {
      padding-inline-start: var(--_px);
      text-indent: 0;
    }

    /* -- Variants (surfaces) --------------------------------------- */
    &[data-variant="muted"],
    &[data-variant="outline"] {
      --_fg: var(--foreground);
      --_dim: var(--muted-foreground);
      --_success: oklch(0.52 0.14 150);
      --_warning: oklch(0.58 0.14 70);
      --_info: oklch(0.52 0.14 250);
      --_destructive: var(--destructive);
    }
    &[data-variant="muted"] {
      --_bg: var(--muted);
    }
    &[data-variant="outline"] {
      --_bg: var(--card);
      border: 1px solid var(--border);
    }
    &[data-variant="primary"] {
      --_bg: var(--primary);
      --_fg: var(--primary-foreground);
      --_dim: color-mix(in oklch, var(--primary-foreground) 65%, var(--primary));
    }

    /* -- Sizes ------------------------------------------------------ */
    &[data-size="sm"] {
      font-size: 0.75rem;
      --_px: 1rem;
      padding-block: 1rem 0.75rem;
    }
    &[data-size="lg"] {
      font-size: 1rem;
      --_px: 1.5rem;
    }
  }

  @keyframes mockup-code-cursor {
    50% {
      opacity: 0;
    }
  }
  /* each keyframe starts neutral and ends on the line's own diff styles */
  @keyframes mockup-code-remove {
    from {
      color: var(--_fg);
      background: transparent;
      opacity: 1;
    }
  }
  @keyframes mockup-code-strike {
    from {
      text-decoration-color: transparent;
    }
  }
  @keyframes mockup-code-add {
    from {
      color: var(--_fg);
      background: transparent;
      opacity: 0;
      translate: -1.5rem 0;
    }
  }

  /* the line's position drives the stagger: sibling-index() where the
     browser has it, a counted fallback for the first 24 lines elsewhere */
  @supports (order: sibling-index()) {
    .mockup-code[data-animate] > pre {
      --_i: sibling-index();
    }
  }
  @supports not (order: sibling-index()) {
    .mockup-code[data-animate] > pre:nth-child(2) {
      --_i: 2;
    }
    .mockup-code[data-animate] > pre:nth-child(3) {
      --_i: 3;
    }
    .mockup-code[data-animate] > pre:nth-child(4) {
      --_i: 4;
    }
    .mockup-code[data-animate] > pre:nth-child(5) {
      --_i: 5;
    }
    .mockup-code[data-animate] > pre:nth-child(6) {
      --_i: 6;
    }
    .mockup-code[data-animate] > pre:nth-child(7) {
      --_i: 7;
    }
    .mockup-code[data-animate] > pre:nth-child(8) {
      --_i: 8;
    }
    .mockup-code[data-animate] > pre:nth-child(9) {
      --_i: 9;
    }
    .mockup-code[data-animate] > pre:nth-child(10) {
      --_i: 10;
    }
    .mockup-code[data-animate] > pre:nth-child(11) {
      --_i: 11;
    }
    .mockup-code[data-animate] > pre:nth-child(12) {
      --_i: 12;
    }
    .mockup-code[data-animate] > pre:nth-child(13) {
      --_i: 13;
    }
    .mockup-code[data-animate] > pre:nth-child(14) {
      --_i: 14;
    }
    .mockup-code[data-animate] > pre:nth-child(15) {
      --_i: 15;
    }
    .mockup-code[data-animate] > pre:nth-child(16) {
      --_i: 16;
    }
    .mockup-code[data-animate] > pre:nth-child(n + 17) {
      --_i: 17;
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  @layer components {
    .mockup-code > pre[data-cursor] > code::after {
      animation: none;
    }
    /* the diff shows its end state at once */
    .mockup-code[data-animate] > pre,
    .mockup-code[data-animate] > pre > code {
      animation: none;
    }
  }
}

/* paper cannot scroll: printed, every line wraps as with data-wrap, and the
   window prints its colors as designed - a print dialog drops background
   graphics by default, which left the terminal's light text pale on white
   paper. An animated diff prints its end state. */
@media print {
  @layer components {
    .mockup-code {
      overflow-x: visible;
      grid-template-columns: minmax(0, 1fr);
      print-color-adjust: exact;
    }
    .mockup-code > pre {
      white-space: pre-wrap;
      overflow-wrap: anywhere;
      padding-inline-start: calc(var(--_px) + 3.25ch);
      text-indent: -3.25ch;
    }
    .mockup-code > pre:not([data-prefix]):not(:where([data-numbers] > pre)) {
      padding-inline-start: var(--_px);
      text-indent: 0;
    }
    .mockup-code > pre[data-cursor] > code::after,
    .mockup-code[data-animate] > pre,
    .mockup-code[data-animate] > pre > code {
      animation: none;
    }
  }
}

@media (prefers-contrast: more) {
  @layer components {
    .mockup-code {
      --_dim: var(--_fg);
      border: 1px solid var(--_fg);
    }
  }
}

@media (forced-colors: active) {
  @layer components {
    .mockup-code {
      border: 1px solid CanvasText;
    }
    .mockup-code > pre[data-highlight] {
      forced-color-adjust: none;
      background: Highlight;
      color: HighlightText;
    }
    /* the +/- signs carry the diff; <ins>/<del> get system marks */
    .mockup-code > pre > code :is(ins, del) {
      outline: 1px solid CanvasText;
    }
  }
}

/* ── components/terminal/terminal.css ── */
/* -- Terminal component ------------------------------------------------- */
/* A terminal emulator (xterm.js) in the Code Mockup's window. xterm paints
   its own cells, so its colours are custom properties the runtime reads:
   --terminal-background / -foreground / -cursor / -cursor-accent /
   -selection and the sixteen ANSI colours (--terminal-red ...
   --terminal-bright-white). A variant sets them; a theme or a page can set
   any of them. --terminal-cursor-style (block, bar, underline) and
   --terminal-line-height (a factor) are read the same way. */

@layer components {
  /* xterm.js names its own element "terminal xterm" too - every rule here skips it */
  .terminal:not(.xterm) {
    /* the default: a dark screen in every theme, the Code Mockup's tones */
    --terminal-background: oklch(0.21 0.006 286);
    --terminal-foreground: oklch(0.94 0 0);
    --terminal-cursor: var(--terminal-foreground);
    --terminal-cursor-accent: var(--terminal-background);
    --terminal-selection: color-mix(in oklch, var(--terminal-foreground) 28%, transparent);
    --terminal-black: oklch(0.32 0.01 286);
    --terminal-red: oklch(0.72 0.18 25);
    --terminal-green: oklch(0.8 0.16 150);
    --terminal-yellow: oklch(0.84 0.15 80);
    --terminal-blue: oklch(0.76 0.13 245);
    --terminal-magenta: oklch(0.76 0.15 320);
    --terminal-cyan: oklch(0.8 0.11 200);
    --terminal-white: oklch(0.86 0 0);
    --terminal-bright-black: oklch(0.62 0.01 286);
    --terminal-bright-red: oklch(0.8 0.15 25);
    --terminal-bright-green: oklch(0.88 0.15 150);
    --terminal-bright-yellow: oklch(0.91 0.13 90);
    --terminal-bright-blue: oklch(0.84 0.11 245);
    --terminal-bright-magenta: oklch(0.84 0.13 320);
    --terminal-bright-cyan: oklch(0.88 0.1 200);
    --terminal-bright-white: oklch(0.99 0 0);
    --terminal-cursor-style: block;
    --terminal-line-height: 1.25;
    --terminal-height: 20rem;
    --terminal-padding: 0.5rem 1rem 0.75rem;

    position: relative;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    min-width: 0;
    max-width: 100%;
    block-size: var(--terminal-height);
    padding-block-start: 2.5rem;
    border-radius: var(--radius-xl);
    background: var(--terminal-background);
    color: var(--terminal-foreground);
    font-family: var(--font-mono);
    font-size: 0.875rem;
    text-align: start;
    /* a terminal reads left to right inside an RTL page too */
    direction: ltr;
    unicode-bidi: isolate;
    overflow: hidden;

    /* -- Window chrome, as the Code Mockup's: three dots; data-chrome="mac"
       colours them, data-chrome="none" drops the bar --------------------- */
    &::before {
      content: "";
      position: absolute;
      inset-block-start: 1rem;
      inset-inline-start: 1.25rem;
      width: 0.75rem;
      height: 0.75rem;
      border-radius: 9999px;
      color: color-mix(in oklch, var(--terminal-foreground) 60%, var(--terminal-background));
      background: currentColor;
      opacity: 0.45;
      box-shadow:
        1.25rem 0 0 currentColor,
        2.5rem 0 0 currentColor;
    }
    &[data-chrome="mac"]::before {
      opacity: 1;
      background: oklch(0.68 0.2 25);
      box-shadow:
        1.25rem 0 0 oklch(0.82 0.16 85),
        2.5rem 0 0 oklch(0.74 0.17 145);
    }
    &[data-chrome="none"] {
      padding-block-start: 0;
    }
    &[data-chrome="none"]::before {
      display: none;
    }
    /* a title in the bar: data-title="bash - 80x24" */
    &[data-title]:not([data-chrome="none"])::after {
      content: attr(data-title);
      position: absolute;
      inset-block-start: 0.75rem;
      inset-inline: 4.5rem;
      font-family: var(--font-sans);
      font-size: 0.75rem;
      line-height: 1.25rem;
      text-align: center;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      color: color-mix(in oklch, var(--terminal-foreground) 65%, var(--terminal-background));
    }

    /* data-rows: as high as that many rows - the screen sizes the box */
    &[data-rows] {
      block-size: auto;
    }
    &:focus-within {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }

    /* -- Sizes ------------------------------------------------------------ */
    &[data-size="sm"] {
      font-size: 0.75rem;
    }
    &[data-size="lg"] {
      font-size: 1rem;
    }

    /* -- Variants --------------------------------------------------------- */
    /* muted: the page's own surface - light in light mode; the ANSI colours
       are mixed toward the text so they keep their contrast in both */
    &[data-variant="muted"] {
      --terminal-background: var(--muted);
      --terminal-foreground: var(--foreground);
      --terminal-black: var(--foreground);
      --terminal-red: color-mix(in oklch, var(--foreground) 30%, oklch(0.6 0.2 25));
      --terminal-green: color-mix(in oklch, var(--foreground) 35%, oklch(0.62 0.16 150));
      --terminal-yellow: color-mix(in oklch, var(--foreground) 40%, oklch(0.7 0.15 75));
      --terminal-blue: color-mix(in oklch, var(--foreground) 30%, oklch(0.6 0.16 255));
      --terminal-magenta: color-mix(in oklch, var(--foreground) 30%, oklch(0.6 0.18 320));
      --terminal-cyan: color-mix(in oklch, var(--foreground) 35%, oklch(0.62 0.11 200));
      --terminal-white: var(--muted-foreground);
      --terminal-bright-black: var(--muted-foreground);
      --terminal-bright-red: var(--terminal-red);
      --terminal-bright-green: var(--terminal-green);
      --terminal-bright-yellow: var(--terminal-yellow);
      --terminal-bright-blue: var(--terminal-blue);
      --terminal-bright-magenta: var(--terminal-magenta);
      --terminal-bright-cyan: var(--terminal-cyan);
      --terminal-bright-white: var(--foreground);
      border: 1px solid var(--border);
    }
    /* dos: the 16 colours of a PC's text mode on black, square, an
       underline cursor */
    &[data-variant="dos"] {
      --terminal-background: #000000;
      --terminal-foreground: #aaaaaa;
      --terminal-cursor: #aaaaaa;
      --terminal-selection: rgb(170 170 170 / 0.35);
      --terminal-black: #000000;
      --terminal-red: #aa0000;
      --terminal-green: #00aa00;
      --terminal-yellow: #aa5500;
      --terminal-blue: #0000aa;
      --terminal-magenta: #aa00aa;
      --terminal-cyan: #00aaaa;
      --terminal-white: #aaaaaa;
      --terminal-bright-black: #555555;
      --terminal-bright-red: #ff5555;
      --terminal-bright-green: #55ff55;
      --terminal-bright-yellow: #ffff55;
      --terminal-bright-blue: #5555ff;
      --terminal-bright-magenta: #ff55ff;
      --terminal-bright-cyan: #55ffff;
      --terminal-bright-white: #ffffff;
      --terminal-cursor-style: underline;
      --terminal-line-height: 1.1;
      border-radius: 0;
    }
  }

  /* the screen xterm opens in (the runtime adds it) - the fit addon measures
     its content box */
  .terminal-screen {
    flex: 1 1 0px;
    box-sizing: content-box;
    min-block-size: 0;
    min-inline-size: 0;
    padding: var(--terminal-padding);
    .terminal[data-rows] > & {
      flex: none;
    }
  }

  /* the vendor build failed to load: the reason, in place of the screen */
  .terminal-error {
    display: block;
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    color: var(--terminal-red);
  }

  /* -- Accessibility ------------------------------------------------------ */
  /* reduced motion: the runtime stops the cursor's blink and the replay's typing (the lines appear at once) */
  @media (prefers-reduced-motion: reduce) {
    .terminal-screen {
      scroll-behavior: auto;
    }
  }

  @media (prefers-contrast: more) {
    .terminal:not(.xterm) {
      --terminal-foreground: oklch(1 0 0);
      --terminal-bright-black: oklch(0.8 0 0);
      border: 1px solid var(--terminal-foreground);
    }
    .terminal:not(.xterm)[data-variant="muted"] {
      --terminal-foreground: var(--foreground);
      --terminal-bright-black: var(--foreground);
      border-color: var(--foreground);
    }
  }

  @media (forced-colors: active) {
    /* the runtime reads these: the system colours, converted like any other */
    .terminal:not(.xterm),
    .terminal:not(.xterm)[data-variant] {
      --terminal-background: Canvas;
      --terminal-foreground: CanvasText;
      --terminal-cursor: CanvasText;
      --terminal-cursor-accent: Canvas;
      --terminal-selection: Highlight;
      --terminal-black: Canvas;
      --terminal-red: CanvasText;
      --terminal-green: CanvasText;
      --terminal-yellow: CanvasText;
      --terminal-blue: CanvasText;
      --terminal-magenta: CanvasText;
      --terminal-cyan: CanvasText;
      --terminal-white: CanvasText;
      --terminal-bright-black: CanvasText;
      --terminal-bright-red: CanvasText;
      --terminal-bright-green: CanvasText;
      --terminal-bright-yellow: CanvasText;
      --terminal-bright-blue: CanvasText;
      --terminal-bright-magenta: CanvasText;
      --terminal-bright-cyan: CanvasText;
      --terminal-bright-white: CanvasText;
      border: 1px solid CanvasText;
      forced-color-adjust: none;
    }
  }
}
