@import url("https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;600&display=swap");

@layer reset, tokens, base, layout, components, duck, harness, overrides;

@layer tokens {
  /* Characters typed so far, per typing run. Integers interpolate in whole steps. */
  @property --c {
    syntax: "<integer>";
    inherits: false;
    initial-value: 0;
  }

  @property --cursor {
    syntax: "<color>";
    inherits: true;
    initial-value: transparent;
  }

  /* Turns to 1 once the bob keyframes are written. */
  @property --live {
    syntax: "<integer>";
    inherits: true;
    initial-value: 1;
  }

  :root {
    color-scheme: dark;

    --color-desk: oklch(12% 0.004 100);
    --color-pane: oklch(17.5% 0.005 100);
    --color-tabs: oklch(14.5% 0.005 100);
    --color-rule: oklch(26% 0.006 100);
    --color-text: oklch(88% 0.01 100);
    --color-muted: oklch(60% 0.01 100);
    --color-gutter: oklch(42% 0.008 100);
    --color-caret: oklch(90% 0.15 95);
    --color-page: oklch(100% 0 0);

    --syn-sel: oklch(86% 0.15 95);
    --syn-prop: oklch(80% 0.07 225);
    --syn-num: oklch(79% 0.13 50);
    --syn-fn: oklch(80% 0.1 330);
    --syn-kw: oklch(80% 0.1 165);
    --syn-at: oklch(78% 0.12 300);
    --syn-hex: oklch(85% 0.08 70);
    --syn-cm: oklch(56% 0.01 100);
    --syn-pn: oklch(64% 0.01 100);

    --font-code: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    --font-ui: system-ui, sans-serif;
    --radius: 0.6rem;
  }
}

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

  body,
  h1,
  p {
    margin: 0;
  }
}

@layer base {
  body {
    background: var(--color-desk);
    color: var(--color-text);
    font: 1rem / 1.4 var(--font-ui);
  }

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

  a:focus-visible {
    outline: 2px solid var(--color-caret);
    outline-offset: 3px;
  }

  @media (prefers-reduced-motion: no-preference) {
    html {
      scroll-behavior: smooth;
    }
  }
}

@layer layout {
  .ide {
    position: fixed;
    inset: 0;
    display: grid;
    grid-template:
      "bar bar" auto
      "editor preview" minmax(0, 1fr)
      / minmax(0, 1fr) minmax(0, 1fr);
    gap: 0.75rem;
    padding: 0.75rem;
  }

  .bar {
    grid-area: bar;
  }

  .editor {
    grid-area: editor;
  }

  .preview {
    grid-area: preview;
  }

  .editor,
  .preview {
    display: grid;
    grid-template-rows: auto minmax(0, 1fr);
    border: 1px solid var(--color-rule);
    border-radius: var(--radius);
    overflow: clip;
  }

  @media (max-aspect-ratio: 1 / 1) {
    .ide {
      grid-template:
        "bar" auto
        "preview" minmax(0, 1fr)
        "editor" minmax(0, 1.2fr)
        / minmax(0, 1fr);
      gap: 0.5rem;
      padding: 0.5rem 1rem 1rem;
    }
  }
}

@layer components {
  .bar {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.25rem 1rem;
    padding-inline: 0.25rem;

    & h1 {
      font-size: 1rem;
      font-weight: 600;
      letter-spacing: 0.01em;
    }

    & small {
      margin-inline-start: 0.4rem;
      color: var(--color-muted);
      font: 400 0.8rem var(--font-code);
    }

    & .hint {
      color: var(--color-muted);
      font-size: 0.85rem;
    }

    & .again {
      display: none;
      font-size: 0.85rem;
    }
  }

  .tab {
    display: flex;
    background: var(--color-tabs);
    border-block-end: 1px solid var(--color-rule);
    font: 0.78rem / 1 var(--font-code);
    color: var(--color-muted);

    & span {
      padding: 0.6rem 0.9rem;
      background: var(--color-pane);
      border-inline-end: 1px solid var(--color-rule);
      color: var(--color-text);
      margin-block-end: -1px;
    }
  }

  .editor {
    container-type: inline-size;
    background: var(--color-pane);
  }

  .scroller {
    display: flex;
    flex-direction: column;
    overflow: auto;
    padding-block: 0.6rem;
  }

  .code {
    --lh: 1.7em;

    display: block;
    flex: none;
    min-block-size: 100%;
    font: clamp(9px, 100cqi / 32, 15px) / var(--lh) var(--font-code);
    font-variant-ligatures: none;
    counter-reset: ln;
    color: var(--color-text);
  }

  .ln {
    position: relative;
    display: block;
    block-size: var(--lh);
    padding-inline-start: 5ch;
    white-space: pre;
    overflow: clip;
    counter-increment: ln;

    &::before {
      content: counter(ln);
      position: absolute;
      inset-inline-start: 0;
      inline-size: 3ch;
      text-align: end;
      color: var(--color-gutter);
    }
  }

  .t {
    display: inline-block;
    vertical-align: top;
  }

  .t.fix {
    display: none;
  }

  .code i {
    font-style: normal;
  }

  .sel { color: var(--syn-sel); }
  .pr { color: var(--syn-prop); }
  .nu { color: var(--syn-num); }
  .fn { color: var(--syn-fn); }
  .kw { color: var(--syn-kw); }
  .at { color: var(--syn-at); }
  .cm { color: var(--syn-cm); }
  .pn { color: var(--syn-pn); }

  /* The color sits under its own hex code, like an editor's color hint. */
  .hex {
    color: var(--syn-hex);
    text-decoration: underline 0.2em var(--sw);
    text-underline-offset: 0.28em;
    text-decoration-skip-ink: none;
  }

  .stage {
    position: relative;
    container-type: size;
    background: var(--color-page);
    overflow: clip;
  }

  .no-timeline {
    display: none;
    position: fixed;
    inset-block-end: 1.25rem;
    inset-inline: 1rem;
    color: var(--color-muted);
    font-size: 0.85rem;
    text-align: center;
  }
}

/* duck.css: the code typed on screen, word for word. */
@layer duck {
  /* duck.css */

  .bath {
    background: #dcedf0;
  }

  .duck > * {
    position: absolute;
  }

  .tail {
    inset: 26% 76% 44% 1%;
    background: #f2b100;
    border-radius: 0 50% 50% 50%;
    rotate: 8deg;
  }

  .body {
    inset: 37.5% 12% 0 2%;
    background: #ffd21f;
    border-radius: 45% 50% 50% 50% /
                   55% 60% 40% 45%;
    box-shadow: inset -1.2em -1em 1.4em #e69500,
                inset 0.6em 0.8em 1em #fff3a6;
  }

  .wing {
    inset: 50% 44% 22.5% 22%;
    background: #f7b500;
    border-radius: 10% 70% 60% 50%;
    rotate: -10deg;
  }

  .head {
    inset: 2.5% 14% 47.5% 46%;
    background: #ffd21f;
    border-radius: 50%;
    box-shadow: inset -0.9em -0.7em 1em #e69500,
                inset 0.5em 0.5em 0.8em #fff3a6;
  }

  .eye {
    inset: 15% 25% 76% 68%;
    background: radial-gradient(circle at 35% 30%,
                #fff 18%, #161616 22%);
    border-radius: 50%;
  }

  .beak {
    inset: 25% 0 61% 80%;
    background: #ff8a1f;
    border-radius: 30% 80% 80% 30% / 50%;
  }

  .water {
    inset: 64% 0 0;
    background: #3a9fd0b0;
    box-shadow: inset 0 0.3em #ffffff70;
  }

  .duck {
    animation: bob 3s ease-in-out infinite;
  }

  @keyframes bob {
    50% { translate: 0 -0.3em; rotate: -2deg; }
  }
}

/* Lays out the scene that duck.css draws, and holds each line back until it's typed. */
@layer harness {
  .bath {
    position: absolute;
    inset: 0;
    font-size: min(3cqi, 3.8cqb);
  }

  .duck {
    position: absolute;
    inline-size: 18em;
    aspect-ratio: 5 / 4;
    inset-inline-start: calc(50% - 9em);
    inset-block-start: calc(64% - 11em);
    transform-origin: 50% 80%;
  }

  .water {
    position: absolute;
  }

  @supports (animation-timeline: scroll()) {
    body {
      block-size: 1400svh;
    }

    /* The newest line sits at the bottom of the pane once the file outgrows it. */
    .scroller {
      justify-content: unsafe end;
      overflow: clip;
    }

    .code {
      --cursor: var(--color-caret);

      animation: blink 1.1s step-end infinite;
    }

    .ln {
      animation: open linear both;
      animation-timeline: scroll(root);
      animation-range: var(--o) calc(var(--o) + 0.15%);
    }

    .t {
      inline-size: calc(var(--c) * 1ch);
      overflow: clip;
      border-inline-end: 0.13em solid transparent;
      margin-inline-end: -0.13em;
      animation-name: type, caret;
      animation-timing-function: linear;
      animation-fill-mode: both, none;
      animation-timeline: scroll(root);
      animation-range: var(--s) var(--e), var(--caret);
    }

    .t.wrong {
      animation-name: type, erase, caret, caret;
      animation-fill-mode: both, forwards, none, none;
      animation-range: var(--s) var(--e), var(--ds) var(--de), var(--caret);
    }

    .t.fix {
      display: inline-block;
    }

    /* Each part keeps its unstyled value until the line that styles it is finished. */
    .bath,
    .duck > *,
    .water {
      animation-name: var(--hold);
      animation-timing-function: step-end;
      animation-fill-mode: both;
      animation-timeline: scroll(root);
      animation-range: var(--at);
    }

    .preview {
      animation: go-live step-end both;
      animation-timeline: scroll(root);
      animation-range: 0% var(--live-at);
    }

    .duck {
      animation-play-state: paused;
    }

    @container style(--live: 1) {
      .duck {
        animation-play-state: running;
      }
    }

    .bar .hint {
      animation: fade linear both reverse;
      animation-timeline: scroll(root);
      animation-range: 0% 3%;
    }

    .bar .again {
      display: inline;
      animation: fade linear both;
      animation-timeline: scroll(root);
      animation-range: calc(var(--live-at) + 0.5%) calc(var(--live-at) + 3%);
    }
  }

  @keyframes open {
    from { block-size: 0; }
  }

  @keyframes type {
    from { --c: 0; }
    to { --c: var(--n); }
  }

  @keyframes erase {
    from { --c: var(--n); }
    to { --c: 0; }
  }

  @keyframes caret {
    from, to { border-inline-end-color: var(--cursor); }
  }

  @keyframes blink {
    50% { --cursor: transparent; }
  }

  @keyframes no-background {
    from { background: none; }
  }

  @keyframes no-inset {
    from { inset: auto; }
  }

  @keyframes no-border-radius {
    from { border-radius: 0; }
  }

  @keyframes no-box-shadow {
    from { box-shadow: none; }
  }

  @keyframes no-rotate {
    from { rotate: none; }
  }

  /* The typo puts the beak on the tail; while it's being fixed the value is invalid, so no beak. */
  @keyframes beak-inset {
    from { inset: auto; }
    90.996% { inset: 25% 80% 61% 0; }
    98.355% { inset: auto; }
  }

  @keyframes go-live {
    from { --live: 0; }
  }

  @keyframes fade {
    from { opacity: 0; visibility: hidden; }
  }
}

@layer overrides {
  @supports not (animation-timeline: scroll()) {
    .no-timeline {
      display: block;
    }

    .bar .hint {
      display: none;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .duck {
      animation: none;
    }

    .code {
      animation: none;
    }
  }
}
