/*
 * Support chat widget (launcher + panel). Built by /frontend/js/support-chat.js inside the
 * root printed by resources/views/support-chat/widget.blade.php.
 * Tokens: values from docs/design-system/tokens.css, scoped to the widget because the live
 * pages do not load tokens.css. The widget is always light, in the light and the dark site theme
 * (owner decision 2026-11-26).
 * The launcher sits on the start side (right in RTL); the back-to-top button uses the other corner.
 * It moves up by --course-chat-clearance (course page mobile bar, set by course-detail.js) or by
 * --fs-chat-bar (other fixed bottom bars, measured by support-chat.js), and respects the safe area.
 */

/* ---------- tokens ---------- */
.fs-chat {
    --bg: #f3f6f8;
    --s1: #fff;
    --s2: #e9eff3;
    --line: #d9e2e9;
    --text: #0f1c26;
    --text2: #33434f;
    --muted: #5a6a78;
    --green: #0e8a43;
    --green-d: #e1f5e9;
    --on-green: #fff;
    --pink: #e41a4b;
    --err: color-mix(in srgb, #e81c4d 68%, var(--text));
    --font: peyda, Vazirmatn, system-ui, sans-serif;
    --mono: "JetBrains Mono", ui-monospace, Consolas, monospace;
    --r: 18px;
    --r-md: 12px;
    --r-btn: 14px;
    --sh-panel: 0 24px 50px -22px rgba(15, 28, 38, .32), 0 2px 10px rgba(15, 28, 38, .08);
    --sh-launcher: 0 12px 26px -10px color-mix(in srgb, var(--green) 60%, transparent), 0 2px 6px rgba(15, 28, 38, .16);
    --ring-focus: 0 0 0 4px color-mix(in srgb, var(--green) 20%, transparent);
    --dur-1: .2s;
    --dur-2: .25s;
    --dur-3: .35s;
    --ease: cubic-bezier(.2, .8, .2, 1);

    --fs-chat-edge: 20px;
    --fs-chat-size: 60px;
    --fs-chat-bottom: calc(max(env(safe-area-inset-bottom, 0px), var(--course-chat-clearance, 0px), var(--fs-chat-bar, 0px)) + var(--fs-chat-edge));
    --fs-chat-panel-bottom: calc(var(--fs-chat-bottom) + var(--fs-chat-size) + 14px);

    font-family: var(--font);
    font-size: 14.5px;
    line-height: 1.9;
    color: var(--text);
    color-scheme: light;
    direction: rtl;
    text-align: start;
}

/* ---------- resets against the legacy page styles (Bootstrap, main.css) ---------- */
.fs-chat[hidden],
.fs-chat [hidden] { display: none !important; }
.fs-chat,
.fs-chat *,
.fs-chat *::before,
.fs-chat *::after { box-sizing: border-box; }
.fs-chat :where(h2, p, ul, ol, li, form) { margin: 0; padding: 0; }
.fs-chat :where(button, input, textarea) { font: inherit; letter-spacing: normal; text-transform: none; }
.fs-chat :where(button) { cursor: pointer; }
.fs-chat :where(button:disabled) { cursor: not-allowed; }
.fs-chat :where(svg) { vertical-align: middle; }
.fs-chat :focus-visible { outline: 2px solid var(--green); outline-offset: 3px; }
.fs-chat-sr {
    position: absolute !important;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}
.fs-chat-i {
    width: 20px;
    height: 20px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ---------- launcher ---------- */
.fs-chat-launcher {
    position: fixed;
    z-index: 40;
    inset-inline-start: var(--fs-chat-edge);
    inset-block-end: var(--fs-chat-bottom);
    width: var(--fs-chat-size);
    height: var(--fs-chat-size);
    padding: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 50%;
    background: var(--green);
    color: var(--on-green);
    box-shadow: var(--sh-launcher);
    transition: transform var(--dur-1) var(--ease), inset-block-end var(--dur-3) var(--ease);
    animation: fs-chat-wake .6s cubic-bezier(.34, 1.56, .64, 1) .3s both;
}
.fs-chat-launcher:hover { transform: translateY(-2px); }
.fs-chat-launcher .fs-chat-i { width: 30px; height: 30px; grid-area: 1 / 1; transition: opacity var(--dur-1), transform var(--dur-2) var(--ease); }
.fs-chat-launcher .fs-chat-i-down { opacity: 0; transform: rotate(-90deg) scale(.6); }
.fs-chat-launcher[aria-expanded="true"] .fs-chat-i-chat { opacity: 0; transform: rotate(90deg) scale(.6); }
.fs-chat-launcher[aria-expanded="true"] .fs-chat-i-down { opacity: 1; transform: none; }
.fs-chat-badge {
    position: absolute;
    inset-block-start: -3px;
    inset-inline-end: -3px;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 999px;
    border: 2px solid var(--bg);
    background: var(--pink);
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    animation: fs-chat-pop var(--dur-3) var(--ease) both;
}

/* ---------- greeting card above the launcher ---------- */
.fs-chat-dotfill { fill: currentColor; stroke: none; }
.fs-chat-teaser {
    position: fixed;
    z-index: 40;
    inset-inline-start: var(--fs-chat-edge);
    inset-block-end: calc(var(--fs-chat-bottom) + var(--fs-chat-size) + 12px);
    width: min(300px, calc(100vw - 2 * var(--fs-chat-edge)));
    border: 1px solid var(--line);
    border-radius: var(--r-md);
    background: var(--s1);
    box-shadow: var(--sh-panel);
    transition: inset-block-end var(--dur-3) var(--ease);
    animation: fs-chat-slide .45s var(--ease) both;
}
.fs-chat[data-open] .fs-chat-teaser { display: none; }
.fs-chat-teaser-open {
    display: block;
    width: 100%;
    padding: 12px 14px 13px;
    border: 0;
    border-radius: inherit;
    background: none;
    color: var(--text2);
    text-align: start;
}
.fs-chat-teaser-head { display: flex; align-items: center; gap: 8px; margin-block-end: 6px; padding-inline-end: 28px; }
.fs-chat-teaser-avatar {
    width: 30px;
    height: 30px;
    flex: none;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: var(--green-d);
    color: var(--green);
}
.fs-chat-teaser-avatar .fs-chat-i { width: 17px; height: 17px; }
.fs-chat-teaser-name { font-size: 14px; font-weight: 700; line-height: 1.5; color: var(--green); }
.fs-chat-teaser-text { display: block; font-size: 13.5px; line-height: 1.85; }
.fs-chat-teaser-x {
    position: absolute;
    inset-block-start: 10px;
    inset-inline-end: 8px;
    width: 28px;
    height: 28px;
    padding: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 50%;
    background: none;
    color: var(--muted);
}
.fs-chat-teaser-x .fs-chat-i { width: 16px; height: 16px; }
.fs-chat-teaser-x:hover { background: var(--s2); color: var(--text); }

/* ---------- panel ---------- */
.fs-chat-panel {
    position: fixed;
    z-index: 41;
    inset-inline-start: var(--fs-chat-edge);
    inset-block-end: var(--fs-chat-panel-bottom);
    width: min(384px, calc(100vw - 2 * var(--fs-chat-edge)));
    height: min(620px, calc(100vh - var(--fs-chat-panel-bottom) - var(--fs-chat-edge)));
    height: min(620px, calc(100dvh - var(--fs-chat-panel-bottom) - var(--fs-chat-edge)));
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--s1);
    border: 1px solid var(--line);
    border-radius: var(--r);
    box-shadow: var(--sh-panel);
    transform-origin: right bottom;
    animation: fs-chat-in var(--dur-3) var(--ease) both;
}
.fs-chat-panel:focus { outline: none; }

.fs-chat-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 16px;
    border-block-end: 1px solid var(--line);
    background: var(--s1);
}
.fs-chat-avatar {
    width: 40px;
    height: 40px;
    flex: none;
    display: grid;
    place-items: center;
    border-radius: var(--r-md);
    background: var(--green-d);
    color: var(--green);
}
.fs-chat-avatar .fs-chat-i { grid-area: 1 / 1; width: 22px; height: 22px; }
.fs-chat:not([data-mode="operator"]) .fs-chat-avatar .fs-chat-i-op,
.fs-chat[data-mode="operator"] .fs-chat-avatar .fs-chat-i-ai { display: none; }
.fs-chat-heading { flex: 1; min-width: 0; }
.fs-chat-title {
    font-size: 16px;
    font-weight: 700;
    line-height: 1.4;
    color: var(--text);
}
.fs-chat-mode {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 12.5px;
    line-height: 1.6;
    color: var(--muted);
}
.fs-chat-dot {
    width: 7px;
    height: 7px;
    flex: none;
    border-radius: 50%;
    background: var(--green);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--green) 22%, transparent);
}
.fs-chat-close {
    width: 38px;
    height: 38px;
    flex: none;
    padding: 0;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: none;
    color: var(--text);
    transition: border-color var(--dur-1), color var(--dur-1);
}
.fs-chat-close:hover { border-color: var(--green); color: var(--green); }
.fs-chat-close .fs-chat-i { width: 18px; height: 18px; }

/* ---------- messages ---------- */
.fs-chat-log {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 18px 16px 12px;
    background: var(--bg);
    scrollbar-width: thin;
    scrollbar-color: var(--line) transparent;
}
.fs-chat-log:focus-visible { outline-offset: -3px; }
.fs-chat-log::-webkit-scrollbar { width: 6px; }
.fs-chat-log::-webkit-scrollbar-track { background: transparent; }
.fs-chat-log::-webkit-scrollbar-thumb { background: var(--line); border-radius: 6px; }

.fs-chat-msg {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    max-width: 86%;
    align-self: flex-start;
    animation: fs-chat-rise var(--dur-2) var(--ease) both;
}
.fs-chat-msg--visitor { align-self: flex-end; align-items: flex-end; }
.fs-chat-msg.is-cont { margin-block-start: -5px; }
.fs-chat-msg.is-cont .fs-chat-who { display: none; }
.fs-chat-who {
    display: flex;
    align-items: center;
    gap: 6px;
    padding-inline: 4px;
    font-size: 12px;
    font-weight: 500;
    line-height: 1.5;
    color: var(--muted);
}
.fs-chat-msg--operator .fs-chat-who { color: var(--text2); }
.fs-chat-msg--operator .fs-chat-who::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--green);
}
.fs-chat-bubble {
    max-width: 100%;
    padding: 9px 14px;
    border: 1px solid var(--line);
    border-radius: 16px;
    border-start-start-radius: 6px;
    background: var(--s1);
    color: var(--text);
    font-size: 14.5px;
    line-height: 1.85;
    overflow-wrap: anywhere;
    user-select: text;
    -webkit-user-select: text;
}
.fs-chat-msg--visitor .fs-chat-bubble {
    border-radius: 16px;
    border-start-end-radius: 6px;
    border-color: color-mix(in srgb, var(--green) 26%, transparent);
    background: var(--green-d);
    white-space: pre-wrap;
}
.fs-chat-msg--operator .fs-chat-bubble { border-color: color-mix(in srgb, var(--green) 40%, var(--line)); }
.fs-chat-bubble :where(p + p, p + ul, p + ol, ul + p, ol + p, ul + ul, ol + ol) { margin-block-start: 6px; }
.fs-chat-bubble :is(ul, ol) { padding: 0; padding-inline-start: 20px; }
.fs-chat-bubble ul { list-style: disc; }
.fs-chat-bubble ol { list-style: decimal; list-style: persian; }
.fs-chat-bubble li + li { margin-block-start: 2px; }
.fs-chat-bubble li::marker { color: var(--muted); }
.fs-chat-bubble strong { font-weight: 600; color: var(--text); }
.fs-chat-bubble a {
    color: var(--green);
    font-weight: 500;
    text-decoration: underline !important;
    text-decoration-thickness: 1px !important;
    text-underline-offset: 4px;
    overflow-wrap: anywhere;
}
.fs-chat-bubble a:hover { text-decoration-thickness: 2px !important; }
.fs-chat-bubble code {
    padding: 0 5px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--bg);
    font-family: var(--mono);
    font-size: .86em;
    direction: ltr;
    unicode-bidi: isolate;
}

.fs-chat-notice {
    align-self: center;
    max-width: 92%;
    margin-block: 2px;
    padding: 4px 12px;
    border-radius: var(--r-md);
    background: color-mix(in srgb, var(--s2) 75%, transparent);
    color: var(--muted);
    font-size: 12.5px;
    line-height: 1.8;
    text-align: center;
    animation: fs-chat-rise var(--dur-2) var(--ease) both;
}

.fs-chat-typing { display: inline-flex; align-items: center; gap: 5px; padding: 15px 16px; }
.fs-chat-typing i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--muted);
    animation: fs-chat-dots 1.2s ease-in-out infinite;
}
.fs-chat-typing i:nth-child(2) { animation-delay: .15s; }
.fs-chat-typing i:nth-child(3) { animation-delay: .3s; }

.fs-chat-loading {
    align-self: center;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    color: var(--muted);
    font-size: 13px;
}
.fs-chat-loading::before {
    content: "";
    width: 14px;
    height: 14px;
    border: 2px solid var(--line);
    border-block-start-color: var(--green);
    border-radius: 50%;
    animation: fs-chat-spin .8s linear infinite;
}

/* ---------- footer: phone step, composer, closed state ---------- */
.fs-chat-foot {
    padding: 10px 12px 12px;
    border-block-start: 1px solid var(--line);
    background: var(--s1);
}
.fs-chat-alert {
    display: flex;
    align-items: flex-start;
    gap: 6px;
    padding: 0 4px 8px;
    color: var(--err);
    font-size: 13px;
    line-height: 1.8;
}
.fs-chat-alert .fs-chat-i { width: 16px; height: 16px; margin-block-start: 4px; }

.fs-chat-start { display: grid; gap: 8px; padding: 2px 4px 0; }
.fs-chat-label { font-size: 13.5px; font-weight: 500; line-height: 1.7; color: var(--text2); }
.fs-chat-row { display: flex; gap: 8px; }
.fs-chat-inp {
    flex: 1;
    min-width: 0;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: var(--r-btn);
    background: var(--bg);
    color: var(--text);
    font-size: 16px;
    line-height: 1.5;
    font-variant-numeric: tabular-nums;
    outline: 0;
    user-select: text;
    -webkit-user-select: text;
    transition: border-color var(--dur-1), box-shadow var(--dur-1);
}
.fs-chat-inp::placeholder { color: var(--muted); opacity: .7; }
.fs-chat-inp:hover { border-color: color-mix(in srgb, var(--green) 40%, var(--line)); }
.fs-chat-inp:focus-visible { outline: 0; border-color: var(--green); box-shadow: var(--ring-focus); }
.fs-chat-inp[aria-invalid="true"] { border-color: var(--err); }
.fs-chat-error { font-size: 13px; line-height: 1.8; color: var(--err); }

.fs-chat-btn {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 10px 18px;
    border: 0;
    border-radius: var(--r-btn);
    background: var(--green);
    color: var(--on-green);
    font-size: 15px;
    font-weight: 700;
    line-height: 1.5;
    transition: transform var(--dur-1) var(--ease), opacity var(--dur-1);
}
.fs-chat-btn:hover:not(:disabled) { transform: translateY(-1px); }
.fs-chat-btn:disabled { opacity: .55; }

.fs-chat-compose {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 5px;
    padding-inline-start: 8px;
    border: 1px solid var(--line);
    border-radius: var(--r-btn);
    background: var(--bg);
    transition: border-color var(--dur-1), box-shadow var(--dur-1);
}
.fs-chat-compose:hover { border-color: color-mix(in srgb, var(--green) 40%, var(--line)); }
.fs-chat-compose:focus-within { border-color: var(--green); box-shadow: var(--ring-focus); }
.fs-chat-input {
    flex: 1;
    min-width: 0;
    height: 38px;
    max-height: 132px;
    padding: 6px 4px;
    border: 0;
    background: transparent;
    color: var(--text);
    font-size: 16px;
    line-height: 1.6;
    resize: none;
    outline: 0;
    overflow-y: auto;
    user-select: text;
    -webkit-user-select: text;
}
.fs-chat-input:focus-visible { outline: 0; }
/* dir="auto" follows what is typed; while empty it would fall back to LTR, so keep the placeholder and caret RTL. */
.fs-chat-input:placeholder-shown { direction: rtl; text-align: right; }
.fs-chat-input::placeholder { color: var(--muted); opacity: .8; }
.fs-chat-send {
    width: 40px;
    height: 40px;
    flex: none;
    padding: 0;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 11px;
    background: var(--green);
    color: var(--on-green);
    transition: opacity var(--dur-1), transform var(--dur-1) var(--ease);
}
.fs-chat-send:hover:not(:disabled) { transform: translateY(-1px); }
.fs-chat-send:disabled { opacity: .4; }
.fs-chat-send .fs-chat-i { width: 19px; height: 19px; transform: scaleX(-1); }
.fs-chat-count {
    padding: 6px 4px 0;
    color: var(--muted);
    font-size: 12px;
    line-height: 1.5;
    text-align: end;
    font-variant-numeric: tabular-nums;
}
.fs-chat-count.is-full { color: var(--err); }

.fs-chat-closed {
    display: grid;
    justify-items: center;
    gap: 10px;
    padding: 4px 4px 2px;
    text-align: center;
}
.fs-chat-closed p { color: var(--text2); font-size: 13.5px; line-height: 1.8; }

/* ---------- phones and short screens: full-screen panel ---------- */
@media (max-width: 560px), (max-height: 520px) {
    .fs-chat { --fs-chat-edge: 16px; }
    .fs-chat-panel {
        inset: var(--fs-chat-top, 0px) 0 auto 0;
        width: 100%;
        height: 100vh;
        height: var(--fs-chat-vh, 100dvh);
        border: 0;
        border-radius: 0;
        box-shadow: none;
        animation-name: fs-chat-up;
    }
    .fs-chat-head { padding-block-start: max(12px, env(safe-area-inset-top)); }
    .fs-chat-foot { padding-block-end: max(12px, env(safe-area-inset-bottom)); }
    .fs-chat[data-open] .fs-chat-launcher { display: none; }
    .fs-chat-msg { max-width: 90%; }
    html.fs-chat-locked,
    html.fs-chat-locked body { overflow: hidden !important; }
}

/* ---------- motion ---------- */
@keyframes fs-chat-in { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
@keyframes fs-chat-up { from { opacity: 0; transform: translateY(24px); } to { opacity: 1; transform: none; } }
@keyframes fs-chat-pop { from { opacity: 0; transform: scale(.7); } to { opacity: 1; transform: none; } }
@keyframes fs-chat-wake { from { opacity: 0; transform: scale(.3); } to { opacity: 1; transform: none; } }
@keyframes fs-chat-slide { from { opacity: 0; transform: translateX(28px); } to { opacity: 1; transform: none; } }
@keyframes fs-chat-rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes fs-chat-dots { 0%, 60%, 100% { opacity: .35; transform: none; } 30% { opacity: 1; transform: translateY(-3px); } }
@keyframes fs-chat-spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
    .fs-chat *,
    .fs-chat *::before,
    .fs-chat *::after { animation: none !important; transition: none !important; }
}
