/* ============ Chat Icon Button ============ */
.nav-btn-chat {
    position: relative;
    padding: 0.5rem;
    color: var(--text-secondary);
    width: 42px;
    height: 42px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: none;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: all 0.2s ease;
}

.nav-btn-chat:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

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

.chat-badge {
    position: absolute;
    top: 2px;
    right: 0px;
    min-width: 15px;
    height: 15px;
    padding: 0 4px;
    background: var(--accent-danger);
    border-radius: var(--radius-full);
    font-size: 0.6rem;
    font-weight: 700;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    line-height: 1;
}

.chat-badge.hidden {
    display: none;
}

/* ============ Chat Panel (Dropdown) ============ */
.chat-panel {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    width: 360px;
    max-height: 520px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-primary);
    border-radius: var(--radius-lg);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: chatSlideIn 0.2s ease;
}

.chat-panel.hidden {
    display: none;
}

@keyframes chatSlideIn {
    from { opacity: 0; transform: translateY(-8px); }
    to   { opacity: 1; transform: translateY(0); }
}

.chat-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.875rem 1rem;
    border-bottom: 1px solid var(--border-primary);
    flex-shrink: 0;
}

.chat-panel-header h3 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.chat-new-btn {
    background: none;
    border: none;
    color: var(--accent-primary);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    padding: 0.3rem 0.6rem;
    border-radius: var(--radius-sm);
    transition: background 0.2s;
}

.chat-new-btn:hover {
    background: rgba(139, 92, 246, 0.15);
}

/* Conversation list */
.chat-convo-list {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.chat-convo-list::-webkit-scrollbar { width: 4px; }
.chat-convo-list::-webkit-scrollbar-thumb {
    background: var(--border-primary);
    border-radius: 4px;
}

.chat-convo-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: background 0.15s;
    border-bottom: 1px solid var(--border-primary);
}

.chat-convo-item:last-child {
    border-bottom: none;
}

.chat-convo-item:hover {
    background: var(--bg-tertiary);
}

.chat-convo-item.has-unread {
    background: rgba(139, 92, 246, 0.06);
}

.chat-convo-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

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

.chat-convo-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}

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

.chat-convo-time {
    font-size: 0.7rem;
    color: var(--text-tertiary);
    flex-shrink: 0;
}

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

.chat-convo-unread-pill {
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    background: var(--accent-primary);
    border-radius: 999px;
    font-size: 0.65rem;
    font-weight: 700;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 1rem;
    color: var(--text-tertiary);
    text-align: center;
}

.chat-empty-icon {
    font-size: 2.5rem;
    margin-bottom: 0.75rem;
    opacity: 0.5;
}

.chat-empty p {
    font-size: 0.85rem;
    margin: 0;
}

/* ============ New Chat Search Modal ============ */
.chat-search-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 2000;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 15vh;
    animation: chatFadeIn 0.15s ease;
}

.chat-search-overlay.hidden {
    display: none;
}

@keyframes chatFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.chat-search-box {
    width: 400px;
    max-width: 95vw;
    background: var(--bg-secondary);
    border: 1px solid var(--border-primary);
    border-radius: var(--radius-lg);
    box-shadow: 0 25px 50px rgba(0, 0, 0, 0.4);
    overflow: hidden;
}

.chat-search-input-wrap {
    display: flex;
    align-items: center;
    padding: 0.875rem 1rem;
    gap: 0.5rem;
    border-bottom: 1px solid var(--border-primary);
}

.chat-search-input-wrap span {
    color: var(--text-tertiary);
    font-size: 1rem;
}

.chat-search-input {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 0.9rem;
    outline: none;
    font-family: inherit;
}

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

.chat-search-results {
    max-height: 300px;
    overflow-y: auto;
}

.chat-search-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.65rem 1rem;
    cursor: pointer;
    transition: background 0.15s;
}

.chat-search-item:hover {
    background: var(--bg-tertiary);
}

.chat-search-item img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-search-item span {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
}

.chat-search-empty {
    padding: 2rem 1rem;
    text-align: center;
    color: var(--text-tertiary);
    font-size: 0.85rem;
}

/* ============ Floating Chat Window ============ */
.chat-window {
    position: fixed;
    bottom: 0;
    right: 24px;
    width: 340px;
    height: 460px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-primary);
    border-bottom: none;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.25);
    z-index: 1500;
    display: flex;
    flex-direction: column;
    animation: chatWindowSlideUp 0.25s ease;
}

.chat-window.minimized {
    height: 48px;
    overflow: hidden;
}

.chat-window.hidden {
    display: none;
}

@keyframes chatWindowSlideUp {
    from { transform: translateY(100%); opacity: 0; }
    to   { transform: translateY(0); opacity: 1; }
}

/* Window header */
.chat-window-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border-primary);
    cursor: pointer;
    flex-shrink: 0;
    background: var(--bg-primary);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    transition: background 0.15s;
}

.chat-window-header:hover {
    background: var(--bg-tertiary);
}

.chat-window-header img {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
}

.chat-window-header-name {
    flex: 1;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-window-header-name a {
    color: inherit;
    text-decoration: none;
}

.chat-window-header-name a:hover {
    color: var(--accent-primary);
}

.chat-window-actions {
    display: flex;
    gap: 0.25rem;
}

.chat-window-actions button {
    background: none;
    border: none;
    color: var(--text-tertiary);
    font-size: 1rem;
    cursor: pointer;
    padding: 0.2rem 0.35rem;
    border-radius: var(--radius-sm);
    transition: all 0.15s;
    line-height: 1;
}

.chat-window-actions button:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

/* Messages area */
.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    overscroll-behavior: contain;
}

.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border-primary);
    border-radius: 4px;
}

.chat-msg {
    max-width: 80%;
    padding: 0.5rem 0.75rem;
    border-radius: 14px;
    font-size: 0.84rem;
    line-height: 1.45;
    word-break: break-word;
}

.chat-msg.mine {
    align-self: flex-end;
    background: var(--accent-primary);
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-msg.theirs {
    align-self: flex-start;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
}

.chat-msg-images {
    display: flex;
    gap: 4px;
    margin-top: 4px;
}

.chat-msg-images img {
    max-width: 140px;
    max-height: 140px;
    border-radius: 8px;
    cursor: pointer;
    object-fit: cover;
    transition: opacity 0.15s;
}

.chat-msg-images img:hover {
    opacity: 0.85;
}

.chat-msg-time {
    font-size: 0.65rem;
    color: var(--text-tertiary);
    margin-top: 2px;
    text-align: right;
}

.chat-msg.mine .chat-msg-time {
    color: rgba(255, 255, 255, 0.7);
}

.chat-date-divider {
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-tertiary);
    padding: 0.5rem 0;
}

.chat-load-older {
    text-align: center;
    padding: 0.5rem;
}

.chat-load-older button {
    background: none;
    border: none;
    color: var(--accent-primary);
    font-size: 0.78rem;
    cursor: pointer;
    font-weight: 500;
}

.chat-load-older button:hover {
    text-decoration: underline;
}

.chat-messages-empty {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-tertiary);
    font-size: 0.84rem;
}

/* Input area */
.chat-input-area {
    border-top: 1px solid var(--border-primary);
    flex-shrink: 0;
}

.chat-image-previews {
    display: flex;
    gap: 6px;
    padding: 6px 10px 0;
}

.chat-image-previews:empty {
    display: none;
}

.chat-img-preview {
    position: relative;
    width: 48px;
    height: 48px;
}

.chat-img-preview img {
    width: 100%;
    height: 100%;
    border-radius: 6px;
    object-fit: cover;
}

.chat-img-preview .remove-img {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 16px;
    height: 16px;
    background: var(--accent-danger);
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 0.6rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.chat-input-row {
    display: flex;
    align-items: flex-end;
    gap: 0.4rem;
    padding: 0.5rem 0.6rem;
}

.chat-input-row textarea {
    flex: 1;
    background: var(--bg-tertiary);
    border: 1px solid var(--border-primary);
    border-radius: 16px;
    padding: 0.45rem 0.75rem;
    font-size: 0.84rem;
    font-family: inherit;
    color: var(--text-primary);
    resize: none;
    outline: none;
    max-height: 100px;
    min-height: 34px;
    line-height: 1.4;
    transition: border-color 0.2s;
}

.chat-input-row textarea:focus {
    border-color: var(--accent-primary);
}

.chat-input-row textarea::placeholder {
    color: var(--text-tertiary);
}

.chat-attach-btn,
.chat-send-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.35rem;
    border-radius: var(--radius-sm);
    transition: all 0.15s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chat-attach-btn {
    color: var(--text-tertiary);
    font-size: 1.1rem;
}

.chat-attach-btn:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
}

.chat-send-btn {
    color: var(--accent-primary);
    font-size: 1.15rem;
}

.chat-send-btn:hover {
    background: rgba(139, 92, 246, 0.15);
}

.chat-send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.chat-char-counter {
    font-size: 0.6rem;
    color: var(--text-tertiary);
    text-align: right;
    padding: 0 0.75rem 0.35rem;
}

.chat-char-counter.near-limit {
    color: var(--accent-warning, #f59e0b);
}

.chat-char-counter.at-limit {
    color: var(--accent-danger);
}

/* ============ Image Lightbox ============ */
.chat-lightbox {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.9);
    z-index: 3000;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: zoom-out;
}

.chat-lightbox.hidden {
    display: none;
}

.chat-lightbox img {
    max-width: 90vw;
    max-height: 90vh;
    border-radius: 8px;
    object-fit: contain;
}

/* ============ Responsive ============ */
@media (max-width: 480px) {
    .chat-panel {
        position: fixed;
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-height: 70vh;
        border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    }

    .chat-window {
        right: 0;
        width: 100%;
        height: 100vh;
        border-radius: 0;
    }

    .chat-search-box {
        width: 100%;
        max-width: 100%;
        border-radius: 0;
        height: 100vh;
    }
}

/* ============ Light Theme Overrides ============ */
html[data-theme="light"] .chat-panel,
html[data-theme="light"] .chat-window,
html[data-theme="light"] .chat-search-box {
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}

html[data-theme="light"] .chat-msg.theirs {
    background: #f0f0f5;
}

html[data-theme="light"] .chat-window-header {
    background: #f8f8fa;
}
