/**
 * Pulseheaven × PeepSo — messaging integration
 * ================================================================
 * Loaded on: PeepSo messages page, PDP, order pages.
 * Depends on: ph-core.css (tokens, .ph-btn, animations).
 *
 * Edit here when:
 *   - styling product thumbs in chat lists or conversation headers
 *   - changing how offer-event cards render inside chat bubbles
 *   - tweaking the "Ask a question" PDP popup
 *   - changing message buttons on order pages
 *
 * The "Ask a question" popup uses .ph-msg-popup (NOT .ph-modal) —
 * different markup, slides up from bottom on mobile. Don't confuse
 * the two systems.
 *
 * TABLE OF CONTENTS
 *   1. Inbox list — small product thumb on avatar
 *   2. Conversation header — inline product thumb after participant name
 *   3. Chat offer-event cards (.ph-evt-card)
 *   4. PDP "Ask a question" popup (.ph-msg-popup)
 *   5. Order page — "Message {role}" buttons
 *   6. Mobile responsiveness
 */
.ph-msg-popup [hidden] {
    display: none !important;
}

/* ==================================================================
 * 1. Inbox list — small product thumb stamped on avatar
 *
 * For each conversation in the messages list, we stamp a tiny
 * product thumbnail on the bottom-right of the participant avatar
 * for visual context about what the conversation is about.
 * ================================================================== */
.pso-messages-list-item__avatar {
    position: relative;
}

.ph-ctx-thumb {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid var(--ph-bg);
    box-shadow: var(--ph-shadow-sm);
    background: var(--ph-bg-softer);
}

.ph-ctx-excerpt {
    color: var(--ph-text);
}
.ph-ctx-excerpt__sub {
    color: var(--ph-text-muted);
    font-weight: normal;
}


/* ==================================================================
 * 2. Conversation header — inline product thumb
 *
 * Rendered as: avatar  Yassine  →  [thumb]
 * Provides context inside an open conversation about which product
 * the discussion centers on. Updates dynamically as the underlying
 * offer's product changes (e.g. when buyer makes an offer on a new
 * product within the same thread).
 * ================================================================== */
.ph-header-product-arrow {
    display: inline-block;
    margin: 0 8px;
    color: var(--ph-text-muted);
    font-weight: 400;
    font-size: 16px;
    line-height: 1;
    vertical-align: middle;
    user-select: none;
    animation: ph-fade-in .2s ease-out;
}

.ph-header-product-thumbwrap {
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
    animation: ph-fade-in .25s ease-out;
}
.ph-header-product-thumbwrap a {
    display: inline-block;
    line-height: 0;
    text-decoration: none;
    border-radius: var(--ph-radius-sm);
    overflow: hidden;
}
.ph-header-product-thumbwrap img {
    width: 36px;
    height: 36px;
    display: block;
}
.ph-header-product-thumbwrap a:hover img {
    transform: scale(1.06);
    box-shadow: var(--ph-shadow-md);
}


/* ==================================================================
 * 3. Chat offer-event cards
 *
 * When the JS scanner finds a [ph-offer:N] token in a chat bubble,
 * it replaces the bubble's content with a rich card. The host bubble
 * gets .ph-evt-host (which strips PeepSo's default bubble styling)
 * and the card itself uses the .ph-evt-card structure.
 *
 * Tones: --success (accept/paid), --danger (decline/withdraw),
 *        --warning (counter), --info (default)
 * ================================================================== */
.ph-evt-host {
    background: transparent !important;
    padding: 0 !important;
    border: none !important;
    max-width: none !important;
    box-shadow: none !important;
}

.ph-evt-card {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px;
    background: var(--ph-bg);
    border: 1px solid var(--ph-border);
    border-radius: var(--ph-radius);
    box-shadow: var(--ph-shadow-sm);
    max-width: 360px;
    /* Padding + border must fit inside max-width — without this the card
     * overflows narrow chat bubbles on mobile and forces horizontal scroll. */
    box-sizing: border-box;
    /* Catch-all: any unbreakable text (long URLs, gigantic prices, etc.)
     * must wrap rather than blow the card past its max-width. */
    overflow-wrap: anywhere;
    word-break: break-word;
    min-width: 0;
    font-size: 14px;
    line-height: 1.4;
    color: var(--ph-text);
}

/* Pre-hydration cloak.
 * The MutationObserver in peepso-messages.js stamps data-ph-evt-id on
 * any chat bubble whose text contains a [ph-offer:N] token, *before*
 * the next paint. Render then adds .ph-evt-host once the rich card
 * replaces the bubble's content. Hiding the gap keeps the raw token
 * text from flashing while /offer-events is in flight. */
[data-ph-evt-id]:not(.ph-evt-host) {
    visibility: hidden;
}

.ph-evt-card--success { border-left: 3px solid #0a6b3a; }
.ph-evt-card--danger  { border-left: 3px solid #7a7a7a; }
.ph-evt-card--warning { border-left: 3px solid #8a5a00; }
.ph-evt-card--info    { border-left: 3px solid #5b21b6; }

.ph-evt-card__head {
    /* flex-wrap lets the badge drop below the heading on extra-narrow
     * bubbles instead of pushing the row past the card width. */
    display: flex; align-items: center; gap: 10px;
    flex-wrap: wrap;
    min-width: 0;
}
.ph-evt-card__thumb {
    flex: 0 0 auto;
    width: 44px; height: 44px;
    border-radius: var(--ph-radius-sm);
    object-fit: cover;
    background: var(--ph-bg-softer);
}
.ph-evt-card__thumb--empty {
    display: inline-block;
    background: linear-gradient(135deg, #e5e7eb, #f3f4f6);
}
.ph-evt-card__heading {
    flex: 1 1 auto; min-width: 0;
}
.ph-evt-card__label {
    font-size: 12px; font-weight: 600;
    color: var(--ph-text-muted);
    text-transform: uppercase;
    letter-spacing: .04em;
}
.ph-evt-card__product {
    display: block;
    font-size: 13px; font-weight: 500;
    color: var(--ph-text);
    text-decoration: none;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    margin-top: 1px;
}
.ph-evt-card__product:hover {
    text-decoration: underline;
}

.ph-evt-card__badge {
    flex: 0 0 auto;
    padding: 4px 10px;
    font-size: 11px; font-weight: 500;
    border-radius: 12px;
    text-transform: uppercase;
    letter-spacing: .04em;
    white-space: nowrap;
}
.ph-evt-card__badge--success { color: #047857; background: #d1fae5; }
.ph-evt-card__badge--danger  { color: #b91c1c; background: #fee2e2; }
.ph-evt-card__badge--warning { color: #8a5a00; background: #fff5d6; }
.ph-evt-card__badge--info    { color: #5b21b6; background: #f4ecff; }

.ph-evt-card__amount {
    font-size: 22px; font-weight: 700;
    color: var(--ph-text);
    letter-spacing: -.01em;
}

.ph-evt-card__message {
    margin: 0;
    padding: 8px 10px;
    background: var(--ph-bg-softer);
    border-left: 2px solid var(--ph-border);
    border-radius: 0 var(--ph-radius-sm) var(--ph-radius-sm) 0;
    font-size: 13px;
    color: var(--ph-text);
    font-style: italic;
}

.ph-evt-card__actions {
    display: flex; flex-wrap: wrap; gap: 8px;
}
.ph-evt-card__actions .ph-btn { font-weight: 400; }


/* ==================================================================
 * 4. PDP "Ask a question" popup — sidebar inquiry layout
 *
 * Triggered by the "Ask a question" button on product pages. Two-pane
 * layout: a left rail of topic options (Sizing / Condition / Shipping
 * / Other) and a right composer. The chosen topic is prepended to the
 * outgoing message as "[Topic] …" so the seller can read the subject
 * at a glance.
 *
 * Mobile (≤560px): the dialog becomes a bottom sheet with a drag
 * handle; the rail collapses into a horizontal-scrolling chip strip
 * above the composer (active chip = solid black pill).
 *
 * Markup (created dynamically by peepso-messages.js):
 *   <div class="ph-msg-popup ph-msg-popup--sidebar">
 *     <div class="ph-msg-popup__overlay"></div>
 *     <div class="ph-msg-popup__panel">
 *       <button class="ph-msg-popup__close">×</button>
 *       <aside class="ph-msg-popup__rail">…topic buttons…</aside>
 *       <section class="ph-msg-popup__main">
 *         <div class="ph-msg-popup__head">…</div>
 *         <div class="ph-msg-popup__field"><textarea/></div>
 *         <div class="ph-msg-popup__foot">count + send</div>
 *       </section>
 *       <div class="ph-msg-popup__success" hidden>…</div>
 *     </div>
 *   </div>
 * ================================================================== */
body.ph-msg-popup-locked { overflow: hidden; }

.ph-msg-popup {
    position: fixed;
    inset: 0;
    z-index: 100000;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.ph-msg-popup--open {
    display: flex;
}

.ph-msg-popup__overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, .4);
    animation: ph-fade-in .18s ease-out;
}

.ph-msg-popup__panel {
    position: relative;
    width: 100%;
    max-width: 600px;
    background: #fff;
    border-radius: 30px;
    box-shadow: 0 30px 80px rgba(0,0,0,.18), 0 4px 8px rgba(0,0,0,.06);
    overflow: hidden;
    display: grid;
    grid-template-columns: 196px 1fr;
    min-height: 420px;
    max-height: calc(100vh - 40px);
    animation: ph-rise .32s cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* Bare X — no circle background, no border. */
.ph-msg-popup__close {
    position: absolute;
    top: 14px; right: 14px;
    background: transparent;
    border: 0;
    padding: 4px;
    color: #000;
    cursor: pointer;
    display: inline-flex; align-items: center; justify-content: center;
    z-index: 3;
    border-radius: 4px;
    transition: opacity .15s ease;
}
.ph-msg-popup__close:hover { opacity: .6; }
.ph-msg-popup__close:focus-visible {
    outline: 2px solid #7c3aed;
    outline-offset: 2px;
}

/* Topic rail (desktop) */
.ph-msg-popup__rail {
    background: #fafafa;
    padding: 22px 14px;
    border-right: 1px solid var(--ph-border-soft);
    display: flex; flex-direction: column;
}
.ph-msg-popup__rail-label {
    font-size: 11px; letter-spacing: 0.16em; text-transform: uppercase;
    color: var(--ph-text-muted);
    font-weight: 500;
    margin-bottom: 12px;
    padding: 0 10px;
}
.ph-msg-popup__rail-list {
    display: flex; flex-direction: column; gap: 2px;
}
.ph-msg-popup__rail-btn {
    background: transparent;
    border: 0; border-radius: 10px;
    padding: 12px 14px;
    text-align: left;
    cursor: pointer;
    font-family: inherit;
    display: flex; flex-direction: column; gap: 2px;
    transition: background .18s ease;
    position: relative;
    width: 100%;
}
.ph-msg-popup__rail-btn:hover { background: rgba(0,0,0,.035); }
.ph-msg-popup__rail-btn.is-on {
    background: #fff;
    box-shadow: 0 0 0 1px var(--ph-border);
}
.ph-msg-popup__rail-row {
    display: flex; align-items: center; justify-content: space-between;
}
.ph-msg-popup__rail-btn b {
    font-weight: 500; font-size: 14px; color: #000;
}
.ph-msg-popup__rail-btn small {
    font-size: 12px; color: var(--ph-text-muted);
    line-height: 1.3;
}
.ph-msg-popup__rail-mark {
    width: 8px; height: 8px; border-radius: 999px;
    border: 1.5px solid #acacac;
    flex-shrink: 0;
    transition: all .18s ease;
}
.ph-msg-popup__rail-btn.is-on .ph-msg-popup__rail-mark {
    background: #7c3aed;
    border-color: #7c3aed;
    box-shadow: 0 0 0 3px rgba(167,139,250,.22);
}

/* Composer */
.ph-msg-popup__main {
    padding: 28px 26px 22px;
    display: flex; flex-direction: column;
    min-width: 0;
}
.ph-msg-popup__head {
    margin-bottom: 18px;
    display: flex; flex-direction: column; gap: 2px;
    padding-right: 28px; /* leave room for the close button */
}
.ph-msg-popup__head .ph-msg-popup__eyebrow {
    font-size: 11px; letter-spacing: 0.16em; text-transform: uppercase;
    color: var(--ph-text-muted);
    font-weight: 500;
}
.ph-msg-popup__head .ph-msg-popup__product-name {
    font-size: 20px; font-weight: 500;
    letter-spacing: -0.01em;
    color: #000;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

.ph-msg-popup__field {
    flex: 1;
    margin-bottom: 16px;
    min-height: 0;
}
.ph-msg-popup__textarea,
.ph-msg-popup__textarea:focus,
.ph-msg-popup__textarea:focus-visible,
.ph-msg-popup__textarea:active {
    /* Defensive: themes (Astra etc.) and browser UA stylesheets can
     * inject borders/outlines/box-shadows on focused textareas. The
     * inquiry composer is a borderless field by design — kill all of
     * them with !important so theme cascade can't reintroduce them. */
    border: 0 !important;
    outline: 0 !important;
    outline: none !important;
    box-shadow: none !important;
    background: transparent !important;
    -webkit-tap-highlight-color: transparent;
    -webkit-appearance: none;
    appearance: none;
}
.ph-msg-popup__textarea {
    width: 100%;
    height: 100%;
    min-height: 160px;
    resize: none;
    font-family: inherit;
    font-size: 16px; /* 16px on mobile to prevent iOS zoom-on-focus */
    line-height: 1.55;
    color: #000;
    padding: 0;
}
.ph-msg-popup__textarea::placeholder { color: #b0b0b0; }

@media (min-width: 61.25em) {
    .ph-msg-popup__textarea { font-size: 15px; }
}

.ph-msg-popup__foot {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px;
    border-top: 1px solid var(--ph-border-soft);
    padding-top: 14px;
}
.ph-msg-popup__count {
    font-size: 12px; color: var(--ph-text-muted);
    font-feature-settings: "tnum";
}

/* Send button — neutral chip that hovers white. */
.ph-msg-popup__send {
    background: #e4e4e4;
    color: #000;
    border: 1px solid #e4e4e4;
    padding: 11px 18px;
    border-radius: 10px;
    font-family: inherit; font-size: 14px;
    cursor: pointer;
    display: inline-flex; align-items: center; gap: 8px;
    letter-spacing: 0.02em;
    transition: background .2s ease, border-color .2s ease;
}
.ph-msg-popup__send:hover:not(:disabled) {
    background: #fff;
    border-color: #e4e4e4;
}
.ph-msg-popup__send:disabled {
    background: #f2f2f2;
    border-color: #f2f2f2;
    color: var(--ph-text-muted);
    cursor: not-allowed;
}
.ph-msg-popup__send-icon { flex-shrink: 0; }

/* ─────────────────────────────────────────────────────────────
 * Success state — shown after the message is sent. Replaces the
 * composer pane in place (spans the full grid), centered, fade-up.
 * Soft mint check-circle, two equal buttons matching the send chip.
 * ───────────────────────────────────────────────────────────── */
.ph-msg-popup__success {
    grid-column: 1 / -1;
    padding: 56px 40px 48px;
    text-align: center;
    background: #fff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 420px;
    box-sizing: border-box;
    animation: ph-msg-success-rise .4s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.ph-msg-popup__success-icon {
    width: 56px; height: 56px;
    margin: 0 auto 18px;
    background: rgba(70, 220, 120, 0.12);
    color: #16a34a;
    border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    animation: ph-msg-success-pop .42s cubic-bezier(0.34, 1.56, 0.64, 1) .05s both;
}
.ph-msg-popup__success-icon svg {
    display: block;
}
.ph-msg-popup__success-title {
    font-size: 22px;
    font-weight: 500;
    letter-spacing: -0.01em;
    color: #000;
    margin: 0 0 6px;
}
.ph-msg-popup__success-sub {
    font-size: 13.5px;
    color: var(--ph-text-muted);
    line-height: 1.5;
    max-width: 320px;
    margin: 0 0 24px;
    text-wrap: pretty;
}
.ph-msg-popup__success-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    width: 100%;
    max-width: 360px;
}

/* Equal-width buttons matching the send chip's pill shape. */
.ph-msg-popup__success-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px 18px;
    border-radius: 10px;
    font-family: inherit;
    font-size: 14px;
    letter-spacing: 0.02em;
    text-decoration: none;
    cursor: pointer;
    transition: background .2s ease, border-color .2s ease, color .2s ease;
    line-height: 1.2;
}
.ph-msg-popup__success-btn--ghost {
    background: #fff;
    color: #000;
    border: 1px solid #e4e4e4;
}
.ph-msg-popup__success-btn--ghost:hover { background: #fafafa; }
.ph-msg-popup__success-btn--solid {
    background: #e4e4e4;
    color: #000;
    border: 1px solid #e4e4e4;
}
.ph-msg-popup__success-btn--solid:hover {
    background: #fff;
    border-color: #e4e4e4;
}

@keyframes ph-rise {
    from { opacity: 0; transform: translateY(8px) scale(.985); }
    to   { opacity: 1; transform: translateY(0)   scale(1); }
}
@keyframes ph-rise-up {
    from { opacity: 0; transform: translateY(40px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes ph-msg-success-rise {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}
@keyframes ph-msg-success-pop {
    0%   { opacity: 0; transform: scale(.6); }
    60%  { opacity: 1; transform: scale(1.08); }
    100% { opacity: 1; transform: scale(1); }
}


/* ==================================================================
 * 5. Order page — "Message {role}" buttons
 *
 * Block of buttons rendered on order detail pages letting buyers
 * message sellers (and vice versa) about a specific order.
 * ================================================================== */
.ph-order-message-section {
    margin-top: 20px;
    padding: 16px;
    background: var(--ph-bg-softer);
    border-radius: var(--ph-radius);
}
.ph-order-message-section__title {
    margin: 0 0 12px;
    font-size: 14px; font-weight: 600;
    color: var(--ph-text);
}
.ph-order-message-section__buttons {
    display: flex; flex-wrap: wrap; gap: 8px;
}
.ph-order-message-section--vendor {
    margin-top: 12px;
    padding: 12px;
}


/* ==================================================================
 * 6. Mobile responsiveness
 *
 * Mobile (≤46.24em / ~740px):
 *   • Inquiry popup keeps the desktop two-column layout (rail +
 *     composer side-by-side) but with a narrower rail, tighter
 *     padding, and reduced font sizes so it fits on phone widths.
 *   • Topic subs are hidden — the rail is too narrow to show them
 *     comfortably alongside the labels.
 *   • Chat cards widen to use the full bubble width.
 * ================================================================== */
@media (max-width: 46.24em) {
    .ph-msg-popup {
        padding: 12px;
    }
    .ph-msg-popup__panel {
        /* Two-column preserved, just narrower rail. */
        grid-template-columns: 132px 1fr;
        max-width: 100%;
        min-height: 0;
        max-height: calc(100vh - 24px);
        border-radius: 24px;
    }

    .ph-msg-popup__close {
        top: 10px;
        right: 10px;
    }

    /* Rail — narrower, tighter padding. */
    .ph-msg-popup__rail {
        padding: 18px 8px;
    }
    .ph-msg-popup__rail-label {
        margin-bottom: 10px;
        padding: 0 8px;
        font-size: 10px;
    }
    .ph-msg-popup__rail-btn {
        padding: 10px 10px;
        border-radius: 8px;
        gap: 1px;
    }
    .ph-msg-popup__rail-btn b {
        font-size: 13px;
    }
    /* Hide topic subs — 132px rail is too tight for both lines. */
    .ph-msg-popup__rail-btn small {
        display: none;
    }
    .ph-msg-popup__rail-mark {
        width: 7px;
        height: 7px;
    }

    /* Composer */
    .ph-msg-popup__main {
        padding: 22px 18px 16px;
        min-height: 0;
    }
    .ph-msg-popup__head {
        margin-bottom: 14px;
        padding-right: 24px;
    }
    .ph-msg-popup__head .ph-msg-popup__eyebrow {
        font-size: 10px;
    }
    .ph-msg-popup__head .ph-msg-popup__product-name {
        font-size: 17px;
    }

    .ph-msg-popup__field {
        margin-bottom: 12px;
    }
    .ph-msg-popup__textarea {
        min-height: 140px;
        font-size: 16px; /* keep 16px to prevent iOS zoom-on-focus */
    }

    .ph-msg-popup__foot {
        padding-top: 12px;
    }
    .ph-msg-popup__send {
        padding: 10px 14px;
        font-size: 13px;
    }

    /* Success pane scales down. */
    .ph-msg-popup__success {
        padding: 36px 22px 30px;
        min-height: 0;
    }
    .ph-msg-popup__success-icon {
        width: 48px;
        height: 48px;
        margin-bottom: 14px;
    }
    .ph-msg-popup__success-icon svg {
        width: 24px;
        height: 24px;
    }
    .ph-msg-popup__success-title {
        font-size: 19px;
    }
    .ph-msg-popup__success-sub {
        margin-bottom: 20px;
    }
    .ph-msg-popup__success-actions {
        max-width: none;
    }
}

@media (max-width: 61.24em) {
    .ph-evt-card {
        max-width: 100%;
        padding: 12px;
        gap: 8px;
        font-size: 13px;
    }
    .ph-evt-card__head {
        gap: 8px;
    }
    .ph-evt-card__thumb {
        width: 38px;
        height: 38px;
    }
    .ph-evt-card__label {
        font-size: 11px;
    }
    .ph-evt-card__product {
        font-size: 12.5px;
    }
    .ph-evt-card__badge {
        padding: 2px 7px;
        font-size: 10px;
    }
    .ph-evt-card__amount {
        font-size: 19px;
    }
    .ph-evt-card__message {
        padding: 6px 8px;
        font-size: 12px;
    }
    .ph-header-product-thumbwrap img {
        width: 32px;
        height: 32px;
    }
    .ph-header-product-arrow {
        margin: 0 6px;
        font-size: 14px;
    }
}