/**
 * AgenticWP Frontend Chat Widget
 *
 * All styles are scoped under .agenticwp-fc to prevent bleed into WordPress themes.
 * CSS custom properties are defined on .agenticwp-fc (not :root) for the same reason.
 *
 * Sections:
 *   1. CSS Variables
 *   2. Root Container
 *   3. Widget Bubble Button
 *   4. Chat Panel
 *   5. Header
 *   6. Messages Area
 *   7. Message Bubbles
 *   8. Typing Indicator
 *   9. Input Area
 *  10. Inline Variant
 *  11. Transitions & Animations
 *  12. Responsive
 *  13. Accessibility
 */

/* ==========================================================================
   1. CSS Variables
   ========================================================================== */

.agenticwp-fc {
    /* Colors */
    --fc-primary:          #007AFF;
    --fc-primary-hover:    #0071e3;
    --fc-primary-light:    rgba(0, 122, 255, 0.15);

    --fc-bg:               #ffffff;
    --fc-bg-messages:      #f5f5f7;
    --fc-bg-input:         #f5f5f7;
    --fc-bg-user-msg:      #007AFF;
    --fc-bg-assistant-msg: #e8e8ed;
    --fc-bg-frosted:       rgba(255, 255, 255, 0.85);

    --fc-text:             #1d1d1f;
    --fc-text-muted:       #86868b;
    --fc-text-on-primary:  #ffffff;

    --fc-border:           rgba(0, 0, 0, 0.06);
    --fc-border-input:     #d2d2d7;

    --fc-error:            #ff3b30;
    --fc-error-bg:         rgba(255, 59, 48, 0.08);

    --fc-disabled:         #d2d2d7;

    /* Typography */
    --fc-font-size-sm:     13px;
    --fc-font-size-base:   14px;
    --fc-font-size-md:     15px;
    --fc-font-weight-medium:   500;
    --fc-font-weight-semibold: 600;

    /* Border Radius */
    --fc-radius-sm:        6px;
    --fc-radius-md:        20px;
    --fc-radius-lg:        18px;
    --fc-radius-bubble:    50%;
    --fc-radius-panel:     14px;

    /* Shadows */
    --fc-shadow-bubble:    0 2px 8px rgba(0, 0, 0, 0.08), 0 4px 20px rgba(0, 0, 0, 0.12);
    --fc-shadow-panel:     0 0 0 1px rgba(0, 0, 0, 0.04), 0 8px 40px rgba(0, 0, 0, 0.12), 0 2px 12px rgba(0, 0, 0, 0.06);

    /* Layout */
    --fc-panel-width:      380px;
    --fc-panel-max-height: 500px;
    --fc-bubble-size:      52px;
    --fc-bubble-offset:    24px;

    --fc-z-bubble:         99998;
    --fc-z-panel:          99999;

    /* Transitions */
    --fc-transition:       0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    --fc-transition-fast:  0.15s ease;
    --fc-transition-spring: 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);

    /* Effects */
    --fc-backdrop:         blur(20px);
    --fc-backdrop-saturate: saturate(180%) blur(20px);

    /* Spacing */
    --fc-space-xs:         4px;
    --fc-space-sm:         8px;
    --fc-space-md:         12px;
    --fc-space-lg:         16px;
    --fc-space-xl:         20px;

    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
    font-size: var(--fc-font-size-base);
    line-height: 1.5;
    color: var(--fc-text);
    box-sizing: border-box;
}

.agenticwp-fc *,
.agenticwp-fc *::before,
.agenticwp-fc *::after {
    box-sizing: inherit;
    margin: 0;
    padding: 0;
}

/* ==========================================================================
   2. Root Container
   ========================================================================== */

.agenticwp-fc {
    position: fixed;
    bottom: var(--fc-bubble-offset);
    right: var(--fc-bubble-offset);
    z-index: var(--fc-z-bubble);
    /* Root container is sized to contain the bubble; panel is absolutely positioned */
}

/* ==========================================================================
   3. Widget Bubble Button
   ========================================================================== */

.agenticwp-fc__bubble {
    width: var(--fc-bubble-size);
    height: var(--fc-bubble-size);
    border-radius: var(--fc-radius-bubble);
    background-color: var(--fc-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--fc-shadow-bubble);
    transition: transform var(--fc-transition-spring),
                background-color var(--fc-transition-fast),
                box-shadow var(--fc-transition-fast);
    position: relative;
    z-index: var(--fc-z-bubble);
    color: white;
    backdrop-filter: var(--fc-backdrop);
    -webkit-backdrop-filter: var(--fc-backdrop);
}

.agenticwp-fc__bubble:hover {
    background-color: var(--fc-primary-hover);
    transform: scale(1.08);
}

.agenticwp-fc__bubble:focus-visible {
    outline: 3px solid var(--fc-primary);
    outline-offset: 3px;
}

.agenticwp-fc__bubble:active {
    transform: scale(0.95);
}

/* Bubble shows close icon when panel is open */
.agenticwp-fc__bubble[aria-expanded="true"] {
    background-color: var(--fc-primary-hover);
}

/* Animated icon crossfade */
.agenticwp-fc__icon {
    position: absolute;
    transition: opacity 0.3s ease, transform 0.3s ease;
}

.agenticwp-fc__icon--chat {
    opacity: 1;
    transform: rotate(0deg);
}

.agenticwp-fc__icon--close {
    opacity: 0;
    transform: rotate(-90deg);
}

.agenticwp-fc__bubble.is-open .agenticwp-fc__icon--chat {
    opacity: 0;
    transform: rotate(90deg);
}

.agenticwp-fc__bubble.is-open .agenticwp-fc__icon--close {
    opacity: 1;
    transform: rotate(0deg);
}

/* ==========================================================================
   4. Chat Panel
   ========================================================================== */

.agenticwp-fc__panel {
    position: absolute;
    bottom: calc(var(--fc-bubble-size) + var(--fc-space-lg));
    right: 0;
    width: var(--fc-panel-width);
    max-height: var(--fc-panel-max-height);
    background-color: var(--fc-bg);
    border-radius: var(--fc-radius-panel);
    box-shadow: var(--fc-shadow-panel);
    border: 1px solid var(--fc-border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: var(--fc-z-panel);

    /* Closed state */
    opacity: 0;
    visibility: hidden;
    transform: translateY(8px);
    transition: opacity var(--fc-transition),
                visibility var(--fc-transition),
                transform var(--fc-transition);
    pointer-events: none;
}

.agenticwp-fc__panel.is-open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
    pointer-events: auto;
}

/* ==========================================================================
   5. Header
   ========================================================================== */

.agenticwp-fc__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background: var(--fc-bg-frosted);
    backdrop-filter: var(--fc-backdrop-saturate);
    -webkit-backdrop-filter: var(--fc-backdrop-saturate);
    color: var(--fc-text);
    font-size: var(--fc-font-size-md);
    font-weight: var(--fc-font-weight-semibold);
    flex-shrink: 0;
    border-bottom: 1px solid var(--fc-border);
}

.agenticwp-fc__header-title {
    font-size: var(--fc-font-size-md);
    font-weight: var(--fc-font-weight-semibold);
    letter-spacing: -0.01em;
}

.agenticwp-fc__header-actions {
    display: flex;
    align-items: center;
    gap: var(--fc-space-xs);
}

.agenticwp-fc__clear,
.agenticwp-fc__close {
    width: 28px;
    height: 28px;
    border-radius: var(--fc-radius-sm);
    background: transparent;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--fc-text-muted);
    transition: color var(--fc-transition-fast);
}

.agenticwp-fc__clear:hover,
.agenticwp-fc__close:hover {
    color: var(--fc-text);
}

.agenticwp-fc__clear:focus-visible,
.agenticwp-fc__close:focus-visible {
    outline: 2px solid var(--fc-primary);
    outline-offset: 2px;
}

/* ==========================================================================
   6. Messages Area
   ========================================================================== */

.agenticwp-fc__messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: var(--fc-space-sm);
    background-color: var(--fc-bg-messages);
    scroll-behavior: smooth;
    min-height: 0; /* Required for flex overflow to work */
}

.agenticwp-fc__messages {
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.15) transparent;
}

.agenticwp-fc__messages::-webkit-scrollbar {
    width: 6px;
}

.agenticwp-fc__messages::-webkit-scrollbar-track {
    background: transparent;
}

.agenticwp-fc__messages::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.15);
    border-radius: 3px;
}

.agenticwp-fc__messages::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.25);
}

/* ==========================================================================
   7. Message Bubbles
   ========================================================================== */

.agenticwp-fc__message {
    max-width: 75%;
    word-wrap: break-word;
    overflow-wrap: break-word;
    animation: agenticwp-fc-message-in var(--fc-transition) ease-out;
}

/* User messages: right-aligned blue bubble */
.agenticwp-fc__message--user {
    align-self: flex-end;
    background-color: var(--fc-bg-user-msg);
    color: var(--fc-text-on-primary);
    padding: 10px 14px;
    border-radius: 18px 18px 4px 18px;
    font-size: var(--fc-font-size-base);
    line-height: 1.4;
}

/* Assistant messages: left-aligned light bubble */
.agenticwp-fc__message--assistant {
    align-self: flex-start;
    background-color: var(--fc-bg-assistant-msg);
    color: var(--fc-text);
    padding: 10px 14px;
    border-radius: 18px 18px 18px 4px;
    font-size: var(--fc-font-size-base);
    line-height: 1.4;
}

/* Markdown content resets — prevent theme styles from bleeding into rendered markdown */
.agenticwp-fc__message--assistant p {
    margin: 0 0 6px 0;
}

.agenticwp-fc__message--assistant p:last-child {
    margin-bottom: 0;
}

.agenticwp-fc__message--assistant ul,
.agenticwp-fc__message--assistant ol {
    margin: 6px 0;
    padding-left: 18px;
}

.agenticwp-fc__message--assistant li {
    margin: 2px 0;
}

.agenticwp-fc__message--assistant code {
    background: rgba(0, 0, 0, 0.07);
    border-radius: 3px;
    padding: 1px 5px;
    font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.88em;
}

.agenticwp-fc__message--assistant pre {
    background: rgba(0, 0, 0, 0.05);
    border: 1px solid var(--fc-border);
    border-radius: var(--fc-radius-sm);
    padding: 10px;
    overflow-x: auto;
    margin: 6px 0;
    font-size: 0.88em;
}

.agenticwp-fc__message--assistant pre code {
    background: none;
    padding: 0;
}

.agenticwp-fc__message--assistant a {
    color: var(--fc-primary);
    text-decoration: underline;
    text-decoration-color: rgba(0, 122, 255, 0.4);
}

.agenticwp-fc__message--assistant a:hover {
    color: var(--fc-primary-hover);
    text-decoration-color: var(--fc-primary-hover);
}

.agenticwp-fc__message--assistant strong {
    font-weight: 600;
}

/* Welcome message — informational text, not a bubble */
.agenticwp-fc__messages > .agenticwp-fc__message--assistant:first-child {
    text-align: center;
    max-width: 100%;
    background: transparent;
    color: var(--fc-text-muted);
    font-size: var(--fc-font-size-sm);
    padding-top: var(--fc-space-xl);
    padding-bottom: var(--fc-space-md);
    border-radius: 0;
}

/* ==========================================================================
   7a. Reasoning Block
   ========================================================================== */

/* Wrapper that holds one reasoning block (hidden when empty) */
.agenticwp-fc__reasoning-wrapper:empty {
    display: none;
}

/* The block itself — subtle, collapsible */
.agenticwp-fc__reasoning-block {
    margin-bottom: var(--fc-space-sm);
    border-radius: var(--fc-radius-sm);
    background-color: rgba(0, 0, 0, 0.04);
    border: 1px solid var(--fc-border);
    overflow: hidden;
}

/* Toggle button */
.agenticwp-fc__reasoning-toggle {
    display: flex;
    align-items: center;
    gap: var(--fc-space-xs);
    width: 100%;
    padding: 6px 10px;
    background: none;
    border: none;
    cursor: pointer;
    color: var(--fc-text-muted);
    font-size: var(--fc-font-size-sm);
    font-family: inherit;
    text-align: left;
    transition: color var(--fc-transition-fast);
}

.agenticwp-fc__reasoning-toggle:hover {
    color: var(--fc-text);
}

.agenticwp-fc__reasoning-toggle:focus-visible {
    outline: 2px solid var(--fc-primary);
    outline-offset: -2px;
}

.agenticwp-fc__reasoning-label {
    flex: 1;
}

/* Arrow indicator — rotates 180° when expanded */
.agenticwp-fc__reasoning-arrow {
    flex-shrink: 0;
    transition: transform var(--fc-transition-fast);
    transform: rotate(0deg);
}

.agenticwp-fc__reasoning-block--expanded .agenticwp-fc__reasoning-arrow {
    transform: rotate(180deg);
}

/* Collapsible content area */
.agenticwp-fc__reasoning-content {
    max-height: 0;
    overflow: hidden;
    padding: 0 10px;
    font-size: var(--fc-font-size-sm);
    color: var(--fc-text-muted);
    line-height: 1.5;
    transition: max-height 0.25s ease, padding 0.25s ease;
}

.agenticwp-fc__reasoning-block--expanded .agenticwp-fc__reasoning-content {
    max-height: 300px;
    overflow-y: auto;
    padding: 6px 10px 10px;
}

/* Markdown resets inside reasoning content */
.agenticwp-fc__reasoning-content p {
    margin: 0 0 4px 0;
}

.agenticwp-fc__reasoning-content p:last-child {
    margin-bottom: 0;
}

.agenticwp-fc__reasoning-content code {
    background: rgba(0, 0, 0, 0.06);
    border-radius: 3px;
    padding: 1px 4px;
    font-size: 0.88em;
}

/* Reduce motion */
@media (prefers-reduced-motion: reduce) {
    .agenticwp-fc__reasoning-content {
        transition: none;
    }

    .agenticwp-fc__reasoning-arrow {
        transition: none;
    }
}

/* Error messages */
.agenticwp-fc__message--error {
    align-self: stretch;
    background-color: var(--fc-error-bg);
    color: var(--fc-error);
    padding: 10px 14px;
    border-radius: 12px;
    font-size: var(--fc-font-size-sm);
    border: 1px solid rgba(255, 59, 48, 0.15);
}

/* ==========================================================================
   8. Typing Indicator
   ========================================================================== */

.agenticwp-fc__typing {
    align-self: flex-start;
    background-color: var(--fc-bg-assistant-msg);
    padding: 10px 14px;
    border-radius: 18px 18px 18px 4px;
    display: none;
    align-items: center;
    gap: 4px;
}

.agenticwp-fc__typing--active {
    display: flex;
}

.agenticwp-fc__typing-dot {
    width: 6px;
    height: 6px;
    background-color: var(--fc-text-muted);
    border-radius: 50%;
    animation: agenticwp-fc-typing 1.3s infinite ease-in-out;
}

.agenticwp-fc__typing-dot:nth-child(1) { animation-delay: 0s; }
.agenticwp-fc__typing-dot:nth-child(2) { animation-delay: 0.2s; }
.agenticwp-fc__typing-dot:nth-child(3) { animation-delay: 0.4s; }

/* ==========================================================================
   9. Input Area
   ========================================================================== */

.agenticwp-fc__input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 14px;
    background: var(--fc-bg-frosted);
    backdrop-filter: var(--fc-backdrop-saturate);
    -webkit-backdrop-filter: var(--fc-backdrop-saturate);
    flex-shrink: 0;
    border-radius: 0 0 var(--fc-radius-panel) var(--fc-radius-panel);
}

.agenticwp-fc__textarea {
    flex: 1;
    min-height: 38px;
    max-height: 96px;
    padding: 9px 16px;
    border: 1px solid var(--fc-border-input);
    border-radius: 20px;
    background-color: var(--fc-bg-input);
    color: var(--fc-text);
    font-family: inherit;
    font-size: var(--fc-font-size-base);
    line-height: 1.45;
    resize: none;
    outline: none;
    overflow-y: auto;
    transition: border-color var(--fc-transition-fast),
                box-shadow var(--fc-transition-fast);
    /* Override WordPress theme textarea styles */
    box-shadow: none;
    appearance: none;
    -webkit-appearance: none;
}

.agenticwp-fc__textarea:focus {
    border-color: var(--fc-primary);
    box-shadow: 0 0 0 3px var(--fc-primary-light);
}

.agenticwp-fc__textarea::placeholder {
    color: var(--fc-text-muted);
}

.agenticwp-fc__textarea:disabled {
    background-color: var(--fc-bg-messages);
    color: var(--fc-text-muted);
    cursor: not-allowed;
}

.agenticwp-fc__send {
    flex-shrink: 0;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background-color: var(--fc-primary);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform var(--fc-transition-fast),
                background-color var(--fc-transition-fast);
    color: white;
}

.agenticwp-fc__send:hover:not(:disabled) {
    background-color: var(--fc-primary-hover);
}

.agenticwp-fc__send:focus-visible {
    outline: 2px solid var(--fc-primary);
    outline-offset: 2px;
}

.agenticwp-fc__send:active:not(:disabled) {
    transform: scale(0.9);
}

.agenticwp-fc__send:disabled {
    background-color: var(--fc-disabled);
    cursor: not-allowed;
    transform: none;
}

/* ==========================================================================
   10. Inline Variant
   ========================================================================== */

/*
 * .agenticwp-fc--inline: used for shortcode embeds. Flows with page content
 * rather than floating fixed over it.
 */
.agenticwp-fc--inline {
    position: static;
    bottom: auto;
    right: auto;
    z-index: auto;
    display: block;
    width: 100%;
    max-width: var(--fc-panel-width);
}

.agenticwp-fc--inline .agenticwp-fc__bubble {
    display: none; /* No bubble in inline mode */
}

.agenticwp-fc--inline .agenticwp-fc__panel {
    position: static;
    opacity: 1;
    visibility: visible;
    transform: none;
    pointer-events: auto;
    width: 100%;
    max-height: var(--fc-panel-max-height);
    box-shadow: var(--fc-shadow-panel);
    border-radius: var(--fc-radius-panel);
    border: 1px solid var(--fc-border);
    overflow: hidden;
    transition: none;
}

/* Solid background fallback for inline mode when backdrop-filter is unsupported */
@supports not (backdrop-filter: blur(1px)) {
    .agenticwp-fc--inline .agenticwp-fc__header,
    .agenticwp-fc--inline .agenticwp-fc__input-area {
        background: var(--fc-bg);
    }
}

/* ==========================================================================
   11. Transitions & Animations
   ========================================================================== */

@keyframes agenticwp-fc-message-in {
    from {
        opacity: 0;
        transform: translateY(4px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

@keyframes agenticwp-fc-typing {
    0%, 60%, 100% {
        opacity: 0.4;
        transform: scale(0.4);
    }
    30% {
        opacity: 1;
        transform: scale(1);
    }
}

/* ==========================================================================
   12. Responsive
   ========================================================================== */

@media (max-width: 480px) {
    .agenticwp-fc {
        --fc-bubble-offset: 16px;
    }

    /* Panel goes near-full-width with gutters on mobile */
    .agenticwp-fc__panel {
        position: fixed;
        left: 12px;
        right: 12px;
        bottom: calc(var(--fc-bubble-offset) + var(--fc-bubble-size) + 12px);
        width: auto;
        max-height: 70vh;
        border-radius: 12px;
    }

    .agenticwp-fc--inline .agenticwp-fc__panel {
        max-height: 80vh;
        left: auto;
        right: auto;
        border-radius: 12px;
    }
}

/* ==========================================================================
   13. Accessibility
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .agenticwp-fc__panel {
        transition: opacity 0.01s, visibility 0.01s;
        transform: none !important;
    }

    .agenticwp-fc__bubble {
        transition: background-color 0.01s;
        transform: none !important;
    }

    .agenticwp-fc__icon {
        transition: opacity 0.01s;
        transform: none !important;
    }

    .agenticwp-fc__message {
        animation: none;
    }

    .agenticwp-fc__send {
        transition: background-color 0.01s;
        transform: none !important;
    }

    .agenticwp-fc__typing-dot {
        animation: none;
        opacity: 0.5;
    }

    .agenticwp-fc__typing-dot:nth-child(2) {
        opacity: 0.7;
    }

    .agenticwp-fc__typing-dot:nth-child(3) {
        opacity: 0.9;
    }
}
