/* =========================================================================
   SBA CHATBOT — Premium Styles v2.0
   CSS Custom Properties for easy theming. Colors injected from PHP admin.
   ========================================================================= */

/* ---- Theme Variables ---- */
:root {
    --sba-chat-primary: #1a1a2e;
    --sba-chat-accent: #e94560;
    --sba-chat-bg: #ffffff;
    --sba-chat-body-bg: #f8f9fb;
    --sba-chat-msg-bot-bg: #ffffff;
    --sba-chat-msg-bot-border: #e8eaef;
    --sba-chat-msg-bot-text: #2d3436;
    --sba-chat-msg-user-text: #ffffff;
    --sba-chat-input-bg: #ffffff;
    --sba-chat-input-border: #dfe3e8;
    --sba-chat-subtle: #8395a7;
    --sba-chat-radius: 16px;
    --sba-chat-shadow: 0 12px 48px rgba(0,0,0,0.15);
    --sba-chat-font: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
}

/* Dark mode */
@media (prefers-color-scheme: dark) {
    :root {
        --sba-chat-bg: #1e1e2e;
        --sba-chat-body-bg: #16161e;
        --sba-chat-msg-bot-bg: #252538;
        --sba-chat-msg-bot-border: #2d2d44;
        --sba-chat-msg-bot-text: #e0e0e0;
        --sba-chat-input-bg: #252538;
        --sba-chat-input-border: #3a3a52;
        --sba-chat-subtle: #8888aa;
        --sba-chat-shadow: 0 12px 48px rgba(0,0,0,0.4);
    }
}

/* ---- Container ---- */
#sba-chatbot {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 99999;
    font-family: var(--sba-chat-font);
}
#sba-chatbot.sba-chatbot-left {
    right: auto;
    left: 24px;
}

/* ---- Toggler Button ---- */
#sba-chatbot-toggler {
    position: relative;
    background: var(--sba-chat-primary);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 60px;
    height: 60px;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 6px 20px rgba(0,0,0,0.2);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: sba-pulse 2.5s infinite;
}
#sba-chatbot-toggler:hover {
    transform: scale(1.08);
    box-shadow: 0 8px 28px rgba(0,0,0,0.25);
}
#sba-chatbot.sba-chat-active #sba-chatbot-toggler {
    animation: none;
}

/* Pulse animation for attention */
@keyframes sba-pulse {
    0%, 100% { box-shadow: 0 6px 20px rgba(0,0,0,0.2), 0 0 0 0 rgba(233,69,96,0.4); }
    50% { box-shadow: 0 6px 20px rgba(0,0,0,0.2), 0 0 0 12px rgba(233,69,96,0); }
}

/* Toggle icon swap */
#sba-chatbot.sba-chat-active .sba-chat-icon-open { display: none !important; }
#sba-chatbot.sba-chat-active .sba-chat-icon-close { display: block !important; }

/* Unread badge */
.sba-unread-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--sba-chat-accent);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    animation: sba-badge-pop 0.3s ease;
}
@keyframes sba-badge-pop {
    0% { transform: scale(0); }
    60% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

/* ---- Chat Window ---- */
#sba-chatbot-window {
    position: absolute;
    bottom: 76px;
    right: 0;
    width: 380px;
    height: 540px;
    background: var(--sba-chat-bg);
    border-radius: var(--sba-chat-radius);
    box-shadow: var(--sba-chat-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px) scale(0.95);
    transition: all 0.35s cubic-bezier(0.165, 0.84, 0.44, 1);
    border: 1px solid rgba(0,0,0,0.06);
}
#sba-chatbot.sba-chatbot-left #sba-chatbot-window {
    right: auto;
    left: 0;
}
#sba-chatbot-window.sba-chatbot-open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* ---- Header (Glassmorphism) ---- */
.sba-chatbot-header {
    background: var(--sba-chat-primary);
    color: #fff;
    padding: 16px 18px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    overflow: hidden;
}
.sba-chatbot-header::before {
    content: '';
    position: absolute;
    top: -50%;
    right: -30%;
    width: 120px;
    height: 120px;
    background: rgba(255,255,255,0.06);
    border-radius: 50%;
}
.sba-chatbot-header::after {
    content: '';
    position: absolute;
    bottom: -60%;
    left: -20%;
    width: 80px;
    height: 80px;
    background: rgba(255,255,255,0.04);
    border-radius: 50%;
}

.sba-chatbot-header-info {
    display: flex;
    align-items: center;
    gap: 12px;
    z-index: 1;
}
.sba-chatbot-header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid rgba(255,255,255,0.2);
}
.sba-chatbot-default-avatar {
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255,255,255,0.15);
    color: #fff;
}
.sba-chatbot-header h4 {
    margin: 0;
    font-size: 17px;
    font-weight: 700;
    letter-spacing: 0.3px;
    color: #ffffff;
    text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}
.sba-chatbot-status {
    font-size: 11px;
    opacity: 0.8;
    display: block;
    margin-top: 1px;
    color: #4ade80;
}

.sba-chatbot-header-actions {
    display: flex;
    gap: 6px;
    z-index: 1;
}
.sba-chatbot-header-actions button {
    background: rgba(255,255,255,0.1);
    border: none;
    color: #fff;
    cursor: pointer;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}
.sba-chatbot-header-actions button:hover {
    background: rgba(255,255,255,0.2);
}

/* ---- Body (Messages) ---- */
.sba-chatbot-body {
    flex: 1;
    padding: 18px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    background: var(--sba-chat-body-bg);
    scroll-behavior: smooth;
}
.sba-chatbot-body::-webkit-scrollbar {
    width: 4px;
}
.sba-chatbot-body::-webkit-scrollbar-track {
    background: transparent;
}
.sba-chatbot-body::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.12);
    border-radius: 4px;
}

/* ---- Messages ---- */
.sba-msg-wrapper {
    display: flex;
    gap: 8px;
    max-width: 88%;
    animation: sba-msg-in 0.3s ease-out;
}
.sba-msg-wrapper.user {
    align-self: flex-end;
    flex-direction: row-reverse;
}
.sba-msg-wrapper.bot {
    align-self: flex-start;
}

@keyframes sba-msg-in {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

.sba-msg-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
    margin-top: 2px;
}
.sba-msg-avatar-default {
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--sba-chat-primary);
    color: #fff;
    font-size: 12px;
}
.sba-msg-avatar-default svg {
    width: 14px;
    height: 14px;
}

.sba-msg-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.sba-msg {
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.55;
    word-wrap: break-word;
    overflow-wrap: break-word;
}
.sba-msg.bot {
    background: var(--sba-chat-msg-bot-bg);
    border: 1px solid var(--sba-chat-msg-bot-border);
    color: var(--sba-chat-msg-bot-text);
    border-top-left-radius: 4px;
}
.sba-msg.user {
    background: var(--sba-chat-primary);
    color: var(--sba-chat-msg-user-text);
    border-top-right-radius: 4px;
}

/* Markdown in bot messages */
.sba-msg.bot strong { font-weight: 600; }
.sba-msg.bot em { font-style: italic; }
.sba-msg.bot a {
    color: var(--sba-chat-accent);
    text-decoration: underline;
    text-decoration-color: transparent;
    transition: text-decoration-color 0.2s;
}
.sba-msg.bot a:hover { text-decoration-color: currentColor; }
.sba-msg.bot code {
    background: rgba(0,0,0,0.06);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: 'Fira Code', 'Consolas', monospace;
}
.sba-msg.bot pre {
    background: rgba(0,0,0,0.06);
    padding: 10px 14px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 6px 0;
    font-size: 13px;
}
.sba-msg.bot pre code {
    background: none;
    padding: 0;
}
.sba-msg.bot ul, .sba-msg.bot ol {
    margin: 6px 0;
    padding-left: 18px;
}
.sba-msg.bot li {
    margin-bottom: 3px;
}

/* Timestamp */
.sba-msg-time {
    font-size: 10px;
    color: var(--sba-chat-subtle);
    padding: 0 4px;
}
.sba-msg-wrapper.user .sba-msg-time {
    text-align: right;
}

/* ---- Typing Indicator ---- */
.sba-typing-wrapper {
    display: flex;
    gap: 8px;
    align-self: flex-start;
    max-width: 88%;
    animation: sba-msg-in 0.3s ease-out;
}
.sba-typing {
    display: flex;
    gap: 5px;
    padding: 14px 18px;
    background: var(--sba-chat-msg-bot-bg);
    border: 1px solid var(--sba-chat-msg-bot-border);
    border-radius: 12px;
    border-top-left-radius: 4px;
    align-self: flex-start;
}
.sba-typing span {
    width: 7px;
    height: 7px;
    background: var(--sba-chat-subtle);
    border-radius: 50%;
    animation: sbaTyping 1.4s infinite ease-in-out both;
}
.sba-typing span:nth-child(1) { animation-delay: -0.32s; }
.sba-typing span:nth-child(2) { animation-delay: -0.16s; }

@keyframes sbaTyping {
    0%, 80%, 100% { transform: scale(0.5); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* ---- Quick Replies ---- */
.sba-quick-replies {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 4px;
    animation: sba-msg-in 0.4s ease-out 0.2s both;
}
.sba-quick-reply-btn {
    background: var(--sba-chat-bg);
    border: 1.5px solid var(--sba-chat-accent);
    color: var(--sba-chat-accent);
    padding: 6px 14px;
    border-radius: 20px;
    font-size: 12.5px;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
    font-family: inherit;
}
.sba-quick-reply-btn:hover {
    background: var(--sba-chat-accent);
    color: #fff;
    transform: translateY(-1px);
}

/* ---- Retry Button ---- */
.sba-retry-btn {
    background: none;
    border: 1px solid var(--sba-chat-accent);
    color: var(--sba-chat-accent);
    padding: 5px 14px;
    border-radius: 16px;
    font-size: 12px;
    cursor: pointer;
    margin-top: 4px;
    transition: all 0.2s;
    font-family: inherit;
}
.sba-retry-btn:hover {
    background: var(--sba-chat-accent);
    color: #fff;
}

/* ---- Footer (Input) ---- */
.sba-chatbot-footer {
    padding: 12px 14px;
    background: var(--sba-chat-bg);
    border-top: 1px solid var(--sba-chat-msg-bot-border);
}
#sba-chatbot-form {
    display: flex;
    gap: 8px;
    align-items: center;
}
.sba-chatbot-input-wrap {
    flex: 1;
    position: relative;
}
#sba-chatbot-input {
    width: 100%;
    padding: 10px 50px 10px 16px;
    border: 1.5px solid var(--sba-chat-input-border);
    border-radius: 24px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    background: var(--sba-chat-input-bg);
    color: var(--sba-chat-msg-bot-text);
    font-family: var(--sba-chat-font);
    box-sizing: border-box;
}
#sba-chatbot-input:focus {
    border-color: var(--sba-chat-primary);
    box-shadow: 0 0 0 3px rgba(26,26,46,0.08);
}
#sba-chatbot-input::placeholder {
    color: var(--sba-chat-subtle);
}
.sba-char-count {
    position: absolute;
    right: 14px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 10px;
    color: var(--sba-chat-subtle);
    pointer-events: none;
    transition: color 0.2s;
}
.sba-char-count.sba-char-warn {
    color: var(--sba-chat-accent);
    font-weight: 600;
}

#sba-chatbot-submit {
    background: var(--sba-chat-primary);
    color: #fff;
    border: none;
    border-radius: 50%;
    width: 42px;
    height: 42px;
    min-width: 42px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.2s;
}
#sba-chatbot-submit:hover {
    transform: scale(1.05);
    opacity: 0.9;
}
#sba-chatbot-submit:active {
    transform: scale(0.95);
}
#sba-chatbot-submit svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

/* ---- Powered By ---- */
.sba-chatbot-powered {
    text-align: center;
    font-size: 10px;
    color: var(--sba-chat-subtle);
    padding: 6px;
    background: var(--sba-chat-bg);
    border-top: 1px solid var(--sba-chat-msg-bot-border);
    letter-spacing: 0.3px;
}

/* ---- Clear Chat Confirm ---- */
.sba-clear-confirm {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: rgba(233,69,96,0.08);
    border-radius: 8px;
    font-size: 12.5px;
    color: var(--sba-chat-msg-bot-text);
    animation: sba-msg-in 0.2s ease-out;
    align-self: center;
}
.sba-clear-confirm button {
    padding: 4px 12px;
    border-radius: 12px;
    border: none;
    font-size: 12px;
    cursor: pointer;
    font-family: inherit;
    transition: opacity 0.2s;
}
.sba-clear-confirm .sba-clear-yes {
    background: var(--sba-chat-accent);
    color: #fff;
}
.sba-clear-confirm .sba-clear-no {
    background: var(--sba-chat-msg-bot-border);
    color: var(--sba-chat-msg-bot-text);
}

/* ---- Responsive ---- */
@media (max-width: 480px) {
    #sba-chatbot-window {
        position: fixed !important;
        bottom: 0 !important;
        right: 0 !important;
        left: 0 !important;
        top: 0 !important;
        width: 100% !important;
        height: 100% !important;
        border-radius: 0;
        max-height: 100vh;
    }
    #sba-chatbot.sba-chatbot-left #sba-chatbot-window {
        left: 0 !important;
    }
    .sba-chatbot-header {
        padding-top: calc(env(safe-area-inset-top, 12px) + 12px);
    }
    .sba-chatbot-footer {
        padding-bottom: calc(env(safe-area-inset-bottom, 8px) + 8px);
    }
}
