@layer message {
  .message-stage {
    align-items: flex-start;
    padding: clamp(18px, 5vw, 48px);
  }

  .message-thread {
    width: min(100%, 580px);
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) + 4px);
    background: var(--card);
    color: var(--card-foreground);
    box-shadow: var(--shadow-sm);
  }

  .message-thread__header {
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 62px;
    padding: 11px 16px;
    border-bottom: 1px solid var(--border);
    background: var(--card);
  }

  .message-thread__header > span:last-child {
    display: grid;
    min-width: 0;
    line-height: 1.25;
  }

  .message-thread__header strong {
    overflow: hidden;
    font-size: 14px;
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .message-thread__header small {
    color: var(--muted-foreground);
    font-size: 12px;
  }

  .message-list {
    display: flex;
    flex-direction: column;
    gap: 24px;
    margin: 0;
    padding: 24px;
    list-style: none;
  }

  .message-row {
    position: relative;
    display: flex;
    width: 100%;
    min-width: 0;
    gap: 8px;
    font-size: 14px;
  }

  .message-row--outgoing {
    flex-direction: row-reverse;
  }

  .message-row:has(.message-footer) > .message-avatar {
    transform: translateY(-32px);
  }

  .message-avatar,
  .message-avatar-spacer {
    width: 32px;
    min-width: 32px;
    height: 32px;
    align-self: flex-end;
  }

  .message-avatar {
    position: relative;
    display: grid;
    place-items: center;
    overflow: hidden;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: linear-gradient(145deg, var(--secondary), var(--accent));
    color: var(--secondary-foreground);
    font-size: 10px;
    font-weight: 650;
    letter-spacing: -.02em;
    user-select: none;
  }

  .message-row--outgoing .message-avatar {
    border-color: transparent;
    background: var(--primary);
    color: var(--primary-foreground);
  }

  .message-avatar--small {
    width: 36px;
    min-width: 36px;
    height: 36px;
    align-self: auto;
    font-size: 12px;
  }

  .message-content {
    display: flex;
    width: 100%;
    min-width: 0;
    flex-direction: column;
    gap: 8px;
    overflow-wrap: anywhere;
  }

  .message-row--outgoing .message-content > * {
    align-self: flex-end;
  }

  .message-header {
    display: flex;
    max-width: 80%;
    align-items: center;
    gap: 8px;
    padding-inline: 12px;
    color: var(--muted-foreground);
    font-size: 12px;
    font-weight: 500;
  }

  .message-header time {
    font-weight: 400;
  }

  .message-bubble {
    width: fit-content;
    max-width: 80%;
    min-width: 0;
    overflow: hidden;
    border: 1px solid transparent;
    border-radius: 24px;
    background: var(--muted);
    padding: 9px 12px;
    color: var(--foreground);
    line-height: 1.45;
  }

  .message-row--outgoing .message-bubble {
    background: var(--primary);
    color: var(--primary-foreground);
  }

  .message-group {
    display: flex;
    min-width: 0;
    flex-direction: column;
    gap: 8px;
  }

  .message-group .message-row + .message-row .message-bubble {
    border-start-end-radius: 8px;
  }

  .message-group .message-row:first-child .message-bubble {
    border-end-end-radius: 8px;
  }

  .message-footer {
    display: flex;
    min-height: 24px;
    max-width: 80%;
    align-items: center;
    gap: 8px;
    padding-inline: 12px;
    color: var(--muted-foreground);
    font-size: 12px;
  }

  .message-delivery,
  .message-error {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
  }

  .message-delivery svg,
  .message-error svg {
    width: 14px;
    height: 14px;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.8;
  }

  .message-footer--actions {
    width: 100%;
    justify-content: space-between;
    padding: 0;
  }

  .message-actions {
    display: flex;
    gap: 2px;
  }

  .message-icon-button {
    display: inline-grid;
    width: 30px;
    height: 30px;
    flex: 0 0 30px;
    place-items: center;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--muted-foreground);
    cursor: pointer;
    transition: background-color 150ms ease, color 150ms ease;
  }

  .message-icon-button:hover {
    background: var(--accent);
    color: var(--accent-foreground);
  }

  .message-icon-button:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--ring) 30%, transparent);
  }

  .message-icon-button:active {
    transform: translateY(1px);
  }

  .message-icon-button:disabled {
    cursor: not-allowed;
    opacity: .5;
  }

  .message-icon-button svg {
    width: 16px;
    height: 16px;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 2;
  }

  .message-error {
    color: var(--destructive);
    font-weight: 500;
  }

  .message-attachment {
    display: flex;
    width: min(100%, 300px);
    min-width: 0;
    align-items: center;
    gap: 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--background);
    padding: 8px 8px 8px 10px;
    box-shadow: var(--shadow-xs);
  }

  .message-attachment__icon {
    display: grid;
    width: 36px;
    height: 36px;
    flex: 0 0 36px;
    place-items: center;
    border-radius: calc(var(--radius) - 2px);
    background: var(--muted);
    color: var(--muted-foreground);
  }

  .message-attachment__icon svg {
    width: 18px;
    height: 18px;
    fill: none;
    stroke: currentColor;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-width: 1.8;
  }

  .message-attachment__text {
    display: grid;
    min-width: 0;
    flex: 1;
    line-height: 1.35;
  }

  .message-attachment__text strong {
    overflow: hidden;
    font-size: 13px;
    font-weight: 550;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .message-attachment__text small {
    color: var(--muted-foreground);
    font-size: 11px;
  }

  .message-typing {
    display: inline-flex;
    width: fit-content;
    align-items: center;
    gap: 8px;
    padding: 6px 2px;
    color: var(--muted-foreground);
    font-size: 12px;
  }

  .message-typing__dots {
    display: flex;
    align-items: center;
    gap: 3px;
    border-radius: 999px;
    background: var(--muted);
    padding: 10px 12px;
  }

  .message-typing__dots i {
    width: 5px;
    height: 5px;
    border-radius: 999px;
    background: currentColor;
    animation: message-typing-pulse 1.2s ease-in-out infinite;
  }

  .message-typing__dots i:nth-child(2) { animation-delay: 150ms; }
  .message-typing__dots i:nth-child(3) { animation-delay: 300ms; }

  @keyframes message-typing-pulse {
    0%, 60%, 100% { opacity: .35; transform: translateY(0); }
    30% { opacity: 1; transform: translateY(-2px); }
  }

  @media (max-width: 540px) {
    .message-stage { padding: 0; }

    .message-thread {
      border-radius: calc(var(--radius) + 2px);
    }

    .message-list {
      gap: 22px;
      padding: 20px 14px;
    }

    .message-bubble,
    .message-header,
    .message-footer {
      max-width: 88%;
    }

    .message-avatar,
    .message-avatar-spacer {
      width: 28px;
      min-width: 28px;
      height: 28px;
      font-size: 9px;
    }

    .message-attachment {
      width: min(100%, 280px);
    }

    .message-footer--actions {
      max-width: 88%;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .message-typing__dots i {
      animation: none;
      opacity: .65;
      transform: none;
    }
  }
}
