/* -- Diff component ----------------------------------------------- */

@layer components {
  /* Two layers in one grid cell - .diff-item-1 (before) over .diff-item-2
     (after) - and a native <input type="range" class="diff-range"> spread
     over the whole figure as the divider: drag anywhere, arrow keys / Home /
     End, and a screen reader announces the value. diff.js mirrors the value
     into --diff-pos; item 1 is clipped at that line. Without JS the figure
     still shows a 50 / 50 split. The range is widened by one knob on both
     sides, so the knob's CENTER sits exactly on --diff-pos. */
  .diff {
    --diff-pos: 50%;
    --_knob: 2.25rem;
    position: relative;
    display: grid;
    margin: 0;
    overflow: hidden;
    border-radius: var(--radius-lg);
    isolation: isolate;
    user-select: none;
    touch-action: pan-y;
    background-color: var(--muted);
    cursor: ew-resize;

    /* the divider line */
    &::after {
      content: '';
      position: absolute;
      z-index: 2;
      top: 0;
      bottom: 0;
      left: var(--diff-pos);
      width: 2px;
      translate: -50% 0;
      background-color: var(--background);
      box-shadow: 0 0 0 1px color-mix(in oklch, var(--foreground) 12%, transparent);
      pointer-events: none;
    }
  }

  .diff-item-1,
  .diff-item-2 {
    grid-area: 1 / 1;
    position: relative;
    min-width: 0;
    min-height: 0;
    overflow: hidden;

    & > :is(img, video, picture, canvas, svg) {
      display: block;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .diff-item-1 {
    z-index: 1;
    clip-path: inset(0 calc(100% - var(--diff-pos)) 0 0);
  }
  .diff-item-2 { z-index: 0; }

  /* a corner caption on either side (Before / After) */
  .diff-label {
    position: absolute;
    top: 0.75rem;
    left: 0.75rem;
    padding: 0.125rem 0.5rem;
    border-radius: var(--radius-sm);
    background-color: color-mix(in oklch, var(--background) 82%, transparent);
    color: var(--foreground);
    font-size: 0.75rem;
    font-weight: 500;
    pointer-events: none;
  }
  .diff-item-2 .diff-label { left: auto; right: 0.75rem; }

  /* -- The range: a knob-high strip across the middle (diff.js makes the
     whole figure a drag surface and keeps keyboard / AT on the range) -- */
  .diff-range {
    position: absolute;
    z-index: 3;
    top: 50%;
    left: calc(var(--_knob) / -2);
    width: calc(100% + var(--_knob));
    height: var(--_knob);
    translate: 0 -50%;
    margin: 0;
    padding: 0;
    background: transparent;
    appearance: none;
    cursor: ew-resize;
    outline: none;

    &::-webkit-slider-runnable-track { height: 100%; background: transparent; }
    &::-moz-range-track { height: 100%; background: transparent; }
    &::-webkit-slider-thumb {
      appearance: none;
      width: var(--_knob);
      height: var(--_knob);
      border: 1px solid var(--border);
      border-radius: 999px;
      background: var(--background) center / 1.125rem no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23808080' stroke-width='2.25' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m9 6-6 6 6 6M15 6l6 6-6 6'/%3E%3C/svg%3E");
      box-shadow: var(--shadow-md);
      transition: scale 120ms ease;
    }
    &::-moz-range-thumb {
      width: var(--_knob);
      height: var(--_knob);
      border: 1px solid var(--border);
      border-radius: 999px;
      background: var(--background) center / 1.125rem no-repeat url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23808080' stroke-width='2.25' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m9 6-6 6 6 6M15 6l6 6-6 6'/%3E%3C/svg%3E");
      box-shadow: var(--shadow-md);
    }
    &:active::-webkit-slider-thumb { scale: 1.08; }
    &:focus-visible::-webkit-slider-thumb { outline: 2px solid var(--ring); outline-offset: 2px; }
    &:focus-visible::-moz-range-thumb { outline: 2px solid var(--ring); outline-offset: 2px; }
  }
  /* RTL: the range runs right → left, so item 1 is revealed from the right */
  .diff:dir(rtl):not([data-orientation="vertical"]) {
    &::after { left: calc(100% - var(--diff-pos)); }
    & .diff-item-1 { clip-path: inset(0 0 0 calc(100% - var(--diff-pos))); }
    & .diff-item-1 .diff-label { left: auto; right: 0.75rem; }
    & .diff-item-2 .diff-label { right: auto; left: 0.75rem; }
  }

  /* -- Aspect ratios (or set aspect-ratio / a height yourself) -------- */
  .diff[data-ratio="16/9"] { aspect-ratio: 16 / 9; }
  .diff[data-ratio="4/3"] { aspect-ratio: 4 / 3; }
  .diff[data-ratio="1/1"] { aspect-ratio: 1; }
  .diff[data-ratio="3/4"] { aspect-ratio: 3 / 4; }

  /* -- Knob variants ---------------------------------------------------- */
  /* line: no knob - just the divider (the whole figure is still the hit area) */
  .diff[data-variant="line"] .diff-range {
    &::-webkit-slider-thumb { opacity: 0; }
    &::-moz-range-thumb { opacity: 0; }
    &:focus-visible::-webkit-slider-thumb { opacity: 1; }
  }
  .diff[data-variant="line"]::after { width: 3px; }
  /* primary: a --primary divider + knob ring */
  .diff[data-variant="primary"] {
    &::after { background-color: var(--primary); box-shadow: none; }
    & .diff-range::-webkit-slider-thumb { border: 2px solid var(--primary); }
    & .diff-range::-moz-range-thumb { border: 2px solid var(--primary); }
  }

  /* -- Vertical: before on top, after below; the range runs top → bottom */
  .diff[data-orientation="vertical"] {
    touch-action: pan-x;
    cursor: ns-resize;

    &::after {
      top: var(--diff-pos);
      bottom: auto;
      left: 0;
      right: 0;
      width: auto;
      height: 2px;
      translate: 0 -50%;
    }
    & .diff-item-1 { clip-path: inset(0 0 calc(100% - var(--diff-pos)) 0); }
    & .diff-item-2 .diff-label { top: auto; bottom: 0.75rem; left: 0.75rem; right: auto; }
    & .diff-range {
      writing-mode: vertical-lr;
      top: calc(var(--_knob) / -2);
      left: 50%;
      width: var(--_knob);
      height: calc(100% + var(--_knob));
      translate: -50% 0;
      cursor: ns-resize;

      &::-webkit-slider-runnable-track { width: 100%; height: auto; }
      &::-webkit-slider-thumb { rotate: 90deg; }
      &::-moz-range-thumb { rotate: 90deg; }
    }
  }
  .diff[data-orientation="vertical"][data-variant="line"]::after { height: 3px; width: auto; }

  /* -- Accessibility ------------------------------------------------------ */
  @media (prefers-reduced-motion: reduce) {
    .diff-range::-webkit-slider-thumb { transition: none; }
  }
  @media (forced-colors: active) {
    .diff::after { background-color: CanvasText; }
    .diff-range::-webkit-slider-thumb { border-color: CanvasText; }
  }
}
