/* ============================================================
   BSN School AI Chatbot Widget — Mobile-First Styles
   ============================================================ */

/* ---- Toggle Button ---- */
#bsn-chatbot-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
    background: linear-gradient(135deg, #fd7e14, #e55a00);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    z-index: 99999;
    box-shadow: 0 4px 20px rgba(253, 126, 20, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.25s ease, box-shadow 0.25s ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

#bsn-chatbot-toggle:hover  { transform: scale(1.1); }
#bsn-chatbot-toggle:active { transform: scale(0.93); }

#bsn-chatbot-toggle svg {
    width: 26px;
    height: 26px;
    fill: white;
    pointer-events: none;
}

#bsn-chat-badge {
    position: absolute;
    top: -3px;
    right: -3px;
    width: 19px;
    height: 19px;
    background: #ef4444;
    color: white;
    border-radius: 50%;
    font-size: 10px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid white;
    font-family: 'Poppins', sans-serif;
    pointer-events: none;
}

/* ================================================================
   CHAT WINDOW — MOBILE FIRST (default = full-screen bottom sheet)
   ================================================================ */
#bsn-chatbot-window {
    position: fixed;
    /* Mobile default: full-width bottom sheet */
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    /* Fill screen above the toggle area */
    height: calc(100dvh - 76px);
    max-height: calc(100dvh - 76px);

    background: #ffffff;
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.18);
    z-index: 99998;
    display: none;
    flex-direction: column;
    overflow: hidden;
    font-family: 'Poppins', sans-serif;
    /* Prevent any child from leaking out */
    contain: layout;
}

#bsn-chatbot-window.bsn-open {
    display: flex;
    animation: bsnSlideUp 0.28s cubic-bezier(0.22, 1, 0.36, 1);
}

/* Full-screen on mobile when open — anchored top+bottom so keyboard resize is handled automatically */
@media (max-width: 600px) {
    #bsn-chatbot-window.bsn-open {
        top: 0;
        bottom: 0;
        height: auto;
        max-height: none;
    }
}

@keyframes bsnSlideUp {
    from { opacity: 0; transform: translateY(40px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ---- Header ---- */
#bsn-chat-header {
    background: linear-gradient(135deg, #ff8c2a 0%, #c8500a 100%);
    padding: 13px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    color: white;
    flex-shrink: 0;
    min-height: 62px;
    box-shadow: 0 2px 10px rgba(200, 80, 10, 0.25);
}

.bsn-avatar {
    width: 38px;
    height: 38px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 19px;
    flex-shrink: 0;
}

.bsn-header-info { flex: 1; min-width: 0; }
.bsn-header-info h4 {
    margin: 0 0 1px 0;
    font-size: 14px;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.bsn-header-info p {
    margin: 0;
    font-size: 11px;
    opacity: 0.88;
}

.bsn-status-dot {
    width: 7px;
    height: 7px;
    background: #4ade80;
    border-radius: 50%;
    display: inline-block;
    margin-right: 4px;
    animation: bsnPulse 2s infinite;
}

@keyframes bsnPulse {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0.4; }
}

#bsn-chat-close {
    flex-shrink: 0;
    background: rgba(255, 255, 255, 0.18);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    -webkit-tap-highlight-color: transparent;
}
#bsn-chat-close:hover  { background: rgba(255,255,255,0.3); }
#bsn-chat-close:active { background: rgba(255,255,255,0.4); }

/* ---- Messages Area ---- */
#bsn-chat-messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: linear-gradient(180deg, #f0f4f8 0%, #f8f9fb 100%);
    scroll-behavior: smooth;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    /* Always fill remaining space */
    min-height: 0;
}

#bsn-chat-messages::-webkit-scrollbar        { width: 3px; }
#bsn-chat-messages::-webkit-scrollbar-track  { background: transparent; }
#bsn-chat-messages::-webkit-scrollbar-thumb  { background: #dde1e7; border-radius: 2px; }

/* ---- Message Bubbles ---- */
.bsn-msg {
    max-width: 88%;
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 13.5px;
    line-height: 1.6;
    /* Critical: prevent long words / URLs breaking layout */
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

.bsn-msg.bsn-bot {
    background: #ffffff;
    color: #1e293b;
    border-bottom-left-radius: 5px;
    align-self: flex-start;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
    border-left: 3px solid #fd7e14;
}

.bsn-msg.bsn-bot strong {
    color: #c2410c;
}

.bsn-msg.bsn-user {
    background: linear-gradient(135deg, #ff8c2a, #e55a00);
    color: white;
    border-bottom-right-radius: 5px;
    align-self: flex-end;
    box-shadow: 0 3px 12px rgba(253, 126, 20, 0.38);
}

/* ---- Typing Indicator ---- */
.bsn-typing {
    display: flex;
    gap: 5px;
    align-items: center;
    padding: 3px 2px;
}

.bsn-typing span {
    width: 8px;
    height: 8px;
    background: #cbd5e1;
    border-radius: 50%;
    animation: bsnBounce 1.3s infinite;
    flex-shrink: 0;
}
.bsn-typing span:nth-child(2) { animation-delay: 0.18s; }
.bsn-typing span:nth-child(3) { animation-delay: 0.36s; }

@keyframes bsnBounce {
    0%, 60%, 100% { transform: translateY(0); }
    30%            { transform: translateY(-7px); }
}

/* ---- Roll Number Bar ---- */
#bsn-roll-bar {
    padding: 9px 12px;
    background: #fff7ed;
    border-top: 1px solid #fed7aa;
    display: none;
    flex-direction: column;
    gap: 7px;
    flex-shrink: 0;
}

#bsn-roll-bar label {
    font-size: 11.5px;
    color: #92400e;
    font-weight: 600;
}

.bsn-roll-row {
    display: flex;
    gap: 8px;
    align-items: center;
}

#bsn-roll-input {
    flex: 1;
    padding: 8px 11px;
    border: 1.5px solid #fdba74;
    border-radius: 9px;
    font-size: 14px;
    outline: none;
    font-family: inherit;
    min-width: 0;
    background: white;
}
#bsn-roll-input:focus { border-color: #fd7e14; }

#bsn-roll-set {
    padding: 8px 16px;
    background: #fd7e14;
    color: white;
    border: none;
    border-radius: 9px;
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
    font-family: inherit;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}

/* ---- Quick Replies Wrapper ---- */
#bsn-qr-wrap {
    position: relative;
    flex-shrink: 0;
    background: #fafbfc;
    border-top: 1px solid #eef1f5;
}


#bsn-quick-replies {
    padding: 7px 10px 8px 10px;
    display: flex;
    gap: 6px;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    scrollbar-color: #fdba74 #f1f5f9;
}

#bsn-quick-replies::-webkit-scrollbar        { height: 4px; }
#bsn-quick-replies::-webkit-scrollbar-track  { background: #f1f5f9; border-radius: 10px; }
#bsn-quick-replies::-webkit-scrollbar-thumb  { background: #fdba74; border-radius: 10px; }
#bsn-quick-replies::-webkit-scrollbar-thumb:hover { background: #fd7e14; }

.bsn-qr-btn {
    background: #fff7ed;
    border: 1.5px solid #fed7aa;
    color: #c2410c;
    padding: 5px 12px;
    border-radius: 20px;
    font-size: 11.5px;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.18s;
    font-family: inherit;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    min-height: 30px;
    display: inline-flex;
    align-items: center;
    letter-spacing: 0.01em;
}

.bsn-qr-btn:hover,
.bsn-qr-btn:active {
    background: linear-gradient(135deg, #fd7e14, #e55a00);
    color: white;
    border-color: #fd7e14;
    box-shadow: 0 2px 8px rgba(253, 126, 20, 0.35);
    transform: translateY(-1px);
}

/* ---- Input Area ---- */
#bsn-input-area {
    padding: 10px 12px;
    display: flex;
    align-items: center;
    gap: 9px;
    background: white;
    border-top: 1px solid #e8ecf1;
    flex-shrink: 0;
    /* Safe area for phones with home bar */
    padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
}

#bsn-chat-input {
    flex: 1;
    border: 1.5px solid #e2e8f0;
    border-radius: 22px;
    padding: 10px 15px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
    min-width: 0;
    background: #f8f9fb;
    /* Prevent iOS zoom on focus */
    font-size: max(16px, 14px);
}

#bsn-chat-input:focus {
    border-color: #fd7e14;
    background: white;
}

#bsn-chat-send {
    width: 44px;
    height: 44px;
    background: linear-gradient(135deg, #fd7e14, #e55a00);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: transform 0.18s, box-shadow 0.18s;
    box-shadow: 0 3px 10px rgba(253, 126, 20, 0.35);
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}
#bsn-chat-send:hover  { transform: scale(1.08); }
#bsn-chat-send:active { transform: scale(0.92); }

#bsn-chat-send svg {
    width: 18px;
    height: 18px;
    fill: white;
    margin-left: 2px;
    pointer-events: none;
}

/* ================================================================
   TABLET & SMALL DESKTOP  (≥ 601px)
   Switches to a corner popup instead of bottom sheet
   ================================================================ */
@media (min-width: 601px) {

    #bsn-chatbot-toggle {
        bottom: 24px;
        right: 24px;
        width: 58px;
        height: 58px;
    }

    #bsn-chatbot-window {
        /* Switch to corner popup */
        bottom: 94px;
        right: 24px;
        left: auto;
        width: 370px;
        /* Cap height so it never reaches behind the browser toolbar */
        height: min(520px, calc(100dvh - 114px));
        max-height: min(520px, calc(100dvh - 114px));
        border-radius: 18px;
        box-shadow: 0 10px 44px rgba(0, 0, 0, 0.18);
    }

    #bsn-chatbot-window.bsn-open {
        animation: bsnSlideIn 0.28s cubic-bezier(0.22, 1, 0.36, 1);
    }

    @keyframes bsnSlideIn {
        from { opacity: 0; transform: translateY(16px) scale(0.97); }
        to   { opacity: 1; transform: translateY(0)    scale(1); }
    }

    /* Slightly smaller font for desktop widget */
    .bsn-msg        { font-size: 13px; }
    #bsn-chat-input { font-size: 13.5px; }
    .bsn-qr-btn     { font-size: 11px; padding: 4px 11px; }
}

/* ================================================================
   HIDE TOGGLE WHEN CHAT IS OPEN ON MOBILE
   (prevents overlap with the send button)
   ================================================================ */
@media (max-width: 600px) {
    #bsn-chatbot-toggle.bsn-hidden {
        display: none !important;
    }
}

/* ================================================================
   VERY SMALL PHONES  (≤ 360px — e.g. older Androids)
   ================================================================ */
@media (max-width: 360px) {
    #bsn-chat-header  { padding: 11px 13px; min-height: 56px; }
    .bsn-avatar       { width: 34px; height: 34px; font-size: 17px; }
    .bsn-header-info h4 { font-size: 13px; }
    #bsn-chat-messages  { padding: 12px 10px; }
    .bsn-msg            { font-size: 13px; padding: 9px 12px; }
    #bsn-chat-input     { padding: 9px 13px; }
    .bsn-qr-btn         { font-size: 11px; padding: 5px 10px; }
}
