@layer components {
  /* The trace explorer: filters, a summary, a chart, and the matching rows. */
  /* Rows keep their content's height; the page scrolls instead of squeezing them. */
  .traces,
  .trace {
    grid-auto-rows: max-content;
  }

  .traces {
    container-type: inline-size;
    gap: 1rem;
  }

  .trace-filters {
    display: grid;
    gap: 0.75rem;

    /* Wider than the shell page's form default. */
    .shell-page & {
      max-inline-size: none;
    }

    .field {
      min-inline-size: 0;
    }
  }

  .trace-filters__grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.75rem;
  }

  .trace-filters__actions {
    display: flex;
    justify-content: flex-end;
  }

  @container (width >= 40rem) {
    .trace-filters__grid {
      grid-template-columns: repeat(4, minmax(0, 1fr));
    }
  }

  @container (width >= 64rem) {
    .trace-filters__grid {
      grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr));
    }
  }

  .trace-views {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .trace-view {
    display: inline-flex;
    align-items: center;
    min-block-size: 2.25rem;
    padding-inline: 0.875rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--foreground);
    font-size: var(--text-sm);
    font-weight: var(--font-weight-medium);
    text-decoration: none;
    transition: background-color 150ms ease-out, border-color 150ms ease-out, transform 100ms ease-out;

    &:hover {
      background: var(--accent);
    }

    &:active {
      transform: scale(0.97);
    }

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

    &[aria-current="true"] {
      border-color: var(--primary);
      background: var(--primary);
      color: var(--primary-foreground);
    }
  }

  .trace-view--clear {
    border-color: transparent;
    color: var(--muted-foreground);
  }

  .trace-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1px;
    margin: 0;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--border);
  }

  @container (width >= 40rem) {
    .trace-stats {
      grid-template-columns: repeat(auto-fit, minmax(8rem, 1fr));
    }
  }

  .trace-stat {
    display: grid;
    gap: 0.25rem;
    padding: 0.75rem 1rem;
    background: var(--card);

    dt {
      color: var(--muted-foreground);
      font-size: var(--text-xs);
      line-height: var(--text-xs--line-height);
    }

    dd {
      display: flex;
      align-items: baseline;
      gap: 0.375rem;
      margin: 0;
      font-size: var(--text-xl);
      font-weight: var(--font-weight-semibold);
      font-variant-numeric: tabular-nums;
      line-height: var(--text-xl--line-height);
    }
  }

  .trace-stat--alert dd {
    color: var(--destructive);
  }

  .trace-stat__note {
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-normal);
  }

  .trace-chart {
    display: grid;
    gap: 0.5rem;
    margin: 0;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
  }

  .trace-chart__caption {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
  }

  .trace-chart__legend {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
  }

  .trace-chart__key {
    inline-size: 0.5rem;
    block-size: 0.5rem;
    border-radius: 2px;
  }

  .trace-chart__key--error {
    background: var(--destructive);
  }

  .trace-chart__bars {
    display: flex;
    align-items: flex-end;
    gap: 2px;
    block-size: 4.5rem;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  /* --total is the bar's share of the busiest bucket; --errors is its failed share. */
  .trace-chart__bar {
    flex: 1;
    min-inline-size: 1px;
    block-size: max(calc(var(--total) * 100%), 1px);
    border-radius: 2px 2px 0 0;
    background: linear-gradient(
      to top,
      var(--destructive) calc(var(--errors) * 100%),
      color-mix(in oklch, var(--foreground) 22%, transparent) calc(var(--errors) * 100%)
    );
    transform-origin: bottom;
    transition: background-color 150ms ease-out;

    &:hover {
      opacity: 0.75;
    }
  }

  @media (prefers-reduced-motion: no-preference) {
    .trace-chart__bar {
      animation: trace-bar-in 240ms ease-out both;
    }
  }

  @keyframes trace-bar-in {
    from { transform: scaleY(0.6); opacity: 0; }
  }

  .trace-chart__axis {
    display: flex;
    justify-content: space-between;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
  }

  .trace-chart__empty {
    margin: 0;
    padding-block: 1.5rem;
    text-align: center;
  }

  /* Results. Narrow screens stack each row; wide ones line up columns. */
  .trace-table {
    display: grid;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
  }

  .trace-table__head {
    display: none;
  }

  .trace-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    padding: 0.75rem 1rem;
    border-block-start: 1px solid var(--border);
    color: inherit;
    font-size: var(--text-sm);
    line-height: var(--text-sm--line-height);
    text-decoration: none;
    transition: background-color 150ms ease-out;

    &:first-of-type {
      border-block-start: 0;
    }

    &:is(a):hover {
      background: var(--accent);
    }

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

  .trace-row__primary {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    column-gap: 0.5rem;
    flex-basis: 100%;
    min-inline-size: 0;
    align-items: baseline;
  }

  .trace-row__dot {
    grid-row: 1;
    inline-size: 0.5rem;
    block-size: 0.5rem;
    border-radius: 999px;
    background: light-dark(oklch(0.65 0.15 150), oklch(0.72 0.15 150));

    .trace-row--error & { background: var(--destructive); }
    .trace-row--running & { background: var(--primary); }
    .trace-row--interrupted &,
    .trace-row--handed_off & { background: var(--muted-foreground); }
  }

  .trace-row__title {
    overflow: hidden;
    font-weight: var(--font-weight-medium);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .trace-row__id {
    color: var(--muted-foreground);
    font-weight: var(--font-weight-normal);
  }

  .trace-row__meta,
  .trace-row__error {
    grid-column: 2;
    overflow: hidden;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .trace-row__error {
    color: var(--destructive);
  }

  .trace-row__cell {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    min-inline-size: 0;
  }

  .trace-row__label {
    color: var(--muted-foreground);
    font-size: var(--text-xs);
  }

  .trace-row__num {
    font-variant-numeric: tabular-nums;
  }

  .trace-row__code {
    flex-basis: 100%;

    code {
      overflow: hidden;
      color: var(--muted-foreground);
      font-family: var(--font-mono);
      font-size: var(--text-xs);
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  @container (width >= 48rem) {
    .trace-table {
      --columns: minmax(0, 1fr);
      grid-template-columns: var(--columns);
    }

    .trace-table--runs { --columns: minmax(0, 2fr) minmax(7rem, auto) repeat(5, minmax(4.5rem, auto)); }
    .trace-table--requests { --columns: minmax(0, 2.5fr) minmax(7rem, auto) repeat(4, minmax(5rem, auto)); }
    .trace-table--tools,
    .trace-table--apps { --columns: minmax(0, 1.5fr) minmax(7rem, auto) minmax(5rem, auto) minmax(0, 2fr); }
    .trace-table--calls { --columns: minmax(0, 2fr) minmax(7rem, auto) repeat(3, minmax(5rem, auto)); }
    .trace-table--runs.trace-table--uncosted { --columns: minmax(0, 2fr) minmax(7rem, auto) repeat(4, minmax(4.5rem, auto)); }
    .trace-table--requests.trace-table--uncosted { --columns: minmax(0, 2.5fr) minmax(7rem, auto) repeat(3, minmax(5rem, auto)); }
    .trace-table--calls.trace-table--uncosted { --columns: minmax(0, 2fr) minmax(7rem, auto) repeat(2, minmax(5rem, auto)); }

    .trace-table__head,
    .trace-row {
      display: grid;
      grid-column: 1 / -1;
      grid-template-columns: subgrid;
      align-items: center;
      column-gap: 1rem;
    }

    .trace-table__head {
      padding: 0.5rem 1rem;
      border-block-end: 1px solid var(--border);
      background: var(--muted);
      color: var(--muted-foreground);
      font-size: var(--text-xs);
      font-weight: var(--font-weight-medium);
      line-height: var(--text-xs--line-height);

      span:not(:first-child) { text-align: end; }
    }

    :is(.trace-table--tools, .trace-table--apps) .trace-table__head span:last-child { text-align: start; }

    .trace-row__primary,
    .trace-row__code {
      flex-basis: auto;
    }

    .trace-row__cell {
      justify-content: flex-end;
    }

    .trace-row__code {
      justify-content: flex-start;
    }

    .trace-row__label {
      position: absolute;
      inline-size: 1px;
      block-size: 1px;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
    }
  }

  .trace-pages {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    font-size: var(--text-sm);
  }

  /* A run's span tree, drawn as a waterfall on the run's timeline. */
  .trace {
    container-type: inline-size;
  }

  .trace__summary {
    display: grid;
    gap: 0.75rem;
  }

  .trace__badges {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
  }

  .span-tree {
    display: grid;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
  }

  .span-tree__head {
    display: grid;
    gap: 0.25rem;
    padding: 0.5rem 1rem;
    border-block-end: 1px solid var(--border);
    background: var(--muted);
  }

  .span-tree__heading {
    margin: 0;
    font-size: var(--text-xs);
    font-weight: var(--font-weight-medium);
    line-height: var(--text-xs--line-height);
    color: var(--muted-foreground);
  }

  /* Narrow rows indent their bars, so the axis only lines up on wide screens. */
  .span-tree__axis {
    display: none;
    justify-content: space-between;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
    line-height: var(--text-xs--line-height);
  }

  .span-tree__list {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .span-row + .span-row {
    border-block-start: 1px solid var(--border);
  }

  .span-row__link {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "name metrics"
      "track track";
    gap: 0.375rem 0.75rem;
    min-block-size: 2.75rem;
    padding-block: 0.5rem;
    padding-inline: calc(1rem + var(--depth) * 1rem) 1rem;
    color: inherit;
    font-size: var(--text-sm);
    line-height: var(--text-sm--line-height);
    text-decoration: none;
    transition: background-color 150ms ease-out;

    &:hover {
      background: var(--accent);
    }

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

    &[aria-current] {
      background: var(--secondary);
      box-shadow: inset 2px 0 0 var(--primary);
    }
  }

  .span-row__name {
    grid-area: name;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-inline-size: 0;
  }

  .span-row__kind {
    flex: none;
    min-inline-size: 3rem;
    padding: 0 0.375rem;
    border-radius: var(--radius-sm);
    background: var(--muted);
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-medium);
    line-height: 1.25rem;
    text-align: center;

    .span-row--request & {
      background: color-mix(in oklch, var(--foreground) 12%, transparent);
      color: var(--foreground);
    }
  }

  .span-row__title {
    overflow: hidden;
    font-weight: var(--font-weight-medium);
    text-overflow: ellipsis;
    white-space: nowrap;

    .span-row--run &, .span-row--call & {
      font-weight: var(--font-weight-semibold);
    }
  }

  /* What a call step reported, such as its SIP response. */
  .span-row__detail {
    flex: 1 1 0;
    min-inline-size: 0;
    overflow: hidden;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .span-row__status {
    flex: none;
    color: var(--muted-foreground);
    font-size: var(--text-xs);

    .span-row--error & {
      color: var(--destructive);
    }
  }

  .span-row__metrics {
    grid-area: metrics;
    display: flex;
    gap: 0.75rem;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
    align-self: center;
  }

  .span-row__track {
    grid-area: track;
    position: relative;
    block-size: 0.5rem;
    border-radius: 999px;
    background: color-mix(in oklch, var(--foreground) 6%, transparent);
  }

  /* --offset and --width place the span on the run's timeline. */
  .span-row__bar {
    position: absolute;
    inset-block: 0;
    inset-inline-start: var(--offset);
    inline-size: max(var(--width), 3px);
    border-radius: 999px;
    background: color-mix(in oklch, var(--foreground) 55%, transparent);

    .span-row--run &, .span-row--call & { background: color-mix(in oklch, var(--foreground) 25%, transparent); }
    .span-row--tool & { background: light-dark(oklch(0.6 0.13 250), oklch(0.7 0.13 250)); }
    .span-row--delivery & { background: light-dark(oklch(0.6 0.13 150), oklch(0.7 0.13 150)); }
    /* A call step happens at an instant, so it is a dot on the timeline. */
    .span-row--call_event & {
      inline-size: 0.5rem;
      translate: -50% 0;
      background: light-dark(oklch(0.65 0.14 60), oklch(0.75 0.14 60));
    }
    .span-row--error & { background: var(--destructive); }
    .span-row--running & {
      background: repeating-linear-gradient(-45deg, var(--primary) 0 4px, color-mix(in oklch, var(--primary) 60%, transparent) 4px 8px);
    }
  }

  /* Wide screens put the bar beside the name, so the waterfall lines up. */
  @container (width >= 44rem) {
    .span-tree__head {
      grid-template-columns: minmax(0, 2fr) 8.5rem minmax(0, 3fr);
      align-items: center;
      column-gap: 0.75rem;
    }

    .span-tree__axis {
      display: flex;
      grid-column: 3;
    }

    .span-row__link {
      grid-template-columns: minmax(0, 2fr) 8.5rem minmax(0, 3fr);
      grid-template-areas: "name metrics track";
      align-items: center;
      padding-inline: 1rem;
    }

    .span-row__name {
      padding-inline-start: calc(var(--depth) * 1.25rem);
    }

    .span-row__metrics {
      justify-content: flex-end;
    }
  }

  /* The selected span. */
  .trace-span-frame {
    display: contents;
  }

  .trace-span-frame[busy] .trace-span {
    opacity: 0.6;
    transition: opacity 150ms ease-out 150ms;
  }

  .trace-span__kind {
    margin: 0;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
    font-weight: var(--font-weight-medium);
    line-height: var(--text-xs--line-height);
    text-transform: uppercase;
    letter-spacing: var(--tracking-wide);
  }

  .trace-span__title {
    margin: 0;
    overflow-wrap: anywhere;
    font-size: var(--text-base);
    font-weight: var(--font-weight-semibold);
    line-height: var(--text-base--line-height);
  }

  .trace-span__mono {
    overflow-wrap: anywhere;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
  }

  .trace-span__payload {
    max-block-size: 24rem;
    margin: 0;
    padding: 0.75rem;
    overflow: auto;
    border-radius: var(--radius-md);
    background: var(--muted);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }

  .trace-span__payload--error {
    color: var(--destructive);
  }

  .trace-span__facts {
    display: grid;
    gap: 0.25rem;
    margin: 0;
    padding: 0;
    list-style: none;
    font-size: var(--text-sm);
    overflow-wrap: anywhere;
  }

  .trace-span__context summary {
    cursor: pointer;
  }

  .trace-span .alert {
    margin: 1rem 1rem 0;
  }
}
