/**
 * ShoppingTogether AI Chat Widget Styles
 * Responsive design per desktop e mobile
 */

/* ===== CHAT BUTTON ===== */
#st-chat-button {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 60px;
    height: 60px;
    background: #764ba2;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(118, 75, 162, 0.4);
    z-index: 9998;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
}

#st-chat-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(118, 75, 162, 0.6);
}

#st-chat-button.st-chat-button-hidden {
    transform: scale(0);
    opacity: 0;
}

.st-chat-btn-icon {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.st-chat-badge {
    position: absolute;
    top: -8px;
    right: -8px;
    background: #ff4757;
    color: white;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    font-size: 12px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
}

/* ===== CHAT CONTAINER ===== */
#st-chat-container {
    position: fixed;
    z-index: 9999;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Desktop Layout */
@media (min-width: 769px) {
    #st-chat-container {
        bottom: 100px;
        right: 20px;
        width: 400px;
        height: 600px;
    }
    
    .st-chat-popup {
        width: 100%;
        height: 100%;
        background: white;
        border-radius: 16px;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
        display: flex;
        flex-direction: column;
        overflow: hidden;
        transform: translateY(20px) scale(0.95);
        opacity: 0;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }
    
    #st-chat-container.st-chat-open .st-chat-popup {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

/* Mobile Layout */
@media (max-width: 768px) {
    #st-chat-container {
        top: 0;
        left: 0;
        width: 100vw;
        height: 100vh;
        background: rgba(0, 0, 0, 0.5);
    }
    
    .st-chat-mobile-overlay {
        width: 100%;
        height: 100%;
        background: white;
        display: flex;
        flex-direction: column;
    }
    
    #st-chat-container.st-chat-open .st-chat-mobile-overlay {
        display: flex;
    }
}

/* ===== CHAT HEADER ===== */
.st-chat-header,
.st-chat-mobile-header {
    background: #764ba2;
    color: white;
    padding: 16px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.st-chat-mobile-header {
    padding: 20px 20px 16px;
    padding-top: calc(env(safe-area-inset-top, 20px) + 24px);
    position: fixed;
    top: 60px; /* Sotto l'header dell'app */
    left: 0;
    right: 0;
    z-index: 9999;
    background: #764ba2;
}

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

.st-chat-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

.st-chat-avatar img {
    width: 24px;
    height: 24px;
    object-fit: contain;
}

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

.st-chat-status {
    font-size: 12px;
    opacity: 0.9;
    margin-top: 2px;
    display: block;
}
.st-chat-controls {
    display: flex;
    gap: 8px;
}

.st-btn-human-operator {
    background: #00d463;
    border: none;
    color: white;
    border-radius: 6px;
    width: 36px;
    height: 36px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: all 0.2s;
}

.st-btn-human-operator:hover {
    background: #00c157;
    transform: scale(1.05);
}

/* Stili pulsanti chiusura rimossi - navigazione tramite navbar */

/* ===== MESSAGES AREA ===== */
.st-chat-messages {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    max-height: 400px;
    background: #f8fafc !important;
    color: #333 !important;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.st-chat-messages::-webkit-scrollbar {
    width: 4px;
}

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

.st-chat-messages::-webkit-scrollbar-thumb {
    background: #cbd5e0;
    border-radius: 2px;
}

/* ===== MESSAGE BUBBLES ===== */
.st-message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    animation: messageSlideIn 0.3s ease-out;
}

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

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

.st-message-bot {
    align-self: flex-start;
    align-items: flex-start;
}

.st-message-content {
    padding: 12px 16px;
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
    font-size: 14px;
}

.st-message-error .st-message-content {
    background: #fed7d7;
    color: #c53030;
    border: 1px solid #feb2b2;
}

.st-message-system .st-message-content {
    background: linear-gradient(135deg, #4CAF50, #45a049);
    color: white;
    font-style: italic;
    border-left: 4px solid #2e7d32;
    font-weight: 500;
    text-align: center;
}

.st-message-user .st-message-content {
    color: #333 !important;
    font-weight: 500;
}

.st-message-bot .st-message-content {
    color: #333 !important;
    font-weight: 500;
}

.st-message-time {
    font-size: 11px;
    color: #a0aec0;
    margin-top: 4px;
    padding: 0 4px;
}

/* Message content formatting */
.st-message-content strong {
    font-weight: 600;
}

/* ===== DARK MODE SUPPORT ===== */
@media (prefers-color-scheme: dark) {
    .st-chat-widget,
    .st-chat-messages {
        background: #1a202c !important;
        color: #e2e8f0 !important;
    }
    
    .st-chat-header {
        background: #764ba2 !important;
        color: white !important;
    }
    
    .st-input-container {
        background: #2d3748 !important;
        border-color: #4a5568 !important;
    }
    
    .st-input-container input {
        background: #2d3748 !important;
        color: white !important;
    }
    
    /* Messaggi bot con sfondo scuro */
    .st-message-bot .st-message-content {
        color: #e2e8f0 !important;
        background: #2d3748 !important;
    }
    
    /* Messaggi utente bianchi in dark mode */
    .st-message-user .st-message-content {
        color: white !important;
    }
    
    /* Messaggi sistema restano bianchi su sfondo verde */
    .st-message-system .st-message-content {
        color: white !important;
    }
}

.st-message-content em {
    font-style: italic;
}

.st-message-content ul {
    margin: 8px 0;
    padding-left: 0;
    list-style: none;
}

.st-message-content li {
    margin: 4px 0;
    padding-left: 16px;
    position: relative;
}

.st-message-content li:before {
    content: "•";
    position: absolute;
    left: 0;
    color: #667eea;
    font-weight: bold;
}

/* ===== STREAMING STYLES ===== */
.st-streaming .st-message-content {
    position: relative;
}

.st-streaming-cursor {
    display: inline-block;
    animation: st-blink 1s infinite;
    color: #667eea;
    font-weight: bold;
    margin-left: 2px;
}

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

.st-streaming-content {
    display: inline;
}

/* ===== TYPING INDICATOR ===== */
.st-chat-typing {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    background: #f1f5f9;
    font-size: 13px;
    color: #64748b;
    border-top: 1px solid #e2e8f0;
}

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

.st-typing-dots span {
    width: 4px;
    height: 4px;
    background: #94a3b8;
    border-radius: 50%;
    animation: typingDot 1.4s infinite;
}

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

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

@keyframes typingDot {
    0%, 60%, 100% {
        transform: scale(1);
        opacity: 0.5;
    }
    30% {
        transform: scale(1.2);
        opacity: 1;
    }
}

/* ===== INPUT AREA ===== */
.st-chat-input-container {
    background: white;
    border-top: 1px solid #e2e8f0;
    flex-shrink: 0;
}

.st-chat-input-wrapper {
    display: flex;
    align-items: flex-end;
    padding: 16px 20px;
    gap: 12px;
    margin-bottom: 0px;
}

#st-chat-input {
    flex: 1;
    border: 1px solid #e2e8f0;
    border-radius: 20px;
    padding: 12px 16px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    outline: none;
    transition: border-color 0.2s;
    max-height: 120px;
    min-height: 44px;
}

#st-chat-input:focus {
    border-color: #764ba2;
    box-shadow: 0 0 0 3px rgba(118, 75, 162, 0.1);
}

#st-chat-input::placeholder {
    color: #a0aec0;
}

.st-btn-send {
    width: 44px;
    height: 44px;
    background: #764ba2;
    border: none;
    border-radius: 50%;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.st-btn-send:hover {
    transform: scale(1.05);
    box-shadow: 0 4px 12px rgba(118, 75, 162, 0.3);
}

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

.st-chat-footer {
    padding: 8px 20px 16px;
    text-align: center;
    border-top: 1px solid #f1f5f9;
}

.st-chat-footer small {
    color: #94a3b8;
    font-size: 11px;
}

.st-chat-footer a {
    color: #667eea;
    text-decoration: none;
}

.st-chat-footer a:hover {
    text-decoration: underline;
}

/* ===== MOBILE SPECIFIC ===== */
@media (max-width: 768px) {
    /* Gestione viewport per iOS */
    #st-chat-container {
        height: 100vh;
        height: 100dvh; /* Dynamic viewport height per iOS */
    }
    
    .st-chat-mobile-overlay {
        height: 100%;
        position: relative;
        padding-top: 60px; /* Spazio per l'header (sticky) */
    }
    
    /* Input area con gestione tastiera iOS e navbar */
    .st-chat-input-container {
        position: fixed !important;
        bottom: 60px !important; /* Spazio per la navbar (60px di altezza) */
        left: 0;
        right: 0;
        background: #f8fafc !important; /* Stesso sfondo dell'area messaggi */
        z-index: 99;
        box-shadow: 0 -2px 10px rgba(0,0,0,0.1);
        padding-bottom: 20px !important; /* Spazio extra sopra la navbar */
    }
    
    .st-chat-input-wrapper {
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0));
        margin-bottom: 0;
    }
    
    /* Migliora la gestione del focus sull'input */
    #st-chat-input {
        /* Previene lo zoom automatico su iOS */
        font-size: 16px;
        /* Assicura che l'input sia sempre visibile */
        transform: translateZ(0);
    }
    
    /* Quando l'input è in focus, sposta il container verso l'alto */
    #st-chat-input:focus {
        /* Forza il browser a scrollare l'input in vista */
        scroll-margin-bottom: calc(env(keyboard-inset-height, 0) + 20px);
    }
    
    #st-chat-button {
        bottom: calc(20px + env(safe-area-inset-bottom, 0));
    }
    
    .st-chat-messages {
        padding: 16px;
        padding-top: 120px; /* Spazio per header chat fisso */
        padding-bottom: 180px; /* Spazio extra per navbar (60px) + input + margine (120px) */
        -webkit-overflow-scrolling: touch;
        flex: 1;
        overflow-y: auto;
        max-height: 100dvh;
    }
    
    .st-message {
        max-width: 90%;
    }
    
    /* Gestione specifica per iPhone con notch */
    @supports (padding: max(0px)) {
        .st-chat-input-wrapper {
            padding-bottom: max(calc(16px + env(safe-area-inset-bottom, 0)), 0px);
        }
    }
    
    /* Stili specifici quando la tastiera iOS è aperta */
    #st-chat-container.st-keyboard-open {
        transition: transform 0.3s ease-out;
    }
    
    #st-chat-container.st-keyboard-open .st-chat-messages {
        /* Riduce l'altezza dei messaggi quando la tastiera è aperta */
        /* max-height: calc(50vh - 120px); */
        overflow-y: auto;
    }
    
    #st-chat-container.st-keyboard-open .st-chat-input-container {
        /* Assicura che l'input rimanga sempre visibile sopra la navbar */
        position: fixed !important;
        bottom: 60px !important; /* Mantieni spazio per navbar anche con tastiera aperta */
        left: 0;
        right: 0;
        z-index: 10000;
        background: #f8fafc !important;
        box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.15);
        padding-bottom: 20px !important; /* Mantieni margine anche con tastiera */
    }
}

/* ===== ANIMATIONS ===== */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ===== DARK MODE SUPPORT ===== */
@media (prefers-color-scheme: dark) {
    .st-chat-popup,
    .st-chat-mobile-overlay {
        background: #1a202c;
    }
    
    .st-chat-messages {
        background: #2d3748;
    }
    
    .st-message-bot .st-message-content {
        background: #4a5568;
        color: #e2e8f0;
    }
    
    .st-chat-input-container {
        background: #1a202c;
        border-top-color: #4a5568;
    }
    
    #st-chat-input {
        background: #2d3748;
        border-color: #4a5568;
        color: #e2e8f0;
    }
    
    #st-chat-input::placeholder {
        color: #718096;
    }
    
    .st-chat-footer {
        border-top-color: #4a5568;
    }
    
    .st-chat-typing {
        background: #4a5568;
        border-top-color: #4a5568;
    }
}
