/* WPAI Assistant — Frontend Chat Widget
   Electric Blue SaaS Design System
   ─────────────────────────────────────────────────────── */

/* ── Static fallback colour tokens (in :root so inline_dynamic_styles()
      can override them with a same-specificity or higher selector later).
      Primary colour vars are intentionally NOT set here on .wpai-widget —
      inline_dynamic_styles() writes them on .wpai-widget, .wpai-chat-window
      which comes after this stylesheet in source order and therefore wins. ── */
:root {
    --wpai-widget-primary:      #6366f1;
    --wpai-widget-primary-dark: #4f46e5;
    --wpai-widget-primary-light:#eff6ff;
    --wpai-primary:             #6366f1;
    --wpai-primary-dark:        #4f46e5;
    --wpai-shadow:              0 12px 40px rgba(99, 102, 241, 0.15);
    --wpai-radius:              16px;
    --wpai-widget-size:         60px;
}

/* ── Widget-scoped non-colour design tokens ────────────── */
.wpai-widget,
.wpai-chat-window {
    --wpai-widget-shadow:       0 12px 40px rgba(0, 0, 0, 0.12);
    --wpai-widget-shadow-lg:    0 20px 60px rgba(0, 0, 0, 0.18);
    --wpai-widget-bg:           #ffffff;
    --wpai-widget-surface:      #F9FAFB;
    --wpai-widget-ice:          #F0F8FF;
    --wpai-widget-border:       #E2EEF9;
    --wpai-widget-text:         #1E293B;
    --wpai-widget-muted:        #64748B;
    --wpai-widget-radius:       16px;
    --wpai-widget-primary-light:#eff6ff;
}

/* ── Font scoped strictly to the widget ────────────────── */
.wpai-widget,
.wpai-chat-window,
.wpai-widget *,
.wpai-chat-window * {
    font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    box-sizing: border-box;
}

/* ════════════════════════════════════════════════════════
   Launcher / Toggle Button
════════════════════════════════════════════════════════ */
.wpai-widget {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 99999;
}

/* Use .wpai-widget .wpai-toggle-btn (two-class specificity) so WordPress
   theme button resets (e.g. "button { background: … }") cannot override
   the primary colour variable without an !important of their own. */
.wpai-widget .wpai-toggle-btn {
    width: var(--wpai-widget-size);
    height: var(--wpai-widget-size);
    border-radius: 50%;
    background: var(--wpai-widget-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #ffffff;
    box-shadow: var(--wpai-widget-shadow);
    transition: transform 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
    position: relative;
    z-index: 2;
}

.wpai-widget .wpai-toggle-btn svg,
.wpai-widget .wpai-toggle-btn img {
    color: #ffffff;
    fill: #ffffff;
    filter: brightness(0) invert(1); /* force crisp white for any icon format */
}

.wpai-widget .wpai-toggle-btn:hover {
    transform: scale(1.09);
    background: var(--wpai-widget-primary-dark);
    box-shadow: var(--wpai-widget-shadow-lg);
}

.wpai-widget .wpai-toggle-btn:focus-visible {
    outline: 3px solid var(--wpai-widget-primary);
    outline-offset: 3px;
}

/* Hide the floating toggle button while the chat window is open.
   The header already provides a close button; keeping the toggle
   visible on top of the input area is confusing on mobile. */
.wpai-widget.wpai-is-open .wpai-toggle-btn {
    display: none !important;
}

/* ════════════════════════════════════════════════════════
   Chat Window
════════════════════════════════════════════════════════ */
.wpai-chat-window {
    position: fixed;
    bottom: 96px;
    right: 24px;
    width: 380px;
    max-width: calc(100vw - 48px);
    height: 560px;
    max-height: calc(100vh - 120px);
    background: var(--wpai-widget-bg);
    border-radius: var(--wpai-widget-radius);
    border: none;
    box-shadow: var(--wpai-widget-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: wpaiSlideIn 0.25s ease;
}

@keyframes wpaiSlideIn {
    from { opacity: 0; transform: translateY(16px) scale(0.97); }
    to   { opacity: 1; transform: translateY(0)    scale(1);    }
}

/* ════════════════════════════════════════════════════════
   Chat Header — solid Electric Blue
════════════════════════════════════════════════════════ */
.wpai-header {
    background: var(--wpai-widget-primary);
    padding: 14px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    flex-shrink: 0;
}

/* Pro CSS variable override from inline_dynamic_styles() targets this */
.wpai-chat-header {
    background: var(--wpai-header-bg, var(--wpai-widget-primary));
}

.wpai-header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.22);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}

.wpai-header-avatar .wpai-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.wpai-avatar-initials {
    font-size: 18px;
    font-weight: 700;
    color: #ffffff;
    text-transform: uppercase;
}

.wpai-header-info {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-width: 0;
}

.wpai-header-name {
    font-size: 15px;
    font-weight: 700;
    color: #ffffff;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.wpai-header-status {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.82);
}

.wpai-header-status::before {
    content: '';
    display: inline-block;
    width: 7px;
    height: 7px;
    background: #4ade80;
    border-radius: 50%;
    margin-right: 5px;
    vertical-align: middle;
}

.wpai-chat-subtitle {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.68);
    line-height: 1.3;
    margin-top: 2px;
    display: block;
}

/* Close button in header */
.wpai-header-close-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    color: rgba(255, 255, 255, 0.78);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    margin-left: auto;
    padding: 0;
    flex-shrink: 0;
    transition: background 0.15s ease, color 0.15s ease, opacity 0.15s ease;
}

/* Chevron-down SVG uses stroke, not fill — ensure CSS doesn't override it */
.wpai-header-close-btn svg {
    fill: none;
    stroke: currentColor;
}

.wpai-header-close-btn:hover {
    background: rgba(255, 255, 255, 0.18);
    color: #ffffff;
    opacity: 1;
}

.wpai-header-close-btn:focus-visible {
    outline: 2px solid rgba(255, 255, 255, 0.7);
    outline-offset: 2px;
}

/* ════════════════════════════════════════════════════════
   Lead Capture Form
════════════════════════════════════════════════════════ */
.wpai-lead-form {
    padding: 20px;
    background: var(--wpai-widget-ice);
    border-bottom: 1px solid var(--wpai-widget-border);
    flex-shrink: 0;
}

.wpai-lead-intro {
    font-size: 14px;
    color: var(--wpai-widget-text);
    margin: 0 0 14px;
    font-weight: 500;
}

.wpai-form-group {
    margin-bottom: 12px;
}

.wpai-form-group label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    color: var(--wpai-widget-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 5px;
}

.wpai-form-group input {
    width: 100%;
    padding: 9px 12px;
    background: var(--wpai-widget-surface);
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    color: var(--wpai-widget-text);
    box-shadow: 0 0 0 1px var(--wpai-widget-border) inset;
    box-sizing: border-box;
    transition: box-shadow 0.2s ease, background 0.2s ease;
}

.wpai-form-group input:focus {
    outline: none;
    background: #ffffff;
    box-shadow: 0 0 0 2px var(--wpai-widget-primary) inset;
}

.wpai-btn {
    width: 100%;
    padding: 10px;
    background: var(--wpai-widget-primary);
    color: #ffffff;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-family: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
    margin-top: 4px;
    box-shadow: 0 4px 12px rgba(0, 102, 255, 0.25);
}

.wpai-btn:hover {
    background: var(--wpai-widget-primary-dark);
    transform: translateY(-1px);
    box-shadow: 0 6px 18px rgba(0, 102, 255, 0.35);
}

.wpai-error {
    color: #dc2626;
    font-size: 12px;
    margin: 8px 0 0;
}

/* ════════════════════════════════════════════════════════
   Message List
════════════════════════════════════════════════════════ */
.wpai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    background: #ffffff;
}

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

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

.wpai-messages::-webkit-scrollbar-thumb {
    background: var(--wpai-widget-border);
    border-radius: 4px;
}

.wpai-message {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 90%;
}

.wpai-message--bot {
    align-self: flex-start;
}

.wpai-message--user {
    align-self: flex-end;
    flex-direction: row-reverse;
}

/* ── Message avatar (bot side only) ─── */
.wpai-message-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--wpai-widget-ice);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}

.wpai-message-avatar .wpai-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.wpai-message-avatar .wpai-avatar-initials {
    font-size: 12px;
    font-weight: 700;
    color: var(--wpai-widget-primary);
}

.wpai-message--user .wpai-message-avatar {
    display: none;
}

/* ── Bubbles ─────────────────────────── */
.wpai-message-bubble {
    padding: 10px 14px;
    font-size: 14px;
    line-height: 1.55;
    max-width: 100%;
    word-wrap: break-word;
    border: none;
}

/* User: Electric Blue, rounded except bottom-right tail */
.wpai-message--user .wpai-message-bubble {
    background: var(--wpai-widget-primary);
    color: #ffffff;
    border-radius: 16px 16px 4px 16px;
    box-shadow: 0 2px 8px rgba(0, 102, 255, 0.2);
}

/* AI / Bot: icy blue, rounded except bottom-left tail */
.wpai-message--bot .wpai-message-bubble {
    background: var(--wpai-widget-ice);
    color: var(--wpai-widget-text);
    border-radius: 16px 16px 16px 4px;
}

/* Markdown content inside bot messages */
.wpai-message-bubble p  { margin: 0 0 8px; }
.wpai-message-bubble p:last-child { margin-bottom: 0; }
.wpai-message-bubble ul,
.wpai-message-bubble ol { margin: 6px 0; padding-left: 18px; }
.wpai-message-bubble li { margin-bottom: 4px; }
.wpai-message-bubble code {
    background: rgba(0, 102, 255, 0.08);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 13px;
}
.wpai-message-bubble pre code {
    background: transparent;
    padding: 0;
}
.wpai-message-bubble pre {
    background: rgba(0, 0, 0, 0.04);
    border-radius: 8px;
    padding: 10px 12px;
    overflow-x: auto;
    font-size: 13px;
}

/* ════════════════════════════════════════════════════════
   Typing Indicator
════════════════════════════════════════════════════════ */
.wpai-typing {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 16px 8px;
    flex-shrink: 0;
}

.wpai-typing-dots {
    display: flex;
    gap: 4px;
    padding: 10px 14px;
    background: var(--wpai-widget-ice);
    border-radius: 16px 16px 16px 4px;
}

.wpai-typing-dots span {
    width: 7px;
    height: 7px;
    background: var(--wpai-widget-primary);
    border-radius: 50%;
    animation: wpaiTyping 1.2s ease-in-out infinite;
    box-shadow: 0 0 6px rgba(0, 102, 255, 0.45);
}

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

@keyframes wpaiTyping {
    0%, 60%, 100% { transform: translateY(0);    opacity: 0.35; box-shadow: 0 0 4px rgba(0, 102, 255, 0.2); }
    30%            { transform: translateY(-5px); opacity: 1;    box-shadow: 0 0 10px rgba(0, 102, 255, 0.6); }
}

/* ── Dynamic Thinking Messages (Pro) ── */
.wpai-thinking-text {
    display: block;
    font-size: 11px;
    font-style: italic;
    color: var(--wpai-widget-muted);
    opacity: 0;
    margin-top: 4px;
    padding-left: 2px;
    transition: opacity 0.4s ease;
    pointer-events: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 220px;
}

.wpai-thinking-text.wpai-thinking-visible {
    opacity: 0.75;
}

/* ════════════════════════════════════════════════════════
   Suggested Questions
════════════════════════════════════════════════════════ */
.wpai-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 14px 10px;
    background: #ffffff;
    border-top: 1px solid var(--wpai-widget-border);
    flex-shrink: 0;
}

.wpai-suggestion-pill {
    padding: 6px 14px;
    background: var(--wpai-widget-ice);
    border: none;
    border-radius: 999px;
    font-size: 13px;
    font-family: inherit;
    color: var(--wpai-widget-primary);
    cursor: pointer;
    line-height: 1.4;
    font-weight: 500;
    transition: background 0.18s ease, box-shadow 0.18s ease;
    white-space: nowrap;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
}

.wpai-suggestion-pill:hover {
    background: var(--wpai-widget-primary-light, #dbeafe);
    box-shadow: 0 0 0 1px var(--wpai-widget-primary);
}

.wpai-suggestion-pill:focus-visible {
    outline: 2px solid var(--wpai-widget-primary);
    outline-offset: 2px;
}

/* ════════════════════════════════════════════════════════
   Chat Input Area
════════════════════════════════════════════════════════ */
.wpai-input-area {
    position: relative;
    padding: 12px 14px;
    border-top: 1px solid var(--wpai-widget-border);
    background: #ffffff;
    flex-shrink: 0;
}

.wpai-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    background: var(--wpai-widget-surface);
    border: none;
    border-radius: 10px;
    /* Right padding reserves space for the absolutely-positioned send button */
    padding: 10px 56px 10px 13px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    overflow-y: auto;
    max-height: 100px;
    line-height: 1.5;
    color: var(--wpai-widget-text);
    box-shadow: 0 0 0 1px var(--wpai-widget-border) inset;
    transition: box-shadow 0.2s ease, background 0.2s ease;
}

.wpai-input::placeholder {
    color: var(--wpai-widget-muted);
    opacity: 0.7;
}

.wpai-input:focus {
    outline: none;
    background: #ffffff;
    box-shadow: 0 0 0 2px var(--wpai-widget-primary) inset;
}

.wpai-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Send button — absolutely positioned inside .wpai-input-area so it sits
   flush with the right edge of the textarea without displacing it. */
.wpai-send-btn {
    position: absolute;
    right: 10px;
    bottom: 10px;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: var(--wpai-widget-primary);
    border: none;
    cursor: pointer;
    color: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 3px 10px rgba(0, 102, 255, 0.3);
    transition: background 0.2s ease, transform 0.15s ease, box-shadow 0.2s ease;
}

.wpai-send-btn svg {
    color: #ffffff;
    fill: #ffffff;
    stroke: #ffffff;
}

.wpai-send-btn:hover {
    background: var(--wpai-widget-primary-dark);
    transform: scale(1.06);
    box-shadow: 0 5px 16px rgba(0, 102, 255, 0.42);
}

.wpai-send-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* ════════════════════════════════════════════════════════
   Chat Disclaimer & Powered-By Footer
════════════════════════════════════════════════════════ */
.wpai-chat-disclaimer {
    font-size: 11px;
    color: var(--wpai-widget-muted);
    text-align: center;
    padding: 0 14px 10px;
    margin: 0;
    line-height: 1.5;
    flex-shrink: 0;
}

.wpai-chat-disclaimer a {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.wpai-chat-disclaimer a:hover {
    color: var(--wpai-widget-text);
}

.wpai-chat-powered-by {
    font-size: 10px;
    color: #CBD5E1;
    text-align: center;
    padding: 0 14px 8px;
    margin: 0;
    line-height: 1.4;
    flex-shrink: 0;
}

.wpai-chat-powered-by a {
    color: inherit;
    text-decoration: none;
}

.wpai-chat-powered-by a:hover {
    color: var(--wpai-widget-muted);
    text-decoration: underline;
    text-underline-offset: 2px;
}

/* ════════════════════════════════════════════════════════
   Mobile Backdrop Overlay
   Always in the DOM; made visible by JS adding
   .wpai-overlay-active on screens ≤ 768 px.
════════════════════════════════════════════════════════ */
.wpai-mobile-overlay {
    display: none; /* hidden on desktop, JS class activates on mobile */
    position: fixed;
    inset: 0;
    z-index: 99998; /* one below the widget (99999) */
    background: rgba(0, 0, 0, 0.28);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    transition: opacity 0.28s ease;
}

@media (max-width: 768px) {
    .wpai-mobile-overlay.wpai-overlay-active {
        display: block;
    }
}

/* ════════════════════════════════════════════════════════
   Mobile Behaviour — Bottom Sheet (≤ 768 px)
════════════════════════════════════════════════════════ */

/* Slide-up animation for the mobile bottom-sheet */
@keyframes wpaiMobileSlideUp {
    from {
        opacity: 0;
        transform: translateY(100%);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Hide widget entirely on mobile when admin chose "hide" */
@media (max-width: 768px) {
    .wpai-mobile-hide {
        display: none !important;
    }
}

/* Bottom-sheet layout for mobile */
@media (max-width: 768px) {
    /* Reposition the widget anchor so the launcher floats above the sheet */
    .wpai-mobile-show {
        bottom: 20px;
        right: 16px;
    }

    /* Bottom-sheet chat window */
    .wpai-mobile-show .wpai-chat-window {
        position: fixed !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        top: auto !important;
        width: 100% !important;
        max-width: 100vw !important;
        /* 70dvh — Dynamic Viewport Height shrinks automatically when the
           virtual keyboard opens, keeping the sheet inside the visible area */
        height: 70dvh !important;
        max-height: 70dvh !important;
        /* Modern bottom-sheet corners — flat at the bottom, rounded at top */
        border-radius: 24px 24px 0 0 !important;
        box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.18) !important;
        /* Slide up from the bottom instead of fading in */
        animation: wpaiMobileSlideUp 0.32s cubic-bezier(0.34, 1.1, 0.64, 1) !important;
    }

    /* Larger close button for fingertip tapping */
    .wpai-mobile-show .wpai-header-close-btn {
        width: 44px;
        height: 44px;
    }

    .wpai-mobile-show .wpai-header-close-btn svg {
        width: 26px;
        height: 26px;
    }

    /* Input area — extra bottom padding for iPhone home-bar safe area */
    .wpai-mobile-show .wpai-input-area {
        padding: 10px 12px;
        padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
    }

    .wpai-mobile-show .wpai-input {
        /* Right padding must clear the larger 44px mobile send button */
        padding-right: 60px;
        font-size: 16px; /* prevents iOS auto-zoom on focus */
    }

    /* Larger tap target on mobile; positioned above the safe-area inset */
    .wpai-mobile-show .wpai-send-btn {
        width: 44px;
        height: 44px;
        right: 12px;
        bottom: max(10px, env(safe-area-inset-bottom, 10px));
    }
}

/* ── Session Restore Notice ───────────────────────────────────────────────── */
/* Displayed in the message list when a previous conversation is resumed.     */
.wpai-session-notice {
    display: block;
    text-align: center;
    font-size: 11px;
    color: #94a3b8;
    margin: 6px 0 10px;
    padding: 0 12px;
    letter-spacing: 0.01em;
    pointer-events: none;
    user-select: none;
}
