/**
 * market-chat.css — WhatsApp-style two-pane marketplace chat.
 * Brand accent: var(--portal-color); falls back to a neutral blue.
 */

:root {
    --mc-accent: var(--portal-color, #0d6efd);
    --mc-border: #e4e7eb;
    --mc-muted: #6c757d;
    --mc-text: #1f2733;
    --mc-text-soft: #2b3440;
    --mc-hover: #f7f9fb;
    --mc-chat-bg: #f4f1ee;
    --mc-border-soft: #f1f3f5;
    --mc-input-border: #ced4da;
    --mc-danger: #dc3545;
}

/* ------------------------------------------------------------------ */
/* App shell (two-pane)                                                */
/* ------------------------------------------------------------------ */

.market-chat-app {
    display: flex;
    max-width: 1080px;
    height: 78vh;
    min-height: 480px;
    margin: 1.25rem auto;
    background: #fff;
    border: 1px solid var(--mc-border);
    border-radius: 14px;
    overflow: hidden;
    box-shadow: 0 6px 24px rgba(20, 30, 50, 0.08);
}

/* ------------------------------------------------------------------ */
/* Sidebar                                                             */
/* ------------------------------------------------------------------ */

.market-chat-app__sidebar {
    display: flex;
    flex-direction: column;
    width: 340px;
    flex: none;
    border-right: 1px solid var(--mc-border);
    background: #fff;
}

.market-chat-app__sidebar-head {
    padding: 1rem 1.1rem;
    border-bottom: 1px solid var(--mc-border);
}

.market-chat-app__title {
    margin: 0;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--mc-text);
}

.market-chat-list {
    flex: 1;
    overflow-y: auto;
    list-style: none;
    margin: 0;
    padding: 0;
}

.market-chat-list__empty {
    padding: 2rem 1.1rem;
    color: var(--mc-muted);
    text-align: center;
    font-size: 0.9rem;
}

/* Conversation row */
.market-chat-row {
    position: relative;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.7rem 1.1rem;
    color: inherit;
    text-decoration: none;
    border-bottom: 1px solid var(--mc-border-soft);
    transition: background-color 0.12s ease-in-out;
}

.market-chat-row:hover {
    background: var(--mc-hover);
    color: inherit;
}

.market-chat-row.is-active {
    background: #eef3fb; /* fallback for browsers without color-mix() */
    background: color-mix(in srgb, var(--mc-accent) 10%, #fff);
}

.market-chat-row__avatar {
    flex: none;
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: var(--mc-accent);
    color: #fff;
    font-weight: 700;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
}

.market-chat-row__body {
    flex: 1;
    min-width: 0;
}

.market-chat-row__top {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
}

.market-chat-row__name {
    flex: 1;
    min-width: 0;
    font-weight: 600;
    color: var(--mc-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.market-chat-row__time {
    flex: none;
    font-size: 0.72rem;
    color: var(--mc-muted);
}

.market-chat-row__preview {
    display: block;
    margin-top: 0.1rem;
    font-size: 0.82rem;
    color: var(--mc-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.market-chat-row__dot {
    flex: none;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--mc-accent);
    visibility: hidden;
}

.market-chat-row.is-unread .market-chat-row__dot { visibility: visible; }
.market-chat-row.is-unread .market-chat-row__name { font-weight: 700; }
.market-chat-row.is-unread .market-chat-row__preview { color: var(--mc-text-soft); font-weight: 600; }

/* ------------------------------------------------------------------ */
/* Main pane                                                           */
/* ------------------------------------------------------------------ */

.market-chat-app__main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    background: var(--mc-chat-bg);
}

.market-chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    color: var(--mc-muted);
    padding: 2rem;
    text-align: center;
}

.market-chat-empty__icon { font-size: 2.5rem; opacity: 0.5; }
.market-chat-empty__text { margin: 0; font-size: 0.95rem; }

/* Conversation panel */
.market-chat-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.market-chat-panel__header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.7rem 1rem;
    background: #fff;
    border-bottom: 1px solid var(--mc-border);
}

.market-chat-panel__back {
    display: none;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    border: 0;
    background: transparent;
    color: var(--mc-text);
    font-size: 1.1rem;
    cursor: pointer;
}

.market-chat-panel__avatar {
    flex: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--mc-accent);
    color: #fff;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
}

.market-chat-panel__heading {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.market-chat-panel__title {
    margin: 0;
    font-size: 0.98rem;
    font-weight: 600;
    color: var(--mc-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Focus is moved here programmatically after an AJAX switch; hide the ring only
   for that programmatic focus, never for real keyboard users (:focus-visible). */
.market-chat-panel__title:focus:not(:focus-visible) { outline: none; }

.market-chat-panel__product {
    font-size: 0.78rem;
    color: var(--mc-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Message list */
.market-chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    list-style: none;
    margin: 0;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.market-chat-messages__empty {
    margin: auto;
    color: var(--mc-muted);
    font-size: 0.9rem;
}

.market-chat-older-row {
    display: flex;
    justify-content: center;
    margin-bottom: 0.25rem;
}

.market-chat-load-older {
    border: 1px solid var(--mc-border);
    background: #fff;
    color: var(--mc-text-soft);
    font-size: 0.8rem;
    padding: 0.3rem 0.9rem;
    border-radius: 999px;
    cursor: pointer;
}

.market-chat-load-older:hover { background: var(--mc-hover); }
.market-chat-load-older:disabled { opacity: 0.6; cursor: default; }

.market-chat-message {
    display: flex;
    flex-direction: column;
    max-width: 72%;
}

.market-chat-message--mine { align-self: flex-end; align-items: flex-end; }
.market-chat-message--theirs { align-self: flex-start; align-items: flex-start; }

.market-chat-message__header {
    display: flex;
    gap: 0.5rem;
    align-items: baseline;
    margin-bottom: 0.15rem;
}

.market-chat-message__sender {
    font-weight: 600;
    font-size: 0.78rem;
    color: var(--mc-text-soft);
}

.market-chat-message__date {
    font-size: 0.7rem;
    color: var(--mc-muted);
}

.market-chat-message__body {
    background: #fff;
    border-radius: 12px;
    padding: 0.5rem 0.8rem;
    font-size: 0.9rem;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
    box-shadow: 0 1px 1px rgba(20, 30, 50, 0.06);
}

.market-chat-message--mine .market-chat-message__body {
    background: var(--mc-accent);
    color: #fff;
}

/* Product card: mini presentation of the product a contact message is about */
.market-chat-product-card {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    width: 100%;
    max-width: 320px;
    margin-bottom: 0.35rem;
    padding: 0.45rem 0.55rem;
    background: #fff;
    border: 1px solid var(--mc-border);
    border-radius: 12px;
    text-decoration: none;
    color: var(--mc-text);
    box-shadow: 0 1px 2px rgba(20, 30, 50, 0.06);
    transition: transform 0.12s ease-in-out, box-shadow 0.12s ease-in-out;
}

.market-chat-product-card:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(20, 30, 50, 0.13);
    color: var(--mc-text);
}

.market-chat-product-card__img {
    flex: none;
    width: 48px;
    height: 48px;
    border-radius: 9px;
    object-fit: cover;
    background: var(--mc-chat-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--mc-muted);
    font-size: 1.1rem;
}

.market-chat-product-card__info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.market-chat-product-card__label {
    font-size: 0.66rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--mc-accent);
}

.market-chat-product-card__name {
    font-size: 0.86rem;
    font-weight: 600;
    color: var(--mc-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.market-chat-product-card__chevron {
    margin-left: auto;
    padding-left: 0.4rem;
    color: var(--mc-muted);
    font-size: 0.72rem;
}

/* Composer */
.market-chat-composer {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 0.5rem;
    padding: 0.6rem 0.85rem;
    background: #fff;
    border-top: 1px solid var(--mc-border);
}

.market-chat-composer__textarea {
    flex: 1;
    min-width: 0;
    resize: none;
    min-height: 42px;
    max-height: 120px;
    border: 1px solid var(--mc-input-border);
    border-radius: 21px;
    padding: 0.55rem 0.9rem;
    font-size: 0.9rem;
    line-height: 1.4;
    transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}

.market-chat-composer__textarea:focus {
    outline: none;
    border-color: var(--mc-accent);
    box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.18); /* fallback */
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--mc-accent) 18%, transparent);
}

.market-chat-composer__send {
    flex: none;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 50%;
    background: var(--mc-accent);
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
    transition: transform 0.12s ease-in-out, opacity 0.12s ease-in-out;
}

.market-chat-composer__send:hover { transform: scale(1.05); }
.market-chat-composer__send:disabled { opacity: 0.6; cursor: default; }

.market-chat-composer__send:focus-visible {
    outline: 3px solid #fff;
    outline-offset: 2px;
    box-shadow: 0 0 0 5px var(--mc-accent);
}

.market-chat-composer__error {
    display: none;
    flex-basis: 100%;
    color: var(--mc-danger);
    font-size: 0.82rem;
}

.market-chat-composer__error.is-visible { display: block; }

/* ------------------------------------------------------------------ */
/* Side-tab unread badge: put it on the inner (left) edge so the page   */
/* scrollbar on the right doesn't clip it. Overrides main.css default.  */
/* ------------------------------------------------------------------ */

.side-tab-market-chat .small-chat-badge {
    right: auto;
    left: -6px;
}

/* ------------------------------------------------------------------ */
/* Widget (product page)                                              */
/* ------------------------------------------------------------------ */

.market-chat-widget { margin: 0.75rem 0; }
.market-chat-widget__login-hint { font-size: 0.9rem; color: var(--mc-muted); }

/* ------------------------------------------------------------------ */
/* Responsive: stack to single pane                                    */
/* ------------------------------------------------------------------ */

@media (max-width: 768px) {
    .market-chat-app {
        height: calc(100vh - 120px);
        height: calc(100dvh - 120px); /* dvh tracks the on-screen keyboard / browser chrome */
        min-height: 0;
        margin: 0.5rem auto;
        border-radius: 10px;
    }

    .market-chat-app__sidebar { width: 100%; }

    .market-chat-app__main { display: none; }

    .market-chat-app--thread-open .market-chat-app__sidebar { display: none; }

    .market-chat-app--thread-open .market-chat-app__main { display: flex; }

    .market-chat-panel__back { display: flex; }

    .market-chat-message { max-width: 85%; }
}

@media (prefers-reduced-motion: reduce) {
    .market-chat-composer__send,
    .market-chat-composer__textarea,
    .market-chat-row {
        transition: none;
    }

    .market-chat-composer__send:hover {
        transform: none;
    }
}

.market-chat-row__avatar--lead { background: #eef2f7; color: #6b7a90; }
.market-chat-row__tag {
    display: inline-block;
    font-size: 10px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .03em;
    color: #6b7a90;
    background: #eef2f7;
    border-radius: 4px;
    padding: 1px 5px;
    margin-right: 4px;
}

/* ------------------------------------------------------------------ */
/* Lead panel (read-only: anonymous contact, no composer)              */
/* ------------------------------------------------------------------ */

/* Header: initials avatar + status pill */
.market-chat-panel__avatar--lead {
    background: color-mix(in srgb, var(--mc-accent) 12%, #fff);
    color: var(--mc-accent);
}
.market-chat-panel__subtitle {
    display: inline-flex;
    align-items: center;
    gap: 0.34rem;
    margin-top: 0.1rem;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--mc-muted);
}
.market-chat-panel__subtitle::before {
    content: "";
    flex: none;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: color-mix(in srgb, var(--mc-accent) 55%, #fff);
}

/* Body: a contact dossier on the chat's warm paper, centered column */
.market-chat-lead {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    background: var(--mc-chat-bg);
    padding: clamp(1rem, 3vw, 1.75rem) 1rem;
}
.market-chat-lead__inner {
    max-width: 34rem;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.05rem;
}
.market-chat-lead .market-chat-product-card { max-width: none; margin: 0; }

/* The buyer's question, rendered as an incoming chat bubble */
.market-chat-lead__quote {
    align-self: flex-start;
    max-width: 94%;
    background: #fff;
    border: 1px solid var(--mc-border);
    border-radius: 5px 16px 16px 16px;
    padding: 0.7rem 0.9rem 0.78rem;
    box-shadow: 0 1px 2px rgba(20, 30, 50, 0.06);
}
.market-chat-lead__quote-eyebrow {
    display: block;
    margin-bottom: 0.28rem;
    font-size: 0.66rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--mc-accent);
}
.market-chat-lead__quote-body {
    margin: 0;
    font-size: 0.92rem;
    line-height: 1.5;
    color: var(--mc-text);
    word-break: break-word;
}

/* Reach: direct-contact action tiles (email primary, phone secondary) */
.market-chat-lead__reach {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}
.market-chat-lead__reach-note {
    margin: 0;
    font-size: 0.78rem;
    line-height: 1.45;
    color: var(--mc-text-soft);
}
.market-chat-lead__tiles {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
}
.market-chat-lead__tile {
    display: flex;
    align-items: center;
    gap: 0.7rem;
    padding: 0.65rem 0.8rem;
    border: 1px solid var(--mc-border);
    border-radius: 13px;
    background: #fff;
    color: var(--mc-text);
    text-decoration: none;
    transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.market-chat-lead__tile:hover {
    transform: translateY(-1px);
    box-shadow: 0 6px 18px rgba(20, 30, 50, 0.14);
    color: var(--mc-text);
}
.market-chat-lead__tile:focus-visible,
.market-chat-product-card:focus-visible {
    outline: 2px solid var(--mc-accent);
    outline-offset: 2px;
}
.market-chat-lead__tile-icon {
    flex: none;
    width: 38px;
    height: 38px;
    border-radius: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.95rem;
    background: color-mix(in srgb, var(--mc-accent) 11%, #fff);
    color: var(--mc-accent);
}
.market-chat-lead__tile-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.market-chat-lead__tile-label {
    font-size: 0.64rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--mc-muted);
}
.market-chat-lead__tile-value {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--mc-text);
    /* Wrap long emails instead of truncating — the seller needs the full address
       to reach the buyer; on narrow screens the tile grows rather than hides it. */
    overflow-wrap: anywhere;
}
.market-chat-lead__tile-go {
    margin-left: auto;
    padding-left: 0.4rem;
    font-size: 0.72rem;
    color: var(--mc-muted);
}
/* Primary tile = the recommended channel (email) */
.market-chat-lead__tile--primary {
    background: var(--mc-accent);
    border-color: transparent;
    color: #fff;
    box-shadow: 0 4px 12px color-mix(in srgb, var(--mc-accent) 30%, transparent);
}
.market-chat-lead__tile--primary:hover { color: #fff; }
.market-chat-lead__tile--primary .market-chat-lead__tile-icon { background: rgba(255, 255, 255, 0.2); color: #fff; }
.market-chat-lead__tile--primary .market-chat-lead__tile-label { color: rgba(255, 255, 255, 0.82); }
.market-chat-lead__tile--primary .market-chat-lead__tile-value,
.market-chat-lead__tile--primary .market-chat-lead__tile-go { color: #fff; }

/* Meta: quiet reference strip */
.market-chat-lead__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.45rem 1.6rem;
    margin: 0;
    padding-top: 0.7rem;
    border-top: 1px solid var(--mc-border-soft);
}
.market-chat-lead__meta > div {
    display: flex;
    flex-direction: column;
    gap: 0.12rem;
}
.market-chat-lead__meta dt {
    font-size: 0.62rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    /* text-soft (not muted): muted ~4.1:1 on the warm paper bg fails WCAG AA for
       this small label; text-soft passes. Size/weight/case keep it visually quiet. */
    color: var(--mc-text-soft);
}
.market-chat-lead__meta dd {
    margin: 0;
    font-size: 0.85rem;
    color: var(--mc-text-soft);
    word-break: break-word;
}

/* One orchestrated reveal on open */
@media (prefers-reduced-motion: no-preference) {
    .market-chat-lead__inner > * { animation: mcLeadIn 0.42s cubic-bezier(0.22, 1, 0.36, 1) both; }
    .market-chat-lead__inner > *:nth-child(2) { animation-delay: 0.05s; }
    .market-chat-lead__inner > *:nth-child(3) { animation-delay: 0.1s; }
    .market-chat-lead__inner > *:nth-child(4) { animation-delay: 0.15s; }
}
@keyframes mcLeadIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: none; }
}

.market-chat-panel__contact { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 2px; }
.market-chat-panel__contact-link { font-size: 12px; color: var(--mc-muted); text-decoration: none; }
.market-chat-panel__contact-link:hover { color: var(--mc-accent); text-decoration: underline; }
.market-chat-panel__contact-link i { font-size: 11px; margin-right: 3px; }

.market-chat-toast {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%) translateY(12px);
    background: #2b3442;
    color: #fff;
    font-size: 14px;
    padding: 10px 18px;
    border-radius: 999px;
    box-shadow: 0 6px 20px rgba(0,0,0,.25);
    opacity: 0;
    transition: opacity .25s ease, transform .25s ease;
    z-index: 9999;
    pointer-events: none;
}
.market-chat-toast.is-visible { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Respect reduced-motion: keep opacity feedback, drop translate/lift transforms */
@media (prefers-reduced-motion: reduce) {
    .market-chat-product-card,
    .market-chat-lead__tile { transition: none; }
    .market-chat-product-card:hover,
    .market-chat-lead__tile:hover { transform: none; }
    .market-chat-toast { transform: translateX(-50%); transition: opacity 0.2s ease; }
    .market-chat-toast.is-visible { transform: translateX(-50%); }
}
