/* Chat / inbox (Instagram-style) */
#view-chat {
    display: none;
    flex-direction: column;
    min-height: calc(100vh - 60px);
}

#view-chat.active {
    display: flex;
}

.chat-shell {
    display: flex;
    flex: 1;
    min-height: 480px;
    border-top: 1px solid var(--border);
}

.chat-inbox-panel {
    width: 100%;
    max-width: 100%;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.chat-thread-panel {
    flex: 1;
    display: none;
    flex-direction: column;
    min-width: 0;
}

.chat-thread-panel.open {
    display: flex;
}

@media (min-width: 769px) {
    .chat-shell {
        width: 100%;
        min-height: calc(100vh - 2px);
    }

    .chat-inbox-panel {
        width: min(360px, 38%);
        max-width: 360px;
        flex-shrink: 0;
        transition: width 0.2s ease, max-width 0.2s ease;
    }

    .chat-shell:not(.thread-open) .chat-inbox-panel {
        width: 100%;
        max-width: none;
        border-right: none;
    }

    .chat-thread-panel {
        display: none;
        flex: 1;
        border-left: 1px solid var(--border);
    }

    .chat-thread-panel.open {
        display: flex;
    }
}

@media (max-width: 768px) {
    .chat-shell.thread-open .chat-inbox-panel { display: none; }
    .chat-shell.thread-open .chat-thread-panel { display: flex; width: 100%; }

    .chat-shell {
        min-height: 0;
        flex: 1;
        border-top: none;
    }

    #view-chat.active {
        height: 100%;
        min-height: 0;
    }

    .chat-inbox-head {
        padding: 12px 14px;
    }

    .chat-compose {
        padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px));
    }

    .chat-thread-head {
        padding: 10px 12px;
    }
}

.chat-inbox-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--border);
}

.chat-inbox-head h2 {
    font-size: 1.25rem;
    font-weight: 800;
    margin: 0;
}

.chat-inbox-actions {
    display: flex;
    gap: 8px;
}

.chat-icon-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 999px;
    background: transparent;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
}

.chat-icon-btn svg.nav-icon-svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
}

.chat-icon-btn:hover { background: var(--hover-surface-strong); }
.chat-icon-btn:active { background: var(--hover-surface-strong); }

.chat-thread-list {
    flex: 1;
    overflow-y: auto;
}

.chat-thread-row {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 12px 16px;
    border: none;
    border-bottom: 1px solid var(--border);
    background: transparent;
    color: var(--text-primary);
    text-align: left;
    cursor: pointer;
    transition: background 0.15s;
}

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

.chat-thread-row.unread .chat-thread-preview {
    font-weight: 600;
    color: var(--text-primary);
}

.chat-thread-row.unread .chat-thread-title {
    font-weight: 700;
}

.chat-thread-unread {
    flex-shrink: 0;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--chat-unread-badge-fill, linear-gradient(180deg, #f0f0f0, #d8d8d8));
    color: var(--chat-unread-badge-ink, #111111);
    font-size: 0.65rem;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
    margin-right: 6px;
    align-self: center;
    box-shadow: 0 0 8px var(--chat-unread-badge-glow, rgba(255, 255, 255, 0.35));
}

.chat-thread-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, #444, #222);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex-shrink: 0;
}

.chat-thread-body {
    flex: 1;
    min-width: 0;
}

.chat-thread-title {
    font-size: 0.95rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-thread-preview {
    font-size: 0.82rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

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

.chat-empty {
    padding: 48px 24px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
}

.chat-thread-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.chat-back-btn {
    display: none;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 1.2rem;
    cursor: pointer;
    padding: 4px 8px;
}

@media (max-width: 768px) {
    .chat-back-btn { display: block; }
}

.chat-thread-title-lg {
    font-weight: 700;
    font-size: 1rem;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-msg {
    max-width: 75%;
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 0.9rem;
    line-height: 1.4;
    word-break: break-word;
}

.chat-msg.mine {
    align-self: flex-end;
    background: #1d9bf0;
    color: #fff;
    border-bottom-right-radius: 4px;
}

.chat-msg.theirs {
    align-self: flex-start;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.chat-msg-author {
    font-size: 0.72rem;
    font-weight: 600;
    margin-bottom: 4px;
    opacity: 0.85;
}

.chat-compose {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

.chat-compose input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--bg-elevated);
    color: var(--text-primary);
    font: inherit;
    font-size: 0.9rem;
}

.chat-compose button {
    padding: 10px 18px;
    border: none;
    border-radius: 999px;
    background: #1d9bf0;
    color: #fff;
    font-weight: 700;
    cursor: pointer;
}

.chat-compose button:disabled { opacity: 0.5; }

.chat-new-overlay {
    position: fixed;
    inset: 0;
    z-index: 300;
    background: rgba(0, 0, 0, 0.75);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.chat-new-overlay.open { display: flex; }

.chat-new-card {
    width: 100%;
    max-width: 420px;
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: 16px;
    padding: 24px;
}

.chat-new-card h3 {
    margin: 0 0 16px;
    font-size: 1.1rem;
}

.chat-new-tabs {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}

.chat-new-tab {
    flex: 1;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
}

.chat-new-tab.active {
    background: var(--hover-surface-strong);
    color: var(--text-primary);
    border-color: var(--border-light);
}

.chat-new-card input,
.chat-new-card textarea {
    width: 100%;
    padding: 12px;
    margin-bottom: 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-surface);
    color: var(--text-primary);
    font: inherit;
}

.chat-new-dm-search {
    position: relative;
    margin-bottom: 12px;
}

.chat-new-dm-search input {
    margin-bottom: 0;
}

.chat-new-dm-results {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 4px);
    z-index: 5;
    max-height: 240px;
    overflow-y: auto;
    background: var(--bg-elevated);
    border: 1px solid var(--border-light);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.chat-new-dm-results .sidebar-search-result {
    width: 100%;
    text-align: left;
}

.chat-new-dm-results .sidebar-search-status,
.chat-new-dm-results .sidebar-search-empty {
    padding: 12px 14px;
    margin: 0;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.chat-new-dm-results .sidebar-search-results-head {
    padding: 8px 14px 4px;
}

.chat-new-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 8px;
}
