/**
 * Message Components Styles
 * Specific styles for different message types and components
 */

/* ===== CLEAN MESSAGE LAYOUT ===== */
.message {
    margin: 8px 16px 12px 16px; /* Reduced bottom margin since timestamp is internal */
    display: flex;
    flex-direction: column;
    position: relative;
    max-width: 100%;
    clear: both;
}

.message.grouped {
    margin-top: 2px;
    margin-bottom: 6px; /* Further reduced margin for grouped messages */
}

.message.own-message {
    align-items: flex-end;
}

.message.other-message {
    align-items: flex-start;
}

/* ===== SENDER NAME ===== */
.sender-name {
    font-size: 12px;
    font-weight: 600;
    color: #6b7280;
    margin-bottom: 4px;
    padding: 0 4px;
    margin-left: 48px; /* align with message content after avatar */
    text-align: left;
}

/* ===== MESSAGE CONTENT WRAPPER ===== */
.message-content-wrapper {
    display: flex;
    align-items: flex-start; /* Changed to flex-start for better text alignment */
    gap: 12px;
    max-width: 85%; /* Increased to 85% for better content space */
    position: relative;
}

.own-message .message-content-wrapper {
    flex-direction: row-reverse;
    margin-left: auto;
    justify-content: flex-start;
}

.other-message .message-content-wrapper {
    flex-direction: row;
    margin-right: auto;
    justify-content: flex-start;
    /* Reserve space for avatar so inbound bubbles stay aligned even when avatar is hidden */
    padding-left: 48px; /* 36px avatar + 12px gap */
    position: relative;
}

/* ===== MESSAGE AVATAR ===== */
.message-avatar {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    margin-bottom: 0;
}

.message-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid #ffffff;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
}

/* Place avatar inside the reserved gutter for inbound messages */
.other-message .message-content-wrapper .message-avatar {
    position: absolute;
    left: 0;
    top: 0;
}

/* ===== CLEAN MESSAGE STYLING ===== */
/* Make message-content a neutral container; visible bubble moved to .text-message */
.message-content {
    position: relative;
    border-radius: 18px;
    padding: 0; /* remove inner padding so .text-message controls spacing */
    max-width: 100%;
    word-wrap: break-word;
    min-height: 0;
    font-size: 15px;
    line-height: 1.4;
    margin: 0;
    background: transparent !important; /* ensure neutral */
    border: none !important;
    box-shadow: none !important;
}

/* Enhanced text message styling for better appearance */
.text-message {
    display: inline-block; /* Allow dynamic width sizing */
    padding: 16px 18px 28px 18px; /* Reduced padding for more natural sizing */
    border-radius: 18px; 
    max-width: 75%; /* Prevent edge-to-edge stretching */
    min-width: 60px; /* Ensure minimum bubble size */
    width: auto; /* Dynamic width based on content */
    box-shadow: 0 2px 4px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04); /* Subtle shadow for colorful bubbles */
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    position: relative;
    margin-bottom: 2px; /* Reduced gap between consecutive messages */
    line-height: 1.4;
    min-height: 50px; /* Reduced minimum height */
}

/* ===== SPEECH BUBBLE TAILS (SUBTLE) ===== */
/* Incoming message tail (pointing left toward sender) */
.other-message .text-message::before {
    content: '';
    position: absolute;
    left: -6px;
    top: 24px;
    width: 0;
    height: 0;
    border: 6px solid transparent;
    border-right-color: inherit; /* Match bubble background */
    border-left: none;
    z-index: 1;
    opacity: 0.9;
}

/* Outgoing message tail (pointing right toward sender) */
.own-message .text-message::before {
    content: '';
    position: absolute;
    right: -6px;
    top: 24px;
    width: 0;
    height: 0;
    border: 6px solid transparent;
    border-left-color: inherit; /* Match bubble background */
    border-right: none;
    z-index: 1;
    opacity: 0.9;
}

/* Media message tails */
.other-message .media-message::before {
    content: '';
    position: absolute;
    left: -8px;
    top: 20px;
    width: 0;
    height: 0;
    border: 8px solid transparent;
    border-right-color: inherit;
    border-left: none;
    z-index: 1;
}

.text-message .message-text {
    margin: 6px 0 4px 0; /* Reduced margins for better proportion */
    padding: 0;
    font-size: 15px;
    line-height: 1.45;
    font-weight: 400;
    text-align: left;
    word-break: break-word;
    overflow-wrap: break-word;
}

/* Let mood colors take precedence - remove hardcoded background overrides */
.message.own-message .text-message {
    margin-left: auto !important;
}

/* Other message bubbles use mood colors applied via inline styles in JavaScript */

/* ===== IMPROVED TEXT ORGANIZATION ===== */
.message-content {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    width: 100%;
}

.text-message {
    display: block;
    width: 100%;
    min-width: fit-content;
}

.text-message .message-text {
    display: block;
    width: 100%;
    overflow-wrap: break-word;
    hyphens: auto;
}

.grouped.own-message .message-content {
    border-bottom-right-radius: 16px;
}

.grouped.other-message .message-content {
    border-bottom-left-radius: 16px;
}

/* ===== TEXT MESSAGE ===== */
.text-message .message-text {
    margin: 28px 0 12px 0; /* create space below mood and above timestamp */
    font-size: 15px;
    line-height: 1.45;
    text-align: center; /* center text horizontally as requested */
}

/* ==========================================================
   Safety overrides: ensure only .text-message shows bubble
   backgrounds. This neutralizes any other stylesheet that
   may set backgrounds on .message-content.
   ========================================================== */
.message-content {
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    padding: 0 !important;
}

.text-message {
    /* visible bubble defaults */
    background-clip: padding-box !important;
}

.text-message .message-text a {
    color: inherit;
    text-decoration: underline;
    opacity: 0.9;
}

.text-message .message-text strong {
    font-weight: 600;
}

.text-message .message-text em {
    font-style: italic;
}

.text-message .message-text code {
    background: rgba(0, 0, 0, 0.1);
    padding: 1px 3px;
    border-radius: 3px;
    font-family: 'Monaco', 'Consolas', monospace;
    font-size: 12px;
}

.own-message .text-message .message-text code {
    background: rgba(255, 255, 255, 0.2);
}

/* ===== MEDIA MESSAGE ===== */
.media-message {
    padding: 36px 24px 32px 24px; /* Increased padding to match text messages */
    position: relative; /* For internal mood/timestamp positioning */
    min-height: 80px; /* Increased minimum height for better separation */
    border-radius: 20px; /* Match text message radius */
    box-shadow: 0 2px 8px rgba(0,0,0,0.12), 0 1px 4px rgba(0,0,0,0.08);
}

/* Outgoing media message tail */
.own-message .media-message::before {
    content: '';
    position: absolute;
    right: -8px;
    top: 20px;
    width: 0;
    height: 0;
    border: 8px solid transparent;
    border-left-color: inherit;
    border-right: none;
    z-index: 1;
}

.media-grid {
    display: grid;
    gap: 4px;
    border-radius: 12px;
    overflow: hidden;
    margin: 28px 0 12px 0; /* Increased top margin to match text positioning */
}

.media-grid:has(.media-item:nth-child(1):last-child) {
    grid-template-columns: 1fr;
}

.media-grid:has(.media-item:nth-child(2):last-child) {
    grid-template-columns: 1fr 1fr;
}

.media-grid:has(.media-item:nth-child(3):last-child) {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}

.media-grid:has(.media-item:nth-child(3):last-child) .media-item:first-child {
    grid-row: 1 / 3;
}

.media-grid:has(.media-item:nth-child(4):last-child) {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}

.media-item {
    position: relative;
    aspect-ratio: 1;
    background: #f3f4f6;
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.media-item:hover {
    transform: scale(1.02);
}

.media-item img,
.media-item video {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.media-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s ease;
    color: #ffffff;
    font-size: 18px;
}

.media-item:hover .media-overlay {
    opacity: 1;
}

.video-duration {
    position: absolute;
    bottom: 8px;
    right: 8px;
    background: rgba(0, 0, 0, 0.7);
    color: #ffffff;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 500;
}

.media-error {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #6b7280;
    font-size: 12px;
    text-align: center;
}

/* ===== FILE MESSAGE ===== */
.file-message {
    padding: 8px;
}

.files-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 8px;
}

.file-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.other-message .file-item {
    background: #f9fafb;
}

.file-item:hover {
    background: rgba(255, 255, 255, 0.2);
}

.other-message .file-item:hover {
    background: #f3f4f6;
}

.file-icon {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: currentColor;
    font-size: 16px;
    flex-shrink: 0;
}

.other-message .file-icon {
    background: #e5e7eb;
    color: #6b7280;
}

.file-info {
    flex: 1;
    min-width: 0;
}

.file-name {
    font-size: 14px;
    font-weight: 500;
    color: currentColor;
    margin-bottom: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-size {
    font-size: 12px;
    opacity: 0.8;
}

.file-download {
    width: 32px;
    height: 32px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: currentColor;
    font-size: 12px;
    flex-shrink: 0;
}

.other-message .file-download {
    background: #e5e7eb;
    color: #6b7280;
}

/* ===== VOICE MESSAGE ===== */
.voice-message {
    padding: 8px;
}

.voice-player {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    margin-bottom: 8px;
}

.other-message .voice-player {
    background: #f9fafb;
}

.voice-play-btn {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border: none;
    border-radius: 50%;
    color: currentColor;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.other-message .voice-play-btn {
    background: #3b82f6;
    color: #ffffff;
}

.voice-play-btn:hover {
    transform: scale(1.1);
}

.voice-waveform {
    flex: 1;
    position: relative;
    height: 30px;
    display: flex;
    align-items: center;
}

.waveform-bars {
    display: flex;
    align-items: center;
    gap: 2px;
    width: 100%;
    height: 100%;
}

.waveform-bars .bar {
    flex: 1;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    min-height: 4px;
    transition: height 0.1s ease;
}

.other-message .waveform-bars .bar {
    background: #d1d5db;
}

.waveform-bars .bar:nth-child(odd) {
    height: 20%;
}

.waveform-bars .bar:nth-child(even) {
    height: 60%;
}

.waveform-bars .bar:nth-child(3n) {
    height: 40%;
}

.voice-progress {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    background: rgba(255, 255, 255, 0.6);
    border-radius: 2px;
    width: 0%;
    transition: width 0.1s ease;
}

.other-message .voice-progress {
    background: #3b82f6;
}

.voice-duration {
    font-size: 12px;
    font-weight: 500;
    opacity: 0.8;
    flex-shrink: 0;
}

/* ===== SYSTEM MESSAGE ===== */
.system-message {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 16px;
    margin: 16px auto;
    background: #f3f4f6;
    border-radius: 16px;
    max-width: 80%;
    font-size: 13px;
    color: #6b7280;
}

.system-icon {
    width: 20px;
    height: 20px;
    background: #e5e7eb;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    flex-shrink: 0;
}

.system-text {
    text-align: center;
}

.system-time {
    font-size: 11px;
    opacity: 0.7;
}

/* ===== CALL MESSAGE ===== */
.call-message {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    margin-bottom: 8px;
}

.other-message .call-message {
    background: #f9fafb;
}

.call-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
}

.call-icon.completed {
    background: #10b981;
    color: #ffffff;
}

.call-icon.missed {
    background: #ef4444;
    color: #ffffff;
}

.call-icon.cancelled,
.call-icon.declined {
    background: #6b7280;
    color: #ffffff;
}

.call-info {
    flex: 1;
}

.call-type {
    font-size: 14px;
    font-weight: 500;
    color: currentColor;
    margin-bottom: 2px;
}

.call-status {
    font-size: 12px;
    opacity: 0.8;
}

.call-duration {
    font-size: 12px;
    opacity: 0.7;
    margin-top: 2px;
}

.call-back-btn {
    width: 36px;
    height: 36px;
    background: #10b981;
    border: none;
    border-radius: 50%;
    color: #ffffff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.call-back-btn:hover {
    background: #059669;
    transform: scale(1.1);
}

/* ===== REPLY PREVIEW ===== */
.reply-preview {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 8px 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    margin-bottom: 8px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.other-message .reply-preview {
    background: #f3f4f6;
}

.reply-preview:hover {
    background: rgba(255, 255, 255, 0.2);
}

.other-message .reply-preview:hover {
    background: #e5e7eb;
}

.reply-bar {
    width: 3px;
    background: currentColor;
    border-radius: 2px;
    opacity: 0.6;
    flex-shrink: 0;
    align-self: stretch;
}

.reply-content {
    flex: 1;
    min-width: 0;
}

.reply-sender {
    font-size: 12px;
    font-weight: 600;
    color: currentColor;
    margin-bottom: 2px;
    opacity: 0.8;
}

.reply-text {
    font-size: 13px;
    color: currentColor;
    opacity: 0.7;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ===== MESSAGE REACTIONS ===== */
.message-reactions {
    display: flex;
    align-items: center;
    gap: 4px;
    margin-top: 6px;
    flex-wrap: wrap;
}

.reaction-item {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 2px 6px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    font-size: 12px;
}

.other-message .reaction-item {
    background: #f3f4f6;
}

.reaction-item:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.05);
}

.other-message .reaction-item:hover {
    background: #e5e7eb;
}

.reaction-emoji {
    font-size: 14px;
}

.reaction-count {
    font-size: 11px;
    font-weight: 600;
    color: currentColor;
    opacity: 0.8;
}

.add-reaction-btn {
    width: 24px;
    height: 24px;
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 50%;
    color: currentColor;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    transition: all 0.2s ease;
    opacity: 0.6;
}

.other-message .add-reaction-btn {
    background: #e5e7eb;
    color: #6b7280;
}

.add-reaction-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    opacity: 1;
    transform: scale(1.1);
}

.other-message .add-reaction-btn:hover {
    background: #d1d5db;
}

/* ===== MESSAGE INTERNAL ELEMENTS ===== */
.message-mood-indicator {
    position: absolute;
    top: 4px;
    left: 10px;
    font-size: 10px;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.95);
    background: rgba(0, 0, 0, 0.15);
    padding: 2px 6px;
    border-radius: 6px;
    z-index: 2;
    pointer-events: none;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(4px);
}

.message-timestamp {
    position: absolute;
    bottom: 4px;
    right: 10px;
    font-size: 9px; /* 1pt smaller for better hierarchy */
    font-weight: 500;
    color: rgba(255, 255, 255, 0.8);
    pointer-events: none;
    z-index: 2;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* ===== MESSAGE FOOTER (Legacy - now unused) ===== */
.message-footer {
    display: none; /* Hide external footer since we moved elements inside */
}

.message-time {
    color: currentColor;
    font-size: 11px;
    font-weight: 400;
}

/* Internal timestamp and mood styling adjustments for different message types */
.own-message .message-timestamp {
    color: rgba(255, 255, 255, 0.8);
}

.other-message .message-timestamp {
    color: #666;
}

.own-message .message-mood-indicator {
    color: rgba(255, 255, 255, 0.9);
    background: rgba(0, 0, 0, 0.15);
}

.other-message .message-mood-indicator {
    color: #666;
    background: rgba(255, 255, 255, 0.4);
}

.edited-indicator {
    color: currentColor;
    font-style: italic;
}

.message-status {
    color: currentColor;
    font-size: 10px;
}

.message-status.sending {
    opacity: 0.5;
}

.message-status.sent {
    opacity: 0.7;
}

.message-status.delivered {
    opacity: 0.8;
    color: #10b981;
}

.message-status.read {
    opacity: 1;
    color: #3b82f6;
}

.message-status.failed {
    color: #ef4444;
    opacity: 1;
}

/* ===== MESSAGE ACTIONS ===== */
.message-actions {
    position: absolute;
    top: -8px;
    right: 16px;
    z-index: 10;
}

.own-message .message-actions {
    left: 16px;
    right: auto;
}

.actions-menu {
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    padding: 4px 0;
    min-width: 120px;
}

.action-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: none;
    border: none;
    width: 100%;
    text-align: left;
    color: #374151;
    font-size: 13px;
    cursor: pointer;
    transition: background 0.2s ease;
}

.action-item:hover {
    background: #f9fafb;
}

.action-item.danger {
    color: #ef4444;
}

.action-item.danger:hover {
    background: #fef2f2;
}

.action-item i {
    width: 16px;
    text-align: center;
    font-size: 12px;
}

/* ===== DATE SEPARATOR ===== */
.date-separator {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 20px 0;
    position: relative;
}

.date-separator::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 0;
    right: 0;
    height: 1px;
    background: #e5e7eb;
}

.date-text {
    background: #f9fafb;
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 12px;
    color: #6b7280;
    font-weight: 500;
    position: relative;
    z-index: 1;
}

/* ===== LOADING MESSAGE ===== */
.loading-message {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: #f9fafb;
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    max-width: 200px;
    margin: 8px 0;
}

.loading-dots {
    display: flex;
    gap: 4px;
}

.loading-dot {
    width: 8px;
    height: 8px;
    background: #d1d5db;
    border-radius: 50%;
    animation: loading-pulse 1.5s infinite;
}

.loading-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.loading-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes loading-pulse {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: scale(0.8);
    }
    30% {
        opacity: 1;
        transform: scale(1);
    }
}

/* ===== RESPONSIVE MESSAGE DESIGN ===== */
@media (max-width: 768px) {
    .message-content-wrapper {
        max-width: 90%;
    }
    
    .message-content {
        padding: 10px 12px;
        font-size: 14px;
    }
    
    .media-item {
        aspect-ratio: 1.2;
    }
    
    .file-item {
        padding: 10px;
    }
    
    .file-icon {
        width: 36px;
        height: 36px;
    }
    
    .voice-player {
        padding: 10px;
    }
    
    .voice-play-btn {
        width: 36px;
        height: 36px;
    }
}

@media (max-width: 480px) {
    .message-content-wrapper {
        max-width: 95%;
    }
    
    .message-content {
        padding: 8px 12px;
        font-size: 13px;
    }
    
    .media-grid {
        gap: 2px;
    }
    
    .message-actions {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        top: auto;
        background: rgba(0, 0, 0, 0.5);
        padding: 16px;
    }
    
    .actions-menu {
        background: #ffffff;
        border-radius: 12px;
        padding: 8px 0;
    }
    
    .action-item {
        padding: 12px 16px;
        font-size: 14px;
    }
}