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

@layer tokens {
  @property --cut { syntax: "<number>"; inherits: true; initial-value: 0; }
  @property --angle { syntax: "<angle>"; inherits: true; initial-value: 0deg; }
  @property --gone { syntax: "<number>"; inherits: true; initial-value: 0; }

  :root {
    color-scheme: light dark;

    --sand-50: oklch(96.5% 0.008 95);
    --sand-950: oklch(18% 0.012 95);
    --ink-900: oklch(24% 0.02 95);
    --ink-100: oklch(95% 0.01 95);

    --color-bg: light-dark(var(--sand-50), var(--sand-950));
    --color-surface: light-dark(oklch(99.5% 0.003 95), oklch(23% 0.014 95));
    --color-text: light-dark(var(--ink-900), var(--ink-100));
    --color-muted: light-dark(oklch(45% 0.02 95), oklch(76% 0.02 95));
    --color-border: light-dark(oklch(88% 0.012 95), oklch(34% 0.016 95));
    --color-accent: light-dark(oklch(50% 0.12 70), oklch(86% 0.15 100));
    --color-accent-soft: color-mix(in oklch, var(--color-accent) 16%, transparent);
    --color-focus: light-dark(oklch(52% 0.2 260), oklch(76% 0.14 260));

    --color-paper: oklch(98.5% 0 0);
    --color-print: var(--ink-900);
    --color-sticker: oklch(92.6% 0.18 106);
    --color-sticker-deep: oklch(85% 0.17 100);
    --color-sticker-back: oklch(98% 0.03 100);
    --color-sticker-back-fold: color-mix(in oklch, var(--color-sticker), white 35%);
    --color-shade: oklch(20% 0.02 95 / 0.3);

    --space-3xs: 0.25rem;
    --space-2xs: 0.4rem;
    --space-l: 1rem;
    --space-xl: 1.25rem;
    --space-3xl: 2rem;
    --space-4xl: 3rem;
    --space-6xl: 8rem;

    --radius-s: 0.5rem;
    --radius-m: 0.75rem;
    --shadow-raised: 0 1px 2px oklch(30% 0.02 50 / 0.06), 0 12px 32px oklch(30% 0.02 50 / 0.1);
    --text-xs: 0.75rem;
    --text-s: 0.8125rem;
    --text-card: 1.05rem;
    --text-l: clamp(1.75rem, 1.1rem + 2.4vw, 3rem);
    --leading: 1.55;
    --leading-snug: 1.45;
    --measure-l: 56.25rem;
    --measure-xl: 68.75rem;
    --focus-ring: 0.1875rem solid var(--color-focus);
    --focus-offset: 0.375rem;

    --sticker-size: clamp(10rem, 50vmin, 18rem);
    --sticker-print: calc(var(--sticker-size) * 0.13);
    --sticker-font: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", system-ui, sans-serif;
    --sticker-face: radial-gradient(circle at 30% 25%, color-mix(in oklch, var(--color-sticker), white 30%), var(--color-sticker) 55%, var(--color-sticker-deep));
    --sticker-rest-cut: 0.07;
    --sticker-rest-angle: 45deg;
    --shadow-paper: 0 0.0625rem 0.1875rem var(--color-shade);
    --shadow-flap: drop-shadow(0 0 calc(var(--sticker-size) * 0.03) var(--color-shade));

    --ease-peel: cubic-bezier(0.4, 0, 0.2, 1);
    --ease-spring: linear(0, 0.45 10%, 1.12 24%, 1.2 32%, 1.04 50%, 0.97 64%, 1.01 80%, 1);
    --peel-duration: 750ms;
  }

  @media (prefers-reduced-motion: reduce) {
    :root {
      --peel-duration: 1ms;
    }
  }
}

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

  body {
    margin: 0;
    font: 16px system-ui, sans-serif;
  }

  button {
    font: inherit;
    color: inherit;
  }
}

@layer base {
  html,
  body {
    min-block-size: 100%;
    background: var(--color-bg);
    color: var(--color-text);
  }

  header {
    max-inline-size: var(--measure-l);
    margin-block: var(--space-4xl) 0;
    margin-inline: auto;
    padding-inline: var(--space-l);

    h1 {
      margin-block-end: var(--space-2xs);
      font-size: var(--text-l);
      letter-spacing: -0.02em;
    }

    p {
      max-inline-size: 70ch;
      margin-block-start: 0;
      color: var(--color-muted);
      line-height: var(--leading);

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

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

  p {
    text-wrap: pretty;
  }

  code {
    padding-inline: var(--space-3xs);
    border-radius: var(--radius-s);
    background: var(--color-accent-soft);
    font-size: 0.9em;
  }

  :where(a):focus-visible {
    outline: var(--focus-ring);
    outline-offset: 2px;
  }
}

@layer layout {
  .stage {
    display: grid;
    place-items: center;
    padding: var(--space-4xl) var(--space-l) var(--space-6xl);
    overflow-inline: clip;
  }
}

@layer components {
  .index-grid {
    --card-min: 17rem;

    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, var(--card-min)), 1fr));
    gap: var(--space-l);
    max-inline-size: var(--measure-xl);
    margin-inline: auto;
    margin-block: var(--space-3xl) var(--space-4xl);
    padding-inline: var(--space-l);
  }

  .demo-card-link {
    display: grid;
    align-content: start;
    gap: var(--space-2xs);
    padding: var(--space-xl);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-m);
    background: var(--color-surface);
    color: var(--color-text);
    text-decoration: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;

    strong {
      font-size: var(--text-card);
    }

    &:focus-visible {
      border-color: var(--color-accent);
    }
  }

  @media (hover: hover) {
    .demo-card-link:hover {
      border-color: var(--color-accent);
      box-shadow: var(--shadow-raised);
    }
  }

  .demo-num {
    color: var(--color-muted);
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
  }

  .demo-desc {
    color: var(--color-muted);
    font-size: var(--text-s);
    line-height: var(--leading-snug);
  }

  .sticker {
    --cut: var(--sticker-rest-cut);
    --angle: var(--sticker-rest-angle);

    position: relative;
    inline-size: var(--sticker-size);
    aspect-ratio: 1;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: none;
    color: var(--color-print);
    font-family: var(--sticker-font);
    font-weight: 700;
    cursor: pointer;
    transition:
      --cut var(--peel-duration) var(--ease-peel),
      --angle var(--peel-duration) var(--ease-peel),
      --gone var(--peel-duration) var(--ease-peel);

    &:focus-visible {
      outline: var(--focus-ring);
      outline-offset: var(--focus-offset);
    }

    .backing {
      position: absolute;
      inset: 0;
      display: grid;
      place-items: center;
      border-radius: 50%;
      background: var(--color-paper);
      box-shadow: var(--shadow-paper);
    }

    .message, .print {
      font-size: var(--sticker-print);
    }

    .peel {
      position: absolute;
      inset: 0;
      rotate: var(--angle);
      translate: calc(var(--fly-x, 0px) * var(--gone)) calc(var(--fly-y, 0px) * var(--gone));
      opacity: calc(1 - var(--gone));
    }

    .stuck {
      position: absolute;
      inset: 0;
      display: grid;
      place-items: center;
      border-radius: 50%;
      background: var(--sticker-face);
      clip-path: inset(calc(var(--cut) * 100%) 0 0);
    }

    .print {
      rotate: calc(var(--angle) * -1);
    }

    .flap {
      position: absolute;
      inset: 0;
      filter: var(--shadow-flap);
      transform-origin: 50% calc(var(--cut) * 100%);
      scale: 1 -1;

      &::before {
        content: "";
        position: absolute;
        inset: 0;
        border-radius: 50%;
        background: linear-gradient(to bottom, var(--color-sticker-back), var(--color-sticker-back-fold) calc(var(--cut) * 100%));
        clip-path: inset(0 0 calc((1 - var(--cut)) * 100%));
      }
    }
  }
}
