/* ── components/bubble/bubble.css ── */
/* -- Bubble component ---------------------------------------------- */
/* A chat message bubble: the surface only - avatars, names, timestamps and
   message actions belong to a message row around it. CSS only. */

@layer components {
  .bubble {
    --_bg: var(--primary);
    --_fg: var(--primary-foreground);
    --_bd: transparent;
    --_r: 1.125rem;
    --_rs: 0.3125rem;
    --_px: 0.875rem;
    --_py: 0.5rem;
    --_fs: 0.875rem;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: fit-content;
    max-width: 80%;
    min-width: 0;

    /* -- Alignment: start = the other side (received), end = you (sent) */
    &[data-align="end"],
    .bubble-group[data-align="end"] > & {
      margin-inline-start: auto;
      align-items: flex-end;
    }

    /* -- Variants -------------------------------------------------- */
    &[data-variant="secondary"] {
      --_bg: var(--secondary);
      --_fg: var(--secondary-foreground);
    }
    &[data-variant="muted"] {
      --_bg: var(--muted);
      --_fg: var(--muted-foreground);
    }
    &[data-variant="tinted"] {
      --_bg: color-mix(in oklch, var(--primary) 12%, var(--background));
      --_fg: var(--foreground);
    }
    &[data-variant="outline"] {
      --_bg: var(--background);
      --_fg: var(--foreground);
      --_bd: var(--border);
    }
    &[data-variant="destructive"] {
      --_bg: color-mix(in oklch, var(--destructive) 12%, var(--background));
      --_fg: var(--destructive);
      --_bd: color-mix(in oklch, var(--destructive) 30%, transparent);
    }
    &[data-variant="ghost"] {
      --_bg: transparent;
      --_fg: var(--foreground);
      --_px: 0;
      max-width: none;
      width: 100%;
    }

    /* -- Sizes ------------------------------------------------------ */
    &[data-size="sm"] {
      --_r: 0.875rem;
      --_px: 0.625rem;
      --_py: 0.3125rem;
      --_fs: 0.8125rem;
    }
    &[data-size="lg"] {
      --_r: 1.375rem;
      --_px: 1.125rem;
      --_py: 0.75rem;
      --_fs: 1rem;
    }
  }

  .bubble-content {
    box-sizing: border-box;
    max-width: 100%;
    padding: var(--_py) var(--_px);
    border: 1px solid var(--_bd);
    border-radius: var(--_r);
    background: var(--_bg);
    color: var(--_fg);
    font: inherit;
    font-size: var(--_fs);
    line-height: 1.45;
    text-align: start;
    text-wrap: pretty;
    overflow-wrap: anywhere;

    & > :first-child {
      margin-block-start: 0;
    }
    & > :last-child {
      margin-block-end: 0;
    }
    & p {
      margin-block: 0 0.5em;
    }
    & a:not(.bubble-content) {
      color: inherit;
      text-decoration: underline;
      text-underline-offset: 2px;
    }
    & code {
      font-family: var(--font-mono);
      font-size: 0.9em;
      padding: 0.0625rem 0.3125rem;
      border-radius: 0.3125rem;
      background: color-mix(in oklch, currentColor 12%, transparent);
    }
    & img,
    & video {
      display: block;
      max-width: 100%;
      height: auto;
      border-radius: calc(var(--_r) - 0.375rem);
    }
    & :is(ul, ol) {
      margin-block: 0.25em 0.5em;
      padding-inline-start: 1.25em;
    }

    /* a whole bubble as a link or a button (quick replies, options) */
    &:is(a, button) {
      appearance: none;
      cursor: pointer;
      text-decoration: none;
      transition:
        background-color 150ms ease,
        border-color 150ms ease;
      &:hover {
        background: color-mix(in oklch, var(--_bg) 88%, var(--_fg));
      }
      &:focus-visible {
        outline: 2px solid var(--ring);
        outline-offset: 2px;
      }
      &:disabled,
      &[aria-disabled="true"] {
        opacity: 0.5;
        pointer-events: none;
      }
    }
  }
  .bubble[data-variant="ghost"] > .bubble-content:is(a, button):hover {
    background: var(--accent);
  }

  /* -- Media bubble: an image / video edge to edge ------------------ */
  .bubble-content[data-media] {
    padding: 0;
    overflow: hidden;
    & img,
    & video {
      border-radius: 0;
    }
    & .bubble-caption {
      padding: var(--_py) var(--_px);
    }
  }

  /* -- Collapsible content: a preview, the rest in a <details> ------- */
  .bubble-more {
    display: flex;
    flex-direction: column-reverse;

    & > summary {
      list-style: none;
      width: fit-content;
      margin-block-start: 0.25rem;
      font-weight: 500;
      text-decoration: underline;
      text-underline-offset: 2px;
      cursor: pointer;
      &::-webkit-details-marker {
        display: none;
      }
      &:focus-visible {
        outline: 2px solid var(--ring);
        outline-offset: 2px;
        border-radius: 0.25rem;
      }
    }
    & .bubble-more-open {
      display: none;
    }
    &[open] .bubble-more-open {
      display: inline;
    }
    &[open] .bubble-more-closed {
      display: none;
    }
    &::details-content {
      interpolate-size: allow-keywords;
      block-size: 0;
      overflow: clip;
      transition:
        block-size 220ms ease,
        content-visibility 220ms allow-discrete;
    }
    &[open]::details-content {
      block-size: auto;
    }
  }

  /* -- Reactions: overlapped emoji anchored to the bubble's edge ------- */
  .bubble-reactions {
    position: relative;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    align-self: flex-end;
    margin-block-start: -0.5rem;
    margin-inline: 0.625rem;
    padding: 0.125rem;
    border-radius: 9999px;
    background: var(--background);
    box-shadow:
      0 0 0 1px var(--border),
      var(--shadow-xs);
    font-size: 0.75rem;
    line-height: 1;

    & > * {
      display: grid;
      place-items: center;
      min-width: 1.375rem;
      height: 1.375rem;
      border-radius: 9999px;
      background: var(--background);
    }
    & > * + * {
      margin-inline-start: -0.3125rem;
      box-shadow: -2px 0 0 var(--background);
    }
    & > .bubble-reactions-count {
      padding-inline: 0.25rem 0.375rem;
      font-weight: 500;
      font-variant-numeric: tabular-nums;
      color: var(--muted-foreground);
      box-shadow: none;
      margin-inline-start: 0;
    }
    &[data-align="start"] {
      align-self: flex-start;
    }
    &[data-side="top"] {
      order: -1;
      margin-block: 0 -0.5rem;
    }
  }
  [dir="rtl"] .bubble-reactions > * + * {
    box-shadow: 2px 0 0 var(--background);
  }

  /* -- Group: consecutive bubbles of one sender, the joined corners
     tightened (logical radii, so RTL mirrors) ---------------------- */
  .bubble-group {
    display: flex;
    flex-direction: column;
    gap: 0.1875rem;

    & > .bubble {
      max-width: 80%;
    }
    & > .bubble:has(> .bubble-reactions:not([data-side="top"])):not(:last-child) {
      margin-block-end: 0.375rem;
    }

    /* received (start): the start-side corners between neighbours */
    &:not([data-align="end"]) > .bubble:not([data-align="end"]) {
      &:not(:last-child) > .bubble-content {
        border-end-start-radius: var(--_rs);
      }
      &:not(:first-child) > .bubble-content {
        border-start-start-radius: var(--_rs);
      }
    }
    /* sent (end) */
    &[data-align="end"] > .bubble,
    & > .bubble[data-align="end"] {
      &:not(:last-child) > .bubble-content {
        border-end-end-radius: var(--_rs);
      }
      &:not(:first-child) > .bubble-content {
        border-start-end-radius: var(--_rs);
      }
    }
  }

  /* -- Tail: data-tail on a bubble (or a group - its last bubble) draws
     a small curved tail at the bottom outer corner ------------------ */
  :is(.bubble[data-tail], .bubble-group[data-tail] > .bubble:last-child):not(
      [data-variant="ghost"],
      [data-variant="outline"],
      [data-variant="destructive"]
    )
    > .bubble-content {
    position: relative;
    border-end-start-radius: var(--_rs);

    &::before {
      /* thick against the bubble, tapering to a point at the outer bottom:
         an SVG path mask, mirrored for the end side and RTL */
      content: "";
      position: absolute;
      inset-block-end: -1px;
      inset-inline-start: -0.75rem;
      width: 0.875rem;
      height: 1.25rem;
      background: var(--_bg);
      -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M14 1C14 11 9 18 0 20H14Z'/%3E%3C/svg%3E")
        0 0 / 100% 100% no-repeat;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M14 1C14 11 9 18 0 20H14Z'/%3E%3C/svg%3E")
        0 0 / 100% 100% no-repeat;
    }
  }
  :is(
      .bubble[data-tail][data-align="end"],
      .bubble-group[data-tail][data-align="end"] > .bubble:last-child,
      .bubble-group[data-tail] > .bubble[data-align="end"]:last-child
    ):not([data-variant="ghost"], [data-variant="outline"], [data-variant="destructive"])
    > .bubble-content {
    border-end-start-radius: var(--_r);
    border-end-end-radius: var(--_rs);

    &::before {
      inset-inline-start: auto;
      inset-inline-end: -0.75rem;
      -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M0 1C0 11 5 18 14 20H0Z'/%3E%3C/svg%3E")
        0 0 / 100% 100% no-repeat;
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M0 1C0 11 5 18 14 20H0Z'/%3E%3C/svg%3E")
        0 0 / 100% 100% no-repeat;
    }
  }
  [dir="rtl"] :is(.bubble[data-tail], .bubble-group[data-tail] > .bubble:last-child) > .bubble-content::before {
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M0 1C0 11 5 18 14 20H0Z'/%3E%3C/svg%3E")
      0 0 / 100% 100% no-repeat;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M0 1C0 11 5 18 14 20H0Z'/%3E%3C/svg%3E")
      0 0 / 100% 100% no-repeat;
  }
  [dir="rtl"]
    :is(
      .bubble[data-tail][data-align="end"],
      .bubble-group[data-tail][data-align="end"] > .bubble:last-child,
      .bubble-group[data-tail] > .bubble[data-align="end"]:last-child
    )
    > .bubble-content::before {
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M14 1C14 11 9 18 0 20H14Z'/%3E%3C/svg%3E")
      0 0 / 100% 100% no-repeat;
    mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 20' preserveAspectRatio='none'%3E%3Cpath d='M14 1C14 11 9 18 0 20H14Z'/%3E%3C/svg%3E")
      0 0 / 100% 100% no-repeat;
  }

  /* -- Typing: three dots that bounce while the other side writes ---- */
  .bubble-typing {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    height: calc(var(--_fs) * 1.45);

    & > span {
      width: 0.4375rem;
      height: 0.4375rem;
      border-radius: 9999px;
      background: currentColor;
      opacity: 0.45;
      animation: bubble-typing 1.2s ease-in-out infinite;
      &:nth-child(2) {
        animation-delay: 0.15s;
      }
      &:nth-child(3) {
        animation-delay: 0.3s;
      }
    }
  }
  @keyframes bubble-typing {
    0%,
    60%,
    100% {
      translate: 0 0;
      opacity: 0.45;
    }
    30% {
      translate: 0 -0.25rem;
      opacity: 1;
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  @layer components {
    .bubble-typing > span {
      animation: none;
      opacity: 0.7;
    }
    .bubble-more::details-content {
      transition: none;
    }
    .bubble-content:is(a, button) {
      transition: none;
    }
  }
}

@media (prefers-contrast: more) {
  @layer components {
    .bubble-content {
      border-color: var(--foreground);
    }
    .bubble[data-variant="ghost"] > .bubble-content {
      border-color: transparent;
    }
  }
}

@media (forced-colors: active) {
  @layer components {
    .bubble-content {
      border: 1px solid CanvasText;
      background: Canvas;
      color: CanvasText;
    }
    .bubble[data-variant="default"] > .bubble-content,
    .bubble:not([data-variant]) > .bubble-content {
      background: Highlight;
      color: HighlightText;
      forced-color-adjust: none;
    }
    .bubble-content::before {
      display: none;
    }
    .bubble-reactions {
      box-shadow: none;
      border: 1px solid CanvasText;
    }
  }
}

/* ── components/marker/marker.css ── */
/* -- Marker component ------------------------------------------------ */
/* An inline status, system note, bordered row or labeled separator in a
   conversation ("Explored 4 files", "Today", "Conversation compacted").
   CSS only; a spinner / icon goes in .marker-icon, the words in
   .marker-content. */

@layer components {
  .marker {
    --_ic: currentColor;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: fit-content;
    max-width: 100%;
    padding-block: 0.25rem;
    font-size: 0.8125rem;
    line-height: 1.4;
    color: var(--muted-foreground);
    text-align: start;

    /* -- Variants -------------------------------------------------- */
    /* border: a full-width row with a rule under it */
    &[data-variant="border"] {
      width: 100%;
      padding-block: 0.5rem;
      border-block-end: 1px solid var(--border);
    }
    /* separator: a centered label between two rules ("Today") */
    &[data-variant="separator"] {
      width: 100%;
      justify-content: center;
      gap: 0.75rem;
      font-size: 0.75rem;
      font-weight: 500;

      &::before,
      &::after {
        content: "";
        flex: 1 1 0;
        min-width: 1rem;
        height: 1px;
        background: var(--border);
      }
    }

    /* stacked: the icon above the words, centered */
    &[data-orientation="vertical"] {
      flex-direction: column;
      gap: 0.375rem;
      text-align: center;
      &:not([data-variant="separator"], [data-variant="border"]) {
        margin-inline: auto;
      }
    }

    /* -- Tones: color the icon (the words stay quiet) -------------- */
    &[data-tone="success"] {
      --_ic: oklch(0.6 0.15 150);
    }
    &[data-tone="warning"] {
      --_ic: oklch(0.72 0.16 70);
    }
    &[data-tone="info"] {
      --_ic: oklch(0.6 0.16 250);
    }
    &[data-tone="destructive"] {
      --_ic: var(--destructive);
    }
    &[data-tone="primary"] {
      --_ic: var(--primary);
    }

    /* -- Sizes ------------------------------------------------------ */
    &[data-size="sm"] {
      font-size: 0.75rem;
      gap: 0.375rem;
      & .marker-icon {
        font-size: 0.875rem;
      }
    }
    &[data-size="lg"] {
      font-size: 0.875rem;
      & .marker-icon {
        font-size: 1.125rem;
      }
    }
  }

  .marker-icon {
    display: inline-flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    line-height: 1;
    color: var(--_ic);

    & svg {
      width: 1em;
      height: 1em;
    }
  }

  .marker-content {
    min-width: 0;
    text-wrap: pretty;
    overflow-wrap: anywhere;

    & strong,
    & b {
      font-weight: 600;
      color: var(--foreground);
    }
    & code {
      font-family: var(--font-mono);
      font-size: 0.92em;
      color: var(--foreground);
    }
  }

  /* -- Shimmer: streaming text ("Thinking…") - a light band sweeps
     across the words (background-clip: text) ------------------------ */
  .marker-content[data-shimmer] {
    color: transparent;
    background: linear-gradient(
        90deg,
        var(--muted-foreground) 0%,
        var(--muted-foreground) 35%,
        var(--foreground) 50%,
        var(--muted-foreground) 65%,
        var(--muted-foreground) 100%
      )
      0 0 / 250% 100%;
    -webkit-background-clip: text;
    background-clip: text;
    animation: marker-shimmer 1.8s linear infinite;
  }
  @keyframes marker-shimmer {
    from {
      background-position: 100% 0;
    }
    to {
      background-position: 0% 0;
    }
  }

  /* -- Interactive: the whole marker as a link or a button ----------- */
  :is(a, button).marker {
    appearance: none;
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    font: inherit;
    font-size: 0.8125rem;
    color: var(--muted-foreground);
    text-decoration: none;
    cursor: pointer;
    transition: color 150ms ease;

    &:hover {
      color: var(--foreground);
      & .marker-content {
        text-decoration: underline;
        text-underline-offset: 2px;
      }
    }
    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }
    &:disabled {
      opacity: 0.5;
      pointer-events: none;
    }
  }
  :is(a, button).marker[data-variant="border"] {
    border-radius: 0;
    border-block-end: 1px solid var(--border);
  }
}

@media (prefers-reduced-motion: reduce) {
  @layer components {
    .marker-content[data-shimmer] {
      animation: none;
      color: var(--muted-foreground);
      background: none;
    }
    :is(a, button).marker {
      transition: none;
    }
  }
}

@media (prefers-contrast: more) {
  @layer components {
    .marker {
      color: var(--foreground);
    }
    .marker[data-variant="separator"]::before,
    .marker[data-variant="separator"]::after {
      background: var(--foreground);
    }
  }
}

@media (forced-colors: active) {
  @layer components {
    .marker {
      color: CanvasText;
    }
    .marker-icon {
      color: CanvasText;
    }
    .marker[data-variant="separator"]::before,
    .marker[data-variant="separator"]::after {
      background: CanvasText;
    }
    .marker-content[data-shimmer] {
      color: CanvasText;
      background: none;
    }
    :is(a, button).marker {
      color: LinkText;
    }
  }
}

/* ── components/message/message.css ── */
/* -- Message component ----------------------------------------------- */
/* One row of a conversation: an avatar beside a column of header (name,
   time), the message surface (a .bubble, an attachment, anything) and a
   footer (status, actions). Start = the other side, end = you. CSS only. */

@layer components {
  .message {
    --_av: 2rem;
    --_gap: 0.5rem;
    --_foot: 1.75rem;
    display: flex;
    align-items: flex-end;
    gap: var(--_gap);
    width: 100%;
    min-width: 0;

    &[data-align="end"] {
      flex-direction: row-reverse;
    }

    /* -- Sizes: avatar column and the text scale ------------------- */
    &[data-size="sm"] {
      --_av: 1.5rem;
      --_gap: 0.375rem;
    }
    &[data-size="lg"] {
      --_av: 2.5rem;
      --_gap: 0.75rem;
    }
  }

  /* The avatar slot keeps its column even when empty (a placeholder keeps
     consecutive messages aligned) and sits level with the surface's
     bottom edge - lifted above the footer when there is one. */
  .message-avatar {
    display: flex;
    flex: none;
    justify-content: center;
    width: var(--_av);

    & > .avatar {
      width: var(--_av);
      height: var(--_av);
    }
  }
  .message:has(> .message-content > .message-footer) > .message-avatar {
    margin-block-end: calc(var(--_foot) + 0.125rem);
  }
  /* no avatar column at all: data-avatar="none" on the message */
  .message[data-avatar="none"] > .message-avatar {
    display: none;
  }

  .message-content {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.125rem;
    min-width: 0;
    max-width: min(80%, 40rem);

    /* the surface fills up to the column; the column carries the 80% cap */
    & > .bubble {
      max-width: 100%;
    }
    & > .bubble[data-variant="ghost"] {
      width: auto;
    }
  }
  .message[data-align="end"] > .message-content {
    align-items: flex-end;
  }
  /* an assistant's ghost reply runs the full width */
  .message-content:has(> .bubble[data-variant="ghost"]) {
    max-width: 100%;
    flex: 1;
  }
  /* its header lines up with the unframed text */
  .message-content:has(> .bubble[data-variant="ghost"]) > :is(.message-header, .message-footer) {
    padding-inline: 0;
  }

  .message-header {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 0.375rem;
    padding-inline: 0.25rem;
    font-size: 0.75rem;
    line-height: 1.4;
    color: var(--muted-foreground);
  }
  .message-name {
    font-weight: 600;
    color: var(--foreground);
  }
  .message-time {
    font-variant-numeric: tabular-nums;
    color: var(--muted-foreground);
  }

  .message-footer {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    height: var(--_foot);
    padding-inline: 0.25rem;
    font-size: 0.75rem;
    color: var(--muted-foreground);
    white-space: nowrap;

    & svg {
      width: 0.875rem;
      height: 0.875rem;
      flex-shrink: 0;
    }
    & .btn svg {
      width: 1rem;
      height: 1rem;
    }
    & .btn {
      color: var(--muted-foreground);
    }
    & .btn:hover {
      color: var(--foreground);
    }
  }

  /* data-reveal="hover": the footer's actions appear on hover / focus of
     the message; always shown where there is no hover (touch) */
  @media (hover: hover) {
    .message-footer[data-reveal="hover"] {
      opacity: 0;
      transition: opacity 150ms ease;
    }
    .message:is(:hover, :focus-within) > .message-content > .message-footer[data-reveal="hover"] {
      opacity: 1;
    }
  }

  /* -- Status: delivery ticks in the footer --------------------------- */
  .message-status {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    &[data-status="read"] {
      color: oklch(0.6 0.16 250);
    }
    &[data-status="failed"] {
      color: var(--destructive);
    }
  }

  /* -- Attachment: a file card as the message surface ------------------ */
  .message-attachment {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-sizing: border-box;
    width: 16rem;
    max-width: 100%;
    padding: 0.625rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    background: var(--card);
    color: var(--card-foreground);
    font-size: 0.8125rem;
    text-decoration: none;

    &:is(a) {
      cursor: pointer;
      transition: background-color 150ms ease;
    }
    &:is(a):hover {
      background: var(--accent);
    }
    &:is(a):focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }
  }
  .message-attachment-icon {
    display: grid;
    flex: none;
    place-items: center;
    width: 2.25rem;
    height: 2.25rem;
    border-radius: var(--radius-md);
    background: var(--muted);
    color: var(--muted-foreground);
    & svg {
      width: 1.125rem;
      height: 1.125rem;
    }
  }
  .message-attachment-body {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    line-height: 1.3;
  }
  .message-attachment-name {
    overflow: hidden;
    font-weight: 500;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .message-attachment-meta {
    font-size: 0.75rem;
    color: var(--muted-foreground);
  }

  /* -- Group: consecutive messages of one sender ---------------------- */
  .message-group {
    display: flex;
    flex-direction: column;
    gap: 0.1875rem;

    /* one avatar per group: only the last message shows it (the others
       keep the empty column, so everything lines up) */
    & > .message:not(:last-child) > .message-avatar > * {
      visibility: hidden;
    }
    /* one header per group: the first message's */
    & > .message:not(:first-child) > .message-content > .message-header {
      display: none;
    }
    /* the joined bubble corners tighten, as in a .bubble-group */
    & > .message:not([data-align="end"]) {
      &:not(:last-child) > .message-content > .bubble > .bubble-content {
        border-end-start-radius: var(--_rs);
      }
      &:not(:first-child) > .message-content > .bubble > .bubble-content {
        border-start-start-radius: var(--_rs);
      }
    }
    & > .message[data-align="end"] {
      &:not(:last-child) > .message-content > .bubble > .bubble-content {
        border-end-end-radius: var(--_rs);
      }
      &:not(:first-child) > .message-content > .bubble > .bubble-content {
        border-start-end-radius: var(--_rs);
      }
    }
  }

  /* -- A conversation: rows of messages, groups and markers ------------- */
  .message-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    min-width: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  @layer components {
    .message-footer[data-reveal="hover"],
    .message-attachment {
      transition: none;
    }
  }
}

@media (prefers-contrast: more) {
  @layer components {
    .message-header,
    .message-footer,
    .message-time,
    .message-attachment-meta {
      color: var(--foreground);
    }
  }
}

@media (forced-colors: active) {
  @layer components {
    .message-attachment {
      border-color: CanvasText;
    }
    .message-status[data-status] {
      color: CanvasText;
    }
  }
}

/* ── components/session/session.css ── */
/* -- Session component -------------------------------------------
   A chat session: a scrolling transcript (role="log") that follows the
   live edge, jumps back to it, keeps its place when history loads, anchors
   new turns, and takes dropped files - plus a footer for the composer.
   The runtime keeps the data-* state current; CSS draws from it. */

@layer components {
  .session {
    position: relative;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    background: var(--card);
    color: var(--card-foreground);
  }

  /* -- Viewport: the scroller --------------------------------------- */
  .session-viewport {
    position: relative;
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    padding: 1rem;
    overflow-y: auto;
    overscroll-behavior: contain;
    /* the runtime keeps the reader's place when history prepends - the
       browser's own scroll anchoring would correct twice */
    overflow-anchor: none;
    scrollbar-gutter: stable;

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

  /* short threads sit at the bottom, like every chat */
  .session-content {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-block-start: auto;
  }

  /* rows off screen skip rendering; their size is remembered. content-
     visibility implies paint containment, which clips at the row's edge -
     the clip margin lets bubble tails, reactions and focus rings draw
     outside it */
  .session-item {
    content-visibility: auto;
    contain-intrinsic-size: auto 3.5rem;
    overflow-clip-margin: 1rem;

    &[data-current] {
      scroll-margin-block-start: 1rem;
    }
  }

  /* data-animate: new rows enter - rise (the default) from below, fade in
     place, or slide from their own side (a sent message from the end, a
     reply from the start). @starting-style: rows present at load never
     animate, only rows added later. VERIFIED: (session.e2e "data-animate")
     slide from each row's own side, fade, tokens; none under reduced motion. */
  .session[data-animate] .session-item {
    transition:
      opacity 250ms ease,
      translate 250ms ease;

    @starting-style {
      opacity: 0;
      translate: 0 0.5rem;
    }
  }
  .session[data-animate="fade"] .session-item {
    @starting-style {
      translate: none;
    }
  }
  .session[data-animate="slide"] .session-item {
    transition-duration: 320ms;
    transition-timing-function: cubic-bezier(0.2, 0.8, 0.2, 1);

    @starting-style {
      translate: -1.5rem 0;
    }
  }
  .session[data-animate="slide"] .session-item:has(> .message[data-align="end"]) {
    @starting-style {
      translate: 1.5rem 0;
    }
  }

  /* a streamed word: wrap each in .session-token as it arrives and it fades
     in - the reply writes itself instead of jumping word by word */
  .session[data-animate] .session-token {
    transition: opacity 180ms ease;

    @starting-style {
      opacity: 0;
    }
  }

  /* -- Scroll buttons: float over the transcript, stuck to its edge -- */
  .session-scroll-button {
    position: sticky;
    z-index: 1;
    align-self: center;
    flex: none;
    display: grid;
    place-items: center;
    width: 2.25rem;
    height: 2.25rem;
    margin: 0;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--background);
    color: var(--foreground);
    box-shadow: var(--shadow-md);
    cursor: pointer;
    opacity: 0;
    scale: 0.85;
    visibility: hidden;
    transition:
      opacity 150ms,
      scale 150ms,
      visibility 150ms allow-discrete,
      background-color 150ms;

    /* a chevron, drawn */
    &::before {
      content: "";
      width: 0.5rem;
      height: 0.5rem;
      border-inline-end: 2px solid currentColor;
      border-block-end: 2px solid currentColor;
      rotate: 45deg;
      translate: 0 -0.125rem;
    }

    &:hover {
      background: var(--accent);
      color: var(--accent-foreground);
    }
    &:focus-visible {
      outline: 2px solid var(--ring);
      outline-offset: 2px;
    }

    /* to the end: at the bottom edge, overlaying the last row */
    &[data-to="end"] {
      order: 2;
      inset-block-end: 0.5rem;
      margin-block-start: -2.25rem;
    }
    /* to the start: at the top edge, pointing up */
    &[data-to="start"] {
      order: -1;
      inset-block-start: 0.5rem;
      margin-block-end: -2.25rem;
      &::before {
        rotate: -135deg;
        translate: 0 0.125rem;
      }
    }
  }

  .session[data-scrollable~="end"] .session-scroll-button[data-to="end"],
  .session[data-scrollable~="start"] .session-scroll-button[data-to="start"] {
    opacity: 1;
    scale: 1;
    visibility: visible;
  }

  /* -- Footer: the composer, a status line ---------------------------- */
  .session-footer {
    flex: none;
    padding: 0.75rem;
    border-top: 1px solid var(--border);
    background: var(--card);
  }

  .session-status {
    padding: 0.375rem 1rem;
    border-top: 1px solid var(--border);
    color: var(--muted-foreground);
    font-size: 0.75rem;
  }

  /* -- Drop target: files dragged over the session ----------------------- */
  .session[data-drop-active]::after {
    content: attr(data-drop-label);
    position: absolute;
    inset: 0.5rem;
    z-index: 2;
    display: grid;
    place-items: center;
    border: 2px dashed var(--ring);
    border-radius: var(--radius-lg);
    background: color-mix(in oklch, var(--background) 85%, transparent);
    color: var(--foreground);
    font-size: 0.875rem;
    font-weight: 500;
    pointer-events: none;
  }

  /* -- Accessibility -------------------------------------------- */
  @media (prefers-reduced-motion: reduce) {
    .session-scroll-button,
    .session[data-animate] :is(.session-item, .session-token) {
      transition: none;
    }
  }

  @media (prefers-contrast: more) {
    .session,
    .session-footer,
    .session-scroll-button {
      border-color: var(--foreground);
    }
  }

  @media (forced-colors: active) {
    .session {
      border: 1px solid CanvasText;
    }
    .session-scroll-button {
      border: 1px solid ButtonText;
    }
    .session[data-drop-active]::after {
      border-color: Highlight;
      forced-color-adjust: none;
    }
  }
}

/* ── components/chat-components/chat-components.css ── */
@layer components {
  /* A Component Schema document shown in a chat: a Card of content, fields and actions inside a message. */
  .chat-components {
    display: block;
    /* a definite width: inside a shrink-to-fit message a percentage alone resolves to nothing */
    inline-size: 34rem;
    max-inline-size: 100%;

    & > script {
      display: none;
    }
  }

  .chat-components-card {
    text-align: start;
  }

  .chat-components-content {
    display: grid;
    gap: 1rem;
    min-inline-size: 0;

    & > :is(p, ul, ol, dl) {
      margin: 0;
    }
  }

  .chat-components-text {
    line-height: 1.55;
    text-wrap: pretty;

    & code {
      font-family: var(--font-mono);
      font-size: 0.875em;
    }
  }

  .chat-components-list {
    padding-inline-start: 1.25rem;
    display: grid;
    gap: 0.25rem;
    line-height: 1.5;
  }

  .chat-components-field {
    display: grid;
    gap: 0.375rem;
    min-inline-size: 0;

    & > .label {
      margin: 0;
    }
  }

  .chat-components-hint {
    margin: 0;
    font-size: 0.8125rem;
    color: var(--muted-foreground);
    text-wrap: pretty;
  }

  /* a radio / checkbox group and a rating: a fieldset with its legend as the label */
  .chat-components-group {
    display: grid;
    gap: 0.5rem;
    margin: 0;
    padding: 0;
    border: 0;
    min-inline-size: 0;

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

  .chat-components-choice {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;

    & > input:is(.radio, .checkbox) {
      margin-block-start: 0.125rem;
      flex: none;
    }

    & > label {
      display: grid;
      gap: 0.125rem;
      font-size: 0.875rem;
      line-height: 1.4;
      cursor: pointer;
    }

    /* "Other": its text field follows the choice */
    &[data-other] {
      flex-wrap: wrap;
      align-items: center;

      & > .chat-components-other {
        flex: 1 1 10rem;
        inline-size: auto;
        min-inline-size: 0;
      }
    }
  }

  .chat-components-choice-description {
    font-size: 0.8125rem;
    color: var(--muted-foreground);
  }

  .chat-components-switch {
    flex-wrap: wrap;

    & > .chat-components-hint {
      flex-basis: 100%;
    }
  }

  .chat-components-slider {
    display: flex;
    align-items: center;
    gap: 0.75rem;

    & > .slider {
      flex: 1;
      min-inline-size: 0;
    }
  }

  .chat-components-output {
    min-inline-size: 3ch;
    font-variant-numeric: tabular-nums;
    font-size: 0.875rem;
    text-align: end;
  }

  /* facts: a definition list of label / value rows (also the answers of a questionnaire) */
  .chat-components-facts {
    display: grid;
    gap: 0;
    font-size: 0.875rem;

    & > div {
      display: grid;
      grid-template-columns: minmax(6rem, 40%) 1fr;
      gap: 0.75rem;
      padding-block: 0.5rem;
      border-block-end: 1px solid var(--border);

      &:last-child {
        border-block-end: 0;
      }
    }

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

    & dd {
      margin: 0;
      font-weight: 500;
      overflow-wrap: anywhere;
    }
  }

  /* a plan: numbered steps, optionally each with a checkbox */
  .chat-components-plan {
    display: grid;
    gap: 0.625rem;
    padding: 0;
    margin: 0;
    list-style: none;
    counter-reset: chat-plan;

    & > li {
      display: grid;
      grid-template-columns: 1.5rem 1fr;
      gap: 0.5rem;
      align-items: start;
      counter-increment: chat-plan;

      &::before {
        content: counter(chat-plan);
        display: grid;
        place-items: center;
        inline-size: 1.5rem;
        block-size: 1.5rem;
        border-radius: 999px;
        background: var(--muted);
        color: var(--muted-foreground);
        font-size: 0.75rem;
        font-weight: 600;
      }

      & > label {
        display: grid;
        gap: 0.125rem;
        cursor: pointer;
      }
    }

    &[data-checkable] > li {
      grid-template-columns: 1.5rem auto 1fr;

      & > .checkbox {
        margin-block-start: 0.25rem;
      }
    }
  }

  .chat-components-plan-title {
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.45;
  }

  .chat-components-plan-description {
    font-size: 0.8125rem;
    color: var(--muted-foreground);
  }

  .chat-components-questionnaire {
    max-inline-size: none;
  }

  .chat-components-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  /* answered or cancelled: only the chosen action stays, marked */
  .chat-components[data-answered] {
    & .chat-components-actions > [data-chat-action]:not([data-chosen]) {
      display: none;
    }

    & [data-chat-action][data-chosen] {
      opacity: 1;
      pointer-events: none;

      &::before {
        content: "\2713";
        margin-inline-end: 0.25rem;
      }
    }

    & .chat-components-content :is(input, select, textarea):disabled {
      opacity: 1;
      cursor: default;
    }
  }

  .chat-components[data-answered="cancelled"] .chat-components-content {
    opacity: 0.6;
  }

  @media (prefers-reduced-motion: reduce) {
    .chat-components * {
      transition: none;
      animation: none;
    }
  }

  @media (prefers-contrast: more) {
    .chat-components-hint,
    .chat-components-choice-description,
    .chat-components-plan-description,
    .chat-components-facts dt {
      color: var(--foreground);
    }
  }

  @media (forced-colors: active) {
    .chat-components-plan > li::before {
      border: 1px solid CanvasText;
    }

    .chat-components[data-answered] [data-chat-action][data-chosen] {
      border: 2px solid Highlight;
    }
  }
}
