/* Messages Page Layout */
.messages-page {
    /* Use dynamic viewport height first (mobile-friendly), fallback to vh */
    height: calc(100dvh - var(--df-navbar-height, 64px));
    overflow: hidden;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* Fallback for browsers that don't support dvh */
@supports not (height: 100dvh) {
    .messages-page {
        height: calc(100vh - var(--df-navbar-height, 64px));
    }
}

.messages-desktop {
    display: flex;
    flex: 1 1 auto;
    height: 100%;
    min-height: 0;
    overflow: hidden;
}

.messages-sidebar {
    width: 360px;
    min-width: 320px;
    max-width: 400px;
    border-right: 1px solid var(--mud-palette-lines-default);
    display: flex;
    flex-direction: column;
    background: var(--mud-palette-surface);
    min-height: 0;
    overflow: hidden;
    height: 100%;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 16px;
    border-bottom: 1px solid var(--mud-palette-lines-default);
    flex: 0 0 auto;
}

.messages-chat {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
    height: 100%;
}

.messages-mobile {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
    height: 100%;
}

.mobile-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--mud-palette-lines-default);
    background: var(--mud-palette-surface);
    flex: 0 0 auto;
}

/* Conversation List */
.conversation-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.conversation-item-skeleton {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
}

.skeleton-content {
    flex: 1;
}

.empty-conversations {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 24px;
    text-align: center;
    opacity: 0.7;
}

.conversation-list-item {
    transition: background-color 0.15s ease;
    /* Ensure minimum touch target */
    min-height: 64px;
}

/* Ensure MudListItem does not clip avatar */
.conversation-list-item .mud-list-item-text {
    overflow: visible;
}

.conversation-list-item:hover {
    background-color: var(--mud-palette-action-default-hover);
}

.conversation-list-item.active {
    background-color: var(--mud-palette-primary-hover);
}

.conversation-list-item.unread {
    background-color: rgba(var(--mud-palette-primary-rgb), 0.05);
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    padding: 4px 0;
    overflow: visible;
}

.conversation-avatar {
    position: relative;
    flex-shrink: 0;
    /* Fixed dimensions to prevent clipping */
    width: 48px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
}

/* Fix MudBadge causing avatar to be clipped */
.conversation-avatar .mud-badge-root {
    overflow: visible !important;
}

.conversation-avatar .mud-badge-wrapper {
    overflow: visible !important;
}

.conversation-avatar .mud-avatar {
    width: 48px;
    height: 48px;
}

.conversation-info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.conversation-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.conversation-name {
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-time {
    flex-shrink: 0;
    font-size: 0.75rem;
    color: var(--mud-palette-text-secondary);
}

.conversation-preview {
    display: flex;
    align-items: center;
    gap: 4px;
}

.preview-text {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.875rem;
    color: var(--mud-palette-text-secondary);
}

.conversation-unread .conversation-name,
.conversation-unread .preview-text {
    font-weight: 600;
}

/* Chat Container */
.chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    min-height: 0;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    border-bottom: 1px solid var(--mud-palette-lines-default);
    background: var(--mud-palette-surface);
    flex: 0 0 auto;
    /* Ensure minimum height for touch */
    min-height: 56px;
}

.back-btn {
    /* Ensure touch target */
    min-width: 44px;
    min-height: 44px;
}

.chat-header-user {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    flex: 1;
    min-width: 0;
    padding: 4px;
    border-radius: var(--df-radius);
    transition: background-color 0.15s ease;
    /* Touch target */
    min-height: 44px;
}

.chat-header-user:hover {
    background-color: var(--mud-palette-action-default-hover);
}

.chat-header-user:focus-visible {
    outline: 2px solid var(--df-primary);
    outline-offset: 2px;
}

.user-info {
    min-width: 0;
    overflow: hidden;
}

.user-info .mud-typography {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Chat Messages Area */
.chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    min-height: 0;
}

.messages-loading,
.messages-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 24px;
}

.load-more {
    display: flex;
    justify-content: center;
    padding: 8px;
    /* Ensure touch target */
    min-height: 44px;
}

.load-more .mud-button {
    min-height: 44px;
}

.date-separator {
    display: flex;
    justify-content: center;
    padding: 8px 0;
}

/* Chat Input Area */
.chat-input {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--mud-palette-lines-default);
    background: var(--mud-palette-surface);
    flex: 0 0 auto;
}

.attach-btn {
    flex-shrink: 0;
}

.message-input {
    flex: 1;
}

.message-input .mud-input-outlined {
    border-radius: 20px !important;
}

.send-btn {
    flex-shrink: 0;
    border-radius: 50% !important;
    /* Ensure touch target of 44px */
    min-width: 48px !important;
    min-height: 48px !important;
    width: 48px !important;
    height: 48px !important;
    padding: 0 !important;
}

.rate-limit-alert {
    margin: 0 16px 8px;
}

/* Message Bubble */
.message-bubble {
    display: flex;
    gap: 8px;
    max-width: 75%;
    animation: fadeInUp 0.2s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message-bubble.sent {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.message-bubble.received {
    align-self: flex-start;
}

.message-bubble.deleted {
    opacity: 0.6;
}

.bubble-avatar {
    flex-shrink: 0;
    margin-bottom: 4px;
}

.bubble-content {
    background: var(--mud-palette-surface);
    border-radius: 16px;
    padding: 10px 14px;
    position: relative;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    word-break: break-word;
}

.message-bubble.sent .bubble-content {
    background: var(--mud-palette-primary);
    color: var(--mud-palette-primary-text);
    border-bottom-right-radius: 4px;
}

.message-bubble.received .bubble-content {
    background: var(--mud-palette-background-grey);
    border-bottom-left-radius: 4px;
}

.bubble-attachments {
    margin-bottom: 8px;
}

.attachment-image {
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    max-width: 280px;
}

.attachment-img {
    max-width: 100%;
    max-height: 300px;
    border-radius: 12px;
}

.attachment-video {
    border-radius: 12px;
    overflow: hidden;
    max-width: 280px;
}

.attachment-vid {
    max-width: 100%;
    max-height: 300px;
    border-radius: 12px;
}

.bubble-text {
    word-wrap: break-word;
    white-space: pre-wrap;
    line-height: 1.4;
}

.bubble-text.deleted-text {
    display: flex;
    align-items: center;
    font-style: italic;
    opacity: 0.8;
}

.bubble-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 4px;
}

.bubble-time {
    font-size: 0.7rem;
}

/* Mensagens enviadas - texto claro para fundo escuro (primary) */
.message-bubble.sent .bubble-time {
    color: rgba(255, 255, 255, 0.8);
}

/* Mensagens recebidas - texto escuro para fundo claro */
.message-bubble.received .bubble-time {
    color: rgba(0, 0, 0, 0.5);
}

/* Dark mode - mensagens recebidas */
.dark .message-bubble.received .bubble-time,
.mud-theme-dark .message-bubble.received .bubble-time {
    color: rgba(255, 255, 255, 0.6);
}

.bubble-menu {
    opacity: 0;
    transition: opacity 0.15s ease;
}

.message-bubble:hover .bubble-menu,
.message-bubble:focus-within .bubble-menu {
    opacity: 1;
}

.bubble-menu .mud-icon-button {
    padding: 2px;
    /* Ensure touch target */
    min-width: 32px;
    min-height: 32px;
}

/* Messages page specific - ensure full height layout */
.main-content-modern:has(.messages-page) {
    overflow: hidden;
}

.main-content-modern:has(.messages-page) > .mud-container {
    height: 100%;
    max-height: 100%;
    padding: 0 !important;
    max-width: none !important;
    overflow: hidden;
}

/* ============================================
   MOBILE RESPONSIVE ADJUSTMENTS
   ============================================ */
@media (max-width: 959.95px) {
    /* Account for bottom nav on mobile */
    .messages-page {
        height: calc(100dvh - var(--df-navbar-height, 64px) - var(--df-bottom-nav-height, 64px) - var(--df-spacing-sm, 8px));
        overflow: hidden;
        min-height: 0;
    }

    @supports not (height: 100dvh) {
        .messages-page {
            height: calc(100vh - var(--df-navbar-height, 64px) - var(--df-bottom-nav-height, 64px) - var(--df-spacing-sm, 8px));
        }
    }

    .messages-mobile {
        flex: 1 1 auto;
        min-height: 0;
        overflow: hidden;
        height: 100%;
    }

    .messages-mobile .chat-container {
        flex: 1 1 auto;
        min-height: 0;
        height: 100%;
    }

    .message-bubble {
        max-width: 85%;
    }

    /* Safe area for input on iOS */
    .chat-input {
        padding: 10px 12px;
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }

    .chat-messages {
        padding: 12px;
    }

    /* Ensure buttons are touch-friendly */
    .chat-header .mud-icon-button {
        min-width: 44px;
        min-height: 44px;
    }
}

/* Extra small screens */
@media (max-width: 600px) {
    .mobile-header {
        padding: 10px 12px;
    }

    .conversation-avatar .mud-avatar {
        width: 44px;
        height: 44px;
    }

    .conversation-list-item {
        min-height: 60px;
    }

    .chat-header {
        padding: 10px 12px;
        gap: 8px;
    }

    .bubble-content {
        padding: 8px 12px;
    }

    .attachment-image,
    .attachment-video {
        max-width: 220px;
    }

    .attachment-img,
    .attachment-vid {
        max-height: 240px;
    }
}

/* New messages indicator */
.new-messages-indicator {
    position: absolute;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 10;
    cursor: pointer;
    animation: bounce-in 0.3s ease-out;
}

.new-messages-indicator .mud-chip {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    /* Ensure touch target */
    min-height: 36px;
}

@keyframes bounce-in {
    0% {
        opacity: 0;
        transform: translateX(-50%) translateY(20px);
    }
    100% {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

/* Conversation unread dot */
.conversation-unread-dot {
    width: 10px;
    height: 10px;
    background: var(--mud-palette-primary);
    border-radius: 50%;
    flex-shrink: 0;
}

/* Dark mode adjustments */
.dark .bubble-content,
.mud-theme-dark .bubble-content {
    background: var(--mud-palette-dark);
}

.dark .message-bubble.sent .bubble-content,
.mud-theme-dark .message-bubble.sent .bubble-content {
    background: var(--mud-palette-primary);
}

.dark .message-bubble.received .bubble-content,
.mud-theme-dark .message-bubble.received .bubble-content {
    background: var(--mud-palette-surface);
}

/* ============================================
   ACCESSIBILITY IMPROVEMENTS
   ============================================ */

/* Focus visible for keyboard navigation */
.conversation-list-item:focus-visible,
.message-bubble:focus-visible,
.chat-header-user:focus-visible {
    outline: 2px solid var(--df-primary, #2563eb);
    outline-offset: 2px;
}

/* Ensure sufficient contrast for timestamps */
.bubble-time,
.conversation-time {
    color: var(--df-muted, #64748b);
}

.dark .bubble-time,
.mud-theme-dark .bubble-time {
    color: rgba(255, 255, 255, 0.6);
}

/* Screen reader only class for additional context */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Pending Attachments */
.pending-attachments {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 16px;
    background: var(--mud-palette-surface);
    border-top: 1px solid var(--mud-palette-lines-default);
}

.pending-attachment {
    position: relative;
    width: 80px;
    height: 80px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--mud-palette-lines-default);
}

.attachment-preview-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.attachment-preview-video {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: var(--mud-palette-background-grey);
    padding: 4px;
}

.attachment-preview-video .attachment-name {
    font-size: 0.65rem;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
    margin-top: 4px;
    color: var(--mud-palette-text-secondary);
}

.remove-attachment-btn {
    position: absolute !important;
    top: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.6) !important;
    padding: 2px !important;
}

.remove-attachment-btn .mud-icon-root {
    font-size: 14px !important;
}

/* Upload Progress */
.upload-progress {
    padding: 8px 16px;
    background: var(--mud-palette-surface);
    border-top: 1px solid var(--mud-palette-lines-default);
}

/* Updated Chat Input */
.chat-input {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--mud-palette-lines-default);
    background: var(--mud-palette-surface);
}
