@layer components {
  .participant-picker {
    display: grid;
    margin: 0;
    padding: 0;
    border: 0;

    & > legend {
      padding: 0;
      margin-block-end: 0.25rem;
    }
  }

  .transcript-panel:not([hidden]) {
    display: flex;
    flex-direction: column;
  }

  /* column-reverse starts scrolled to the newest turn and stays there as turns arrive. */
  .transcript {
    display: flex;
    flex: 1;
    flex-direction: column-reverse;
    min-block-size: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .transcript__inner {
    display: grid;
    align-content: start;
    gap: 1rem;
    inline-size: 100%;
    max-inline-size: 48rem;
    margin-inline: auto;
    padding: 1rem;
  }

  .transcript__composer {
    flex-shrink: 0;
    border-block-start: 1px solid var(--border);
    background: var(--background);

    .composer {
      max-inline-size: 48rem;
      margin-inline: auto;
      padding: 0.75rem 1rem 1rem;
    }
  }

  .conversation-activity {
    max-inline-size: 48rem;
    margin-inline: auto;
    padding-inline: 1rem;

    /* Reserve a line for an available agent without moving the composer. */
    &:not([data-conversation-activity-pending-value=""]) {
      min-block-size: 2.75rem;
      padding-block-start: 0.75rem;
    }
  }

  .run-list {
    display: grid;
    gap: 0.75rem;
    max-inline-size: 48rem;
    margin: 0;
    padding: 0;
    list-style: none;
  }

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

  .run__header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .run__agent {
    flex: 1;
    font-weight: var(--font-weight-medium);
  }

  @media (width >= 48rem) {
    .transcript__inner {
      padding: 1.5rem;
    }
  }

  .conversation__status {
    display: grid;
    gap: 0.75rem;
    justify-items: start;

    & > p {
      margin: 0;
    }

    & > .alert {
      justify-self: stretch;
    }
  }

  .turn-list {
    display: grid;
    gap: 0.75rem;
    margin: 0;
    padding: 0;
    list-style: none;

    &:empty {
      display: none;
    }
  }

  .turn {
    display: grid;
    gap: 0.25rem;
    justify-self: start;
    inline-size: fit-content;
    min-inline-size: min(100%, 12rem);
    max-inline-size: min(100%, 36rem);
    padding: 0.625rem 0.875rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--card);
    color: var(--card-foreground);
  }

  .turn--mine {
    justify-self: end;
    background: var(--muted);
  }

  .turn--pending {
    opacity: 0.7;
    transition: opacity 200ms ease-out, scale 200ms ease-out;
    transform-origin: bottom right;

    @starting-style {
      opacity: 0;
      scale: 0.95;
    }
  }

  .turn[data-optimistic-placeholder] .turn__text {
    white-space: pre-wrap;
  }

  .turn--failed {
    border-color: var(--destructive);
  }

  /* The agent's steps between messages, as a trace rather than a bubble. */
  .turn--steps {
    justify-self: stretch;
    inline-size: auto;
    max-inline-size: none;
    padding: 0;
    border: 0;
    background: transparent;
  }

  /* A proposal draws its own card. */
  .turn--tool_call {
    justify-self: stretch;
    max-inline-size: none;
    padding: 0;
    border: 0;
    background: transparent;

    & > .turn__header {
      padding-inline: 0.875rem;
    }
  }

  .turn__header {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.25rem 0.5rem;
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
    color: var(--muted-foreground);
  }

  .turn__author {
    font-weight: var(--font-weight-medium);
    color: var(--foreground);
  }

  .turn__status {
    margin-inline-start: auto;
  }

  .turn--failed .turn__status,
  .turn__status--failed {
    color: var(--destructive);
  }

  .turn__delivery-error {
    margin: 0;
    color: var(--destructive);
    font-size: var(--text-sm);
    line-height: var(--text-sm--line-height);
  }

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

  .turn__body {
    display: grid;
    gap: 0.5rem;
    overflow-wrap: anywhere;
  }

  .turn__text {
    margin: 0;
    text-wrap: pretty;
  }

  /* Markdown blocks keep the bubble's type and space out evenly. */
  .turn__markdown {
    display: grid;
    gap: 0.5rem;
    min-inline-size: 0;

    & > * {
      margin: 0;
    }

    :is(h1, h2, h3, h4, h5, h6) {
      font-size: var(--text-base);
      line-height: var(--text-base--line-height);
      font-weight: var(--font-weight-semibold);
    }

    :is(ul, ol) {
      padding-inline-start: 1.25rem;
    }

    blockquote {
      padding-inline-start: 0.75rem;
      border-inline-start: 2px solid var(--border);
      color: var(--muted-foreground);
    }

    hr {
      inline-size: 100%;
      border: 0;
      border-block-start: 1px solid var(--border);
    }

    a {
      text-decoration: underline;
      text-underline-offset: 2px;
    }

    :not(pre) > code {
      padding: 0.1em 0.3em;
      border: 1px solid var(--border);
      border-radius: var(--radius-sm);
    }

    pre {
      overflow-x: auto;
      padding: 0.5rem 0.75rem;
      border: 1px solid var(--border);
      border-radius: var(--radius-md);
      overflow-wrap: normal;
    }

    table {
      display: block;
      overflow-x: auto;
      border-collapse: collapse;
      font-size: var(--text-sm);
      line-height: var(--text-sm--line-height);
      overflow-wrap: normal;
    }

    :is(th, td) {
      padding: 0.25rem 0.5rem;
      border: 1px solid var(--border);
      text-align: start;
    }
  }

  .turn__sources {
    font-size: var(--text-sm);

    ul {
      display: grid;
      margin: 0;
      padding: 0;
      list-style: none;
    }

    .button {
      max-inline-size: 100%;
      justify-content: start;
      text-align: start;
      white-space: normal;
    }
  }

  .turn__image {
    display: block;
    max-inline-size: 100%;
    max-block-size: 20rem;
    border-radius: var(--radius-md);
  }

  .turn__media {
    display: grid;
    gap: 0.375rem;
    margin: 0;
  }

  .turn__audio,
  .turn__video {
    display: block;
    inline-size: min(100%, 20rem);
  }

  .turn__video {
    max-block-size: 20rem;
    border-radius: var(--radius-md);
  }

  .turn__transcript {
    font-size: var(--text-sm);
    line-height: var(--text-sm--line-height);
    color: var(--muted-foreground);
    text-wrap: pretty;
  }

  /* A file to download, such as a PDF. */
  .turn__file {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-block-size: 2.75rem;
    padding-inline: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--background);
    color: var(--foreground);
    font-size: var(--text-sm);
    text-decoration: none;
    transition: background-color 150ms ease-out, scale 150ms ease-out;

    &:hover { background: var(--muted); }
    &:active { scale: 0.97; }
    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }
  }

  .turn__file-icon {
    display: inline-flex;
    flex-shrink: 0;
    color: var(--muted-foreground);

    svg { inline-size: 1.125rem; block-size: 1.125rem; }
  }

  .turn__file-name {
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-weight: var(--font-weight-medium);
  }

  .turn__file-size {
    margin-inline-start: auto;
    flex-shrink: 0;
    color: var(--muted-foreground);
    font-size: var(--text-xs);
  }

  .turn__pending-file:empty {
    display: none;
  }

  .composer-attachment {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    min-inline-size: 0;
    /* Fills the toolbar's middle, so it sits beside Dictate and the send buttons stay at the end. */
    flex: 1;

    [hidden] { display: none; }
  }

  .composer-attachment__file:not([hidden]) {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    min-inline-size: 0;
    padding-inline-start: 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--muted);
    font-size: var(--text-sm);
    transition: opacity 150ms ease-out, scale 150ms ease-out;

    @starting-style {
      opacity: 0;
      scale: 0.95;
    }
  }

  .composer-attachment__name {
    max-inline-size: 12rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .turn__children .turn-list {
    padding-block: 0.25rem 0.5rem;
  }

  /* What a step opens to: its inspector, payloads, and any error. */
  .turn-step {
    display: grid;
    gap: 0.75rem;
    padding-block: 0.25rem 0.5rem;

    & pre {
      margin: 0;
      padding: 0.5rem 0.75rem;
      max-block-size: 16rem;
      overflow: auto;
      border-radius: var(--radius-md);
      background: var(--muted);
      color: var(--foreground);
      font-family: var(--font-mono);
      font-size: var(--text-xs);
      line-height: var(--text-xs--line-height);
      white-space: pre-wrap;
    }

    & .inspector {
      margin: 0;
    }

    & .inspector dd {
      color: var(--foreground);
    }
  }

  .turn-step__meta {
    margin: 0;
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
  }

  .turn-step__error {
    margin: 0;
    color: var(--destructive);
  }

  .inspector {
    display: grid;
    gap: 0.25rem;
    margin: 0.5rem 0 0;

    div {
      display: grid;
      gap: 0.125rem;
    }

    dt {
      color: var(--muted-foreground);
    }

    dd {
      margin: 0;
      overflow-wrap: anywhere;
    }

    @media (width >= 40rem) {
      div {
        grid-template-columns: 12rem 1fr;
        gap: 0.5rem;
      }
    }
  }

  .composer {
    display: grid;
    gap: 0.75rem;

    & .button {
      justify-self: end;
    }
  }

  .dictation {
    container-type: inline-size;
    display: grid;
    gap: 0.5rem;

    [hidden] { display: none; }
  }

  .dictation__toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
  }

  /* Only the home's toolbar composer shows these icons. */
  :is([data-dictation-target="start"], [data-dictation-target="send"]) .button__icon {
    display: none;
  }

  .dictation__send {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-inline-start: auto;
  }

  .dictation__session:not([hidden]) {
    display: flex;
    flex: 1;
    align-items: center;
    gap: 0.5rem;
    padding-inline: 0.75rem 0.25rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--muted);
    transition: opacity 180ms ease-out, scale 180ms ease-out;

    @starting-style {
      opacity: 0;
      scale: 0.95;
    }
  }

  .dictation__indicator {
    flex-shrink: 0;
    inline-size: 0.375rem;
    block-size: 0.375rem;
    border-radius: 50%;
    background: var(--destructive);
    box-shadow: 0 0 0 0.25rem color-mix(in srgb, var(--destructive) 12%, transparent);
  }

  .dictation__wave {
    display: flex;
    flex: 1;
    align-items: center;
    gap: 2px;
    min-inline-size: 3rem;
    max-inline-size: 12rem;
    block-size: 2.75rem;
    overflow: hidden;

    & > span {
      flex: 1;
      block-size: 1.75rem;
      border-radius: var(--radius-full, 999px);
      background: var(--foreground);
      opacity: 0.65;
      transform: scaleY(var(--level, 0.12));
      transition: transform 100ms ease-out;
    }
  }

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

  .dictation__status {
    min-block-size: 1.25rem;
    margin: 0;
    font-size: var(--text-xs);
    line-height: var(--text-xs--line-height);
    color: var(--muted-foreground);
    text-wrap: pretty;
  }

  .dictation[data-state="transcribing"] {
    .dictation__indicator { background: var(--foreground); box-shadow: none; }
    .dictation__wave > span {
      animation: dictation-processing 900ms ease-in-out infinite alternate;
      &:nth-child(3n) { animation-delay: -300ms; }
      &:nth-child(3n + 1) { animation-delay: -600ms; }
    }
  }

  .dictation[data-state="requesting"] .dictation__indicator { background: var(--muted-foreground); box-shadow: none; }
  .dictation[data-state="error"] {
    .dictation__status { color: var(--destructive); }
    :is(.dictation__indicator, .dictation__wave, .dictation__timer) { display: none; }
  }

  @container (width < 28rem) {
    .dictation:is([data-state="requesting"], [data-state="recording"], [data-state="transcribing"]) [data-dictation-target="send"] { display: none; }
  }

  @keyframes dictation-processing {
    from { transform: scaleY(0.15); opacity: 0.3; }
    to { transform: scaleY(0.5); opacity: 0.65; }
  }

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

    .dictation__session:not([hidden]), .dictation__wave > span { transition: none; }
    .turn__file, .composer-attachment__file:not([hidden]) { transition: none; }
    .dictation[data-state="transcribing"] .dictation__wave > span { animation: none; transform: scaleY(0.35); }
  }
}

@layer components {
  .template-picker {
    display: grid;
    gap: 0.75rem;
  }

  .template-picker__fields {
    display: grid;
    gap: 0.75rem;
    margin: 0;
    padding: 0;
    border: 0;

    /* Only the chosen template's variables show. */
    &[hidden] {
      display: none;
    }
  }

  .template-picker__preview {
    display: grid;
    gap: 0.375rem;
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--muted);

    &[hidden] {
      display: none;
    }
  }
}
