/* A.L.E.X. chat widget (markup: templates/base.html, logic: static/js/chatbot.js).
   Colours are variables on #alex-root; html.dark swaps them, so the whole widget follows the site's dark mode. */

#alex-root {
    --a-brand: #801423;
    --a-brand-2: #9b1a2f;
    --a-brand-dark: #5a0d18;
    --a-gold: #f5b50a;
    --a-bg: #faf6f5;
    --a-surface: #ffffff;
    --a-surface-2: #f4eeed;
    --a-border: #eadedc;
    --a-text: #292524;
    --a-muted: #78716c;
    --a-link: #801423;
    --a-code: #f1e9e8;
    --a-shadow: 0 28px 64px -14px rgba(60, 10, 20, .38), 0 10px 24px rgba(0, 0, 0, .12);
    font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
html.dark #alex-root {
    --a-bg: #0f172a;
    --a-surface: #1e293b;
    --a-surface-2: #273449;
    --a-border: #334155;
    --a-text: #e2e8f0;
    --a-muted: #94a3b8;
    --a-link: #fca5a5;
    --a-code: #0b1220;
    --a-shadow: 0 28px 64px -14px rgba(0, 0, 0, .7), 0 10px 24px rgba(0, 0, 0, .4);
}

/* ---------- floating button ---------- */
#chat-toggle-btn {
    position: fixed;
    right: 24px;
    bottom: 24px;
    z-index: 9999;
    width: 62px;
    height: 62px;
    padding: 0;
    border-radius: 50%;
    border: 3px solid var(--a-gold);
    background: linear-gradient(135deg, var(--a-brand-2), var(--a-brand-dark));
    box-shadow: 0 14px 30px rgba(90, 13, 24, .45);
    cursor: pointer;
    display: grid;
    place-items: center;
    transition: transform .2s ease, box-shadow .2s ease;
}
#chat-toggle-btn:hover { transform: translateY(-2px) scale(1.06); box-shadow: 0 18px 36px rgba(90, 13, 24, .5); }
#chat-toggle-btn:active { transform: scale(.96); }
#chat-toggle-btn:focus-visible { outline: 3px solid #fff; outline-offset: 3px; box-shadow: 0 0 0 7px var(--a-brand); }
.alex-fab-img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; display: block; background: #fff; }
.alex-fab-pulse {
    position: absolute;
    inset: -5px;
    border-radius: 50%;
    border: 2px solid rgba(245, 181, 10, .65);
    animation: alex-pulse 2.4s ease-out infinite;
    pointer-events: none;
}
.alex-fab-dot {
    position: absolute;
    top: -3px;
    right: -3px;
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #fff;
    box-shadow: 0 0 0 0 rgba(34, 197, 94, .6);
    animation: alex-dot 2s ease-out infinite;
}
.alex-fab-dot[hidden] { display: none; }

/* small "Ask A.L.E.X." greeting that appears once per visit */
.alex-fab-label {
    position: fixed;
    right: 98px;
    bottom: 34px;
    z-index: 9999;
    max-width: 220px;
    padding: 10px 14px;
    border: 1px solid var(--a-border);
    border-radius: 16px 16px 4px 16px;
    background: var(--a-surface);
    color: var(--a-text);
    box-shadow: 0 10px 26px rgba(0, 0, 0, .16);
    font-family: inherit;
    font-size: 13px;
    font-weight: 800;
    line-height: 1.25;
    text-align: left;
    cursor: pointer;
    opacity: 0;
    transform: translateX(10px);
    pointer-events: none;
    transition: opacity .3s ease, transform .3s ease;
}
.alex-fab-label span { display: block; margin-top: 2px; font-weight: 500; font-size: 11.5px; color: var(--a-muted); }
.alex-fab-label.is-shown { opacity: 1; transform: none; pointer-events: auto; }
.alex-fab-label[hidden] { display: none; }

/* ---------- window ---------- */
#chat-window {
    position: fixed;
    right: 24px;
    bottom: 98px;
    z-index: 9998;
    width: 392px;
    max-width: calc(100vw - 32px);
    height: min(640px, calc(100vh - 130px));
    height: min(640px, calc(100dvh - 130px));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--a-bg);
    color: var(--a-text);
    border: 1px solid var(--a-border);
    border-radius: 24px;
    box-shadow: var(--a-shadow);
    opacity: 0;
    visibility: hidden;                        /* closed: not clickable and not reachable with Tab */
    transform: translateY(16px) scale(.96);
    transform-origin: bottom right;
    transition: opacity .22s ease, transform .28s cubic-bezier(.2, .8, .2, 1), visibility 0s linear .28s;
}
#chat-window.is-open { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }

.alex-head {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 14px 16px 16px;
    color: #fff;
    background: linear-gradient(135deg, #951b30 0%, #801423 48%, #5a0d18 100%);
}
.alex-head::after {
    content: "";
    position: absolute;
    left: 0; right: 0; bottom: 0;
    height: 3px;
    background: linear-gradient(90deg, var(--a-gold), rgba(245, 181, 10, 0) 72%);
}
.alex-head-id { display: flex; align-items: center; gap: 12px; min-width: 0; }
.alex-head h2 { margin: 0; font-size: 16px; font-weight: 800; letter-spacing: -.01em; line-height: 1.15; color: #fff; }
.alex-head p { margin: 3px 0 0; font-size: 11px; font-weight: 500; opacity: .88; color: #fff; }
.alex-head-actions { display: flex; gap: 8px; flex: none; }
.alex-head-actions button {
    width: 32px; height: 32px;
    display: grid; place-items: center;
    border: 0; border-radius: 50%;
    background: rgba(255, 255, 255, .16);
    color: #fff;
    cursor: pointer;
    transition: background .2s ease, transform .2s ease;
}
.alex-head-actions button:hover { background: rgba(255, 255, 255, .3); }
.alex-head-actions button:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.alex-head-actions #alex-new:hover i { transform: rotate(-180deg); }
.alex-head-actions i { transition: transform .35s ease; font-size: 13px; }

.alex-avatar {
    position: relative;
    flex: none;
    width: 32px; height: 32px;
    margin-top: 2px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, .18);
}
.alex-avatar img { display: block; width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.alex-avatar-lg { width: 44px; height: 44px; margin: 0; border: 2px solid rgba(255, 255, 255, .9); }
.alex-online {
    position: absolute;
    right: -2px; bottom: -2px;
    width: 12px; height: 12px;
    border-radius: 50%;
    background: #22c55e;
    border: 2px solid #801423;
}

/* ---------- conversation ---------- */
#chat-messages {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 14px;
    padding: 16px 14px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    background: radial-gradient(circle at top left, rgba(128, 20, 35, .07), transparent 46%), var(--a-bg);
    scrollbar-width: thin;
    scrollbar-color: rgba(128, 20, 35, .35) transparent;
}
#chat-messages::-webkit-scrollbar { width: 6px; }
#chat-messages::-webkit-scrollbar-thumb { background: rgba(128, 20, 35, .3); border-radius: 6px; }

.alex-row { display: flex; align-items: flex-start; gap: 8px; max-width: 100%; animation: alex-in .3s ease both; }
.alex-row-user { justify-content: flex-end; }
.alex-col { display: flex; flex-direction: column; min-width: 0; max-width: calc(100% - 42px); }
.alex-row-user .alex-col { align-items: flex-end; max-width: 86%; }

.alex-bubble {
    min-width: 0;
    padding: 11px 14px;
    border-radius: 18px;
    font-size: 13.5px;
    line-height: 1.55;
    overflow-wrap: anywhere;                   /* long web addresses wrap instead of pushing the bubble wider */
    word-break: break-word;
}
.alex-bubble-bot {
    background: var(--a-surface);
    color: var(--a-text);
    border: 1px solid var(--a-border);
    border-top-left-radius: 5px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .05);
}
.alex-bubble-user {
    color: #fff;
    background: linear-gradient(135deg, var(--a-brand-2), var(--a-brand));
    border-bottom-right-radius: 5px;
    box-shadow: 0 6px 14px rgba(128, 20, 35, .28);
}
.alex-time { margin: 3px 5px 0; font-size: 10px; font-weight: 500; color: var(--a-muted); }

.alex-notice { margin: -4px 8px 0; font-size: 10.5px; line-height: 1.55; text-align: center; color: var(--a-muted); }
.alex-notice a { color: var(--a-link); font-weight: 700; text-decoration: underline; }
.alex-chips-label { margin: 2px 4px -6px; font-size: 10px; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; color: var(--a-muted); }

.suggested-chips-container { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-btn {
    padding: 7px 13px;
    border: 1px solid var(--a-border);
    border-radius: 999px;
    background: var(--a-surface);
    color: var(--a-text);
    font-family: inherit;
    font-size: 12px;
    font-weight: 600;
    line-height: 1.2;
    cursor: pointer;
    transition: background .2s ease, color .2s ease, border-color .2s ease, transform .2s ease, box-shadow .2s ease;
}
.chip-btn:hover { background: var(--a-brand); border-color: var(--a-brand); color: #fff; transform: translateY(-1px); box-shadow: 0 6px 14px rgba(128, 20, 35, .25); }
.chip-btn:focus-visible { outline: 2px solid var(--a-brand); outline-offset: 2px; }

/* three bouncing dots while A.L.E.X. is answering */
.alex-typing .alex-bubble { padding: 13px 15px; }
.alex-typing-dots { display: inline-flex; align-items: center; gap: 5px; }
.alex-typing-dots span { width: 7px; height: 7px; border-radius: 50%; background: var(--a-brand); animation: alex-dot-bounce 1.2s infinite ease-in-out; }
html.dark .alex-typing-dots span { background: #fca5a5; }
.alex-typing-dots span:nth-child(2) { animation-delay: .15s; }
.alex-typing-dots span:nth-child(3) { animation-delay: .3s; }

.alex-retry {
    margin-top: 10px;
    padding: 6px 12px;
    border: 1px solid var(--a-brand);
    border-radius: 999px;
    background: transparent;
    color: var(--a-link);
    font-family: inherit;
    font-size: 11.5px;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
}
.alex-retry:hover { background: var(--a-brand); color: #fff; }

/* ---------- bot answers (markdown) ---------- */
.bot-response > :first-child { margin-top: 0; }
.bot-response > :last-child { margin-bottom: 0; }
.bot-response p { margin: 0 0 .55rem; }
.bot-response h1, .bot-response h2, .bot-response h3, .bot-response h4, .bot-response h5, .bot-response h6 {
    margin: .8rem 0 .35rem; font-size: .92rem; font-weight: 800; color: var(--a-link); line-height: 1.3;
}
.bot-response ul, .bot-response ol { margin: 0 0 .55rem; padding-left: 1.25rem; }
.bot-response ul { list-style: disc; }
.bot-response ol { list-style: decimal; }
.bot-response li { margin-bottom: .25rem; padding-left: .1rem; }
.bot-response li::marker { color: var(--a-link); }
.bot-response strong, .bot-response b { font-weight: 700; color: inherit; }
.bot-response a { color: var(--a-link); font-weight: 600; text-decoration: underline; text-underline-offset: 2px; }
.bot-response code { padding: 1px 6px; border-radius: 6px; background: var(--a-code); font: 500 .85em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.bot-response pre { margin: 0 0 .55rem; padding: 10px 12px; overflow-x: auto; border-radius: 10px; background: var(--a-code); }
.bot-response pre code { padding: 0; background: none; }
.bot-response blockquote { margin: 0 0 .55rem; padding: 2px 0 2px 12px; border-left: 3px solid var(--a-gold); color: var(--a-muted); }
.bot-response hr { margin: .7rem 0; border: 0; border-top: 1px solid var(--a-border); }
.bot-response table { display: block; max-width: 100%; margin: 0 0 .55rem; overflow-x: auto; border-collapse: collapse; font-size: .92em; }
.bot-response th, .bot-response td { padding: 5px 9px; border: 1px solid var(--a-border); text-align: left; }
.bot-response th { background: var(--a-surface-2); font-weight: 700; }

/* "open this page" buttons under an answer */
.bot-links { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 11px; padding-top: 10px; border-top: 1px dashed var(--a-border); }
.bot-link-btn {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    max-width: 100%;
    padding: 6px 12px;
    border-radius: 999px;
    background: linear-gradient(135deg, var(--a-brand-2), var(--a-brand));
    color: #fff !important;
    font-size: 11.5px;
    font-weight: 700;
    text-decoration: none !important;
    box-shadow: 0 3px 8px rgba(128, 20, 35, .25);
    transition: transform .2s ease, box-shadow .2s ease;
}
.bot-link-btn span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bot-link-btn i { font-size: 10px; opacity: .85; }
.bot-link-btn:hover { transform: translateY(-1px); box-shadow: 0 7px 14px rgba(128, 20, 35, .32); }

/* ---------- input ---------- */
.alex-foot { padding: 11px 12px 9px; background: var(--a-surface); border-top: 1px solid var(--a-border); }
.alex-form {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 4px 4px 15px;
    border: 1px solid var(--a-border);
    border-radius: 999px;
    background: var(--a-surface-2);
    transition: border-color .2s ease, box-shadow .2s ease, background .2s ease;
}
.alex-form:focus-within { border-color: var(--a-brand); background: var(--a-surface); box-shadow: 0 0 0 3px rgba(128, 20, 35, .16); }
html.dark .alex-form:focus-within { border-color: #fca5a5; box-shadow: 0 0 0 3px rgba(252, 165, 165, .2); }
#chat-input {
    flex: 1;
    min-width: 0;
    padding: 9px 0;
    border: 0;
    outline: 0;
    background: transparent;
    color: var(--a-text);
    font-family: inherit;
    font-size: 13.5px;
    font-weight: 500;
    line-height: 1.3;
}
#chat-input::placeholder { color: var(--a-muted); opacity: .8; }
.alex-count { display: none; flex: none; font-size: 10.5px; font-weight: 600; color: var(--a-muted); }
.alex-count.is-on { display: block; }
.alex-count.is-max { color: #dc2626; }
#chat-send-btn {
    flex: none;
    width: 38px; height: 38px;
    display: grid; place-items: center;
    border: 0; border-radius: 50%;
    background: linear-gradient(135deg, var(--a-brand-2), var(--a-brand));
    color: #fff;
    cursor: pointer;
    box-shadow: 0 5px 12px rgba(128, 20, 35, .3);
    transition: transform .15s ease, opacity .2s ease, box-shadow .2s ease;
}
#chat-send-btn:hover:not(:disabled) { transform: scale(1.07); }
#chat-send-btn:active:not(:disabled) { transform: scale(.93); }
#chat-send-btn:disabled { opacity: .4; cursor: not-allowed; box-shadow: none; }
#chat-send-btn:focus-visible { outline: 2px solid var(--a-brand); outline-offset: 2px; }
#chat-send-btn i { font-size: 13px; margin-left: -1px; }
.alex-disclaimer { margin: 8px 0 0; text-align: center; font-size: 10px; color: var(--a-muted); }

.alex-sr { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ---------- phones: a nearly full-width sheet above the button ---------- */
@media (max-width: 640px) {
    #chat-toggle-btn { right: 16px; bottom: 16px; width: 58px; height: 58px; }
    #chat-window {
        right: 12px; left: 12px;
        width: auto; max-width: none;
        bottom: 86px;
        height: min(680px, calc(100vh - 106px));
        height: min(680px, calc(100dvh - 106px));
        border-radius: 22px;
    }
    .alex-fab-label { display: none; }
}

@media print { #alex-root { display: none !important; } }

@keyframes alex-pulse { 0% { transform: scale(.95); opacity: .9; } 100% { transform: scale(1.4); opacity: 0; } }
@keyframes alex-dot { 0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, .6); } 70%, 100% { box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); } }
@keyframes alex-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes alex-dot-bounce { 0%, 80%, 100% { opacity: .35; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-4px); } }

@media (prefers-reduced-motion: reduce) {
    #alex-root *, #alex-root *::before, #alex-root *::after { animation: none !important; transition-duration: .01ms !important; scroll-behavior: auto !important; }
    #chat-window { transform: none; }
}
