/* ============================================
   KONTURA AI RECEPTIONIST — Chat Widget
   ChatGPT-inspired, minimalist, dark/light adaptive
   ============================================ */

:root {
    --k-primary: #6C5CE7;
    --k-primary-dark: #5a4bd1;
    --k-primary-rgb: 108, 92, 231;
    --k-bg: #ffffff;
    --k-surface: #f0f2f5;
    --k-border: #e0e4e8;
    --k-text: #0d0d0d;
    --k-text-secondary: #6b7280;
    --k-shadow: 0 0 0 1px rgba(0,0,0,0.04), 0 8px 32px rgba(0,0,0,0.12);
    --k-radius: 14px;
    --k-radius-sm: 8px;
    --k-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    --k-spacing: 15px;
    --k-fontSize: 15px;
}

/* --- Reset / scoped --- */
.k-trigger,
.k-backdrop,
.k-chat,
.k-chat *,
.k-chat *::before,
.k-chat *::after {
    box-sizing: border-box;
}

/* --- Backdrop --- */
.k-backdrop {
    position: fixed;
    inset: 0;
    z-index: 99990;
    background: rgba(0, 0, 0, 0.25);
    backdrop-filter: blur(2px);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
    -webkit-tap-highlight-color: transparent;
}

.k-backdrop.open {
    opacity: 1;
    visibility: visible;
}

/* --- Trigger Button --- */
.k-trigger {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 99991;
    width: 58px;
    height: 58px;
    border-radius: 50%;
    background: var(--k-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(var(--k-primary-rgb), 0.35);
    border: none;
    outline: none;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.k-trigger:hover {
    transform: scale(1.07);
    box-shadow: 0 6px 28px rgba(var(--k-primary-rgb), 0.45);
}

.k-trigger:active {
    transform: scale(0.95);
}

.k-trigger.open .k-trigger-icon { display: none; }
.k-trigger.open .k-trigger-close { display: flex; }
.k-trigger:not(.open) .k-trigger-icon { display: flex; }
.k-trigger:not(.open) .k-trigger-close { display: none; }

/* --- Chat Container --- */
.k-chat {
    display: none;
    position: fixed;
    bottom: 100px;
    right: 24px;
    z-index: 99992;
    width: 400px;
    height: 640px;
    max-height: calc(100vh - 140px);
    max-width: calc(100vw - 48px);
    background: var(--k-bg);
    border-radius: var(--k-radius);
    box-shadow: var(--k-shadow);
    flex-direction: column;
    overflow: hidden;
    font-family: var(--k-font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--k-text);
    -webkit-font-smoothing: antialiased;
    transform-origin: bottom right;
}

.k-chat.open {
    display: flex;
    animation: k-scaleIn 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes k-scaleIn {
    from {
        opacity: 0;
        transform: scale(0.92) translateY(12px);
    }
    to {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

/* --- Header --- */
.k-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    background: var(--k-primary);
    color: #fff;
    flex-shrink: 0;
    user-select: none;
}

.k-chat-header-left {
    display: flex;
    align-items: center;
    gap: 11px;
    min-width: 0;
}

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

.k-chat-title {
    font-size: 15px;
    font-weight: 600;
    line-height: 1.3;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.k-chat-status {
    font-size: 11px;
    opacity: 0.85;
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 1px;
}

.k-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #4ade80;
    animation: k-pulse 2.5s infinite;
}

@keyframes k-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.45; }
}

.k-close {
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    font-size: 22px;
    line-height: 1;
    padding: 2px 6px;
    border-radius: 6px;
    opacity: 0.75;
    transition: opacity 0.15s, background 0.15s;
}

.k-close:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.12);
}

/* --- Body (messages + composer) --- */
.k-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    background: var(--k-bg);
}

/* --- Messages Area --- */
.k-messages {
    flex: 1;
    overflow-y: auto;
    padding: var(--k-spacing);
    display: flex;
    flex-direction: column;
    gap: var(--k-spacing);
    scroll-behavior: smooth;
    overscroll-behavior: contain;
    background: var(--k-bg);
}

.k-messages::-webkit-scrollbar { width: 5px; }
.k-messages::-webkit-scrollbar-track { background: transparent; }
.k-messages::-webkit-scrollbar-thumb { background: #d0d5dd; border-radius: 10px; }
.k-messages::-webkit-scrollbar-thumb:hover { background: #b0b5bd; }

/* --- Message Bubbles --- */
.k-msg {
    max-width: 88%;
    padding: 10px 14px;
    border-radius: 14px;
    font-size: var(--k-fontSize);
    line-height: 1.55;
    word-wrap: break-word;
    position: relative;
    animation: k-msgIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes k-msgIn {
    from { opacity: 0; transform: translateY(8px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

.k-msg.user {
    align-self: flex-end;
    background: var(--k-primary);
    color: #fff;
    max-width: 70%;
    padding: 10px 16px;
    border-radius: 20px;
    border-bottom-right-radius: 4px;
}

.k-msg.assistant {
    align-self: flex-start;
    color: var(--k-text);
    max-width: 100%;
    padding: 0;
    background: transparent;
    border-radius: 0;
}

.k-msg.system {
    align-self: center;
    background: transparent;
    color: var(--k-text-secondary);
    font-size: 12px;
    font-style: italic;
    max-width: 100%;
    padding: 4px 0;
}

/* --- Consecutive message grouping --- */
.k-msg.user + .k-msg.user {
    margin-top: 2px;
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}

.k-msg.assistant + .k-msg.assistant {
    margin-top: 2px;
}

.k-msg.user:first-of-type,
.k-msg.assistant:first-of-type {
    margin-top: 0;
}

/* --- Message meta --- */
.k-msg-time {
    font-size: 10px;
    opacity: 0.5;
    margin-top: 4px;
    line-height: 1;
}

.k-msg.user .k-msg-time {
    text-align: right;
    color: rgba(255, 255, 255, 0.6);
}

.k-msg.assistant .k-msg-time {
    color: var(--k-text-secondary);
}

/* --- Code inside messages --- */
.k-msg code {
    background: rgba(0,0,0,0.06);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 12px;
    font-family: 'SF Mono', 'Menlo', 'Monaco', 'Consolas', monospace;
}

.k-msg.user code {
    background: rgba(255,255,255,0.15);
    color: #fff;
}

/* --- Typing Indicator --- */
.k-typing {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 14px 18px;
    background: var(--k-surface);
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    animation: k-msgIn 0.2s ease;
}

.k-typing span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--k-text-secondary);
    animation: k-bounce 1.4s infinite;
}

.k-typing span:nth-child(2) { animation-delay: 0.2s; }
.k-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes k-bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30% { transform: translateY(-7px); }
}

/* --- Composer (ChatGPT-style pill) --- */
.k-composer {
    flex-shrink: 0;
    padding: 0 var(--k-spacing) var(--k-spacing);
    background: transparent;
}

.k-composer-inner {
    display: flex;
    align-items: flex-end;
    gap: 4px;
    padding: 4px 6px 5px 4px;
    background: var(--k-surface);
    border: 1px solid var(--k-border);
    border-radius: 26px;
    transition: border-color 0.15s;
}

.k-composer-inner:focus-within {
    border-color: var(--k-primary);
}

.k-input {
    flex: 1;
    border: none;
    outline: none;
    font-size: var(--k-fontSize);
    line-height: 1.5;
    color: var(--k-text);
    background: transparent;
    font-family: inherit;
    resize: none;
    max-height: 120px;
    padding: 7px 10px;
    overflow-y: auto;
}

.k-input::placeholder {
    color: var(--k-text-secondary);
    opacity: 0.5;
}

.k-input::-webkit-scrollbar { width: 4px; }
.k-input::-webkit-scrollbar-thumb { background: #d0d5dd; border-radius: 4px; }

.k-send {
    width: 32px;
    height: 32px;
    min-width: 32px;
    border-radius: 50%;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    background: rgba(128, 128, 128, 0.1);
    color: rgba(128, 128, 128, 0.35);
    cursor: not-allowed;
    transition: background 0.15s, color 0.15s, transform 0.15s;
}

.k-send:not(:disabled) {
    background: var(--k-primary);
    color: #fff;
    cursor: pointer;
}

.k-send:not(:disabled):hover {
    filter: brightness(1.1);
}

.k-send:not(:disabled):active {
    transform: scale(0.88);
}

/* --- Lead Form --- */
.k-lead-form {
    padding: 8px 16px 12px;
    border-top: 1px solid var(--k-border);
    display: flex;
    flex-direction: column;
    gap: 8px;
    animation: k-msgIn 0.25s ease;
}

.k-lead-form input {
    padding: 9px 12px;
    border: 1px solid var(--k-border);
    border-radius: var(--k-radius-sm);
    font-size: 13px;
    outline: none;
    transition: border-color 0.15s;
    background: var(--k-bg);
    color: var(--k-text);
    font-family: inherit;
}

.k-lead-form input:focus {
    border-color: var(--k-primary);
}

.k-lead-form button {
    padding: 9px 14px;
    background: var(--k-primary);
    color: #fff;
    border: none;
    border-radius: var(--k-radius-sm);
    cursor: pointer;
    font-weight: 500;
    font-size: 13px;
    transition: background 0.15s;
    font-family: inherit;
}

.k-lead-form button:hover {
    background: var(--k-primary-dark);
}

/* --- Dark Mode --- */
@media (prefers-color-scheme: dark) {
    :root {
        --k-bg: #1a1b2e;
        --k-surface: #252640;
        --k-border: #2f3150;
        --k-text: #e8e8f0;
        --k-text-secondary: #8b8da0;
        --k-shadow: 0 0 0 1px rgba(255,255,255,0.04), 0 8px 32px rgba(0,0,0,0.5);
    }

    .k-messages::-webkit-scrollbar-thumb { background: #3d3f5a; }
    .k-messages::-webkit-scrollbar-thumb:hover { background: #505270; }

    .k-composer-inner {
        background: var(--k-surface);
        border-color: var(--k-border);
    }

    .k-msg.assistant code {
        background: rgba(255,255,255,0.08);
    }

    .k-backdrop {
        background: rgba(0, 0, 0, 0.45);
    }
}

/* --- Mobile --- */
@media (max-width: 480px) {
    .k-chat {
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        max-width: 100%;
        height: 100dvh;
        max-height: 100dvh;
        border-radius: 0;
    }

    .k-trigger {
        bottom: 16px;
        right: 16px;
        width: 54px;
        height: 54px;
    }

    .k-messages {
        padding: calc(var(--k-spacing) * 0.8);
    }

    .k-composer {
        padding: 0 calc(var(--k-spacing) * 0.8) calc(var(--k-spacing) * 0.8);
    }
}

@media (min-width: 481px) and (max-width: 768px) {
    .k-chat {
        width: 380px;
        height: 520px;
    }
}

/* --- Inline Widget (Shortcode) --- */
.k-widget--inline {
    position: relative;
    width: 100%;
    max-width: 480px;
    margin: 16px 0;
}

.k-widget--inline .k-chat {
    position: relative;
    bottom: auto;
    right: auto;
    display: flex;
    width: 100%;
    height: 520px;
    max-height: 70vh;
    max-width: 100%;
    box-shadow: 0 1px 6px rgba(0,0,0,0.08);
    border: 1px solid var(--k-border);
    animation: none;
}

.k-widget--inline .k-chat.open {
    animation: none;
}

.k-widget--inline .k-trigger,
.k-widget--inline .k-backdrop {
    display: none !important;
}
