/* Stan AI Assistant — Bottom bar chat
   Pattern: persistent input at bottom, conversation panel slides up above. */

:root {
    --sai-bg: #ffffff;
    --sai-bg-elevated: #fafaf9;
    --sai-bg-bar: rgba(255, 255, 255, 0.92);
    --sai-border: #e7e5e4;
    --sai-border-strong: #d6d3d1;
    --sai-text: #1c1917;
    --sai-text-muted: #57534e;
    --sai-text-faint: #a8a29e;
    --sai-accent: #0f172a;
    --sai-accent-hover: #1e293b;
    --sai-user-bg: #0f172a;
    --sai-user-text: #ffffff;
    --sai-assistant-bg: #f5f5f4;
    --sai-radius: 14px;
    --sai-radius-sm: 10px;
    --sai-shadow: 0 1px 2px rgba(0,0,0,0.04), 0 8px 24px rgba(15,23,42,0.08);
    --sai-shadow-bar: 0 -2px 16px rgba(15,23,42,0.06), 0 -1px 0 rgba(15,23,42,0.04);
    --sai-font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
    --sai-blur: saturate(180%) blur(20px);
}

@media (prefers-color-scheme: dark) {
    :root {
        --sai-bg: #1c1917;
        --sai-bg-elevated: #292524;
        --sai-bg-bar: rgba(28, 25, 23, 0.92);
        --sai-border: #44403c;
        --sai-border-strong: #57534e;
        --sai-text: #fafaf9;
        --sai-text-muted: #d6d3d1;
        --sai-text-faint: #78716c;
        --sai-accent: #fafaf9;
        --sai-accent-hover: #e7e5e4;
        --sai-user-bg: #fafaf9;
        --sai-user-text: #1c1917;
        --sai-assistant-bg: #292524;
        --sai-shadow-bar: 0 -2px 16px rgba(0,0,0,0.4), 0 -1px 0 rgba(255,255,255,0.04);
    }
}

/* ============================================================
   BOTTOM BAR (always visible at page bottom)
   ============================================================ */

.sai-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999990;
    padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
    background: var(--sai-bg-bar);
    -webkit-backdrop-filter: var(--sai-blur);
    backdrop-filter: var(--sai-blur);
    border-top: 1px solid var(--sai-border);
    box-shadow: var(--sai-shadow-bar);
    font-family: var(--sai-font);
    transition: transform 0.24s cubic-bezier(0.32, 0.72, 0, 1);
}

.sai-bar.is-hidden {
    transform: translateY(110%);
}

.sai-bar-inner {
    max-width: 760px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--sai-bg);
    border: 1px solid var(--sai-border);
    border-radius: var(--sai-radius);
    padding: 8px 8px 8px 14px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.03);
    transition: border-color 0.16s ease, box-shadow 0.16s ease;
}
.sai-bar-inner:focus-within {
    border-color: var(--sai-border-strong);
    box-shadow: 0 0 0 4px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(0,0,0,0.03);
}
@media (prefers-color-scheme: dark) {
    .sai-bar-inner:focus-within {
        box-shadow: 0 0 0 4px rgba(250, 250, 249, 0.08), 0 1px 2px rgba(0,0,0,0.3);
    }
}

.sai-bar-input {
    flex: 1;
    border: none;
    outline: none;
    background: transparent;
    color: var(--sai-text);
    font-family: inherit;
    font-size: 15px;
    line-height: 1.5;
    resize: none;
    min-height: 28px;
    max-height: 140px;
    padding: 6px 0;
    overflow-y: auto;
}
.sai-bar-input::placeholder {
    color: var(--sai-text-faint);
}

.sai-bar-send {
    width: 36px;
    height: 36px;
    flex-shrink: 0;
    border-radius: var(--sai-radius-sm);
    background: var(--sai-accent);
    color: #ffffff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.12s ease, transform 0.08s ease, opacity 0.12s ease;
}
.sai-bar-send:hover { background: var(--sai-accent-hover); }
.sai-bar-send:active { transform: scale(0.94); }
.sai-bar-send:disabled {
    background: var(--sai-border-strong);
    cursor: not-allowed;
    opacity: 0.6;
}
.sai-bar-send svg {
    width: 16px;
    height: 16px;
    stroke-width: 2;
}
@media (prefers-color-scheme: dark) {
    .sai-bar-send { color: var(--sai-user-text); }
}

.sai-bar-hint {
    max-width: 760px;
    margin: 0 auto 6px;
    font-size: 11px;
    color: var(--sai-text-faint);
    text-align: center;
    letter-spacing: 0.02em;
    opacity: 0.8;
    transition: opacity 0.2s ease;
}
.sai-bar.is-conversation-open .sai-bar-hint {
    display: none;
}

/* ============================================================
   CONVERSATION PANEL (slides up when conversation starts)
   ============================================================ */

.sai-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 999989;
    padding-bottom: calc(82px + env(safe-area-inset-bottom));
    height: 75vh;
    max-height: 720px;
    background: var(--sai-bg);
    border-top: 1px solid var(--sai-border);
    box-shadow: 0 -8px 32px rgba(15,23,42,0.12);
    display: flex;
    flex-direction: column;
    pointer-events: none;
    opacity: 0;
    transform: translateY(20px);
    transition: opacity 0.24s ease, transform 0.24s cubic-bezier(0.32, 0.72, 0, 1);
    font-family: var(--sai-font);
    color: var(--sai-text);
}
.sai-panel.is-open {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}

.sai-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 20px;
    border-bottom: 1px solid var(--sai-border);
    max-width: 760px;
    width: 100%;
    margin: 0 auto;
    flex-shrink: 0;
    box-sizing: border-box;
}
.sai-panel-title {
    font-size: 14px;
    font-weight: 600;
    letter-spacing: -0.01em;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--sai-text);
}
.sai-panel-title::before {
    content: '';
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 999px;
    background: #10b981;
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.18);
}
.sai-panel-actions {
    display: flex;
    gap: 4px;
}
.sai-icon-btn {
    width: 30px;
    height: 30px;
    border-radius: 8px;
    background: transparent;
    border: none;
    color: var(--sai-text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.12s ease, color 0.12s ease;
}
.sai-icon-btn:hover {
    background: var(--sai-bg-elevated);
    color: var(--sai-text);
}
.sai-icon-btn svg {
    width: 16px;
    height: 16px;
    stroke-width: 2;
}

/* ============================================================
   MESSAGES
   ============================================================ */

.sai-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 16px;
    scrollbar-width: thin;
    scrollbar-color: var(--sai-border-strong) transparent;
}
.sai-messages::-webkit-scrollbar { width: 6px; }
.sai-messages::-webkit-scrollbar-thumb {
    background: var(--sai-border-strong);
    border-radius: 999px;
}
.sai-messages::-webkit-scrollbar-track { background: transparent; }

.sai-messages-inner {
    max-width: 760px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.sai-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: var(--sai-radius-sm);
    font-size: 14.5px;
    line-height: 1.55;
    word-wrap: break-word;
    animation: sai-msg-in 0.22s ease-out;
}
@keyframes sai-msg-in {
    from { opacity: 0; transform: translateY(4px); }
    to   { opacity: 1; transform: translateY(0); }
}
.sai-msg-user {
    background: var(--sai-user-bg);
    color: var(--sai-user-text);
    align-self: flex-end;
    border-bottom-right-radius: 2px;
}
.sai-msg-assistant {
    background: var(--sai-assistant-bg);
    color: var(--sai-text);
    align-self: flex-start;
    border-bottom-left-radius: 2px;
}
.sai-msg-assistant p { margin: 0 0 8px 0; }
.sai-msg-assistant p:last-child { margin-bottom: 0; }
.sai-msg-assistant a {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 2px;
}
.sai-msg-assistant code {
    background: rgba(0,0,0,0.06);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 0.9em;
}
@media (prefers-color-scheme: dark) {
    .sai-msg-assistant code { background: rgba(255,255,255,0.08); }
}

.sai-msg-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    align-self: flex-start;
    font-size: 13px;
}
@media (prefers-color-scheme: dark) {
    .sai-msg-error {
        background: rgba(127, 29, 29, 0.2);
        border-color: rgba(220, 38, 38, 0.4);
        color: #fecaca;
    }
}

.sai-typing {
    align-self: flex-start;
    background: var(--sai-assistant-bg);
    padding: 12px 16px;
    border-radius: var(--sai-radius-sm);
    border-bottom-left-radius: 2px;
    display: flex;
    gap: 4px;
}
.sai-typing span {
    width: 6px;
    height: 6px;
    border-radius: 999px;
    background: var(--sai-text-faint);
    animation: sai-bounce 1.2s infinite ease-in-out;
}
.sai-typing span:nth-child(2) { animation-delay: 0.15s; }
.sai-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes sai-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30%           { transform: translateY(-3px); opacity: 1; }
}

/* ============================================================
   SOURCES
   ============================================================ */

.sai-sources {
    align-self: flex-start;
    max-width: 100%;
    width: 100%;
    padding: 12px 14px;
    background: var(--sai-bg-elevated);
    border: 1px solid var(--sai-border);
    border-radius: var(--sai-radius-sm);
    font-size: 12px;
    color: var(--sai-text-muted);
}
.sai-sources-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--sai-text-faint);
    margin-bottom: 8px;
    font-weight: 600;
}
.sai-source-link {
    display: block;
    padding: 6px 0;
    color: var(--sai-text);
    text-decoration: none;
    transition: color 0.12s ease;
    border-bottom: 1px solid var(--sai-border);
    font-size: 13px;
}
.sai-source-link:last-child { border-bottom: none; }
.sai-source-link:hover { color: var(--sai-accent); text-decoration: underline; }
.sai-source-num {
    display: inline-block;
    min-width: 18px;
    font-variant-numeric: tabular-nums;
    color: var(--sai-text-faint);
    font-size: 11px;
    margin-right: 6px;
}

/* ============================================================
   EMBED MODE (the [stan_ai_chat] shortcode - inline)
   ============================================================ */

.sai-embed {
    display: block;
    border: 1px solid var(--sai-border);
    border-radius: var(--sai-radius);
    background: var(--sai-bg);
    box-shadow: var(--sai-shadow);
    overflow: hidden;
    font-family: var(--sai-font);
    color: var(--sai-text);
}
.sai-embed-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--sai-border);
}
.sai-embed-title {
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 8px;
}
.sai-embed-title::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 999px;
    background: #10b981;
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.18);
}
.sai-embed-messages {
    height: 480px;
    overflow-y: auto;
    padding: 20px 16px;
    scrollbar-width: thin;
    scrollbar-color: var(--sai-border-strong) transparent;
}
.sai-embed-messages-inner {
    max-width: 720px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 14px;
}
.sai-embed-input-row {
    border-top: 1px solid var(--sai-border);
    padding: 12px 14px;
    background: var(--sai-bg);
    display: flex;
    gap: 8px;
    align-items: flex-end;
}
.sai-embed-input {
    flex: 1;
    resize: none;
    border: 1px solid var(--sai-border);
    background: var(--sai-bg);
    color: var(--sai-text);
    border-radius: var(--sai-radius-sm);
    padding: 10px 14px;
    font-family: inherit;
    font-size: 14.5px;
    line-height: 1.5;
    min-height: 42px;
    max-height: 140px;
    outline: none;
    transition: border-color 0.12s ease, box-shadow 0.12s ease;
}
.sai-embed-input:focus {
    border-color: var(--sai-accent);
    box-shadow: 0 0 0 3px rgba(15, 23, 42, 0.08);
}

/* ============================================================
   MOBILE
   ============================================================ */
@media (max-width: 640px) {
    .sai-bar {
        padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
    }
    .sai-bar-inner {
        padding: 6px 6px 6px 12px;
        border-radius: 12px;
    }
    .sai-bar-input { font-size: 16px; /* prevent iOS zoom */ }
    .sai-panel {
        height: 72vh;
        padding-bottom: calc(72px + env(safe-area-inset-bottom));
    }
    .sai-bar-hint { font-size: 10px; }
    .sai-embed-messages { height: 380px; }
    .sai-embed-input { font-size: 16px; }
}

@media (prefers-reduced-motion: reduce) {
    .sai-bar,
    .sai-panel,
    .sai-msg {
        transition: none;
        animation: none;
    }
}
