/* ============================================================
   CHAT WIDGET - VERSIONE 2 STEP
   ============================================================ */

#chat-app {
    position: fixed;
    bottom: var(--chat-offset, 20px);
    right: var(--chat-offset, 20px);
    z-index: 9999;
    font-family: var(--chat-font, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif);
}

#chat-app.chat-pos-left {
    right: auto;
    left: var(--chat-offset, 20px);
}

/* Pulsante toggle */
#chat-toggle {
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, var(--chat-color-primary, #0073aa), var(--chat-color-primary-dark, #005a87));
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 10px rgba(0,0,0,0.2);
    position: relative;
    transition: transform 0.2s;
}

#chat-toggle:hover {
    transform: scale(1.05);
}

#chat-toggle i {
    font-size: 28px;
    color: white;
}

.chat-toggle-avatar {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.chat-header-avatar {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 8px;
    flex-shrink: 0;
}

.message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    margin-top: 2px;
}

.message.operator {
    align-items: flex-end;
    gap: 8px;
}

#chat-status-led {
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 2px solid white;
}

#chat-status-led.online {
    background: #00c853;
    animation: pulse 1.5s infinite;
}

#chat-status-led.offline {
    background: #d32f2f;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(0,200,83,0.7); }
    70% { box-shadow: 0 0 0 10px rgba(0,200,83,0); }
    100% { box-shadow: 0 0 0 0 rgba(0,200,83,0); }
}

#chat-toggle .badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #d32f2f;
    color: white;
    border-radius: 50%;
    padding: 2px 6px;
    font-size: 10px;
    font-weight: bold;
    min-width: 20px;
    text-align: center;
}

/* Pannello chat */
#chat-panel {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: var(--chat-panel-width, 380px);
    height: var(--chat-panel-height, 550px);
    background: white;
    border-radius: 12px;
    box-shadow: 0 5px 30px rgba(0,0,0,0.2);
    display: none;
    flex-direction: column;
    overflow: hidden;
}

#chat-app.chat-pos-left #chat-panel {
    right: auto;
    left: 0;
}

#chat-app.chat-pos-left #chat-toggle .badge {
    right: auto;
    left: -5px;
}

#chat-app.chat-pos-left #chat-status-led {
    right: auto;
    left: 2px;
}

/* Header comune */
.chat-header {
    background: linear-gradient(135deg, var(--chat-color-primary, #0073aa), var(--chat-color-primary-dark, #005a87));
    color: white;
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.chat-header i {
    margin-right: 8px;
}

.chat-header button {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 0 8px;
}

.chat-status-badge {
    font-size: 11px;
    background: rgba(0,0,0,0.2);
    padding: 4px 10px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.chat-status-badge i {
    font-size: 8px;
    margin-right: 0;
}

.chat-status-badge i.online { color: #00c853; }
.chat-status-badge i.offline { color: #d32f2f; }

/* Step container */
.chat-step {
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* Step 1: Login */
.chat-body {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
}

.chat-body p {
    margin: 0 0 15px 0;
    color: #666;
    font-size: 14px;
}

.chat-body input {
    width: 100%;
    padding: 12px;
    margin-bottom: 12px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 14px;
    box-sizing: border-box;
}

.chat-body input:focus {
    outline: none;
    border-color: var(--chat-color-primary, #0073aa);
}

.privacy-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 15px 0;
    font-size: 12px;
}

.privacy-row input {
    width: auto;
    margin: 0;
}

.privacy-row label {
    color: #666;
}

.privacy-row a {
    color: var(--chat-color-primary, #0073aa);
    text-decoration: none;
}

.privacy-row a:hover {
    text-decoration: underline;
}

#chat-start-btn {
    width: 100%;
    padding: 12px;
    background: var(--chat-color-primary, #0073aa);
    color: white;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    transition: background 0.2s;
}

#chat-start-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

#chat-start-btn:enabled:hover {
    background: var(--chat-color-primary-dark, #005a87);
}

/* Step 2: Chat attiva */
#chat-messages-list {
    flex: 1;
    padding: 15px;
    overflow-y: auto;
    background: #f8f9fa;
    display: flex;
    flex-direction: column;
}

.system-message {
    text-align: center;
    padding: 10px;
    background: #e3f2fd;
    border-radius: 8px;
    font-size: 12px;
    color: #1976d2;
    margin-bottom: 15px;
}

.system-message i {
    margin-right: 5px;
}

/* Messaggi */
.message {
    margin-bottom: 15px;
    display: flex;
    animation: fadeIn 0.3s ease;
}

.message.user {
    justify-content: flex-end;
    align-items: flex-end;
    gap: 8px;
}

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

.message-content {
    max-width: 80%;
    padding: 10px 15px;
    border-radius: 15px;
    word-wrap: break-word;
}

.message.user .message-content {
    background: linear-gradient(135deg, var(--chat-color-primary, #0073aa), var(--chat-color-primary-dark, #005a87));
    color: white;
}

.message.operator .message-content {
    background: white;
    color: #333;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.message-name {
    font-size: 11px;
    margin-bottom: 5px;
    opacity: 0.8;
}

.message.user .message-name {
    text-align: right;
}

.message-text {
    font-size: 13px;
    line-height: 1.4;
}

.message-text img {
    max-width: 200px;
    border-radius: 8px;
    max-height: 200px;
}

.message-time {
    font-size: 10px;
    margin-top: 5px;
    opacity: 0.6;
}

.message.user .message-time {
    text-align: right;
}

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

/* Input area */
.chat-input-area {
    padding: 15px;
    border-top: 1px solid #e0e0e0;
    background: white;
    display: flex;
    gap: 10px;
    flex-shrink: 0;
}

.chat-input-area textarea {
    flex: 1;
    height: 44px;
    max-height: 44px;
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 8px;
    font-size: 13px;
    resize: none;
    font-family: inherit;
    box-sizing: border-box;
    overflow-y: auto;
}

.chat-input-area textarea:focus {
    outline: none;
    border-color: var(--chat-color-primary, #0073aa);
}

#chat-send-btn {
    width: 100px;
    height: 44px;
    padding: 0 12px;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: center;
    gap: 6px;
    white-space: nowrap;
    line-height: 1;
    background: linear-gradient(135deg, var(--chat-color-primary, #0073aa), var(--chat-color-primary-dark, #005a87));
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    transition: transform 0.2s;
    flex-shrink: 0;
}

#chat-send-btn i {
    flex-shrink: 0;
}

#chat-send-btn:hover:not(:disabled) {
    transform: translateY(-1px);
}

#chat-send-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

/* Allegati: bottone "clip", anteprima invio, e chip file nei messaggi */
#chat-attach-btn {
    width: 40px;
    height: 44px;
    flex-shrink: 0;
    border: 1px solid #ddd;
    border-radius: 8px;
    background: #f5f5f5;
    color: #555;
    cursor: pointer;
    font-size: 15px;
}

#chat-attach-btn:hover:not(:disabled) {
    background: #ebebeb;
}

#chat-attach-btn:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

#chat-attach-preview {
    padding: 6px 15px;
    font-size: 12px;
    color: #555;
    background: white;
    border-top: 1px solid #eee;
}

#chat-attach-preview.attach-error {
    color: #d32f2f;
}

.chat-attachment-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: #f5f5f5;
    border: 1px solid #ddd;
    border-radius: 8px;
    text-decoration: none;
    color: #333;
    font-size: 12px;
    max-width: 200px;
}

.chat-attachment-chip:hover {
    background: #ebebeb;
}

.chat-attachment-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Badge per nuove conversazioni */
#chat-toggle .badge {
    position: absolute;
    top: -5px;
    right: -5px;
    background: #d32f2f;
    color: white;
    border-radius: 50%;
    padding: 2px 6px;
    font-size: 10px;
    font-weight: bold;
    min-width: 20px;
    text-align: center;
    animation: pulse-badge 1s infinite;
}

/* Stili per immagini nei messaggi */
.message-text img {
    max-width: 200px;
    max-height: 200px;
    border-radius: 8px;
    display: block;
    margin-top: 5px;
    object-fit: cover;
}

.message-text a {
    color: var(--chat-color-primary, #0073aa);
    word-break: break-all;
}

.message.user .message-text a {
    color: #90caf9;
}

.message.user .message-text a:hover {
    color: #ffffff;
}

.message.operator .message-text a:hover {
    color: var(--chat-color-primary-dark, #005a87);
}

@keyframes pulse-badge {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

/* Responsive */
@media (max-width: 480px) {
    #chat-panel {
        width: 100%;
        height: 100%;
        bottom: 0;
        right: 0;
        border-radius: 0;
    }
    
    #chat-toggle {
        bottom: 10px;
        right: 10px;
    }

    #chat-app.chat-pos-left #chat-panel {
        left: 0;
    }

    #chat-app.chat-pos-left #chat-toggle {
        right: auto;
        left: 10px;
    }
}