/* ============================================
   ZENDESK-STYLE FLOATING CHAT WIDGET
   ============================================ */

/* Root Container */
#floating-chat-root {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 99999;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* ============ TOGGLE BUTTON (Fab) ============ */
.chat-fab {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #1949ff 0%, #0a2fd6 100%);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(25, 73, 255, 0.4);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    color: white;
    font-size: 26px;
    position: relative;
}

.chat-fab:hover {
    transform: scale(1.08) translateY(-2px);
    box-shadow: 0 8px 32px rgba(25, 73, 255, 0.5);
}

.chat-fab.active {
    transform: rotate(90deg);
    background: #374151;
}

/* Notification badge */
.chat-fab .badge-notify {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 20px;
    height: 20px;
    background: #ef4444;
    border-radius: 50%;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
}

/* ============ CHAT WINDOW ============ */
.chat-widget {
    position: absolute;
    bottom: 72px;
    right: 0;
    width: 400px;
    height: 560px;
    background: #ffffff;
    border-radius: 16px;
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.2);
    display: none;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(20px) scale(0.95);
    transition: all 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.chat-widget.open {
    display: flex;
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* ============ HEADER ============ */
.chat-widget-header {
    background: linear-gradient(135deg, #1949ff 0%, #0a2fd6 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 68px;
}

.chat-widget-header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255,255,255,0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
}

.chat-header-info h4 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.chat-status-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    opacity: 0.9;
}

.status-dot-online {
    width: 8px;
    height: 8px;
    background: #22c55e;
    border-radius: 50%;
    animation: pulse 2s infinite;
}

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

.chat-header-actions {
    display: flex;
    gap: 4px;
    margin-right: -8px;  /* Compensa padding header per allineare a destra */
}

.chat-header-btn {
    background: rgba(255,255,255,0.15);
    border: none;
    border-radius: 6px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: white;
    font-size: 16px;
    transition: background 0.2s;
}

.chat-header-btn:hover {
    background: rgba(255,255,255,0.25);
}

.chat-header-btn.tts-toggle {
    font-size: 14px;
    opacity: 0.6;
    transition: opacity 0.2s, background 0.2s, color 0.2s;
}

.chat-header-btn.tts-toggle:hover {
    opacity: 1;
}

.chat-header-btn.tts-toggle.tts-active {
    opacity: 1;
    background: rgba(247, 148, 29, 0.3);
    color: #F7941D;
}

/* ============ MESSAGES AREA ============ */
.chat-messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #f9fafb;
}

/* Welcome state */
.chat-welcome {
    text-align: center;
    padding: 40px 20px;
}

.chat-welcome-icon {
    width: 64px;
    height: 64px;
    background: linear-gradient(135deg, #1949ff 0%, #0a2fd6 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 16px;
    font-size: 28px;
    color: white;
}

.chat-welcome h4 {
    margin: 0 0 8px;
    color: #111827;
    font-size: 18px;
}

.chat-welcome p {
    margin: 0 0 20px;
    color: #6b7280;
    font-size: 14px;
}

.chat-quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
}

.chat-quick-btn {
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 20px;
    padding: 8px 16px;
    font-size: 13px;
    color: #374151;
    cursor: pointer;
    transition: all 0.2s;
}

.chat-quick-btn:hover {
    background: #1949ff;
    color: white;
    border-color: #1949ff;
}

/* Messages */
.chat-message {
    margin-bottom: 16px;
    display: flex;
    flex-direction: column;
}

.chat-message.user {
    align-items: flex-end;
}

.chat-message.assistant {
    align-items: flex-start;
}

.chat-bubble {
    max-width: 85%;
    padding: 12px 16px;
    border-radius: 16px;
    font-size: 14px;
    line-height: 1.5;
}

.chat-message.user .chat-bubble {
    background: linear-gradient(135deg, #1949ff 0%, #0a2fd6 100%);
    color: white;
    border-bottom-right-radius: 4px;
}

.chat-message.assistant .chat-bubble {
    background: white;
    color: #374151;
    border: 1px solid #e5e7eb;
    border-bottom-left-radius: 4px;
}

/* Markdown formatting inside assistant bubbles */
.chat-message.assistant .chat-bubble p {
    margin: 0 0 8px 0;
}
.chat-message.assistant .chat-bubble p:last-child {
    margin-bottom: 0;
}
.chat-message.assistant .chat-bubble ul,
.chat-message.assistant .chat-bubble ol {
    margin: 4px 0 8px 0;
    padding-left: 20px;
}
.chat-message.assistant .chat-bubble li {
    margin-bottom: 2px;
}
.chat-message.assistant .chat-bubble h1,
.chat-message.assistant .chat-bubble h2,
.chat-message.assistant .chat-bubble h3,
.chat-message.assistant .chat-bubble h4 {
    margin: 8px 0 4px 0;
    font-size: 0.95em;
    font-weight: 600;
}
.chat-message.assistant .chat-bubble h1 { font-size: 1.1em; }
.chat-message.assistant .chat-bubble h2 { font-size: 1.05em; }
.chat-message.assistant .chat-bubble strong {
    font-weight: 600;
}
.chat-message.assistant .chat-bubble code {
    background: #f3f4f6;
    padding: 1px 4px;
    border-radius: 3px;
    font-size: 0.9em;
    font-family: 'SF Mono', Monaco, Consolas, monospace;
}
.chat-message.assistant .chat-bubble pre {
    background: #1e1e2e;
    color: #cdd6f4;
    padding: 10px 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 6px 0;
    font-size: 0.85em;
}
.chat-message.assistant .chat-bubble pre code {
    background: transparent;
    padding: 0;
    color: inherit;
}
.chat-message.assistant .chat-bubble blockquote {
    border-left: 3px solid #e5e7eb;
    margin: 6px 0;
    padding: 4px 10px;
    color: #6b7280;
    font-style: italic;
}
.chat-message.assistant .chat-bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 6px 0;
    font-size: 0.9em;
}
.chat-message.assistant .chat-bubble th,
.chat-message.assistant .chat-bubble td {
    border: 1px solid #e5e7eb;
    padding: 4px 8px;
    text-align: left;
}
.chat-message.assistant .chat-bubble th {
    background: #f9fafb;
    font-weight: 600;
}
.chat-message.assistant .chat-bubble hr {
    border: none;
    border-top: 1px solid #e5e7eb;
    margin: 8px 0;
}

.chat-timestamp {
    font-size: 11px;
    color: #9ca3af;
    margin-top: 4px;
    padding: 0 4px;
}

.chat-attachment-preview {
    max-width: 200px;
    border-radius: 8px;
    margin-bottom: 8px;
}

/* Typing indicator */
.chat-typing-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: white;
    border-radius: 16px;
    border: 1px solid #e5e7eb;
    width: fit-content;
}

.typing-dots {
    display: flex;
    gap: 4px;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: #9ca3af;
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

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

@keyframes typing {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-4px); }
}

/* Voice preparing indicator — shown during TTS synthesis */
.chat-voice-preparing {
    display: flex;
    align-items: center;
    padding: 8px 16px;
    margin: 4px 0;
}

.voice-wave {
    display: flex;
    align-items: center;
    gap: 3px;
    height: 20px;
}

.voice-wave span {
    display: inline-block;
    width: 3px;
    height: 8px;
    background: #1949ff;
    border-radius: 2px;
    animation: voice-wave 0.8s ease-in-out infinite;
}

.voice-wave span:nth-child(1) { animation-delay: 0s; }
.voice-wave span:nth-child(2) { animation-delay: 0.15s; }
.voice-wave span:nth-child(3) { animation-delay: 0.3s; }

@keyframes voice-wave {
    0%, 100% { transform: scaleY(1); }
    50% { transform: scaleY(2.5); }
}

/* ============ INPUT AREA ============ */
.chat-input-area {
    padding: 16px;
    background: white;
    border-top: 1px solid #e5e7eb;
}

.chat-input-row {
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

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

.chat-input-field {
    width: 100%;
    border: 1px solid #e5e7eb;
    border-radius: 24px;
    padding: 12px 80px 12px 16px;
    font-size: 14px;
    resize: none;
    outline: none;
    font-family: inherit;
    transition: border-color 0.2s;
}

.chat-input-field:focus {
    border-color: #1949ff;
}

.chat-input-actions {
    position: absolute;
    right: 8px;
    bottom: 8px;
    display: flex;
    gap: 4px;
}

.chat-action-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #9ca3af;
    font-size: 16px;
    transition: all 0.2s;
}

.chat-action-btn:hover {
    background: #f3f4f6;
    color: #374151;
}

.chat-action-btn.mic.recording {
    background: #fee2e2;
    color: #ef4444;
    animation: pulse-red 1s infinite;
}

@keyframes pulse-red {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
    50% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
}

.chat-send-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: linear-gradient(135deg, #1949ff 0%, #0a2fd6 100%);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 18px;
    transition: transform 0.2s, box-shadow 0.2s;
}

.chat-send-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(25, 73, 255, 0.4);
}

/* icon provided by fa-paper-plane in HTML */

/* ============ MINIMIZED STATE ============ */
.chat-widget.minimized {
    height: 68px;
    overflow: hidden;
}

.chat-widget.minimized .chat-messages-area,
.chat-widget.minimized .chat-input-area {
    display: none;
}

/* ============ EXPANDED/MAXIMIZED STATE ============ */
.chat-widget.expanded {
    position: fixed !important;
    top: 50px !important;
    left: 50% !important;
    right: auto !important;
    bottom: auto !important;
    transform: translateX(-50%) !important;
    width: min(900px, calc(100vw - 80px)) !important;
    height: calc(100vh - 100px) !important;
    max-width: none !important;
    max-height: none !important;
    border-radius: 20px;
    z-index: 99999 !important;
    display: flex !important;
    flex-direction: column !important;
    pointer-events: auto !important;
}

.chat-widget.expanded .chat-widget-header {
    flex-shrink: 0;
}

.chat-widget.expanded .chat-messages-area {
    flex: 1 1 auto !important;
    min-height: 0 !important;
    overflow-y: auto !important;
}

.chat-widget.expanded .chat-input-area {
    flex-shrink: 0;
}

.chat-widget.expanded .chat-bubble {
    max-width: 70%;
}

.chat-widget.expanded .chat-input-field {
    padding: 16px 20px;
    padding-right: 100px;
    font-size: 15px;
}

.chat-widget.expanded .chat-welcome {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

/* Backdrop for expanded mode */
.chat-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99990;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.chat-backdrop.active {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* Expand button styling */
.chat-expand-btn {
    background: rgba(255,255,255,0.15);
    border: none;
    border-radius: 8px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: white;
    font-size: 14px;
    transition: background 0.2s;
}

.chat-expand-btn:hover {
    background: rgba(255,255,255,0.25);
}

/* Animation for expand */
.chat-widget.expanding {
    animation: expandWidget 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes expandWidget {
    from {
        width: 400px;
        height: 560px;
    }
    to {
        width: calc(100vw - 100px);
        height: calc(100vh - 100px);
    }
}

/* ============ FA ICON FALLBACK ============ */
/* Typing indicator animation */
.typing-dots span {
    animation: typing-blink 1.4s infinite both;
    font-weight: bold;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-blink {
    0%, 80%, 100% { opacity: 0.2; }
    40% { opacity: 1; }
}

/* Streaming cursor animation */
.chat-bubble.streaming .streaming-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: #1949ff;
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: blink-cursor 0.8s steps(2) infinite;
}

@keyframes blink-cursor {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* Pause animations when chat widget is hidden to save CPU */
.chat-widget:not(.open) .streaming-cursor,
.chat-widget:not(.open) .typing-dots span,
.chat-widget:not(.open) .voice-wave span,
.chat-widget:not(.open) .status-dot-online {
    animation-play-state: paused;
}

/* If Font Awesome fails to load, show text fallback for chatbot icons */
.chat-action-btn i.fa-solid,
.chat-send-btn i.fa-solid,
.chat-header-btn i.fa-solid {
    font-style: normal;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

/* ============ PIPECAT VOICE MODE ============ */
.pipecat-voice.active {
    background: #10b981 !important;
    color: white !important;
}

.pipecat-overlay {
    position: fixed;
    bottom: 96px;
    left: 24px;
    z-index: 10001;
    animation: fadeIn 0.3s ease;
}

.pipecat-overlay-content {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 20px;
    padding: 20px 28px;
    text-align: center;
    width: 220px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
    border: 1px solid rgba(230, 230, 230, 0.5);
    cursor: grab;
}

.pipecat-overlay-content:active {
    cursor: grabbing;
}

.pipecat-overlay-content h4 {
    text-align: center !important;
    letter-spacing: 0 !important;
    padding: 0 !important;
    width: 100%;
}

.pipecat-pulse {
    width: 64px;
    height: 64px;
    background: url('/assets/orb_voice.png') center center no-repeat;
    background-size: cover;
    border-radius: 50%;
    margin: 0 auto;
    animation: entity-pulse 2s ease-in-out infinite;
    filter: drop-shadow(0 0 15px rgba(30, 144, 255, 0.6));
}

@keyframes entity-pulse {
    0% { transform: scale(0.95); filter: drop-shadow(0 0 10px rgba(30, 144, 255, 0.4)); }
    50% { transform: scale(1.05); filter: drop-shadow(0 0 25px rgba(30, 144, 255, 0.8)); }
    100% { transform: scale(0.95); filter: drop-shadow(0 0 10px rgba(30, 144, 255, 0.4)); }
}

.pipecat-status {
    font-size: 0.85rem;
    font-weight: 500;
    padding: 5px 12px;
    border-radius: 16px;
    display: inline-block;
    margin: 8px 0;
}

.pipecat-status.listening {
    background: #ecfdf5;
    color: #059669;
}

.pipecat-status.speaking {
    background: #eff6ff;
    color: #1d4ed8;
}

.pipecat-status.connecting {
    background: #fef3c7;
    color: #d97706;
}

.pipecat-status.disconnected {
    background: #fee2e2;
    color: #dc2626;
}

.pipecat-status.waiting_bot {
    background: #fef3c7;
    color: #d97706;
    animation: pulse 1.5s ease-in-out infinite;
}

.pipecat-status.reconnecting {
    background: #fef3c7;
    color: #d97706;
    animation: pulse 1s ease-in-out infinite;
}

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

.pipecat-stop-btn {
    margin-top: 12px;
    padding: 8px 20px;
    background: linear-gradient(135deg, #1949ff 0%, #0a2fd6 100%);
    color: white;
    border: none;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.2s;
}

.pipecat-stop-btn:hover {
    opacity: 0.85;
}

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