/**
 * CHAT SYSTEM - STILI UNIFICATI
 * Sistema chat uniforme per tutti i ruoli (admin, consulente, azienda)
 * Versione: 2.0 - Unificata
 */

/* ===================================
   CONTAINER PRINCIPALE
   =================================== */

.chat-container {
    position: fixed;
    right: 20px;
    bottom: 20px;
    width: 400px;
    height: 85vh; /* Altezza adattiva: 85% dell'altezza viewport */
    max-height: 700px; /* Massimo su schermi molto grandi */
    min-height: 450px; /* Minimo per usabilità */
    background: white;
    border-radius: 16px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    display: none;
    flex-direction: column;
    z-index: 1000;
    overflow: hidden;
    animation: slideUp 0.3s ease;
}

.chat-container.open {
    display: flex;
}

/* ===================================
   HEADER
   =================================== */

.chat-header {
    background: linear-gradient(135deg, var(--color-primary, #374151) 0%, #1f2937 100%);
    color: white;
    padding: 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-radius: 16px 16px 0 0;
}

.chat-header h5 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 600;
}

.chat-header-actions {
    display: flex;
    gap: 0.5rem;
}

/* ===================================
   LISTA CHAT
   =================================== */

.chat-list {
    flex: 1;
    overflow-y: auto;
    background: #f8f9fa;
    padding: 0.5rem;
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
    background: white;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 1px solid transparent;
}

.chat-item:hover {
    background: #f0f4f8;
    transform: translateX(4px);
}

.chat-item.active {
    background: linear-gradient(135deg, rgba(var(--color-primary-rgb, 55, 65, 81), 0.1) 0%, rgba(var(--color-primary-rgb, 55, 65, 81), 0.05) 100%);
    border-color: var(--color-primary, #374151);
}

.chat-item-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--color-primary, #374151) 0%, #4b5563 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1rem;
    flex-shrink: 0;
}

.chat-item-content {
    flex: 1;
    min-width: 0;
}

.chat-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.25rem;
}

.chat-item-name {
    font-weight: 600;
    color: #2c3e50;
    font-size: 0.95rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-item-time {
    font-size: 0.75rem;
    color: #6c757d;
    flex-shrink: 0;
}

.chat-item-preview {
    font-size: 0.875rem;
    color: #6c757d;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-item-unread {
    width: 24px;
    height: 24px;
    background: #dc3545;
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 600;
    flex-shrink: 0;
}

/* ===================================
   CONVERSAZIONE
   =================================== */

.chat-conversation {
    display: flex;
    flex-direction: column;
    height: 100%;
    background: white;
}

.chat-conversation-header {
    padding: 1rem;
    border-bottom: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f8f9fa;
}

.chat-conversation-header h6 {
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 1rem;
    font-weight: 600;
    color: #2c3e50;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.chat-message {
    display: flex;
    gap: 0.75rem;
    max-width: 85%;
    animation: slideIn 0.2s ease;
}

.chat-message.own {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.chat-message.pending {
    opacity: 0.6;
}

.chat-message.pending::after {
    content: '⏱️';
    margin-left: 0.5rem;
}

.chat-message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--color-primary, #374151) 0%, #4b5563 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.75rem;
    flex-shrink: 0;
}

.chat-message-content {
    background: white;
    border-radius: 12px;
    padding: 0.75rem;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    word-wrap: break-word;
}

.chat-message.own .chat-message-content {
    background: linear-gradient(135deg, var(--color-primary, #374151) 0%, #1f2937 100%);
    color: white;
}

.chat-message-text {
    margin-bottom: 0.5rem;
    line-height: 1.5;
    white-space: pre-wrap;
}

.chat-message-meta {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.75rem;
    opacity: 0.7;
    margin-top: 0.25rem;
}

.chat-message.own .chat-message-meta {
    color: rgba(255, 255, 255, 0.9);
}

/* ===================================
   MESSAGGI SPECIALI
   =================================== */

.chat-message.special-alert {
    max-width: 95%;
    width: 100%;
}

.chat-message.special-alert.own {
    align-self: flex-end;
}

/* I messaggi speciali hanno il content trasparente per mostrare il gradiente del banner */
.chat-message.special-alert .chat-message-content {
    background: transparent !important;
    box-shadow: none !important;
    padding: 0 !important;
}

.chat-special-banner {
    position: relative;
    overflow: hidden;
    width: 100%;
}

.chat-special-banner::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 50%;
    height: 100%;
    background: linear-gradient(90deg, transparent 0%, rgba(255, 255, 255, 0.3) 50%, transparent 100%);
    animation: shimmer 3s infinite;
}

@keyframes shimmer {
    0% {
        left: -100%;
    }
    100% {
        left: 200%;
    }
}

/* ===================================
   INPUT
   =================================== */

.chat-input-container {
    border-top: 1px solid #e9ecef;
    padding: 1rem;
    background: white;
}

.chat-input {
    width: 100%;
    border: 1px solid #dee2e6;
    border-radius: 20px;
    padding: 0.5rem 1rem;
    resize: none;
    font-size: 0.9rem;
    font-family: inherit;
    transition: border-color 0.2s ease;
}

.chat-input:focus {
    outline: none;
    border-color: var(--color-primary, #374151);
}

.chat-send-button {
    background: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
    color: white;
    border: none;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.chat-send-button:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0, 123, 255, 0.3);
}

.chat-send-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ===================================
   STATI VUOTI
   =================================== */

.chat-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #6c757d;
    text-align: center;
    padding: 2rem;
}

.chat-empty i {
    font-size: 4rem;
    margin-bottom: 1rem;
    opacity: 0.3;
}

.chat-empty p {
    margin: 0;
    font-size: 0.9rem;
}

/* ===================================
   ALLEGATI
   =================================== */

.chat-attachment {
    margin-top: 0.5rem;
    border-radius: 8px;
    overflow: hidden;
    transition: all 0.2s ease;
}

.chat-attachment.image-attachment img {
    max-width: 100%;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.2s ease;
}

.chat-attachment.image-attachment img:hover {
    transform: scale(1.02);
}

.chat-attachment.pdf-attachment,
.chat-attachment.doc-attachment,
.chat-attachment.other-attachment {
    padding: 0.75rem;
    background: #f8f9fa;
    border: 1px solid #dee2e6;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    cursor: pointer;
}

.chat-attachment-preview {
    margin: 0.75rem 0;
    padding: 0.75rem;
    background: #f8f9fa;
    border: 2px solid var(--color-primary, #374151);
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* ===================================
   BADGE E NOTIFICHE
   =================================== */

.chat-badge {
    position: absolute;
    top: -8px;
    right: -8px;
    background: #dc3545;
    color: white;
    border-radius: 50%;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(220, 53, 69, 0.3);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.1);
    }
}

/* ===================================
   SKELETON LOADERS
   =================================== */

.chat-skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s infinite;
    border-radius: 4px;
}

@keyframes loading {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

.chat-item-skeleton {
    display: flex;
    gap: 0.75rem;
    padding: 0.75rem;
    margin-bottom: 0.5rem;
}

.chat-item-skeleton-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
}

.chat-item-skeleton-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.chat-item-skeleton-line {
    height: 12px;
    border-radius: 4px;
}

.chat-item-skeleton-line.short {
    width: 60%;
}

.chat-message-skeleton {
    display: flex;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.chat-message-skeleton.own {
    flex-direction: row-reverse;
}

.chat-message-skeleton-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
}

.chat-message-skeleton-content {
    flex: 1;
    max-width: 70%;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.chat-message-skeleton-line {
    height: 10px;
    border-radius: 4px;
}

.chat-message-skeleton-line.medium {
    width: 80%;
}

.chat-message-skeleton-line.short {
    width: 50%;
}

/* Animazione skeleton loading */
.skeleton-loading {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: loading 1.5s ease-in-out infinite;
}

@keyframes loading {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

/* ===================================
   RICERCA UTENTI
   =================================== */

.chat-user-search-item {
    padding: 0.75rem;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 1px solid #e9ecef;
    margin-bottom: 0.5rem;
}

.chat-user-search-item:hover {
    background: #f8f9fa;
    border-color: var(--color-primary, #374151);
    transform: translateX(4px);
}

/* ===================================
   CARD TIPO CHAT
   =================================== */

.chat-type-card {
    border: 2px solid #e9ecef;
    border-radius: 12px;
    padding: 1.5rem;
    transition: all 0.3s ease;
    cursor: pointer;
}

.chat-type-card:hover {
    border-color: var(--color-primary, #374151);
    transform: translateX(5px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* ===================================
   RESPONSIVE
   =================================== */

/* Tablet e schermi medi (768px - 1024px) */
@media (max-width: 1024px) {
    .chat-container {
        width: 380px;
        height: 80vh;
        max-height: 600px;
        min-height: 450px;
    }
}

/* Mobile landscape e tablet portrait (577px - 768px) */
@media (max-width: 768px) {
    .chat-container {
        right: 10px;
        bottom: 10px;
        width: calc(100% - 20px);
        max-width: 420px;
        height: 75vh;
        max-height: 550px;
        min-height: 400px;
    }

    .chat-header h5 {
        font-size: 1rem;
    }

    .chat-item {
        padding: 0.625rem;
    }

    .chat-item-avatar {
        width: 40px;
        height: 40px;
        font-size: 0.875rem;
    }

    .chat-item-info h6 {
        font-size: 0.9375rem;
    }

    .chat-item-info p {
        font-size: 0.8125rem;
    }

    .chat-message {
        max-width: 85%;
    }

    .chat-message-avatar {
        width: 28px;
        height: 28px;
        font-size: 0.75rem;
    }

    .chat-input-container {
        padding: 0.625rem;
        gap: 0.375rem;
    }

    .chat-input {
        font-size: 0.9375rem;
        padding: 0.5rem 0.875rem;
    }
}

/* Mobile portrait (max 576px) */
@media (max-width: 576px) {
    .chat-container {
        right: 0;
        bottom: 0;
        width: 100%;
        max-width: 100%;
        height: 100%;
        border-radius: 0;
        box-shadow: none;
    }

    .chat-header {
        border-radius: 0;
        padding: 0.875rem 1rem;
    }

    .chat-header h5 {
        font-size: 0.9375rem;
    }

    .chat-list {
        padding: 0.375rem;
    }

    .chat-item {
        padding: 0.5rem;
        margin-bottom: 0.375rem;
        border-radius: 10px;
    }

    .chat-item-avatar {
        width: 36px;
        height: 36px;
        font-size: 0.8125rem;
    }

    .chat-item-info h6 {
        font-size: 0.875rem;
    }

    .chat-item-info p {
        font-size: 0.75rem;
    }

    .chat-item-meta {
        font-size: 0.6875rem;
    }

    .chat-message {
        max-width: 90%;
        gap: 0.5rem;
    }

    .chat-message-avatar {
        width: 26px;
        height: 26px;
        font-size: 0.6875rem;
    }

    .chat-message-content {
        padding: 0.625rem 0.75rem;
        border-radius: 12px;
    }

    .chat-message-text {
        font-size: 0.9375rem;
    }

    .chat-message-meta {
        font-size: 0.6875rem;
        margin-top: 0.25rem;
    }

    .chat-input-container {
        padding: 0.5rem;
        gap: 0.375rem;
    }

    .chat-input {
        font-size: 0.875rem;
        padding: 0.5rem 0.75rem;
        border-radius: 18px;
    }

    .chat-send-button,
    .chat-input-container button {
        width: 32px;
        height: 32px;
    }

    .chat-send-button i,
    .chat-input-container button i {
        font-size: 0.875rem;
    }

    /* Conversation header responsive */
    .chat-conversation-header {
        padding: 0.75rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .chat-conversation-header h6 {
        font-size: 0.875rem;
        flex: 1 1 100%;
    }

    /* Modal responsive */
    [id$="AttachmentModal"] > div,
    [id$="MessageTypeModal"] > div {
        width: 95% !important;
        max-width: 95% !important;
    }

    /* Attachment badge responsive */
    [id$="AttachmentBadge"] {
        padding: 0.375rem 0.5rem;
        font-size: 0.8125rem;
    }

    /* Chat type cards responsive */
    .chat-type-card {
        padding: 1rem !important;
    }

    .chat-type-card h6 {
        font-size: 1rem !important;
    }

    .chat-type-card p {
        font-size: 0.8125rem !important;
    }

    /* Search results responsive */
    .chat-user-search-item {
        padding: 0.625rem;
    }

    /* Skeleton responsive */
    .chat-item-skeleton,
    .chat-message-skeleton {
        gap: 0.5rem;
    }

    .chat-item-skeleton-avatar,
    .chat-message-skeleton-avatar {
        width: 36px;
        height: 36px;
    }
}

/* Small mobile (max 400px) */
@media (max-width: 400px) {
    .chat-header {
        padding: 0.75rem 0.875rem;
    }

    .chat-header h5 {
        font-size: 0.875rem;
    }

    .chat-item-avatar {
        width: 32px;
        height: 32px;
        font-size: 0.75rem;
    }

    .chat-item-info h6 {
        font-size: 0.8125rem;
    }

    .chat-item-info p {
        font-size: 0.6875rem;
    }

    .chat-message-avatar {
        width: 24px;
        height: 24px;
        font-size: 0.625rem;
    }

    .chat-message-content {
        padding: 0.5rem 0.625rem;
    }

    .chat-message-text {
        font-size: 0.875rem;
    }

    .chat-type-card {
        padding: 0.75rem !important;
    }

    .chat-type-card h6 {
        font-size: 0.9375rem !important;
    }

    .chat-type-card p {
        font-size: 0.75rem !important;
    }
}

/* Landscape orientation */
@media (max-height: 500px) and (orientation: landscape) {
    .chat-container {
        height: calc(100vh - 20px);
        max-height: 450px;
    }

    .chat-header {
        padding: 0.625rem 0.875rem;
    }

    .chat-item {
        padding: 0.5rem;
        margin-bottom: 0.25rem;
    }

    .chat-message {
        margin-bottom: 0.375rem;
    }

    .chat-input-container {
        padding: 0.5rem;
    }
}

/* ===================================
   DARK MODE
   =================================== */

body.dark-theme .chat-container {
    background: #1a1a1a;
    color: #ecf0f1;
}

body.dark-theme .chat-header {
    background: linear-gradient(135deg, #2c3e50 0%, #1f2937 100%);
}

body.dark-theme .chat-list {
    background: #2c2c2c;
}

body.dark-theme .chat-item {
    background: #3a3a3a;
    color: #ecf0f1;
}

body.dark-theme .chat-item:hover {
    background: #4a4a4a;
}

body.dark-theme .chat-item.active {
    background: rgba(52, 152, 219, 0.2);
    border-color: #3498db;
}

body.dark-theme .chat-messages {
    background: #2c2c2c;
}

body.dark-theme .chat-message-content {
    background: #3a3a3a;
    color: #ecf0f1;
}

body.dark-theme .chat-message.own .chat-message-content {
    background: linear-gradient(135deg, #3498db 0%, #2980b9 100%);
}

body.dark-theme .chat-input-container {
    background: #1a1a1a;
    border-top-color: #3a3a3a;
}

body.dark-theme .chat-input {
    background: #3a3a3a;
    color: #ecf0f1;
    border-color: #4a4a4a;
}

body.dark-theme .chat-empty {
    color: #95a5a6;
}

body.dark-theme .chat-attachment-preview {
    background: #2c3e50;
    border-color: #34495e;
}

body.dark-theme .chat-attachment:not(.image-attachment) {
    background: #2c3e50;
    color: #ecf0f1;
    border-color: #34495e;
}

/* ===================================
   UTILITÀ
   =================================== */

.fade-in {
    animation: fadeIn 0.3s ease;
}

.slide-up {
    animation: slideUp 0.3s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ===================================
   LAZY LOADING
   =================================== */

.scroll-loader {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    background: #f8f9fa;
    border-radius: 8px;
    margin: 0.5rem;
    color: #6c757d;
    font-size: 0.875rem;
    animation: fadeIn 0.3s ease;
}

.scroll-loader .spinner-border {
    margin-right: 0.5rem;
}

.no-more-messages {
    padding: 1rem;
    text-align: center;
    color: #6c757d;
    font-size: 0.8rem;
    border-bottom: 1px solid #e9ecef;
}

.no-more-messages i {
    margin-right: 0.25rem;
}

body.dark-theme .scroll-loader {
    background: #2c3e50;
    color: #95a5a6;
}

body.dark-theme .no-more-messages {
    color: #95a5a6;
    border-bottom-color: #34495e;
}
