/* ============================================================================
   FinRiskLens AI Assistant (Views/Shared/_Chatbot.cshtml)
   A side panel about a third of the desktop width; full screen on phones.
   Theme tokens match the dashboard maroon palette.
   ========================================================================== */

.frl-chatbot {
    --frl-maroon: var(--brand-primary);
    --frl-maroon-dark: var(--brand-primary-hover);
    --frl-maroon-soft: var(--brand-tint-bg);
    --frl-border: var(--brand-tint-border);
    position: fixed;
    right: 22px;
    bottom: 8px;
    z-index: 1080;
    font-family: inherit;
}

/* ── Launcher — the robot mascot GIF floating free, no button chrome ──── */
.frl-chat-launcher {
    position: relative;
    width: 117px;
    height: 106px;
    border: none;
    padding: 0;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform .2s ease;
}
.frl-chat-launcher:hover { transform: translateY(-2px) scale(1.05); }
.frl-chat-launcher:focus-visible { outline: 3px solid var(--frl-maroon); outline-offset: 4px; border-radius: 50%; }
.frl-chat-launcher-img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; }
/* One robot GIF per theme (data-theme on <html>, set by theme.js): theme1 green, theme2 maroon */
.frl-chat-launcher-img.frl-bot-theme2 { display: none; }
:root[data-theme="theme2"] .frl-chat-launcher-img.frl-bot-theme1 { display: none; }
:root[data-theme="theme2"] .frl-chat-launcher-img.frl-bot-theme2 { display: block; }
/* The maroon GIF has more empty space around its robot (about 66% of the height vs 73%), so it is
   drawn 10% larger to look the same size as the green one */
.frl-chat-launcher > .frl-chat-launcher-img.frl-bot-theme2 { transform: scale(1.1); }
.frl-chatbot.open .frl-chat-launcher { display: none; }

/* ── Panel ───────────────────────────────────────────────────────────── */
.frl-chat-panel {
    position: fixed;
    top: 14px;
    right: 14px;
    bottom: 14px;
    width: clamp(380px, 33vw, 620px);
    background: #fff;
    border: 1px solid var(--frl-border);
    border-radius: 18px;
    box-shadow: 0 24px 60px rgba(0, 0, 0, .18);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    transform: translateX(24px);
    pointer-events: none;
    transition: opacity .25s ease, transform .25s ease;
}
.frl-chatbot.open .frl-chat-panel { opacity: 1; transform: translateX(0); pointer-events: auto; }

/* Expanded: most of the workspace, centred, with a small margin; the page stays visible around it */
.frl-chatbot.expanded .frl-chat-panel {
    width: min(88vw, 1500px);
    right: calc((100vw - min(88vw, 1500px)) / 2);
    top: 3vh;
    bottom: 3vh;
}
.frl-chatbot.expanded.open::before {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(30, 10, 18, .28);
    pointer-events: none;
}
.frl-chatbot.expanded .frl-chat-messages { padding: 22px max(22px, calc((100% - 980px) / 2)); }
.frl-chatbot.expanded .frl-chat-suggestions,
.frl-chatbot.expanded .frl-chat-input,
.frl-chatbot.expanded .frl-chat-foot { padding-left: max(16px, calc((100% - 980px) / 2)); padding-right: max(16px, calc((100% - 980px) / 2)); }

/* ── Header ──────────────────────────────────────────────────────────── */
.frl-chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    background: linear-gradient(120deg, var(--brand-primary-dark) 0%, var(--frl-maroon) 100%);
    color: #fff;
}
.frl-chat-avatar {
    width: 38px; height: 38px; flex: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, .18);
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 1.15rem;
}
.frl-chat-head-text { flex: 1; min-width: 0; }
.frl-chat-title { font-size: 1rem; font-weight: 700; margin: 0; line-height: 1.2; color: #fff; }
.frl-chat-status { display: flex; align-items: center; gap: 6px; font-size: .75rem; opacity: .9; }
.frl-status-dot { width: 8px; height: 8px; border-radius: 50%; background: #3ddc84; }
.frl-chat-status.is-busy .frl-status-dot { background: #ffd166; animation: frl-pulse 1s ease-in-out infinite; }
.frl-chat-status.is-error .frl-status-dot { background: #ff6b6b; }
@keyframes frl-pulse { 50% { opacity: .35; } }
.frl-chat-icon {
    border: none; background: rgba(255, 255, 255, .14); color: #fff;
    width: 34px; height: 34px; border-radius: 10px; flex: none;
    display: inline-flex; align-items: center; justify-content: center;
}
.frl-chat-icon:hover { background: rgba(255, 255, 255, .26); }
.frl-chat-icon:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }

.frl-chat-scope {
    font-size: .75rem; color: #6b6b6b;
    background: var(--frl-maroon-soft);
    border-bottom: 1px solid var(--frl-border);
    padding: 6px 16px;
}

/* ── Messages ────────────────────────────────────────────────────────── */
.frl-chat-messages {
    position: relative;                 /* offsetTop of a row is measured from here */
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    background: #fbf8f9;
}
.frl-row { display: flex; gap: 10px; align-items: flex-start; }
.frl-row-user { justify-content: flex-end; }
.frl-row-avatar {
    width: 30px; height: 30px; flex: none; border-radius: 50%;
    background: var(--frl-maroon); color: #fff;
    display: inline-flex; align-items: center; justify-content: center; font-size: .9rem;
}
.frl-msg { font-size: .9rem; line-height: 1.6; border-radius: 14px; padding: 10px 14px; overflow-wrap: anywhere; min-width: 0; }
.frl-msg-user { max-width: 82%; background: var(--frl-maroon); color: #fff; border-bottom-right-radius: 4px; white-space: pre-wrap; }
.frl-msg-bot { flex: 1; background: #fff; border: 1px solid var(--frl-border); color: #2b2b2b; border-top-left-radius: 4px; }
.frl-msg-error { border-color: #f1b8b8; background: #fff6f6; }
.frl-msg-error p { margin: 0 0 6px; }

/* Markdown inside an answer */
.frl-md > :first-child { margin-top: 0; }
.frl-md > :last-child { margin-bottom: 0; }
.frl-md + .frl-md { margin-top: 10px; }
.frl-md p { margin: 0 0 10px; }
.frl-md .frl-h { color: var(--brand-primary-dark); font-weight: 700; line-height: 1.3; margin: 16px 0 8px; }
.frl-md h3.frl-h { font-size: 1rem; padding-bottom: 4px; border-bottom: 1px solid var(--frl-border); }
.frl-md h4.frl-h { font-size: .92rem; }
.frl-md h5.frl-h, .frl-md h6.frl-h { font-size: .86rem; text-transform: uppercase; letter-spacing: .04em; }
.frl-md strong { color: #1f1f1f; font-weight: 700; }
.frl-md em { color: #555; }
.frl-md code { font-size: .82rem; background: var(--frl-maroon-soft); border: 1px solid var(--frl-border); border-radius: 4px; padding: 0 4px; }
.frl-md .frl-pre { white-space: pre-wrap; font-size: .82rem; background: var(--frl-maroon-soft); border-radius: 8px; padding: 8px 10px; }
.frl-md .frl-mdlist { margin: 0 0 10px; padding-left: 22px; }
.frl-md .frl-mdlist li { margin-bottom: 5px; }
.frl-md .frl-mdlist .frl-mdlist { margin: 4px 0 0; }
.frl-md ol.frl-mdlist > li::marker { color: var(--frl-maroon); font-weight: 700; }
.frl-md .frl-quote { margin: 0 0 10px; padding: 6px 12px; border-left: 3px solid var(--frl-maroon); background: var(--frl-maroon-soft); border-radius: 0 8px 8px 0; color: #444; }
.frl-md .frl-hr { border: 0; border-top: 1px solid var(--frl-border); margin: 12px 0; }
.frl-md .frl-table-wrap { overflow-x: auto; margin: 0 0 12px; border: 1px solid var(--frl-border); border-radius: 10px; }
.frl-md .frl-table { width: 100%; border-collapse: collapse; font-size: .84rem; }
.frl-md .frl-table th {
    background: var(--frl-maroon-soft); color: var(--brand-primary-dark); font-weight: 700;
    text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--frl-border); white-space: nowrap;
}
.frl-md .frl-table td { padding: 8px 10px; border-bottom: 1px solid #f1e8eb; vertical-align: top; min-width: 90px; }
.frl-md .frl-table tr:last-child td { border-bottom: 0; }
.frl-md .frl-table tbody tr:nth-child(even) td { background: #fdfafb; }

.frl-retry {
    margin-top: 6px; border: 1px solid var(--frl-maroon); color: var(--frl-maroon);
    background: #fff; border-radius: 8px; padding: 4px 14px; font-weight: 600; font-size: .82rem;
}
.frl-retry:hover { background: var(--frl-maroon); color: #fff; }

.frl-typing { display: inline-flex; gap: 4px; width: auto; }
.frl-typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--frl-maroon); opacity: .4; animation: frl-dot 1.2s infinite; }
.frl-typing span:nth-child(2) { animation-delay: .2s; }
.frl-typing span:nth-child(3) { animation-delay: .4s; }
@keyframes frl-dot { 40% { opacity: 1; transform: translateY(-3px); } }

/* ── Suggestions ─────────────────────────────────────────────────────── */
.frl-chat-suggestions {
    padding: 8px 16px 0; display: flex; flex-wrap: wrap; gap: 6px; background: #fff;
    max-height: 124px; overflow-y: auto;           /* never squeezes the conversation */
    border-top: 1px solid var(--frl-border);
}
.frl-chat-suggestions:empty { display: none; }
.frl-sugg-title { width: 100%; font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em; color: var(--brand-secondary); }
.frl-sugg {
    border: 1px solid var(--frl-border); background: var(--frl-maroon-soft); color: var(--brand-primary-dark);
    border-radius: 999px; padding: 5px 12px; font-size: .8rem; text-align: left;
}
.frl-sugg:hover, .frl-sugg:focus-visible { border-color: var(--frl-maroon); outline: none; }

/* ── Input ───────────────────────────────────────────────────────────── */
.frl-chat-input { display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px 4px; background: #fff; }
.frl-chat-input textarea {
    flex: 1; resize: none; max-height: 140px;
    border: 1px solid var(--frl-border); border-radius: 12px;
    padding: 9px 12px; font-size: .9rem; line-height: 1.4;
}
.frl-chat-input textarea:focus { outline: none; border-color: var(--frl-maroon); box-shadow: 0 0 0 3px rgba(var(--brand-primary-rgb), .12); }
.frl-chat-input button {
    width: 42px; height: 42px; flex: none; border: none; border-radius: 12px;
    background: var(--frl-maroon); color: #fff;
}
.frl-chat-input button:disabled { opacity: .5; }
.frl-chat-foot { font-size: .68rem; color: #8a8a8a; padding: 2px 16px 10px; background: #fff; }

/* ── Phones and small tablets: full screen ───────────────────────────── */
@media (max-width: 767.98px) {
    .frl-chat-panel,
    .frl-chatbot.expanded .frl-chat-panel { top: 0; right: 0; bottom: 0; left: 0; width: auto; border-radius: 0; border: none; }
    .frl-chat-expand { display: none; }                /* already full screen */
    .frl-chatbot.expanded.open::before { display: none; }
    .frl-row-avatar { display: none; }                 /* give answers the full width */
    .frl-msg-user { max-width: 90%; }
    .frl-chatbot { right: 12px; }
    .frl-chat-launcher { width: 80px; height: 80px; }
}

@media (prefers-reduced-motion: reduce) {
    .frl-chat-panel, .frl-chat-launcher { transition: none; }
    .frl-typing span, .frl-chat-status.is-busy .frl-status-dot { animation: none; }
}
