/* Chatbot Widget Styles */
.chatbot-widget-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 1000;
    font-family: 'Arial', sans-serif;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    /* IMPORTANTE: la finestra chat chiusa resta nel layout (visibility:hidden,
       su mobile width ~100vw x 60vh) -> il box del container coprirebbe mezza
       pagina a z-index 1000 e rubai i tap ai bottoni sotto (sezioni, Try AR).
       Il container NON deve intercettare i tap: li riattiviamo solo sul bottone
       e sulla finestra quando e' aperta. */
    pointer-events: none;
}

/* Toggle Button (Avatar) */
.chatbot-toggle-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #4CAF50, #45a049);
    border: none;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    position: relative;
    overflow: hidden;
    /* Riattiva i tap sul bottone (il container e' pointer-events:none). */
    pointer-events: auto;
}

.chatbot-toggle-btn:hover {
    transform: scale(1.1);
}

.chatbot-toggle-btn img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.chatbot-toggle-icon {
    font-size: 30px;
    color: white;
}

/* Chat Window */
.chatbot-window {
    width: 350px;
    height: 500px;
    /* Default height */
    max-height: 80vh;
    /* Max height for smaller screens */
    background: white;
    border-radius: 20px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    /* overflow: hidden; REMOVED */
    margin-bottom: 20px;
    transform-origin: bottom right;
    transition: all 0.3s cubic-bezier(0.19, 1, 0.22, 1);

    /* Hidden state */
    opacity: 0;
    transform: scale(0) translateY(20px);
    pointer-events: none;
    visibility: hidden;
}

.chatbot-window.active {
    opacity: 1;
    transform: scale(1) translateY(0);
    pointer-events: all;
    visibility: visible;
}

/* Avatar Container */
/* Avatar Container */
.chatbot-avatar-container {
    position: absolute;
    top: -100px;
    left: 0;
    width: 100%;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1001;
    pointer-events: none;
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    opacity: 0;
    transform: translateY(20px) scale(0.8);
}

.chatbot-window.active .chatbot-avatar-container {
    opacity: 1;
    transform: translateY(0) scale(1);
}


/* Avatar Image */
.chatbot-avatar-img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    border: 4px solid white;
    background: white;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    object-fit: cover;
    transform-origin: bottom center;
}

/* Talking Animation */
.chatbot-avatar-img.talking {
    animation: talkBounce 0.25s infinite alternate ease-in-out;
}

@keyframes talkBounce {
    0% {
        transform: scaleY(1);
    }

    100% {
        transform: scaleY(1.08) scaleX(0.95);
    }
}

/* Header */
.chat-widget-header {
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
    background: linear-gradient(135deg, #4CAF50, #45a049);
    color: white;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.chat-widget-title h3 {
    margin: 0;
    font-size: 16px;
    font-weight: bold;
}

.chat-widget-title p {
    margin: 2px 0 0;
    font-size: 11px;
    opacity: 0.9;
}

.chat-widget-controls button {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background 0.2s;
    font-size: 14px;
}

.chat-widget-controls button:hover {
    background: rgba(255, 255, 255, 0.4);
}

/* Messages Area */
.chat-widget-messages {
    flex: 1;
    padding: 15px;
    overflow-y: auto;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 15px;
    font-size: 14px;
    line-height: 1.4;
    word-wrap: break-word;
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chat-message.bot {
    background: white;
    border: 1px solid #e0e0e0;
    border-bottom-left-radius: 2px;
    align-self: flex-start;
    color: #333;
}

.chat-message.user {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
    border-bottom-right-radius: 2px;
    align-self: flex-end;
}

.chat-message.system {
    align-self: center;
    background: rgba(0, 0, 0, 0.05);
    color: #666;
    font-size: 11px;
    padding: 5px 10px;
    border-radius: 10px;
    text-align: center;
    max-width: 90%;
}

.chat-loading {
    display: none;
    align-self: flex-start;
    background: white;
    border: 1px solid #e0e0e0;
    border-radius: 15px;
    padding: 10px 14px;
    border-bottom-left-radius: 2px;
    margin-bottom: 5px;
}

.chat-loading.show {
    display: block;
}

.typing-dots span {
    display: inline-block;
    width: 6px;
    height: 6px;
    background: #ccc;
    border-radius: 50%;
    margin-right: 3px;
    animation: typing 1.4s infinite ease-in-out both;
}

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

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

@keyframes typing {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

/* Input Area */
.chat-widget-input {
    padding: 12px;
    background: white;
    border-top: 1px solid #eee;
    border-bottom-left-radius: 20px;
    border-bottom-right-radius: 20px;
    display: flex;
    gap: 8px;
    align-items: center;
}

.chat-widget-input input {
    flex: 1;
    border: 1px solid #ddd;
    border-radius: 20px;
    padding: 10px 15px;
    outline: none;
    font-size: 14px;
    transition: border-color 0.2s;
}

.chat-widget-input input:focus {
    border-color: #4CAF50;
}

.chat-widget-input button {
    background: #4CAF50;
    color: white;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background 0.2s, transform 0.2s;
}

.chat-widget-input button:hover {
    background: #45a049;
    transform: scale(1.05);
}

.chat-widget-input button:disabled {
    background: #ccc;
    cursor: default;
    transform: none;
}

/* Mobile Responsiveness */
@media (max-width: 480px) {
    .chatbot-window {
        width: calc(100vw - 40px);
        right: 20px;
        bottom: 90px;
        height: 60vh;
        margin-bottom: 10px;
    }

    .chatbot-toggle-btn {
        width: 50px;
        height: 50px;
    }

    .chatbot-toggle-icon {
        font-size: 24px;
    }
}

/* ========================================
   Avatar 3D Container nel Chatbot
   ======================================== */

.chatbot-avatar-3d-container {
    position: absolute;
    top: -120px;
    left: 0;
    width: 100%;
    height: 120px;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1001;
    pointer-events: none;
    opacity: 0;
    transform: translateY(20px) scale(0.8);
    transition: all 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border-radius: 15px 15px 0 0;
    overflow: hidden;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
}

.chatbot-window.active .chatbot-avatar-3d-container {
    opacity: 1;
    transform: translateY(0) scale(1);
}

#avatar-canvas {
    width: 100%;
    height: 100%;
    position: relative;
}

#avatar-canvas canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain;
}

/* Loading state per avatar */
.avatar-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
}

.avatar-loading-spinner {
    width: 30px;
    height: 30px;
    border: 3px solid rgba(255, 255, 255, 0.2);
    border-top-color: #4CAF50;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.avatar-loading-text {
    color: rgba(255, 255, 255, 0.6);
    font-size: 12px;
}

/* Hide old avatar image when 3D is active */
#chatbot-toggle img {
    transition: opacity 0.3s;
}

.chatbot-window.active ~ .chatbot-toggle-btn #toggle-avatar-img {
    opacity: 0.5;
}