/* ═══════════════════════════════════════════════════════════════
   United Academy — AI Chat Widget
   Glassmorphism Dark/Light · RTL · Accessible
   ═══════════════════════════════════════════════════════════════ */

/* ─── CSS Variables ─────────────────────────────────────────── */
:root {
    --ai-bg:          rgba(11, 18, 32, 0.92);
    --ai-surface:     rgba(30, 41, 59, 0.85);
    --ai-surface-2:   rgba(30, 41, 59, 0.5);
    --ai-border:      rgba(59, 130, 246, 0.15);
    --ai-text:        #e2e8f0;
    --ai-text-muted:  #94a3b8;
    --ai-primary:     #3b82f6;
    --ai-primary-rgb: 59, 130, 246;
    --ai-user-bubble: #3b82f6;
    --ai-user-text:   #ffffff;
    --ai-bot-bubble:  rgba(30, 41, 59, 0.9);
    --ai-bot-text:    #e2e8f0;
    --ai-input-bg:    rgba(15, 23, 42, 0.8);
    --ai-input-border:rgba(51, 65, 85, 0.6);
    --ai-shadow:      0 12px 48px rgba(0, 0, 0, 0.5);
    --ai-glow:        0 0 24px rgba(59, 130, 246, 0.25);
    --ai-radius:      16px;
    --ai-radius-sm:   10px;
    --ai-backdrop:    blur(20px) saturate(1.4);
}

/* ─── Light Mode ────────────────────────────────────────────── */
body.theme-light {
    --ai-bg:          rgba(248, 250, 252, 0.95);
    --ai-surface:     rgba(255, 255, 255, 0.9);
    --ai-surface-2:   rgba(241, 245, 249, 0.7);
    --ai-border:      rgba(59, 130, 246, 0.12);
    --ai-text:        #0f172a;
    --ai-text-muted:  #64748b;
    --ai-user-bubble: #3b82f6;
    --ai-user-text:   #ffffff;
    --ai-bot-bubble:  rgba(241, 245, 249, 0.95);
    --ai-bot-text:    #1e293b;
    --ai-input-bg:    rgba(255, 255, 255, 0.9);
    --ai-input-border:rgba(203, 213, 225, 0.8);
    --ai-shadow:      0 12px 48px rgba(0, 0, 0, 0.12);
    --ai-glow:        0 0 24px rgba(59, 130, 246, 0.15);
    --ai-backdrop:    blur(20px) saturate(1.2);
}

/* ─── Floating Trigger Button ───────────────────────────────── */
.ai-chat-trigger {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 10000;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: 1px solid rgba(var(--ai-primary-rgb), 0.3);
    background: linear-gradient(135deg, #2563eb, #3b82f6);
    color: #fff;
    cursor: grab;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(var(--ai-primary-rgb), 0.4);
    transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1),
                box-shadow 0.25s ease,
                left 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
                right 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
                top 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
                bottom 0.35s cubic-bezier(0.34, 1.56, 0.64, 1),
                opacity 0.3s ease;
    -webkit-tap-highlight-color: transparent;
    animation: ai-float 3s ease-in-out infinite;
}

/* Floating bob animation */
@keyframes ai-float {
    0%, 100% { transform: translateY(0); }
    50%      { transform: translateY(-6px); }
}

/* Hide during scroll */
.ai-chat-trigger.scrolling {
    opacity: 0.4;
    animation: none;
}

.ai-chat-trigger:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 28px rgba(var(--ai-primary-rgb), 0.55);
    animation: none;
}

/* ─── Page-aware Hint Bubble (anchored to trigger) ──────────── */
.ai-hint-bubble {
    position: fixed;
    z-index: 10001;
    bottom: 90px;
    right: 10px;
    background: var(--ai-surface);
    color: var(--ai-text);
    padding: 10px 30px 10px 14px;
    border-radius: 12px;
    font-size: 13px;
    font-family: 'Tajawal', sans-serif;
    border: 1px solid var(--ai-border);
    box-shadow: 0 4px 20px rgba(0,0,0,0.3);
    backdrop-filter: blur(12px);
    max-width: 230px;
    line-height: 1.6;
    opacity: 0;
    transform: translateY(8px) scale(0.9);
    transform-origin: bottom right;
    pointer-events: none;
    transition: opacity 0.4s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
    cursor: pointer;
}

/* Arrow pointing down to the trigger */
.ai-hint-bubble::after {
    content: '';
    position: absolute;
    bottom: -8px;
    right: 20px;
    width: 16px;
    height: 16px;
    background: var(--ai-surface);
    border-right: 1px solid var(--ai-border);
    border-bottom: 1px solid var(--ai-border);
    transform: rotate(45deg);
}

[dir="rtl"] .ai-hint-bubble {
    right: 10px;
    left: auto;
    padding: 10px 14px 10px 30px;
    transform-origin: bottom right;
}

[dir="rtl"] .ai-hint-bubble::after {
    right: 20px;
    left: auto;
}

[dir="rtl"] .ai-hint-bubble .ai-hint-close {
    right: auto;
    left: 6px;
}

.ai-hint-bubble.show {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

.ai-hint-bubble .ai-hint-close {
    position: absolute;
    top: 4px;
    right: 6px;
    background: none;
    border: none;
    color: var(--ai-text-muted);
    cursor: pointer;
    font-size: 14px;
    padding: 2px 4px;
    line-height: 1;
}

body.theme-light .ai-hint-bubble {
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}

body.theme-light .ai-hint-bubble::after {
    background: var(--ai-surface);
}

.ai-chat-trigger:active {
    cursor: grabbing;
    transform: scale(0.95);
}

.ai-chat-trigger svg {
    width: 26px;
    height: 26px;
    transition: transform 0.3s ease, opacity 0.2s ease;
}

.ai-chat-trigger .ai-icon-close {
    position: absolute;
    transform: rotate(90deg) scale(0);
    opacity: 0;
}

.ai-chat-trigger.active .ai-icon-chat {
    transform: rotate(-90deg) scale(0);
    opacity: 0;
}

.ai-chat-trigger.active .ai-icon-close {
    transform: rotate(0deg) scale(1);
    opacity: 1;
}

.ai-chat-trigger.active {
    animation: none;
}

/* Pulse ring on first load */
.ai-chat-trigger::after {
    content: '';
    position: absolute;
    inset: -4px;
    border-radius: 50%;
    border: 2px solid rgba(var(--ai-primary-rgb), 0.4);
    animation: ai-pulse 2s ease-out infinite;
}

.ai-chat-trigger.active::after {
    display: none;
}

@keyframes ai-pulse {
    0%   { transform: scale(1); opacity: 0.6; }
    100% { transform: scale(1.5); opacity: 0; }
}

/* ─── Chat Panel ────────────────────────────────────────────── */
.ai-chat-panel {
    position: fixed;
    bottom: 92px;
    right: 24px;
    z-index: 10000;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 520px;
    max-height: calc(100vh - 120px);
    display: flex;
    flex-direction: column;
    border-radius: var(--ai-radius);
    background: var(--ai-bg);
    border: 1px solid var(--ai-border);
    box-shadow: var(--ai-shadow);
    backdrop-filter: var(--ai-backdrop);
    -webkit-backdrop-filter: var(--ai-backdrop);
    overflow: hidden;
    /* Animation */
    opacity: 0;
    transform: translateY(16px) scale(0.96);
    pointer-events: none;
    transition: opacity 0.3s cubic-bezier(0.16, 1, 0.3, 1),
                transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

.ai-chat-panel.open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ─── Header ────────────────────────────────────────────────── */
.ai-chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 16px;
    background: var(--ai-surface);
    border-bottom: 1px solid var(--ai-border);
    flex-shrink: 0;
}

.ai-chat-header-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #2563eb, #3b82f6);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    box-shadow: 0 0 12px rgba(var(--ai-primary-rgb), 0.3);
}

.ai-chat-header-avatar svg {
    width: 18px;
    height: 18px;
    color: #fff;
}

.ai-chat-header-info {
    flex: 1;
    min-width: 0;
}

.ai-chat-header-title {
    font-size: 14px;
    font-weight: 700;
    color: var(--ai-text);
    line-height: 1.3;
}

.ai-chat-header-status {
    font-size: 11px;
    color: #10b981;
    display: flex;
    align-items: center;
    gap: 4px;
}

.ai-chat-header-status::before {
    content: '';
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #10b981;
    box-shadow: 0 0 8px rgba(16, 185, 129, 0.5);
    display: inline-block;
}

.ai-chat-header-close {
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--ai-text-muted);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s ease, color 0.15s ease;
}

.ai-chat-header-close:hover {
    background: var(--ai-surface-2);
    color: var(--ai-text);
}

/* ─── Messages Area ─────────────────────────────────────────── */
.ai-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
}

/* Custom scrollbar */
.ai-chat-messages::-webkit-scrollbar {
    width: 4px;
}

.ai-chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.ai-chat-messages::-webkit-scrollbar-thumb {
    background: rgba(var(--ai-primary-rgb), 0.2);
    border-radius: 4px;
}

.ai-chat-messages::-webkit-scrollbar-thumb:hover {
    background: rgba(var(--ai-primary-rgb), 0.4);
}

/* ─── Message Bubbles ───────────────────────────────────────── */
.ai-msg {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: var(--ai-radius-sm);
    font-size: 13.5px;
    line-height: 1.7;
    word-wrap: break-word;
    animation: ai-msg-in 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

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

/* Bot message */
.ai-msg.bot {
    align-self: flex-start;
    background: var(--ai-bot-bubble);
    color: var(--ai-bot-text);
    border: 1px solid var(--ai-border);
    border-bottom-right-radius: 4px;
}

[dir="rtl"] .ai-msg.bot {
    border-bottom-right-radius: var(--ai-radius-sm);
    border-bottom-left-radius: 4px;
}

/* User message */
.ai-msg.user {
    align-self: flex-end;
    background: var(--ai-user-bubble);
    color: var(--ai-user-text);
    border-bottom-left-radius: 4px;
}

[dir="rtl"] .ai-msg.user {
    border-bottom-left-radius: var(--ai-radius-sm);
    border-bottom-right-radius: 4px;
}

/* ─── Typing Indicator ──────────────────────────────────────── */
.ai-typing {
    align-self: flex-start;
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 12px 18px;
    background: var(--ai-bot-bubble);
    border: 1px solid var(--ai-border);
    border-radius: var(--ai-radius-sm);
    animation: ai-msg-in 0.3s ease;
}

.ai-typing span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--ai-text-muted);
    animation: ai-bounce 1.4s ease-in-out infinite;
}

.ai-typing span:nth-child(2) { animation-delay: 0.16s; }
.ai-typing span:nth-child(3) { animation-delay: 0.32s; }

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

/* ─── Quick Actions ─────────────────────────────────────────── */
.ai-quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 0 16px 8px;
    flex-shrink: 0;
}

.ai-quick-btn {
    padding: 6px 12px;
    border-radius: 20px;
    border: 1px solid var(--ai-border);
    background: var(--ai-surface-2);
    color: var(--ai-text-muted);
    font-size: 12px;
    font-family: 'Tajawal', sans-serif;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

.ai-quick-btn:hover {
    background: rgba(var(--ai-primary-rgb), 0.12);
    border-color: rgba(var(--ai-primary-rgb), 0.3);
    color: var(--ai-primary);
}

/* ─── Input Area ────────────────────────────────────────────── */
.ai-chat-input-area {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--ai-border);
    background: var(--ai-surface);
    flex-shrink: 0;
}

.ai-chat-input {
    flex: 1;
    padding: 10px 14px;
    border-radius: var(--ai-radius-sm);
    border: 1px solid var(--ai-input-border);
    background: var(--ai-input-bg);
    color: var(--ai-text);
    font-size: 13.5px;
    font-family: 'Tajawal', sans-serif;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    min-height: 42px;
}

.ai-chat-input::placeholder {
    color: var(--ai-text-muted);
}

.ai-chat-input:focus {
    border-color: rgba(var(--ai-primary-rgb), 0.5);
    box-shadow: 0 0 0 3px rgba(var(--ai-primary-rgb), 0.1);
}

.ai-chat-send {
    width: 42px;
    height: 42px;
    border-radius: var(--ai-radius-sm);
    border: none;
    background: var(--ai-primary);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s ease, transform 0.15s ease;
}

.ai-chat-send:hover {
    background: #2563eb;
}

.ai-chat-send:active {
    transform: scale(0.93);
}

.ai-chat-send:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.ai-chat-send svg {
    width: 18px;
    height: 18px;
}

/* RTL: flip send arrow */
[dir="rtl"] .ai-chat-send svg {
    transform: scaleX(-1);
}

/* ─── Error Message ─────────────────────────────────────────── */
.ai-msg.error {
    align-self: center;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.25);
    color: #fca5a5;
    font-size: 12px;
    text-align: center;
    padding: 8px 14px;
}

body.theme-light .ai-msg.error {
    background: rgba(239, 68, 68, 0.06);
    color: #dc2626;
}

/* ─── Powered by footer ─────────────────────────────────────── */
.ai-chat-footer {
    text-align: center;
    padding: 6px;
    font-size: 10px;
    color: var(--ai-text-muted);
    opacity: 0.6;
    border-top: 1px solid var(--ai-border);
    flex-shrink: 0;
}

/* ─── Mobile Responsive ─────────────────────────────────────── */
@media (max-width: 480px) {
    .ai-chat-panel {
        right: 0;
        bottom: 0;
        left: 0;
        top: 0;
        width: 100vw;
        max-width: 100vw;
        height: 100vh;
        max-height: 100vh;
        height: 100dvh;
        border-radius: 0;
    }

    .ai-chat-trigger {
        bottom: 16px;
        right: 16px;
    }

    /* ── Mobile Header: bigger close button ── */
    .ai-chat-header {
        padding: 12px 16px;
        padding-top: max(12px, env(safe-area-inset-top));
    }

    .ai-chat-header-close {
        width: 44px;
        height: 44px;
        border-radius: 50%;
        background: rgba(239, 68, 68, 0.15);
        color: #ef4444;
        border: 1px solid rgba(239, 68, 68, 0.25);
    }

    .ai-chat-header-close:hover,
    .ai-chat-header-close:active {
        background: rgba(239, 68, 68, 0.3);
        color: #fff;
    }

    .ai-chat-header-close svg {
        width: 22px;
        height: 22px;
        stroke-width: 2.5;
    }

    /* ── Mobile Input: safe area padding ── */
    .ai-chat-input-area {
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }

    .ai-chat-input {
        min-height: 46px;
        font-size: 16px; /* prevents iOS auto-zoom */
    }

    .ai-chat-send {
        width: 46px;
        height: 46px;
    }

    /* ── Mobile Messages: more space ── */
    .ai-msg {
        max-width: 90%;
        font-size: 14px;
    }

    .ai-quick-btn {
        font-size: 13px;
        padding: 8px 14px;
    }

    /* Hide floating trigger when chat is fullscreen */
    .ai-chat-trigger.active {
        opacity: 0;
        pointer-events: none;
    }
}

/* ─── Reduced Motion ────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    .ai-chat-trigger::after { animation: none; }
    .ai-msg { animation: none; }
    .ai-typing span { animation: none; opacity: 0.6; }
    .ai-chat-panel { transition-duration: 0.1s; }
}
