/**
 * Gestiolex AI Widget - Stili CSS
 * Widget fluttuante per assistente AI
 */

/* ========================================
   CSS Variables
   ======================================== */
:root {
    --ai-primary: #6366f1;
    --ai-primary-dark: #4f46e5;
    --ai-primary-light: #818cf8;
    --ai-secondary: #8b5cf6;
    --ai-bg-dark: #1e1e2e;
    --ai-bg-light: #ffffff;
    --ai-text-dark: #1f2937;
    --ai-text-light: #6b7280;
    --ai-text-muted: #9ca3af;
    --ai-border: #e5e7eb;
    --ai-border-dark: #374151;
    --ai-success: #10b981;
    --ai-warning: #f59e0b;
    --ai-error: #ef4444;
    --ai-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    --ai-shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.1);
    --ai-radius: 16px;
    --ai-radius-sm: 8px;
    --ai-transition: all 0.2s ease;
}

/* ========================================
   Floating Action Button (FAB)
   ======================================== */
.ai-fab {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-secondary) 100%);
    border: none;
    cursor: pointer;
    box-shadow: var(--ai-shadow);
    z-index: 9998;
    transition: var(--ai-transition);
    display: flex;
    align-items: center;
    justify-content: center;
    outline: none;
}

.ai-fab:hover {
    transform: scale(1.1);
    box-shadow: 0 12px 48px rgba(99, 102, 241, 0.4);
}

.ai-fab:active {
    transform: scale(0.95);
}

.ai-fab-icon {
    font-size: 28px;
    line-height: 1;
    transition: var(--ai-transition);
}

.ai-fab.open .ai-fab-icon {
    transform: rotate(90deg);
}

.ai-fab-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    background: var(--ai-error);
    color: white;
    font-size: 11px;
    font-weight: 600;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ========================================
   Widget Panel
   ======================================== */
.ai-widget {
    position: fixed;
    bottom: 100px;
    right: 24px;
    width: 420px;
    height: 600px;
    max-height: calc(100vh - 140px);
    max-width: calc(100vw - 48px);
    background: var(--ai-bg-light);
    border-radius: var(--ai-radius);
    box-shadow: var(--ai-shadow);
    z-index: 9999;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
}

.ai-widget.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ========================================
   Widget Header
   ======================================== */
.ai-widget-header {
    display: flex;
    align-items: center;
    padding: 14px 16px;
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-secondary) 100%);
    color: white;
    flex-shrink: 0;
}

.ai-widget-logo {
    width: 32px;
    height: 32px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    margin-right: 10px;
}

.ai-widget-title {
    flex: 1;
}

.ai-widget-title h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
}

.ai-widget-title span {
    font-size: 11px;
    opacity: 0.8;
}

.ai-widget-actions {
    display: flex;
    gap: 4px;
}

.ai-widget-actions button {
    width: 32px;
    height: 32px;
    border: none;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 6px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: var(--ai-transition);
}

.ai-widget-actions button:hover {
    background: rgba(255, 255, 255, 0.25);
}

/* Context Badge */
.ai-context-badge {
    display: none;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 20px;
    font-size: 11px;
    margin-right: 8px;
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ai-context-badge.visible {
    display: flex;
}

/* ========================================
   Widget Body - Messages Area
   ======================================== */
.ai-widget-body {
    flex: 1;
    display: flex;
    overflow: hidden;
    position: relative;
}

.ai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.ai-messages::-webkit-scrollbar {
    width: 6px;
}

.ai-messages::-webkit-scrollbar-track {
    background: transparent;
}

.ai-messages::-webkit-scrollbar-thumb {
    background: var(--ai-border);
    border-radius: 3px;
}

.ai-messages::-webkit-scrollbar-thumb:hover {
    background: var(--ai-text-muted);
}

/* Welcome Message */
.ai-welcome {
    text-align: center;
    padding: 40px 20px;
    color: var(--ai-text-light);
}

.ai-welcome-icon {
    font-size: 48px;
    margin-bottom: 16px;
}

.ai-welcome h4 {
    margin: 0 0 8px 0;
    color: var(--ai-text-dark);
    font-size: 18px;
}

.ai-welcome p {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
}

.ai-welcome-suggestions {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 20px;
}

.ai-loading-actions {
    color: var(--ai-text-light);
    font-size: 12px;
    text-align: center;
    padding: 10px;
}

.ai-suggestion-btn {
    padding: 10px 16px;
    background: #f3f4f6;
    border: 1px solid var(--ai-border);
    border-radius: var(--ai-radius-sm);
    color: var(--ai-text-dark);
    font-size: 13px;
    cursor: pointer;
    text-align: left;
    transition: var(--ai-transition);
}

.ai-suggestion-btn:hover {
    background: #e5e7eb;
    border-color: var(--ai-primary-light);
}

/* ========================================
   Message Bubbles
   ======================================== */
.ai-message {
    max-width: 85%;
    animation: fadeInUp 0.2s ease;
    display: flex;
    align-items: flex-start;
}

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

.ai-message.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ai-message.assistant {
    align-self: flex-start;
}

/* Avatar messaggio */
.ai-message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    flex-shrink: 0;
    margin-top: 2px;
}

.ai-message.user .ai-message-avatar {
    background: #e9ecef;
    margin-left: 8px;
}

.ai-message.assistant .ai-message-avatar {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    margin-right: 8px;
}

.ai-message-avatar .user-avatar-img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.ai-message-content {
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
}

.ai-message.user .ai-message-content {
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-secondary) 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

.ai-message.assistant .ai-message-content {
    background: #f3f4f6;
    color: var(--ai-text-dark);
    border-bottom-left-radius: 4px;
}

/* Message content markdown */
.ai-message-content p {
    margin: 0 0 8px 0;
}

.ai-message-content p:last-child {
    margin-bottom: 0;
}

.ai-message-content strong {
    font-weight: 600;
}

.ai-message-content code {
    background: rgba(0, 0, 0, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Consolas', 'Monaco', monospace;
    font-size: 13px;
}

.ai-message.user .ai-message-content code {
    background: rgba(255, 255, 255, 0.2);
}

.ai-message-content pre {
    background: #1e1e2e;
    color: #e5e7eb;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 8px 0;
}

.ai-message-content pre code {
    background: none;
    padding: 0;
    color: inherit;
}

.ai-message-content ul, .ai-message-content ol {
    margin: 8px 0;
    padding-left: 20px;
}

.ai-message-content li {
    margin: 4px 0;
}

.ai-message-content blockquote {
    border-left: 3px solid var(--ai-primary);
    margin: 8px 0;
    padding-left: 12px;
    color: var(--ai-text-light);
}

/* Message Meta */
.ai-message-meta {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 4px;
    font-size: 11px;
    color: var(--ai-text-muted);
}

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

/* Feedback buttons */
.ai-message-feedback {
    display: flex;
    gap: 4px;
    margin-top: 6px;
}

.ai-feedback-btn {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: var(--ai-text-muted);
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    transition: var(--ai-transition);
}

.ai-feedback-btn:hover {
    background: #f3f4f6;
    color: var(--ai-text-dark);
}

.ai-feedback-btn.active {
    color: var(--ai-primary);
}

/* ========================================
   Typing Indicator
   ======================================== */
.ai-typing {
    display: none;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    background: #f3f4f6;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    max-width: 80px;
}

.ai-typing.visible {
    display: flex;
}

.ai-typing span {
    width: 8px;
    height: 8px;
    background: var(--ai-text-muted);
    border-radius: 50%;
    animation: typingDot 1.4s infinite;
}

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

.ai-typing span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingDot {
    0%, 60%, 100% {
        transform: translateY(0);
        opacity: 0.4;
    }
    30% {
        transform: translateY(-4px);
        opacity: 1;
    }
}

/* ========================================
   Widget Footer - Input Area
   ======================================== */
.ai-widget-footer {
    padding: 12px 16px;
    border-top: 1px solid var(--ai-border);
    background: var(--ai-bg-light);
    flex-shrink: 0;
}

.ai-usage-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    font-size: 11px;
    color: var(--ai-text-muted);
}

.ai-usage-progress {
    flex: 1;
    height: 4px;
    background: var(--ai-border);
    border-radius: 2px;
    margin: 0 10px;
    overflow: hidden;
}

.ai-usage-progress-fill {
    height: 100%;
    background: var(--ai-primary);
    border-radius: 2px;
    transition: width 0.3s ease;
}

.ai-usage-progress-fill.warning {
    background: var(--ai-warning);
}

.ai-usage-progress-fill.danger {
    background: var(--ai-error);
}

.ai-input-container {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.ai-input-wrapper {
    flex: 1;
    position: relative;
}

#ai-input {
    width: 100%;
    min-height: 44px;
    max-height: 120px;
    padding: 12px 16px;
    border: 1px solid var(--ai-border);
    border-radius: 22px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    outline: none;
    transition: var(--ai-transition);
    line-height: 1.4;
}

#ai-input:focus {
    border-color: var(--ai-primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

#ai-input::placeholder {
    color: var(--ai-text-muted);
}

#ai-input:disabled {
    background: #f9fafb;
    cursor: not-allowed;
}

.ai-send-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-secondary) 100%);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: var(--ai-transition);
    flex-shrink: 0;
}

.ai-send-btn:hover:not(:disabled) {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}

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

.ai-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ========================================
   Sidebar - Conversation History
   ======================================== */
.ai-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    width: 280px;
    height: 100%;
    background: var(--ai-bg-light);
    border-right: 1px solid var(--ai-border);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 10;
    display: flex;
    flex-direction: column;
}

.ai-sidebar.open {
    transform: translateX(0);
}

.ai-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--ai-border);
}

.ai-sidebar-header h4 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    color: var(--ai-text-dark);
}

.ai-sidebar-close {
    width: 28px;
    height: 28px;
    border: none;
    background: transparent;
    color: var(--ai-text-light);
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
}

.ai-sidebar-close:hover {
    background: #f3f4f6;
    color: var(--ai-text-dark);
}

.ai-conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.ai-conversation-item {
    padding: 12px;
    border-radius: var(--ai-radius-sm);
    cursor: pointer;
    margin-bottom: 4px;
    transition: var(--ai-transition);
}

.ai-conversation-item:hover {
    background: #f3f4f6;
}

.ai-conversation-item.active {
    background: rgba(99, 102, 241, 0.1);
    border: 1px solid rgba(99, 102, 241, 0.2);
}

.ai-conversation-item h5 {
    margin: 0 0 4px 0;
    font-size: 13px;
    font-weight: 500;
    color: var(--ai-text-dark);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-conversation-item p {
    margin: 0;
    font-size: 11px;
    color: var(--ai-text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-conversation-item .ai-conv-date {
    font-size: 10px;
    color: var(--ai-text-muted);
    margin-top: 4px;
}

.ai-no-conversations {
    text-align: center;
    padding: 40px 20px;
    color: var(--ai-text-muted);
    font-size: 13px;
}

/* ========================================
   Error State
   ======================================== */
.ai-error-message {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #dc2626;
    padding: 12px 16px;
    border-radius: var(--ai-radius-sm);
    font-size: 13px;
    margin: 8px 0;
}

.ai-error-message button {
    margin-top: 8px;
    padding: 6px 12px;
    background: #dc2626;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

/* ========================================
   Rate Limit Warning
   ======================================== */
.ai-rate-limit-warning {
    background: #fffbeb;
    border: 1px solid #fcd34d;
    color: #92400e;
    padding: 10px 14px;
    border-radius: var(--ai-radius-sm);
    font-size: 12px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    gap: 8px;
}

/* ========================================
   AI Actions (Function Calling)
   ======================================== */
.ai-action-buttons {
    display: flex;
    gap: 10px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.ai-action-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.ai-action-btn.confirm {
    background: linear-gradient(135deg, #22c55e, #16a34a);
    color: white;
}

.ai-action-btn.confirm:hover {
    background: linear-gradient(135deg, #16a34a, #15803d);
    transform: translateY(-1px);
}

.ai-action-btn.cancel {
    background: #f3f4f6;
    color: #6b7280;
    border: 1px solid #e5e7eb;
}

.ai-action-btn.cancel:hover {
    background: #fee2e2;
    color: #dc2626;
    border-color: #fecaca;
}

.ai-action-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.ai-action-loading {
    color: #6b7280;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.ai-action-error {
    color: #dc2626;
    font-size: 14px;
}

/* ========================================
   Mobile Responsive
   ======================================== */
@media (max-width: 480px) {
    .ai-widget {
        width: 100%;
        height: 100%;
        max-height: 100vh;
        bottom: 0;
        right: 0;
        border-radius: 0;
    }
    
    .ai-fab {
        bottom: 16px;
        right: 16px;
        width: 56px;
        height: 56px;
    }
    
    .ai-sidebar {
        width: 100%;
    }
}

/* ========================================
   Print - Hide Widget
   ======================================== */
@media print {
    .ai-fab,
    .ai-widget {
        display: none !important;
    }
}

/* ========================================
   Links in Messages
   ======================================== */
.ai-message-content .ai-link {
    color: #7c3aed;
    text-decoration: none;
    font-weight: 500;
    border-bottom: 1px dashed #7c3aed;
    transition: all 0.2s ease;
}

.ai-message-content .ai-link:hover {
    color: #5b21b6;
    border-bottom-style: solid;
}


/* ========================================
   VOICE CHAT STYLES
   ======================================== */

/* Toggle voce nell'header */
#ai-voice-toggle {
    position: relative;
}

#ai-voice-toggle.voice-off {
    opacity: 0.6;
}

/* Pulsante microfono */
.ai-mic-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: #f3f4f6;
    color: #666;
    border-radius: 8px;
    cursor: pointer;
    font-size: 1.1rem;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.ai-mic-btn:hover {
    background: #e5e7eb;
    color: #7c3aed;
}

.ai-mic-btn.recording {
    background: #dc2626;
    color: #fff;
    animation: widgetMicPulse 1s infinite;
}

@keyframes widgetMicPulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.1); }
}

.ai-mic-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Indicatore "Sto parlando..." */
.ai-speaking-indicator {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    background: linear-gradient(135deg, #7c3aed 0%, #5b21b6 100%);
    color: #fff;
    border-radius: 8px;
    margin-bottom: 8px;
    font-size: 0.85rem;
    animation: fadeIn 0.3s ease;
}

.ai-speaking-indicator.visible {
    display: flex;
}

.ai-speaking-left {
    display: flex;
    align-items: center;
    gap: 8px;
}

.ai-speaking-icon {
    animation: widgetSpeakPulse 1s infinite;
}

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

.ai-speaking-text {
    font-weight: 500;
}

.ai-speaking-stop {
    background: rgba(255,255,255,0.2);
    border: none;
    color: #fff;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.8rem;
    transition: all 0.2s;
}

.ai-speaking-stop:hover {
    background: rgba(255,255,255,0.3);
}

/* Indicatore "Sto ascoltando..." */
.ai-listening-indicator {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: #fef2f2;
    color: #dc2626;
    border-radius: 6px;
    font-size: 0.8rem;
    margin-bottom: 6px;
}

.ai-listening-indicator.visible {
    display: flex;
}

.ai-listening-icon {
    animation: widgetMicPulse 1s infinite;
}

/* Aggiusta layout input container per microfono */
.ai-input-container {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}
