/* =========================================
   Kortana Lead Chat Widget — Kaia
   Fully scoped with kp-chat__ prefix
   ========================================= */

/* --- CSS Variables (scoped) --- */
.kp-chat {
    --kp-primary: #2563EB;
    --kp-secondary: #7C3AED;
    --kp-gradient: linear-gradient(135deg, #2563EB, #7C3AED);
    --kp-bg-dark: #0D0D1A;
    --kp-bg-card: #13131F;
    --kp-border: rgba(99, 102, 241, 0.2);
    --kp-text: #E8E8F0;
    --kp-text-muted: #8B8BA0;
    --kp-success: #10B981;
    --kp-danger: #EF4444;
    --kp-font: system-ui, -apple-system, 'Segoe UI', sans-serif;
}

/* --- Toggle Button --- */
.kp-chat__toggle {
    position: fixed;
    bottom: 28px;
    right: 28px;
    z-index: 9999;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 24px rgba(37, 99, 235, 0.4);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    opacity: 0;
    transform: translateY(80px);
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    padding: 0;
    outline: none;
}

.kp-chat__toggle:hover {
    transform: scale(1.08) !important;
    box-shadow: 0 6px 32px rgba(37, 99, 235, 0.55);
}

.kp-chat__toggle:focus-visible {
    outline: 2px solid #7C3AED;
    outline-offset: 3px;
}

/* Toggle icon states */
.kp-chat__toggle-icon {
    width: 28px;
    height: 28px;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.kp-chat__toggle-icon--close {
    position: absolute;
    opacity: 0;
    transform: scale(0.5) rotate(-90deg);
}

.kp-chat--open .kp-chat__toggle-icon--k {
    opacity: 0;
    transform: scale(0.5) rotate(90deg);
}

.kp-chat--open .kp-chat__toggle-icon--close {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

/* Bounce entrance animation */
.kp-chat__toggle--visible {
    animation: kpChatBounceIn 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

@keyframes kpChatBounceIn {
    0% {
        opacity: 0;
        transform: translateY(80px);
    }
    100% {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Ping / ripple animation */
.kp-chat__ping {
    position: absolute;
    inset: 0;
    border-radius: 50%;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    z-index: -1;
    opacity: 0;
    animation: kpChatPing 4s ease-out infinite;
}

.kp-chat--open .kp-chat__ping {
    animation: none;
    opacity: 0;
}

@keyframes kpChatPing {
    0% {
        transform: scale(1);
        opacity: 0.5;
    }
    25% {
        transform: scale(1.5);
        opacity: 0;
    }
    100% {
        transform: scale(1);
        opacity: 0;
    }
}

/* --- Tooltip bubble --- */
.kp-chat__tooltip {
    position: fixed;
    bottom: 98px;
    right: 28px;
    z-index: 9998;
    background: #1A1A2E;
    color: #E8E8F0;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    font-size: 14px;
    padding: 10px 16px;
    border-radius: 12px;
    border: 1px solid rgba(99, 102, 241, 0.3);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
    white-space: nowrap;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity 0.4s ease, transform 0.4s ease;
    pointer-events: none;
}

.kp-chat__tooltip--visible {
    opacity: 1;
    transform: translateY(0);
}

.kp-chat__tooltip::after {
    content: '';
    position: absolute;
    bottom: -6px;
    right: 24px;
    width: 12px;
    height: 12px;
    background: #1A1A2E;
    border-right: 1px solid rgba(99, 102, 241, 0.3);
    border-bottom: 1px solid rgba(99, 102, 241, 0.3);
    transform: rotate(45deg);
}

/* --- Chat Panel --- */
.kp-chat__panel {
    position: fixed;
    bottom: 100px;
    right: 28px;
    z-index: 9999;
    width: 380px;
    height: 560px;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 130px);
    border-radius: 20px;
    background: #0D0D1A;
    border: 1px solid rgba(99, 102, 241, 0.2);
    box-shadow: 0 12px 48px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(99, 102, 241, 0.1);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: scale(0.8);
    transform-origin: bottom right;
    transition: opacity 0.35s ease, transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1), visibility 0.35s ease;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
}

.kp-chat--open .kp-chat__panel {
    opacity: 1;
    visibility: visible;
    transform: scale(1);
}

/* --- Header --- */
.kp-chat__header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 20px;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    flex-shrink: 0;
}

.kp-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-weight: 700;
    font-size: 14px;
    color: #fff;
    flex-shrink: 0;
    letter-spacing: 1px;
}

.kp-chat__header-info {
    flex: 1;
}

.kp-chat__header-name {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
    color: #fff;
    line-height: 1.3;
}

.kp-chat__header-status {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.85);
    margin-top: 2px;
}

.kp-chat__status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #10B981;
    display: inline-block;
    animation: kpChatStatusPulse 2s ease-in-out infinite;
}

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

/* --- Messages area --- */
.kp-chat__messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scrollbar-width: thin;
    scrollbar-color: rgba(99, 102, 241, 0.3) transparent;
}

.kp-chat__messages::-webkit-scrollbar {
    width: 4px;
}

.kp-chat__messages::-webkit-scrollbar-track {
    background: transparent;
}

.kp-chat__messages::-webkit-scrollbar-thumb {
    background: rgba(99, 102, 241, 0.3);
    border-radius: 4px;
}

/* Bot message */
.kp-chat__msg {
    display: flex;
    gap: 10px;
    animation: kpChatMsgIn 0.3s ease;
    max-width: 100%;
}

.kp-chat__msg--user {
    flex-direction: row-reverse;
}

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

.kp-chat__msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 10px;
    color: #fff;
    flex-shrink: 0;
    letter-spacing: 0.5px;
}

.kp-chat__msg-bubble {
    background: #13131F;
    color: #E8E8F0;
    padding: 10px 14px;
    border-radius: 14px 14px 14px 4px;
    font-size: 14px;
    line-height: 1.55;
    max-width: 80%;
    border-left: 3px solid transparent;
    border-image: linear-gradient(to bottom, #2563EB, #7C3AED) 1;
    word-wrap: break-word;
}

.kp-chat__msg--user .kp-chat__msg-bubble {
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    color: #fff;
    border-radius: 14px 14px 4px 14px;
    border-left: none;
    border-image: none;
}

/* Typewriter cursor */
.kp-chat__cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: #7C3AED;
    margin-left: 2px;
    vertical-align: text-bottom;
    animation: kpChatBlink 0.6s step-end infinite;
}

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

/* Typing indicator */
.kp-chat__typing {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    align-items: center;
}

.kp-chat__typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #8B8BA0;
    animation: kpChatTypingBounce 1.2s ease-in-out infinite;
}

.kp-chat__typing-dot:nth-child(2) {
    animation-delay: 0.15s;
}

.kp-chat__typing-dot:nth-child(3) {
    animation-delay: 0.3s;
}

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

/* --- Quick Replies --- */
.kp-chat__replies {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 16px 16px;
    animation: kpChatMsgIn 0.3s ease;
}

.kp-chat__reply-btn {
    padding: 8px 16px;
    font-size: 13px;
    font-weight: 500;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    color: #E8E8F0;
    background: transparent;
    border: 1px solid rgba(99, 102, 241, 0.35);
    border-radius: 999px;
    cursor: pointer;
    transition: all 0.25s ease;
    white-space: nowrap;
    outline: none;
}

.kp-chat__reply-btn:hover {
    background: rgba(99, 102, 241, 0.15);
    border-color: rgba(99, 102, 241, 0.6);
    transform: translateY(-1px);
}

.kp-chat__reply-btn:focus-visible {
    outline: 2px solid #7C3AED;
    outline-offset: 2px;
}

/* --- Recommendation Card --- */
.kp-chat__rec-card {
    background: #13131F;
    border: 1px solid rgba(99, 102, 241, 0.25);
    border-radius: 14px;
    padding: 16px;
    margin-top: 4px;
    animation: kpChatMsgIn 0.4s ease;
    max-width: 100%;
}

.kp-chat__rec-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 10px;
}

.kp-chat__rec-icon {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
}

.kp-chat__rec-title {
    font-size: 15px;
    font-weight: 600;
    color: #E8E8F0;
    margin: 0;
}

.kp-chat__rec-benefits {
    list-style: none;
    padding: 0;
    margin: 0 0 12px 0;
}

.kp-chat__rec-benefits li {
    font-size: 13px;
    color: #8B8BA0;
    padding: 4px 0;
    padding-left: 20px;
    position: relative;
    line-height: 1.4;
}

.kp-chat__rec-benefits li::before {
    content: '✓';
    position: absolute;
    left: 0;
    color: #10B981;
    font-weight: 700;
}

.kp-chat__rec-cta {
    font-size: 13px;
    color: #7C3AED;
    font-weight: 500;
    margin: 0;
}

/* --- Phone Input Area --- */
.kp-chat__phone-area {
    padding: 12px 16px;
    border-top: 1px solid rgba(99, 102, 241, 0.15);
    animation: kpChatMsgIn 0.3s ease;
    flex-shrink: 0;
}

.kp-chat__phone-form {
    display: flex;
    gap: 8px;
}

.kp-chat__phone-input {
    flex: 1;
    padding: 10px 14px;
    font-size: 14px;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    color: #E8E8F0;
    background: #1A1A2E;
    border: 1px solid rgba(99, 102, 241, 0.25);
    border-radius: 10px;
    outline: none;
    transition: border-color 0.25s ease;
}

.kp-chat__phone-input::placeholder {
    color: #8B8BA0;
}

.kp-chat__phone-input:focus {
    border-color: #7C3AED;
}

.kp-chat__phone-input--error {
    border-color: #EF4444 !important;
}

.kp-chat__phone-submit {
    padding: 10px 18px;
    font-size: 14px;
    font-weight: 600;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    color: #fff;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    border: none;
    border-radius: 10px;
    cursor: pointer;
    transition: opacity 0.25s ease, transform 0.25s ease;
    white-space: nowrap;
    outline: none;
}

.kp-chat__phone-submit:hover {
    opacity: 0.9;
    transform: translateY(-1px);
}

.kp-chat__phone-submit:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.kp-chat__phone-submit:focus-visible {
    outline: 2px solid #7C3AED;
    outline-offset: 2px;
}

.kp-chat__phone-error {
    font-size: 12px;
    color: #EF4444;
    margin: 6px 0 0 0;
    display: none;
}

.kp-chat__phone-error--visible {
    display: block;
}

/* --- Free Text Input --- */
.kp-chat__input-area {
    padding: 12px 16px;
    border-top: 1px solid rgba(99, 102, 241, 0.15);
    flex-shrink: 0;
}

.kp-chat__input-form {
    display: flex;
    gap: 8px;
}

.kp-chat__text-input {
    flex: 1;
    padding: 10px 14px;
    font-size: 14px;
    font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
    color: #E8E8F0;
    background: #1A1A2E;
    border: 1px solid rgba(99, 102, 241, 0.25);
    border-radius: 10px;
    outline: none;
    transition: border-color 0.25s ease;
}

.kp-chat__text-input::placeholder {
    color: #8B8BA0;
}

.kp-chat__text-input:focus {
    border-color: #7C3AED;
}

.kp-chat__send-btn {
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #2563EB, #7C3AED);
    border: none;
    border-radius: 10px;
    color: #fff;
    cursor: pointer;
    transition: opacity 0.25s ease;
    flex-shrink: 0;
    outline: none;
    padding: 0;
}

.kp-chat__send-btn:hover {
    opacity: 0.9;
}

.kp-chat__send-btn:focus-visible {
    outline: 2px solid #7C3AED;
    outline-offset: 2px;
}

.kp-chat__send-btn svg {
    width: 18px;
    height: 18px;
}

/* --- Responsive --- */
@media (max-width: 480px) {
    .kp-chat__panel {
        width: 95vw;
        height: calc(100vh - 130px);
        right: 2.5vw;
        bottom: 100px;
        border-radius: 16px;
    }

    .kp-chat__tooltip {
        right: 2.5vw;
        font-size: 13px;
    }
}

@media (max-width: 768px) {
    .kp-chat__panel {
        width: 95vw;
        right: 2.5vw;
    }
}

/* --- Accessibility: Reduced Motion --- */
@media (prefers-reduced-motion: reduce) {
    .kp-chat__toggle--visible {
        animation: none;
        opacity: 1;
        transform: translateY(0);
    }

    .kp-chat__ping {
        animation: none;
    }

    .kp-chat__msg,
    .kp-chat__replies,
    .kp-chat__rec-card,
    .kp-chat__phone-area {
        animation: none;
    }

    .kp-chat__panel {
        transition: opacity 0.15s ease, visibility 0.15s ease;
        transform: scale(1) !important;
    }

    .kp-chat__tooltip {
        transition: opacity 0.15s ease;
        transform: translateY(0) !important;
    }

    .kp-chat__status-dot {
        animation: none;
    }

    .kp-chat__typing-dot {
        animation: none;
    }

    .kp-chat__cursor {
        animation: none;
    }
}
