/* Chat Widget CSS */
:root {
    --chat-width: 360px;
    --chat-height: 550px;
    --chat-header-height: 60px;
    --chat-z-index: 9999;
}

/* Launcher Button */
.chat-launcher {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 60px;
    height: 60px;
    background-color: var(--primary-color);
    border-radius: 50%;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: var(--chat-z-index);
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    color: #fff;
    font-size: 24px;
}

.chat-launcher:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.2);
}

/* Pulse Animation for Unread Messages */
@keyframes chatPulse {
    0% {
        box-shadow: 0 0 0 0 rgba(var(--primary-r, 59), var(--primary-g, 130), var(--primary-b, 246), 0.7);
        transform: scale(1);
    }
    70% {
        box-shadow: 0 0 0 15px rgba(var(--primary-r, 59), var(--primary-g, 130), var(--primary-b, 246), 0);
        transform: scale(1.05);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(var(--primary-r, 59), var(--primary-g, 130), var(--primary-b, 246), 0);
        transform: scale(1);
    }
}

.chat-launcher.has-unread {
    animation: chatPulse 2s infinite;
}

.chat-launcher .badge-count {
    position: absolute;
    top: -5px;
    right: -5px;
    background-color: var(--white);
    color: var(--danger);
    font-size: 12px;
    font-weight: bold;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--white);
    padding: 0 4px;
}

/* Chat Container */
.chat-widget {
    position: fixed;
    bottom: 90px;
    right: 20px;
    width: var(--chat-width);
    height: var(--chat-height);
    background-color: var(--theme-card-bg, #fff);
    border-radius: 16px;
    box-shadow: 0 5px 40px rgba(0, 0, 0, 0.16);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: var(--chat-z-index);
    transform-origin: bottom right;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    opacity: 0;
    pointer-events: none;
    transform: scale(0.9) translateY(20px);
    border: 1px solid var(--theme-border-color, #eee);
}

.chat-widget.active {
    opacity: 1;
    pointer-events: all;
    transform: scale(1) translateY(0);
}

/* Chat Header */
.chat-header {
    height: var(--chat-header-height);
    background-color: var(--primary-color);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    flex-shrink: 0;
}

.chat-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.chat-header-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chat-header-text h5 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.chat-header-text small {
    font-size: 12px;
    opacity: 0.8;
}

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

.chat-header-btn {
    background: transparent;
    border: none;
    color: #fff;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.2s;
}

.chat-header-btn:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Chat Views (List & Conversation) */
.chat-body {
    flex: 1;
    position: relative;
    overflow: hidden;
    background-color: var(--theme-bg-secondary, #f8f9fa);
}

.chat-view {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s ease;
    background-color: var(--theme-bg-secondary, #f8f9fa);
}

/* List View */
.view-list {
    z-index: 1;
    transform: translateX(0);
}

.view-list.hidden {
    transform: translateX(-100%);
}

.chat-search {
    padding: 12px;
    background-color: var(--theme-card-bg, #fff);
    border-bottom: 1px solid var(--theme-border-color, #eee);
}

.search-input-wrapper {
    position: relative;
}

.search-input-wrapper i {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
}

.chat-search-input {
    width: 100%;
    padding: 8px 12px 8px 36px;
    border: 1px solid var(--theme-border-color, #ddd);
    border-radius: 20px;
    background-color: var(--theme-bg-tertiary, #f1f3f4);
    color: var(--theme-text-primary, #333);
    outline: none;
}

.chat-list {
    flex: 1;
    overflow-y: auto;
    background-color: var(--theme-card-bg, #fff);
}

.chat-item {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    cursor: pointer;
    transition: background-color 0.2s;
    border-bottom: 1px solid var(--theme-border-color, #f0f0f0);
}

.chat-item:hover {
    background-color: var(--bg-hover, #f5f5f5);
}

.chat-item-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background-color: #ddd;
    margin-right: 12px;
    position: relative;
    flex-shrink: 0;
}

.chat-item-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.chat-item-info {
    flex: 1;
    min-width: 0; /* For truncation */
}

.chat-item-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.chat-item-name {
    font-weight: 600;
    color: var(--theme-text-primary, #333);
    font-size: 15px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-time {
    font-size: 11px;
    color: var(--text-secondary, #888);
}

.chat-item-preview {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-item-message {
    color: var(--text-secondary, #666);
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    margin-right: 8px;
}

.chat-unread-badge {
    background-color: var(--success);
    color: #fff;
    font-size: 10px;
    height: 18px;
    min-width: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    font-weight: bold;
}

/* Conversation View */
.view-conversation {
    z-index: 2;
    transform: translateX(100%);
    background-color: #e5ddd5; /* WhatsApp default bg */
}

/* Dark mode override for conversation background */
.theme-dark .view-conversation {
    background-color: #0d1117;
    background-image: linear-gradient(rgba(255,255,255,0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.05) 1px, transparent 1px);
    background-size: 20px 20px;
}

.view-conversation.active {
    transform: translateX(0);
}

.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    background-image: url('data:image/svg+xml,%3Csvg width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"%3E%3Cpath d="M0 0h100v100H0z" fill="none"/%3E%3Cpath d="M10 10h10v10H10zM50 50h10v10H50z" fill="%23000" fill-opacity="0.03"/%3E%3C/svg%3E');
}

.theme-dark .messages-area {
    background-image: none; /* Already handled by .view-conversation bg */
}

.message-bubble {
    max-width: 80%;
    padding: 8px 12px;
    border-radius: 8px;
    position: relative;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.message-bubble.sent {
    align-self: flex-end;
    background-color: #d9fdd3; /* WhatsApp sent color */
    border-top-right-radius: 0;
    color: #111;
}

.theme-dark .message-bubble.sent {
    background-color: var(--primary-dark);
    color: #fff;
}

.message-bubble.received {
    align-self: flex-start;
    background-color: #fff;
    border-top-left-radius: 0;
    color: #111;
}

.theme-dark .message-bubble.received {
    background-color: var(--theme-bg-tertiary);
    color: var(--theme-text-primary);
}

.message-meta {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 4px;
    margin-top: 4px;
    font-size: 10px;
    opacity: 0.7;
}

.message-check {
    color: #53bdeb;
}

/* Typing indicator (like WhatsApp) */
.typing-indicator {
    display: none;
    align-self: flex-start;
    margin: 8px 0;
}

.typing-bubble {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 10px 12px;
    border-radius: 14px;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

.theme-dark .typing-bubble {
    background: var(--theme-bg-tertiary);
}

.typing-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: rgba(0,0,0,0.35);
    animation: typingBounce 1.1s infinite ease-in-out;
}

.theme-dark .typing-dot {
    background: rgba(255,255,255,0.55);
}

.typing-dot:nth-child(2) { animation-delay: 0.15s; }
.typing-dot:nth-child(3) { animation-delay: 0.30s; }

@keyframes typingBounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.55; }
    40% { transform: translateY(-4px); opacity: 1; }
}

/* Audio Player (Custom) */
.audio-player {
    width: min(320px, 100%);
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: 14px;
}

.message-bubble.sent .audio-player {
    background: rgba(255, 255, 255, 0.55);
}

.message-bubble.received .audio-player {
    background: rgba(0, 0, 0, 0.04);
}

.theme-dark .message-bubble.sent .audio-player {
    background: rgba(255, 255, 255, 0.16);
}

.theme-dark .message-bubble.received .audio-player {
    background: rgba(255, 255, 255, 0.08);
}

.audio-player-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background: var(--primary-color);
    color: #fff;
    flex: 0 0 auto;
}

.audio-player-btn:active {
    transform: scale(0.98);
}

.audio-player-track {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.audio-player-range {
    width: 100%;
}

.audio-player-time {
    font-size: 11px;
    opacity: 0.75;
    display: flex;
    justify-content: space-between;
}

.audio-player-el {
    display: none;
}

/* Media Styles */
.media-preview {
    width: 100%;
    margin: 2px 0;
}

.media-preview video {
    max-width: 100%;
    border-radius: 4px;
    background: #000;
}

.media-preview audio {
    width: min(280px, 100%);
    display: block;
}

.media-preview img {
    cursor: pointer;
    transition: opacity 0.2s;
}

.media-preview img:hover {
    opacity: 0.9;
}

/* Lightbox Modal */
.chat-lightbox {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s;
}

.chat-lightbox.active {
    opacity: 1;
    visibility: visible;
}

.chat-lightbox img {
    max-width: 90%;
    max-height: 90%;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    border-radius: 4px;
}

.chat-lightbox-close {
    position: absolute;
    top: 20px;
    right: 20px;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 30px;
    cursor: pointer;
}

.chat-input-area {
    padding: 10px;
    background-color: var(--theme-card-bg, #fff);
    display: flex;
    align-items: center;
    gap: 10px;
    border-top: 1px solid var(--theme-border-color, #ddd);
    position: relative; /* For attachment menu positioning */
}

.chat-action-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 20px;
    cursor: pointer;
    padding: 4px;
    transition: color 0.2s;
}

.chat-action-btn:hover {
    color: var(--primary-color);
}

.chat-input {
    flex: 1;
    padding: 10px 14px;
    border-radius: 20px;
    border: 1px solid var(--theme-border-color, #ddd);
    background-color: var(--theme-bg-tertiary, #fff);
    color: var(--theme-text-primary);
    outline: none;
    max-height: 100px;
    overflow-y: auto;
}

.chat-send-btn {
    background-color: var(--success);
    color: #fff;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s;
}

.chat-send-btn:hover {
    transform: scale(1.1);
}

/* Attachment Menu */
.chat-attachment-menu {
    position: absolute;
    bottom: 70px; /* Above input area */
    left: 10px;
    background-color: var(--theme-card-bg, #fff);
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    padding: 8px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    z-index: 10;
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
    transition: all 0.2s cubic-bezier(0.25, 0.8, 0.25, 1);
    min-width: 140px;
    border: 1px solid var(--theme-border-color, #eee);
}

.chat-attachment-menu.active {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.attachment-option {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    cursor: pointer;
    border-radius: 8px;
    transition: background-color 0.2s;
    color: var(--theme-text-primary, #333);
}

.attachment-option:hover {
    background-color: var(--bg-hover, #f5f5f5);
}

.attachment-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 14px;
}

.icon-image {
    background: linear-gradient(135deg, #ac2deb, #d95ce9);
}

.icon-audio {
    background: linear-gradient(135deg, #f53d2d, #f66d4c);
}

.icon-video {
    background: linear-gradient(135deg, #007bfb, #00afff);
}

.attachment-label {
    font-size: 14px;
    font-weight: 500;
}

/* Date Divider */
.date-divider {
    text-align: center;
    margin: 12px 0;
    position: relative;
}

.date-divider span {
    background-color: rgba(225, 245, 254, 0.9);
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 11px;
    color: #555;
    box-shadow: 0 1px 1px rgba(0,0,0,0.1);
}

.theme-dark .date-divider span {
    background-color: rgba(50, 50, 50, 0.9);
    color: #ccc;
}

/* Responsive */
@media (max-width: 480px) {
    .chat-widget {
        right: 0;
        bottom: 0;
        width: 100%;
        height: 100%;
        border-radius: 0;
        transform: translateY(100%);
    }

    .chat-widget.active {
        transform: translateY(0);
    }
    
    .chat-launcher {
        bottom: 20px;
        right: 20px;
    }
}
