/* The shared transcript for rooms and direct messages (S1, #166).
   Both lists are <ol>s of <li> items (transcript.js). Each item is a flex
   column, so a message keeps aligning itself (DM outgoing to the end) as it
   did when it was the list's direct child. */
.message-list,
.dm-message-list {
  margin: 0;
  list-style: none;
}

.message-list > li,
.dm-message-list > li {
  display: flex;
  min-width: 0;
  flex-direction: column;
  flex-shrink: 0;
}

/* Day headings: h3s inside the list, so H jumps between days in browse mode. */
.transcript-day { align-items: center; }
.transcript-day-label {
  margin: 0;
  padding: 0.15rem 0.75rem;
  border-radius: 999px;
  background: var(--raised);
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1.6;
}

/* "New messages" is a heading too; it keeps the divider's look. */
.new-messages-label {
  margin: 0;
  font: inherit;
  letter-spacing: inherit;
  text-transform: inherit;
}

.transcript-control { align-items: center; }

/* The current message (S1b): one tab stop; a ring outside the message, never colour alone. */
.message-list article[tabindex],
.dm-message-list article[tabindex] {
  scroll-margin-block: 24px;
  border-radius: 1rem;
}
.message-list article:focus-visible,
.dm-message-list article:focus-visible {
  outline: 2px solid var(--field-line);
  outline-offset: 2px;
  box-shadow: 0 0 0 2px var(--stage);
}

/* Message actions: a menu by the message, a bottom sheet on touch. */
.message-menu {
  position: fixed;
  z-index: 60;
  display: grid;
  min-width: 13rem;
  max-height: calc(100dvh - 1rem);
  padding: 0.35rem;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: var(--surface);
  box-shadow: var(--shadow-strong);
}
.message-menu-item {
  min-height: 2.75rem;
  padding: 0.55rem 0.8rem;
  border: 0;
  border-radius: 0.7rem;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: start;
}
.message-menu-item:hover,
.message-menu-item:focus-visible { background: var(--raised); outline: 2px solid var(--field-line); outline-offset: -2px; }
.message-menu-item.danger { color: var(--error); }
.message-menu-sheet {
  inset: auto 0 0 0;
  min-width: 0;
  padding: 0.75rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
  border-radius: 1.75rem 1.75rem 0 0;
}
.message-menu-sheet .message-menu-item { min-height: 3rem; }

@media (forced-colors: active) {
  .transcript-day-label { border: 1px solid CanvasText; }
  .message-list article:focus-visible,
  .dm-message-list article:focus-visible { outline: 2px solid CanvasText; }
  .message-menu { border: 1px solid CanvasText; }
}

/* ---------- S1c: the approved transcript (bubbles, runs, pebbles, chit) ---------- */
.message-list,
.dm-message-list { gap: 0.25rem; }

/* A new run, a notice or a day starts with more room; a run stays together. */
.message-list > li:has(> article[data-run="start"], > article[data-run="single"], > .ping-notice, > .room-notice, > .ignored-message),
.dm-message-list > li:has(> article[data-run="start"], > article[data-run="single"]),
.message-list > .transcript-day,
.dm-message-list > .transcript-day,
.message-list > .transcript-marker { margin-top: 0.9rem; }

.message-list .message[data-run] {
  row-gap: 0.25rem;
  padding-left: 2.75rem;
  justify-items: start;
}

/* People are pebbles in their identity hue, shown once, at the end of a run. */
.message-list .message[data-run] > .message-avatar {
  top: auto;
  bottom: 0;
  width: 2rem;
  height: 2rem;
  border: 0;
  border-radius: 50%;
  font-size: 0.72rem;
}
.message-list .message[data-run="start"] > .message-avatar,
.message-list .message[data-run="middle"] > .message-avatar { visibility: hidden; }
.message-avatar[data-hue="0"] { background: var(--hue-0); color: var(--hue-ink); }
.message-avatar[data-hue="1"] { background: var(--hue-1); color: var(--hue-ink); }
.message-avatar[data-hue="2"] { background: var(--hue-2); color: var(--hue-ink); }
.message-avatar[data-hue="3"] { background: var(--hue-3); color: var(--hue-ink); }
.message-avatar[data-hue="4"] { background: var(--hue-4); color: var(--hue-ink); }
.message-avatar[data-hue="5"] { background: var(--hue-5); color: var(--hue-ink); }
.message-avatar[data-hue="6"] { background: var(--hue-6); color: var(--hue-ink); }
.message-avatar[data-hue="7"] { background: var(--hue-7); color: var(--hue-ink); }

/* Your own messages: no name or pebble, on the right, in the outgoing colour. */
.message-list .message.mine { padding-left: 0; justify-items: end; }
.message-list .message.mine > .message-avatar { display: none; }

/* The name line: shown at the start of someone else's run. */
.message[data-run] > .message-header { width: 100%; font-size: 0.82rem; }
.message[data-run="middle"] .message-author,
.message[data-run="end"] .message-author,
.message.mine .message-author,
.message[data-run="middle"] .guest-badge,
.message[data-run="end"] .guest-badge,
.message.mine .guest-badge { display: none; }
/* No opacity on top of the muted colour: blended, it falls below 4.5:1. */
:is(.message[data-run], .dm-message) .message-edited-indicator { color: var(--text-muted); font-size: 0.72rem; opacity: 1; }

/* Actions appear on hover and focus only (never read state or typing here). */
.message[data-run] .message-react-button { order: 9; margin-left: auto; }
.message[data-run] .message-reply-button { order: 10; }
.message[data-run] .message-mutation-actions { order: 11; }
.message[data-run] .message-more-button { order: 12; }
.message[data-run]:is([data-run="middle"], [data-run="end"], .mine) > .message-header,
.dm-message > .message-header {
  position: absolute;
  z-index: 2;
  /* Clear of the bubble (Lantern Next L4): with the time out of it a one-line bubble is
     about 39px tall, and at -0.85rem this toolbar covered its middle, so a hover or a click
     on the bubble landed on Edit or Delete. It now overlaps the bubble's top edge by a few
     pixels only. */
  top: -1.6rem;
  right: 0;
  width: auto;
  padding: 0.15rem;
  border-radius: 1rem;
  background: var(--surface);
  box-shadow: 0 4px 14px var(--shadow-soft), inset 0 0 0 1px var(--line);
}
.message[data-run] .message-react-button,
.message[data-run] .message-reply-button,
.message[data-run] .message-mutation-actions,
.message[data-run] .message-more-button,
.dm-message .message-header,
.dm-message > .message-mutation-actions,
/* At rest the actions are transparent and take no click or tap (Lantern Next L4): a floating
   toolbar sits over the bubble above it, and a tap meant for that bubble must reach it. They
   stay in the accessibility tree, as before. The pointer, focus inside the message and the
   arrow keys bring them back; touch has the actions sheet (transcript-keys.js). */
:is(.message[data-run], .dm-message) .reaction-add-button { opacity: 0; pointer-events: none; transition: opacity 0.12s ease; }
.message[data-run]:is(:hover, :focus-within) .message-react-button,
:is(.message[data-run], .dm-message):is(:hover, :focus-within) .reaction-add-button,
.message[data-run]:is(:hover, :focus-within) .message-reply-button,
.message[data-run]:is(:hover, :focus-within) .message-mutation-actions,
.message[data-run]:is(:hover, :focus-within) .message-more-button,
.dm-message:is(:hover, :focus-within) .message-header,
.dm-message:is(:hover, :focus-within) > .message-mutation-actions { opacity: 1; pointer-events: auto; }
.message[data-run]:is([data-run="middle"], [data-run="end"], .mine):not(:hover, :focus-within) > .message-header { background: transparent; box-shadow: none; pointer-events: none; }
/* A floating toolbar sits above its message, so a mouse on its way there can leave the
   message for a moment (a slanting path from a bubble at one side to a toolbar at the
   other). For a short while after the pointer leaves, the toolbar's own box can still be
   hovered, which brings the message, and so the toolbar, back. Its buttons take no click
   in that while: nothing unseen can be pressed. Only where a pointer can hover: a tap
   never meets it. */
@media (hover: hover) {
  .message[data-run]:is([data-run="middle"], [data-run="end"], .mine):not(:hover, :focus-within) > .message-header,
  .dm-message:not(:hover, :focus-within) > .message-header {
    transition: opacity 0.12s ease, pointer-events 0s 0.4s;
    transition-behavior: allow-discrete;
  }
}
.dm-message:not(:hover, :focus-within) > .message-header :is(button, a) { pointer-events: none; }
/* A reaction bar with nothing in it takes no room, unless its picker is open. */
:is(.message[data-run], .dm-message) .reaction-bar:not(:has(.reaction-button)):not(:has(.reaction-picker:not([hidden]))) { display: none; }

.message-react-button,
.message-more-button {
  display: inline-grid;
  width: 2rem;
  height: 1.75rem;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 0.9rem;
  background: none;
  color: var(--text-muted);
}
.message-react-button:hover,
.message-react-button:focus-visible,
.message-more-button:hover,
.message-more-button:focus-visible { background: var(--raised); color: var(--text); }

/* The bubble: reply quote, text and time; the chit sits beside the last one of a run. */
.message-bubble {
  position: relative;
  max-width: min(100%, 42rem);
  padding: 0.55rem 0.9rem;
  border-radius: 20px;
  background: var(--raised);
  color: var(--text);
}
/* Inside your own bubble every muted text (time, "edited", "read", file
   details, reply excerpts) uses the bubble's text colour: the scheme's
   muted colour is chosen for the ground, not for --outgoing. */
.message.mine .message-bubble,
.dm-message.outgoing .message-bubble { background: var(--outgoing); color: var(--on-outgoing); --text-muted: var(--on-outgoing); }
/* The link accent doesn't reach 4.5:1 on every scheme's outgoing colour, so
   links in your own bubbles take the bubble's text colour, underlined. */
.message.mine .message-bubble a,
.dm-message.outgoing .message-bubble a { color: var(--on-outgoing); text-decoration: underline; text-underline-offset: 0.15em; }
.message-bubble .message-body,
.message-bubble .dm-message-body { line-height: 1.45; }
.message-bubble > .message-time,
.message-bubble > .dm-message-meta {
  float: inline-end;
  margin: 0.4rem 0 -0.15rem 0.75rem;
  color: var(--text-muted);
  font-size: 0.72rem;
}
.message.mine .message-bubble > .message-time,
.dm-message.outgoing .message-bubble > .dm-message-meta { color: var(--on-outgoing); }
.message-bubble > .reply-preview {
  display: grid;
  width: calc(100% + 0.6rem);
  margin: 0 -0.3rem 0.4rem;
  padding: 0.35rem 0.6rem;
  border: 0;
  border-radius: 12px;
  background: color-mix(in srgb, currentColor 8%, transparent);
  color: inherit;
  text-align: start;
}
.message:is([data-run="single"], [data-run="end"]) .message-bubble::after,
.dm-message:is([data-run="single"], [data-run="end"]) .message-bubble::after {
  position: absolute;
  bottom: -4px;
  left: -11px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: inherit;
  content: "";
}
.message.mine:is([data-run="single"], [data-run="end"]) .message-bubble::after,
.dm-message.outgoing:is([data-run="single"], [data-run="end"]) .message-bubble::after { right: -11px; left: auto; }
/* In rooms the incoming chit sits lower and nearer the bubble, so it doesn't touch the
   sender's pebble and read as a presence dot (#256). */
.message:not(.mine):is([data-run="single"], [data-run="end"]) .message-bubble::after { bottom: -8px; left: -8px; }
.message.deleted .message-bubble,
.dm-message.deleted .message-bubble { background: transparent; box-shadow: inset 0 0 0 1px var(--line); color: var(--text-muted); font-style: italic; }
.message.mine > .reaction-bar,
.message.mine > .attachment-card { justify-self: end; }

/* Direct messages: the article is now only a frame; the bubble carries the look. */
.dm-message-list > li > .dm-message,
.dm-message-list > li > .dm-message.outgoing {
  position: relative;
  max-width: min(42rem, 84%);
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
}
.dm-message-list > li > .dm-message.outgoing { justify-items: end; }

/* One image box for room and DM attachments (#158): space reserved from the image's ratio. */
.image-box {
  position: relative;
  display: block;
  overflow: hidden;
  width: min(100%, 22rem);
  aspect-ratio: 4 / 3;
  border-radius: 14px;
  background: var(--raised);
}
.image-box[data-sized="true"] {
  width: min(100%, 22rem, calc(20rem * var(--image-ratio)));
  aspect-ratio: var(--image-ratio);
}
.image-box img { display: block; width: 100%; height: 100%; object-fit: contain; }
.image-box-unavailable { display: grid; height: 100%; place-items: center; color: var(--text-muted); font-size: 0.85rem; }
.image-box-label {
  position: absolute;
  top: 0.5rem;
  left: 0.5rem;
  padding: 0.05rem 0.45rem;
  border-radius: 0.5rem;
  background: var(--surface);
  color: var(--text);
  font-size: 0.7rem;
  font-weight: 700;
}

/* Inside a bubble the file needs no frame of its own: the image box and the name and size. */
.message-bubble > .attachment-card {
  width: min(22rem, 100%);
  margin: 0 0 0.25rem;
  padding: 0;
  border: 0;
  background: none;
}
.message-bubble > .attachment-card .attachment-metadata { color: inherit; opacity: 1; }

/* The composer: a pill field and a round Send button. */
.composer { border-radius: 1.75rem; }
.send-round {
  display: inline-grid;
  width: 3rem;
  min-width: 3rem;
  height: 3rem;
  min-height: 3rem;
  place-items: center;
  padding: 0;
  border-radius: 50%;
}

@media (prefers-reduced-motion: reduce) {
  .message[data-run] .message-reply-button,
  .message[data-run] .message-mutation-actions,
  .message[data-run] .message-more-button,
  .dm-message .message-header,
  .dm-message > .message-mutation-actions,
  .message[data-run] .message-react-button,
  :is(.message[data-run], .dm-message) .reaction-add-button { transition: none; }
}
/* The moment a toolbar's box stays hoverable (above) is not motion; it stays. */
@media (prefers-reduced-motion: reduce) and (hover: hover) {
  .message[data-run]:is([data-run="middle"], [data-run="end"], .mine):not(:hover, :focus-within) > .message-header,
  .dm-message:not(:hover, :focus-within) > .message-header {
    transition: pointer-events 0s 0.4s;
    transition-behavior: allow-discrete;
  }
}
@media (forced-colors: active) {
  .message-bubble { border: 1px solid CanvasText; }
  .message-bubble::after { border: 1px solid CanvasText; }
}

/* ---------- Lantern Next L4 (#388): bubbles hold words ----------
   A run shows its time once: beside the name that starts someone else's run in a room
   (app.js draws that copy, hidden from assistive technology), or under the run's last
   bubble for your own messages and in direct conversations. Every message still carries
   its own time in its bubble (<time datetime>, in its accessible text, with the full
   moment as its title): it is taken out of sight, not out of the page, and comes back as
   a small chip on the bubble's edge under the pointer, when focus is inside the message,
   and when the arrow keys reach it. */
.message-list,
.dm-message-list { --stage-muted: var(--text-muted); }

.message-run-time {
  margin-inline-start: 0.1rem;
  color: var(--text-muted);
  font-size: var(--type-caption);
  font-weight: 400;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
/* The name line keeps one height and one line: its actions are invisible until the
   pointer or focus is on the message, so until then they take no room beside the name: they wait, out of flow, at the line's end (never over the name), taking no clicks
   and the time (at 360 CSS px they pushed "More" onto a second line); when they show,
   the time's copy steps aside for them, and the bubble's own chip gives the exact time. */
.message[data-run]:is([data-run="start"], [data-run="single"]):not(.mine) > .message-header { position: relative; min-height: 1.75rem; align-items: center; }
.message[data-run]:is([data-run="start"], [data-run="single"]):not(.mine):not(:hover, :focus-within) > .message-header > :is(.message-react-button, .message-reply-button, .message-mutation-actions, .message-more-button) { position: absolute; inset-block-start: 0; inset-inline-end: 0; }
.message[data-run]:is(:hover, :focus-within) .message-run-time { display: none; }

/* The copy belongs to the name line of someone else's run; nowhere else. */
.message[data-run="middle"] .message-run-time,
.message[data-run="end"] .message-run-time,
.message.mine .message-run-time,
.message:not([data-run]) .message-run-time { display: none; }

/* Out of sight, still in the page and in the accessibility tree. */
.message[data-run] .message-bubble > .message-time,
.dm-message .message-bubble > .dm-message-meta {
  position: absolute;
  float: none;
  width: 1px;
  height: 1px;
  margin: 0;
  overflow: hidden;
  clip-path: inset(50%);
  font-size: var(--type-caption);
  font-variant-numeric: tabular-nums;
  line-height: 1.5;
  white-space: nowrap;
}

/* The run's own time, under its last bubble: your messages in rooms, both sides in Direct. */
.message.mine:is([data-run="single"], [data-run="end"]) .message-bubble,
.dm-message:is([data-run="single"], [data-run="end"]) .message-bubble { margin-bottom: 1.3rem; }
.message.mine:is([data-run="single"], [data-run="end"]) .message-bubble > .message-time,
.dm-message:is([data-run="single"], [data-run="end"]) .message-bubble > .dm-message-meta {
  top: calc(100% + 0.25rem);
  inset-inline-start: 0.6rem;
  width: auto;
  height: auto;
  overflow: visible;
  clip-path: none;
  color: var(--stage-muted);
}
.message.mine:is([data-run="single"], [data-run="end"]) .message-bubble > .message-time,
.dm-message.outgoing:is([data-run="single"], [data-run="end"]) .message-bubble > .dm-message-meta {
  inset-inline: auto 0.6rem;
}

/* Any other message's time: a chip on the bubble's lower edge, on hover and on focus
   (the pointer, a control inside the message, or the arrow keys on the message itself).
   It overlaps; it never moves the conversation or widens the page. */
.message[data-run]:not(.mine:is([data-run="single"], [data-run="end"])):is(:hover, :focus, :focus-within) .message-bubble > .message-time,
.dm-message:not([data-run="single"], [data-run="end"]):is(:hover, :focus, :focus-within) .message-bubble > .dm-message-meta {
  z-index: 1;
  bottom: -0.6rem;
  inset-inline-end: 0.6rem;
  width: auto;
  height: auto;
  padding: 0 0.45rem;
  overflow: visible;
  clip-path: none;
  border: 1px solid transparent;
  border-radius: var(--radius-pill);
  background: var(--surface);
  color: var(--stage-muted);
  box-shadow: var(--elev-1);
}
.message[data-run]:not(.mine):is(:hover, :focus, :focus-within) .message-bubble > .message-time,
.dm-message:not(.outgoing, [data-run="single"], [data-run="end"]):is(:hover, :focus, :focus-within) .message-bubble > .dm-message-meta {
  inset-inline: 0.6rem auto;
}

/* Bubbles a little closer to their words, now that no time floats inside them. */
.message-bubble { padding: 0.5rem 0.85rem; border-radius: var(--radius-bubble); }

/* A message arriving live rises into place (transcript.js marks it). Only when motion is
   welcome; under reduced motion the rule doesn't apply at all, and --motion-base is cut. */
@keyframes message-arrive {
  from { opacity: 0; transform: translateY(0.5rem); }
}
@media (prefers-reduced-motion: no-preference) {
  /* "backwards" only: once it has arrived the item carries no transform, like every other. */
  :is(.message-list, .dm-message-list) > li.arriving { animation: message-arrive var(--motion-base) var(--ease-out) backwards; }
}

/* The composer (L4): one slim row when empty, a pill that keeps its corner as it grows. */
.composer, .dm-composer { border-radius: 1.65rem; }
.send-round {
  width: var(--target-min);
  min-width: var(--target-min);
  height: var(--target-min);
  min-height: var(--target-min);
}
