/* ==========================================================================
   Chat — a conversation: message bubbles, grouped by sender and day, and a composer.

   <div class="chat" data-moey-chat>
     <div class="chat-log">
       <div class="chat-msg"><span class="avatar">AK</span>
         <div class="chat-bubbles"><div class="chat-bubble">Hi!</div><div class="chat-meta">Ana · 9:41</div></div>
       </div>
       <div class="chat-msg chat-msg-out">
         <div class="chat-bubbles"><div class="chat-bubble">Hello 👋</div><div class="chat-meta">9:42 · Read</div></div>
       </div>
     </div>
     <form class="chat-composer"><textarea class="form-control" rows="1" placeholder="Message" aria-label="Message"></textarea>
       <button class="btn btn-default btn-icon" aria-label="Send"><i class="mi mi-send"></i></button></form>
   </div>

   --mo-chat-height (32rem) · .chat-full: the whole screen, sitting right on the phone keyboard.
   Files and pictures: see Attachments below; an <input type="file" multiple> in the composer adds the attach button.
   ========================================================================== */

.chat {
  --mo-chat-height: 32rem;
  position: relative;
  display: flex;
  flex-direction: column;
  height: var(--mo-chat-height);
  min-height: 0;
  overflow: hidden;
  background: var(--mo-bg-surface);
  border-radius: var(--mo-radius-card);
}
.chat-full { --mo-chat-height: 100dvh; border-radius: 0; }
/* While the phone keyboard is up, a full-screen chat fits exactly the part you can see */
html.mo-keyboard .chat-full { position: fixed; inset-inline: 0; top: var(--mo-vv-top, 0); z-index: var(--mo-z-fixed); height: var(--mo-vv-h, 100dvh); }

.chat-header {
  display: flex;
  flex: none;
  gap: .75rem;
  align-items: center;
  min-height: 3.5rem;
  padding: .5rem 1rem;
  border-bottom: 1px solid var(--mo-border-color);
}
.chat-header-title { flex: 1; min-width: 0; font-weight: var(--mo-font-weight-semibold); line-height: 1.25; }
.chat-header-title small { display: block; font-size: .75rem; font-weight: var(--mo-font-weight-normal); color: var(--mo-fg-3); }

.chat-log {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: .875rem;
  min-height: 0;
  padding: 1rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  outline: none;
}
.chat-log > :first-child { margin-top: auto; }       /* a short conversation starts at the bottom */
.chat-day {
  position: sticky;
  top: 0;
  z-index: 1;
  align-self: center;
  padding: .1875rem .75rem;
  font-size: .75rem;
  font-weight: var(--mo-font-weight-medium);
  color: var(--mo-fg-2);
  background: color-mix(in oklab, var(--mo-bg-2) 90%, transparent);
  backdrop-filter: blur(8px);
  border-radius: 999px;
}

.chat-msg { display: flex; gap: .5rem; align-items: flex-end; max-width: min(85%, 36rem); }
.chat-msg > .avatar { flex: none; }
.chat-msg-out { flex-direction: row-reverse; align-self: flex-end; }
.chat-bubbles { display: grid; grid-template-columns: minmax(0, 1fr); gap: 2px; justify-items: start; min-width: 0; }   /* the column can shrink: wide bubbles (players, images) fit small phones */
.chat-msg-out .chat-bubbles { justify-items: end; }
.chat-name { padding-inline: .75rem; margin-bottom: .125rem; font-size: .75rem; font-weight: var(--mo-font-weight-medium); color: var(--mo-fg-3); }
.chat-bubble {
  max-width: 100%;
  padding: .5rem .8125rem;
  font-size: .9375rem;
  line-height: 1.4;
  color: var(--mo-fg-body);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  background: var(--mo-bg-2);
  border-radius: 1.125rem;
}
.chat-msg-out .chat-bubble { color: var(--mo-default-base-contrast, var(--mo-white)); background: var(--mo-default-base); }
.chat-msg-out .chat-bubble a { color: inherit; }
/* Bubbles in a row from one person hug each other */
.chat-msg:not(.chat-msg-out) .chat-bubble:has(+ .chat-bubble) { border-end-start-radius: .375rem; }
.chat-msg:not(.chat-msg-out) .chat-bubble + .chat-bubble { border-start-start-radius: .375rem; }
.chat-msg-out .chat-bubble:has(+ .chat-bubble) { border-end-end-radius: .375rem; }
.chat-msg-out .chat-bubble + .chat-bubble { border-start-end-radius: .375rem; }
/* A voice note (or any player) in a bubble: a real width — a player sizes from its own width, a bubble from its
   content, so without one both would shrink to nothing. Never wider than the bubble on a small phone. */
.chat-bubble:has(> .player) { width: 18rem; max-width: 100%; white-space: normal; }
.chat-bubble > .player { width: 100%; }
.chat-bubble > img { display: block; max-width: 100%; margin: -.25rem -.4375rem; border-radius: .875rem; }
.chat-meta { padding-inline: .5rem; margin-top: .125rem; font-size: .6875rem; font-variant-numeric: tabular-nums; color: var(--mo-fg-3); }
.chat-meta .is-failed { color: var(--mo-red-fg); }
.chat-bubble.is-pending { opacity: .7; }

/* Attachments ------------------------------------------------------------------------------------
   A file:   <div class="chat-bubble"><a class="chat-file" href="report.pdf" download>
               <span class="chat-file-name">Quarterly report.pdf</span><span class="chat-file-meta">2.4 MB</span></a></div>
             (moey.js adds the icon for the type, keeps the extension visible when the name is cut, and the download mark)
   Pictures: <div class="chat-bubble chat-media"><a href="big.jpg"><img src="small.jpg" alt="…"></a> …</div>
             1 – 4 in a grid, "+3" on the fourth when there are more; they open in the lightbox.
   A video:  <a href="clip.mp4" data-duration="0:14"><img src="poster.jpg" alt="…"></a> inside .chat-media              */
.chat-bubble:has(> .chat-file) { width: 17rem; max-width: 100%; padding: .375rem; white-space: normal; }
.chat-bubble > .chat-file + .chat-file { margin-top: .25rem; }
.chat-file {
  position: relative;
  display: flex;
  gap: .625rem;
  align-items: center;
  min-width: 0;
  padding: .375rem .5rem .375rem .375rem;
  color: inherit;
  text-decoration: none;
  background: color-mix(in oklab, currentcolor 7%, transparent);
  border-radius: .75rem;
}
a.chat-file:hover { background: color-mix(in oklab, currentcolor 12%, transparent); }
a.chat-file:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 2px; }
.chat-file-icon {
  display: grid;
  flex: none;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  font-size: 1.25rem;
  color: var(--mo-theme-fg, var(--mo-fg-2));
  background: var(--mo-theme-bg-subtle, var(--mo-bg-3));
  border-radius: .5rem;
}
.chat-msg-out .chat-file-icon { color: var(--mo-theme-fg-emphasis, var(--mo-fg-body)); background: var(--mo-theme-subtle, var(--mo-bg-1)); }
.chat-file-body { display: grid; flex: 1; min-width: 0; line-height: 1.3; }
.chat-file-name { display: flex; min-width: 0; font-size: .875rem; font-weight: var(--mo-font-weight-medium); }
.chat-file-base { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-file-ext { flex: none; white-space: nowrap; }
.chat-file-meta { font-size: .75rem; font-variant-numeric: tabular-nums; opacity: .75; }
.chat-file-action { flex: none; font-size: 1.125rem; opacity: .75; }

/* Pictures and videos */
.chat-media {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
  width: 16rem;
  max-width: 100%;
  padding: 0;
  overflow: hidden;
  white-space: normal;
  background: transparent;
}
.chat-msg-out .chat-media { background: transparent; }
.chat-media > * { position: relative; display: block; min-width: 0; aspect-ratio: 1; overflow: hidden; background: var(--mo-bg-3); }
.chat-media > :only-child { grid-column: 1 / -1; aspect-ratio: auto; }
.chat-media:has(> :nth-child(3):last-child) > :first-child { grid-column: 1 / -1; aspect-ratio: 2; }
.chat-media img, .chat-media video { display: block; width: 100%; height: 100%; object-fit: cover; }
.chat-media > :only-child :is(img, video) { height: auto; max-height: 20rem; }
.chat-media > a:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: -3px; }
.chat-media > .chat-media-extra { display: none; }      /* past the fourth: still in the lightbox */
.chat-media > [data-more]::after {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 1.375rem;
  font-weight: var(--mo-font-weight-semibold);
  color: #fff;
  content: attr(data-more);
  background: rgb(0 0 0 / .45);
}
/* a video: a play mark and its length */
.chat-media > [data-duration]::before {
  position: absolute;
  inset-inline-start: .375rem;
  bottom: .375rem;
  z-index: 1;
  padding: .0625rem .375rem;
  font-size: .6875rem;
  font-weight: var(--mo-font-weight-medium);
  font-variant-numeric: tabular-nums;
  color: #fff;
  content: "\25B6\FE0E\00a0 " attr(data-duration);   /* FE0E: the plain triangle, never the emoji */
  background: rgb(0 0 0 / .55);
  border-radius: 999px;
}

/* Sending: a ring with the progress (a turning one until it's known) that cancels when pressed */
.chat-upload {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  background: rgb(0 0 0 / .35);
}
.chat-file > .chat-upload { position: static; flex: none; background: none; }
.chat-upload-btn {
  --p: -1;
  position: relative;
  display: grid;
  place-items: center;
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  font-size: 1rem;
  color: #fff;
  cursor: pointer;
  background: rgb(0 0 0 / .5);
  border: 0;
  border-radius: 50%;
}
.chat-file .chat-upload-btn { width: 2rem; height: 2rem; color: inherit; background: color-mix(in oklab, currentcolor 12%, transparent); }
.chat-upload-btn::before {
  position: absolute;
  inset: 0;
  content: "";
  background: conic-gradient(currentcolor calc(var(--p) * 1%), transparent 0);
  border-radius: 50%;
  mask: radial-gradient(farthest-side, transparent calc(100% - 3px), #000 calc(100% - 2.5px));
}
.chat-upload-btn.is-unknown::before { background: conic-gradient(currentcolor 25%, transparent 0); animation: mo-chat-spin .9s linear infinite; }
.chat-upload-btn:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 2px; }
@keyframes mo-chat-spin { to { rotate: 1turn; } }
.chat-bubble.is-failed :is(.chat-media > *, .chat-file) { opacity: .55; }
.chat-retry { padding: 0; margin-inline-start: .25rem; font: inherit; font-weight: var(--mo-font-weight-semibold); color: var(--mo-red-fg); text-decoration: underline; cursor: pointer; background: none; border: 0; }

/* The composer: an attach button, and what you picked, above the message box */
.chat-composer { flex-wrap: wrap; }
.chat-attach { flex: none; border-radius: 999px; }
.chat-composer > input[type="file"] { display: none; }
.chat-tray { display: flex; flex-basis: 100%; order: -1; gap: .5rem; padding: 0 0 .25rem; margin: 0; overflow-x: auto; list-style: none; scrollbar-width: none; }
.chat-tray:empty { display: none; }
.chat-tray-item {
  position: relative;
  display: flex;
  flex: none;
  gap: .5rem;
  align-items: center;
  max-width: 12rem;
  height: 3.5rem;
  padding: .375rem 2rem .375rem .375rem;
  font-size: .8125rem;
  background: var(--mo-bg-2);
  border-radius: .75rem;
}
.chat-tray-item.is-picture { width: 3.5rem; padding: 0; overflow: hidden; }
.chat-tray-item img, .chat-tray-item video { width: 100%; height: 100%; object-fit: cover; }
.chat-tray-item .chat-file-icon { width: 2.25rem; height: 2.25rem; font-size: 1.125rem; }
.chat-tray-item .chat-file-body { line-height: 1.25; }
.chat-tray-item .chat-file-name { font-size: .8125rem; }
.chat-tray-remove {
  position: absolute;
  top: .25rem;
  inset-inline-end: .25rem;
  display: grid;
  place-items: center;
  width: 1.5rem;
  height: 1.5rem;
  padding: 0;
  font-size: .875rem;
  color: #fff;
  cursor: pointer;
  background: rgb(0 0 0 / .55);
  border: 0;
  border-radius: 50%;
}
.chat-tray-remove::after { position: absolute; inset: -.5rem; content: ""; }   /* a bigger target for fingers */
.chat-tray-remove:focus-visible { outline: var(--mo-focus-ring-width) solid var(--mo-theme-focus-ring); outline-offset: 1px; }
.chat-tray-note { flex-basis: 100%; order: -1; margin: 0; font-size: .8125rem; color: var(--mo-red-fg); }
.chat-tray-note:empty { display: none; }
/* Dragging files over the chat */
.chat.is-dropping::after {
  position: absolute;
  inset: .5rem;
  z-index: 3;
  display: grid;
  place-items: center;
  font-weight: var(--mo-font-weight-semibold);
  color: var(--mo-fg-body);
  pointer-events: none;
  content: attr(data-drop-text);
  background: color-mix(in oklab, var(--mo-bg-surface) 85%, transparent);
  border: 2px dashed var(--mo-theme-border, var(--mo-border-color));
  border-radius: var(--mo-radius-card);
}
@media (prefers-reduced-motion: reduce) { .chat-upload-btn.is-unknown::before { animation-duration: 3s; } }

/* Someone is typing */
.chat-typing { display: inline-flex; gap: .25rem; align-items: center; align-self: flex-start; height: 2.25rem; padding: 0 .875rem; background: var(--mo-bg-2); border-radius: 1.125rem; }
.chat-typing > i { width: .4375rem; height: .4375rem; background: var(--mo-fg-3); border-radius: 50%; animation: mo-chat-dot 1.2s infinite ease-in-out; }
.chat-typing > i:nth-child(2) { animation-delay: .15s; }
.chat-typing > i:nth-child(3) { animation-delay: .3s; }
@keyframes mo-chat-dot { 0%, 60%, 100% { opacity: .35; translate: 0 0; } 30% { opacity: 1; translate: 0 -3px; } }

/* "New messages" — when you have scrolled up */
.chat-jump {
  position: sticky;
  bottom: 0;
  z-index: 1;
  display: inline-flex;
  flex: none;
  gap: .375rem;
  align-items: center;
  align-self: center;
  padding: .375rem .875rem;
  font: inherit;
  font-size: .8125rem;
  font-weight: var(--mo-font-weight-medium);
  color: var(--mo-default-base-contrast, var(--mo-white));
  cursor: pointer;
  background: var(--mo-default-base);
  border: 0;
  border-radius: 999px;
  box-shadow: var(--mo-shadow-overlay);
}
.chat-jump[hidden] { display: none; }

/* The composer */
.chat-composer {
  display: flex;
  flex: none;
  gap: .5rem;
  align-items: flex-end;
  padding: .625rem .75rem calc(.625rem + env(safe-area-inset-bottom));
  margin: 0;
  background: var(--mo-bg-surface);
  border-top: 1px solid var(--mo-border-color);
}
html.mo-keyboard .chat-full .chat-composer { padding-bottom: .625rem; }   /* the keyboard covers the home bar */
.chat-composer > textarea.form-control { flex: 1; min-height: var(--mo-control-height); max-height: 9.5rem; resize: none; border-radius: 1.25rem; }
.chat-composer > .btn { flex: none; border-radius: 999px; }

@media (prefers-reduced-motion: reduce) { .chat-typing > i { animation: none; opacity: .6; } }
