@layer reset, tokens, base, layout, components, utilities, overrides;

@layer tokens {
  :root {
    color-scheme: dark;
    --color-bg: oklch(16% 0.01 250);
    --color-surface: oklch(22% 0.01 250);
    --color-text: oklch(95% 0.005 250);
    --color-muted: oklch(72% 0.01 250);
    --color-border: oklch(34% 0.01 250);
    --color-accent: oklch(80% 0.12 85);
    --space-xs: .35rem;
    --space-s: .6rem;
    --space-m: 1rem;
    --space-l: 1.5rem;
    --space-xl: 2.5rem;
    --radius-s: .4rem;
    --radius-m: .75rem;
    --leading-tight: 1.25;
    --leading-body: 1.5;
    --text-s: .85rem;
    --text-m: 1rem;
    --size-target-min: 24px;
  }

  @function --when-supported(--value) {
    result: var(--value);
  }
}

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

  body {
    margin: 0;
  }

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

@layer base {
  html,
  body {
    background: var(--color-bg);
    color: var(--color-text);
    font: var(--text-m) / var(--leading-body) system-ui, sans-serif;
  }

  header {
    max-inline-size: 900px;
    margin-block: var(--space-xl) 0;
    margin-inline: auto;
    padding-inline: var(--space-m);

    & h1 {
      margin-block-end: var(--space-xs);
      line-height: var(--leading-tight);
    }

    & p {
      max-inline-size: 70ch;
      margin-block-start: 0;
      color: var(--color-muted);
    }

    & a {
      color: var(--color-accent);
    }
  }

  :is(h1, h2, h3) {
    text-wrap: balance;
  }

  p {
    text-wrap: pretty;
  }

  :where(a, button, input, [tabindex]):focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }
}

@layer components {
  .support-banner {
    display: block;
    display: --when-supported(none);
    max-inline-size: 900px;
    margin-block: var(--space-m);
    margin-inline: auto;
    padding: var(--space-s) var(--space-m);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-s);
    background: var(--color-surface);
    color: var(--color-muted);
    font-size: var(--text-s);

    & strong {
      color: var(--color-text);
    }
  }

  .chips {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-xs);
    margin: 0;
    padding: 0;
    border: 0;

    & legend {
      inline-size: 100%;
      margin-block-end: var(--space-xs);
      color: var(--color-muted);
      font-size: var(--text-s);
    }

    & label {
      display: inline-flex;
      align-items: center;
      gap: var(--space-xs);
      min-block-size: var(--size-target-min);
      padding: var(--space-xs) var(--space-s);
      border: 1px solid var(--color-border);
      border-radius: 999px;
      background: var(--color-surface);
      font-size: var(--text-s);
      cursor: pointer;

      &:has(:checked) {
        border-color: var(--color-accent);
        color: var(--color-accent);
      }
    }

    & input {
      accent-color: var(--color-accent);
      margin: 0;
    }
  }
}

@layer tokens {
  :root {
    --mark-ring: oklch(98% 0 0);
    --mark-core: oklch(60% 0.22 25);
  }

  @property --mode { syntax: "<integer>"; inherits: true; initial-value: 0; }

  @function --cover-scale(--w <number>, --h <number>) returns <length> {
    result: max(100cqi / var(--w), 100cqb / var(--h));
  }

  @function --focus-shift(--focus <number>, --box <length>, --size <number>, --scale <length>) returns <length> {
    --rendered: calc(var(--size) * var(--scale));
    result: calc(-1 * clamp(0px, var(--focus) / 100 * var(--rendered) - var(--box) / 2, var(--rendered) - var(--box)));
  }

  @function --percent-shift(--focus <number>, --box <length>, --size <number>, --scale <length>) returns <length> {
    result: calc((var(--box) - var(--size) * var(--scale)) * var(--focus) / 100);
  }
}

@layer layout {
  .crops {
    display: grid;
    gap: var(--space-l);
    max-inline-size: 1000px;
    margin-block: var(--space-l) var(--space-xl);
    margin-inline: auto;
    padding-inline: var(--space-m);

    &:has([value="percent"]:checked) { --mode: 1; }
    &:has([value="center"]:checked) { --mode: 2; }
  }

  .controls {
    display: grid;
    gap: var(--space-m);
  }

  .gallery {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 13rem), 1fr));
    align-items: start;
    gap: var(--space-l);
  }

  .banner {
    grid-column: 1 / -1;
  }
}

@layer components {
  figure {
    display: grid;
    gap: var(--space-xs);
    margin: 0;
  }

  figcaption {
    color: var(--color-muted);
    font-size: var(--text-s);
  }

  .crop {
    --w: attr(data-w type(<number>), 1);
    --h: attr(data-h type(<number>), 1);
    --fx: attr(data-focus-x type(<number>), 50);
    --fy: attr(data-focus-y type(<number>), 50);
    position: relative;
    container-type: size;
    aspect-ratio: 4 / 3;
    border-radius: var(--radius-s);
    overflow: clip;

    .crops:has([value="boat"]:checked) & {
      --fx: 16;
      --fy: 61.5;
    }

    .banner & { aspect-ratio: 4 / 1; }
    .square & { aspect-ratio: 1; }
    .phone & { aspect-ratio: 9 / 16; }

    & > * {
      --s: --cover-scale(var(--w), var(--h));
      --sx: if(
        style(--mode: 0): --focus-shift(var(--fx), 100cqi, var(--w), var(--s));
        style(--mode: 1): --percent-shift(var(--fx), 100cqi, var(--w), var(--s));
        else: --percent-shift(50, 100cqi, var(--w), var(--s))
      );
      --sy: if(
        style(--mode: 0): --focus-shift(var(--fy), 100cqb, var(--h), var(--s));
        style(--mode: 1): --percent-shift(var(--fy), 100cqb, var(--h), var(--s));
        else: --percent-shift(50, 100cqb, var(--h), var(--s))
      );
    }

    & img {
      position: absolute;
      inset: 0;
      inline-size: 100%;
      block-size: 100%;
      object-fit: cover;
      object-position: var(--sx) var(--sy);
    }

    & .mark {
      position: absolute;
      inset-block-start: calc(var(--fy) / 100 * var(--h) * var(--s) + var(--sy));
      inset-inline-start: calc(var(--fx) / 100 * var(--w) * var(--s) + var(--sx));
      inline-size: 14px;
      aspect-ratio: 1;
      border: 3px solid var(--mark-ring);
      border-radius: 50%;
      background: var(--mark-core);
      box-shadow: 0 0 0 1px oklch(0% 0 0 / 0.5);
      translate: -50% -50%;
    }
  }
}
