/* ==========================================================================
   IRONITT v2 — Whispers (direct messages)
   --------------------------------------------------------------------------
   The conversation itself: header extras, bubbles, replies, shared posts,
   Notes, the phone's whisper list and the avatar strip shown when the
   chats column is closed. The frame (header bar, composer) is in
   communication.css; the call stage stays with the voice surface.

   Bubbles are the one place colour fills a shape in chat: yours sit on the
   accent's softest step, theirs on the surface fill. Nothing floats here,
   so nothing has a shadow, and nothing animates in on render (re-renders
   would replay it for every bubble).
   JS contracts kept: .own/.other and .cluster-start/.cluster-end on rows;
   .visible on the jump button; .mobile-dm-list-open on .dms-panel;
   .reply-bar is rebuilt by _renderReplyBar for channels and whispers.
   ========================================================================== */

/* ---- Layout ------------------------------------------------------------ */
.dm-chat-main,
.dm-messages-area,
.dm-messages-wrapper {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.dm-conversation-col {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 1100px;
  min-width: 0;
  min-height: 0;
  margin: 0 auto;
}
.dm-messages-area.with-thread { flex-direction: row; gap: var(--space-sm); min-width: 0; }
.dm-messages-area.with-thread > .thread-panel { flex: 0 0 clamp(320px, 38%, 420px); min-width: 0; }

/* ---- Header extras ------------------------------------------------------ */
.dm-thread-recipient {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  color: var(--text-primary);
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dm-thread-header .iron-ai-trigger,
.dm-call-start-btn { height: 34px; }
.dm-call-start-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
  padding: 0 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  cursor: pointer;
  transition: color 150ms ease, border-color 150ms ease, background-color 150ms ease;
}
.dm-call-start-btn:hover { border-color: var(--border-strong); background: var(--bg-hover); color: var(--text-primary); }
.dm-call-start-btn:focus-visible { outline: 0; box-shadow: var(--focus-ring); }
.dm-mobile-list-back { display: none; }

/* ---- Bubbles ------------------------------------------------------------ */
.dm-bubble-messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  gap: 4px;
  padding: var(--space-md) var(--space-lg);
  overflow-y: auto;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

.dm-bubble-row { display: flex; align-items: flex-end; gap: var(--space-sm); max-width: 72%; }
.dm-bubble-row.own { align-self: flex-end; flex-direction: row-reverse; }
.dm-bubble-row.other { align-self: flex-start; }
.dm-bubble-row.cluster-end { margin-bottom: var(--space-sm); }

.dm-bubble-stack { display: flex; flex-direction: column; min-width: 0; max-width: 100%; }
.dm-bubble-row.own .dm-bubble-stack { align-items: flex-end; }
.dm-bubble-row.other .dm-bubble-stack { align-items: flex-start; }

.dm-bubble-avatar {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  margin-bottom: 22px;           /* sits beside the bubble, above the time */
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  font-size: var(--font-size-2xs);
  font-weight: var(--font-weight-semibold);
}
.dm-bubble-avatar-spacer { flex: 0 0 28px; width: 28px; }

.dm-bubble {
  position: relative;
  min-width: 0;
  max-width: 100%;
  padding: 9px 14px;
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  font-size: var(--font-size-sm);
  line-height: 1.5;
  overflow-wrap: anywhere;
}
.dm-bubble.own { background: var(--accent-subtle); }
.dm-bubble.other { background: var(--bg-secondary); }
/* A cluster reads as one block: the corners that face each other tighten. */
.dm-bubble-row.own:not(.cluster-start) .dm-bubble { border-top-right-radius: var(--radius-sm); }
.dm-bubble-row.own:not(.cluster-end) .dm-bubble { border-bottom-right-radius: var(--radius-sm); }
.dm-bubble-row.other:not(.cluster-start) .dm-bubble { border-top-left-radius: var(--radius-sm); }
.dm-bubble-row.other:not(.cluster-end) .dm-bubble { border-bottom-left-radius: var(--radius-sm); }
.thread-root .dm-bubble { max-width: 100%; }

.dm-bubble-content strong { color: var(--text-primary); font-weight: var(--font-weight-semibold); }
.dm-bubble-content del { opacity: 0.6; }
.dm-bubble-content--embed { padding: 0; background: transparent; }
.dm-bubble .reaction-row { margin-top: var(--space-xs); }
.dm-bubble:hover .reaction-add-btn { opacity: 1; }

.dm-bubble-time { margin-top: 4px; color: var(--text-muted); font-size: var(--font-size-2xs); font-variant-numeric: tabular-nums; }
.dm-bubble-row.own .dm-bubble-time { text-align: right; }
.dm-edited-tag { color: var(--text-muted); font-size: var(--font-size-2xs); }

.dm-bubble-menu-btn {
  position: absolute;
  top: 4px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-round);
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-size: 11px;
  opacity: 0;
  cursor: pointer;
  transition: opacity 150ms ease, color 150ms ease;
}
.dm-bubble:hover .dm-bubble-menu-btn,
.dm-bubble-menu-btn:focus-visible { opacity: 1; }
.dm-bubble-menu-btn:hover { color: var(--text-primary); }
@media (hover: none) { .dm-bubble-menu-btn { display: none; } }

/* A quoted message inside a reply. */
.dm-reply-quote {
  display: grid;
  gap: 1px;
  margin-bottom: 6px;
  padding: 4px 10px;
  border-left: 2px solid var(--accent-color);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  background: var(--bg-hover);
  font-size: var(--font-size-xs);
  line-height: 1.4;
}
.dm-reply-quote-author { color: var(--accent-color); font-size: var(--font-size-2xs); font-weight: var(--font-weight-semibold); }
.dm-reply-quote-text { overflow: hidden; color: var(--text-muted); text-overflow: ellipsis; white-space: nowrap; }

/* ---- Replying to … (channels and whispers) ------------------------------ */
/* Sits on top of the round composer as one piece with it. */
.chat-input-area .reply-bar {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin: 0 var(--space-md) -1px;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--border-color);
  border-bottom: 0;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
}
.reply-bar > i { flex-shrink: 0; font-size: 12px; }
.reply-bar-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar-text strong { color: var(--text-primary); font-weight: var(--font-weight-semibold); }
.reply-bar-close {
  display: grid;
  place-items: center;
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: var(--radius-round);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background-color 150ms ease, color 150ms ease;
}
.reply-bar-close:hover { background: var(--bg-hover); color: var(--text-primary); }

/* The field inside the composer never draws its own ring; the round field
   shows focus as a whole. */
.chat-input-wrapper input:focus,
.chat-input-wrapper input:focus-visible { outline: 0; box-shadow: none; }
.chat-input-wrapper:focus-within { border-color: var(--accent-color); }

/* ---- Jump to latest (floats over the messages) -------------------------- */
.dm-scroll-bottom-btn {
  position: absolute;
  bottom: 12px;
  left: 50%;
  z-index: 10;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-round);
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: 14px;
  box-shadow: var(--shadow-md);
  opacity: 0;
  pointer-events: none;
  cursor: pointer;
  transform: translate(-50%, 6px);
  transition: opacity 180ms ease, transform 180ms ease;
}
.dm-scroll-bottom-btn.visible { opacity: 1; pointer-events: auto; transform: translate(-50%, 0); }
.dm-scroll-bottom-btn:hover { color: var(--text-primary); }

/* ---- A post shared into a whisper -------------------------------------- */
.dm-post-embed {
  display: grid;
  gap: var(--space-sm);
  max-width: 380px;
  margin-top: var(--space-xs);
  padding: var(--space-md);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  background: var(--bg-secondary);
  cursor: pointer;
  transition: border-color 150ms ease;
}
.dm-post-embed:hover { border-color: var(--border-strong); }
.dm-post-embed-header { display: flex; align-items: center; gap: var(--space-sm); min-width: 0; }
.dm-post-embed-avatar { flex-shrink: 0; width: 36px; height: 36px; font-size: var(--font-size-sm); }
.dm-post-embed-meta { flex: 1; min-width: 0; }
.dm-post-embed-author { overflow: hidden; color: var(--text-primary); font-size: var(--font-size-sm); font-weight: var(--font-weight-semibold); text-overflow: ellipsis; white-space: nowrap; }
.dm-post-embed-sub { display: flex; align-items: center; gap: 6px; margin-top: 2px; color: var(--text-muted); font-size: var(--font-size-xs); }
.dm-post-embed-sub i { color: var(--accent-color); font-size: var(--font-size-2xs); }
.dm-post-embed-content {
  overflow: hidden;
  color: var(--text-primary);
  font-size: var(--font-size-sm);
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 5;
  -webkit-box-orient: vertical;
}
.dm-post-embed-footer { display: flex; justify-content: flex-end; }
.dm-post-embed-open {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--text-primary);
  font-size: var(--font-size-xs);
  font-weight: var(--font-weight-semibold);
  cursor: pointer;
  transition: background-color 150ms ease, color 150ms ease;
}
.dm-post-embed-open:hover,
.dm-post-embed:hover .dm-post-embed-open { background: var(--bg-hover); color: var(--accent-color); }

/* ---- Notes: flat cards, full width -------------------------------------- */
.dm-bubble.note-rect {
  align-self: stretch;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 100%;
  padding: var(--space-md);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  background: var(--bg-secondary);
  transition: border-color 150ms ease;
}
.dm-bubble.note-rect:hover,
.dm-bubble.note-rect.pinned { border-color: var(--border-strong); }
.dm-bubble.note-rect .dm-bubble-menu-btn,
.dm-bubble.note-rect .dm-bubble-time { display: none; }

/* ---- Empty and loading -------------------------------------------------- */
.dm-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  height: 100%;
  color: var(--text-muted);
  font-size: var(--font-size-sm);
  text-align: center;
}
.dm-empty i { color: var(--text-muted); font-size: var(--font-size-lg); }
.dm-empty p { max-width: 40ch; margin: 0; line-height: 1.5; }
.dm-empty b { color: var(--text-primary); }

.dm-loading-state { display: flex; flex-direction: column; gap: var(--space-md); padding: var(--space-lg); pointer-events: none; }
.dm-loading-row { display: flex; align-items: center; gap: var(--space-sm); }
.dm-loading-row.own { justify-content: flex-end; }
.dm-loading-row .sk { background: var(--bg-tertiary); }
.dm-loading-row .sk-icon { width: 28px; height: 28px; flex-shrink: 0; border-radius: var(--radius-round); }
.dm-loading-row .sk-line { height: 34px; max-width: min(360px, 60vw); border-radius: var(--radius-lg); }

/* ---- Avatar strip (chats column closed) --------------------------------- */
.dm-avatar-strip {
  display: flex;
  flex-shrink: 0;
  align-items: flex-start;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg) var(--space-sm);
  border-bottom: 1px solid var(--border-color);
  overflow-x: auto;
  scrollbar-width: none;
}
.dm-avatar-strip::-webkit-scrollbar { display: none; }
.dm-strip-item { display: flex; flex-direction: column; flex-shrink: 0; align-items: center; gap: 4px; cursor: pointer; }
.dm-strip-text { display: flex; flex: 1; flex-direction: column; min-width: 0; }
.dm-strip-avatar {
  position: relative;
  width: 48px;
  height: 48px;
  border: 2px solid transparent;
  background: var(--bg-secondary);
  color: var(--accent-color);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-bold);
  transition: border-color 150ms ease;
}
.dm-strip-item:hover .dm-strip-avatar { border-color: var(--border-strong); }
.dm-strip-item.active .dm-strip-avatar { border-color: var(--accent-color); }
.dm-strip-item.notes-dm .dm-strip-avatar { color: var(--text-secondary); }
.dm-strip-item.notes-dm .dm-strip-avatar i { font-size: 18px; }
.dm-strip-name { max-width: 64px; overflow: hidden; color: var(--text-muted); font-size: var(--font-size-2xs); text-align: center; text-overflow: ellipsis; white-space: nowrap; }
.dm-strip-item.active .dm-strip-name { color: var(--text-primary); font-weight: var(--font-weight-semibold); }
.dm-strip-status { display: none; max-width: 100%; color: var(--text-muted); font-size: var(--font-size-2xs); }
.dm-strip-status.has-emoji,
.dm-strip-status.has-text,
.dm-strip-status.has-activity { display: inline-flex; }
.dm-strip-status .status-text:not(:empty) { display: inline; }
.dm-strip-unread {
  position: absolute;
  top: -4px;
  right: -4px;
  display: grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border: 2px solid var(--bg-primary);
  border-radius: var(--radius-pill);
  background: var(--accent-color);
  color: var(--accent-text);
  font-size: var(--font-size-2xs);
  font-weight: var(--font-weight-bold);
}

/* ---- Phones: the whisper list, then one conversation -------------------- */
.dm-mobile-list { display: none; }
@media (max-width: 768px) {
  .dm-avatar-strip { display: none; }
  .dms-panel.mobile-dm-list-open .dm-mobile-list { display: flex; }
  .dms-panel.mobile-dm-list-open .dm-chat-main { display: none; }

  .dm-mobile-list {
    flex: 1;
    flex-direction: column;
    min-height: 0;
    padding: var(--space-sm) var(--space-md) 0;
    overflow: hidden;
  }
  .dm-mobile-list-header { display: grid; flex-shrink: 0; gap: var(--space-md); padding: var(--space-sm) 0 var(--space-md); }
  .dm-mobile-list-heading { display: flex; align-items: center; gap: var(--space-sm); }
  .dm-mobile-list-heading h2 { margin: 0; color: var(--text-primary); font-family: var(--font-display); font-size: var(--font-size-xl); font-weight: var(--font-weight-bold); }
  .dm-mobile-list-count { color: var(--text-muted); font-size: var(--font-size-sm); font-variant-numeric: tabular-nums; }
  .dm-mobile-list-search {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    height: 44px;
    padding: 0 14px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-pill);
    background: var(--bg-secondary);
    color: var(--text-muted);
    transition: border-color 150ms ease;
  }
  .dm-mobile-list-search:focus-within { border-color: var(--accent-color); }
  .dm-mobile-list-search input { flex: 1; min-width: 0; padding: 0; border: 0; outline: 0; background: transparent; color: var(--text-primary); font: inherit; font-size: var(--font-size-md); }
  .dm-mobile-list-search input:focus-visible { box-shadow: none; }
  .dm-mobile-list-search input::placeholder { color: var(--text-muted); }

  .dm-mobile-list-rows {
    display: flex;
    flex-direction: column;
    min-height: 0;
    margin: 0 calc(-1 * var(--space-md));
    padding: 0 0 var(--space-xl);
    overflow-y: auto;
    scroll-behavior: smooth;
    overscroll-behavior-y: contain;
    touch-action: pan-y;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .dm-mobile-list-rows::-webkit-scrollbar { display: none; }
  .dm-mobile-list-item {
    display: flex;
    flex: 0 0 auto;
    flex-direction: row;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 68px;
    padding: 10px var(--space-md);
    border: 0;
    background: transparent;
    color: var(--text-primary);
    font: inherit;
    text-align: left;
    transition: background-color 150ms ease;
  }
  .dm-mobile-list-item:active { background: var(--bg-tertiary); }
  .dm-mobile-list-item:focus-visible { background: var(--bg-hover); outline: 0; }
  .dm-mobile-list-item[hidden] { display: none; }
  .dm-mobile-list-avatar {
    position: relative;
    border: 0;
    flex: 0 0 48px;
    width: 48px;
    height: 48px;
    background: var(--bg-tertiary);
    color: var(--accent-color);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-bold);
  }
  .dm-mobile-list-avatar .status-dot { right: 0; bottom: 0; border-color: var(--bg-primary); }
  .dm-mobile-list-copy { display: grid; flex: 1; gap: 2px; min-width: 0; }
  .dm-mobile-list-top,
  .dm-mobile-list-bottom { display: flex; gap: var(--space-sm); min-width: 0; }
  .dm-mobile-list-top { align-items: baseline; }
  .dm-mobile-list-bottom { align-items: center; }
  .dm-mobile-list-name,
  .dm-mobile-list-preview { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .dm-mobile-list-name { color: var(--text-primary); font-size: var(--font-size-md); font-weight: var(--font-weight-semibold); }
  .dm-mobile-list-preview,
  .dm-mobile-list-time { color: var(--text-muted); font-size: var(--font-size-xs); }
  .dm-mobile-list-time { flex: 0 0 auto; font-variant-numeric: tabular-nums; }
  .dm-mobile-list-unread {
    display: grid;
    place-items: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: var(--radius-pill);
    background: var(--accent-color);
    color: var(--accent-text);
    font-size: var(--font-size-2xs);
    font-weight: var(--font-weight-bold);
  }

  .dm-mobile-list-back {
    display: grid;
    place-items: center;
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: var(--radius-round);
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    transition: background-color 150ms ease;
  }
  .dm-mobile-list-back:hover,
  .dm-mobile-list-back:focus-visible { background: var(--bg-hover); outline: 0; }
  .communication-view:not(.comm-menu-open) .comm-back-btn.dm-detail-open { display: none; }

  /* Opening a conversation: a short fade, once. */
  .dm-chat-main.dm-mobile-conversation-enter { animation: dm-mobile-conversation-enter 180ms ease both; }

  .chat-input-area .reply-bar { margin: 0 var(--space-sm) -1px; }
}
@media (max-width: 900px) {
  .dm-messages-area.with-thread { gap: 0; }
  .dm-messages-area.with-thread > .dm-conversation-col { display: none; }
  .dm-messages-area.with-thread > .thread-panel { flex: 1 1 auto; border-left: 0; }
}
@media (max-width: 640px) {
  .dm-bubble-messages { padding: var(--space-sm) var(--space-md); }
  .dm-bubble-row { max-width: 88%; }
}

/* The name is a JS contract: the animationend handler removes the class. */
@keyframes dm-mobile-conversation-enter { from { opacity: 0.72; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .dm-chat-main.dm-mobile-conversation-enter { animation: none; }
  .dm-mobile-list-rows { scroll-behavior: auto; }
  .dm-scroll-bottom-btn { transition: opacity 120ms ease; }
}
