/* ============================================================
   BEM AI — Adaptive Light/Dark Chat Interface
   Light mode: default | Dark mode: html.bem-dark (class on <html>)
   ============================================================ */
/* Font loaded globally via theme — no @import needed */

/* ── CSS Variables ── */
:root {
    --ai-bg:          #f8f7ff;
    --ai-surface:     #ffffff;
    --ai-sidebar:     #ffffff;
    --ai-border:      #e8e3f5;
    --ai-border-soft: #f0ebff;
    --ai-text:        #1a1040;
    --ai-text-2:      #5a5070;
    --ai-text-3:      #9b93b0;
    --ai-accent:      #7c3aed;
    --ai-accent-2:    #6d28d9;
    --ai-accent-soft: rgba(124,58,237,0.08);
    --ai-user-bubble: linear-gradient(135deg, #6d28d9, #8b5cf6);
    --ai-bot-bubble:  #ffffff;
    --ai-bot-border:  #ede9fe;
    --ai-input-bg:    #ffffff;
    --ai-chip-bg:     #f5f0ff;
    --ai-chip-color:  #6d28d9;
    --ai-shadow:      0 1px 3px rgba(124,58,237,0.08), 0 4px 16px rgba(124,58,237,0.06);
    --ai-shadow-lg:   0 8px 32px rgba(124,58,237,0.12);
    --ai-header-bg:   rgba(255,255,255,0.97);
    --ai-chat-bg:     #f4f2fb;
    --ai-stat-bg:     #f8f6ff;
    --ai-radius:      18px;
}

.bem-dark {
    --ai-bg:          #0d0820;
    --ai-surface:     #16102a;
    --ai-sidebar:     #100c20;
    --ai-border:      #2a1f45;
    --ai-border-soft: #1e1535;
    --ai-text:        #e8e3f5;
    --ai-text-2:      #94a3b8;
    --ai-text-3:      #4a3f6a;
    --ai-accent:      #a78bfa;
    --ai-accent-2:    #8b5cf6;
    --ai-accent-soft: rgba(139,92,246,0.10);
    --ai-user-bubble: linear-gradient(135deg, #4c1d95, #6d28d9);
    --ai-bot-bubble:  #1a1230;
    --ai-bot-border:  #2a1f45;
    --ai-input-bg:    #1a1230;
    --ai-chip-bg:     #1e1535;
    --ai-chip-color:  #c4b5fd;
    --ai-shadow:      0 2px 12px rgba(0,0,0,0.3);
    --ai-shadow-lg:   0 8px 32px rgba(0,0,0,0.4);
    --ai-header-bg:   rgba(13,8,32,0.97);
    --ai-chat-bg:     #0d0820;
    --ai-stat-bg:     #1a1230;
}

/* ── Base ── */
.ai-pro-container {
    font-family: 'Tajawal', 'Segoe UI', Tahoma, sans-serif;
    direction: rtl;
    background: var(--ai-bg);
    min-height: 100vh;
    padding: 0;
    color: var(--ai-text);
    transition: background 0.3s, color 0.3s;
}

.ai-pro-wrapper {
    display: grid;
    grid-template-columns: 260px 1fr;
    height: 100vh;
    overflow: hidden;
}

/* ══════════════════════ SIDEBAR ══════════════════════ */
.ai-pro-sidebar {
    background: var(--ai-sidebar);
    border-left: 1px solid var(--ai-border);
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 12px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--ai-border) transparent;
    transition: background 0.3s, border-color 0.3s;
}

.ai-pro-sidebar::-webkit-scrollbar { width: 4px; }
.ai-pro-sidebar::-webkit-scrollbar-thumb { background: var(--ai-border); border-radius: 4px; }

/* Profile */
.ai-pro-profile {
    text-align: center;
    padding: 16px 12px;
    background: var(--ai-accent-soft);
    border-radius: 14px;
    border: 1px solid var(--ai-border-soft);
}

.ai-pro-avatar {
    width: 60px;
    height: 60px;
    margin: 0 auto 10px;
    background: linear-gradient(135deg, #7c3aed, #5b21b6);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 0 3px var(--ai-accent-soft), 0 4px 16px rgba(124,58,237,0.25);
}

.ai-avatar-letter { color: white; font-size: 24px; font-weight: 900; }
.ai-student-name  { font-size: 15px; font-weight: 700; color: var(--ai-text); margin: 0 0 4px; }
.ai-student-email { font-size: 11px; color: var(--ai-text-3); margin: 0; }

/* Points Card */
.ai-points-card {
    background: linear-gradient(135deg, #5b21b6 0%, #7c3aed 100%);
    color: white;
    padding: 18px 14px;
    border-radius: 14px;
    text-align: center;
    position: relative;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(124,58,237,0.3);
}

.ai-points-card::after {
    content: '';
    position: absolute;
    top: -50%;
    right: -30%;
    width: 80%;
    height: 80%;
    background: radial-gradient(circle, rgba(255,255,255,0.12) 0%, transparent 65%);
    pointer-events: none;
}

.ai-points-icon  { font-size: 24px; margin-bottom: 4px; }
.ai-points-label { font-size: 11px; opacity: 0.85; }
.ai-points-value { font-size: 40px; font-weight: 900; margin: 6px 0; line-height: 1; }

.ai-points-bar   { background: rgba(255,255,255,0.2); height: 4px; border-radius: 4px; margin: 8px 0 12px; overflow: hidden; }
.ai-points-fill  { background: white; height: 100%; border-radius: 4px; transition: width 0.5s ease; }

.ai-recharge-btn {
    width: 100%;
    padding: 8px;
    background: rgba(255,255,255,0.15);
    color: white;
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 8px;
    font-weight: 600;
    font-size: 12px;
    cursor: pointer;
    font-family: 'Tajawal', sans-serif;
    transition: background 0.2s;
}
.ai-recharge-btn:hover { background: rgba(255,255,255,0.25); }

/* Subject Filter */
.ai-subjects-filter {
    padding: 12px;
    background: var(--ai-stat-bg);
    border-radius: 12px;
    border: 1px solid var(--ai-border-soft);
}

.ai-filter-label {
    display: block;
    font-size: 11px;
    font-weight: 700;
    color: var(--ai-text-3);
    margin-bottom: 6px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.ai-subject-select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--ai-border);
    border-radius: 8px;
    font-size: 13px;
    background: var(--ai-surface);
    color: var(--ai-text);
    cursor: pointer;
    font-family: 'Tajawal', sans-serif;
    outline: none;
    transition: border-color 0.2s;
}
.ai-subject-select:focus { border-color: #7c3aed; }

/* Stats */
.ai-stats-mini { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.ai-stat-item {
    background: var(--ai-stat-bg);
    border: 1px solid var(--ai-border-soft);
    padding: 12px 8px;
    border-radius: 12px;
    text-align: center;
}

.ai-stat-value { display: block; font-size: 22px; font-weight: 900; color: #7c3aed; margin-bottom: 2px; }
.ai-stat-label { display: block; font-size: 11px; color: var(--ai-text-3); }

/* Back Button */
.ai-back-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 11px;
    background: var(--ai-stat-bg);
    border: 1px solid var(--ai-border-soft);
    color: var(--ai-text-2);
    border-radius: 12px;
    text-decoration: none;
    font-weight: 600;
    font-size: 13px;
    transition: all 0.2s;
    margin-top: auto;
}
.ai-back-btn:hover { background: var(--ai-accent-soft); color: #7c3aed; border-color: #c4b5fd; }

/* ══════════════════════ MAIN ══════════════════════ */
.ai-pro-main {
    background: var(--ai-bg);
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

/* Header */
.ai-pro-header {
    padding: 12px 24px;
    border-bottom: 1px solid var(--ai-border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--ai-header-bg);
    backdrop-filter: blur(12px);
    flex-shrink: 0;
    z-index: 10;
    box-shadow: 0 1px 0 var(--ai-border);
}

.ai-header-left { display: flex; align-items: center; gap: 12px; }

.ai-header-bot-icon {
    width: 40px;
    height: 40px;
    background: linear-gradient(135deg, #5b21b6, #7c3aed);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    box-shadow: 0 2px 10px rgba(124,58,237,0.3);
    flex-shrink: 0;
}

.ai-header-title    { font-size: 17px; font-weight: 800; color: var(--ai-text); margin: 0 0 1px; }
.ai-header-subtitle { font-size: 12px; color: var(--ai-text-3); margin: 0; }

.ai-status-badge {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    background: rgba(16,185,129,0.08);
    color: #059669;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    border: 1px solid rgba(16,185,129,0.15);
}

.bem-dark .ai-status-badge { color: #34d399; background: rgba(16,185,129,0.1); border-color: rgba(16,185,129,0.2); }

.ai-status-dot {
    width: 6px; height: 6px;
    background: #10b981;
    border-radius: 50%;
    animation: blink 2s ease-in-out infinite;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.25; }
}

/* ── Chat Area ── */
.ai-chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px 20px;
    background: var(--ai-chat-bg);
    scroll-behavior: smooth;
    scrollbar-width: thin;
    scrollbar-color: var(--ai-border) transparent;
    transition: background 0.3s;
}

.ai-chat-area::-webkit-scrollbar { width: 4px; }
.ai-chat-area::-webkit-scrollbar-thumb { background: var(--ai-border); border-radius: 4px; }

.ai-messages-container {
    max-width: 800px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

/* ── Welcome Screen ── */
.ai-welcome-msg {
    text-align: center;
    padding: 32px 16px 20px;
    max-width: 560px;
    margin: 0 auto;
}

.ai-welcome-icon {
    width: 80px;
    height: 80px;
    margin: 0 auto 20px;
    background: linear-gradient(135deg, #5b21b6, #7c3aed);
    border-radius: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 38px;
    box-shadow: 0 8px 24px rgba(124,58,237,0.25), 0 0 0 1px rgba(124,58,237,0.15);
}

.ai-welcome-title {
    font-size: 24px;
    font-weight: 800;
    color: var(--ai-text);
    margin-bottom: 8px;
}

.ai-welcome-sub {
    font-size: 14px;
    color: var(--ai-text-2);
    margin-bottom: 24px;
    line-height: 1.7;
}

/* Quick Chips */
.ai-quick-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    justify-content: center;
}

.ai-chip {
    padding: 8px 16px;
    background: var(--ai-chip-bg);
    border: 1px solid var(--ai-border);
    border-radius: 24px;
    font-size: 13px;
    font-weight: 600;
    color: var(--ai-chip-color);
    cursor: pointer;
    transition: all 0.18s;
    font-family: 'Tajawal', sans-serif;
    line-height: 1.4;
}

.ai-chip:hover {
    background: var(--ai-accent-soft);
    border-color: #a78bfa;
    color: #5b21b6;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(124,58,237,0.12);
}

.bem-dark .ai-chip:hover { color: #ddd6fe; }

/* ── Messages ── */
.ai-message {
    display: flex;
    gap: 10px;
    margin-bottom: 2px;
    animation: fadeUp 0.2s ease;
}

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

.ai-message.user { flex-direction: row-reverse; }

.ai-msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    flex-shrink: 0;
    margin-top: 8px;
}

.ai-message.bot  .ai-msg-avatar { background: linear-gradient(135deg, #5b21b6, #7c3aed); box-shadow: 0 2px 8px rgba(124,58,237,0.2); }
.ai-message.user .ai-msg-avatar { background: linear-gradient(135deg, #1e40af, #3b82f6); }

.ai-msg-bubble {
    max-width: 72%;
    padding: 12px 16px;
    border-radius: 18px;
    box-shadow: var(--ai-shadow);
}

.ai-message.user .ai-msg-bubble {
    background: var(--ai-user-bubble);
    color: white;
    border-bottom-left-radius: 4px;
}

.ai-message.bot .ai-msg-bubble {
    background: var(--ai-bot-bubble);
    color: var(--ai-text);
    border: 1px solid var(--ai-bot-border);
    border-bottom-right-radius: 4px;
}

/* Streaming cursor */
.ai-streaming .ai-stream-text::after {
    content: '▍';
    display: inline-block;
    animation: ai-blink .6s step-end infinite;
    color: var(--ai-primary, #7c3aed);
    margin-right: 2px;
}
@keyframes ai-blink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}

.ai-msg-text {
    font-size: 15px;
    line-height: 1.78;
    white-space: pre-wrap;
    word-wrap: break-word;
}

.ai-msg-images {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.ai-msg-image {
    max-width: 100%;
    max-height: 220px;
    border-radius: 10px;
    border: 1px solid var(--ai-border);
    cursor: zoom-in;
    transition: transform 0.2s;
}
.ai-msg-image:hover { transform: scale(1.02); }

/* Single image fills wider */
.ai-msg-images:has(.ai-msg-image:only-child) .ai-msg-image { max-height: 260px; }

.ai-msg-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 6px;
}

.ai-msg-time { font-size: 10px; opacity: 0.4; }

.ai-copy-btn {
    background: none;
    border: 1px solid var(--ai-border);
    border-radius: 6px;
    padding: 2px 10px;
    font-size: 11px;
    cursor: pointer;
    color: var(--ai-text-3);
    font-family: 'Tajawal', sans-serif;
    transition: all 0.18s;
}

.ai-copy-btn:hover {
    background: var(--ai-accent-soft);
    color: #7c3aed;
    border-color: #c4b5fd;
}

.ai-message.user .ai-copy-btn { display: none; }

/* ── Typing Indicator ── */
.ai-typing-indicator {
    display: none;
    flex-direction: row;
    align-items: center;
    gap: 6px;
    padding: 14px 18px;
    background: var(--ai-bot-bubble);
    border: 1px solid var(--ai-bot-border);
    border-radius: 18px;
    border-bottom-right-radius: 4px;
    width: fit-content;
    box-shadow: var(--ai-shadow);
    margin-top: 4px;
    margin-right: 0;
}

.ai-typing-indicator.active { display: flex; }

.ai-typing-indicator > span {
    display: block;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #7c3aed;
    animation: aiBounce 1.2s ease-in-out infinite;
    flex-shrink: 0;
}

.ai-typing-indicator > span:nth-child(2) { animation-delay: 0.2s; }
.ai-typing-indicator > span:nth-child(3) { animation-delay: 0.4s; }

@keyframes aiBounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
    40%           { transform: translateY(-8px); opacity: 1; }
}

/* ── Geometry Viewer (Professional) ── */
.ai-geo-wrapper {
    margin-top: 12px;
    border-top: 1px solid var(--ai-border);
    padding-top: 12px;
}

.ai-geo-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: linear-gradient(135deg, rgba(109,40,217,0.08), rgba(139,92,246,0.12));
    border: 1px solid rgba(124,58,237,0.25);
    color: #6d28d9;
    padding: 8px 16px;
    border-radius: 12px;
    font-size: 13px;
    font-weight: 700;
    font-family: 'Tajawal', sans-serif;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(124,58,237,0.08);
}
.ai-geo-toggle svg { flex-shrink: 0; }
.ai-geo-toggle:hover {
    background: linear-gradient(135deg, rgba(109,40,217,0.14), rgba(139,92,246,0.18));
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(124,58,237,0.15);
}
.ai-geo-toggle:active { transform: translateY(0); }
.bem-dark .ai-geo-toggle {
    background: linear-gradient(135deg, rgba(139,92,246,0.15), rgba(167,139,250,0.1));
    border-color: rgba(167,139,250,0.3);
    color: #c4b5fd;
}

.ai-geo-viewer {
    margin-top: 12px;
    background: var(--ai-stat-bg);
    border: 1px solid var(--ai-border);
    border-radius: 16px;
    padding: 16px;
    overflow: hidden;
    animation: geoSlideIn 0.3s ease;
}
@keyframes geoSlideIn {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: translateY(0); }
}

.ai-geo-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.ai-geo-title {
    font-weight: 700;
    font-size: 14px;
    color: #5b21b6;
    font-family: 'Tajawal', sans-serif;
}
.bem-dark .ai-geo-title { color: #c4b5fd; }

.ai-geo-fullscreen {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    border: 1px solid var(--ai-border);
    background: var(--ai-surface);
    color: var(--ai-text-2);
    cursor: pointer;
    transition: all 0.15s;
}
.ai-geo-fullscreen:hover {
    background: rgba(124,58,237,0.1);
    color: #7c3aed;
    border-color: rgba(124,58,237,0.3);
}

.ai-geo-canvas {
    display: flex;
    justify-content: center;
    direction: ltr;
    overflow: hidden;
    border-radius: 12px;
    touch-action: pan-x pan-y;
}
.ai-geo-canvas .ai-geo-svg {
    width: 100%;
    height: auto;
    max-height: 400px;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(124,58,237,0.08), 0 1px 4px rgba(0,0,0,0.05);
    transition: transform 0.2s ease;
}
.bem-dark .ai-geo-canvas .ai-geo-svg {
    box-shadow: 0 4px 20px rgba(0,0,0,0.3), 0 1px 4px rgba(139,92,246,0.1);
}

/* Responsive: smaller phones */
@media (max-width: 480px) {
    .ai-geo-viewer { padding: 10px; }
    .ai-geo-canvas .ai-geo-svg { max-height: 280px; }
    .ai-geo-title { font-size: 12px; }
    .ai-geo-toggle { font-size: 12px; padding: 6px 12px; }
}
@media (max-width: 360px) {
    .ai-geo-canvas .ai-geo-svg { max-height: 240px; }
}

/* Fullscreen mode */
.ai-geo-viewer.ai-geo-fullscreen-active {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 99999;
    margin: 0;
    border-radius: 0;
    padding: 20px;
    display: flex;
    flex-direction: column;
    background: var(--ai-stat-bg, #fff);
}
.bem-dark .ai-geo-viewer.ai-geo-fullscreen-active { background: #13111c; }
.ai-geo-viewer.ai-geo-fullscreen-active .ai-geo-canvas {
    flex: 1;
    align-items: center;
}
.ai-geo-viewer.ai-geo-fullscreen-active .ai-geo-svg {
    max-height: 80vh;
    width: auto;
    max-width: 95vw;
}

/* Legend */
.ai-geo-legend {
    text-align: center;
    margin-top: 10px;
    font-size: 12px;
    color: var(--ai-text-3);
    font-family: 'Tajawal', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    flex-wrap: wrap;
}

.ai-geo-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 11px;
    direction: ltr;
}
.ai-geo-badge--parallel {
    background: #fff7ed;
    color: #c2410c;
    border: 1px solid #fed7aa;
}
.ai-geo-badge--perp {
    background: #ecfeff;
    color: #0e7490;
    border: 1px solid #a5f3fc;
}
.bem-dark .ai-geo-badge--parallel { background: #3b2510; color: #fb923c; border-color: #7c4a1a; }
.bem-dark .ai-geo-badge--perp { background: #0c2a33; color: #67e8f9; border-color: #155e75; }

/* Date Separator */
.ai-date-sep { text-align: center; margin: 12px 0 4px; }
.ai-date-sep span {
    background: var(--ai-surface);
    color: var(--ai-text-3);
    font-size: 11px;
    padding: 3px 12px;
    border-radius: 20px;
    border: 1px solid var(--ai-border);
}

/* ══════════════════════ INPUT ══════════════════════ */
.ai-input-section {
    padding: 12px 20px 16px;
    background: var(--ai-surface);
    border-top: 1px solid var(--ai-border);
    flex-shrink: 0;
    transition: background 0.3s;
}

/* Multi-image preview */
.ai-image-preview { display: none; margin-bottom: 10px; max-width: 800px; margin-left: auto; margin-right: auto; }
.ai-image-preview.active { display: block; }

.ai-preview-images {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 0 4px;
}

.ai-preview-item { position: relative; flex-shrink: 0; }

.ai-preview-thumb {
    width: 72px;
    height: 72px;
    object-fit: cover;
    border-radius: 10px;
    border: 2px solid var(--ai-border);
    display: block;
    transition: opacity 0.18s;
}
.ai-preview-thumb:hover { opacity: 0.85; }

.ai-remove-thumb {
    position: absolute;
    top: -6px;
    left: -6px;
    width: 20px; height: 20px;
    background: #ef4444;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    font-size: 11px;
    font-weight: bold;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    padding: 0;
    transition: background 0.15s;
}
.ai-remove-thumb:hover { background: #dc2626; }

.ai-input-form {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 800px;
    margin: 0 auto;
    background: var(--ai-input-bg);
    border: 1.5px solid var(--ai-border);
    border-radius: 16px;
    padding: 6px 6px 6px 10px;
    transition: border-color 0.2s, box-shadow 0.2s;
    box-shadow: var(--ai-shadow);
}

.ai-input-form:focus-within {
    border-color: #7c3aed;
    box-shadow: 0 0 0 3px rgba(124,58,237,0.1);
}

.ai-input-wrapper { flex: 1; min-width: 0; }

.ai-question-input {
    width: 100%;
    padding: 8px 4px;
    border: none;
    background: transparent;
    font-size: 15px;
    font-family: 'Tajawal', sans-serif;
    resize: none;
    min-height: 38px;
    max-height: 130px;
    direction: rtl;
    color: var(--ai-text);
    line-height: 1.6;
    outline: none;
    box-sizing: border-box;
}

.ai-question-input::placeholder { color: var(--ai-text-3); }

.ai-input-actions {
    display: flex;
    align-items: flex-end;
    gap: 5px;
    flex-shrink: 0;
    padding-bottom: 2px;
}

.ai-attach-btn {
    height: 38px;
    padding: 0 14px;
    background: var(--ai-stat-bg);
    border: 1.5px solid var(--ai-border);
    border-radius: 11px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all 0.18s;
    color: var(--ai-text-2);
    font-family: 'Tajawal', sans-serif;
    font-size: 13px;
    font-weight: 600;
    white-space: nowrap;
    flex-shrink: 0;
}

.ai-attach-btn:hover {
    background: var(--ai-accent-soft);
    border-color: #a78bfa;
    color: #7c3aed;
}

.ai-send-btn {
    padding: 0 20px;
    height: 38px;
    background: linear-gradient(135deg, #7c3aed, #5b21b6);
    color: white;
    border: none;
    border-radius: 11px;
    cursor: pointer;
    font-family: 'Tajawal', sans-serif;
    font-size: 14px;
    font-weight: 700;
    white-space: nowrap;
    transition: all 0.18s;
    box-shadow: 0 3px 10px rgba(124,58,237,0.35);
    flex-shrink: 0;
}

.ai-send-btn:hover    { transform: translateY(-1px); box-shadow: 0 5px 16px rgba(124,58,237,0.45); }
.ai-send-btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; box-shadow: none; }

.ai-input-hint {
    text-align: center;
    font-size: 11px;
    color: var(--ai-text-3);
    margin-top: 7px;
    opacity: 0.7;
}

/* ══════════════════════ MODAL ══════════════════════ */
.ai-modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    align-items: center;
    justify-content: center;
    z-index: 99999;
    backdrop-filter: blur(5px);
}

.ai-modal-overlay.active { display: flex; }

.ai-modal-content {
    background: var(--ai-surface);
    border: 1px solid var(--ai-border);
    padding: 36px 40px;
    border-radius: 20px;
    text-align: center;
    max-width: 400px;
    box-shadow: var(--ai-shadow-lg);
    animation: fadeUp 0.22s ease;
}

.ai-modal-icon  { font-size: 52px; margin-bottom: 12px; }
.ai-modal-title { font-size: 20px; font-weight: 800; color: #ef4444; margin-bottom: 10px; }
.ai-modal-text  { font-size: 14px; color: var(--ai-text-2); line-height: 1.8; margin-bottom: 24px; }

.ai-modal-close {
    padding: 10px 32px;
    background: linear-gradient(135deg, #7c3aed, #5b21b6);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    font-family: 'Tajawal', sans-serif;
    transition: all 0.18s;
    box-shadow: 0 3px 10px rgba(124,58,237,0.3);
}

.ai-modal-close:hover { transform: translateY(-2px); box-shadow: 0 5px 14px rgba(124,58,237,0.4); }

/* ══════════════════════ KATEX — RTL FIX ══════════════════════ */
/*
 * The chat container is direction:rtl (Arabic).
 * KaTeX always renders math LTR internally, but the browser wraps
 * it in RTL which flips display math visually (A appears on right,
 * expression on left). Fix: force direction:ltr on all katex elements.
 */
.ai-message .katex,
.ai-message .katex * {
    direction: ltr;
    unicode-bidi: embed;
}

/* Display math ($$...$$) — textbook style: boxed, centered */
.ai-message .katex-display {
    direction: ltr;
    text-align: center;
    overflow-x: auto;
    overflow-y: hidden;
    margin: 14px 8px;
    padding: 14px 18px;
    background: linear-gradient(135deg, #faf8ff, #f0ecff);
    border-right: 4px solid #7c3aed;
    border-radius: 8px;
    font-size: 1.15em;
}

/* Inline math ($...$) — stays inline within Arabic text */
.ai-message .katex-inline,
.ai-message .katex:not(.katex-display .katex) {
    direction: ltr;
    display: inline-block;
    vertical-align: middle;
    color: #5b21b6;
    font-size: 1.05em;
}

/* ══════════════ HEADINGS & KEYWORDS ══════════════ */
.ai-message .ai-h2 {
    font-size: 1.35em;
    font-weight: 800;
    color: #7c3aed;
    margin: 18px 0 10px;
    padding-bottom: 6px;
    border-bottom: 3px solid #7c3aed;
}
.ai-message .ai-h3 {
    font-size: 1.18em;
    font-weight: 700;
    color: #6d28d9;
    margin: 14px 0 8px;
    padding-right: 10px;
    border-right: 4px solid #8b5cf6;
}
.ai-message .ai-h4 {
    font-size: 1.05em;
    font-weight: 700;
    color: #5b21b6;
    margin: 10px 0 6px;
}

/* Keywords: المعطيات، المطلوب، الحل، القانون... */
.ai-keyword {
    display: inline-block;
    background: linear-gradient(135deg, #7c3aed, #6d28d9);
    color: #fff;
    font-weight: 700;
    padding: 3px 12px;
    border-radius: 6px;
    font-size: 0.95em;
    margin: 4px 0;
}

/* Bold text styling */
.ai-message strong {
    color: #4c1d95;
    font-weight: 700;
}

/* Dark mode headings & keywords */
html.bem-dark .ai-message .ai-h2 { color: #c4b5fd; border-bottom-color: #8b5cf6; }
html.bem-dark .ai-message .ai-h3 { color: #c4b5fd; border-right-color: #8b5cf6; }
html.bem-dark .ai-message .ai-h4 { color: #c4b5fd; }
html.bem-dark .ai-keyword { background: linear-gradient(135deg, #8b5cf6, #7c3aed); }
html.bem-dark .ai-message strong { color: #ddd6fe; }

/* ── Math solution steps ── */
.ai-message ol {
    padding-right: 22px;
    padding-left: 0;
    margin: 10px 0;
    counter-reset: step;
    list-style: none;
}
.ai-message ol > li {
    counter-increment: step;
    position: relative;
    padding: 8px 38px 8px 8px;
    margin-bottom: 6px;
    background: #f9f8ff;
    border-radius: 8px;
    border-right: 3px solid #8b5cf6;
}
.ai-message ol > li::before {
    content: counter(step);
    position: absolute;
    right: 8px;
    top: 8px;
    width: 24px;
    height: 24px;
    background: #7c3aed;
    color: #fff;
    border-radius: 50%;
    font-size: 0.8em;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Unordered lists */
.ai-message ul {
    padding-right: 20px;
    padding-left: 0;
    margin: 8px 0;
}
.ai-message ul > li {
    padding: 4px 0;
    position: relative;
}

/* Dark mode math */
html.bem-dark .ai-message .katex-display {
    background: linear-gradient(135deg, #1e1540, #251b4a);
    border-right-color: #8b5cf6;
}
html.bem-dark .ai-message .katex:not(.katex-display .katex) {
    color: #c4b5fd;
}
html.bem-dark .ai-message ol > li {
    background: rgba(91,33,182,.1);
    border-right-color: #8b5cf6;
}
html.bem-dark .ai-message ol > li::before {
    background: #8b5cf6;
}

/* ══════════════════════ RESPONSIVE ══════════════════════ */
@media (max-width: 900px) {
    .ai-pro-wrapper { grid-template-columns: 220px 1fr; }
}

/* ══════════════ INLINE TEXTBOOK FIGURES ══════════════ */
.bem-ai-figure {
    display: block;
    text-align: center;
    margin: 12px auto;
    max-width: 95%;
    padding: 0;
    background: none;
    border: none;
    border-radius: 0;
}
.bem-ai-figure img {
    max-width: 100%;
    max-height: 420px;
    height: auto;
    border-radius: 8px;
    cursor: zoom-in;
    transition: transform .2s;
}
.bem-ai-figure img:hover { transform: scale(1.02); }

/* Dark mode figures */
html.bem-dark .bem-ai-figure img { opacity: .92; }

/* ══════════════ TABLES ══════════════ */
.ai-table-wrap {
    overflow-x: auto;
    margin: 12px 0;
    border-radius: 10px;
    border: 1.5px solid #ddd6fe;
}
.ai-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92em;
    direction: rtl;
    text-align: right;
}
.ai-table th {
    background: linear-gradient(135deg, #7c3aed, #6d28d9);
    color: #fff;
    font-weight: 700;
    padding: 10px 14px;
    white-space: nowrap;
}
.ai-table td {
    padding: 9px 14px;
    border-bottom: 1px solid #ede9fe;
}
.ai-table tr:last-child td { border-bottom: none; }
.ai-table tr:nth-child(even) td { background: #f5f3ff; }
.ai-table tr:hover td { background: #ede9fe; }

/* Streaming table (partial, still loading) */
.ai-table-streaming { opacity: 0.8; }
.ai-table-loading {
    text-align: center;
    padding: 8px 12px;
    font-size: 0.82em;
    color: #7c3aed;
    background: #f5f3ff;
    border-top: 1px dashed #ddd6fe;
}
html.bem-dark .ai-table-loading { background: rgba(91,33,182,.12); color: #c4b5fd; border-color: #5b3a9c; }

/* Dark mode tables */
html.bem-dark .ai-table-wrap { border-color: #5b3a9c; }
html.bem-dark .ai-table th { background: linear-gradient(135deg, #5b21b6, #4c1d95); }
html.bem-dark .ai-table td { border-color: #3b2870; color: #e2ddf8; }
html.bem-dark .ai-table tr:nth-child(even) td { background: rgba(91,33,182,.12); }
html.bem-dark .ai-table tr:hover td { background: rgba(91,33,182,.22); }

@media (max-width: 700px) {
    .ai-pro-wrapper { grid-template-columns: 1fr; height: auto; }
    .ai-pro-sidebar  { display: none; }
    .ai-pro-main     { height: 100vh; }
    .ai-chat-area    { padding: 14px; }
    .ai-input-section { padding: 10px 12px 14px; }
    .ai-pro-header   { padding: 10px 14px; }
    .ai-msg-bubble   { max-width: 85%; }
    .ai-header-subtitle { display: none; }
}
