/* ───────────────────────────────────────────────────────────────────────────
   Messages — buyer↔seller chat threads. RTL-first, logical properties.
   ─────────────────────────────────────────────────────────────────────────── */

.msg-thread {
    --msg-primary: #002b80;
    --msg-primary-soft: #e8edf9;
    --msg-ink: #191c20;
    --msg-ink-soft: #5b6472;
    --msg-line: rgba(25, 28, 32, 0.10);
    --msg-surface: #fff;
    --msg-surface-2: #f4f6fb;
    display: flex;
    flex-direction: column;
    min-height: calc(100vh - 9rem);
}

/* ── Header ── */
.msg-thread__head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--msg-line);
}
.msg-thread__back {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: var(--msg-ink-soft);
    flex-shrink: 0;
    transition: background .15s;
}
.msg-thread__back:hover { background: var(--msg-surface-2); }
.msg-thread__peer { display: flex; align-items: center; gap: 10px; min-width: 0; }
.msg-thread__avatar,
.msg-thread__avatar-img {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    flex-shrink: 0;
}
.msg-thread__avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--msg-primary);
    color: #fff;
    font-weight: 800;
    font-size: 1.1rem;
}
.msg-thread__avatar-img { object-fit: cover; background: var(--msg-surface-2); }
.msg-thread__peer-name { font-weight: 800; color: var(--msg-ink); line-height: 1.2; }
.msg-thread__peer-sub { font-size: .78rem; color: var(--msg-ink-soft); }

/* ── Listing context strip ── */
.msg-thread__listing {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-top: 12px;
    padding: 10px 12px;
    background: var(--msg-surface-2);
    border: 1px solid var(--msg-line);
    border-radius: 16px;
    transition: filter .15s;
}
.msg-thread__listing:hover { filter: brightness(.98); }
.msg-thread__listing-img {
    width: 52px;
    height: 52px;
    border-radius: 12px;
    object-fit: cover;
    flex-shrink: 0;
    background: #e7eaf0;
}
.msg-thread__listing-img--none {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--msg-ink-soft);
}
.msg-thread__listing-text { flex: 1; min-width: 0; }
.msg-thread__listing-title {
    font-weight: 700;
    color: var(--msg-ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.msg-thread__listing-price { color: var(--msg-primary); font-weight: 800; font-size: .92rem; }
.msg-thread__listing-badge {
    font-size: .72rem;
    font-weight: 700;
    color: #9a3412;
    background: #ffedd5;
    padding: 3px 8px;
    border-radius: 999px;
    flex-shrink: 0;
}
.msg-thread__listing-chevron { color: var(--msg-ink-soft); flex-shrink: 0; }

/* ── Messages body ── */
.msg-thread__body {
    flex: 1;
    overflow-y: auto;
    padding: 18px 4px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 240px;
}
/* Bottom-anchor the conversation (newest message rests just above the composer).
   The auto margin collapses to 0 once messages overflow, so scrolling still works. */
.msg-thread__body > :first-child { margin-block-start: auto; }
.msg-thread__empty {
    margin: auto;
    max-width: 320px;
    text-align: center;
    color: var(--msg-ink-soft);
    padding: 24px;
}
.msg-thread__empty .material-symbols-outlined { font-size: 2.5rem; color: var(--msg-primary); }
.msg-thread__empty p { margin: 10px 0 6px; color: var(--msg-ink); font-weight: 700; }
.msg-thread__empty small { font-size: .82rem; line-height: 1.5; display: block; }

.msg-day { text-align: center; margin: 10px 0; }
.msg-day span {
    font-size: .72rem;
    color: var(--msg-ink-soft);
    background: var(--msg-surface-2);
    padding: 3px 12px;
    border-radius: 999px;
}

.msg-row { display: flex; }
.msg-row--mine { justify-content: flex-start; }   /* RTL: outgoing on the inline-end (left) */
.msg-row--theirs { justify-content: flex-end; }

.msg-bubble {
    /* inline-flex column so the bubble hugs its content (a floated time used to
       break shrink-to-fit and stretch every bubble to the 80% max-width). */
    display: inline-flex;
    flex-direction: column;
    align-items: flex-end;          /* timestamp sits at the inline-end below the text */
    width: fit-content;
    max-width: min(82%, 480px);
    padding: 7px 12px 5px;
    border-radius: 16px;
    font-size: .92rem;
    line-height: 1.45;
    position: relative;
    word-break: break-word;
    white-space: pre-wrap;
}
.msg-bubble__text { align-self: stretch; }
.msg-row--theirs .msg-bubble {
    background: var(--msg-surface);
    border: 1px solid var(--msg-line);
    color: var(--msg-ink);
    border-start-end-radius: 4px;
}
.msg-row--mine .msg-bubble {
    background: var(--msg-primary);
    color: #fff;
    border-start-start-radius: 4px;
}
.msg-bubble__time {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    font-size: .66rem;
    opacity: .7;
    margin-top: 2px;
    /* The bubble uses pre-wrap to keep message line breaks; without this the
       timestamp inherits it and the markup's indentation whitespace renders as
       a ~60px run of spaces, bloating every bubble. */
    white-space: nowrap;
}
.msg-bubble__tick { font-size: .9rem !important; }
.msg-bubble__tick.is-read { color: #7fd3ff; opacity: 1; }

/* ── Composer ── */
.msg-composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    margin-top: 12px;
    padding: 8px;
    background: var(--msg-surface);
    border: 1px solid var(--msg-line);
    border-radius: 22px;
    position: sticky;
    bottom: 12px;
}
.msg-composer__input {
    flex: 1;
    border: 0;
    outline: none;
    resize: none;
    background: transparent;
    font: inherit;
    font-size: .95rem;
    color: var(--msg-ink);
    padding: 8px 10px;
    max-height: 140px;
    line-height: 1.4;
}
.msg-composer__send {
    width: 42px;
    height: 42px;
    border: 0;
    border-radius: 50%;
    background: var(--msg-primary);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: filter .15s, transform .1s;
}
.msg-composer__send:hover { filter: brightness(1.1); }
.msg-composer__send:active { transform: scale(.93); }
.msg-composer__send .material-symbols-outlined { transform: scaleX(-1); }  /* point send arrow correctly in RTL */

.msg-composer--disabled {
    justify-content: center;
    color: var(--msg-ink-soft);
    font-size: .85rem;
    gap: 6px;
    align-items: center;
}

/* ── Mobile: the thread IS the screen (native chat app) ──────────────────────
   Fill the space between the fixed top bar and bottom tab bar exactly, so the
   page itself never scrolls — only the message list does. ─────────────────── */
@media (max-width: 768px) {
    /* Full-screen chat view — drop the app-shell footer so the page can't scroll. */
    body:has(.msg-thread) footer { display: none; }

    .msg-thread {
        height: calc(100vh - 11rem);
        height: calc(100dvh - 11rem);
        min-height: 0;
        max-width: none;
        margin-inline: 0;
        padding: 8px 12px 0;     /* override Tailwind px-4 py-6 */
    }
    .msg-thread__head,
    .msg-thread__listing { flex-shrink: 0; }
    .msg-thread__body {
        min-height: 0;           /* allow the flex child to scroll instead of growing the page */
        padding: 14px 2px 6px;
    }
    .msg-composer {
        position: static;        /* pinned by the flex column, not by sticky */
        flex-shrink: 0;
        margin-top: 8px;
        margin-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    }
}
