/* EM Messenger — Telegram-style chat, site-themed & responsive */

.em-msgr {
    --em-accent: #ffca0d;
    --em-accent-hover: #ffb700;
    --em-ink: #1d252c;
    --em-text: #131313;
    --em-muted: #6b6b6b;
    --em-line: #eaeaea;
    --em-bg: #ffffff;
    --em-bg-soft: #f6f8f9;
    --em-bubble-me: #fff7d9;
    --em-bubble-them: #f1f5f9;
    --em-unread: #ef4444;
    --em-radius: 14px;

    display: grid;
    grid-template-columns: 340px 1fr;
    /* Constrain the single grid row to the container height (minmax min of 0
       lets children shrink below content) so the message list scrolls instead
       of the whole block growing with the conversation. */
    grid-template-rows: minmax(0, 1fr);
    position: relative;
    height: min(72vh, 760px);
    min-height: 480px;
    border: 1px solid var(--em-line);
    border-radius: var(--em-radius);
    overflow: hidden;
    background: var(--em-bg);
    color: var(--em-text);
    font-family: inherit;
    box-shadow: 0 10px 30px rgba(12, 12, 13, 0.05);
}

.em-msgr * {
    box-sizing: border-box;
}

/* The plugin toggles visibility via the [hidden] attribute in JS. Class-based
   display rules (e.g. .em-msgr__active{display:flex}) would otherwise win over
   the UA [hidden] rule, so enforce it explicitly. */
.em-msgr [hidden] {
    display: none !important;
}

.em-msgr--guest {
    display: block;
    height: auto;
    min-height: 0;
    padding: 32px;
    text-align: center;
}

/* ---------------- Conversation list ---------------- */

.em-msgr__list {
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--em-line);
    background: var(--em-bg);
    min-height: 0;
}

.em-msgr__list-head {
    padding: 18px 18px 10px;
}

.em-msgr__title {
    margin: 0;
    font-size: 20px;
    font-weight: 700;
    color: var(--em-ink);
}

.em-msgr__search {
    padding: 0 14px 12px;
}

.em-msgr__search-input {
    width: 100%;
    padding: 9px 14px;
    border: 1px solid var(--em-line);
    border-radius: 999px;
    background: var(--em-bg-soft);
    font-size: 14px;
    outline: none;
}

.em-msgr__search-input:focus {
    border-color: var(--em-accent);
}

.em-msgr__threads {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    flex: 1;
    min-height: 0;
}

.em-msgr__thread {
    display: flex;
    gap: 12px;
    align-items: center;
    padding: 12px 16px;
    cursor: pointer;
    border-bottom: 1px solid var(--em-line);
    transition: background 0.15s ease;
}

.em-msgr__thread:hover {
    background: var(--em-bg-soft);
}

.em-msgr__thread.is-active {
    background: #fefce8;
}

/* Conversations with messages the user hasn't seen yet */
.em-msgr__thread.is-unread .em-msgr__thread-name {
    font-weight: 700;
}

.em-msgr__thread.is-unread .em-msgr__thread-preview {
    color: var(--em-text);
    font-weight: 500;
}

.em-msgr__thread-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    background: var(--em-bg-soft);
}

.em-msgr__thread-body {
    flex: 1;
    min-width: 0;
}

.em-msgr__thread-top {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.em-msgr__thread-name {
    font-weight: 600;
    font-size: 15px;
    color: var(--em-ink);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.em-msgr__thread-time {
    font-size: 12px;
    color: var(--em-muted);
    flex-shrink: 0;
}

.em-msgr__thread-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-top: 2px;
}

.em-msgr__thread-preview {
    font-size: 13px;
    color: var(--em-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.em-msgr__thread-listing {
    display: block;
    font-size: 12px;
    color: var(--em-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}

.em-msgr__badge {
    background: var(--em-unread);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    line-height: 1;
    min-width: 18px;
    height: 18px;
    border-radius: 999px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    flex-shrink: 0;
}

.em-msgr__empty {
    padding: 24px 18px;
    color: var(--em-muted);
    font-size: 14px;
    text-align: center;
}

/* ---------------- Chat pane ---------------- */

.em-msgr__chat {
    display: flex;
    flex-direction: column;
    min-height: 0;
    background: var(--em-bg-soft);
}

.em-msgr__placeholder {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--em-muted);
    text-align: center;
}

.em-msgr__placeholder-icon {
    font-size: 42px;
    display: block;
    margin-bottom: 12px;
    opacity: 0.6;
}

.em-msgr__active {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.em-msgr__chat-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 18px;
    background: var(--em-bg);
    border-bottom: 1px solid var(--em-line);
    flex-shrink: 0;
}

.em-msgr__back {
    display: none;
    border: none;
    background: none;
    font-size: 22px;
    cursor: pointer;
    color: var(--em-ink);
    padding: 0 4px;
    line-height: 1;
}

.em-msgr__chat-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--em-bg-soft);
}

.em-msgr__chat-meta {
    min-width: 0;
}

.em-msgr__chat-name {
    display: block;
    font-weight: 700;
    color: var(--em-ink);
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.em-msgr__chat-listing {
    display: block;
    font-size: 13px;
    color: var(--em-muted);
    text-decoration: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 360px;
}

.em-msgr__chat-listing:hover {
    color: var(--em-accent-hover);
    text-decoration: underline;
}

/* Presence: online dot on avatars + header status line */

.em-msgr__avatar-wrap {
    position: relative;
    display: inline-block;
    flex-shrink: 0;
    line-height: 0;
}

.em-msgr__avatar-wrap.is-online::after {
    content: '';
    position: absolute;
    right: 0;
    bottom: 0;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #fff;
}

.em-msgr__chat-status {
    display: block;
    font-size: 12px;
    line-height: 1.25;
    color: var(--em-muted);
}

.em-msgr__chat-status:empty {
    display: none;
}

.em-msgr__chat-userinfo {
    display: block;
    font-size: 12px;
    line-height: 1.35;
    color: var(--em-muted);
    margin-top: 1px;
}

.em-msgr__chat-userinfo:empty {
    display: none;
}

.em-msgr__chat-status.is-online {
    color: #16a34a;
}

.em-msgr__chat-status.is-typing {
    color: var(--em-accent-hover);
    font-style: italic;
    animation: em-pulse 1.2s ease-in-out infinite;
}

@keyframes em-pulse {
    0%, 100% { opacity: 0.55; }
    50% { opacity: 1; }
}

/* ---------------- Messages ---------------- */

.em-msgr__messages {
    flex: 1;
    overflow-y: auto;
    padding: 18px;
    min-height: 0;
}

.em-msgr__messages-inner {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.em-msgr__load-more {
    display: block;
    margin: 0 auto 14px;
    background: var(--em-bg);
    border: 1px solid var(--em-line);
    border-radius: 999px;
    padding: 6px 16px;
    font-size: 13px;
    cursor: pointer;
    color: var(--em-muted);
}

/* Platform / service message */
.em-msgr__system {
    align-self: center;
    max-width: 80%;
    margin: 4px auto;
    text-align: center;
}

.em-msgr__system-text {
    display: inline-block;
    background: #fff7d9;
    color: #7a6a1f;
    border: 1px solid #f3e6b3;
    border-radius: 999px;
    padding: 6px 14px;
    font-size: 12.5px;
    line-height: 1.4;
    white-space: pre-wrap;
}

.em-msgr__att--system {
    margin-top: 8px;
    align-items: center;
}

.em-msgr__msg {
    max-width: 74%;
    display: flex;
    flex-direction: column;
}

.em-msgr__msg--me {
    align-self: flex-end;
    align-items: flex-end;
}

.em-msgr__msg--them {
    align-self: flex-start;
    align-items: flex-start;
}

.em-msgr__bubble {
    padding: 9px 13px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    background: var(--em-bubble-them);
    color: var(--em-text);
}

.em-msgr__msg--me .em-msgr__bubble {
    background: var(--em-bubble-me);
    border-bottom-right-radius: 5px;
}

.em-msgr__msg--them .em-msgr__bubble {
    border-bottom-left-radius: 5px;
}

.em-msgr__meta {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 3px;
    font-size: 11px;
    color: var(--em-muted);
    padding: 0 4px;
}

.em-msgr__tick {
    font-size: 12px;
    line-height: 1;
}

.em-msgr__tick.is-read {
    color: #1356b3;
}

/* Attachments */

.em-msgr__att {
    margin-top: 6px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.em-msgr__att-img {
    max-width: 240px;
    max-height: 260px;
    border-radius: 12px;
    display: block;
    cursor: pointer;
    object-fit: cover;
}

.em-msgr__att-file {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 12px;
    background: var(--em-bg);
    border: 1px solid var(--em-line);
    border-radius: 12px;
    text-decoration: none;
    color: var(--em-text);
    max-width: 260px;
}

.em-msgr__att-file:hover {
    border-color: var(--em-accent);
}

.em-msgr__att-icon {
    width: 34px;
    height: 34px;
    border-radius: 8px;
    background: #fefce8;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.em-msgr__att-info {
    min-width: 0;
}

.em-msgr__att-name {
    display: block;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.em-msgr__att-size {
    display: block;
    font-size: 11px;
    color: var(--em-muted);
}

/* ---------------- Composer ---------------- */

.em-msgr__composer {
    flex-shrink: 0;
    border-top: 1px solid var(--em-line);
    background: var(--em-bg);
    padding: 10px 14px;
}

.em-msgr__attachments-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding-bottom: 10px;
}

.em-msgr__chip {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--em-bg-soft);
    border: 1px solid var(--em-line);
    border-radius: 10px;
    padding: 6px 8px 6px 10px;
    font-size: 12px;
    max-width: 220px;
}

.em-msgr__chip-thumb {
    width: 28px;
    height: 28px;
    border-radius: 6px;
    object-fit: cover;
}

.em-msgr__chip-name {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.em-msgr__chip-remove {
    border: none;
    background: none;
    cursor: pointer;
    font-size: 15px;
    color: var(--em-muted);
    line-height: 1;
    padding: 0 2px;
}

.em-msgr__chip.is-uploading {
    opacity: 0.6;
}

.em-msgr__composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.em-msgr__attach-btn {
    cursor: pointer;
    font-size: 20px;
    padding: 8px;
    line-height: 1;
    color: var(--em-muted);
    flex-shrink: 0;
    border-radius: 50%;
}

.em-msgr__attach-btn:hover {
    background: var(--em-bg-soft);
}

.em-msgr__input {
    flex: 1;
    resize: none;
    border: 1px solid var(--em-line);
    border-radius: 20px;
    padding: 10px 16px;
    font-size: 14px;
    font-family: inherit;
    line-height: 1.4;
    max-height: 140px;
    outline: none;
    background: var(--em-bg-soft);
}

.em-msgr__input:focus {
    border-color: var(--em-accent);
}

.em-msgr__send {
    flex-shrink: 0;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    background: var(--em-accent);
    color: var(--em-ink);
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease;
}

.em-msgr__send:hover {
    background: var(--em-accent-hover);
}

.em-msgr__send:disabled {
    opacity: 0.5;
    cursor: default;
}

/* ---------------- Loader / toast ---------------- */

.em-msgr__loader {
    display: none;
    justify-content: center;
    padding: 16px;
}

.em-msgr__loader.is-active {
    display: flex;
}

.em-msgr__spinner {
    width: 22px;
    height: 22px;
    border: 3px solid var(--em-line);
    border-top-color: var(--em-accent);
    border-radius: 50%;
    animation: em-spin 0.7s linear infinite;
}

@keyframes em-spin {
    to {
        transform: rotate(360deg);
    }
}

.em-msgr__toast {
    position: absolute;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%);
    background: var(--em-ink);
    color: #fff;
    padding: 10px 18px;
    border-radius: 10px;
    font-size: 13px;
    z-index: 20;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

/* ---------------- Contact button (listing pages) ---------------- */

.em-msgr-contact-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: var(--em-accent, #ffca0d);
    color: #1d252c;
    border: none;
    border-radius: 999px;
    padding: 12px 22px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s ease;
}

.em-msgr-contact-btn:hover {
    background: #ffb700;
}

.em-msgr-contact-btn:disabled {
    opacity: 0.6;
    cursor: default;
}

/* Nav badge (theme can place <span class="em-msgr-badge"></span>) */

.em-msgr-badge {
    display: none;
    background: #ef4444;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 999px;
    padding: 0 5px;
    line-height: 18px;
    text-align: center;
}

.em-msgr-badge.is-visible {
    display: inline-block;
}

/* ---------------- Responsive / mobile ---------------- */

@media (max-width: 768px) {
    .em-msgr {
        grid-template-columns: 1fr;
        height: 80vh;
        position: relative;
    }

    .em-msgr__list {
        border-right: none;
    }

    /* Single-pane: chat overlays the list when a conversation is open */
    .em-msgr__chat {
        position: absolute;
        inset: 0;
        transform: translateX(100%);
        transition: transform 0.25s ease;
        z-index: 5;
    }

    .em-msgr.is-chat-open .em-msgr__chat {
        transform: translateX(0);
    }

    .em-msgr.is-chat-open .em-msgr__list {
        visibility: hidden;
    }

    .em-msgr__back {
        display: block;
    }

    .em-msgr__msg {
        max-width: 85%;
    }

    .em-msgr__chat-listing {
        max-width: 60vw;
    }
}
