#psia-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

/* Screen reader only content */
.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;
}

/* Bottone di attivazione - FIX Z-INDEX */
#psia-trigger {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #ffffff;
    border: 2px solid #f0f0f0;
    cursor: pointer !important; /* Forza cursor */
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: visible; /* Cambiato da hidden */
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9998; /* Sotto il popup */
    pointer-events: auto; /* Assicura che sia cliccabile */
}

#psia-trigger:hover {
    cursor: pointer !important;
    transform: scale(1.05);
    box-shadow: 0 6px 30px rgba(0, 0, 0, 0.15);
}

#psia-trigger:active {
    transform: scale(0.95);
    transition: all 0.1s ease;
}

#psia-trigger:focus {
    outline: 3px solid rgba(102, 126, 234, 0.6);
    outline-offset: 3px;
}

/* Logo aziendale nel bottone - FIX */
#company-logo {
    width: 40px;
    height: 40px;
    transition: all 0.3s ease;
    pointer-events: none; /* Impedisce interferenze con il click */
}

/* Stili specifici per SVG - FIX */
#psia-trigger svg {
    fill: #667eea;
    transition: all 0.3s ease;
    pointer-events: none; /* Impedisce interferenze */
}

#psia-trigger:hover svg {
    transform: rotate(360deg) scale(1.1);
}

#psia-trigger.active svg {
    fill: white;
}

#psia-trigger.active {
    width: 80px;
    height: 80px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

#psia-trigger.active #company-logo {
    filter: brightness(0) invert(1);
}

/* Animazione del path SVG */
#psia-trigger svg path,
#psia-trigger svg circle {
    transition: all 0.3s ease;
    pointer-events: none;
}

#psia-trigger:hover svg path {
    stroke-dasharray: 100;
    animation: dash 2s linear infinite;
}

@keyframes dash {
    to {
        stroke-dashoffset: -100;
    }
}

/* Animazioni trigger avanzate */
#psia-trigger.pulse-notification {
    animation: pulseNotification 2s ease-in-out;
}

@keyframes pulseNotification {
    0%, 100% {
        transform: scale(1);
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    }
    25%, 75% {
        transform: scale(1.05);
        box-shadow: 0 6px 25px rgba(102, 126, 234, 0.3);
    }
    50% {
        transform: scale(1.1);
        box-shadow: 0 8px 30px rgba(102, 126, 234, 0.4);
    }
}

#psia-trigger.animating {
    pointer-events: auto; /* Cambiato da none - deve rimanere cliccabile */
}

/* Indicatore di notifica */
.notification-dot {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 12px;
    height: 12px;
    background: #ff4444;
    border-radius: 50%;
    border: 2px solid white;
    display: none;
    animation: pulse-dot 2s infinite;
    pointer-events: none; /* Non interferisce con il click */
}

@keyframes pulse-dot {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.2); opacity: 0.8; }
    100% { transform: scale(1); opacity: 1; }
}

/* Canvas 3D nel bottone */
#mini-canvas {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

/* Popup chat - Solo resizable su desktop */
#psia-popup {
    position: fixed;
    top: 20px;
    right: 20px;
    bottom: 100px;
    width: 350px;
    min-width: 300px;
    min-height: 400px;
    max-width: 500px;
    max-height: calc(100vh - 120px); /* Spazio per trigger */
    background: rgba(30, 30, 30, 0.95);
    backdrop-filter: blur(20px);
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
    opacity: 0;
    transform: translateY(20px) scale(0.9);
    transition: all 0.3s ease;
    visibility: hidden;
    border: 1px solid rgba(255, 255, 255, 0.1);
    overflow: hidden;
    z-index: 9999;
}


#psia-popup.show {
    opacity: 1;
    transform: translateY(0) scale(1);
    visibility: visible; /* Fix per visualizzazione */
}

/* Header popup */
.popup-header {
    padding: 20px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    flex-direction: column;
    position: relative;
}

.popup-header .header-1{
    display: flex;
    justify-content: space-between; /* Spazia titolo e X */
    align-items: center;
}

.popup-header .header-2{
    display: block;
    color: #ffffff;
    margin-top: 5px;
}

.popup-header .header-2 .popup-subtitle{
    font-size: 12px;
    font-style: italic;
    font-weight: 700;
}

.popup-title {
    color: white;
    font-size: 16px;
    font-weight: 500;
}

.close-btn {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    font-size: 20px;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    transition: all 0.2s;
}

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

.close-btn:focus {
    outline: 2px solid rgba(255, 255, 255, 0.5);
    outline-offset: 2px;
}

.close-btn:active {
    transform: scale(0.9);
}

/* Rimuovi l'area avatar separata */
/* .avatar-area rimossa - ora integrata */

/* Stili per i messaggi */
.message {
    margin-bottom: 15px;
    display: flex;
    animation: fadeInUp 0.3s ease;
    position: relative;
    z-index: 2;
}

.message.user-message {
    justify-content: flex-end;
}

.message.bot-message {
    justify-content: flex-start;
}

.message-content {
    max-width: 85%; /* Leggermente più largo */
    position: relative;
}

.message-time {
    font-size: 11px;
    color: rgba(255, 255, 255, 0.6);
    margin-top: 6px;
    text-align: right;
}

.bot-message .message-time {
    text-align: left;
}

/* Indicatore di digitazione */
.typing-indicator {
    animation: fadeIn 0.3s ease;
    z-index: 2;
    position: relative;
}

.typing-dots {
    display: flex;
    gap: 3px;
    padding: 15px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.typing-dots span {
    width: 6px;
    height: 6px;
    background: rgba(255, 255, 255, 0.8);
    border-radius: 50%;
    animation: typingDots 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes typingDots {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* Chat area unificata - altezza maggiore */
.chat-area {
    display: flex;
    flex-direction: column;
    height: calc(100% - 136px);
    padding: 0;
    position: relative;
}



/* Avatar 3D piccolo e rotondo */
.avatar-section {
    position: absolute;
    top: 15px;
    left: 15px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(102, 126, 234, 0.9) 0%, rgba(118, 75, 162, 0.9) 100%);
    border: 2px solid rgba(255, 255, 255, 0.2);
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    z-index: 10;
    transition: all 0.3s ease;
}

.avatar-section:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.avatar-section.speaking {
    animation: avatarPulse 0.5s ease-in-out infinite alternate;
    box-shadow: 0 0 20px rgba(102, 126, 234, 0.6);
}

.avatar-section.listening {
    animation: avatarListen 1s ease-in-out infinite;
    border-color: rgba(102, 126, 234, 0.8);
}

@keyframes avatarPulse {
    0% { transform: scale(1); }
    100% { transform: scale(1.1); }
}

@keyframes avatarListen {
    0%, 100% { box-shadow: 0 0 15px rgba(102, 126, 234, 0.3); }
    50% { box-shadow: 0 0 25px rgba(102, 126, 234, 0.6); }
}

.avatar-section #main-canvas {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    opacity: 1;
    border-radius: 50%;
}


/* Controlli chat semplificati */
.chat-controls {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding: 8px 15px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    position: relative;
    z-index: 2;
}

.clear-history-btn {
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 12px;
    transition: all 0.2s;
}

.clear-history-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: rgba(255, 255, 255, 0.9);
}

.clear-history-btn:focus {
    outline: 2px solid rgba(255, 255, 255, 0.5);
    outline-offset: 2px;
}

.clear-history-btn:active {
    transform: scale(0.95);
}

/* Container messaggi ridimensionato */
#messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    padding-top: 80px; /* Spazio per avatar piccolo */
    background: rgba(0, 0, 0, 0.3);
    position: relative;
    z-index: 2;
}

/* Messaggi con background più opaco per visibilità */
.message-text {
    padding: 12px 16px; /* Padding maggiore */
    border-radius: 20px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    backdrop-filter: blur(15px); /* Più blur */
}

.user-message .message-text {
    background: rgba(102, 126, 234, 0.95); /* Più opaco */
    color: white;
    border-bottom-right-radius: 6px;
    box-shadow: 0 2px 10px rgba(102, 126, 234, 0.4);
}

.bot-message .message-text {
    background: rgba(255, 255, 255, 0.2); /* Leggermente più opaco */
    color: rgba(255, 255, 255, 1);
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-bottom-left-radius: 6px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

/* Stati dell'indicatore - sopra avatar */
.listening-indicator {
    position: absolute;
    bottom: 10px;
    left: 50%;
    transform: translateX(-50%);
    color: #ffffff;
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.3s;
    z-index: 3;
    background: rgba(102, 126, 234, 0.9);
    padding: 6px 16px;
    border-radius: 20px;
    backdrop-filter: blur(10px);
    font-weight: 500;
}

.listening .listening-indicator {
    opacity: 1;
    animation: pulse 1.5s infinite;
}

/* Input area con z-index maggiore */
.input-area {
    padding: 15px;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.3);
    border-radius: 0 0 20px 20px;
    position: relative;
    z-index: 3;
    backdrop-filter: blur(15px);
}

.input-group {
    display: flex;
    gap: 10px;
    align-items: center;
}

#psia-input {
    flex: 1;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 20px;
    padding: 10px 15px;
    color: white;
    font-size: 14px;
    outline: none;
    transition: all 0.3s;
}

#psia-input:focus {
    background: rgba(255, 255, 255, 0.15);
    border-color: rgba(102, 126, 234, 0.5);
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.3);
    outline: none;
}

#psia-input::placeholder {
    color: rgba(255, 255, 255, 0.5);
}

.send-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    color: white;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.send-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.send-btn:focus {
    outline: 2px solid rgba(102, 126, 234, 0.8);
    outline-offset: 2px;
}

.send-btn:active {
    transform: scale(0.95);
}

/* Stati */
.listening-indicator {
    position: absolute;
    bottom: 5px;
    left: 50%;
    transform: translateX(-50%);
    color: #667eea;
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.3s;
}

.listening .listening-indicator {
    opacity: 1;
    animation: pulse 1.5s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.5; }
    50% { opacity: 1; }
}

/* Animazioni */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Scrollbar personalizzata */
#messages-container::-webkit-scrollbar {
    width: 4px;
}

#messages-container::-webkit-scrollbar-track {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 2px;
}

#messages-container::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
}

#messages-container::-webkit-scrollbar-thumb:hover {
    background: rgba(255, 255, 255, 0.5);
}

/* Focus visible for accessibility */
*:focus-visible {
    outline: 2px solid rgba(102, 126, 234, 0.8);
    outline-offset: 2px;
    border-radius: 4px;
}

/* Smooth transitions for all interactive elements */
.message {
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.message:hover {
    opacity: 0.9;
}

/* Improved typing indicator */
.typing-indicator {
    position: relative;
}

.typing-indicator::before {
    content: 'PsIA sta scrivendo';
    position: absolute;
    left: -9999px;
    top: 0;
    opacity: 0;
    font-size: 0;
}

/* Loading state for avatar */
.avatar-section.loading {
    opacity: 0.6;
    pointer-events: none;
}

.avatar-section.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top: 2px solid rgba(102, 126, 234, 0.8);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Prevent text selection during drag/resize */
.no-select {
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Status indicators */
.connection-status {
    position: absolute;
    top: 5px;
    right: 5px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #44ff44;
    z-index: 20;
}

.connection-status.offline {
    background: #ff4444;
    animation: pulse 2s infinite;
}

.connection-status.connecting {
    background: #ffaa44;
    animation: pulse 1s infinite;
}

/* Responsive - Mobile occupa tutto lo schermo */
@media (max-width: 768px) {
    #psia-popup {
        position: fixed;
        right: 20px;
        bottom: 20px;
        width: auto !important;
        height: auto !important;
        max-width: none !important;
        max-height: none !important;
        min-width: auto !important;
        min-height: auto !important;
        border-radius: 15px;
    }

    #psia-popup.show {
        transform: translateY(0) scale(1);
    }
    

    .chat-area {
        height: calc(100% - 90px); /* Dinamico */
    }

    .avatar-section {
        width: 50px;
        height: 50px;
        top: 10px;
        left: 10px;
    }

    #messages-container {
        padding-top: 70px; /* Spazio per avatar mobile */
    }
    
    
    /* Migliora accessibilità touch su mobile */
    .send-btn,
    .close-btn,
    .clear-history-btn {
        min-height: 44px;
        min-width: 44px;
    }
    
    #psia-input {
        min-height: 44px;
        font-size: 16px; /* Previene zoom su iOS */
    }
    
    #psia-trigger {
        width: 56px;
        height: 56px;
        min-height: 44px;
        min-width: 44px;
    }
    
    .avatar-section {
        touch-action: none; /* Previene scroll accidentale */
    }

    .message-text {
        font-size: 13px;
        padding: 10px 14px;
    }

    .message-content {
        max-width: 90%;
    }
}