@layer layout {
  .stage {
    --stage-max: 1100px;
    --stage-min: 300px;
  }
}

@layer base {
  header {
    .eyebrow {
      margin: 0;
      font-size: .75rem;
      font-weight: 700;
      letter-spacing: .14em;
      color: var(--color-muted);
    }

    h1 {
      font-size: clamp(2rem, 6vw, 3.5rem);
      line-height: 1;
      letter-spacing: -.03em;
    }

    code {
      padding-inline: var(--space-3xs);
      background: var(--color-surface-2);
      border-radius: var(--space-3xs);
      font-size: .85em;
    }
  }
}

@layer components {
  .drag-hint {
    margin: 0 0 var(--space-l);
    color: var(--color-muted);
    font-size: .8rem;
  }

  .pipeline {
    --state: 0;
    display: grid;
    gap: var(--space-2xl);
    transition: --state .2s ease-out;

    @media (prefers-reduced-motion: reduce) {
      transition: none;
    }

    @container (width >= 320px) { --state: 1; }
    @container (width >= 560px) { --state: 2; }
    @container (width >= 800px) { --state: 3; }
    @container (width >= 1040px) { --state: 4; }
  }

  /* every "sN" toggle in this file (state badge, capacity, flow values)
     shares this one rule: hide by default, one @container block per
     threshold reveals the span whose class matches the current range */
  :is(.state-badge, .capacity-value, .flow-value) span {
    display: none;
  }

  @container (width < 320px) { :is(.state-badge, .capacity-value, .flow-value) .s0 { display: inline; } }
  @container (320px <= width < 560px) { :is(.state-badge, .capacity-value, .flow-value) .s1 { display: inline; } }
  @container (560px <= width < 800px) { :is(.state-badge, .capacity-value, .flow-value) .s2 { display: inline; } }
  @container (800px <= width < 1040px) { :is(.state-badge, .capacity-value, .flow-value) .s3 { display: inline; } }
  @container (width >= 1040px) { :is(.state-badge, .capacity-value, .flow-value) .s4 { display: inline; } }

  .component {
    --capacity: calc(var(--state) + 1);
    padding: calc(var(--space-l) + var(--state) * .3rem);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-m);
  }

  .component-head {
    display: flex;
    justify-content: space-between;
    align-items: start;
    gap: var(--space-l);

    h2 {
      margin: var(--space-3xs) 0 0;
      font-size: 1.1rem;
    }
  }

  .kicker {
    font-size: .7rem;
    font-weight: 700;
    letter-spacing: .12em;
    color: var(--color-muted);
  }

  .state-badge {
    flex-shrink: 0;
    padding-block: var(--space-3xs);
    padding-inline: var(--space-s);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-full);
    font-size: .7rem;
    white-space: nowrap;

    span {
      color: var(--color-accent);
      font-weight: 600;
    }
  }

  .metrics {
    display: grid;
    grid-template-columns: repeat(var(--capacity), 1fr);
    gap: calc(var(--space-2xs) + var(--state) * .2rem);
    margin-block-start: var(--space-l);
  }

  .metric {
    min-inline-size: 0;
    padding: var(--space-s);
    background: var(--color-surface-2);
    border-radius: var(--radius-s);

    span,
    b {
      display: block;
    }

    span {
      color: var(--color-muted);
      font-size: .75rem;
    }

    b {
      margin-block-start: var(--space-3xs);
      font-size: .85rem;
      font-variant-numeric: tabular-nums;
    }
  }

  :is(.t1, .t2, .t3) {
    display: none;
  }

  @container (width >= 320px) { .t1 { display: block; } }
  @container (width >= 560px) { .t2 { display: block; } }
  @container (width >= 800px) { .t3 { display: block; } }

  .capacity {
    display: flex;
    justify-content: space-between;
    margin-block-start: var(--space-l);
    color: var(--color-muted);
    font-size: .8rem;

    .capacity-value span {
      color: var(--color-text);
      font-variant-numeric: tabular-nums;
    }
  }

  .flow {
    display: grid;
    /* below state 3 there isn't room for five boxes on one line; rather
       than let the row scroll (a diagram meant to be read as a whole
       shouldn't need scrolling to read half of it), the pipeline reads
       top-to-bottom instead, arrows included, until the container itself
       says there's finally enough width for it to read left-to-right */
    grid-template-columns: 1fr;
    gap: var(--space-s);

    > div {
      padding: var(--space-m);
      background: var(--color-surface);
      border: 1px solid var(--color-border);
      border-radius: var(--radius-s);

      small {
        display: block;
        color: var(--color-muted);
        font-size: .65rem;
        letter-spacing: .08em;
      }

      strong {
        display: block;
        margin-block-start: var(--space-3xs);
        font-variant-numeric: tabular-nums;
        white-space: nowrap;
      }
    }

    > span {
      justify-self: center;
      color: var(--color-muted);
      rotate: 90deg;
    }

    @container (width >= 800px) {
      grid-template-columns: repeat(4, minmax(6rem, 1fr) auto) minmax(6rem, 1fr);
      align-items: center;

      > span {
        rotate: 0deg;
      }
    }
  }
}

@layer components {
  .code-panels,
  .see-more {
    max-inline-size: var(--measure-xl);
    margin-block-start: var(--space-4xl);
    margin-inline: auto;
    padding-inline: var(--space-l);
  }

  .code-panels {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
    gap: var(--space-l);
  }

  .panel {
    padding: var(--space-l);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-m);

    h2 {
      margin: 0;
      font-size: 1rem;
    }

    p {
      color: var(--color-muted);
      font-size: .85rem;
    }

    pre {
      margin: 0;
      overflow: auto;
      font: .78rem/1.6 ui-monospace, Consolas, monospace;
      color: var(--color-text);
    }

    ul {
      margin: 0;
      padding-inline-start: 1.1em;
      color: var(--color-muted);
      font-size: .9rem;

      li {
        margin-block: var(--space-2xs);
      }
    }
  }

  .see-more h2 {
    font-size: 1.1rem;
  }

  .link-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
    gap: var(--space-l);
    margin-block-start: var(--space-l);
  }

  .link-grid a {
    display: grid;
    gap: var(--space-2xs);
    padding: var(--space-l);
    background: var(--color-surface);
    color: var(--color-text);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-m);
    text-decoration: none;
    transition: border-color .15s ease;

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

    span {
      color: var(--color-muted);
      font-size: .85rem;
    }
  }
}
