/**
 * KnowYa — Frontend Widget Styles
 *
 * Uses CSS custom properties (--knowya-*) injected by PHP
 * so admin colour choices apply automatically.
 *
 * @package KnowYa
 */

/* ------------------------------------------------------------------ */
/*  CSS Custom Properties (defaults — overridden by inline style)      */
/* ------------------------------------------------------------------ */

:root {
    --knowya-primary: #2563eb;
    --knowya-primary-hover: #1d4ed8;
    --knowya-primary-text: #ffffff;
    --knowya-bg: #ffffff;
    --knowya-bg-secondary: #f8f9fa;
    --knowya-text: #1f2937;
    --knowya-text-muted: #6b7280;
    --knowya-border: #e5e7eb;
    --knowya-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
    --knowya-radius: 16px;
    --knowya-radius-sm: 12px;
    --knowya-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
    --knowya-font-size: 14px;
    --knowya-z-index: 99999;
    --knowya-widget-width: 380px;
    --knowya-widget-height: 560px;
    --knowya-toggle-size: 56px;
}

/* ------------------------------------------------------------------ */
/*  Reset — scope everything under #knowya-chat-widget                   */
/* ------------------------------------------------------------------ */

#knowya-chat-widget,
#knowya-chat-widget *,
#knowya-chat-widget *::before,
#knowya-chat-widget *::after {
    box-sizing: border-box;
    font-family: var(--knowya-font);
}

/* Reset browser/theme default margins on common elements so they don't
   bleed into widget layout. Use element+class selectors so these can be
   overridden by the widget's own class rules below. */
#knowya-chat-widget p,
#knowya-chat-widget ul,
#knowya-chat-widget ol,
#knowya-chat-widget li,
#knowya-chat-widget h1, #knowya-chat-widget h2, #knowya-chat-widget h3,
#knowya-chat-widget h4, #knowya-chat-widget h5, #knowya-chat-widget h6,
#knowya-chat-widget blockquote, #knowya-chat-widget figure, #knowya-chat-widget fieldset {
    margin: 0;
    padding: 0;
}

/* ------------------------------------------------------------------ */
/*  Widget container                                                    */
/* ------------------------------------------------------------------ */

#knowya-chat-widget {
    position: fixed;
    z-index: var(--knowya-z-index);
    font-size: var(--knowya-font-size);
    line-height: 1.5;
    color: var(--knowya-text);
}

/* Positions */
.knowya-position-bottom-right {
    bottom: 20px;
    right: 20px;
}

.knowya-position-bottom-left {
    bottom: 20px;
    left: 20px;
}

/* Sizes */
.knowya-size-compact {
    --knowya-widget-width: 340px;
    --knowya-widget-height: 480px;
}

.knowya-size-large {
    --knowya-widget-width: 420px;
    --knowya-widget-height: 620px;
}

/* ------------------------------------------------------------------ */
/*  Toggle button (chat bubble)                                         */
/* ------------------------------------------------------------------ */

.knowya-chat-toggle {
    width: var(--knowya-toggle-size);
    height: var(--knowya-toggle-size);
    border-radius: 50%;
    background: var(--knowya-primary);
    color: var(--knowya-primary-text);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.2);
    transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease;
    position: relative;
}

.knowya-chat-toggle:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
    background: var(--knowya-primary-hover);
}

.knowya-chat-toggle:active {
    transform: scale(0.96);
}

.knowya-toggle-icon {
    display: flex;
    align-items: center;
    justify-content: center;
}

.knowya-unread-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #ef4444;
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
}

/* ------------------------------------------------------------------ */
/*  Chat window                                                         */
/* ------------------------------------------------------------------ */

.knowya-chat-window {
    position: absolute;
    bottom: calc(var(--knowya-toggle-size) + 16px);
    right: 0;
    width: var(--knowya-widget-width);
    height: var(--knowya-widget-height);
    max-height: calc(100vh - 120px);
    background: var(--knowya-bg);
    border-radius: var(--knowya-radius);
    box-shadow: var(--knowya-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px) scale(0.95);
    transition: opacity 0.25s ease, transform 0.25s ease, visibility 0.25s;
    border: 1px solid var(--knowya-border);
}

.knowya-position-bottom-left .knowya-chat-window {
    right: auto;
    left: 0;
}

.knowya-chat-window.knowya-open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* ------------------------------------------------------------------ */
/*  Header                                                              */
/* ------------------------------------------------------------------ */

.knowya-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: var(--knowya-primary);
    color: var(--knowya-primary-text);
    flex-shrink: 0;
}

.knowya-chat-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

.knowya-chat-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.knowya-chat-header-text {
    display: flex;
    flex-direction: column;
}

.knowya-chat-bot-name {
    font-weight: 600;
    font-size: 15px;
    line-height: 1.2;
}

.knowya-chat-status {
    font-size: 12px;
    opacity: 0.85;
}

.knowya-chat-header-actions {
    display: flex;
    gap: 4px;
    align-items: center;
}

/* Visual separator between "new chat" and the minimize/close group */
.knowya-btn-new-chat {
    margin-right: 4px;
}

.knowya-btn-new-chat::after {
    content: '';
    display: inline-block;
    width: 1px;
    height: 16px;
    background: rgba(255,255,255,0.35);
    position: absolute;
    right: -6px;
    top: 50%;
    transform: translateY(-50%);
}

.knowya-btn-new-chat {
    position: relative;
}

.knowya-chat-header-actions button {
    width: 32px;
    height: 32px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.15);
    color: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
}

.knowya-chat-header-actions button:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* ------------------------------------------------------------------ */
/*  Messages area                                                       */
/* ------------------------------------------------------------------ */

.knowya-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--knowya-bg-secondary);
    scroll-behavior: smooth;
}

/* Scrollbar */
.knowya-chat-messages::-webkit-scrollbar {
    width: 5px;
}

.knowya-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.knowya-chat-messages::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
}

/* ------------------------------------------------------------------ */
/*  Message bubbles                                                     */
/* ------------------------------------------------------------------ */

.knowya-message {
    display: flex;
    /* Column, so anything appended after the bubble (product cards, source
       links) stacks BENEATH the answer instead of becoming a second column
       beside it and squeezing the text into a narrow strip. */
    flex-direction: column;
    max-width: 85%;
}

.knowya-message-user {
    align-self: flex-end;
    margin-left: auto;
    align-items: flex-end;
}

.knowya-message-assistant {
    align-self: flex-start;
    align-items: flex-start;
}

.knowya-message-bubble {
    padding: 12px 16px;
    border-radius: var(--knowya-radius-sm);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.knowya-message-user .knowya-message-bubble {
    background: var(--knowya-primary);
    color: var(--knowya-primary-text);
    border-bottom-right-radius: 4px;
}

.knowya-message-assistant .knowya-message-bubble {
    background: var(--knowya-bg);
    color: var(--knowya-text);
    border: 1px solid var(--knowya-border);
    border-bottom-left-radius: 4px;
}

.knowya-message-content {
    font-size: var(--knowya-font-size);
    line-height: 1.55;
}

.knowya-message-content p {
    margin: 0 0 8px;
}

.knowya-message-content p:last-child {
    margin-bottom: 0;
}

.knowya-message-content strong {
    font-weight: 600;
}

.knowya-message-content code {
    background: rgba(0, 0, 0, 0.06);
    padding: 2px 5px;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: "SFMono-Regular", Consolas, monospace;
}

.knowya-message-content ul,
.knowya-message-content ol {
    margin: 6px 0;
    padding-left: 20px;
}

.knowya-message-content li {
    margin-bottom: 2px;
}

.knowya-message-content a {
    color: var(--knowya-primary);
    text-decoration: underline;
}

.knowya-message-user .knowya-message-content a {
    color: var(--knowya-primary-text);
}

/* Message animation */
.knowya-animate-in {
    animation: knowyaSlideIn 0.25s ease-out;
}

@keyframes knowyaSlideIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Streaming cursor */
.knowya-streaming .knowya-message-content::after {
    content: '\2588';
    animation: knowyaBlink 0.7s infinite;
    color: var(--knowya-text-muted);
    margin-left: 1px;
}

@keyframes knowyaBlink {
    0%, 50% { opacity: 1; }
    51%, 100% { opacity: 0; }
}

/* ------------------------------------------------------------------ */
/*  Source attribution — hidden (sources not shown in chat UI)         */
/* ------------------------------------------------------------------ */

.knowya-message-sources {
    display: none;
}

/* ------------------------------------------------------------------ */
/*  Typing indicator                                                    */
/* ------------------------------------------------------------------ */

.knowya-typing-indicator .knowya-message-bubble {
    padding: 14px 18px;
}

.knowya-dots {
    display: flex;
    gap: 5px;
    align-items: center;
}

.knowya-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--knowya-text-muted);
    animation: knowyaDotPulse 1.4s infinite ease-in-out both;
}

.knowya-dots span:nth-child(1) { animation-delay: -0.32s; }
.knowya-dots span:nth-child(2) { animation-delay: -0.16s; }
.knowya-dots span:nth-child(3) { animation-delay: 0s; }

@keyframes knowyaDotPulse {
    0%, 80%, 100% {
        transform: scale(0.6);
        opacity: 0.4;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ------------------------------------------------------------------ */
/*  Suggestions                                                         */
/* ------------------------------------------------------------------ */

.knowya-chat-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 16px;
    background: var(--knowya-bg-secondary);
    border-top: 1px solid var(--knowya-border);
}

.knowya-suggestion-chip {
    padding: 6px 12px;
    border-radius: 16px;
    border: 1px solid var(--knowya-primary);
    background: transparent;
    color: var(--knowya-primary);
    font-size: 12px;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    white-space: nowrap;
}

.knowya-suggestion-chip:hover {
    background: var(--knowya-primary);
    color: var(--knowya-primary-text);
}

/* ------------------------------------------------------------------ */
/*  Input area                                                          */
/* ------------------------------------------------------------------ */

.knowya-chat-input-area {
    flex-shrink: 0;
    border-top: 1px solid var(--knowya-border);
    background: var(--knowya-bg);
}

.knowya-chat-form {
    padding: 12px 12px 0;
}

.knowya-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--knowya-bg-secondary);
    border: 1px solid var(--knowya-border);
    border-radius: 12px;
    padding: 6px 6px 6px 14px;
    transition: border-color 0.15s;
}

.knowya-input-wrapper:focus-within {
    border-color: var(--knowya-primary);
}

.knowya-chat-input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    resize: none;
    font-size: var(--knowya-font-size);
    line-height: 1.4;
    color: var(--knowya-text);
    padding: 6px 0;
    min-height: 24px;
    max-height: 120px;
    font-family: var(--knowya-font);
}

.knowya-chat-input::placeholder {
    color: var(--knowya-text-muted);
}

.knowya-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: none;
    background: var(--knowya-primary);
    color: var(--knowya-primary-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, opacity 0.15s;
}

.knowya-send-btn:hover:not(:disabled) {
    background: var(--knowya-primary-hover);
}

.knowya-send-btn:disabled {
    opacity: 0.4;
    cursor: default;
}

/* ------------------------------------------------------------------ */
/*  Footer / powered by                                                 */
/* ------------------------------------------------------------------ */

.knowya-chat-footer {
    text-align: center;
    padding: 6px 12px 10px;
}

.knowya-powered-by {
    font-size: 11px;
    color: var(--knowya-text-muted);
}

.knowya-powered-by a {
    color: var(--knowya-text-muted);
    text-decoration: none;
}

.knowya-powered-by a:hover {
    color: var(--knowya-primary);
}

/* ------------------------------------------------------------------ */
/*  Mobile responsive                                                   */
/* ------------------------------------------------------------------ */

@media (max-width: 480px) {
    #knowya-chat-widget {
        bottom: 12px;
        right: 12px;
    }

    .knowya-position-bottom-left#knowya-chat-widget {
        left: 12px;
        right: auto;
    }

    .knowya-chat-window {
        position: fixed;
        bottom: 0;
        right: 0;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
    }

    .knowya-position-bottom-left .knowya-chat-window {
        left: 0;
        right: 0;
    }

    .knowya-chat-toggle {
        width: 50px;
        height: 50px;
    }

    .knowya-chat-header {
        padding: 16px;
        border-radius: 0;
    }

    .knowya-chat-messages {
        padding: 12px;
    }

    .knowya-message {
        max-width: 90%;
    }
}

/* ------------------------------------------------------------------ */
/*  Reduced motion preference                                           */
/* ------------------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
    .knowya-chat-window {
        transition: none;
    }

    .knowya-animate-in {
        animation: none;
    }

    .knowya-streaming .knowya-message-content::after {
        animation: none;
    }

    .knowya-dots span {
        animation: none;
        opacity: 0.6;
    }

    .knowya-chat-toggle {
        transition: none;
    }
}

/* ------------------------------------------------------------------ */
/*  Print — hide the widget                                             */
/* ------------------------------------------------------------------ */

@media print {
    #knowya-chat-widget {
        display: none !important;
    }
}

/* ------------------------------------------------------------------ */
/*  Footer quick actions                                               */
/* ------------------------------------------------------------------ */

.knowya-chat-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.knowya-footer-actions {
    display: flex;
    gap: 10px;
}

.knowya-footer-action {
    background: none;
    border: none;
    padding: 0;
    font-size: 11px;
    color: var(--knowya-primary, #2563eb);
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.knowya-footer-action:hover {
    color: var(--knowya-primary-hover, #1d4ed8);
}

/* ------------------------------------------------------------------ */
/*  Lead capture / order lookup cards                                  */
/* ------------------------------------------------------------------ */

.knowya-lead-card {
    margin: 8px 12px;
    padding: 12px;
    border: 1px solid rgba(0, 0, 0, 0.08);
    border-radius: 12px;
    background: var(--knowya-bg-secondary, #f8f8f8);
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.knowya-lead-heading {
    font-size: 13px;
    font-weight: 600;
}

.knowya-lead-card input,
.knowya-lead-card textarea {
    width: 100%;
    box-sizing: border-box;
    padding: 8px 10px;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    resize: none;
}

.knowya-lead-card input:focus,
.knowya-lead-card textarea:focus {
    outline: none;
    border-color: var(--knowya-primary, #2563eb);
}

.knowya-lead-card .knowya-input-error {
    border-color: #dc2626;
}

.knowya-lead-actions {
    display: flex;
    gap: 8px;
}

.knowya-lead-submit {
    background: var(--knowya-primary, #2563eb);
    color: var(--knowya-primary-text, #ffffff);
    border: none;
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 13px;
    cursor: pointer;
}

.knowya-lead-submit:hover { background: var(--knowya-primary-hover, #1d4ed8); }
.knowya-lead-submit:disabled { opacity: 0.6; cursor: default; }

.knowya-lead-dismiss {
    background: none;
    border: none;
    color: rgba(0, 0, 0, 0.68);
    font-size: 13px;
    cursor: pointer;
}

/* ------------------------------------------------------------------ */
/*  Sources & product cards                                            */
/* ------------------------------------------------------------------ */

.knowya-sources {
    /* No horizontal inset — these are siblings of the bubble now, so they
       should line up with its left edge. */
    margin: 4px 0 0;
    font-size: 11px;
    color: rgba(0, 0, 0, 0.68);
}

.knowya-sources a {
    color: var(--knowya-primary, #2563eb);
    text-decoration: none;
}

.knowya-sources a:hover { text-decoration: underline; }

/* ------------------------------------------------------------------ */
/*  Accessibility                                                      */
/* ------------------------------------------------------------------ */

/* Visually hidden but available to screen readers (WP core convention). */
.knowya-sr-only {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Visible keyboard focus on every interactive element in the widget.
   :focus-visible keeps the ring off mouse users while guaranteeing
   keyboard users can always see where they are (WCAG 2.4.7). */
#knowya-chat-widget button:focus-visible,
#knowya-chat-widget a:focus-visible,
#knowya-chat-widget input:focus-visible,
#knowya-chat-widget textarea:focus-visible {
    outline: 2px solid var(--knowya-primary, #2563eb);
    outline-offset: 2px;
    border-radius: 4px;
}

/* The toggle sits on the primary colour, so it needs a contrasting ring. */
#knowya-chat-toggle:focus-visible {
    outline: 2px solid #ffffff;
    outline-offset: -4px;
    box-shadow: 0 0 0 4px var(--knowya-primary, #2563eb);
}

/* Inline validation message — paired with role="alert" in the markup so the
   error is announced, not conveyed by the red border alone. */
.knowya-lead-error {
    margin: 0;
    font-size: 12px;
    color: #b91c1c;
    min-height: 0;
}

.knowya-lead-error:empty {
    display: none;
}
