/* ============================================================
   FINAP - Financial Advisor Chatbot Widget v2.0
   Prefix: fcb- (finance chatbot)
   Features: dark mode, search, scroll btn, satisfaction, confidence
   ============================================================ */

/* ---------- CSS Variables ---------- */
#fcb-widget {
    --fcb-brand: #072890;
    --fcb-brand-light: #0a3ab8;
    --fcb-brand-rgb: 7, 40, 144;
    --fcb-bg: #f8f9fc;
    --fcb-surface: #fff;
    --fcb-border: #e2e8f0;
    --fcb-text: #1e293b;
    --fcb-text-sec: #94a3b8;
    --fcb-bot-bg: #fff;
    --fcb-bot-border: #e2e8f0;
    --fcb-user-bg: linear-gradient(135deg, #072890, #0a3ab8);
    --fcb-input-bg: #fff;
    --fcb-shadow: 0 12px 48px rgba(0, 0, 0, .18);
}

/* ---------- Dark Mode ---------- */
#fcb-widget.fcb-dark {
    --fcb-bg: #1a1b2e;
    --fcb-surface: #232540;
    --fcb-border: #2d3050;
    --fcb-text: #e2e8f0;
    --fcb-text-sec: #7c8db5;
    --fcb-bot-bg: #232540;
    --fcb-bot-border: #2d3050;
    --fcb-user-bg: linear-gradient(135deg, #1e40af, #3b82f6);
    --fcb-input-bg: #232540;
    --fcb-shadow: 0 12px 48px rgba(0, 0, 0, .5);
}

/* ---------- Floating trigger button ---------- */
.fcb-trigger {
    position: fixed;
    bottom: 28px;
    right: 28px;
    z-index: 9990;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--fcb-brand) 0%, var(--fcb-brand-light) 100%);
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(var(--fcb-brand-rgb), .35);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform .25s ease, box-shadow .25s ease;
}
.fcb-trigger:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 28px rgba(var(--fcb-brand-rgb), .5);
}
.fcb-trigger svg { width: 28px; height: 28px; fill: #fff; }
.fcb-trigger .fcb-close-icon { display: none; }
.fcb-trigger.fcb-open .fcb-bot-icon { display: none; }
.fcb-trigger.fcb-open .fcb-close-icon { display: block; }

.fcb-trigger:not(.fcb-open)::after {
    content: '';
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 2px solid rgba(var(--fcb-brand-rgb), .3);
    animation: fcb-pulse 2.5s ease-out infinite;
}
@keyframes fcb-pulse {
    0%   { transform: scale(1); opacity: .7; }
    100% { transform: scale(1.5); opacity: 0; }
}

.fcb-badge {
    position: absolute; top: -2px; right: -2px;
    width: 18px; height: 18px; border-radius: 50%;
    background: #e30613; color: #fff;
    font-size: 11px; font-weight: 700;
    display: none; align-items: center; justify-content: center;
    line-height: 1;
}

/* ---------- Chat window ---------- */
.fcb-window {
    position: fixed;
    bottom: 100px; right: 28px;
    z-index: 9989;
    width: 400px;
    max-width: min(400px, 100vw - 56px);
    box-sizing: border-box;
    height: 560px;
    max-height: calc(100vh - 140px);
    background: var(--fcb-surface);
    border-radius: 16px;
    box-shadow: var(--fcb-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateY(20px) scale(.95);
    pointer-events: none;
    transition: opacity .3s ease, transform .3s ease;
}
.fcb-window.fcb-visible {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* Maximized */
.fcb-window.fcb-maximized {
    bottom: 0; right: 0;
    width: 100%; max-width: 100%;
    height: 100%; max-height: 100%;
    border-radius: 0; z-index: 10000;
}
@media (min-width: 768px) {
    .fcb-window.fcb-maximized {
        bottom: 20px; right: 20px;
        width: calc(100vw - 40px); max-width: 900px;
        height: calc(100vh - 40px); max-height: calc(100vh - 40px);
        border-radius: 16px;
    }
}

/* ---------- Header ---------- */
.fcb-header {
    background: linear-gradient(135deg, var(--fcb-brand) 0%, var(--fcb-brand-light) 100%);
    padding: 12px 14px;
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}
.fcb-avatar {
    width: 36px; height: 36px;
    border-radius: 50%;
    background: rgba(255,255,255,.15);
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
}
.fcb-avatar svg { width: 20px; height: 20px; fill: #fff; }
.fcb-header-info { flex: 1; min-width: 0; }
.fcb-header-name { color: #fff; font-size: 14px; font-weight: 700; line-height: 1.2; }
.fcb-header-status {
    color: rgba(255,255,255,.7); font-size: 11px;
    display: flex; align-items: center; gap: 5px;
}
.fcb-header-status::before {
    content: ''; width: 7px; height: 7px;
    border-radius: 50%; background: #4ade80;
    display: inline-block;
}
.fcb-status-typing {
    animation: fcb-status-pulse 1.2s ease-in-out infinite;
}
.fcb-status-typing::before { display: none; }
@keyframes fcb-status-pulse {
    0%, 100% { opacity: .7; }
    50% { opacity: 1; }
}

.fcb-header-actions {
    display: flex; gap: 3px; flex-wrap: wrap; justify-content: flex-end;
}
.fcb-header-btn {
    width: 28px; height: 28px;
    border-radius: 6px; border: none;
    background: rgba(255,255,255,.1);
    color: #fff; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: background .2s;
}
.fcb-header-btn:hover { background: rgba(255,255,255,.22); }
.fcb-header-btn svg { width: 14px; height: 14px; fill: currentColor; }
.fcb-lang-btn {
    font-family: inherit;
    min-width: 28px;
}

/* ---------- Search bar ---------- */
.fcb-search-bar {
    display: none;
    padding: 8px 12px;
    background: var(--fcb-surface);
    border-bottom: 1px solid var(--fcb-border);
    gap: 8px;
    align-items: center;
}
.fcb-search-bar.active { display: flex; }
.fcb-search-input {
    flex: 1;
    padding: 6px 10px;
    border: 1px solid var(--fcb-border);
    border-radius: 8px;
    font-size: 13px;
    font-family: inherit;
    outline: none;
    background: var(--fcb-input-bg);
    color: var(--fcb-text);
}
.fcb-search-input:focus { border-color: var(--fcb-brand); }
.fcb-search-count {
    font-size: 11px;
    color: var(--fcb-text-sec);
    white-space: nowrap;
}
.fcb-search-close {
    background: none; border: none;
    color: var(--fcb-text-sec); cursor: pointer;
    padding: 2px;
}
.fcb-search-close:hover { color: var(--fcb-text); }
.fcb-search-hit {
    background: rgba(255, 237, 74, 0.15);
    border-radius: 8px;
}
.fcb-dark .fcb-search-hit {
    background: rgba(255, 237, 74, 0.08);
}
mark.fcb-highlight {
    background: #fef08a;
    color: #1e293b;
    padding: 0 2px;
    border-radius: 2px;
}
.fcb-dark mark.fcb-highlight {
    background: #854d0e;
    color: #fef08a;
}

/* ---------- Messages area ---------- */
.fcb-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--fcb-bg);
    position: relative;
}
.fcb-messages::-webkit-scrollbar { width: 4px; }
.fcb-messages::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.fcb-dark .fcb-messages::-webkit-scrollbar-thumb { background: #3d4060; }

/* ---------- Scroll to bottom button ---------- */
.fcb-scroll-btn {
    position: absolute;
    bottom: 70px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 5;
    width: 36px; height: 36px;
    border-radius: 50%;
    border: 1px solid var(--fcb-border);
    background: var(--fcb-surface);
    color: var(--fcb-text);
    cursor: pointer;
    box-shadow: 0 2px 12px rgba(0,0,0,.12);
    display: flex; align-items: center; justify-content: center;
    transition: all .2s;
    animation: fcb-bounce-in .3s ease;
}
.fcb-scroll-btn:hover {
    background: var(--fcb-brand);
    color: #fff;
    border-color: var(--fcb-brand);
}
.fcb-scroll-btn svg { fill: currentColor; }
@keyframes fcb-bounce-in {
    from { transform: translateX(-50%) scale(0); }
    to { transform: translateX(-50%) scale(1); }
}

/* ---------- Message bubbles ---------- */
.fcb-msg {
    max-width: 85%;
    display: flex;
    flex-direction: column;
    animation: fcb-msg-in .3s ease both;
}
@keyframes fcb-msg-in {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}
.fcb-msg--bot { align-self: flex-start; }
.fcb-msg--user { align-self: flex-end; }

.fcb-msg-bubble {
    padding: 10px 14px;
    border-radius: 14px;
    font-size: 14px;
    line-height: 1.55;
    word-break: break-word;
}
.fcb-msg--bot .fcb-msg-bubble {
    background: var(--fcb-bot-bg);
    color: var(--fcb-text);
    border: 1px solid var(--fcb-bot-border);
    border-bottom-left-radius: 4px;
}
.fcb-msg--user .fcb-msg-bubble {
    background: var(--fcb-user-bg);
    color: #fff;
    border-bottom-right-radius: 4px;
}
.fcb-msg-time {
    font-size: 11px;
    color: var(--fcb-text-sec);
    margin-top: 3px;
    padding: 0 4px;
}
.fcb-msg--user .fcb-msg-time { text-align: right; }

/* ---------- Typing indicator ---------- */
.fcb-typing {
    display: flex;
    gap: 4px;
    padding: 12px 16px;
    align-self: flex-start;
    background: var(--fcb-bot-bg);
    border: 1px solid var(--fcb-bot-border);
    border-radius: 14px;
    border-bottom-left-radius: 4px;
    align-items: center;
    flex-wrap: wrap;
}
.fcb-typing span:not(.fcb-typing-text) {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--fcb-text-sec);
    animation: fcb-bounce .6s ease infinite alternate;
}
.fcb-typing span:nth-child(2) { animation-delay: .15s; }
.fcb-typing span:nth-child(3) { animation-delay: .3s; }
@keyframes fcb-bounce {
    from { transform: translateY(0); }
    to   { transform: translateY(-5px); }
}
.fcb-typing-text {
    width: 100%;
    font-size: 11px;
    color: var(--fcb-text-sec);
    margin-top: 4px;
    font-style: italic;
}

/* ---------- Quick suggestions (stagger) ---------- */
.fcb-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 0 4px;
    align-self: flex-start;
}
.fcb-suggestion {
    padding: 6px 12px;
    border-radius: 20px;
    border: 1px solid var(--fcb-brand);
    background: rgba(var(--fcb-brand-rgb), .04);
    color: var(--fcb-brand);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    transition: all .2s;
    white-space: nowrap;
    opacity: 0;
    transform: translateY(8px);
    animation: fcb-sug-in .35s ease forwards;
}
@keyframes fcb-sug-in {
    to { opacity: 1; transform: translateY(0); }
}
.fcb-suggestion:hover {
    background: var(--fcb-brand);
    color: #fff;
}
.fcb-dark .fcb-suggestion {
    border-color: #5b7cfa;
    color: #93b4fd;
    background: rgba(91, 124, 250, .08);
}
.fcb-dark .fcb-suggestion:hover {
    background: #3b5bdb;
    color: #fff;
    border-color: #3b5bdb;
}

/* ---------- Confidence dot ---------- */
.fcb-conf-dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    display: inline-block;
    margin-left: 2px;
    cursor: help;
}
.fcb-conf-high { background: #22c55e; }
.fcb-conf-mid { background: #f59e0b; }
.fcb-conf-low { background: #ef4444; }

/* ---------- Input area ---------- */
.fcb-input-area {
    padding: 10px 12px;
    border-top: 1px solid var(--fcb-border);
    display: flex;
    gap: 8px;
    align-items: flex-end;
    background: var(--fcb-surface);
    flex-shrink: 0;
}
.fcb-input {
    flex: 1;
    min-height: 38px;
    max-height: 100px;
    padding: 8px 14px;
    border: 1px solid var(--fcb-border);
    border-radius: 20px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    outline: none;
    line-height: 1.4;
    transition: border-color .2s;
    overflow-y: auto;
    background: var(--fcb-input-bg);
    color: var(--fcb-text);
}
.fcb-input:focus { border-color: var(--fcb-brand); }
.fcb-input::placeholder { color: var(--fcb-text-sec); }

.fcb-send {
    width: 38px; height: 38px;
    border-radius: 50%; border: none;
    background: var(--fcb-brand);
    color: #fff; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: background .2s, transform .15s;
}
.fcb-send:hover { background: var(--fcb-brand-light); }
.fcb-send:active { transform: scale(.92); }
.fcb-send:disabled { background: #cbd5e1; cursor: not-allowed; }
.fcb-dark .fcb-send:disabled { background: #3d4060; }
.fcb-send svg { width: 18px; height: 18px; fill: currentColor; }

/* ---------- Markdown in bot messages ---------- */
.fcb-msg--bot .fcb-msg-bubble p { margin: 0 0 8px; }
.fcb-msg--bot .fcb-msg-bubble p:last-child { margin-bottom: 0; }
.fcb-msg--bot .fcb-msg-bubble ul,
.fcb-msg--bot .fcb-msg-bubble ol { margin: 4px 0 8px 18px; padding: 0; }
.fcb-msg--bot .fcb-msg-bubble li { margin-bottom: 2px; }
.fcb-msg--bot .fcb-msg-bubble strong { font-weight: 600; color: var(--fcb-brand); }
.fcb-dark .fcb-msg--bot .fcb-msg-bubble strong { color: #93b4fd; }
.fcb-msg--bot .fcb-msg-bubble code {
    background: rgba(var(--fcb-brand-rgb), .06);
    padding: 1px 5px; border-radius: 4px; font-size: 13px;
}

/* ---------- Tables ---------- */
.fcb-table {
    width: 100%; border-collapse: collapse;
    margin: 8px 0; font-size: 13px; line-height: 1.4;
}
.fcb-table th, .fcb-table td {
    padding: 6px 10px;
    border: 1px solid var(--fcb-border);
    text-align: left; word-break: break-word;
}
.fcb-table th {
    background: rgba(var(--fcb-brand-rgb), .06);
    font-weight: 600; color: var(--fcb-brand);
    font-size: 12px; white-space: nowrap;
}
.fcb-dark .fcb-table th { color: #93b4fd; background: rgba(91,124,250,.1); }
.fcb-table tr:nth-child(even) td { background: rgba(var(--fcb-brand-rgb), .02); }
.fcb-table tr:hover td { background: rgba(var(--fcb-brand-rgb), .04); }

/* ---------- Streaming cursor ---------- */
.fcb-cursor {
    display: inline-block;
    color: var(--fcb-brand);
    font-weight: 700; font-size: 16px;
    animation: fcb-blink .6s step-end infinite;
    margin-left: 1px;
}
@keyframes fcb-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}
.fcb-stream-text strong { font-weight: 600; color: var(--fcb-brand); }
.fcb-dark .fcb-stream-text strong { color: #93b4fd; }
.fcb-dark .fcb-cursor { color: #5b7cfa; }

/* ---------- Feedback buttons ---------- */
.fcb-msg-meta {
    display: flex; align-items: center;
    gap: 6px; padding: 0 4px; margin-top: 3px;
}
.fcb-feedback { display: flex; gap: 2px; }
.fcb-fb-btn {
    background: none; border: none;
    padding: 2px 4px; cursor: pointer;
    color: var(--fcb-text-sec);
    border-radius: 4px;
    transition: color .2s, background .2s;
    line-height: 1;
}
.fcb-fb-btn:hover {
    color: var(--fcb-brand);
    background: rgba(var(--fcb-brand-rgb), .08);
}
.fcb-fb-btn.fcb-fb-down:hover {
    color: #e30613;
    background: rgba(227, 6, 19, .08);
}
.fcb-copy-btn { opacity: 0; transition: opacity .2s, color .2s, background .2s; }
.fcb-msg:hover .fcb-copy-btn { opacity: 1; }

/* ---------- Dislike form ---------- */
.fcb-dislike-form {
    background: var(--fcb-bg);
    border: 1px solid var(--fcb-border);
    border-radius: 12px;
    padding: 12px; margin-top: 6px;
    animation: fcb-msg-in .3s ease both;
}
.fcb-df-title { font-size: 12px; font-weight: 600; color: var(--fcb-text); margin-bottom: 8px; }
.fcb-df-comment {
    width: 100%; padding: 8px 10px;
    border: 1px solid var(--fcb-border);
    border-radius: 8px; font-size: 13px;
    font-family: inherit; resize: none; outline: none;
    transition: border-color .2s; box-sizing: border-box;
    background: var(--fcb-input-bg); color: var(--fcb-text);
}
.fcb-df-comment:focus { border-color: var(--fcb-brand); }
.fcb-df-contact { margin-top: 8px; }
.fcb-df-label { font-size: 11px; color: var(--fcb-text-sec); margin-bottom: 6px; }
.fcb-df-input {
    width: 100%; padding: 7px 10px;
    border: 1px solid var(--fcb-border);
    border-radius: 8px; font-size: 13px;
    font-family: inherit; outline: none;
    margin-bottom: 4px; transition: border-color .2s;
    box-sizing: border-box;
    background: var(--fcb-input-bg); color: var(--fcb-text);
}
.fcb-df-input:focus { border-color: var(--fcb-brand); }
.fcb-df-actions {
    display: flex; gap: 6px; margin-top: 8px;
    justify-content: flex-end;
}
.fcb-df-skip, .fcb-df-submit {
    padding: 6px 12px; border-radius: 8px;
    border: none; font-size: 12px; font-weight: 500;
    cursor: pointer; transition: all .2s;
}
.fcb-df-skip { background: var(--fcb-bg); color: var(--fcb-text-sec); }
.fcb-df-skip:hover { background: var(--fcb-border); }
.fcb-df-submit { background: var(--fcb-brand); color: #fff; }
.fcb-df-submit:hover { background: var(--fcb-brand-light); }
.fcb-df-success {
    display: flex; align-items: center; gap: 4px;
    padding: 10px 12px; border-radius: 8px;
    font-size: 13px; line-height: 1.4;
    background: #f0fdf4; border: 1px solid #bbf7d0; color: #166534;
}
.fcb-dark .fcb-df-success { background: #14532d20; border-color: #16653440; color: #4ade80; }
.fcb-df-error {
    margin-top: 6px; padding: 6px 10px;
    background: #fef2f2; border: 1px solid #fecaca;
    border-radius: 6px; font-size: 12px; color: #dc2626;
    text-align: center;
}
.fcb-dark .fcb-df-error { background: #7f1d1d20; border-color: #dc262640; color: #fca5a5; }

/* ---------- Satisfaction prompt ---------- */
.fcb-satisfaction {
    align-self: center;
    background: var(--fcb-surface);
    border: 1px solid var(--fcb-border);
    border-radius: 14px;
    padding: 14px 18px;
    text-align: center;
    animation: fcb-msg-in .4s ease both;
    transition: opacity .3s;
    max-width: 300px;
}
.fcb-sat-text {
    font-size: 13px;
    font-weight: 600;
    color: var(--fcb-text);
    margin-bottom: 10px;
}
.fcb-sat-btns {
    display: flex; gap: 6px;
    justify-content: center; flex-wrap: wrap;
}
.fcb-sat-btn {
    padding: 5px 10px;
    border-radius: 16px;
    border: 1px solid var(--fcb-border);
    background: var(--fcb-bg);
    color: var(--fcb-text);
    font-size: 12px;
    cursor: pointer;
    transition: all .2s;
}
.fcb-sat-btn:hover {
    background: var(--fcb-brand);
    color: #fff;
    border-color: var(--fcb-brand);
}
.fcb-sat-thanks {
    font-size: 13px;
    color: #22c55e;
    font-weight: 600;
    padding: 4px 0;
}

/* ---------- Back-to-top offset ---------- */
body:has(#fcb-widget) .back-to-top { right: 100px; }

/* ---------- Mobile responsive ---------- */
@media (max-width: 768px) {
    .fcb-trigger {
        bottom: 16px; right: 16px;
        width: 50px; height: 50px;
    }
    .fcb-trigger svg { width: 22px; height: 22px; }
    .fcb-window {
        bottom: 80px; right: 8px; left: 8px;
        width: auto; max-width: none;
    }
    .fcb-header { padding: 10px 10px; gap: 8px; }
    .fcb-header-actions { gap: 2px; flex-wrap: nowrap; }
    .fcb-header-btn { width: 26px; height: 26px; }
    .fcb-header-btn svg { width: 12px; height: 12px; }
    .fcb-avatar { width: 30px; height: 30px; }
    .fcb-avatar svg { width: 16px; height: 16px; }
    .fcb-header-name { font-size: 13px; }
}
@media (max-width: 480px) {
    .fcb-window {
        bottom: 0; right: 0; left: 0;
        width: 100vw; max-width: 100vw;
        height: 100dvh; max-height: 100dvh;
        border-radius: 0;
        margin: 0; padding: 0;
        box-sizing: border-box;
    }
    /* Header compact : minimize bien visible */
    .fcb-header { padding: 10px 12px; gap: 8px; }
    .fcb-header-actions { gap: 4px; flex-wrap: nowrap; }
    #fcb-sound, #fcb-darkmode, #fcb-search-btn, #fcb-export, #fcb-clear, #fcb-maximize {
        display: none;
    }
    #fcb-minimize {
        width: 34px; height: 34px;
        background: rgba(255,255,255,.25);
        border-radius: 50%;
    }
    #fcb-minimize svg {
        width: 16px; height: 16px;
    }
    /* Hide trigger when chat open */
    .fcb-trigger.fcb-open {
        display: none;
    }
}
