/* Direct messages UI */
.msg-container { max-width: 720px; }

.msg-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.msg-header h1 { margin: 0; }
.msg-header-actions { display: flex; gap: 8px; flex: none; }
.msg-header-actions .btn {
    padding: 9px 16px; font-size: 0.88rem; font-weight: 600; border-radius: 20px;
    line-height: 1; white-space: nowrap; cursor: pointer; transition: background 0.15s;
}
#newGroupBtn { background: var(--white); color: var(--tl-blue); border: 1.5px solid var(--tl-blue); }
#newGroupBtn:hover { background: rgba(0, 75, 135, 0.07); }
#newMsgBtn { background: #004B87; color: #fff; border: 1.5px solid #004B87; }
#newMsgBtn:hover { background: #00396a; }
@media (max-width: 480px) {
    .msg-header { flex-wrap: wrap; }
    .msg-header h1 { font-size: 1.6rem; }
    .msg-header-actions .btn { padding: 8px 13px; font-size: 0.82rem; }
}

/* Group chat bits */
.msg-avatar-group { background: #2D5E2A; color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 1rem; }
.msg-avatar-support { background: #004B87; color: #fff; display: inline-flex; align-items: center; justify-content: center; font-size: 1rem; }
.msg-conv-note {
    background: var(--gray-100); border: 1px solid var(--gray-200); color: var(--gray-700);
    border-radius: 10px; padding: 10px 13px; font-size: 0.85rem; line-height: 1.4; margin: 10px 2px 4px;
}
.msg-bubble-sender { font-size: 0.72rem; font-weight: 700; color: var(--green-forest, #2D5E2A); margin: 0 4px 2px; }
.msg-picker-item.selected { background: rgba(0, 75, 135, 0.08); }
.msg-picker-check { margin-left: auto; color: #004B87; font-weight: 700; min-width: 18px; text-align: center; }
.msg-picker-footer { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 4px 2px; border-top: 1px solid var(--gray-200, #e5e2da); margin-top: 4px; }
.msg-picker-count { font-size: 0.85rem; color: var(--gray-500, #8C8578); }

.msg-empty { color: var(--gray-500, #8C8578); font-size: 0.92rem; padding: 24px 4px; text-align: center; }
.msg-retry-btn { background: #004B87; color: #fff; border: none; border-radius: 8px; padding: 8px 16px; font-size: 0.9rem; font-weight: 600; cursor: pointer; margin-top: 6px; }
.msg-retry-btn:hover { background: #00396a; }

/* --- Inbox list --- */
.msg-conv-list { display: flex; flex-direction: column; }
.msg-conv-row {
    display: flex; align-items: center; gap: 12px; width: 100%;
    background: none; border: none; border-bottom: 1px solid var(--gray-200, #eee);
    padding: 14px 6px; cursor: pointer; text-align: left; font-family: inherit;
}
.msg-conv-row:hover { background: var(--gray-100, #f6f4ef); }
.msg-avatar {
    flex: none; width: 42px; height: 42px; border-radius: 50%;
    background: #004B87; color: #fff; display: flex; align-items: center; justify-content: center;
    font-family: 'Oswald', sans-serif; font-weight: 600; font-size: 1.05rem;
}
.msg-conv-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.msg-conv-name { font-weight: 700; color: var(--gray-900, #1E1B16); font-size: 0.95rem; }
.msg-conv-preview {
    color: var(--gray-500, #8C8578); font-size: 0.86rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.msg-conv-meta { flex: none; display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.msg-conv-time { color: var(--gray-500, #9ca3af); font-size: 0.76rem; }
.msg-unread-dot { width: 10px; height: 10px; border-radius: 50%; background: #BA272C; }
.msg-conv-row.is-unread .msg-conv-name,
.msg-conv-row.is-unread .msg-conv-preview { color: var(--gray-900, #1E1B16); font-weight: 700; }

/* --- Conversation view --- */
/* Height is set precisely in JS (sizeConvView) to fill from the view's top to
   the bottom of the screen; this calc is only a pre-JS fallback. 100dvh tracks
   the iOS dynamic toolbar so the compose bar can't hide behind it. */
#convView { display: flex; flex-direction: column; height: calc(100dvh - 220px); min-height: 320px; }
/* An id display rule outranks the UA [hidden] rule, so hide explicitly. */
#convView[hidden], #inboxView[hidden] { display: none; }
.msg-conv-header { display: flex; align-items: center; gap: 10px; padding-bottom: 12px; border-bottom: 1px solid var(--gray-200, #eee); }
.msg-back { background: none; border: none; font-size: 1.5rem; line-height: 1; color: var(--tl-blue); cursor: pointer; padding: 0 4px; }
.msg-conv-title { flex: 1; font-family: 'Oswald', sans-serif; font-weight: 600; font-size: 1.15rem; color: var(--brown-dark, #3E2E18); }
.msg-conv-menu { flex: none; background: none; border: none; font-size: 1.5rem; line-height: 1; color: var(--gray-500, #8C8578); cursor: pointer; padding: 0 6px; }
.msg-conv-menu-pop { position: absolute; right: 8px; top: 40px; z-index: 50; background: var(--white); border: 1px solid var(--gray-200, #e5e2da); border-radius: 10px; box-shadow: 0 6px 20px rgba(0,0,0,0.25); overflow: hidden; min-width: 150px; }
.msg-conv-menu-pop button { display: block; width: 100%; text-align: left; background: none; border: none; padding: 11px 14px; font-size: 0.9rem; color: var(--gray-900, #1E1B16); cursor: pointer; }
.msg-conv-menu-pop button:hover { background: var(--gray-100, #f6f4ef); }
#convDeleteBtn { color: #B3261E; }
.msg-conv-header { position: relative; }

.msg-thread { flex: 1; overflow-y: auto; padding: 14px 2px; display: flex; flex-direction: column; gap: 8px; }
/* Anchor bubbles to the BOTTOM (chat convention): with few messages they sit
 * just above the compose bar instead of clustering at the top over a void.
 * margin-top:auto on the first child keeps long threads scrollable (the
 * justify-content:flex-end + overflow combo would clip the top). */
.msg-thread > :first-child { margin-top: auto; }
.msg-bubble-row { display: flex; flex-direction: column; max-width: 78%; }
.msg-bubble-row.mine { align-self: flex-end; align-items: flex-end; }
.msg-bubble-row.theirs { align-self: flex-start; align-items: flex-start; }
.msg-bubble {
    padding: 9px 13px; border-radius: 16px; font-size: 0.94rem; line-height: 1.4; word-break: break-word;
}
.msg-bubble-row.mine .msg-bubble { background: #004B87; color: #fff; border-bottom-right-radius: 5px; }
/* Received bubbles use gray-200 (not gray-100) so they stand out from the page
   background, which is also gray-100 — otherwise they're invisible (both themes). */
.msg-bubble-row.theirs .msg-bubble { background: var(--gray-200, #eceae4); color: var(--gray-900, #1E1B16); border-bottom-left-radius: 5px; }
.msg-bubble-time { font-size: 0.7rem; color: var(--gray-500, #9ca3af); margin-top: 2px; padding: 0 4px; }

/* Compose bar: theme-aware so it isn't a white slab in dark mode. Its
   background fills the safe area (extends under the home indicator) so the
   iOS-rounded viewport corners clip it cleanly instead of showing a square edge.
   The bottom padding lifts the controls WELL clear of the home indicator AND
   the display's rounded corners, which curve ~15px into the bottom edges on
   modern iPhones and were visually clipping the textarea/Send button. */
.msg-compose { display: flex; flex-direction: column; align-items: stretch; gap: 0; padding-top: 12px; border-top: 1px solid var(--gray-200, #eee); padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 14px); background: var(--gray-100); }
.msg-compose textarea {
    flex: 1; resize: none; border: 1.5px solid var(--gray-200, #d8d4cc); border-radius: 21px;
    /* 16px min: iOS Safari auto-zooms the page when focusing a control with a
       smaller font. Keeping it at 16px stops that jarring zoom-in. */
    padding: 11px 15px; font-family: inherit; font-size: 16px; color: var(--gray-900, #1E1B16);
    background: var(--white); max-height: 120px; line-height: 1.35;
}
.msg-compose textarea::placeholder { color: var(--gray-500); }
.msg-compose textarea:focus { outline: none; border-color: #004B87; }
.msg-send {
    flex: none; background: #004B87; color: #fff; border: none; border-radius: 21px;
    padding: 11px 20px; font-family: 'Oswald', sans-serif; font-weight: 600; font-size: 0.95rem; cursor: pointer;
}
.msg-send:hover { background: #013a68; }

/* --- Member picker modal --- */
.msg-picker-modal { position: fixed; inset: 0; z-index: 9000; display: flex; align-items: center; justify-content: center; padding: 16px; }
/* The `display:flex` above outranks the UA `[hidden]{display:none}` (equal
   specificity, author wins), so `hidden` alone wouldn't close it. */
.msg-picker-modal[hidden] { display: none; }
.msg-picker-overlay { position: absolute; inset: 0; background: rgba(30,27,22,0.5); }
.msg-picker-content {
    position: relative; background: var(--white); color: var(--gray-900); border-radius: 14px; width: 100%; max-width: 440px;
    max-height: 80vh; display: flex; flex-direction: column; padding: 18px; box-shadow: 0 12px 40px rgba(0,0,0,0.35);
}
.msg-picker-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.msg-picker-head h2 { margin: 0; font-size: 1.15rem; }
.msg-picker-close { background: none; border: none; font-size: 24px; line-height: 1; color: var(--gray-500); cursor: pointer; }
.msg-picker-search {
    width: 100%; box-sizing: border-box; padding: 10px 12px; border: 1.5px solid var(--gray-200, #d8d4cc);
    border-radius: 9px; font-family: inherit; font-size: 16px; margin-bottom: 10px;
    background: var(--gray-100); color: var(--gray-900);
}
.msg-picker-search:focus { outline: none; border-color: #004B87; }
.msg-picker-results { overflow-y: auto; }
.msg-picker-item {
    display: flex; align-items: center; gap: 12px; width: 100%; background: none; border: none;
    padding: 10px 6px; cursor: pointer; text-align: left; border-radius: 8px; font-family: inherit;
}
.msg-picker-item:hover { background: var(--gray-100, #f6f4ef); }
.msg-picker-name { font-weight: 600; color: var(--gray-900, #1E1B16); }

@media (max-width: 600px) {
    #convView { height: calc(100dvh - 170px); }
    /* dash-main drops to 12px side padding on mobile — not enough to keep the
       compose controls out of the iPhone's curved bottom corners. Pad the bar
       itself so its contents sit ~20px in from the screen edges. */
    .msg-compose { padding-left: 8px; padding-right: 8px; }
}


/* === Dark mode (audit #45-46, added 2026-08-05) =========================== */
[data-theme="dark"] .msg-picker-check { color: var(--tl-blue); }
[data-theme="dark"] .msg-picker-row.selected { background: rgba(58, 143, 214, 0.18); }
[data-theme="dark"] #convDeleteBtn { color: var(--crimson); }


/* ===================== Messaging v2 ===================== */
/* Day dividers */
.msg-day { align-self: center; font-size: 0.72rem; font-weight: 700; color: var(--gray-500, #8C8578); margin: 12px 0 8px; }

/* Reply quote inside a bubble */
.msg-quote {
    border-left: 3px solid var(--sand, #C4A76C); background: rgba(0,0,0,0.07);
    border-radius: 8px; padding: 5px 9px; font-size: 0.8rem; margin-bottom: 5px; cursor: pointer;
}
.msg-bubble-row.mine .msg-quote { background: rgba(255,255,255,0.16); border-left-color: #F3C94F; }
.msg-quote b { display: block; font-size: 0.72rem; margin-bottom: 1px; }
.msg-bubble-row.theirs .msg-quote b { color: var(--green-forest, #2D5E2A); }
.msg-flash .msg-bubble { animation: msgFlash 1.2s ease; }
@keyframes msgFlash { 0%, 100% { filter: none; } 30% { filter: brightness(1.35); } }

/* Reactions */
.msg-bubble { position: relative; }
.msg-bubble-row.has-reacts { margin-bottom: 13px; }
.msg-reacts { position: absolute; bottom: -12px; display: flex; gap: 2px; z-index: 2; }
.msg-bubble-row.mine .msg-reacts { left: -6px; }
.msg-bubble-row.theirs .msg-reacts { right: -6px; }
.msg-react {
    background: var(--white, #fff); border: 1.5px solid var(--gray-100, #F0EDE6); border-radius: 14px;
    padding: 1px 7px; font-size: 0.78rem; cursor: pointer; box-shadow: 0 1px 4px rgba(30,27,22,0.22);
    display: inline-flex; align-items: center; gap: 3px;
}
.msg-react.is-mine { border-color: #004B87; }
.msg-react small { font-size: 0.66rem; color: var(--gray-500, #6b6558); font-weight: 700; }

/* Media in bubbles */
.msg-bubble-media { padding: 4px !important; background: transparent !important; }
.msg-media { border-radius: 12px; overflow: hidden; max-width: 240px; }
.msg-media-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 3px; }
.msg-media-item { position: relative; cursor: pointer; }
.msg-media-item img, .msg-media-item video { width: 100%; display: block; border-radius: 4px; }
.msg-media-grid .msg-media-item img, .msg-media-grid .msg-media-item video { height: 110px; object-fit: cover; }
.msg-media-play {
    position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
    color: #fff; font-size: 1.6rem; text-shadow: 0 2px 10px rgba(0,0,0,0.6); background: rgba(0,0,0,0.18);
}
.msg-text { display: block; }
.msg-bubble a { color: inherit; text-decoration: underline; }
.msg-bubble-deleted { font-style: italic; opacity: 0.65; }
.msg-seen { align-self: flex-end; font-size: 0.68rem; color: var(--gray-500, #8C8578); font-weight: 600; margin: 0 6px 4px; }

/* `hidden` must beat our display:flex rules — otherwise the reply bar and the
   typing dots stay on screen after being dismissed. */
.msg-typing-row[hidden], .msg-reply-bar[hidden], .msg-attach-row[hidden] { display: none !important; }

/* Typing indicator */
.msg-typing-row { display: flex; align-items: center; gap: 8px; padding: 0 14px 4px; }
.msg-typing { display: inline-flex; gap: 4px; background: var(--gray-200, #E4E1D8); border-radius: 14px; padding: 8px 12px; }
.msg-typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--gray-500, #8C8578); animation: msgBlink 1.2s infinite; }
.msg-typing i:nth-child(2) { animation-delay: 0.2s; }
.msg-typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes msgBlink { 0%, 60%, 100% { opacity: 0.35; } 30% { opacity: 1; } }
.msg-typing-who { font-size: 0.75rem; color: var(--gray-500, #8C8578); }

/* Composer additions */
.msg-compose-bar { display: flex; align-items: flex-end; gap: 8px; }
.msg-attach-btn {
    flex: none; width: 34px; height: 34px; border-radius: 50%; border: 0; cursor: pointer;
    background: var(--gray-200, #E4E1D8); color: #004B87; font-size: 1.3rem; font-weight: 700; line-height: 1;
}
.msg-reply-bar {
    display: flex; align-items: center; gap: 10px; margin-bottom: 8px;
    background: var(--gray-100, #F0EDE6); border-left: 3px solid #004B87; border-radius: 8px; padding: 6px 10px;
}
.msg-reply-body { min-width: 0; flex: 1; }
.msg-reply-who { display: block; font-size: 0.72rem; font-weight: 700; color: #004B87; }
.msg-reply-text { display: block; font-size: 0.8rem; color: var(--gray-700, #4a4437); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-reply-x { border: 0; background: none; cursor: pointer; color: var(--gray-500, #8C8578); font-size: 1.1rem; }
.msg-attach-row { display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.msg-attach { position: relative; width: 62px; height: 62px; border-radius: 10px; overflow: hidden; }
.msg-attach img, .msg-attach video { width: 100%; height: 100%; object-fit: cover; }
.msg-attach-x {
    position: absolute; top: 2px; right: 2px; width: 18px; height: 18px; border: 0; cursor: pointer;
    background: rgba(30,27,22,0.75); color: #fff; border-radius: 50%; font-size: 0.7rem; line-height: 1;
}
.msg-attach-vid { position: absolute; bottom: 3px; left: 5px; color: #fff; font-size: 0.7rem; text-shadow: 0 1px 3px rgba(0,0,0,0.8); }
.msg-compose-status { font-size: 0.8rem; color: var(--gray-500, #8C8578); margin-bottom: 6px; }

/* Long-press sheet + tapbacks */
/* iOS turns a long press into a text selection + callout; suppress both on the
   thread and the sheet so the gesture reads as a press, not a highlight. */
.msg-thread, .msg-sheet-overlay, .msg-tapbacks, .msg-sheet {
    -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
/* …but keep selection available where it's useful: copying a link. */
.msg-bubble a { -webkit-user-select: text; user-select: text; }
.msg-sheet-overlay { position: fixed; inset: 0; background: rgba(30,27,22,0.45); z-index: 200; display: flex; flex-direction: column; justify-content: flex-end; padding: 18px; }
.msg-tapbacks {
    position: fixed; top: var(--tb-top, 22vh); left: var(--tb-left, 50%);
    transform: translateX(-50%); display: flex; gap: 6px; z-index: 2;
    background: var(--white, #fff); border-radius: 26px; padding: 8px 12px; box-shadow: 0 6px 24px rgba(0,0,0,0.35);
}
.msg-tapbacks button { border: 0; background: none; font-size: 1.5rem; cursor: pointer; padding: 2px 4px; border-radius: 50%; line-height: 1; }
.msg-tapbacks button.on { background: rgba(0,75,135,0.15); }
.msg-sheet { background: var(--white, #FAFAF7); border-radius: 14px; overflow: hidden; box-shadow: 0 8px 30px rgba(0,0,0,0.4); }
.msg-sheet button {
    display: block; width: 100%; text-align: left; padding: 14px 18px; border: 0; cursor: pointer;
    background: none; font: inherit; font-size: 1rem; color: var(--gray-900, #1E1B16);
    border-bottom: 1px solid var(--gray-200, #EDEAE2);
}
.msg-sheet button:last-child { border-bottom: 0; }
.msg-sheet button.danger { color: var(--crimson, #BA272C); }

/* Media viewer */
.msg-viewer { position: fixed; inset: 0; background: rgba(20,17,12,0.94); z-index: 300; display: flex; align-items: center; justify-content: center; padding: 20px; }
.msg-viewer-stage { max-width: 100%; max-height: 100%; display: flex; }
.msg-viewer-stage img, .msg-viewer-stage video { max-width: 100%; max-height: 88vh; border-radius: 6px; }
.msg-viewer-nav {
    position: absolute; top: 50%; transform: translateY(-50%); width: 48px; height: 72px;
    border: 0; cursor: pointer; border-radius: 10px; background: rgba(255,255,255,0.10); color: #fff; font-size: 2.2rem;
}
.msg-viewer-nav.prev { left: 10px; } .msg-viewer-nav.next { right: 10px; }
.msg-viewer-close {
    position: absolute; top: max(12px, env(safe-area-inset-top)); right: 14px; width: 44px; height: 44px;
    border: 0; border-radius: 50%; cursor: pointer; background: rgba(255,255,255,0.12); color: #fff; font-size: 1.5rem;
}
.msg-viewer-count { position: absolute; bottom: max(14px, env(safe-area-inset-bottom)); left: 0; right: 0; text-align: center; color: rgba(255,255,255,0.75); font-size: 0.85rem; }

/* Archived-threads toggle at the foot of the inbox */
.msg-archived-toggle {
    display: block; width: 100%; margin: 10px 0 4px; padding: 11px;
    font: inherit; font-size: 0.88rem; font-weight: 600; cursor: pointer;
    color: var(--gray-500, #8C8578); background: none;
    border: 1px dashed var(--gray-200, #DDD5C6); border-radius: 10px;
}
.msg-archived-toggle:hover { color: var(--gray-900, #1E1B16); background: var(--gray-100, #F0EDE6); }

.msg-viewer-share {
    position: absolute; top: max(12px, env(safe-area-inset-top)); left: 14px; width: 44px; height: 44px;
    border: 0; border-radius: 50%; cursor: pointer; background: rgba(255,255,255,0.12); color: #fff; font-size: 1.3rem;
}

/* Pins, mentions, search, forward, group photos */
.msg-pin { font-size: 0.75rem; }
.msg-avatar-img { object-fit: cover; }
.msg-mention { font-weight: 700; color: #F3C94F; }
.msg-bubble-row.theirs .msg-mention { color: #004B87; }
.msg-mention-box {
    display: flex; flex-direction: column; margin-bottom: 8px; overflow: hidden;
    background: var(--white, #FAFAF7); border: 1px solid var(--gray-200, #DDD5C6); border-radius: 12px;
    box-shadow: 0 4px 14px rgba(30,27,22,0.15);
}
.msg-mention-box button {
    display: flex; align-items: center; gap: 10px; padding: 9px 12px; border: 0; cursor: pointer;
    background: none; font: inherit; font-size: 0.92rem; color: var(--gray-900, #1E1B16); text-align: left;
}
.msg-mention-box button:hover { background: var(--gray-100, #F0EDE6); }
.msg-mention-box .msg-avatar { width: 26px; height: 26px; font-size: 0.8rem; }
.msg-search-bar {
    display: flex; align-items: center; gap: 8px; padding: 8px 12px;
    background: var(--white, #FAFAF7); border-bottom: 1px solid var(--gray-200, #DDD5C6);
}
.msg-search-bar input {
    flex: 1; min-width: 0; font: inherit; font-size: 16px; padding: 8px 12px;
    border: 1px solid var(--gray-200, #DDD5C6); border-radius: 10px; background: var(--gray-100, #F0EDE6); color: var(--gray-900, #1E1B16);
}
.msg-search-bar button { border: 0; background: none; cursor: pointer; font-size: 1rem; color: var(--tl-blue, #004B87); padding: 6px 8px; }
.msg-search-count { font-size: 0.78rem; color: var(--gray-500, #8C8578); white-space: nowrap; }
.msg-search-hit .msg-bubble { outline: 2px solid #F3C94F; }
.msg-fwd-tag { font-size: 0.7rem; font-weight: 700; opacity: 0.75; margin-bottom: 3px; }
.msg-fwd-sheet { max-height: 60vh; display: flex; flex-direction: column; }
.msg-fwd-head { font-family: 'Oswald', sans-serif; font-weight: 600; padding: 13px 18px; border-bottom: 1px solid var(--gray-200, #EDEAE2); color: var(--brown-dark, #3E2E18); }
.msg-fwd-list { overflow-y: auto; }
