/* =============================================================
   EXAMOO MESSAGING — ENTERPRISE CSS
   Supports: Desktop (sidebar + chat), Tablet, Mobile
   Brand: --brand-primary = green (#22c55e / #16a34a)
   ============================================================= */

/* ── Variables (fallback to Examoo brand green) ──────────────── */
:root {
    --msg-brand: #16a34a;
    --msg-brand-light: #dcfce7;
    --msg-brand-dark: #15803d;
    --msg-bg: #f0fdf4;
    --msg-surface: #ffffff;
    --msg-border: #e2e8f0;
    --msg-text: #0f172a;
    --msg-text-2: #475569;
    --msg-text-3: #94a3b8;
    --msg-mine-bg: #16a34a;
    --msg-mine-text: #ffffff;
    --msg-theirs-bg: #ffffff;
    --msg-theirs-text: #0f172a;
    --msg-theirs-border: #e2e8f0;
    --msg-hover: #f0fdf4;
    --msg-active: #dcfce7;
    --msg-radius: 18px;
    --msg-sidebar-w: 320px;
    --msg-topbar-h: 64px;
    --msg-transition: 0.18s ease;
}

/* ── Page lock (only on messages page) ─────────────────────── */
body.messages-page {
    overflow: hidden !important;
    height: 100vh !important;
}

/* ── Outer layout: fills viewport below topbar ─────────────── */
.messaging-layout {
    display: flex;
    width: 100%;
    height: calc(100vh - var(--msg-topbar-h));
    overflow: hidden;
    background: var(--msg-bg);
    /* Accommodate Examoo's left sidebar (280px on desktop) */
    position: relative;
}

/* ═══════════════════════════════════════════════════════════════
   LEFT SIDEBAR — conversation list
   ═══════════════════════════════════════════════════════════════ */
.msg-sidebar {
    width: var(--msg-sidebar-w);
    min-width: var(--msg-sidebar-w);
    display: flex;
    flex-direction: column;
    background: var(--msg-surface);
    border-right: 1px solid var(--msg-border);
    overflow: hidden;
    flex-shrink: 0;
    z-index: 3;
}

.msg-sidebar-header {
    padding: 18px 16px 12px;
    border-bottom: 1px solid var(--msg-border);
    background: var(--msg-surface);
    flex-shrink: 0;
}

.msg-sidebar-header h2 {
    margin: 0 0 14px 0;
    font-size: 20px;
    font-weight: 800;
    color: var(--msg-text);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

/* search */
.msg-search-wrapper {
    position: relative;
}

.msg-search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--msg-text-3);
    font-size: 13px;
    pointer-events: none;
    z-index: 1;
}

.msg-search-input {
    width: 100%;
    padding: 9px 12px 9px 36px;
    border: 1.5px solid var(--msg-border);
    border-radius: 24px;
    font-size: 14px;
    background: #f8fafc;
    color: var(--msg-text);
    outline: none;
    box-sizing: border-box;
    transition: border-color var(--msg-transition), background var(--msg-transition);
}

.msg-search-input:focus {
    border-color: var(--msg-brand);
    background: #fff;
}

/* new chat button */
.msg-new-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--msg-brand-light);
    color: var(--msg-brand);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: background var(--msg-transition), color var(--msg-transition);
    flex-shrink: 0;
}

.msg-new-btn:hover {
    background: var(--msg-brand);
    color: #fff;
}

/* conversation list scroll area */
.conv-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    scroll-behavior: smooth;
}

.conv-list::-webkit-scrollbar {
    width: 4px;
}

.conv-list::-webkit-scrollbar-track {
    background: transparent;
}

.conv-list::-webkit-scrollbar-thumb {
    background: var(--msg-border);
    border-radius: 4px;
}

/* individual conversation item */
.conv-item {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 11px 14px;
    cursor: pointer;
    border-bottom: 1px solid #f1f5f9;
    position: relative;
    transition: background var(--msg-transition);
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.conv-item:hover {
    background: var(--msg-hover);
}

.conv-item.active {
    background: var(--msg-active);
}

.conv-item.active .conv-name {
    color: var(--msg-brand-dark);
}

.conv-avatar-wrap {
    position: relative;
    flex-shrink: 0;
}

.conv-avatar {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    background: #f1f5f9;
}

.conv-online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #fff;
}

.conv-info {
    flex: 1;
    min-width: 0;
}

.conv-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    margin-bottom: 3px;
}

.conv-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--msg-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.conv-time {
    font-size: 11px;
    color: var(--msg-text-3);
    white-space: nowrap;
    flex-shrink: 0;
}

.conv-preview {
    font-size: 13px;
    color: var(--msg-text-2);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.4;
}

.conv-preview.has-unread {
    font-weight: 700;
    color: var(--msg-text);
}

.conv-unread-badge {
    background: var(--msg-brand);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    flex-shrink: 0;
}

/* context menu (right-click / long-press) */
.conv-context-menu {
    position: fixed;
    background: #fff;
    border: 1px solid var(--msg-border);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .12);
    z-index: 9999;
    min-width: 160px;
    padding: 4px 0;
    animation: ctxFade .1s ease;
}

@keyframes ctxFade {
    from {
        opacity: 0;
        transform: scale(.97);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.conv-context-menu button {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    padding: 9px 16px;
    border: none;
    background: none;
    font-size: 13px;
    color: var(--msg-text);
    cursor: pointer;
    text-align: left;
    transition: background .12s;
}

.conv-context-menu button:hover {
    background: #f8fafc;
}

.conv-context-menu button.danger {
    color: #dc2626;
}

.conv-context-menu button.danger:hover {
    background: #fef2f2;
}

.conv-ctx-sep {
    height: 1px;
    background: var(--msg-border);
    margin: 4px 0;
}

/* ═══════════════════════════════════════════════════════════════
   RIGHT MAIN — chat window
   ═══════════════════════════════════════════════════════════════ */
.msg-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* ← critical */
    position: relative;
    background: var(--msg-bg);
    min-width: 0;
}

/* empty state */
.msg-empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 40px 24px;
    color: var(--msg-text-2);
}

.msg-empty-state .empty-icon {
    font-size: 72px;
    margin-bottom: 20px;
    opacity: .7;
}

.msg-empty-state h2 {
    font-size: 22px;
    font-weight: 800;
    color: var(--msg-text);
    margin: 0 0 10px;
}

.msg-empty-state p {
    font-size: 15px;
    color: var(--msg-text-2);
    max-width: 280px;
    line-height: 1.6;
    margin: 0;
}

/* chat header (flex-shrink: 0 keeps it pinned at top) */
.msg-main-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 18px;
    background: var(--msg-surface);
    border-bottom: 1px solid var(--msg-border);
    flex-shrink: 0;
    z-index: 2;
    gap: 12px;
}

.chat-user-info {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1;
}

.chat-back-btn {
    display: none;
    background: none;
    border: none;
    color: var(--msg-text-2);
    cursor: pointer;
    font-size: 20px;
    padding: 4px 8px 4px 0;
    line-height: 1;
    flex-shrink: 0;
}

.chat-avatar-wrap {
    position: relative;
    flex-shrink: 0;
}

.chat-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    background: #f1f5f9;
}

.chat-online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #fff;
}

.chat-user-name {
    font-size: 15px;
    font-weight: 700;
    color: var(--msg-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-user-sub {
    font-size: 12px;
    color: var(--msg-text-3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 260px;
}

.chat-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.chat-header-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--msg-text-2);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: background var(--msg-transition), color var(--msg-transition);
}

.chat-header-btn:hover {
    background: var(--msg-hover);
    color: var(--msg-brand);
}

/* ── messages scroll container — THE KEY FIX ───────────────── */
/* flex:1 + overflow-y:auto = fills space, scrolls internally   */
/* Input area is BELOW this, flex-shrink:0, always visible      */
.msg-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 20px 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    scroll-behavior: smooth;
}

.msg-container::-webkit-scrollbar {
    width: 4px;
}

.msg-container::-webkit-scrollbar-track {
    background: transparent;
}

.msg-container::-webkit-scrollbar-thumb {
    background: var(--msg-border);
    border-radius: 4px;
}

/* date separator */
.msg-date-sep {
    text-align: center;
    font-size: 11px;
    color: var(--msg-text-3);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin: 14px 0 8px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.msg-date-sep::before,
.msg-date-sep::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--msg-border);
}

/* ── message bubbles ──────────────────────────────────────── */
.msg-bubble-group {
    display: flex;
    flex-direction: column;
    max-width: 70%;
    gap: 2px;
}

.msg-bubble-group.mine {
    align-self: flex-end;
    align-items: flex-end;
}

.msg-bubble-group.theirs {
    align-self: flex-start;
    align-items: flex-start;
}

.msg-bubble {
    position: relative;
    padding: 9px 13px;
    border-radius: var(--msg-radius);
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
    max-width: 100%;
    cursor: default;
    transition: opacity .1s;
    animation: bubblePop .18s ease;
}

@keyframes bubblePop {
    from {
        opacity: 0;
        transform: scale(.97) translateY(4px);
    }

    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

.msg-bubble-group.mine .msg-bubble {
    background: var(--msg-mine-bg);
    color: var(--msg-mine-text);
    border-bottom-right-radius: 4px;
}

.msg-bubble-group.theirs .msg-bubble {
    background: var(--msg-theirs-bg);
    color: var(--msg-theirs-text);
    border: 1px solid var(--msg-theirs-border);
    border-bottom-left-radius: 4px;
}

/* first/last radius tweaks in consecutive groups */
.msg-bubble-group.mine .msg-bubble:not(:last-child) {
    border-bottom-right-radius: 6px;
}

.msg-bubble-group.theirs .msg-bubble:not(:last-child) {
    border-bottom-left-radius: 6px;
}

.msg-bubble:hover .msg-bubble-actions {
    opacity: 1;
    pointer-events: auto;
}

/* per-bubble timestamp */
.msg-time {
    font-size: 10px;
    color: var(--msg-text-3);
    margin-top: 4px;
    padding: 0 2px;
}

.msg-bubble-group.mine .msg-time {
    text-align: right;
}

/* links inside messages */
a.msg-link {
    color: inherit;
    text-decoration: underline;
    opacity: .85;
}

a.msg-link:hover {
    opacity: 1;
}

.msg-bubble-group.mine a.msg-link {
    color: #d1fae5;
}

/* bubble hover actions (delete) */
.msg-bubble-actions {
    position: absolute;
    top: -28px;
    right: 0;
    opacity: 0;
    pointer-events: none;
    transition: opacity .15s;
    display: flex;
    gap: 4px;
    background: #fff;
    border: 1px solid var(--msg-border);
    border-radius: 8px;
    padding: 3px 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .1);
}

.msg-bubble-action-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 12px;
    color: var(--msg-text-2);
    padding: 2px 4px;
    border-radius: 4px;
    transition: color .12s, background .12s;
}

.msg-bubble-action-btn:hover {
    background: #f1f5f9;
    color: #dc2626;
}

/* ── input area — PINNED at bottom ──────────────────────────── */
/* flex-shrink:0 = never shrinks, never gets pushed off screen  */
.msg-input-area {
    flex-shrink: 0;
    background: var(--msg-surface);
    border-top: 1px solid var(--msg-border);
    padding: 10px 16px 12px;
    z-index: 2;
}

.msg-form {
    display: flex;
    align-items: flex-end;
    gap: 10px;
}

.msg-textarea {
    flex: 1;
    padding: 10px 14px;
    border: 1.5px solid var(--msg-border);
    border-radius: 22px;
    font-size: 14px;
    line-height: 1.5;
    resize: none;
    outline: none;
    background: #f8fafc;
    color: var(--msg-text);
    max-height: 120px;
    min-height: 42px;
    overflow-y: auto;
    box-sizing: border-box;
    font-family: inherit;
    transition: border-color var(--msg-transition), background var(--msg-transition);
}

.msg-textarea:focus {
    border-color: var(--msg-brand);
    background: #fff;
}

.msg-send-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--msg-brand);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
    transition: background var(--msg-transition), transform .1s;
}

.msg-send-btn:hover {
    background: var(--msg-brand-dark);
}

.msg-send-btn:active {
    transform: scale(.92);
}

.msg-send-btn:disabled {
    background: #cbd5e1;
    cursor: not-allowed;
    transform: none;
}

.msg-input-footer {
    display: flex;
    justify-content: space-between;
    padding: 5px 4px 0;
    font-size: 11px;
    color: var(--msg-text-3);
}

/* ── typing indicator ──────────────────────────────────────── */
.msg-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    background: var(--msg-theirs-bg);
    border: 1px solid var(--msg-theirs-border);
    border-radius: var(--msg-radius);
    border-bottom-left-radius: 4px;
    width: fit-content;
    align-self: flex-start;
    animation: bubblePop .18s ease;
}

.msg-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--msg-text-3);
    animation: typingBounce 1.2s infinite ease-in-out;
}

.msg-typing span:nth-child(2) {
    animation-delay: .2s;
}

.msg-typing span:nth-child(3) {
    animation-delay: .4s;
}

@keyframes typingBounce {

    0%,
    60%,
    100% {
        transform: translateY(0);
    }

    30% {
        transform: translateY(-6px);
    }
}

/* ── loading spinner ───────────────────────────────────────── */
.msg-spinner-wrap {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 10;
    background: rgba(255, 255, 255, .6);
}

.msg-spinner {
    width: 28px;
    height: 28px;
    border: 3px solid var(--msg-border);
    border-top-color: var(--msg-brand);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

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

/* ── utility ───────────────────────────────────────────────── */
.hidden {
    display: none !important;
}

.btn-icon {
    background: none;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
    border-radius: 8px;
    transition: background var(--msg-transition);
}

.btn-icon:hover {
    background: var(--msg-hover);
}

/* toast */
.msg-toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: #1e293b;
    color: #fff;
    padding: 10px 20px;
    border-radius: 24px;
    font-size: 13px;
    z-index: 99999;
    opacity: 0;
    transition: opacity .2s, transform .2s;
    pointer-events: none;
    white-space: nowrap;
}

.msg-toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.msg-toast.error {
    background: #dc2626;
}

.msg-toast.success {
    background: #16a34a;
}

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE — Tablet (≤900px): sidebar collapses to icons
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 900px) {
    :root {
        --msg-sidebar-w: 72px;
    }

    .msg-sidebar-header h2,
    .msg-search-wrapper,
    .conv-info,
    .conv-unread-badge {
        display: none;
    }

    .msg-new-btn {
        margin: 0 auto;
    }

    .msg-sidebar-header {
        padding: 14px 8px;
    }

    .conv-item {
        justify-content: center;
        padding: 10px 0;
    }

    .conv-avatar {
        width: 42px;
        height: 42px;
    }
}

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE — Mobile (≤640px): full-screen panels
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 640px) {
    :root {
        --msg-topbar-h: 56px;
        --msg-sidebar-w: 100%;
    }

    .messaging-layout {
        height: calc(100vh - var(--msg-topbar-h));
        /* subtract mobile nav bar height if present */
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    /* Sidebar takes full width on mobile */
    .msg-sidebar {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 20;
        border-right: none;
        transition: transform .22s ease;
    }

    .msg-sidebar.slide-out {
        transform: translateX(-100%);
    }

    /* Restore all hidden elements on mobile */
    .msg-sidebar-header h2,
    .msg-search-wrapper,
    .conv-info,
    .conv-unread-badge {
        display: flex;
    }

    .msg-sidebar-header h2 {
        display: flex;
    }

    .msg-search-wrapper {
        display: block;
    }

    .conv-info {
        display: block;
    }

    /* Main takes full width, sits behind sidebar */
    .msg-main {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 10;
    }

    /* Show back button on mobile */
    .chat-back-btn {
        display: flex;
    }

    /* Bubbles narrower on small screens */
    .msg-bubble-group {
        max-width: 88%;
    }

    /* Pad input above mobile bottom nav */
    .msg-input-area {
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    }
}

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE — Very small (≤360px)
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 360px) {
    .msg-bubble-group {
        max-width: 95%;
    }

    .msg-textarea {
        font-size: 13px;
    }
}