@import url(https://fonts.bunny.net/css?family=jura:300,500);
@layer base, controls, demo;

@layer demo {
  body {
    min-height: 100vh;
    min-height: 100dvh;
    min-height: 100svh;
    overflow: hidden;
    font-family: "Jura", sans-serif;
  }

  @property --x {
    syntax: "<percentage>";
    inherits: true;
    initial-value: 0%;
  }

  .optical {
    position: fixed;
    inset: 0;
    margin: auto;
    width: min(90vw, 400px);
    aspect-ratio: 1;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    contain: layout paint;
    backface-visibility: hidden;
    background-image: url("https://raw.githubusercontent.com/cbolson/assets/refs/heads/main/codepen/optical/man-running.png");
    --stripe-px: 6px;
    --ground-y: -1px;
    --x: 0%;
    transform: translateZ(0);
  }

  body[data-scene="panther"] .optical {
    --stripe-px: 7.2px;
    --ground-y: 30px;
    aspect-ratio: 6/4;
    background-image: url("https://raw.githubusercontent.com/cbolson/assets/refs/heads/main/codepen/optical/cat-running.png");
  }

  body[data-scene="bmx"] .optical {
    --stripe-px: 7px;
    --ground-y: 10px;
    aspect-ratio: 6/4;
    background-image: url("https://raw.githubusercontent.com/cbolson/assets/refs/heads/main/codepen/optical/bmx.png");
  }

  .optical::before {
    content: "";
    position: absolute;
    inset: auto -50px var(--ground-y, 0);
    height: 1px;
    background: radial-gradient(rgb(53 83 14), #0000);
    z-index: 2;
  }

  .optical::after {
    content: "";
    position: absolute;
    inset: 0;
    background-image: linear-gradient(90deg, var(--clr-bg) 0 80%, transparent 0 100%);
    background-size: var(--stripe-px) 100%;
    background-repeat: repeat;
    background-position-x: var(--x);
    transition: background-position-x 80ms linear;
    will-change: background-position;
  }
}

@layer controls {
  .scene-toggle {
    position: fixed;
    top: 50%;
    left: max(0.75rem, env(safe-area-inset-left));
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    border: 0;
    border-radius: 12px;
    margin: 0;
    padding: 0.2rem;
    background-color: transparent;
    box-shadow: none;
  }

  .scene-option {
    display: flex;
    align-items: center;
    color: #8b9097;
    font-size: 0.84rem;
    line-height: 1;
    cursor: pointer;
    border-radius: 999px;
    padding: 0.33rem 0.52rem;
    transition: color 150ms ease, background-color 150ms ease;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
  }

  .scene-option input[type="radio"] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }

  .scene-option span {
    letter-spacing: 0.02em;
    font-weight: 500;
  }

  .scene-option input[type="radio"]:checked + span {
    color: #000;
    font-weight: 600;
  }

  .scene-option:has(input[type="radio"]:checked) {
    background: rgba(0, 0, 0, 0.05);
  }

  .controls {
    position: fixed;
    left: 50%;
    bottom: max(1rem, env(safe-area-inset-bottom));
    transform: translateX(-50%);
    display: flex;
    width: min(92vw, 420px);
    align-items: center;
    justify-content: center;
    border: 0;
    padding: 0.28rem 0.6rem;
    background: transparent;
    border-radius: 999px;
    box-shadow: none;
  }

  #scrub {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 16px;
    touch-action: pan-x;
    accent-color: #0f172a;
    background: transparent;
    -webkit-user-select: none;
    user-select: none;
  }

  #scrub:focus-visible,
  .scene-option input:focus-visible + span {
    outline: 2px solid rgba(0, 0, 0, 0.35);
    outline-offset: 3px;
    border-radius: 4px;
  }

  #scrub::-webkit-slider-runnable-track {
    height: 2px;
    border-radius: 999px;
    background: rgba(107, 114, 128, 0.45);
    border: 0;
  }

  #scrub::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: #111;
    border: 0;
    margin-top: -4.5px;
    box-shadow: 0 0 0 1px rgba(17, 17, 17, 0.08);
  }

  #scrub::-moz-range-track {
    height: 2px;
    border: 0;
    border-radius: 999px;
    background: rgba(107, 114, 128, 0.45);
  }

  #scrub::-moz-range-thumb {
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: #111;
    border: 0;
    box-shadow: 0 0 0 1px rgba(17, 17, 17, 0.08);
  }

  #scrub.is-dragging {
    cursor: grabbing;
  }
}

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

  :root {
    --clr-bg: rgb(248, 244, 238);
  }

  body {
    margin: 0;
    padding: 2rem;
    background-color: var(--clr-bg);
    display: grid;
    place-items: center;
    color: #000;
    line-height: 1.5;
  }
}

@media (max-width: 1100px), (hover: none) and (pointer: coarse) {
  .scene-toggle {
    top: auto;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom) + 3.65rem);
    transform: translateX(-50%);
    flex-direction: row;
    gap: 0.1rem;
    padding: 0.16rem 0.2rem;
    border-radius: 999px;
    max-width: min(92vw, 360px);
    overflow-x: auto;
    scrollbar-width: none;
    z-index: 20;
  }

  .scene-toggle::-webkit-scrollbar {
    height: 2px;
    width: 2px;
  }

  .scene-toggle::-webkit-scrollbar-track {
    background: transparent;
    border: 0;
  }

  .scene-toggle::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.25);
    border-radius: 999px;
    border: 0;
  }

  .scene-option {
    font-size: 0.68rem;
    padding: 0.28rem 0.4rem;
    white-space: nowrap;
  }

  .controls {
    left: 50%;
    bottom: max(0.95rem, env(safe-area-inset-bottom));
    top: auto;
    transform: translateX(-50%);
    width: min(58vw, 250px);
    height: auto;
    padding: 0.24rem 0.42rem;
    border-radius: 14px;
    z-index: 21;
    touch-action: none;
  }

  #scrub {
    width: 100%;
    height: 20px;
    writing-mode: horizontal-tb;
    direction: ltr;
    -webkit-appearance: none;
    appearance: none;
    touch-action: none;
  }

  #scrub::-webkit-slider-runnable-track {
    width: 100%;
    height: 2px;
  }

  #scrub::-webkit-slider-thumb {
    width: 26px;
    height: 26px;
    border-radius: 14px;
    margin-top: -12px;
    margin-left: 0;
    box-shadow: 0 0 0 1px rgba(17, 17, 17, 0.15), 0 2px 10px rgba(0, 0, 0, 0.2);
  }

  #scrub::-moz-range-track {
    width: 100%;
    height: 2px;
  }

  #scrub::-moz-range-thumb {
    width: 26px;
    height: 26px;
    border-radius: 14px;
    border: 0;
    box-shadow: 0 0 0 1px rgba(17, 17, 17, 0.15), 0 2px 10px rgba(0, 0, 0, 0.2);
  }

  .optical {
    width: min(82vw, 340px);
    max-height: 72vh;
    background-size: contain;
    background-position: center center;
    image-rendering: auto;
  }

  #scrub::-moz-range-thumb {
    width: 28px;
    height: 28px;
    border-radius: 16px;
    border: 0;
    box-shadow: 0 0 0 1px rgba(17, 17, 17, 0.15), 0 2px 10px rgba(0, 0, 0, 0.2);
  }

  .optical {
    width: min(88vw, 380px);
    max-height: 76vh;
    background-size: contain;
    background-position: center center;
    image-rendering: -webkit-optimize-contrast;
  }
}
