/**
 * AutoFit AI Search - Chat Assistant Stylesheet
 *
 * v3.4.1 - FIX: Product card images now use aspect-ratio: 1/1 (square)
 *          with object-fit: cover + object-position: center. Balanced
 *          crop, consistent card heights.
 * v2.5.8 - Renamed @keyframes voice-pulse to autofit-voice-pulse-chat.
 * v2.5.6 - Fixed: All syntax errors (invalid spaces in selectors/properties).
 */

#autofit-chat-fab {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    right: calc(20px + env(safe-area-inset-right, 0px));
    background: #2563eb;
    color: #fff;
    border-radius: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(37, 99, 235, 0.4);
    z-index: 99999;
    transition: 0.2s;
    padding: 8px 16px;
    font-family: Tahoma, Arial, sans-serif;
    min-height: 44px;
    min-width: 44px;
}

#autofit-chat-fab:hover {
    transform: scale(1.04);
    background: #1d4ed8;
}

.autofit-fab-content {
    display: flex;
    align-items: center;
    gap: 8px;
}

.autofit-fab-custom-img {
    width: 26px;
    height: 26px;
    object-fit: contain;
    border-radius: 50%;
    display: block;
}

.autofit-fab-custom-text {
    font-size: 20px;
    line-height: 1;
}

.autofit-fab-text {
    font-size: 13px;
    font-weight: 600;
    color: #fff;
    white-space: nowrap;
}

#autofit-chat-window {
    position: fixed;
    bottom: calc(90px + env(safe-area-inset-bottom, 0px));
    right: calc(20px + env(safe-area-inset-right, 0px));
    width: 380px;
    max-width: calc(100vw - 30px);
    background: #fff;
    border-radius: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    border: 1px solid #e2e8f0;
    z-index: 99999;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: Tahoma, Arial, sans-serif;
    direction: rtl;
}

#autofit-chat-window.autofit-chat-hidden {
    display: none;
}

.autofit-chat-header {
    background: #2563eb;
    color: #fff;
    padding: 12px 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: calc(12px + env(safe-area-inset-top, 0px));
}

.autofit-chat-header h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 700;
    color: #fff;
}

#autofit-chat-close {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 8px;
    border-radius: 8px;
    transition: background 0.2s;
}

#autofit-chat-close:hover,
#autofit-chat-close:active {
    background: rgba(255, 255, 255, 0.15);
}

#autofit-chat-messages {
    padding: 12px;
    height: 340px;
    overflow-y: auto;
    background: #f8fafc;
    display: flex;
    flex-direction: column;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
}

.autofit-chat-msg {
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 13px;
    width: 100%;
    line-height: 1.4;
    box-sizing: border-box;
    word-wrap: break-word;
}

.autofit-chat-msg.ai {
    background: #e2e8f0;
    color: #1e293b;
    align-self: flex-start;
}

.autofit-chat-msg.user {
    background: #2563eb;
    color: #fff;
    align-self: flex-end;
    width: auto;
    max-width: 90%;
}

.autofit-chat-input-area {
    padding: 10px;
    background: #fff;
    border-top: 1px solid #e2e8f0;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
}

.autofit-chat-input-area .autofit-search-input-group {
    display: flex;
    gap: 6px;
    align-items: center;
    background: #f8fafc;
    padding: 6px;
    border: 2px solid #cbd5e1;
    border-radius: 10px;
    transition: all 0.3s ease;
    margin-bottom: 8px;
}

.autofit-chat-input-area .autofit-search-input-group:focus-within {
    border-color: #2563eb;
    background: #fff;
}

#autofit-chat-input {
    flex: 1;
    border: none;
    background: transparent;
    padding: 6px;
    font-size: 13px;
    outline: none;
    color: #1e293b;
    font-family: Tahoma, Arial, sans-serif;
}

#autofit-chat-input::placeholder {
    color: #94a3b8;
}

#autofit-chat-clear-btn {
    background: #fff;
    border: 1px solid #cbd5e1;
    width: 44px;
    height: 44px;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #64748b;
    padding: 0;
    flex-shrink: 0;
}

#autofit-chat-clear-btn.autofit-chat-clear-hidden {
    display: none;
}

#autofit-chat-send-btn {
    width: 100%;
    background: #2563eb;
    color: #fff;
    border: none;
    padding: 12px;
    min-height: 44px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    font-family: Tahoma, Arial, sans-serif;
    margin-top: 8px;
}

#autofit-chat-send-btn:hover {
    background: #1d4ed8;
}

#autofit-chat-voice-btn {
    width: 100%;
    background: #25D366;
    color: #fff;
    border: none;
    padding: 12px;
    min-height: 48px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    font-family: Tahoma, Arial, sans-serif;
    margin-top: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: 0.2s;
    box-shadow: 0 2px 8px rgba(37, 211, 102, 0.3);
}

#autofit-chat-voice-btn:hover {
    background: #1DA851;
    box-shadow: 0 4px 12px rgba(37, 211, 102, 0.5);
}

#autofit-chat-voice-btn .autofit-voice-icon {
    fill: currentColor;
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

#autofit-chat-voice-btn.recording {
    background: #dc2626;
    box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.6);
    animation: autofit-voice-pulse-chat 1.2s ease-in-out infinite;
}

#autofit-chat-voice-btn.recording .autofit-voice-icon {
    fill: #fff;
}

@keyframes autofit-voice-pulse-chat {
    0% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0.5); }
    70% { box-shadow: 0 0 0 12px rgba(220, 38, 38, 0); }
    100% { box-shadow: 0 0 0 0 rgba(220, 38, 38, 0); }
}

#autofit-chat-recent {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

#autofit-chat-recent .autofit-recent-item {
    background: #f1f5f9;
    border: 1px solid #e2e8f0;
    color: #334155;
    font-size: 11px;
    padding: 4px 10px;
    border-radius: 20px;
    cursor: pointer;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    min-height: 32px;
    display: inline-flex;
    align-items: center;
}

#autofit-chat-recent .autofit-recent-item:hover {
    background: #e2e8f0;
}

/* =========================================================
 * Chat Product Cards
 * ========================================================= */
@keyframes autofitFadeIn {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.autofit-chat-products-list {
    margin-top: 8px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.autofit-chat-prod-card {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    background: #fff;
    padding: 10px;
    border-radius: 6px;
    border: 1px solid #e2e8f0;
    text-decoration: none;
    color: #1e293b;
    transition: all 0.2s ease;
    animation: autofitFadeIn 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.autofit-chat-prod-card:hover {
    border-color: #2563eb;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.1);
}

/* ✅ v3.4.1: Square image container via aspect-ratio, balanced crop */
.autofit-chat-prod-img {
    width: 52px;
    aspect-ratio: 1 / 1;
    height: auto;
    object-fit: cover;
    object-position: center;
    border-radius: 4px;
    flex-shrink: 0;
    background: #f1f5f9;
    border: 1px solid #e2e8f0;
    display: block;
}

.autofit-chat-prod-info {
    flex: 1;
    min-width: 0;
}

.autofit-chat-prod-name {
    font-weight: bold;
    font-size: 12px;
    color: #1e293b;
    line-height: 1.5;
    word-break: break-word;
    white-space: normal;
    overflow: visible;
}

.autofit-chat-prod-price {
    color: #2563eb;
    font-size: 11px;
    margin-top: 4px;
    font-weight: 600;
}

/* =========================================================
 * ✅ Skeleton Loading Animation
 * ========================================================= */
.autofit-skeleton-loading {
    animation: autofitFadeIn 0.3s ease;
}

.autofit-skeleton-card {
    pointer-events: none;
}

.autofit-skeleton-img {
    width: 52px;
    aspect-ratio: 1 / 1;
    height: auto;
    border-radius: 4px;
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: autofitSkeletonShimmer 1.5s infinite;
    flex-shrink: 0;
}

.autofit-skeleton-line {
    height: 12px;
    border-radius: 4px;
    background: linear-gradient(90deg, #e2e8f0 25%, #f1f5f9 50%, #e2e8f0 75%);
    background-size: 200% 100%;
    animation: autofitSkeletonShimmer 1.5s infinite;
    margin-bottom: 6px;
}

.autofit-skeleton-name {
    width: 85%;
}

.autofit-skeleton-name-short {
    width: 60%;
}

.autofit-skeleton-price {
    width: 40%;
    height: 10px;
    margin-top: 4px;
}

@keyframes autofitSkeletonShimmer {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

/* لودینگ نقطه‌چین متحرک (نگه داشته شده برای backward compatibility) */
.autofit-typing-dots span {
    height: 7px;
    width: 7px;
    float: left;
    margin: 0 2px;
    background-color: #94a3b8;
    border-radius: 50%;
    display: inline-block;
    animation: autofitBounce 1.3s infinite ease-in-out both;
}

.autofit-typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.autofit-typing-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes autofitBounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1.0); }
}

/* =========================================================
 * 📱 Mobile Optimizations
 * ========================================================= */
@media (max-width: 480px) {
    #autofit-chat-window {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100vw;
        max-width: 100vw;
        height: 100dvh;
        max-height: 100dvh;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
        border: none;
        z-index: 999999;
        display: flex;
        flex-direction: column;
    }

    #autofit-chat-window.autofit-chat-hidden {
        display: none !important;
    }

    .autofit-chat-header {
        padding: 16px 20px;
        padding-top: calc(16px + env(safe-area-inset-top, 0px));
        background: #2563eb;
    }

    .autofit-chat-header h3 {
        font-size: 16px;
    }

    #autofit-chat-close {
        font-size: 22px;
        padding: 8px;
    }

    #autofit-chat-messages {
        flex: 1;
        height: auto !important;
        max-height: none;
        padding: 16px;
    }

    .autofit-chat-input-area {
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
        background: #fff;
        box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.05);
    }

    /* ✅ iOS Zoom Prevention */
    #autofit-chat-input {
        font-size: 16px !important;
    }

    /* ✅ Larger touch targets */
    #autofit-chat-voice-btn {
        font-size: 15px;
        min-height: 52px;
        padding: 14px;
    }

    #autofit-chat-send-btn {
        font-size: 15px;
        min-height: 48px;
    }

    #autofit-chat-clear-btn {
        width: 44px;
        height: 44px;
    }

    /* ✅ Better product cards */
    .autofit-chat-prod-card {
        padding: 12px;
        gap: 12px;
    }

    .autofit-chat-prod-img {
        width: 60px;
        aspect-ratio: 1 / 1;
        height: auto;
    }

    .autofit-chat-prod-name {
        font-size: 13px;
        line-height: 1.6;
    }

    .autofit-chat-prod-price {
        font-size: 12px;
    }

    /* ✅ Skeleton loading on mobile */
    .autofit-skeleton-img {
        width: 60px;
        aspect-ratio: 1 / 1;
        height: auto;
    }

    .autofit-skeleton-line {
        height: 14px;
    }

    /* ✅ Recent searches */
    #autofit-chat-recent .autofit-recent-item {
        font-size: 12px;
        padding: 6px 12px;
        min-height: 36px;
    }
}

/* Landscape mobile */
@media (max-height: 500px) and (orientation: landscape) {
    #autofit-chat-messages {
        height: 200px !important;
    }

    .autofit-chat-header {
        padding: 8px 16px;
    }
}

/* Dark Mode */
@media (prefers-color-scheme: dark) {
    #autofit-chat-window {
        background: #1e293b;
        border-color: #334155;
    }

    #autofit-chat-messages {
        background: #0f172a;
    }

    .autofit-chat-msg.ai {
        background: #334155;
        color: #e2e8f0;
    }

    .autofit-chat-prod-card {
        background: #1e293b;
        border-color: #334155;
        color: #e2e8f0;
    }

    .autofit-chat-prod-name {
        color: #f1f5f9;
    }

    .autofit-chat-input-area {
        background: #1e293b;
        border-top-color: #334155;
    }

    #autofit-chat-input {
        color: #f1f5f9;
    }

    /* Skeleton loading in dark mode */
    .autofit-skeleton-img,
    .autofit-skeleton-line {
        background: linear-gradient(90deg, #334155 25%, #475569 50%, #334155 75%);
        background-size: 200% 100%;
    }
}
/* =========================================================
 * 🔒 Admin-only fields (chat)
 * v3.5.0 - Hide admin-only elements from visitors in chat cards.
 * ========================================================= */

body.autofit-is-visitor .autofit-chat-admin-only,
body.autofit-is-visitor .autofit-chat-prod-date,
body.autofit-is-visitor .autofit-chat-prod-meta,
body.autofit-is-visitor .autofit-chat-prod-card time,
body.autofit-is-visitor .autofit-chat-prod-card .date,
body.autofit-is-visitor .autofit-chat-prod-card .updated {
    display: none !important;
}