/* AI Agent Widget — Gemini-style floating panel */

/* ── Root container ─────────────────────────────────────────────────────── */
#sf-agent-widget {
    position: fixed;
    /* Fixed 60px bottom + iPhone safe-area (home-indicator / notch) */
    bottom: calc(60px + env(safe-area-inset-bottom, 0px));
    left: 50%;
    transform: translateX(-50%);
    z-index: 9999;
    font-family: "Google Sans", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none; /* let clicks through gaps */
}

#sf-agent-widget * {
    pointer-events: auto;
    box-sizing: border-box;
}

/* ── Hard-reset Bootstrap interference on ALL widget inputs ─────────────── */
#sf-agent-widget input[type="text"],
#sf-agent-widget input[type="text"]:focus,
#sf-agent-widget input[type="text"]:active,
#sf-agent-widget textarea,
#sf-agent-widget textarea:focus,
#sf-agent-widget textarea:active {
    border: none !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    outline: none !important;
    background: transparent !important;
    padding: 0 !important;
    margin: 0 !important;
    line-height: 1.4 !important;
    color: #3c4043 !important;
    font-size: 14px !important;
    font-family: inherit !important;
    display: block !important;
    width: 100% !important;
}

/* inputs need height:auto to override Bootstrap's fixed heights */
#sf-agent-widget input[type="text"],
#sf-agent-widget input[type="text"]:focus,
#sf-agent-widget input[type="text"]:active {
    height: auto !important;
}

#sf-agent-widget input[type="text"]::placeholder,
#sf-agent-widget textarea::placeholder {
    color: #80868b !important;
}

/* Textarea: height is managed entirely by autoResize() in JS — no !important override */
#sf-agent-widget textarea {
    resize: none !important;
    overflow: hidden !important;
    max-height: 120px !important;
    height: 1.4em; /* initial single-line height, JS takes over on input */
}

/* ── Collapsed pill ─────────────────────────────────────────────────────── */
#sf-agent-pill {
    display: flex;
    align-items: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.78);
    -webkit-backdrop-filter: blur(14px) saturate(1.4);
    backdrop-filter: blur(14px) saturate(1.4);
    border-radius: 50px;
    padding: 8px 10px;
    box-shadow: 0 2px 14px rgba(0,0,0,.16), 0 1px 4px rgba(0,0,0,.10);
    cursor: text;
    transition: width 0.28s cubic-bezier(.4,0,.2,1),
                padding 0.28s cubic-bezier(.4,0,.2,1),
                box-shadow 0.28s,
                opacity 0.28s;
    width: 46px;   /* 26px logo + 10px padding each side */
    opacity: 0.70; /* subtle transparency when minimised */
    overflow: hidden;
    white-space: nowrap;
}

#sf-agent-pill:hover,
#sf-agent-pill:focus-within {
    width: min(680px, calc(100vw - 24px));
    padding: 8px 14px;
    box-shadow: 0 4px 18px rgba(0,0,0,.22);
    opacity: 1;
}

/* Logo icon */
.sf-agent-logo {
    flex-shrink: 0;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    object-fit: contain;
    display: block;
}

@keyframes sf-pulse {
    0%, 100% { opacity: 1; }
    50%       { opacity: .7; }
}

/* Pill input wrapper — fills remaining space */
.sf-agent-pill-input-wrap {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

/* Context badge */
#sf-agent-ctx-badge {
    flex-shrink: 0;
    font-size: 11px;
    color: #5f6368;
    background: #f1f3f4;
    border-radius: 10px;
    padding: 2px 8px;
    max-width: 120px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    opacity: 0;
    transition: opacity 0.2s;
    display: none;
}

#sf-agent-pill:hover #sf-agent-ctx-badge,
#sf-agent-pill:focus-within #sf-agent-ctx-badge {
    opacity: 1;
    display: block;
}

/* No badge when empty */
#sf-agent-ctx-badge:empty {
    display: none !important;
}

/* Pill send button */
#sf-agent-pill-send {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #e8f0fe;
    border: none !important;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s, background 0.15s;
    padding: 0 !important;
    box-shadow: none !important;
    outline: none !important;
    line-height: 1 !important;
}

#sf-agent-pill:hover #sf-agent-pill-send,
#sf-agent-pill:focus-within #sf-agent-pill-send {
    opacity: 1;
}

#sf-agent-pill-send:hover {
    background: #d2e3fc !important;
}

#sf-agent-pill-send svg {
    width: 15px;
    height: 15px;
    color: #1a73e8;
    display: block;
}

/* ── Main card (working + response) ─────────────────────────────────────── */
#sf-agent-card {
    display: none;
    flex-direction: column;
    width: min(700px, calc(100vw - 16px));
    /* dvh keeps the card inside the visible viewport on mobile browsers that
       shrink/grow the chrome; falls back to vh on older engines. */
    max-height: min(60vh, calc(100dvh - 160px));
    background: rgba(255, 255, 255, 0.88);
    -webkit-backdrop-filter: blur(16px) saturate(1.5);
    backdrop-filter: blur(16px) saturate(1.5);
    border-radius: 16px;
    box-shadow: 0 4px 28px rgba(0,0,0,.16), 0 1px 6px rgba(0,0,0,.08);
    margin-bottom: 10px;
    /* overflow:clip clips visually (keeps border-radius) but does NOT create a
       block formatting context, so nested overflow-x:auto scroll containers
       work correctly. */
    overflow: clip;
    animation: sf-card-in 0.22s cubic-bezier(.4,0,.2,1);
}

@keyframes sf-card-in {
    from { opacity: 0; transform: translateY(12px) scale(.97); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

#sf-agent-card.sf-visible {
    display: flex;
}

/* Card header */
.sf-agent-card-header {
    display: flex;
    align-items: center;
    padding: 12px 16px 4px;
    gap: 8px;
    flex-shrink: 0;
}

.sf-agent-card-header .sf-agent-logo {
    width: 26px;
    height: 26px;
    animation: none;
}

/* Header logo pulses when agent is working */
.sf-agent-card-header .sf-agent-logo.sf-logo-pulse {
    animation: sf-shimmer 1.4s ease-in-out infinite;
}

.sf-agent-card-header-actions {
    margin-left: auto;
    display: flex;
    gap: 2px;
    align-items: center;
}

/* Status text next to header logo */
.sf-agent-status-text {
    font-size: 12px;
    font-weight: 500;
    padding: 2px 8px;
    border-radius: 10px;
    flex-shrink: 0;
    transition: opacity 0.2s;
}

.sf-agent-status-text:empty {
    display: none;
}

.sf-agent-status-text.sf-status-working {
    color: #1a73e8;
    background: #e8f0fe;
}

/* Session status badge in card header */
#sf-agent-session-status {
    font-size: 11px;
    font-weight: 500;
    border-radius: 10px;
    padding: 2px 8px;
    margin-left: 4px;
    display: inline-flex;
    align-items: center;
    gap: 5px;
    transition: opacity 0.2s;
}

#sf-agent-session-status:empty {
    display: none;
}

#sf-agent-session-status::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
}

.sf-status-working {
    color: #1a73e8;
    background: #e8f0fe;
}

.sf-status-working::before {
    background: #1a73e8;
    animation: sf-dot-pulse 1.2s ease-in-out infinite;
}

.sf-status-awaiting {
    color: #137333;
    background: #e6f4ea;
}

.sf-status-awaiting::before {
    background: #34a853;
}

@keyframes sf-dot-pulse {
    0%, 100% { opacity: 1; }
    50%       { opacity: .25; }
}

.sf-agent-icon-btn {
    width: 30px;
    height: 30px;
    border: none !important;
    background: transparent !important;
    border-radius: 50% !important;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #5f6368;
    transition: background 0.15s;
    padding: 0 !important;
    box-shadow: none !important;
    outline: none !important;
    line-height: 1 !important;
}

.sf-agent-icon-btn:hover {
    background: #f1f3f4 !important;
    color: #202124;
}

.sf-agent-icon-btn svg {
    width: 17px;
    height: 17px;
    display: block;
}

@keyframes sf-shimmer {
    0%, 100% { opacity: 1; }
    50%       { opacity: .25; }
}

/* Stop button — compact, lives inside card header actions */
.sf-agent-stop-btn {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: #1a73e8;
    border: none !important;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s;
    padding: 0 !important;
    box-shadow: none !important;
    outline: none !important;
    line-height: 1 !important;
}

.sf-agent-stop-btn:hover {
    background: #1557b0 !important;
}

.sf-agent-stop-btn svg {
    width: 10px;
    height: 10px;
    color: #fff;
    display: block;
}

@keyframes sf-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Messages area */
.sf-agent-messages {
    flex: 1;
    overflow-y: auto;
    padding: 8px 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    min-height: 0;
}

.sf-agent-messages::-webkit-scrollbar { width: 5px; }
.sf-agent-messages::-webkit-scrollbar-thumb {
    background: #dadce0;
    border-radius: 3px;
}

/* Message bubbles */
.sf-msg {
    display: flex;
    flex-direction: column;
    gap: 4px;
    animation: sf-fade-in 0.18s ease;
}

.sf-msg-user      { align-items: flex-end; }
.sf-msg-assistant { align-items: flex-start; }

.sf-msg-bubble {
    max-width: 86%;
    padding: 9px 13px;
    border-radius: 12px;
    line-height: 1.55;
    word-wrap: break-word;
    font-size: 13.5px;
}

.sf-msg-user .sf-msg-bubble {
    background: #e8f0fe;
    color: #1a1a2e;
    border-bottom-right-radius: 4px;
}

.sf-msg-assistant .sf-msg-bubble {
    background: #f8f9fa;
    color: #202124;
    border-bottom-left-radius: 4px;
    width: 100%;    /* fill container so scroll wrapper has a defined width */
    max-width: 100%;
    box-sizing: border-box;
}

/* Links inside bubbles */
.sf-msg-bubble a {
    color: #1a73e8;
    text-decoration: none;
}
.sf-msg-bubble a:hover {
    text-decoration: underline;
}

/* Markdown inside bubbles */
.sf-msg-bubble p          { margin: 0 0 7px; }
.sf-msg-bubble p:last-child { margin-bottom: 0; }
.sf-msg-bubble ul,
.sf-msg-bubble ol         { margin: 4px 0; padding-left: 18px; }
.sf-msg-bubble li         { margin: 2px 0; }
.sf-msg-bubble strong     { font-weight: 600; }
.sf-msg-bubble code {
    background: #e8eaed;
    border-radius: 3px;
    padding: 1px 4px;
    font-family: "Roboto Mono", monospace;
    font-size: 12px;
}
.sf-msg-bubble pre {
    background: #f1f3f4;
    border-radius: 6px;
    padding: 9px 11px;
    overflow-x: auto;
    margin: 6px 0;
}
.sf-msg-bubble pre code { background: none; padding: 0; }

/* Tables in markdown output — wrapped in .sf-table-wrap by JS for reliable scroll */
.sf-table-wrap {
    display: block;
    width: 100%;          /* explicit width gives the scroll container a reference */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 6px 0;
    box-sizing: border-box;
}

.sf-msg-bubble table {
    border-collapse: collapse;
    font-size: 12.5px;
    /* width and table-layout are set per-table by sizeTable() in JS */
}

.sf-msg-bubble th,
.sf-msg-bubble td {
    border: 1px solid #e0e0e0;
    padding: 5px 9px;
    text-align: left;
    vertical-align: top;
    white-space: nowrap;
    overflow: hidden;         /* with table-layout:fixed, clip rather than bleed */
    text-overflow: ellipsis;  /* safety fallback if JS underestimates a column */
}

.sf-msg-bubble th {
    background: #f1f3f4;
    font-weight: 600;
}

.sf-msg-bubble tr:nth-child(even) td {
    background: #fafafa;
}

/* Message timestamp */
.sf-msg-time {
    font-size: 11px;
    color: #9aa0a6;
    padding: 0 4px;
}

.sf-msg-user .sf-msg-time      { text-align: right; }
.sf-msg-assistant .sf-msg-time { text-align: left; }

/* Typing indicator */
.sf-typing-dots {
    display: flex;
    gap: 4px;
    padding: 11px 13px;
    background: #f8f9fa;
    border-radius: 12px;
    border-bottom-left-radius: 4px;
    width: fit-content;
}

.sf-typing-dots span {
    width: 6px;
    height: 6px;
    background: #80868b;
    border-radius: 50%;
    animation: sf-bounce 1.2s ease infinite;
}
.sf-typing-dots span:nth-child(2) { animation-delay: .15s; }
.sf-typing-dots span:nth-child(3) { animation-delay: .3s; }

@keyframes sf-bounce {
    0%, 60%, 100% { transform: translateY(0); }
    30%           { transform: translateY(-6px); }
}

/* Divider */
.sf-agent-card-divider {
    height: 1px;
    background: #e0e0e0;
    flex-shrink: 0;
}

/* Card footer */
.sf-agent-card-footer {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    flex-shrink: 0;
}

/* Context badge in card footer — always visible, hidden when empty.
   Uses !important throughout to prevent Bootstrap from overriding the pill shape. */
#sf-agent-card-ctx-badge {
    display: inline-block !important;
    flex-shrink: 0;
    font-size: 11px !important;
    font-weight: 400 !important;
    line-height: 1.4 !important;
    color: #5f6368 !important;
    background: #f1f3f4 !important;
    border-radius: 10px !important;
    padding: 2px 8px !important;
    margin: 0 !important;
    border: none !important;
    box-shadow: none !important;
    max-width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#sf-agent-card-ctx-badge:empty {
    display: none !important;
}

/* Footer session-management buttons */
#sf-agent-sessions,
#sf-agent-new {
    flex-shrink: 0;
}

/* New-conversation button — blue tint to mark it as the primary session action */
#sf-agent-new {
    color: #1a73e8 !important;
}

#sf-agent-sessions:disabled,
#sf-agent-new:disabled {
    opacity: .35;
    pointer-events: none;
}

/* Tooltip bubble — injected by JS as position:fixed so it is never clipped */
.sf-agent-tooltip {
    position: fixed;
    background: #3c4043;
    color: #fff;
    font-size: 11px;
    white-space: nowrap;
    padding: 4px 8px;
    border-radius: 4px;
    pointer-events: none;
    z-index: 10001;
    opacity: 0;
    transition: opacity 0.15s;
}

.sf-agent-card-footer textarea {
    flex: 1 !important;
    width: auto !important;
    align-self: center;
}

.sf-agent-footer-send {
    flex-shrink: 0;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #e8f0fe;
    border: none !important;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, opacity 0.15s;
    opacity: .4;
    padding: 0 !important;
    box-shadow: none !important;
    outline: none !important;
    line-height: 1 !important;
}

.sf-agent-footer-send.active { opacity: 1; }
.sf-agent-footer-send.active:hover { background: #d2e3fc !important; }

.sf-agent-footer-send svg {
    width: 14px;
    height: 14px;
    color: #1a73e8;
    display: block;
}

/* ── Init loading state — prevents interaction before first AJAX resolves ── */
#sf-agent-widget.sf-agent-initializing #sf-agent-pill {
    opacity: 0.55;
    pointer-events: none;
}

/* ── Running indicator: pulsing blue ring on pill when card is minimized ──── */
@keyframes sf-pill-running {
    0%, 100% { box-shadow: 0 2px 14px rgba(0,0,0,.16), 0 1px 4px rgba(0,0,0,.10),
                           0 0 0 1.5px rgba(26,115,232,.45); }
    50%       { box-shadow: 0 2px 14px rgba(0,0,0,.16), 0 1px 4px rgba(0,0,0,.10),
                           0 0 0 3px   rgba(26,115,232,.80); }
}

#sf-agent-widget.sf-pill-running:not(.sf-card-open) #sf-agent-pill {
    opacity: 0.90;
    animation: sf-pill-running 1.6s ease-in-out infinite;
}

/* ── Sessions home panel ─────────────────────────────────────────────────── */
#sf-agent-sessions-home {
    flex: 1;
    overflow-y: auto;
    padding: 8px 16px 12px;
    min-height: 0;
    display: none;
}

#sf-agent-sessions-home::-webkit-scrollbar { width: 5px; }
#sf-agent-sessions-home::-webkit-scrollbar-thumb {
    background: #dadce0;
    border-radius: 3px;
}

.sf-sessions-header {
    font-size: 11px;
    font-weight: 600;
    color: #80868b;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 4px 0 8px;
}

.sf-session-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 9px 12px;
    border-radius: 10px;
    cursor: pointer;
    border: 1px solid #e8eaed;
    margin-bottom: 6px;
    transition: background 0.15s, border-color 0.15s;
}

.sf-session-item:hover {
    background: #f1f3f4;
    border-color: #dadce0;
}

.sf-session-item-title {
    font-size: 13px;
    font-weight: 500;
    color: #202124;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sf-session-item-meta {
    font-size: 11px;
    color: #80868b;
}

.sf-sessions-empty {
    font-size: 13px;
    color: #9aa0a6;
    text-align: center;
    padding: 28px 0 12px;
}

.sf-sessions-loading {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 28px 0;
    justify-content: center;
    color: #80868b;
    font-size: 13px;
}

.sf-sessions-spinner {
    width: 18px;
    height: 18px;
    border: 2px solid #e8eaed;
    border-top-color: #1a73e8;
    border-radius: 50%;
    animation: sf-spin 0.75s linear infinite;
    flex-shrink: 0;
}

@keyframes sf-spin {
    to { transform: rotate(360deg); }
}

/* ── Sidebar / right-column mode ─────────────────────────────────────────── */

body.sf-agent-sidebar .content-wrapper {
    margin-right: 360px;
    transition: margin-right 0.25s ease;
}


/* The 360px sidebar shrinks the content pane — stack two-column Bootstrap grids
   on viewports where the remaining content width would be too narrow.
   At ≥1400px the content area is still wide enough for two columns. */
@media (max-width: 1600px) {
    body.sf-agent-sidebar .content-wrapper .col-xl-6,
    body.sf-agent-sidebar .content-wrapper .col-lg-6,
    body.sf-agent-sidebar .content-wrapper .col-md-6 {
        -ms-flex: 0 0 100% !important;
        flex: 0 0 100% !important;
        max-width: 100% !important;
    }
}

body.sf-agent-sidebar #sf-agent-widget {
    position: fixed;
    right: 0;
    top: calc(3.5rem + 1px); /* below AdminLTE navbar */
    bottom: 0;
    left: auto;
    transform: none;
    width: 360px;
    z-index: 1039;
    align-items: stretch;
}

body.sf-agent-sidebar #sf-agent-pill {
    display: none !important;
}

body.sf-agent-sidebar #sf-agent-card {
    display: flex !important;
    width: 100%;
    max-height: none;
    height: 100%;
    border-radius: 0;
    margin-bottom: 0;
    border-left: 1px solid #dee2e6;
    box-shadow: -2px 0 12px rgba(0,0,0,.08);
    background: rgba(255,255,255,0.98);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
    animation: none;
    overflow: hidden;
    padding-bottom: 50px;
}

/* On narrow viewports revert everything so the floating widget takes over */
@media (max-width: 991px) {
    #sf-agent-dock { display: none !important; }

    body.sf-agent-sidebar .content-wrapper { margin-right: 0; }

    body.sf-agent-sidebar #sf-agent-widget {
        right: auto;
        left: 50%;
        top: auto;
        bottom: calc(60px + env(safe-area-inset-bottom, 0px));
        transform: translateX(-50%);
        width: auto;
        align-items: center;
    }

    body.sf-agent-sidebar #sf-agent-pill { display: flex !important; }

    body.sf-agent-sidebar #sf-agent-card {
        display: none;
        width: min(700px, calc(100vw - 16px));
        max-height: min(60vh, calc(100dvh - 160px));
        height: auto;
        border-radius: 16px;
        margin-bottom: 10px;
        border-left: none;
        box-shadow: 0 4px 28px rgba(0,0,0,.16), 0 1px 6px rgba(0,0,0,.08);
        animation: sf-card-in 0.22s cubic-bezier(.4,0,.2,1);
    }

    body.sf-agent-sidebar #sf-agent-card.sf-visible { display: flex; }
}

/* ── Mobile layout fixes ─────────────────────────────────────────────────── */
@media (max-width: 768px) {
    /* Narrower card — 16 px margin on each side */
    #sf-agent-card {
        width: calc(100vw - 32px);
        /* Tighter max-height so the card never runs off the top of the screen.
         * dvh (dynamic viewport height) tracks the visible area on iOS/Android
         * Safari where the chrome shrinks/grows as the user scrolls. */
        max-height: calc(100dvh - 160px);
    }

    /* Narrower pill when expanded — matches card width */
    #sf-agent-pill:hover,
    #sf-agent-pill:focus-within {
        width: calc(100vw - 32px);
    }

    /* Root widget keeps the fixed 60 px bottom set globally; no dynamic
     * Zendesk-based overrides.  env(safe-area-inset-bottom) already handles
     * iPhone home-indicator clearance. */
}
