/**
 * FLOSC Companion Widget (v1.6.1)
 * ================================
 * Floating chat widget for non-app WordPress pages.
 * Pattern: FAB trigger → expandable chat container → FLOSC iframe.
 * Styled via FLOSC CSS custom properties.
 * Reference: mesolitica/nous-chat-widget
 */

/* ─── Container ──────────────────────────────── */
.flosc-companion {
    --flosc-companion-viewport-height: 100dvh;
    --flosc-companion-viewport-width: 100vw;
    --flosc-companion-edge-gap: 16px;
    --flosc-companion-safe-bottom: calc(env(safe-area-inset-bottom, 0px) + var(--flosc-companion-edge-gap));
    --flosc-companion-safe-top: calc(env(safe-area-inset-top, 0px) + var(--flosc-companion-edge-gap));
    --flosc-companion-safe-side: calc(env(safe-area-inset-right, 0px) + var(--flosc-companion-edge-gap));
    --flosc-companion-window-offset: calc(var(--flosc-companion-launcher-size, 60px) + 16px);
    --flosc-companion-max-window-height: calc(var(--flosc-companion-viewport-height) - var(--flosc-companion-safe-top) - var(--flosc-companion-safe-bottom) - var(--flosc-companion-window-offset));
    position: fixed;
    bottom: var(--flosc-companion-safe-bottom);
    right: var(--flosc-companion-safe-side);
    z-index: 9999;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.flosc-companion.flosc-companion--left {
    right: auto;
    left: calc(env(safe-area-inset-left, 0px) + var(--flosc-companion-edge-gap));
}

/* ─── FAB Trigger ────────────────────────────── */
.flosc-companion-fab {
    width: var(--flosc-companion-launcher-size, 60px);
    height: var(--flosc-companion-launcher-size, 60px);
    border-radius: 50%;
    background: var(--flosc-accent, #2563eb);
    color: #ffffff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 22px -6px rgba(0, 0, 0, 0.30);
    transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
                box-shadow 0.35s ease;
    /* Gentle "breathing" glow to invite engagement (disabled under reduced-motion). */
    animation: flosc-companion-breathe 4.5s ease-in-out infinite;
}

.flosc-companion-fab:hover {
    transform: scale(1.1);
}

/* Pause the breathing while the panel is open or when motion is reduced. */
.flosc-companion.is-open .flosc-companion-fab,
.flosc-companion.flosc-companion--motion-reduce .flosc-companion-fab {
    animation: none;
}

.flosc-companion.flosc-companion--motion-reduce .flosc-companion-fab,
.flosc-companion.flosc-companion--motion-reduce .flosc-companion-window,
.flosc-companion.flosc-companion--motion-reduce .flosc-companion-fab svg {
    transition: none !important;
}

.flosc-companion.flosc-companion--motion-reduce .flosc-companion-fab:hover {
    transform: none;
}

.flosc-companion.flosc-companion--motion-full .flosc-companion-fab {
    transition-duration: 0.4s;
}

.flosc-companion.flosc-companion--motion-full .flosc-companion-window {
    transition-duration: 0.35s;
}

.flosc-companion-fab svg {
    width: calc(var(--flosc-companion-launcher-size, 60px) * 0.46);
    height: calc(var(--flosc-companion-launcher-size, 60px) * 0.46);
    fill: currentColor;
    transition: transform 0.3s ease;
}

.flosc-companion.is-open .flosc-companion-fab svg {
    transform: rotate(180deg);
}

/* ─── Chat Window (soft-glass surface) ───────── */
.flosc-companion-window {
    position: absolute;
    bottom: var(--flosc-companion-window-offset);
    right: 0;
    width: var(--flosc-companion-width, 380px);
    max-width: calc(var(--flosc-companion-viewport-width) - (var(--flosc-companion-edge-gap) * 2));
    max-height: var(--flosc-companion-max-window-height);
    height: min(var(--flosc-companion-height, 560px), var(--flosc-companion-max-window-height));
    display: flex;
    flex-direction: column;
    border-radius: 18px;
    overflow: hidden;
    background: var(--flosc-bg, #ffffff);
    box-shadow: 0 24px 60px -12px rgba(0, 0, 0, 0.40),
                0 0 0 1px rgba(0, 0, 0, 0.05);
    -webkit-backdrop-filter: blur(20px) saturate(1.15);
    backdrop-filter: blur(20px) saturate(1.15);
    opacity: 0;
    transform: translateY(16px) scale(0.96);
    pointer-events: none;
    transition: opacity 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
                transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.flosc-companion.flosc-companion--left .flosc-companion-window {
    right: auto;
    left: 0;
}

.flosc-companion.is-open .flosc-companion-window {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

.flosc-companion.is-panel .flosc-companion-window {
    width: var(--flosc-companion-width, 380px);
    height: var(--flosc-companion-height, 560px);
}

.flosc-companion.is-expanded .flosc-companion-window {
    width: min(520px, calc(var(--flosc-companion-viewport-width) - (var(--flosc-companion-edge-gap) * 2)));
    max-height: calc(var(--flosc-companion-viewport-height) - var(--flosc-companion-safe-top) - var(--flosc-companion-safe-bottom));
    height: min(820px, calc(var(--flosc-companion-viewport-height) - var(--flosc-companion-safe-top) - var(--flosc-companion-safe-bottom)));
}

/* ─── Header (accent gradient with hairline) ───
   Product chrome only — wallet/username live under the chat input
   inside the iframe (companion-embed session strip). */
.flosc-companion-header {
    background: var(--flosc-accent, #2563eb);
    background: linear-gradient(135deg, var(--flosc-accent, #2563eb),
                color-mix(in srgb, var(--flosc-accent, #2563eb) 80%, #000));
    color: #ffffff;
    padding: 12px 14px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.10);
    flex-shrink: 0;
}

.flosc-companion-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.flosc-companion-header-info {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    flex: 1 1 auto;
}

.flosc-companion-header-text {
    min-width: 0;
    flex: 1 1 auto;
}

.flosc-companion-avatar {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.18);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    flex-shrink: 0;
    overflow: hidden;
}

.flosc-companion-avatar--image {
    background: #ffffff;
    box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.25);
}

.flosc-companion-avatar-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
    padding: 3px;
    box-sizing: border-box;
}

.flosc-companion-fab-logo {
    width: 62%;
    height: 62%;
    object-fit: contain;
    display: block;
    border-radius: 6px;
    background: #fff;
    padding: 2px;
    box-sizing: border-box;
}

.flosc-companion-title {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.25;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
}

.flosc-companion-subtitle {
    font-size: 11px;
    opacity: 0.8;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Legacy class kept for older cached shells; no longer used for wallet. */
.flosc-companion-token-allotment {
    display: none;
}

.flosc-companion-close {
    background: none;
    border: none;
    color: #ffffff;
    cursor: pointer;
    padding: 4px;
    opacity: 0.85;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.2s, transform 0.2s ease;
}

.flosc-companion-close:hover {
    opacity: 1;
    transform: translateY(1px);
}

.flosc-companion-close svg {
    width: 18px;
    height: 18px;
}

.flosc-companion-fullscreen {
    background: rgba(255, 255, 255, 0.12);
    border: none;
    color: #ffffff;
    cursor: pointer;
    font-size: 16px;
    width: 34px;
    height: 34px;
    border-radius: 999px;
    opacity: 0.9;
    line-height: 1;
    transition: opacity 0.2s, background-color 0.2s;
}

.flosc-companion-fullscreen:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.22);
}

.flosc-companion-expand,
.flosc-companion-open-fullpage {
    background: rgba(255, 255, 255, 0.12);
    border: none;
    color: #ffffff;
    cursor: pointer;
    width: 38px;
    height: 30px;
    border-radius: 9px;
    font-size: 18px;
    font-weight: 700;
    line-height: 1;
    transition: opacity 0.2s, background-color 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.flosc-companion-open-fullpage {
    margin-right: 2px;
    padding: 0;
    line-height: 0;
}

.flosc-companion-open-fullpage svg {
    width: 17px;
    height: 17px;
    display: block;
}

.flosc-companion-expand:hover,
.flosc-companion-open-fullpage:hover {
    background: rgba(255, 255, 255, 0.22);
}

.flosc-companion.is-fullscreen .flosc-companion-window {
    position: fixed;
    inset: max(10px, env(safe-area-inset-top, 0px)) max(10px, env(safe-area-inset-right, 0px)) max(10px, env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    border-radius: 22px;
    z-index: 999999;
}

.flosc-companion.is-fullscreen::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: 999998;
    background: rgba(15, 23, 42, 0.28);
    backdrop-filter: blur(10px);
}

.flosc-companion.is-fullscreen .flosc-companion-fab {
    display: none;
}

/* ─── Iframe Body ────────────────────────────── */
.flosc-companion-body {
    width: 100%;
    height: auto;
    min-height: 0;
    flex: 1 1 auto;
    border: none;
}

.flosc-companion.is-open .flosc-companion-fab {
    display: none;
}

/* ─── Mobile Responsive ──────────────────────── */
@media (max-width: 640px) {
    .flosc-companion:not(.flosc-companion--mobile-panel) .flosc-companion-window {
        width: calc(var(--flosc-companion-viewport-width) - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
        height: calc(var(--flosc-companion-viewport-height) - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
        max-width: none;
        max-height: none;
        bottom: env(safe-area-inset-bottom, 0px);
        right: env(safe-area-inset-right, 0px);
        left: env(safe-area-inset-left, 0px);
        top: env(safe-area-inset-top, 0px);
        border-radius: 0;
        position: fixed;
    }

    .flosc-companion:not(.flosc-companion--mobile-panel).is-open .flosc-companion-fab {
        display: none;
    }

    .flosc-companion.flosc-companion--mobile-panel .flosc-companion-window {
        width: min(var(--flosc-companion-width, 380px), calc(var(--flosc-companion-viewport-width) - 24px));
        max-height: calc(var(--flosc-companion-viewport-height) - var(--flosc-companion-safe-top) - var(--flosc-companion-safe-bottom) - var(--flosc-companion-window-offset));
        height: min(var(--flosc-companion-height, 560px), calc(var(--flosc-companion-viewport-height) - var(--flosc-companion-safe-top) - var(--flosc-companion-safe-bottom) - var(--flosc-companion-window-offset)));
    }

    .flosc-companion.is-expanded .flosc-companion-window,
    .flosc-companion.is-fullscreen .flosc-companion-window {
        inset: 10px;
        width: auto;
        height: auto;
        border-radius: 18px;
    }
}

/* ─── Notification Badge ─────────────────────── */
.flosc-companion-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #dc2626;
    color: #ffffff;
    font-size: 11px;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #ffffff;
}

.flosc-companion-badge:empty {
    display: none;
}

.flosc-message--bot {
    background: #f1f5f9;
    border-bottom-left-radius: 4px;
    align-self: flex-start;
}

.flosc-message--user {
    background: var(--flosc-accent, #2563eb);
    color: #ffffff;
    border-bottom-right-radius: 4px;
    align-self: flex-end;
}

.flosc-launcher {
    animation: breathe 4s ease-in-out infinite;
}

@keyframes breathe {
    0%, 100% { box-shadow: 0 10px 30px -10px rgba(99, 102, 241, 0.5); }
    50% { box-shadow: 0 15px 40px -8px rgba(99, 102, 241, 0.7); }
}

/* ─── Breathing launcher keyframes ───────────── */
@keyframes flosc-companion-breathe {
    0%, 100% { box-shadow: 0 8px 22px -6px rgba(0, 0, 0, 0.30); }
    50%      { box-shadow: 0 14px 36px -6px rgba(0, 0, 0, 0.42); }
}

/* ─── Accessibility: visible keyboard focus + OS reduced-motion ─── */
.flosc-companion-fab:focus-visible,
.flosc-companion-close:focus-visible,
.flosc-companion-expand:focus-visible,
.flosc-companion-open-fullpage:focus-visible,
.flosc-companion-fullscreen:focus-visible,
.flosc-companion-window a:focus-visible,
.flosc-companion-window button:focus-visible {
    outline: 2px solid var(--flosc-accent, #2563eb);
    outline-offset: 2px;
    border-radius: 8px;
}

@media (prefers-reduced-motion: reduce) {
    .flosc-companion,
    .flosc-companion * {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
    }
}
