/* ===== Tafiti AI Chatbot Widget ===== */

:root {
    --chat-primary: #0058b5;
    --chat-secondary: #00b2ed;
    --chat-dark: #001b4f;
    --chat-radius: 20px;
}

/* ─── Widget Root ─────────────────────────────────────────────────────────── */
#tafiti-chat-widget {
    position: fixed;
    bottom: 20px;
    left: 20px;
    z-index: 9999;
    font-family: 'Poppins', sans-serif;
}

/* ─── Pulse Ring ──────────────────────────────────────────────────────────── */
@keyframes chatPulse {
    0%   { transform: scale(1);    opacity: 0.55; }
    70%  { transform: scale(1.7);  opacity: 0;   }
    100% { transform: scale(1.7);  opacity: 0;   }
}

#tafiti-chat-widget::before {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--chat-primary), var(--chat-secondary));
    animation: chatPulse 3.2s ease-out infinite;
    z-index: -1;
    pointer-events: none;
}

#tafiti-chat-widget.widget-open::before {
    animation: none;
    opacity: 0;
}

/* ─── Toggle Button ───────────────────────────────────────────────────────── */
#tafiti-chat-toggle {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0058b5, #00b2ed);
    border: none;
    cursor: pointer;
    box-shadow:
        0 4px 16px rgba(0, 112, 207, 0.45),
        0 2px 6px  rgba(0,  27,  79, 0.20);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.25s cubic-bezier(.34, 1.56, .64, 1),
                box-shadow 0.2s ease;
    outline: none;
    position: relative;
}

#tafiti-chat-toggle:hover {
    transform: scale(1.12) translateY(-2px);
    box-shadow:
        0 8px 24px rgba(0, 112, 207, 0.55),
        0 3px 10px  rgba(0,  27,  79, 0.25);
}

/* Tooltip */
#tafiti-chat-toggle::after {
    content: 'Chat with us';
    position: absolute;
    left: calc(100% + 10px);
    top: 50%;
    transform: translateY(-50%) translateX(-6px);
    background: var(--chat-dark);
    color: #fff;
    font-size: 0.72rem;
    font-family: 'Poppins', sans-serif;
    font-weight: 500;
    white-space: nowrap;
    padding: 5px 10px;
    border-radius: 7px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    box-shadow: 0 3px 10px rgba(0, 27, 79, 0.18);
}

#tafiti-chat-toggle:hover::after {
    opacity: 1;
    transform: translateY(-50%) translateX(0);
}

/* Icon transitions */
#tafiti-chat-toggle svg {
    width: 18px;
    height: 18px;
    stroke: #ffffff;
    transition: opacity 0.2s ease, transform 0.22s ease;
    position: absolute;
}

#tafiti-chat-toggle .chat-icon-open  { opacity: 1;  transform: scale(1) rotate(0deg); }
#tafiti-chat-toggle .chat-icon-close { opacity: 0;  transform: scale(0.4) rotate(90deg); }

#tafiti-chat-toggle.open .chat-icon-open  { opacity: 0;  transform: scale(0.4) rotate(-90deg); }
#tafiti-chat-toggle.open .chat-icon-close { opacity: 1;  transform: scale(1)   rotate(0deg); }

/* ─── Chat Window ─────────────────────────────────────────────────────────── */
#tafiti-chat-window {
    position: absolute;
    bottom: 54px;
    left: 0;
    width: 370px;
    max-height: 560px;
    background: #ffffff;
    border-radius: var(--chat-radius);
    box-shadow:
        0 24px 64px rgba(0, 27,  79, 0.18),
        0  6px 24px rgba(0, 112, 207, 0.12);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(18px) scale(0.95);
    pointer-events: none;
    transition: opacity 0.28s cubic-bezier(.34, 1.56, .64, 1),
                transform 0.28s cubic-bezier(.34, 1.56, .64, 1);
    transform-origin: bottom left;
}

#tafiti-chat-window.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* ─── Header ──────────────────────────────────────────────────────────────── */
#tafiti-chat-header {
    background: linear-gradient(135deg, #0058b5 0%, #00b2ed 100%);
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

/* Decorative circles */
#tafiti-chat-header::before,
#tafiti-chat-header::after {
    content: '';
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    pointer-events: none;
}
#tafiti-chat-header::before {
    width: 110px;
    height: 110px;
    top: -42px;
    right: -18px;
}
#tafiti-chat-header::after {
    width: 65px;
    height: 65px;
    bottom: -22px;
    left: 55px;
}

.tafiti-chat-brand {
    display: flex;
    align-items: center;
    gap: 11px;
    color: #ffffff;
    position: relative;
    z-index: 1;
}

.tafiti-chat-avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.22);
    border: 1.5px solid rgba(255, 255, 255, 0.42);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    color: #fff;
    flex-shrink: 0;
}

.tafiti-chat-brand strong {
    display: block;
    font-size: 0.97rem;
    line-height: 1.2;
    font-weight: 600;
}

.tafiti-status-row {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 2px;
}

.tafiti-status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
    flex-shrink: 0;
    animation: statusPulse 2.5s ease-in-out infinite;
}

@keyframes statusPulse {
    0%, 100% { box-shadow: 0 0 0 2px rgba(74, 222, 128, 0.35); }
    50%       { box-shadow: 0 0 0 4px rgba(74, 222, 128, 0.15); }
}

#tafiti-chat-status {
    font-size: 0.72rem;
    opacity: 0.9;
}

/* Header actions group */
.tafiti-header-actions {
    display: flex;
    align-items: center;
    gap: 6px;
    position: relative;
    z-index: 1;
}

.tafiti-icon-btn {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    border-radius: 8px;
    width: 34px;
    height: 34px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    flex-shrink: 0;
}

.tafiti-icon-btn:hover { background: rgba(255, 255, 255, 0.28); }
.tafiti-icon-btn svg   { width: 16px; height: 16px; stroke: #fff; }

/* Voice toggle active state */
.tafiti-icon-btn#tafiti-voice-toggle.active {
    background: rgba(255, 255, 255, 0.32);
    box-shadow: 0 0 0 2px rgba(255,255,255,0.5);
}

/* Keep old clear-button id selector working */
#tafiti-chat-clear { /* inherits from .tafiti-icon-btn */ }

/* ─── Messages Area ───────────────────────────────────────────────────────── */
#tafiti-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px 14px 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    background: #f4f7fb;
}

#tafiti-chat-messages::-webkit-scrollbar { width: 4px; }
#tafiti-chat-messages::-webkit-scrollbar-thumb {
    background: #cbd5e1;
    border-radius: 4px;
}

/* ─── Quick Reply Chips ───────────────────────────────────────────────────── */
.tafiti-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 4px;
    align-self: flex-start;
}

.tafiti-chip {
    padding: 5px 12px;
    background: #ffffff;
    border: 1.5px solid #bfdbfe;
    border-radius: 20px;
    font-size: 0.76rem;
    color: #0058b5;
    cursor: pointer;
    font-family: 'Poppins', sans-serif;
    font-weight: 500;
    transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.15s;
    white-space: nowrap;
}

.tafiti-chip:hover {
    background: #0058b5;
    border-color: #0058b5;
    color: #fff;
    transform: translateY(-1px);
}

/* ─── Message Bubbles ─────────────────────────────────────────────────────── */
.tafiti-msg {
    max-width: 86%;
    animation: chatFadeIn 0.2s ease forwards;
}

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

.tafiti-msg p {
    margin: 0;
    font-size: 0.875rem;
    line-height: 1.6;
    padding: 10px 14px;
    border-radius: 16px;
    word-break: break-word;
}

/* Bot message */
.tafiti-msg.bot { align-self: flex-start; }
.tafiti-msg.bot p {
    background: #ffffff;
    color: #1e293b;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 27, 79, 0.08);
    border: 1px solid #e2eaf5;
}

/* User message */
.tafiti-msg.user { align-self: flex-end; }
.tafiti-msg.user p {
    background: linear-gradient(135deg, #0058b5, #00b2ed);
    color: #ffffff;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 10px rgba(0, 112, 207, 0.3);
}

/* Error state */
.tafiti-msg.error p {
    background: #fff1f2;
    color: #be123c;
    border: 1px solid #fecdd3;
}

/* ─── Typing Indicator ────────────────────────────────────────────────────── */
.tafiti-msg.typing {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 13px 18px;
    background: #ffffff;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 4px rgba(0, 27, 79, 0.08);
    border: 1px solid #e2eaf5;
}

.tafiti-msg.typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #93c5fd;
    display: inline-block;
    animation: typingBounce 1.2s infinite ease-in-out;
}
.tafiti-msg.typing span:nth-child(2) { animation-delay: 0.2s; }
.tafiti-msg.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0);   background: #93c5fd; }
    30%            { transform: translateY(-6px); background: #0070cf; }
}

/* ─── Input Row ───────────────────────────────────────────────────────────── */
#tafiti-chat-input-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-top: 1px solid #e2e8f0;
    background: #ffffff;
    flex-shrink: 0;
}

#tafiti-chat-input {
    flex: 1;
    padding: 10px 14px;
    border: 1.5px solid #e2e8f0;
    border-radius: 24px;
    font-size: 0.875rem;
    font-family: 'Poppins', sans-serif;
    color: #1e293b;
    background: #f8fafc;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}

#tafiti-chat-input:focus {
    border-color: #0070cf;
    box-shadow: 0 0 0 3px rgba(0, 112, 207, 0.12);
    background: #fff;
}

#tafiti-chat-input:disabled {
    opacity: 0.55;
    cursor: not-allowed;
}

#tafiti-chat-send {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    background: linear-gradient(135deg, #0058b5, #00b2ed);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.15s,
                transform 0.2s cubic-bezier(.34, 1.56, .64, 1),
                box-shadow 0.2s;
    box-shadow: 0 2px 8px rgba(0, 112, 207, 0.3);
}

#tafiti-chat-send:hover {
    opacity: 0.9;
    transform: scale(1.1);
    box-shadow: 0 4px 16px rgba(0, 112, 207, 0.42);
}

#tafiti-chat-send:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

#tafiti-chat-send svg { width: 17px; height: 17px; stroke: #ffffff; }

/* ─── Mic Button ──────────────────────────────────────────────────────────── */
.tafiti-mic-btn {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: #f1f5f9;
    border: 1.5px solid #e2e8f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s, box-shadow 0.2s, transform 0.15s;
}

.tafiti-mic-btn svg { width: 16px; height: 16px; stroke: #64748b; transition: stroke 0.15s; }

.tafiti-mic-btn:hover {
    background: #e0f2fe;
    border-color: #0070cf;
    transform: scale(1.08);
}
.tafiti-mic-btn:hover svg { stroke: #0070cf; }

.tafiti-mic-btn:disabled { opacity: 0.35; cursor: not-allowed; transform: none; }

/* Listening state */
@keyframes micPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(0,112,207,0.4); }
    50%       { box-shadow: 0 0 0 8px rgba(0,112,207,0); }
}

.tafiti-mic-btn.listening {
    background: #0058b5;
    border-color: #0058b5;
    animation: micPulse 1.2s ease-in-out infinite;
}
.tafiti-mic-btn.listening svg { stroke: #fff; }

/* ─── Navigation message ──────────────────────────────────────────────────── */
.tafiti-msg.nav-msg p {
    background: linear-gradient(135deg, #000b3c, #0058b5);
    color: #ffffff;
    border-bottom-left-radius: 4px;
    display: flex;
    align-items: center;
    gap: 8px;
    font-style: italic;
    border: none;
}

.tafiti-msg.nav-msg p::before {
    content: '';
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255,255,255,0.5);
    border-top-color: #fff;
    border-radius: 50%;
    animation: navSpin 0.7s linear infinite;
    flex-shrink: 0;
}

@keyframes navSpin {
    to { transform: rotate(360deg); }
}

/* ─── Powered-by footer ───────────────────────────────────────────────────── */
#tafiti-chat-footer {
    text-align: center;
    font-size: 0.67rem;
    color: #94a3b8;
    padding: 5px 12px 8px;
    background: #fff;
    flex-shrink: 0;
    border-top: 1px solid #f1f5f9;
    letter-spacing: 0.2px;
}

/* ─── Inactivity Greeting Bubble ─────────────────────────────────────────── */
.tafiti-greet-bubble {
    position: absolute;
    bottom: 54px;
    left: 0;
    width: 230px;
    background: #ffffff;
    border-radius: 16px;
    border-bottom-left-radius: 4px;
    padding: 14px 14px 12px;
    box-shadow:
        0 8px 32px rgba(0, 27, 79, 0.18),
        0 2px 8px rgba(0, 112, 207, 0.12);
    display: flex;
    flex-direction: column;
    gap: 10px;
    opacity: 0;
    transform: translateY(14px) scale(0.9);
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.35s cubic-bezier(.34, 1.56, .64, 1);
    transform-origin: bottom left;
}

.tafiti-greet-bubble.show {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

@keyframes greetShrinkOut {
    0%   { opacity: 1; transform: translateY(0) scale(1); }
    100% { opacity: 0; transform: translateY(12px) scale(0.75); }
}

.tafiti-greet-bubble.shrink-out {
    animation: greetShrinkOut 0.38s cubic-bezier(.4, 0, .6, 1) forwards;
    pointer-events: none;
}

.tafiti-greet-bubble p {
    margin: 0;
    font-size: 0.85rem;
    line-height: 1.55;
    color: #1e293b;
    padding-right: 18px;
}

.tafiti-greet-close {
    position: absolute;
    top: 8px;
    right: 10px;
    background: none;
    border: none;
    font-size: 1.1rem;
    color: #94a3b8;
    cursor: pointer;
    line-height: 1;
    padding: 2px 5px;
    transition: color 0.15s;
}

.tafiti-greet-close:hover { color: #475569; }

.tafiti-greet-cta {
    align-self: flex-start;
    padding: 6px 16px;
    background: linear-gradient(135deg, #0058b5, #00b2ed);
    color: #fff;
    border: none;
    border-radius: 20px;
    font-size: 0.78rem;
    font-family: 'Poppins', sans-serif;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s, transform 0.15s;
    box-shadow: 0 2px 8px rgba(0, 112, 207, 0.3);
}

.tafiti-greet-cta:hover {
    opacity: 0.9;
    transform: translateY(-1px);
}

/* ─── Links inside bot messages ───────────────────────────────────────────── */
.tafiti-msg.bot p a {
    color: #0058b5;
    text-decoration: underline;
    text-underline-offset: 2px;
    font-weight: 500;
    transition: color 0.15s;
    word-break: break-all;
}

.tafiti-msg.bot p a:hover { color: #00b2ed; }

/* ─── Mobile ──────────────────────────────────────────────────────────────── */
@media (max-width: 480px) {
    #tafiti-chat-widget {
        bottom: 20px;
        left: 16px;
    }

    #tafiti-chat-window {
        width: calc(100vw - 24px);
        left: 0;
        right: auto;
        bottom: 54px;
        max-height: 72vh;
    }

    #tafiti-chat-toggle::after { display: none; }

    .tafiti-greet-bubble {
        width: calc(100vw - 56px);
        max-width: 280px;
    }
}
