.message-area {
  display: contents;

  &:has(.message) {
    .message-area--empty {
      display: none;
    }
  }

  &:not(:has(.message)) {
    --opacity: 0.2;

    block-size: 100vh;
    display: grid;
    place-content: center stretch;
    place-items: center center;

    > * {
      grid-area: 1/1;
    }

    .message-area--empty > figure img {
      inline-size: 30dvw;
    }

  }
}


#system_welcome {
  --avatar-size: 5ch;

  display: block;
  margin-block-start: calc(var(--avatar-size) * 2);
  position: relative;
  z-index: 1;

  .avatar {
    margin-block-start: calc(var(--avatar-size) * -0.5);
  }

  .message__body {
    display: block;
    max-inline-size: unset;
  }

  .message__body-content {
    overflow: unset;
  }

  .system-welcome--translation {
    inset-block-start: var(--block-space);
    inset-inline-start: var(--block-space);
    position: absolute;
  }

  .invite-label,
  .btn--regenerate {
    display: none;
  }
}

.messages {
  --message-column-gap: 0.66ch;
  --message-row-gap: 0.1em;
  --message-space: 1.33em;

  display: grid;
  flex: 1;
  /* One column no wider than the list: an auto track grows to its widest message, and every bubble aligned to it runs off the screen. */
  grid-template-columns: minmax(0, 1fr);
  grid-auto-rows: min-content;
  overflow-y: auto;
  overscroll-behavior: contain;
  position: relative;
  row-gap: var(--message-space);

  @media (max-width: 120ch) {
    padding-block-start: var(--navbar-height);
  }
}

.message {
  --content-padding-block: 0.66rem;
  --content-padding-inline: calc(var(--inline-space) * 1.5);

  column-gap: var(--message-column-gap);
  display: grid;
  grid-auto-columns: var(--inline-space-double) 1fr min-content;
  grid-auto-rows: min-content;
  grid-template-areas:
    "loading_up loading_up loading_up"
    "new new new"
    "sep sep sep"
    "avatar body body"
    "replies_sep replies_sep replies_sep"
    "loading_down loading_down loading_down";
  padding-inline: calc(var(--inline-space) + 1vw);
  position: relative;
  row-gap: var(--message-row-gap);
  visibility: hidden;
  z-index: 0;

  &:last-of-type {
    &:has(.boost__reactions) {
      .message__body {
        padding-block-end: 3.2rem;
      }
    }
  }

  &.message--formatted {
    visibility: visible;
  }

  &:has([open]) {
    z-index: 1;

    [open] .popup-menu {
      display: block;
    }
  }

  + .message--threaded {
    margin-block-start: calc((var(--message-space) + (var(--message-row-gap) * 2) - 2px) * -1);
  }

  &.message--failed {
    opacity: 0.5;

    .message__body-content {
      animation: wiggle 0.4s both;
      outline: 1px dashed var(--color-negative);
    }
  }
}


.message__body {
  column-gap: var(--inline-space-half);
  display: flex;
  grid-area: body;
  max-inline-size: min(80ch, 85%);
  position: relative;
  inline-size: 100%;

  .threads {
    margin-block-start: calc(var(--block-space-half) / 2);

    &:empty {
      display: none;
    }
  }

  .thread__link {
    font-family: sans-serif;
    font-size: 0.85em;
    text-decoration: none;
    color: inherit;
    gap: 0.5em;
    align-items: center;
    padding: 0.25em 0.5em;
    margin: -0.25em -0.5em;
    border-radius: 0.3em;
    flex-wrap: wrap;

    @media (any-hover: hover) {
      &:where(:not(:active):hover) {
        background-color: var(--color-message-bg);
        filter: brightness(0.9);
      }
    }

    .txt-muted {
      color: var(--color-text-muted, #666);
      font-size: 0.95em;
    }
  }

  .thread__avatar {
    --avatar-size: 20px;

    inline-size: auto;
  }

  @media (max-width: 120ch) {
    max-inline-size: 100%;
  }
}

.message__body-content {
  background-color: var(--message-background, var(--color-message-bg));
  border-radius: 0.66em;
  color: var(--message-color, var(--color-text));
  font-weight: 400;
  inline-size: 100%;
  margin-block-end: var(--message-row-gap);
  max-inline-size: 100%;
  padding: var(--content-padding-block) var(--content-padding-inline);
  position: relative;
  word-break: break-word;

  p:last-child {
    margin-block-end: 0;
  }

  &:has(.message__attachment) {
    margin-block-end: 0.33em;
    padding: 0;

    &:has(.boost) {
      padding-block-end: var(--block-space-half);

      .boosts {
        padding-inline: var(--inline-space);
      }
    }
  }

  &:has(.message__pending-upload) {
    padding: 0;
  }
}

.message__attachment {
  block-size: auto;
  border: 1px solid var(--color-border);
  border-radius: 0.66em;

  .message__body-content:has(.boost) & {
    border-end-start-radius: 0;
    border-end-end-radius: 0;
  }
}

img.message__attachment {
  cursor: zoom-in;
}

img.message__attachment--loading {
  background: var(--color-message-bg, var(--color-border));
  animation: pulse-bg 1.6s ease-in-out infinite;
}

.message__attachment--processing {
  align-items: center;
  background: var(--color-message-bg, var(--color-border));
  display: flex;
  justify-content: center;
  inline-size: 100%;
  block-size: 100%;
  min-block-size: 5em;
  animation: pulse-bg 1.6s ease-in-out infinite;
}

.message__attachment--previewing {
  animation: none;
  background-position: center;
  background-size: cover;
}

img.message__attachment--reveal {
  animation: unblur-in 0.35s ease-out;
}

@media (prefers-reduced-motion: reduce) {
  img.message__attachment--reveal {
    animation: none;
  }
}

@keyframes unblur-in {
  from { filter: blur(14px); }
  to { filter: blur(0); }
}

.message__attachment-spinner {
  block-size: 1.8em;
  inline-size: 1.8em;
  border: 2px solid var(--color-border);
  border-block-start-color: var(--color-text);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}



.popup-menu {
  --arrow-size: 1.5em;
  --boost-border-color: var(--color-selected-dark);
  --border-color: var(--color-border-darker);
  --border-size: 1px;

  background-color: var(--color-bg);
  border-radius: calc(var(--arrow-size) / 2);
  display: none;
  padding: var(--inline-space-half);
  position: absolute;

  &::before {
     aspect-ratio: 2;
     background: var(--border-color);
     clip-path: polygon(50% 0, 100% 100%, 0 100%);
     inline-size: var(--arrow-size);
     content: "";
     display: block;
     inset-block-start: calc(var(--arrow-size) / -2);
     inset-inline-start: 50%;
     position: absolute;
     transform: translateX(-50%);
   }
  
  &::after {
     aspect-ratio: 2;
     background: var(--color-bg);
     clip-path: polygon(50% 0, 100% 100%, 0 100%);
     inline-size: var(--arrow-size);
     content: "";
     display: block;
     inset-block-start: calc((var(--arrow-size) / -2) + var(--border-size) * 1.5);
     inset-inline-start: 50%;
     position: absolute;
     transform: translateX(-50%);
   }


  .popup-orientation-top & {
    inset-block-end: var(--btn-size);
    inset-block-start: auto;
    transform-origin: bottom left;

    &::before {
      clip-path: polygon(50% 100%, 100% 0, 0 0);
      inset-block-end: auto;
      inset-block-start: 100%;
    }

    &::after {
      clip-path: polygon(50% 100%, 100% 0, 0 0);
      inset-block-end: auto;
      inset-block-start: calc(100% - var(--border-size) * 1.5);
    }

  }
}

.message.search-highlight {
  .message__body-content {
    border: 2px solid var(--color-text);
    animation: border-fade-out 0.5s 1s forwards;
  }
}

.message__pending-upload {
  border-radius: 0.66em;
  overflow: hidden;
  padding: var(--content-padding-block) var(--content-padding-inline);
  position: relative;

  &::before {
    background-color: var(--color-border-dark);
    block-size: 100%;
    content: "";
    inline-size: 100%;
    inset-block-start: 0;
    inset-inline-start: 0;
    position: absolute;
    transform: translateX(calc(var(--percentage) - 100%));
    transition: transform 300ms ease-out;
    z-index: 1;
  }

  &[data-phase="compressing"]::before,
  &[data-phase="finishing"]::before {
    transform: translateX(0);
    opacity: 0.4;
    animation: pulse-bg 1.5s ease-in-out infinite;
  }

  &[data-phase="failed"]::before {
    transform: translateX(0);
    opacity: 0.4;
    background-color: color-mix(in oklab, var(--color-negative) 35%, transparent);
  }

  > div:not(.message__pending-upload-overlay) {
    position: relative;
    z-index: 2;
  }

  span {
    inline-size: 3rem;
    display: inline-block;
  }

  .message__upload-spinner,
  .message__upload-failure {
    inline-size: auto;
  }

  .message__upload-failure {
    color: var(--color-negative);
    font-weight: 600;
  }

  .composer__file-thumbnail {
    aspect-ratio: 1;
    background-size: contain;
    inline-size: 1.4em;
  }
}

.message__pending-upload--image {
  padding: 0;

  &::before {
    display: none;
  }

  .message__attachment, .message__pending-upload-preview {
    border: 1px solid var(--color-border);
    border-radius: 0.66em;
  }
}

.cs-msg__bubble .message__pending-upload-preview,
.message__pending-upload-preview {
  display: block;
  filter: blur(2px) brightness(0.7);
  inline-size: 100%;
  block-size: 100%;
  object-fit: cover;
}

.message__pending-upload-overlay {
  align-items: center;
  display: flex;
  flex-direction: column;
  gap: 0.5em;
  inset: 0;
  justify-content: center;
  position: absolute;
  z-index: 2;
}

.message__pending-upload-status {
  background: oklch(0% 0 0 / 0.55);
  border-radius: 2em;
  color: white;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.5em 1.4em;

}

.message__pending-upload-bar {
  background: oklch(100% 0 0 / 0.25);
  block-size: 4px;
  border-radius: 2px;
  inline-size: 60%;
  overflow: hidden;

  &::after {
    background: white;
    block-size: 100%;
    content: "";
    display: block;
    inline-size: var(--percentage);
    transition: inline-size 300ms ease-out;
  }
}

.message__upload-spinner {
  display: inline-block;
}

@keyframes pulse-bg {
  0%, 100% { opacity: 0.25; }
  50% { opacity: 0.5; }
}


[id^="messages_"] {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 4px;
}

.messages:has(.cs-msg) {
  --message-space: 4px;
}

.cs-msg--chat .cs-msg__bubble :is(img, video):not(.message__attachment):not(.message__pending-upload-preview) { max-width: min(100%, 480px); height: auto; border-radius: 12px; }
.cs-msg--chat .cs-msg__bubble .max-inline-size { max-inline-size: 100%; margin-inline: auto; }

.lexxy-content { line-height: 1.625; }
.lexxy-content a:not(.mention) { color: var(--color-accent-strong); font-weight: 500; }
.lexxy-content code {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Menlo, monospace;
  font-size: 0.88em;
  background: var(--color-surface);
  padding: 2px 6px;
  border-radius: 5px;
  color: var(--color-accent-strong);
}
.lexxy-content img, .lexxy-content video { max-width: min(100%, 480px); height: auto; border-radius: 12px; }
.lexxy-content .attachment { display: block; margin: 8px auto; max-width: min(100%, 480px); }
.lexxy-content .attachment__caption { font-size: 0.85rem; color: var(--color-txt-muted); margin-top: 4px; }


.msg-edit-btn { display: none; }
.cs-msg--me .msg-edit-btn,
.admin .msg-edit-btn { display: block; }

/* The message menu is the Atendimento's card, opened with the reaction bar by reaction-popover#openMenu. */
.cs-msg__menu-item.is-copied { color: var(--ds-link); }
.cs-msg__menu-item[aria-disabled="true"] { color: var(--ds-text-muted); cursor: default; }

.msg-edit {
  padding: 8px;
  animation: fadeUp 0.15s ease both;
}

.msg-edit { flex: 1; min-width: 0; }

.msg-edit__form { display: contents; }

.msg-edit__pill lexxy-editor {
  --lexxy-editor-rows: 1lh;
  border: none;
  box-shadow: none;
  display: flex;
  flex-direction: column;
}

.msg-edit__pill .lexxy-editor__content {
  padding: 0;
  outline: none;
  order: -1;
}

.msg-edit__pill lexxy-toolbar {
  display: flex;
  position: relative;
  gap: 0.2em;
  border: none;
  background: none;
  padding: 0.4em 0 0;
  min-height: 0;
  align-items: center;
  order: 1;
}

.msg-edit__pill .lexxy-editor__toolbar-button {
  --lexxy-toolbar-button-size: 1.75rem;
  background: transparent;
  border-radius: 6px;
}

.msg-edit__pill .lexxy-editor__toolbar-button[aria-pressed="true"],
.msg-edit__pill [open] > .lexxy-editor__toolbar-button {
  background-color: var(--color-hover);
}

@media (any-hover: hover) {
  .msg-edit__pill .lexxy-editor__toolbar-button:hover:not([aria-pressed="true"]) {
    background-color: var(--color-hover);
  }
}

.msg-edit__pill button[name="upload"],
.msg-edit__pill button[name="table"],
.msg-edit__pill button[name="divider"],
.msg-edit__pill button[name="undo"],
.msg-edit__pill button[name="redo"],
.msg-edit__pill .lexxy-editor__toolbar-spacer,
.msg-edit__pill .lexxy-editor__toolbar-overflow,
.msg-edit__pill .lexxy-editor__toolbar-dropdown:has(> [name="highlight"]) {
  display: none;
}

.msg-edit__pill .lexxy-editor__toolbar-dropdown summary ~ * {
  background-color: var(--color-bg-pure);
  border: 1px solid var(--color-border);
  border-radius: 0.75em;
  box-shadow: 0 4px 16px oklch(0% 0 0 / 0.15);
  color: var(--color-txt);
}

.msg-edit__pill lexxy-link-dropdown input[type="url"] {
  background-color: transparent;
  border: 1px solid var(--color-border);
  border-radius: 0.5em;
  color: var(--color-txt);
  padding: 0.5em 0.8em;
  font-size: max(16px, 1em);
}

.msg-edit__pill lexxy-link-dropdown input[type="url"]::placeholder {
  color: var(--color-txt-muted);
}

.msg-edit__pill lexxy-link-dropdown input[type="url"]:focus {
  outline: 2px solid var(--ds-focus-ring);
  outline-offset: -1px;
}

.msg-edit__actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: var(--ds-space-2);
  padding-block-start: var(--ds-space-2);
}

/* O Turbo marca o frame como ocupado assim que o pedido sai, entao o sinal aparece no toque e
   nao quando a resposta chega. Sem bloquear ponteiro: a sala anterior continua rolavel. */
#module_pane[aria-busy="true"] .chat-pane::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 40;
  background: color-mix(in oklch, var(--color-bg) 40%, transparent);
}

#module_pane[aria-busy="true"] .chat-pane::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 41;
  margin: auto;
  inline-size: 28px;
  block-size: 28px;
  border-radius: 50%;
  border: 3px solid var(--color-border);
  border-block-start-color: var(--accent);
}

@media (prefers-reduced-motion: no-preference) {
  #module_pane[aria-busy="true"] .chat-pane::before { animation: chat-spin 0.7s linear infinite; }
}

@keyframes chat-spin { to { transform: rotate(360deg); } }

/* The Chat's own paper, a shade off the sidebar as WhatsApp's, under the room, the discussion and the pages drawn on the wallpaper; its composer band and pill with it. */
main:has(.chat-pane, .ds-wallpaper-host),
main:has(.chat-pane, .ds-wallpaper-host) .chat-composer { background-color: var(--ds-bg-chat); }
:where(html[data-visual="v5"]) main:has(.chat-pane, .ds-wallpaper-host) .composer-pill:not(.composer--note *) { background-color: var(--ds-composer-field-chat); }
