/* Messaggi - WhatsApp Style */

.messages-container {
    height: calc(100vh - 60px);
    display: flex;
    background-color: #f0f2f5;
}

/* Sidebar Conversazioni */
.conversations-sidebar {
    width: 350px;
    background-color: #fff;
    border-right: 1px solid #e9ecef;
    display: flex;
    flex-direction: column;
    height: 100%; /* Altezza fissa per contenere gli elementi */
}

.conversations-header {
    padding: 1rem 1.25rem;
    background-color: #f0f2f5;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #e9ecef;
    flex-shrink: 0; /* Non si riduce */
}

.search-bar {
    padding: 0.75rem 1.25rem;
    position: relative;
    flex-shrink: 0; /* Non si riduce */
}

.search-bar i {
    position: absolute;
    left: 2rem;
    top: 50%;
    transform: translateY(-50%);
    color: #8696a0;
}

.search-bar input {
    padding-left: 2.5rem;
    border-radius: 8px;
    border: none;
    background-color: #f0f2f5;
}

.search-bar input:focus {
    background-color: #fff;
    box-shadow: 0 0 0 0.25rem rgba(102, 126, 234, 0.1);
}

.conversations-list {
    flex: 1;
    overflow-y: auto;
    min-height: 0; /* Permette al flex child di scrollare */
}

.conversation-item {
    display: flex;
    align-items: center;
    padding: 0.5rem 1rem;
    border-bottom: 1px solid #f0f2f5;
    cursor: pointer;
    transition: background-color 0.2s;
}

.conversation-item:hover {
    background-color: #f5f6f6;
}

.conversation-item.active {
    background-color: #e7f3ff;
}

.conversation-item .avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    margin-right: 0.75rem;
    object-fit: cover;
}

.conversation-item .content {
    flex: 1;
    min-width: 0;
    line-height: 1.3;
}

.conversation-item .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0;
}

.conversation-item .name {
    font-weight: 600;
    color: #111b21;
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-item .time {
    font-size: 0.7rem;
    color: #667781;
}

.conversation-subtitle {
    font-size: 0.7rem;
    display: flex;
    align-items: center;
    gap: 0.2rem;
    margin-bottom: 0.1rem;
    white-space: nowrap;
    overflow: hidden;
}

.conversation-subtitle i {
    font-size: 0.7rem;
    flex-shrink: 0;
}

.conversation-subtitle span {
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-item .last-message {
    font-size: 0.8rem;
    color: #667781;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
}

.conversation-item .last-message i {
    margin-right: 0.25rem;
    font-size: 0.75rem;
}

.conversation-item .unread-badge {
    background-color: #25d366;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 600;
    margin-left: 0.5rem;
}

/* Area Chat */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #efeae2;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23d9d9d9' fill-opacity='0.1'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
    height: 100%; /* Altezza fissa per contenere gli elementi */
}

.chat-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #667781;
}

.chat-active {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%; /* Altezza fissa per contenere gli elementi */
    min-height: 0; /* Permette al flex child di scrollare */
}

.chat-header {
    background-color: #f0f2f5;
    padding: 0.75rem 1.25rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #e9ecef;
    flex-shrink: 0; /* Non si riduce */
}

.chat-header-info {
    line-height: 1.3;
}

.chat-header-name {
    font-weight: 600;
    color: #111b21;
    font-size: 0.95rem;
}

.chat-header-type {
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.chat-header-type i {
    font-size: 0.8rem;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Permette al flex child di scrollare */
}

.message-bubble {
    max-width: 65%;
    margin-bottom: 0.5rem;
    display: flex;
    flex-direction: column;
}

.message-bubble.sent {
    align-self: flex-end;
}

.message-bubble.received {
    align-self: flex-start;
}

.message-bubble .bubble {
    padding: 0.5rem 0.75rem;
    border-radius: 8px;
    position: relative;
}

.message-bubble.sent .bubble {
    background-color: #d9fdd3;
    border-bottom-right-radius: 2px;
}

.message-bubble.received .bubble {
    background-color: #fff;
    border-bottom-left-radius: 2px;
}

.message-bubble .sender-name {
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: 0.25rem;
    color: #06cf9c;
}

.message-bubble .content {
    word-wrap: break-word;
    margin-bottom: 0.25rem;
    color: #111b21;
    font-size: 0.9rem;
}

.message-bubble .meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    font-size: 0.7rem;
    color: #667781;
    margin-top: 0.25rem;
}

.message-bubble .meta i {
    margin-left: 0.25rem;
    font-size: 0.75rem;
}

.message-bubble .meta .bi-check-all {
    color: #53bdeb;
}

/* Sending Bubble - Stato invio in corso */
.sending-bubble {
    opacity: 0.7;
}

.sending-bubble .bubble {
    background-color: #d9fdd3;
}

.sending-status {
    display: flex;
    align-items: center;
    color: #667781;
    font-style: italic;
}

.sending-attachment-indicator {
    font-size: 0.8rem;
    color: #667781;
    padding: 0.4rem 0;
    display: flex;
    align-items: center;
    font-style: italic;
}

.message-bubble .attachment {
    background-color: rgba(0, 0, 0, 0.05);
    padding: 0.5rem;
    border-radius: 4px;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
}

.message-bubble .attachment i {
    margin-right: 0.5rem;
    font-size: 1.25rem;
}

/* Attachment Images */
.message-bubble .attachment-image {
    max-width: 300px;
    max-height: 300px;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 0.5rem;
    display: block;
}

/* Attachment PDFs */
.message-bubble .attachment-pdf {
    background-color: rgba(0, 0, 0, 0.05);
    padding: 0.75rem;
    border-radius: 8px;
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    cursor: pointer;
    transition: background-color 0.2s;
}

.message-bubble .attachment-pdf:hover {
    background-color: rgba(0, 0, 0, 0.1);
}

.message-bubble .attachment-pdf i {
    font-size: 2rem;
    color: #d32f2f;
    margin-right: 0.75rem;
}

.message-bubble .attachment-pdf-info {
    flex: 1;
}

.message-bubble .attachment-pdf-name {
    font-weight: 500;
    color: #111b21;
    font-size: 0.9rem;
    margin-bottom: 0.15rem;
}

.message-bubble .attachment-pdf-size {
    font-size: 0.75rem;
    color: #667781;
}

.chat-input {
    background-color: #f0f2f5;
    padding: 0.75rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    flex-shrink: 0; /* Non si riduce */
    position: relative; /* Per posizionamento emoji picker */
}

.chat-input-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.chat-input .form-control {
    flex: 1;
    border-radius: 8px;
    border: none;
    padding: 0.6rem 1rem;
}

.chat-input .form-control:focus {
    box-shadow: 0 0 0 0.25rem rgba(102, 126, 234, 0.1);
}

.chat-input .btn {
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

/* Emoji Picker */
.emoji-picker-container {
    position: absolute;
    bottom: 100%;
    left: 0;
    z-index: 100;
    margin-bottom: 8px;
}

.emoji-picker-container emoji-picker {
    --num-columns: 8;
    --emoji-size: 1.3rem;
    --category-emoji-size: 1.1rem;
    --border-color: #e9ecef;
    --background: #fff;
    --input-border-color: #e9ecef;
    --input-font-color: #111b21;
    --indicator-color: var(--primary-color, #f58220);
    --outline-color: var(--primary-color, #f58220);
    height: 320px;
    width: 320px;
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
}

/* Attachments Preview Grid (pre-invio) */
.attachments-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px;
    background: #fff;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}

.attachment-thumb {
    position: relative;
    width: 64px;
    height: 64px;
    border-radius: 8px;
    overflow: hidden;
    background: #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.attachment-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.attachment-thumb i {
    font-size: 1.8rem;
    color: #d32f2f;
}

.attachment-thumb-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.6);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 0.7rem;
    padding: 0;
    line-height: 1;
}

.attachment-thumb-remove:hover {
    background: rgba(220, 53, 69, 0.9);
}

/* Message Gallery Grid (nella bolla del messaggio) */
.message-gallery {
    display: grid;
    gap: 2px;
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 4px;
    max-width: 300px;
    position: relative;
}

.message-gallery.gallery-1 {
    grid-template-columns: 1fr;
}

.message-gallery.gallery-2 {
    grid-template-columns: 1fr 1fr;
}

.message-gallery.gallery-3 {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}

.message-gallery.gallery-3 .gallery-image-wrapper:first-child {
    grid-row: span 2;
}

.message-gallery.gallery-4 {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
}

/* Wrapper immagine con placeholder */
.gallery-image-wrapper {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    background-color: #e9ecef;
}

/* Gallery singola: il wrapper usa aspect-ratio inline dal JS per mantenere
   le proporzioni esatte dell'immagine. */
.message-gallery.gallery-1 .gallery-image-wrapper {
    max-width: 300px;
}

/* Fallback per immagini vecchie senza dimensioni (no aspect-ratio inline) */
.message-gallery.gallery-1 .gallery-image-wrapper:not([style*="aspect-ratio"]) {
    min-height: 200px;
}

/* Gallery multi: altezza fissa per le celle della grid, l'immagine viene croppata */
.message-gallery:not(.gallery-1) .gallery-image-wrapper {
    height: 150px;
}

.message-gallery.gallery-3 .gallery-image-wrapper:first-child {
    height: 100%;
}

/* Placeholder loading (centrato nel wrapper) */
.gallery-image-placeholder {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #e9ecef;
    z-index: 1;
}

/* Immagine: nascosta finché non caricata */
.gallery-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: opacity 0.3s ease;
    opacity: 0;
}

.gallery-image.loaded {
    opacity: 1;
}

.gallery-image-wrapper:hover .gallery-image.loaded {
    opacity: 0.9;
}

/* Overlay +N per 5+ immagini (ora dentro l'ultimo wrapper) */
.gallery-more-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    font-weight: 600;
    cursor: pointer;
    z-index: 2;
}

.gallery-more-overlay:hover {
    background: rgba(0, 0, 0, 0.65);
}

/* Lightbox Modal */
.lightbox-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.95);
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lightbox-image {
    max-width: 90vw;
    max-height: 85vh;
    object-fit: contain;
    user-select: none;
}

.lightbox-close {
    position: absolute;
    top: 16px;
    right: 16px;
    background: none;
    border: none;
    color: #fff;
    font-size: 1.5rem;
    cursor: pointer;
    z-index: 10001;
    opacity: 0.8;
    transition: opacity 0.2s;
}

.lightbox-close:hover {
    opacity: 1;
}

.lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: #fff;
    font-size: 2rem;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    z-index: 10001;
}

.lightbox-nav:hover {
    background: rgba(255, 255, 255, 0.3);
}

.lightbox-prev {
    left: 16px;
}

.lightbox-next {
    right: 16px;
}

.lightbox-counter {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: #fff;
    font-size: 0.9rem;
    background: rgba(0, 0, 0, 0.5);
    padding: 4px 12px;
    border-radius: 12px;
}

/* Date Divider */
.date-divider {
    text-align: center;
    margin: 1rem 0;
}

.date-divider span {
    background-color: #fff;
    padding: 0.35rem 0.75rem;
    border-radius: 8px;
    font-size: 0.75rem;
    color: #667781;
    box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13);
}

/* Contact Type Badge */
.contact-type-badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
}

/* Modal Nuovo Messaggio */
.new-message-modal {
    height: 70vh;
    display: flex;
    flex-direction: column;
}

.new-message-header {
    background-color: var(--primary-color, #f58220);
    color: #fff;
    border-bottom: none;
    flex-shrink: 0;
}

.new-message-header .modal-title {
    font-size: 1.1rem;
    font-weight: 600;
}

.new-message-filters {
    flex-shrink: 0;
}

.new-message-body {
    flex: 1;
    overflow-y: auto;
    min-height: 0;
}

#newConversationModal .input-group-text {
    border-radius: 8px 0 0 8px;
}

#newConversationModal .input-group .form-control {
    border-radius: 0 8px 8px 0;
}

/* Tab filtro categorie */
.contact-filter-tabs .nav-tabs {
    border-bottom: 2px solid #e9ecef;
}

.contact-filter-tabs .nav-link {
    font-size: 0.8rem;
    font-weight: 500;
    color: #667781;
    border: none;
    border-bottom: 2px solid transparent;
    padding: 0.5rem 0.25rem;
    margin-bottom: -2px;
    transition: color 0.2s, border-color 0.2s;
}

.contact-filter-tabs .nav-link:hover {
    color: var(--primary-color, #f58220);
    border-color: transparent;
}

.contact-filter-tabs .nav-link.active {
    color: var(--primary-color, #f58220);
    background: none;
    border-color: var(--primary-color, #f58220);
    font-weight: 600;
}

.contact-item {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: background-color 0.2s;
    border-bottom: 1px solid #f0f2f5;
}

.contact-item:hover {
    background-color: #f5f6f6;
}

.contact-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 1rem;
    object-fit: cover;
}

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

.contact-name {
    font-weight: 500;
    color: #111b21;
    font-size: 0.95rem;
    margin-bottom: 0.15rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.contact-type-label {
    font-size: 0.75rem;
    color: #667781;
    display: flex;
    align-items: center;
    gap: 0.3rem;
}

.contact-type-label i {
    font-size: 0.8rem;
}

/* Colori per tipo contatto */
.contact-type-operator,
.contact-type-bot {
    color: #f58220;
}

.contact-type-user {
    color: #1976d2;
}

.contact-type-bambino {
    color: #2e7d32;
}

.contact-type-gruppo {
    color: #7b1fa2;
}

.contact-item i.bi-chevron-right {
    color: #8696a0;
    font-size: 1rem;
}

/* Broadcast Item - Invia a tutti */
.broadcast-item {
    display: flex;
    align-items: center;
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: background-color 0.2s;
    border-bottom: 1px solid #f0f2f5;
    background-color: #fff8e1;
}

.broadcast-item:hover {
    background-color: #ffecb3;
}

.broadcast-icon {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--primary-color, #f58220);
    color: #fff;
    font-size: 1.1rem;
}

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

.broadcast-label {
    font-weight: 600;
    color: #111b21;
    font-size: 0.95rem;
}

.broadcast-description {
    font-size: 0.75rem;
    color: #667781;
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.broadcast-description i {
    font-size: 0.7rem;
}

.contact-type-badge.operator {
    background-color: #e3f2fd;
    color: #1976d2;
}

.contact-type-badge.user {
    background-color: #f3e5f5;
    color: #7b1fa2;
}

.contact-type-badge.bambino {
    background-color: #fff3e0;
    color: #e65100;
}

.contact-type-badge.gruppo {
    background-color: #e8f5e9;
    color: #2e7d32;
}

/* Refresh Button Animation */
@keyframes spin {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}

.spin-animation {
    animation: spin 0.5s linear infinite;
}

#refreshConversationsBtn {
    font-size: 1.1rem;
    text-decoration: none;
}

#refreshConversationsBtn:hover {
    color: var(--primary-color, #f58220) !important;
}

/* Scrollbar Styling */
.conversations-list::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar {
    width: 6px;
}

.conversations-list::-webkit-scrollbar-track,
.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.conversations-list::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

.conversations-list::-webkit-scrollbar-thumb:hover,
.chat-messages::-webkit-scrollbar-thumb:hover {
    background-color: rgba(0, 0, 0, 0.3);
}

/* Sidebar - Stile uniforme con pagina contatti */
.sidebar {
    position: fixed;
    top: 56px;
    bottom: 0;
    left: 0;
    z-index: 100;
    padding: 0;
    overflow-x: hidden;
    overflow-y: auto;
    background-color: #fff;
    border-right: 1px solid #e9ecef;
}

.sidebar .nav-link {
    padding: 0.75rem 1rem;
    color: #333;
    font-weight: 500;
    border-left: 3px solid transparent;
    transition: all 0.2s;
    display: flex;
    align-items: center;
}

.sidebar .nav-link:hover {
    background-color: #f5f6f6;
    color: var(--primary-color, #f58220);
}

.sidebar .nav-link.active {
    background-color: var(--primary-light, #fff3e8);
    color: var(--primary-color, #f58220);
    border-left-color: var(--primary-color, #f58220);
}

.sidebar .nav-link i {
    margin-right: 0.5rem;
    font-size: 1rem;
}

.sidebar-heading {
    font-size: 0.75rem;
    font-weight: 600;
    letter-spacing: 0.05em;
}

/* Responsive - Sidebar icon-only su schermi medi (md: 768px - lg: 992px) */
@media (min-width: 768px) and (max-width: 991.98px) {
    .sidebar {
        width: 60px !important;
        min-width: 60px;
        max-width: 60px;
        flex: 0 0 60px !important;
    }

    .sidebar .sidebar-heading {
        display: none;
    }

    .sidebar .nav-link {
        justify-content: center;
        padding: 0.75rem 0.5rem;
        border-left: none;
    }

    .sidebar .nav-link .sidebar-label {
        display: none;
    }

    .sidebar .nav-link i {
        margin-right: 0;
        font-size: 1.2rem;
    }

    .sidebar .nav-link.active {
        border-left: none;
        border-bottom: 2px solid var(--primary-color, #f58220);
    }

    /* Main content prende tutto lo spazio rimanente */
    #mainContent {
        margin-left: 60px !important;
        width: calc(100% - 60px) !important;
        flex: 0 0 calc(100% - 60px) !important;
        max-width: calc(100% - 60px) !important;
    }

    /* Sidebar conversazioni piu' compatta */
    .conversations-sidebar {
        width: 280px;
        min-width: 280px;
    }
}

/* Responsive - Mobile (< 768px) */
@media (max-width: 767.98px) {
    /* Sidebar nascosta su mobile */
    .sidebar {
        display: none !important;
    }

    /* Main content prende il 100% */
    #mainContent {
        margin-left: 0 !important;
        width: 100% !important;
        flex: 0 0 100% !important;
        max-width: 100% !important;
    }

    .conversations-sidebar {
        width: 100%;
        position: absolute;
        height: 100%;
        z-index: 1;
        transition: transform 0.3s;
    }

    .conversations-sidebar.hidden {
        transform: translateX(-100%);
    }

    .chat-area {
        position: absolute;
        width: 100%;
        height: 100%;
        z-index: 2;
    }

    .message-bubble {
        max-width: 85%;
    }
}

