/* ---------- Messages pages (#/messages, #/inbox, #/messages/<id>; js/messages-page.js). Prefix ms-. ----------
   - Below a 1200px window: the list and the conversation are separate screens. On a phone (below 860px) the conversation
     is full-bleed, hides the bottom tab bar and the footer (body.ms-thread-mode), and keeps its header and message box
     stuck to the top and bottom of the screen.
   - From a 1200px window (.ms-wide, MS_WIDE_QUERY in the JS): two panes in one bordered box that fills the window height;
     the list and the messages each scroll inside it. */

.ms-page { display: flex; flex-direction: column; gap: 12px; }

/* ---------- List pane ---------- */
.ms-list-pane { display: flex; flex-direction: column; min-width: 0; }
.ms-list-head { display: flex; flex-direction: column; gap: 12px; padding-bottom: 12px; }
.ms-title-row { display: flex; align-items: baseline; gap: 8px; }
.ms-h1 { margin: 0; }
.ms-count { font-size: 14px; color: var(--text-muted); }

.ms-switch { display: flex; padding: 4px; background: var(--surface-2); border-radius: var(--radius-lg); }
.ms-switch a {
  flex: 1; min-height: 40px; display: flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 9px; color: var(--text-muted); font-size: 14px; font-weight: 600; text-decoration: none;
}
.ms-switch a.ms-on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08); }
.ms-badge { min-width: 20px; height: 20px; padding: 0 6px; box-sizing: border-box; border-radius: 999px; background: var(--brand); color: var(--on-brand); font-size: 12px; display: inline-flex; align-items: center; justify-content: center; }

.ms-search {
  height: 44px; display: flex; align-items: center; gap: 8px; padding: 0 12px; box-sizing: border-box;
  border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); color: var(--text-muted);
}
.ms-search input { flex: 1; min-width: 0; border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; font-size: 16px; }
.ms-search:focus-within { border-color: var(--focus-ring); box-shadow: 0 0 0 2px var(--focus-ring); }

.ms-chips { display: flex; gap: 8px; flex-wrap: wrap; }
.ms-chips .chip { min-height: 44px; padding: 0 14px; font-size: 14px; font-weight: 600; background: var(--surface); }
.ms-chips .chip.active { background: var(--brand); color: var(--on-brand); border-color: var(--brand); }
.ms-chip-count { font-weight: 500; opacity: 0.8; }

.ms-list { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; }
.ms-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px; min-height: 76px; box-sizing: border-box; color: var(--text); text-decoration: none; border-bottom: 1px solid var(--border); }
.ms-row:last-child { border-bottom: 0; }
.ms-row:hover { background: var(--surface-2); }
.ms-row:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: -2px; }
.ms-row.ms-selected { background: var(--brand-tint); box-shadow: inset 3px 0 0 var(--brand-text); }
.ms-row-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.ms-row-top, .ms-row-bottom { display: flex; align-items: center; gap: 8px; min-width: 0; }
.ms-row-name { flex: 1; min-width: 0; font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-row-time { flex: none; font-size: 12px; color: var(--text-muted); }
.ms-row-context { font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-row-preview { flex: 1; min-width: 0; font-size: 14px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-unread .ms-row-name { font-weight: 700; }
.ms-unread .ms-row-time { color: var(--brand-text); font-weight: 700; }
.ms-unread .ms-row-preview { color: var(--text); font-weight: 600; }
.ms-dot { flex: none; width: 10px; height: 10px; border-radius: 999px; background: var(--brand-text); }
.ms-label { flex: none; display: inline-block; font-size: 12px; font-weight: 600; color: var(--text-muted); border: 1px solid var(--border); border-radius: 999px; padding: 1px 8px; vertical-align: middle; }

.ms-avatar {
  flex: none; width: 48px; height: 48px; border-radius: 999px; display: flex; align-items: center; justify-content: center;
  color: #fff; overflow: hidden;
}
.ms-avatar svg { width: 22px; height: 22px; }
.ms-avatar-initials { background: var(--brand); color: var(--on-brand); font-weight: 700; font-size: 16px; }
.ms-avatar-sm { width: 40px; height: 40px; }
.ms-avatar-sm.ms-avatar-initials { font-size: 15px; }

.ms-empty { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; padding: 24px 16px; }
.ms-empty p { margin: 0; font-size: 15px; line-height: 1.45; }

/* ---------- Conversation ---------- */
.ms-thread-pane { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.ms-thread-head {
  display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.ms-back { flex: none; width: 44px; height: 44px; margin-left: -8px; display: flex; align-items: center; justify-content: center; color: var(--text); border-radius: 999px; }
.ms-head-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.ms-head-title { margin: 0; font-size: 16px; font-weight: 700; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-head-link { align-self: flex-start; max-width: 100%; display: inline-flex; font-size: 13px; font-weight: 600; color: var(--brand-text); text-decoration: none; padding: 4px 0; }
.ms-head-link > span { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-head-link:hover { text-decoration: underline; }
.ms-head-meta { font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ms-head-meta strong { color: var(--text); }
.ms-head-btn { flex: none; display: none; min-height: 40px; padding: 0 14px; align-items: center; border: 1px solid var(--border); border-radius: 999px; color: var(--text); font-size: 14px; font-weight: 600; text-decoration: none; }
.ms-menu .row-menu-btn { width: 44px; height: 44px; color: var(--text); }
.ms-menu .row-menu-btn::after { content: none; }
.ms-menu .row-menu-dropdown button { min-height: 44px; font-size: 14px; }
.ms-notice { margin: 0; padding: 10px 16px; font-size: 14px; background: var(--warning-bg); color: var(--warning-text); }

.ms-msgs { flex: 1; display: flex; flex-direction: column; gap: 6px; padding: 12px 16px 16px; }
.ms-thread-start { margin: auto; text-align: center; color: var(--text-muted); font-size: 15px; }
.ms-thread-start p { margin: 0; }
.ms-day { align-self: center; margin: 10px 0 4px; }
.ms-day span { display: inline-block; padding: 4px 12px; border-radius: 999px; background: var(--surface-2); color: var(--text-muted); font-size: 12px; font-weight: 600; }
.ms-msg { max-width: min(80%, 560px); display: flex; flex-direction: column; gap: 4px; padding: 10px 12px; box-sizing: border-box; }
.ms-msg p { margin: 0; font-size: 15px; line-height: 1.4; white-space: pre-wrap; overflow-wrap: anywhere; }
.ms-mine { align-self: flex-end; background: var(--brand-tint); border-radius: 16px 16px 4px 16px; }
.ms-theirs { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-radius: 16px 16px 16px 4px; }
.ms-mine.ms-more { border-bottom-right-radius: 16px; }
.ms-theirs.ms-more { border-bottom-left-radius: 16px; }
.ms-cont { margin-top: -3px; }
.ms-mine.ms-cont { border-top-right-radius: 6px; }
.ms-theirs.ms-cont { border-top-left-radius: 6px; }
.ms-sending { opacity: 0.7; }
.ms-failed { border: 1px solid var(--danger-text); }
.ms-msg-time { font-size: 12px; color: var(--text-muted); }
.ms-mine .ms-msg-time { align-self: flex-end; }
.ms-owner { font-size: 12px; font-weight: 700; color: var(--brand-text); }
.ms-re { align-self: flex-start; font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 999px; background: var(--surface); color: var(--brand-text); border: 1px solid var(--border); text-decoration: none; }
.ms-msg-failed { align-self: flex-end; display: flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; color: var(--danger-text); }
.ms-retry { min-height: 44px; margin: -14px -4px -14px 0; padding: 0 6px; border: 0; background: transparent; color: var(--brand-text); font: inherit; font-weight: 700; cursor: pointer; }

.ms-compose { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; background: var(--surface); border-top: 1px solid var(--border); }
.ms-compose-field { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.ms-compose textarea {
  width: 100%; box-sizing: border-box; min-height: 48px; max-height: 160px; resize: none; display: block;
  border: 1px solid var(--border); border-radius: 24px; padding: 12px 16px; background: var(--bg); color: var(--text);
  font: inherit; font-size: 16px; line-height: 1.4;
}
.ms-compose textarea:focus { outline: 0; border-color: var(--focus-ring); box-shadow: 0 0 0 2px var(--focus-ring); }
.ms-compose-hint { display: none; font-size: 12px; color: var(--text-muted); padding-left: 16px; }
.ms-send {
  flex: none; width: 48px; height: 48px; border-radius: 999px; border: 0; background: var(--brand); color: var(--on-brand);
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.ms-send:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 2px; }
@media (hover: hover) and (pointer: fine) { .ms-compose-hint { display: block; } .ms-send { margin-bottom: 21px; } }

/* One screen per step (below 1200px): the conversation fills the screen height, header and message box stuck. */
.ms-thread-only .ms-thread-pane { min-height: calc(100dvh - var(--topbar-h, 96px) - 60px); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: clip; }
.ms-thread-only .ms-thread-head { position: sticky; top: var(--topbar-h, 96px); z-index: 5; }
.ms-thread-only .ms-compose { position: sticky; bottom: 0; z-index: 5; }

@media (max-width: 859px) {
  body.ms-thread-mode { padding-bottom: 0; }
  body.ms-thread-mode .bottom-nav, body.ms-thread-mode > footer { display: none; }
  .ms-thread-only { margin: -16px -16px -32px; }
  .ms-thread-only .ms-thread-pane { min-height: calc(100dvh - var(--topbar-h, 96px)); border: 0; border-radius: 0; }
  .ms-compose { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .ms-list { margin: 0 -16px; border-radius: 0; border-left: 0; border-right: 0; }
}

/* ---------- Two panes (1200px window and wider) ---------- */
@media (min-width: 1200px) {
  .ms-wide {
    flex-direction: row; gap: 0; height: calc(100dvh - var(--topbar-h, 96px) - 48px); min-height: 520px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 16px; overflow: hidden;
  }
  .ms-wide .ms-list-pane { flex: 0 0 360px; border-right: 1px solid var(--border); }
  .ms-wide .ms-list-head { padding: 16px 14px 12px; border-bottom: 1px solid var(--border); }
  .ms-wide .ms-list { flex: 1; overflow-y: auto; border: 0; border-radius: 0; }
  .ms-wide .ms-thread-pane { flex: 1 1 auto; overflow: hidden; }
  .ms-wide .ms-thread-head { padding: 12px 12px 12px 20px; }
  .ms-wide .ms-head-title { font-size: 17px; }
  .ms-wide .ms-head-btn { display: inline-flex; }
  .ms-wide .ms-msgs { overflow-y: auto; padding: 16px 24px; }
  .ms-thread-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--text-muted); font-size: 15px; }
  .ms-thread-empty p { margin: 0; }
}

/* Tap areas of at least 44px for the two small links (docs/DESIGN-RULES.md rule 1), without changing how they look. */
.ms-head-link, .ms-re { position: relative; z-index: 1; } /* above the title and the message text their tap areas overlap */
.ms-head-link::after { content: ""; position: absolute; left: 0; right: 0; top: -8px; bottom: -8px; }
.ms-re::after { content: ""; position: absolute; left: 0; right: 0; top: -10px; bottom: -10px; }
