/*
 * AI Chat Widget — Kokila Storefront
 * Floating chat popup for EN / Sinhala / Tamil support.
 */

:root {
    --ai-primary:        #e83e8c;
    --ai-primary-dark:   #c0392b;
    --ai-primary-hover:  #d1347e;
    --ai-bg:             #ffffff;
    --ai-msg-user-bg:    #e83e8c;
    --ai-msg-bot-bg:     #f5f6fa;
    --ai-border:         #ebebeb;
    --ai-text:           #2d2d2d;
    --ai-text-light:     #7a7a8a;
    --ai-radius:         16px;
    --ai-shadow:         0 8px 32px rgba(0, 0, 0, 0.18);
    --ai-z:              10500;
}

/* =========================================================
   Keyframe Animations
   ========================================================= */

@keyframes ai-chat-bounce {
    0%,  100% { transform: translateY(0); }
    50%        { transform: translateY(-8px); }
}

@keyframes ai-chat-pulse {
    0%   { transform: scale(1);    opacity: 0.7; }
    70%  { transform: scale(1.55); opacity: 0;   }
    100% { transform: scale(1.55); opacity: 0;   }
}

@keyframes ai-chat-slide-in {
    from { transform: translateY(20px); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

@keyframes ai-dot-bounce {
    0%, 80%, 100% { transform: translateY(0);    opacity: 0.4; }
    40%            { transform: translateY(-6px); opacity: 1;   }
}

@keyframes ai-fade-in {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0);   }
}

/* =========================================================
   Widget wrapper
   ========================================================= */

#ai-chat-widget {
    position: fixed;
    bottom: 208px;
    right: 20px;
    z-index: var(--ai-z);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

/* =========================================================
   Floating toggle button
   ========================================================= */

.ai-chat-toggle {
    position: relative;
    width:  56px;
    height: 56px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-primary-dark) 100%);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(232, 62, 140, 0.45);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    outline: none;
    flex-shrink: 0;
}

.ai-chat-toggle:hover {
    background: linear-gradient(135deg, var(--ai-primary-hover) 0%, #a93226 100%);
    box-shadow: 0 6px 20px rgba(232, 62, 140, 0.55);
    transform: scale(1.06) translateY(-1px);
}

.ai-chat-toggle:focus-visible {
    outline: 3px solid rgba(232, 62, 140, 0.5);
    outline-offset: 3px;
}

/* Bounce when chat is closed */
.ai-chat-toggle.is-bouncing {
    animation: ai-chat-bounce 2s ease-in-out infinite;
}

/* Pulse ring */
.ai-chat-toggle::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background: rgba(232, 62, 140, 0.55);
    animation: ai-chat-pulse 2.2s ease-out infinite;
    pointer-events: none;
}

/* Stop pulse ring when open */
.ai-chat-toggle.is-open::before {
    animation: none;
    opacity: 0;
}

.ai-chat-toggle svg {
    width:  26px;
    height: 26px;
    fill:   #ffffff;
    flex-shrink: 0;
    transition: transform 0.25s ease;
}

/* Unread badge */
.ai-chat-toggle__badge {
    position: absolute;
    top:   -2px;
    right: -2px;
    min-width: 18px;
    height: 18px;
    padding: 0 4px;
    background: #ff3b30;
    color: #fff;
    border-radius: 9px;
    font-size: 10px;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
    border: 2px solid #fff;
}

/* =========================================================
   Chat window
   ========================================================= */

.ai-chat-window {
    position: absolute;
    bottom: 70px;        /* sits above the toggle button */
    right: 0;
    width:  380px;
    max-height: 520px;
    background: var(--ai-bg);
    border-radius: var(--ai-radius);
    box-shadow: var(--ai-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: ai-chat-slide-in 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

/* hidden state — just toggled via [hidden] attribute */
.ai-chat-window[hidden] {
    display: none;
}

/* =========================================================
   Header
   ========================================================= */

.ai-chat-header {
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-primary-dark) 100%);
    padding: 14px 14px 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-shrink: 0;
}

.ai-chat-header__info {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.ai-chat-avatar {
    width:  38px;
    height: 38px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.ai-chat-avatar svg {
    width:  22px;
    height: 22px;
    fill:   #ffffff;
}

.ai-chat-header__name {
    color: #ffffff;
    font-size: 14px;
    font-weight: 700;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ai-chat-header__status {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 11px;
    color: rgba(255, 255, 255, 0.85);
    line-height: 1;
}

.ai-chat-status-dot {
    width:  7px;
    height: 7px;
    border-radius: 50%;
    background: #4cdd78;
    box-shadow: 0 0 0 2px rgba(76, 221, 120, 0.35);
    flex-shrink: 0;
}

.ai-chat-header__actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* Language selector pills */
.ai-chat-langs {
    display: flex;
    gap: 4px;
}

.ai-lang-btn {
    padding: 3px 8px;
    border-radius: 20px;
    border: 1.5px solid rgba(255, 255, 255, 0.55);
    background: transparent;
    color: rgba(255, 255, 255, 0.85);
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    line-height: 1.4;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
    white-space: nowrap;
}

.ai-lang-btn:hover,
.ai-lang-btn:focus-visible {
    background: rgba(255, 255, 255, 0.2);
    border-color: #ffffff;
    color: #ffffff;
    outline: none;
}

.ai-lang-btn.active {
    background: rgba(255, 255, 255, 0.92);
    border-color: transparent;
    color: var(--ai-primary);
    font-weight: 700;
}

/* Close button */
.ai-chat-close {
    width:  28px;
    height: 28px;
    border-radius: 50%;
    border: 1.5px solid rgba(255, 255, 255, 0.55);
    background: transparent;
    color: rgba(255, 255, 255, 0.9);
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}

.ai-chat-close:hover,
.ai-chat-close:focus-visible {
    background: rgba(255, 255, 255, 0.22);
    color: #ffffff;
    outline: none;
}

/* =========================================================
   Messages area
   ========================================================= */

.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    min-height: 0;
    max-height: 340px;

    /* Custom scrollbar */
    scrollbar-width: thin;
    scrollbar-color: #ddd transparent;
}

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

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

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

/* Message bubble base */
.ai-msg {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 88%;
    animation: ai-fade-in 0.2s ease both;
}

/* Bot messages — left aligned */
.ai-msg--bot {
    align-self: flex-start;
}

.ai-msg--bot .ai-msg__avatar {
    width:  28px;
    height: 28px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--ai-primary) 0%, var(--ai-primary-dark) 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-bottom: 2px;
}

.ai-msg--bot .ai-msg__avatar svg {
    width:  15px;
    height: 15px;
    fill:   #ffffff;
}

.ai-msg--bot .ai-msg__bubble {
    background: var(--ai-msg-bot-bg);
    color: var(--ai-text);
    border-radius: 18px 18px 18px 4px;
    padding: 10px 14px;
    font-size: 13.5px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
}

/* User messages — right aligned */
.ai-msg--user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

.ai-msg--user .ai-msg__bubble {
    background: var(--ai-msg-user-bg);
    color: #ffffff;
    border-radius: 18px 18px 4px 18px;
    padding: 10px 14px;
    font-size: 13.5px;
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
}

/* =========================================================
   Typing indicator
   ========================================================= */

.ai-typing-indicator {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 10px 16px 4px 52px;   /* align with bot bubbles (avatar=28 + gap=8 + msg-pad=16) */
    flex-shrink: 0;
}

.ai-typing-indicator[hidden] {
    display: none;
}

.ai-typing-indicator span {
    width:  8px;
    height: 8px;
    border-radius: 50%;
    background: var(--ai-primary);
    display: inline-block;
    opacity: 0.4;
    animation: ai-dot-bounce 1.2s ease-in-out infinite both;
}

.ai-typing-indicator span:nth-child(1) { animation-delay: 0s;    }
.ai-typing-indicator span:nth-child(2) { animation-delay: 0.2s;  }
.ai-typing-indicator span:nth-child(3) { animation-delay: 0.4s;  }

/* =========================================================
   Suggested actions (chip row)
   ========================================================= */

.ai-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 16px 8px;
    flex-shrink: 0;
}

.ai-suggestion-chip {
    padding: 5px 12px;
    border-radius: 20px;
    border: 1.5px solid var(--ai-primary);
    background: transparent;
    color: var(--ai-primary);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, color 0.15s;
    line-height: 1.4;
}

.ai-suggestion-chip:hover,
.ai-suggestion-chip:focus-visible {
    background: var(--ai-primary);
    color: #ffffff;
    outline: none;
}

/* =========================================================
   Product cards in chat
   ========================================================= */

.ai-product-card {
    display: flex;
    align-items: center;
    gap: 10px;
    background: #fff;
    border: 1px solid var(--ai-border);
    border-radius: 10px;
    padding: 8px 10px;
    margin-top: 6px;
    animation: ai-fade-in 0.25s ease both;
    width: 100%;
}

.ai-product-card img {
    width:  60px;
    height: 60px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
    background: #f0f0f0;
}

.ai-product-info {
    flex: 1;
    min-width: 0;
}

.ai-product-name {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--ai-text);
    line-height: 1.35;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    margin-bottom: 3px;
}

.ai-product-name a {
    color: inherit;
    text-decoration: none;
}

.ai-product-name a:hover {
    color: var(--ai-primary);
}

.ai-product-price {
    font-size: 12px;
    font-weight: 700;
    color: var(--ai-primary);
    line-height: 1;
}

.ai-product-price--sale {
    color: var(--ai-primary);
}

.ai-product-price--original {
    font-size: 11px;
    color: var(--ai-text-light);
    text-decoration: line-through;
    font-weight: 400;
    margin-left: 4px;
}

.ai-add-cart-btn {
    padding: 5px 10px;
    border-radius: 6px;
    border: none;
    background: var(--ai-primary);
    color: #ffffff;
    font-size: 11px;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    line-height: 1.4;
    transition: background 0.15s, transform 0.1s;
}

.ai-add-cart-btn:hover {
    background: var(--ai-primary-hover);
    transform: scale(1.04);
}

.ai-add-cart-btn:active {
    transform: scale(0.97);
}

.ai-add-cart-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
}

/* Product cards container inside a bot message */
.ai-msg__products {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
    width: 100%;
}

/* =========================================================
   Input area
   ========================================================= */

.ai-chat-input-area {
    padding: 10px 12px 12px;
    border-top: 1px solid var(--ai-border);
    background: var(--ai-bg);
    flex-shrink: 0;
}

#ai-chat-form {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #f5f6fa;
    border-radius: 24px;
    padding: 6px 6px 6px 14px;
    border: 1.5px solid transparent;
    transition: border-color 0.2s;
}

#ai-chat-form:focus-within {
    border-color: var(--ai-primary);
    background: #ffffff;
}

#ai-chat-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 13.5px;
    color: var(--ai-text);
    line-height: 1.4;
    outline: none;
    min-width: 0;
    padding: 2px 0;
}

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

#ai-chat-send {
    width:  34px;
    height: 34px;
    border-radius: 50%;
    border: none;
    background: var(--ai-primary);
    color: #ffffff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, transform 0.1s;
}

#ai-chat-send:hover {
    background: var(--ai-primary-hover);
    transform: scale(1.08);
}

#ai-chat-send:active {
    transform: scale(0.95);
}

#ai-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

#ai-chat-send svg {
    width:  16px;
    height: 16px;
    fill:   #ffffff;
}

/* =========================================================
   Error message inside chat
   ========================================================= */

.ai-msg--error .ai-msg__bubble {
    background: #fff2f2;
    color: #c0392b;
    border: 1px solid #fcd7d7;
}

/* =========================================================
   Dark mode support
   ========================================================= */

[data-theme="dark"] .ai-chat-window {
    background: #1e1e2e;
    --ai-bg: #1e1e2e;
    --ai-msg-bot-bg: #2a2a3e;
    --ai-border: #3a3a50;
    --ai-text: #e0e0f0;
}

[data-theme="dark"] #ai-chat-form {
    background: #2a2a3e;
}

[data-theme="dark"] #ai-chat-form:focus-within {
    background: #1e1e2e;
}

[data-theme="dark"] #ai-chat-input {
    color: #e0e0f0;
}

[data-theme="dark"] .ai-product-card {
    background: #2a2a3e;
    border-color: #3a3a50;
}

[data-theme="dark"] .ai-product-name {
    color: #e0e0f0;
}

/* =========================================================
   Mobile — ≤576px
   ========================================================= */

@media (max-width: 576px) {
    #ai-chat-widget {
        bottom: 148px;
        right: 12px;
        left: 0;
        align-items: flex-end;
    }

    .ai-chat-toggle {
        /* stays fixed-size on mobile */
        width:  52px;
        height: 52px;
    }

    .ai-chat-window {
        position: fixed;
        bottom:  148px;
        right:   0;
        left:    0;
        width:   100vw;
        max-height: calc(100vh - 80px - 148px);
        border-radius: 16px 16px 0 0;
        animation: ai-chat-slide-in 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) both;
    }

    /* On mobile, the widget wrapper is fixed so the window needs separate positioning */
    #ai-chat-widget {
        position: fixed;
        left: auto;
        right: 12px;
        bottom: 148px;
        width: auto;
    }

    .ai-chat-messages {
        max-height: calc(100vh - 80px - 148px - 180px);
    }
}
