/* WhatsApp-style Chats: master-detail, dark theme; tablet = mobile UI (1024px) */

/* ---- Theme tokens ---- */
:root {
    --wa-bg: #0b141a;
    --wa-list: #111b21;
    --wa-surface: #202c33;
    --wa-bubble-in: #202c33;
    --wa-bubble-out: #005c4b;
    --wa-green: #00a884;
    --wa-green-active: #06cf9c;
    --wa-text: #e9edef;
    --wa-text-muted: #8696a0;
    --wa-unread-badge: #00a884;
    --keyboard-offset: 0px;
}

/* ---- Chats main: full height, no padding; no scroll so composer stays fixed ---- */
.main.chats-main {
    padding: 0;
    min-height: var(--app-viewport-height);
    display: flex;
    flex-direction: column;
    background: var(--wa-bg);
    overflow: hidden;
}

/* ---- Master-detail shell ---- */
.dm-shell {
    display: flex;
    flex: 1;
    min-height: 0;
    height: calc(var(--app-viewport-height) - var(--mobile-bottom-nav-space));
    max-height: calc(var(--app-viewport-height) - var(--mobile-bottom-nav-space));
}

@media (min-width: 1025px) {
    .dm-shell {
        height: var(--app-viewport-height);
        max-height: var(--app-viewport-height);
    }

    .main.chats-main {
        padding-bottom: 0;
    }
}

/* ---- List (master): fixed width on desktop ---- */
.dm-list {
    width: 100%;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    background: var(--wa-list);
    border-right: 1px solid rgba(255, 255, 255, 0.06);
    min-width: 0;
}

@media (min-width: 1025px) {
    .dm-list {
        width: 400px;
        max-width: 400px;
        flex-shrink: 0;
    }
}

.list-header {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: var(--wa-list);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    color: var(--wa-text);
    font-weight: 700;
    font-size: 1rem;
}

.list-header-new {
    color: var(--wa-green);
    font-weight: 600;
    font-size: 0.9rem;
    text-decoration: none;
}

.list-header-new:hover {
    color: var(--wa-green-active);
}

.list-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-list-toolbar {
    padding: 8px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-search-input {
    width: 100%;
    padding: 8px 12px;
    font-size: 0.9rem;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: var(--wa-surface);
    color: var(--wa-text);
}

.chat-search-input::placeholder {
    color: var(--wa-text-muted);
}

.chat-filters {
    display: flex;
    gap: 6px;
}

.chat-filter-btn {
    padding: 4px 10px;
    font-size: 0.8rem;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.15);
    background: transparent;
    color: var(--wa-text-muted);
    cursor: pointer;
}

.chat-filter-btn:hover {
    color: var(--wa-text);
}

.chat-filter-btn.active {
    background: var(--wa-green);
    color: var(--wa-bg);
    border-color: var(--wa-green);
}

.chat-row-pinned .chat-row-name::before {
    content: "PIN ";
    font-size: 0.85em;
}

/* ---- Chat list scroll ---- */
.dm-list .chat-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    list-style: none;
    margin: 0;
    padding: 0;
}

/* ---- Chat row: dense, 44-48px avatar ---- */
.chat-row {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
    transition: background 0.1s ease;
    min-height: 72px;
}

.chat-row:hover,
.chat-row:active {
    background: var(--wa-surface);
}

.chat-row.active {
    background: var(--wa-surface);
}

.chat-row .avatar {
    width: 48px;
    height: 48px;
    min-width: 48px;
    min-height: 48px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
    font-weight: 600;
    background: var(--wa-surface);
    color: var(--wa-text);
}

.chat-row-main {
    flex: 1;
    min-width: 0;
}

.chat-row-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 2px;
}

.chat-row-name {
    font-weight: 600;
    font-size: 1rem;
    color: var(--wa-text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row-time {
    font-size: 0.75rem;
    color: var(--wa-text-muted);
    flex-shrink: 0;
}

.chat-row-preview {
    font-size: 0.875rem;
    color: var(--wa-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.chat-row-empty {
    justify-content: center;
    color: var(--wa-text-muted);
    pointer-events: none;
}

.chat-row-empty a {
    color: var(--wa-green);
    pointer-events: auto;
}

.chat-row-unread {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    font-size: 0.7rem;
    font-weight: 700;
    line-height: 20px;
    text-align: center;
    color: #111b21;
    background: var(--wa-unread-badge);
    border-radius: 10px;
}

/* ---- Thread (detail) ---- */
.dm-thread {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    min-width: 0;
    background: var(--wa-bg);
    position: relative;
}

/* Tablet and mobile: same UI; thread slides over, bottom nav, full-screen conversation */
@media (max-width: 1024px) {
    .dm-thread {
        position: absolute;
        inset: 0;
        transform: translateX(100%);
        transition: transform 0.2s ease;
        z-index: 2;
    }

    .dm-shell.thread-open .dm-thread {
        transform: translateX(0);
    }

    .dm-shell.thread-open .dm-list {
        display: none;
    }

    .main.chats-main {
        padding: 0;
        min-height: var(--app-viewport-height);
    }

    .dm-shell {
        height: calc(var(--app-viewport-height) - var(--mobile-bottom-nav-space));
        max-height: calc(var(--app-viewport-height) - var(--mobile-bottom-nav-space));
    }

    /* Chat list is the scroll container on Chats tab; reserve space so last row sits above bottom nav. */
    .dm-list .chat-list {
        padding-bottom: max(
            var(--mobile-bottom-nav-space),
            calc(var(--mobile-bottom-nav-height, 62px) + env(safe-area-inset-bottom, 0px))
        );
    }

    body.keyboard-open .dm-shell {
        height: var(--app-viewport-height);
        max-height: var(--app-viewport-height);
    }

    body.conversation-view .bottom-nav {
        display: none !important;
    }

    body.conversation-view .dm-shell {
        height: var(--app-viewport-height);
        max-height: var(--app-viewport-height);
    }
}

.thread-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: transparent;
    border-bottom: 1px solid transparent;
    color: var(--wa-text);
    position: sticky;
    top: 0;
    z-index: 12;
    transition: background 180ms ease, border-color 180ms ease, backdrop-filter 180ms ease;
}

.list-header {
    background: transparent;
    border-bottom-color: transparent;
    transition: background 180ms ease, border-color 180ms ease, backdrop-filter 180ms ease;
}

.list-header.scroll-reveal-visible,
.thread-header.scroll-reveal-visible {
    background: rgba(32, 44, 51, 0.72);
    border-bottom-color: rgba(255, 255, 255, 0.08);
    backdrop-filter: blur(8px);
}

.thread-header .back {
    display: none;
    width: 44px;
    height: 44px;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--wa-text);
    font-size: 1.4rem;
    cursor: pointer;
    border-radius: 50%;
    transition: background 0.15s;
    position: relative;
    z-index: 2;
    pointer-events: auto;
}

.thread-header .back:hover,
.thread-header .back:active {
    background: var(--wa-surface);
}

.thread-header .back .ui-icon,
.thread-action-btn .ui-icon,
.composer .attach .ui-icon,
.composer .send .ui-icon,
.chats-search-toggle .ui-icon {
    width: 1.15rem;
    height: 1.15rem;
    color: currentColor;
}

@media (max-width: 1024px) {
    .thread-header .back {
        display: inline-flex;
    }
}

.thread-header-info {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.thread-title {
    font-weight: 600;
    font-size: 1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.thread-status {
    font-size: 0.8rem;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.thread-header-info.thread-header-clickable {
    cursor: pointer;
}

.thread-header-avatar {
    display: inline-flex;
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: var(--wa-surface);
    color: var(--wa-text);
    overflow: hidden;
    flex-shrink: 0;
}

.thread-header-actions,
.thread-status,
.thread-settings-btn {
    display: none !important;
}

/* ---- Messages wrap: scrollable area + floating pill ---- */
.messages-wrap {
    flex: 1;
    min-height: 0;
    position: relative;
    display: flex;
    flex-direction: column;
}

.messages-wrap .new-messages-pill {
    position: absolute;
    bottom: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 15;
}

/* ---- Messages area (scrollable like WhatsApp) ---- */
.dm-thread .messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background: var(--wa-bg);
    -webkit-overflow-scrolling: touch;
    scroll-padding-bottom: 100px;
}

/* ---- Message bubbles: float, max 70%, timestamp inside ---- */
.message {
    max-width: 70%;
    padding: 6px 10px 10px;
    border-radius: 8px;
    font-size: 0.9375rem;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    align-self: flex-start;
    animation: msg-pop 0.2s ease;
}

.message.outgoing {
    align-self: flex-end;
    background: var(--wa-bubble-out);
    color: var(--wa-text);
    border-top-right-radius: 2px;
}

.message.incoming {
    background: var(--wa-bubble-in);
    color: var(--wa-text);
    border-top-left-radius: 2px;
}

.msg-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 4px;
    margin-left: 8px;
}

.msg-sender {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--wa-text-muted);
    margin-bottom: 4px;
}

.msg-sender-avatar {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    object-fit: cover;
    display: inline-block;
    flex-shrink: 0;
}

.chat-avatar {
    position: relative;
    overflow: hidden;
    border-radius: 50%;
    background: var(--wa-surface);
}

.chat-avatar-fallback {
    position: absolute;
    inset: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    z-index: 0;
}

.chat-avatar-fallback svg {
    width: 62%;
    height: 62%;
}

.chat-avatar-img {
    position: relative;
    z-index: 1;
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.message .msg-time {
    font-size: 0.65rem;
    color: var(--wa-text-muted);
    flex-shrink: 0;
}

.message.outgoing .msg-time {
    color: rgba(255, 255, 255, 0.7);
}

/* Delivery/read status: only on outgoing messages */
.msg-status {
    font-size: 0.75rem;
    display: inline-flex;
    align-items: center;
    gap: 1px;
    flex-shrink: 0;
    margin-left: 2px;
}

.msg-status-sending {
    color: rgba(255, 255, 255, 0.5);
}

.msg-status-delivered {
    color: rgba(255, 255, 255, 0.7);
}

.msg-status-read {
    color: var(--wa-green);
}

@keyframes msg-pop {
    0% {
        transform: scale(0.96);
        opacity: 0.8;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.msg-media-wrap {
    margin-top: 6px;
}

.msg-media-img {
    max-width: 260px;
    max-height: 260px;
    border-radius: 6px;
    display: block;
    cursor: pointer;
}

.msg-media-video {
    max-width: 260px;
    max-height: 200px;
    border-radius: 6px;
    display: block;
}

.msg-media-download {
    color: var(--wa-green);
    font-size: 0.875rem;
}

/* ---- Composer: pill input, attach, circular send (sticky at bottom, always usable) ---- */
.composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 16px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    background: var(--wa-list);
    border-top: 1px solid rgba(255, 255, 255, 0.06);
    flex-shrink: 0;
    position: relative;
    z-index: 10;
}

.composer .attach {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border: none;
    background: transparent;
    color: var(--wa-text-muted);
    font-size: 1.25rem;
    cursor: pointer;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.composer .attach:hover {
    background: var(--wa-surface);
    color: var(--wa-text);
}

.composer #msgInput {
    flex: 1;
    min-width: 0;
    min-height: 44px;
    max-height: 120px;
    height: 44px;
    padding: 12px 16px;
    border: none;
    border-radius: 22px;
    background: var(--wa-surface);
    color: var(--wa-text);
    font-size: 16px;
    line-height: 1.35;
    resize: none;
    outline: none;
    overflow-y: hidden;
}

.composer #msgInput::placeholder {
    color: var(--wa-text-muted);
}

.composer:focus-within {
    box-shadow: inset 0 1px 0 rgba(0, 168, 132, 0.25);
}

.composer .send {
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--wa-green);
    color: #111b21;
    font-size: 1.2rem;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, opacity 0.15s;
}

.composer .send:hover:not(:disabled) {
    background: var(--wa-green-active);
}

.composer .send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ---- Date separator (Today, Yesterday, etc.) ---- */
.msg-date-separator {
    text-align: center;
    font-size: 0.75rem;
    color: var(--wa-text-muted);
    padding: 8px 0 4px;
    margin: 4px 0;
}

/* ---- Typing indicator placeholder ---- */
.typing-indicator {
    align-self: flex-start;
    padding: 8px 14px;
    background: var(--wa-bubble-in);
    border-radius: 8px;
    border-top-left-radius: 2px;
    font-size: 0.8rem;
    color: var(--wa-text-muted);
}

/* ---- Skeleton (optional) ---- */
.chat-skeleton-item {
    display: flex;
    gap: 12px;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

.chat-skeleton-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--wa-surface);
    flex-shrink: 0;
}

.chat-skeleton-main {
    flex: 1;
    min-width: 0;
}

.chat-skeleton-line {
    height: 10px;
    border-radius: 4px;
    background: var(--wa-surface);
    margin-bottom: 6px;
}

.chat-skeleton-line.name {
    width: 50%;
}

.chat-skeleton-line.preview {
    width: 80%;
}

/* ---- Lightbox (unchanged) ---- */
.media-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(11, 20, 26, 0.92);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s;
}

.media-lightbox-open {
    opacity: 1;
    visibility: visible;
}

.media-lightbox-img {
    max-width: 100%;
    max-height: 90vh;
    object-fit: contain;
    border-radius: 10px;
}

.media-lightbox-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: white;
    font-size: 1.7rem;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    cursor: pointer;
}

/* ---- Inbox filter (All - Teams - Direct) ---- */
.inbox-filter {
    display: flex;
    gap: 6px;
    padding: 8px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
    background: var(--wa-list);
}

.inbox-filter.hidden {
    display: none !important;
}

.inbox-filter-btn {
    font-weight: 600;
}

.inbox-filter-btn.active {
    background: var(--wa-green);
    color: #fff;
    border-color: var(--wa-green);
}

.chat-row-team .avatar {
    background: rgba(31, 111, 95, 0.4);
}

.chat-row-meta {
    font-size: 0.85em;
    color: var(--wa-text-muted);
}

/* ---- Thread header: title + settings (team) ---- */
.thread-title-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    min-width: 0;
}

.thread-settings-btn {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border: none;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 8px;
    color: var(--wa-text);
    cursor: pointer;
    font-size: 1rem;
}

.thread-settings-btn.hidden {
    display: none !important;
}

.thread-settings-btn:hover {
    background: rgba(255, 255, 255, 0.14);
}

/* ---- Team Info panel (slide-over) ---- */
.team-info-panel {
    position: fixed;
    inset: 0;
    z-index: 200;
    display: flex;
    justify-content: flex-end;
    pointer-events: none;
}

.team-info-panel:not(.hidden) {
    pointer-events: auto;
}

.team-info-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
}

.team-info-slide {
    position: relative;
    width: min(360px, 92vw);
    max-width: 100%;
    height: 100%;
    margin: 0;
    border-radius: 0;
    border-left: 1px solid rgba(255, 255, 255, 0.1);
    overflow-y: auto;
    padding: 20px;
}

.team-info-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.team-info-title {
    font-size: 1.1rem;
    margin: 0;
}

.team-info-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--wa-text-muted);
    cursor: pointer;
    padding: 0 4px;
}

.team-info-description {
    margin-bottom: 16px;
    font-size: 0.95rem;
}

.team-info-subtitle {
    font-size: 0.9rem;
    margin: 0 0 8px;
    color: var(--wa-text-muted);
}

.team-info-members {
    list-style: none;
    padding: 0;
    margin: 0 0 20px;
}

.team-info-members li {
    padding: 6px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.team-info-role {
    display: inline-block;
    min-width: 56px;
    font-size: 0.8rem;
    color: var(--wa-text-muted);
}

.team-info-actions {
    margin-top: 16px;
}

.teams-landing-card {
    max-width: 520px;
    padding: 24px;
}

.teams-landing-card .section-title {
    margin-bottom: 12px;
}

.teams-landing-list {
    margin: 16px 0;
    padding-left: 1.2em;
}

.teams-landing-list li {
    margin-bottom: 8px;
}

.teams-landing-card .btn {
    margin-top: 8px;
}
