/* =============================================
   Intellichat — Premium Frontend Widget
   ============================================= */

/* CSS Custom Properties (set dynamically via PHP inline style) */
:host {
    --ic-primary: #6C5CE7;
    --ic-primary-rgb: 108, 92, 231;
    --ic-primary-dark: #5A4BD1;
    --ic-primary-light: rgba(var(--ic-primary-rgb), 0.12);
    --ic-radius: 16px;
    --ic-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    --ic-font: 'Segoe UI', system-ui, -apple-system, sans-serif;

    /* Theme Variables (Light by default) */
    --ic-bg-widget: #ffffff;
    --ic-bg-body: #f8f9fb;
    --ic-bg-input: #ffffff;
    --ic-border: #e8eaed;
    --ic-text-main: #2d3436;
    --ic-text-muted: #636e72;
    --ic-msg-bot-bg: #ffffff;
    --ic-msg-user-text: #ffffff;
    --ic-scrollbar-thumb: #d1d5db;
    --ic-close-btn-bg: rgba(255, 255, 255, 0.15);
    --ic-close-btn-hover: rgba(255, 255, 255, 0.3);
    --ic-typing-bg: #ffffff;
    --ic-typing-dot: #b2bec3;
    --ic-code-bg: #f0f0f3;
    --ic-code-text: #e74c3c;
    --ic-pre-bg: #2d3436;
    --ic-pre-text: #dfe6e9;
}

/* Dark Mode Overrides */
:host(.ic-theme-dark),
.ic-theme-dark {
    --ic-bg-widget: #1e1e1e;
    --ic-bg-body: #121212;
    --ic-bg-input: #1e1e1e;
    --ic-border: #333333;
    --ic-text-main: #e0e0e0;
    --ic-text-muted: #a0a0a0;
    --ic-msg-bot-bg: #262626;
    --ic-scrollbar-thumb: #555555;
    --ic-close-btn-bg: rgba(255, 255, 255, 0.1);
    --ic-close-btn-hover: rgba(255, 255, 255, 0.2);
    --ic-typing-bg: #262626;
    --ic-typing-dot: #888888;
    --ic-code-bg: #333;
    --ic-code-text: #ff7675;
    --ic-pre-bg: #000000;
    --ic-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
}

@media (prefers-color-scheme: dark) {

    :host(.ic-theme-auto),
    .ic-theme-auto {
        --ic-bg-widget: #1e1e1e;
        --ic-bg-body: #121212;
        --ic-bg-input: #1e1e1e;
        --ic-border: #333333;
        --ic-text-main: #e0e0e0;
        --ic-text-muted: #a0a0a0;
        --ic-msg-bot-bg: #262626;
        --ic-scrollbar-thumb: #555555;
        --ic-close-btn-bg: rgba(255, 255, 255, 0.1);
        --ic-close-btn-hover: rgba(255, 255, 255, 0.2);
        --ic-typing-bg: #262626;
        --ic-typing-dot: #888888;
        --ic-code-bg: #333;
        --ic-code-text: #ff7675;
        --ic-pre-bg: #000000;
        --ic-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
    }
}

/* ── Toggle Button ───────────────────────────── */
#intellichat-widget-toggle {
    position: fixed;
    bottom: 24px;
    z-index: 999999;
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 50%;
    background: var(--ic-primary);
    color: #fff;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(var(--ic-primary-rgb), 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1),
        box-shadow 0.3s ease;
    outline: none;
}

#intellichat-widget-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 8px 28px rgba(var(--ic-primary-rgb), 0.55);
}

#intellichat-widget-toggle:active {
    transform: scale(0.95);
}

#intellichat-widget-toggle svg {
    width: 26px;
    height: 26px;
    fill: currentColor;
    transition: transform 0.3s ease, opacity 0.2s ease;
}

#intellichat-widget-toggle .ic-icon-close {
    display: none;
}

#intellichat-widget-toggle.ic-active .ic-icon-default {
    display: none;
}

#intellichat-widget-toggle.ic-active .ic-icon-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
}

#intellichat-widget-toggle.ic-active svg {
    transform: scale(1.1);
}

/* Pulse animation on load */
@keyframes ic-pulse {

    0%,
    100% {
        box-shadow: 0 6px 20px rgba(var(--ic-primary-rgb), 0.45);
    }

    50% {
        box-shadow: 0 6px 20px rgba(var(--ic-primary-rgb), 0.45), 0 0 0 12px rgba(var(--ic-primary-rgb), 0.15);
    }
}

#intellichat-widget-toggle:not(.ic-active) {
    animation: ic-pulse 2.5s ease-in-out infinite;
}

#intellichat-widget-toggle.ic-active {
    animation: none;
}

/* Position modifiers */
#intellichat-widget-toggle.ic-pos-right {
    right: 24px;
}

#intellichat-widget-toggle.ic-pos-left {
    left: 24px;
}

/* ── Chat Window ─────────────────────────────── */
#intellichat-widget {
    position: fixed;
    bottom: 96px;
    z-index: 999999;
    width: 380px;
    height: min(75vh, 600px);
    background: var(--ic-bg-widget);
    color: var(--ic-text-main);
    border-radius: var(--ic-radius);
    box-shadow: var(--ic-shadow);
    display: none;
    flex-direction: column;
    font-family: var(--ic-font);
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(0.96);
    transition: opacity 0.35s cubic-bezier(0.22, 1, 0.36, 1),
        transform 0.35s cubic-bezier(0.22, 1, 0.36, 1), background 0.3s ease, color 0.3s ease;
}

#intellichat-widget.ic-open {
    display: flex;
    opacity: 1;
    transform: translateY(0) scale(1);
}

/* Position modifiers */
#intellichat-widget.ic-pos-right {
    right: 24px;
}

#intellichat-widget.ic-pos-left {
    left: 24px;
}

/* ── Header ──────────────────────────────────── */
#intellichat-widget-header {
    background: var(--ic-primary);
    color: #fff;
    padding: 16px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    position: relative;
    overflow: hidden;
}

#intellichat-widget-header::after {
    content: '';
    position: absolute;
    top: -30%;
    right: -20%;
    width: 120px;
    height: 120px;
    background: rgba(255, 255, 255, 0.06);
    border-radius: 50%;
    pointer-events: none;
}

.ic-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
}

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

.ic-header-avatar svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

.ic-header-text h4 {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.2px;
}

.ic-header-text span {
    font-size: 11px;
    opacity: 0.8;
    display: flex;
    align-items: center;
    gap: 4px;
}

.ic-status-dot {
    width: 6px;
    height: 6px;
    background: #55efc4;
    border-radius: 50%;
    display: inline-block;
    animation: ic-blink 1.8s ease-in-out infinite;
}

@keyframes ic-blink {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.4;
    }
}

#intellichat-close {
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    cursor: pointer;
    background: var(--ic-close-btn-bg);
    transition: background 0.2s ease;
    font-size: 16px;
    line-height: 1;
}

#intellichat-close:hover {
    background: var(--ic-close-btn-hover);
}

/* ── Chat Body ───────────────────────────────── */
#intellichat-widget-body {
    flex: 1;
    min-height: 0;
    padding: 16px;
    overflow-y: auto;
    background: var(--ic-bg-body);
    display: flex;
    flex-direction: column;
    gap: 10px;
    scroll-behavior: smooth;
    transition: background 0.3s ease;
}

/* Custom scrollbar */
#intellichat-widget-body::-webkit-scrollbar {
    width: 5px;
}

#intellichat-widget-body::-webkit-scrollbar-track {
    background: transparent;
}

#intellichat-widget-body::-webkit-scrollbar-thumb {
    background: var(--ic-scrollbar-thumb);
    border-radius: 10px;
}

/* ── Preset Questions ───────────────────────── */
.intellichat-preset-questions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 2px;
}

.intellichat-preset-btn {
    border: 1px solid var(--ic-border);
    background: var(--ic-bg-widget);
    color: var(--ic-text-main);
    border-radius: 999px;
    padding: 8px 12px;
    font-size: 12px;
    line-height: 1.35;
    cursor: pointer;
    transition: border-color 0.2s ease, transform 0.15s ease, background 0.2s ease;
}

.intellichat-preset-btn:hover {
    border-color: var(--ic-primary);
    background: var(--ic-primary-light);
    transform: translateY(-1px);
}

/* ── Messages ────────────────────────────────── */
.intellichat-msg {
    padding: 10px 14px;
    border-radius: 14px;
    max-width: 85%;
    word-wrap: break-word;
    font-size: 13.5px;
    line-height: 1.55;
    animation: ic-msgIn 0.3s cubic-bezier(0.22, 1, 0.36, 1) both;
    position: relative;
}

@keyframes ic-msgIn {
    from {
        opacity: 0;
        transform: translateY(8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Bot message */
.intellichat-msg.bot {
    background: var(--ic-msg-bot-bg);
    color: var(--ic-text-main);
    align-self: flex-start;
    border: 1px solid var(--ic-border);
    border-bottom-left-radius: 4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.intellichat-msg.bot.ic-has-feedback {
    padding-bottom: 40px;
}

/* User message */
.intellichat-msg.user {
    background: var(--ic-primary);
    color: var(--ic-msg-user-text);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
    box-shadow: 0 2px 8px rgba(var(--ic-primary-rgb), 0.25);
}

/* ── Markdown Formatting inside bot messages ── */
.intellichat-msg.bot h1,
.intellichat-msg.bot h2,
.intellichat-msg.bot h3,
.intellichat-msg.bot h4 {
    margin: 8px 0 4px 0;
    font-size: 13.5px;
    font-weight: 700;
    color: var(--ic-text-main);
    line-height: 1.4;
}

.intellichat-msg.bot h2 {
    font-size: 14px;
}

.intellichat-msg.bot h3 {
    font-size: 13.5px;
}

.intellichat-msg.bot p {
    margin: 0 0 8px 0;
}

.intellichat-msg.bot p:last-child {
    margin-bottom: 0;
}

.intellichat-msg.bot ul,
.intellichat-msg.bot ol {
    margin: 4px 0 8px 0;
    padding-left: 18px;
}

.intellichat-msg.bot li {
    margin-bottom: 3px;
}

.intellichat-msg.bot strong,
.intellichat-msg.bot b {
    font-weight: 600;
    color: var(--ic-text-main);
}

.intellichat-msg.bot em,
.intellichat-msg.bot i {
    font-style: italic;
}

.intellichat-msg.bot code {
    background: var(--ic-code-bg);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 12px;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    color: var(--ic-code-text);
}

.intellichat-msg.bot pre {
    background: var(--ic-pre-bg);
    color: var(--ic-pre-text);
    padding: 10px 12px;
    border-radius: 8px;
    overflow-x: auto;
    font-size: 12px;
    margin: 6px 0;
    font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
    line-height: 1.5;
}

.intellichat-msg.bot pre code {
    background: none;
    color: inherit;
    padding: 0;
    font-size: inherit;
}

.intellichat-msg.bot a {
    color: var(--ic-primary);
    text-decoration: none;
    font-weight: 500;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s ease;
}

.intellichat-msg.bot a:hover {
    border-bottom-color: var(--ic-primary);
}

.intellichat-msg.bot blockquote {
    border-left: 3px solid var(--ic-primary);
    margin: 6px 0;
    padding: 4px 10px;
    background: var(--ic-primary-light);
    border-radius: 0 6px 6px 0;
    font-size: 13px;
}

.intellichat-msg.bot hr {
    border: none;
    border-top: 1px solid var(--ic-border);
    margin: 8px 0;
}

/* ── Feedback Actions ────────────────────────── */
.intellichat-feedback-btns {
    position: absolute;
    right: 10px;
    bottom: 10px;
    display: inline-flex;
    align-items: center;
    padding: 4px;
    border: 1px solid rgba(var(--ic-primary-rgb), 0.12);
    border-radius: 999px;
    background: rgba(var(--ic-primary-rgb), 0.06);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
    backdrop-filter: blur(10px);
    opacity: 0;
    transform: translateY(4px);
    pointer-events: none;
    transition: opacity 0.18s ease, transform 0.18s ease, border-color 0.2s ease;
}

.intellichat-msg.bot:hover .intellichat-feedback-btns,
.intellichat-msg.bot:focus-within .intellichat-feedback-btns {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.ic-fb-btn {
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--ic-text-muted);
    cursor: pointer;
    transition: background 0.18s ease, color 0.18s ease, transform 0.18s ease;
}

.ic-fb-btn:hover,
.ic-fb-btn:focus-visible {
    background: var(--ic-primary-light);
    color: var(--ic-primary);
    outline: none;
    transform: translateY(-1px);
}

.ic-fb-btn svg {
    width: 15px;
    height: 15px;
    display: block;
}

.intellichat-feedback-btns.is-submitted {
    padding: 6px 10px;
    border-color: rgba(var(--ic-primary-rgb), 0.24);
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.ic-fb-thanks {
    display: inline-block;
    font-size: 11px;
    line-height: 1.2;
    color: var(--ic-text-muted);
    white-space: nowrap;
}

/* Source links styling */
.intellichat-msg.bot .ic-sources {
    margin-top: 10px;
    padding-top: 8px;
    border-top: 1px solid var(--ic-border);
    font-size: 12px;
    color: var(--ic-text-muted);
}

/* ── Typing Indicator ────────────────────────── */
.intellichat-typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    background: var(--ic-typing-bg);
    border: 1px solid var(--ic-border);
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
    animation: ic-msgIn 0.3s cubic-bezier(0.22, 1, 0.36, 1) both;
}

.intellichat-typing span {
    width: 7px;
    height: 7px;
    background: var(--ic-typing-dot);
    border-radius: 50%;
    animation: ic-typing 1.4s ease-in-out infinite;
}

.intellichat-typing span:nth-child(2) {
    animation-delay: 0.15s;
}

.intellichat-typing span:nth-child(3) {
    animation-delay: 0.3s;
}

@keyframes ic-typing {

    0%,
    60%,
    100% {
        transform: translateY(0);
        opacity: 0.4;
    }

    30% {
        transform: translateY(-6px);
        opacity: 1;
    }
}

/* ── Input Area ──────────────────────────────── */
#intellichat-widget-input {
    display: flex;
    align-items: center;
    padding: 12px;
    border-top: 1px solid var(--ic-border);
    background: var(--ic-bg-input);
    gap: 8px;
    flex-shrink: 0;
    transition: background 0.3s ease;
}

#intellichat-q {
    flex: 1;
    border: 1px solid var(--ic-border);
    border-radius: 24px;
    padding: 10px 16px;
    font-size: 13.5px;
    font-family: var(--ic-font);
    outline: none;
    background: var(--ic-bg-body);
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.3s ease, color 0.3s ease;
    color: var(--ic-text-main);
}

#intellichat-q::placeholder {
    color: var(--ic-text-muted);
}

#intellichat-q:focus {
    border-color: var(--ic-primary);
    box-shadow: 0 0 0 3px var(--ic-primary-light);
    background: var(--ic-bg-input);
}

#intellichat-send {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: var(--ic-primary);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.2s ease, transform 0.15s ease;
    outline: none;
}

#intellichat-send:hover {
    background: var(--ic-primary-dark);
    transform: scale(1.05);
}

#intellichat-send:active {
    transform: scale(0.95);
}

#intellichat-send svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

/* ── Powered-by Footer ───────────────────────── */
.ic-powered-by {
    text-align: center;
    padding: 0 0 10px 0;
    font-size: 10px;
    color: var(--ic-text-muted);
    background: var(--ic-bg-widget);
    font-family: var(--ic-font);
    letter-spacing: 0.3px;
    transition: background 0.3s ease, color 0.3s ease;
}

.ic-powered-by a {
    color: inherit;
    text-decoration: none;
    transition: opacity 0.2s;
}

.ic-powered-by a:hover {
    opacity: 0.8;
}

/* ── Responsive ──────────────────────────────── */
@media (max-width: 768px) {
    #intellichat-widget {
        width: 100vw !important;
        height: 100dvh !important;
        max-height: 100dvh !important;
        bottom: 0 !important;
        right: 0 !important;
        left: 0 !important;
        border-radius: 0 !important;
        z-index: 9999999 !important;
    }

    #intellichat-widget.ic-open+#intellichat-widget-toggle {
        display: none !important;
    }

    #intellichat-widget-toggle {
        width: 54px;
        height: 54px;
    }

    .intellichat-msg.bot {
        max-width: calc(100% - 18px);
    }

    .intellichat-msg.bot.ic-has-feedback {
        padding-bottom: 44px;
    }

    .intellichat-feedback-btns {
        right: 8px;
        bottom: 8px;
        opacity: 1;
        transform: translateY(0);
        pointer-events: auto;
        padding: 3px;
    }

    .ic-fb-btn {
        width: 26px;
        height: 26px;
    }

    .ic-fb-thanks {
        font-size: 10px;
    }
}