/* === css/variables.css === */
/* ==============================================
   CSS Variables & Design System
   LaTeX Editor Lite
   ============================================== */

:root {
    /* Nagwa Brand Colors */
    --nagwa-dark: #414346;
    --nagwa-light: #F5F5F5;
    --nagwa-gray: #939394;
    --nagwa-pink: #D90775;
    --nagwa-orange: #E09327;
    --nagwa-blue: #6894D0;

    /* Primary Colors - Nagwa Blue Theme */
    --primary-50: #f0f5fc;
    --primary-100: #dce8f8;
    --primary-200: #b9d1f1;
    --primary-300: #8fb4e5;
    --primary-400: #6894D0;
    --primary-500: #5580c0;
    --primary-600: #4a6fb0;
    --primary-700: #3d5a8f;
    --primary-800: #334a75;
    --primary-gradient: linear-gradient(135deg, #6894D0 0%, #5580c0 100%);

    /* Accent Colors - Nagwa Brand */
    --accent-green: #22c55e;
    --accent-green-light: #dcfce7;
    --accent-blue: #6894D0;
    --accent-blue-light: #dce8f8;
    --accent-orange: #E09327;
    --accent-orange-light: #fef3c7;
    --accent-red: #ef4444;
    --accent-red-light: #fee2e2;
    --accent-pink: #D90775;
    --accent-pink-light: #fce7f3;

    /* Neutral Colors - Based on Nagwa palette */
    --gray-50: #FAFAFA;
    --gray-100: #F5F5F5;
    --gray-200: #E8E8E8;
    --gray-300: #D4D4D4;
    --gray-400: #939394;
    --gray-500: #737374;
    --gray-600: #5A5A5B;
    --gray-700: #414346;
    --gray-800: #2D2F31;
    --gray-900: #1A1B1D;

    /* Spacing Scale */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 20px;
    --space-6: 24px;
    --space-8: 32px;
    --space-10: 40px;

    /* Border Radius */
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 14px;
    --radius-xl: 20px;
    --radius-full: 9999px;

    /* Shadows */
    --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.12);
    --shadow-xl: 0 16px 48px rgba(0, 0, 0, 0.16);
    --shadow-primary: 0 4px 16px rgba(104, 148, 208, 0.3);
    --shadow-success: 0 4px 12px rgba(34, 197, 94, 0.25);

    /* Typography */
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
    --font-mono: 'SF Mono', 'Fira Code', monospace;
    --font-size-xs: 11px;
    --font-size-sm: 13px;
    --font-size-md: 15px;
    --font-size-lg: 17px;
    --font-size-xl: 20px;
    --font-size-2xl: 24px;

    /* Transitions */
    --transition-fast: 0.15s ease;
    --transition-normal: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 0.4s cubic-bezier(0.4, 0, 0.2, 1);

    /* Z-Index Scale */
    --z-dropdown: 100;
    --z-sticky: 200;
    --z-modal: 500;
    --z-toast: 1000;
}


/* === css/base.css === */
/* ==============================================
   Base & Layout Styles
   LaTeX Editor Lite
   ============================================== */

/* ========== CSS RESET ========== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* ========== BODY ========== */
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    background: linear-gradient(135deg, #f5f7fa 0%, #e8eef5 100%);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* ========== MAIN LAYOUT ========== */
.main-container {
    display: flex;
    flex: 1;
    overflow: hidden;
}

/* ========== LEFT PANEL ========== */
.left-panel {
    width: 480px;
    min-width: 400px;
    max-width: 550px;
    background: var(--gray-50);
    border-right: 1px solid var(--gray-200);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: var(--shadow-md);
}

/* ========== RIGHT PANEL / PDF AREA ========== */
.right-panel {
    flex: 1;
    background:
        radial-gradient(circle at 50% 50%, rgba(102, 126, 234, 0.03) 0%, transparent 50%),
        linear-gradient(135deg, var(--gray-100) 0%, var(--gray-50) 100%);
    padding: var(--space-5);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-4);
    overflow: auto;
    position: relative;
}

/* Subtle grid pattern overlay */
.right-panel::before {
    content: '';
    position: absolute;
    inset: 0;
    background-image:
        linear-gradient(var(--gray-200) 1px, transparent 1px),
        linear-gradient(90deg, var(--gray-200) 1px, transparent 1px);
    background-size: 40px 40px;
    opacity: 0.3;
    pointer-events: none;
}

/* ========== PLACEHOLDER MESSAGE ========== */
.placeholder-message {
    text-align: center;
    color: #999;
    padding: 40px;
    font-size: 16px;
}

/* ========== CUSTOM SCROLLBAR ========== */
.tab-content::-webkit-scrollbar {
    width: 6px;
}

.tab-content::-webkit-scrollbar-track {
    background: transparent;
}

.tab-content::-webkit-scrollbar-thumb {
    background: var(--gray-300);
    border-radius: var(--radius-full);
}

.tab-content::-webkit-scrollbar-thumb:hover {
    background: var(--gray-400);
}

/* === css/typography.css === */
/* ==============================================
   Typography & Font Faces
   LaTeX Editor Lite
   ============================================== */

/* ========== CUSTOM FONTS (LaTeX Matching) ========== */

/* NagwaTKText - Regular */
@font-face {
    font-family: 'NagwaTKText';
    src: url('../static/fonts/NagwaTKText-Regular.otf') format('opentype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'NagwaTKText';
    src: url('../static/fonts/NagwaTKText-Bold.otf') format('opentype');
    font-weight: 700;
    font-style: normal;
    font-display: swap;
}

@font-face {
    font-family: 'NagwaTKText';
    src: url('../static/fonts/NagwaTKText-Oblique.otf') format('opentype');
    font-weight: 400;
    font-style: italic;
    font-display: swap;
}

@font-face {
    font-family: 'NagwaTKText';
    src: url('../static/fonts/NagwaTKText-BoldOblique.otf') format('opentype');
    font-weight: 700;
    font-style: italic;
    font-display: swap;
}

@font-face {
    font-family: 'NagwaArabic';
    src: url('../static/fonts/NagwaArabic-Regular.ttf') format('truetype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

/* NagwaTKMath font for MathLive rendering */
@font-face {
    font-family: 'NagwaTKMath';
    src: url('../static/fonts/NagwaTKMath-Regular.otf') format('opentype');
    font-weight: 400;
    font-style: normal;
    font-display: swap;
}

/* ========== MathLive Custom Font Override ========== */
/* Force MathLive to use NagwaTKMath for all math symbols */

/* Math italic letters (variables like x, y, a, b) */
.ML__mathit,
.ML__it {
    font-family: 'NagwaTKMath', 'Latin Modern Math', 'Cambria Math', serif !important;
}

/* Math roman (upright) letters */
.ML__mathrm,
.ML__rm {
    font-family: 'NagwaTKMath', 'Latin Modern Math', 'Cambria Math', serif !important;
}

/* Math bold */
.ML__mathbf,
.ML__bf {
    font-family: 'NagwaTKMath', 'Latin Modern Math', 'Cambria Math', serif !important;
    font-weight: 700;
}

/* Numbers in math */
.ML__mn {
    font-family: 'NagwaTKMath', 'Latin Modern Math', 'Cambria Math', serif !important;
}

/* Operators (+, -, =, etc.) */
.ML__mo {
    font-family: 'NagwaTKMath', 'Latin Modern Math', 'Cambria Math', serif !important;
}

/* All other math elements */
.ML__base,
.ML__text {
    font-family: 'NagwaTKMath', 'Latin Modern Math', 'Cambria Math', serif !important;
}

/* Apply NagwaTKText font to elements with this class */
.nagwa-tk-text {
    font-family: 'NagwaTKText', 'Times New Roman', serif !important;
}

/* === css/palette.css === */
/* ==============================================
   Palette Item Styles
   LaTeX Editor Lite
   ============================================== */

/* ========== PALETTE ITEMS ========== */
.palette-item {
    background: white;
    border: 2px solid var(--gray-200);
    border-radius: var(--radius-lg);
    padding: var(--space-4) var(--space-5);
    margin-bottom: var(--space-3);
    cursor: grab;
    transition: var(--transition-normal);
    font-weight: 500;
    color: var(--gray-700);
    box-shadow: var(--shadow-sm);
    font-family: 'NagwaTKText', sans-serif;
    font-size: var(--font-size-md);
    line-height: 1.6;
    user-select: none;
    -webkit-user-select: none;
    position: relative;
}

/* Drag handle indicator */
.palette-item::before {
    content: '⋮⋮';
    position: absolute;
    left: var(--space-2);
    top: 50%;
    transform: translateY(-50%);
    color: var(--gray-300);
    font-size: 14px;
    letter-spacing: -2px;
    opacity: 0;
    transition: var(--transition-fast);
}

.palette-item:hover::before {
    opacity: 1;
}

.palette-item:hover {
    border-color: var(--primary-500);
    background: linear-gradient(135deg, var(--primary-50) 0%, white 100%);
    transform: translateY(-2px);
    box-shadow: var(--shadow-primary);
    padding-left: var(--space-8);
}

.palette-item:active {
    cursor: grabbing;
    transform: scale(0.98);
    box-shadow: var(--shadow-sm);
}

/* Used palette items */
.palette-item.used {
    opacity: 0.75;
    border-color: var(--gray-300);
    background: linear-gradient(135deg, var(--gray-50) 0%, var(--gray-100) 100%);
}

.palette-item.used::after {
    content: '✓';
    position: absolute;
    right: var(--space-3);
    top: var(--space-3);
    width: 20px;
    height: 20px;
    background: var(--accent-green);
    color: white;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
}

.palette-item.used:hover {
    border-color: var(--primary-400);
    opacity: 0.85;
}

/* ========== CURSOR FIXES FOR EDITORS ========== */
.palette-item .rich-note-editor {
    cursor: text !important;
    user-select: text;
    -webkit-user-select: text;
}

.palette-item:hover .rich-note-editor {
    cursor: text !important;
}

.rich-note-editor math-field {
    cursor: text !important;
}

.palette-item .item-header {
    cursor: grab;
}

.palette-item .item-header:active {
    cursor: grabbing;
}

.palette-item .item-controls {
    cursor: default;
}

.palette-item input,
.palette-item textarea,
.palette-item [contenteditable="true"] {
    cursor: text !important;
}

/* ========== PALETTE ITEM ID ========== */
.palette-item-id {
    display: inline-block;
    min-width: 48px;
    font-weight: 700;
    color: #3b82f6;
    margin-right: 14px;
    font-size: 16px;
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.15) 0%, rgba(59, 130, 246, 0.1) 100%);
    padding: 4px 10px;
    border-radius: 8px;
    border: 1px solid rgba(59, 130, 246, 0.2);
}

.palette-item-id-input {
    width: 50px;
    padding: 4px 8px;
    font-weight: 700;
    color: #3b82f6;
    border: 1px solid rgba(59, 130, 246, 0.3);
    border-radius: 8px;
    margin-right: 14px;
    text-align: center;
    font-size: 16px;
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.15) 0%, rgba(59, 130, 246, 0.1) 100%);
    transition: border-color 0.2s, box-shadow 0.2s;
    appearance: textfield;
    -moz-appearance: textfield;
}

.palette-item-id-input::-webkit-inner-spin-button,
.palette-item-id-input::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.palette-item-id-input:focus {
    outline: none;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.palette-item-id-input:hover {
    border-color: #3b82f6;
}

/* Item Text */
.item-text {
    flex: 1;
    font-size: var(--font-size-md);
    color: var(--gray-700);
    line-height: 1.5;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ========== ACCORDION UI ========== */
.item-header {
    display: flex;
    align-items: center;
    cursor: pointer;
    padding: 0;
    margin: 0;
    user-select: none;
    gap: var(--space-3);
}

/* Hide text in palette item headers */
.palette-item .item-header .item-text,
.palette-item .item-header math-field,
.palette-item .item-header .math-ltr-isolate {
    display: none !important;
}

.item-controls {
    display: none;
    margin-top: var(--space-4);
    padding-top: var(--space-4);
    border-top: 1px solid var(--gray-200);
}

.palette-item.active-expanded {
    border-color: var(--primary-500);
    background: white;
    box-shadow: var(--shadow-lg), 0 0 0 2px rgba(102, 126, 234, 0.1);
}

.palette-item.active-expanded .item-controls {
    display: block;
}

/* ========== CONTROL SECTIONS ========== */
.control-section {
    margin-bottom: var(--space-4);
}

.control-section:last-child {
    margin-bottom: 0;
}

/* ========== PROPERTY LABELS ========== */
.property-label {
    font-size: 10px;
    font-weight: 700;
    color: var(--gray-400);
    text-transform: uppercase;
    letter-spacing: 0.8px;
    margin-bottom: var(--space-2);
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.property-label::before {
    content: '';
    width: 3px;
    height: 10px;
    background: var(--primary-gradient);
    border-radius: 2px;
}

/* NOTE: Editor styles moved to rich-note-editor in components.css */

/* ========== LOCATION INPUT ========== */
.location-input {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    font-family: 'NagwaTKText', system-ui, sans-serif;
    font-size: 18px;
    color: var(--gray-700);
    background: var(--gray-50);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    transition: var(--transition-fast);
    user-select: text;
    -webkit-user-select: text;
    cursor: text;
}

.location-input:focus {
    outline: none;
    background: white;
    border-color: var(--primary-500);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* ========== PROPERTIES ROW ========== */
.properties-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

/* ========== ROTATION INPUT ========== */
.rotation-input {
    width: 60px;
    padding: var(--space-2);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    font-weight: 600;
    color: var(--gray-700);
    background: var(--gray-50);
    text-align: center;
    transition: var(--transition-fast);
}

.rotation-input:focus {
    outline: none;
    background: white;
    border-color: var(--primary-500);
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* ========== COLOR SWATCHES ========== */
.colors-row {
    display: flex;
    gap: var(--space-2);
    flex-wrap: wrap;
    flex: 1;
    align-items: center;
}

.palette-color-swatch {
    width: 28px;
    height: 28px;
    border-radius: var(--radius-full);
    cursor: pointer;
    border: 2px solid white;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08), var(--shadow-xs);
    transition: var(--transition-fast);
}

.palette-color-swatch:hover {
    transform: scale(1.15);
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15), var(--shadow-md);
}

.palette-color-swatch.active {
    transform: scale(1.1);
    box-shadow: 0 0 0 2px white, 0 0 0 4px var(--primary-500);
}

/* ========== DRAG OVER STATE ========== */
.palette-item.drag-over {
    border-top: 5px solid #3b82f6;
    margin-top: -2px;
    padding-top: 20px;
    background: linear-gradient(180deg, rgba(59, 130, 246, 0.2) 0%, rgba(59, 130, 246, 0.1) 30%, transparent 100%);
    box-shadow: 0 -6px 16px rgba(59, 130, 246, 0.4);
}

.palette-item.drag-over::before {
    content: '↓ أفلت هنا';
    position: absolute;
    top: -20px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
    color: white;
    font-size: 12px;
    font-weight: 700;
    padding: 4px 14px;
    border-radius: 12px;
    white-space: nowrap;
    z-index: 10;
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.4);
}

/* ========== LOCATION CONTAINER ========== */
.palette-item-location-container {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.palette-item-location {
    width: 100%;
    padding: 8px 12px;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    font-size: 13px;
    color: #475569;
    font-style: italic;
    background: rgba(255, 255, 255, 0.9);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    font-family: 'NagwaTKText', sans-serif;
}

.palette-item-location::placeholder {
    color: #94a3b8;
    font-style: italic;
}

.palette-item-location:focus {
    outline: none;
    border-color: #667eea;
    background: white;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    transform: translateY(-1px);
}

/* ========== KATEX MATH STYLES ========== */
.katex,
.katex * {
    font-family: 'NagwaTKMath', 'Times New Roman', serif !important;
}

.item-text .katex {
    font-size: 1em;
    line-height: 1.4;
    font-family: 'NagwaTKMath', 'Times New Roman', serif !important;
}

.handwriting-item .katex {
    font-size: inherit;
    line-height: 1.2;
    font-family: 'NagwaTKMath', 'Times New Roman', serif !important;
}

.katex-html {
    white-space: nowrap;
}

/* ========== DELETE BUTTON ========== */
.delete-btn {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gray-100);
    color: var(--gray-500);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-full);
    cursor: pointer;
    transition: var(--transition-fast);
    flex-shrink: 0;
}

.delete-btn:hover {
    background: var(--accent-red);
    color: white;
    border-color: var(--accent-red);
    transform: scale(1.1);
}

.delete-btn:active {
    transform: scale(0.95);
}

.delete-btn-icon {
    font-size: 16px;
    line-height: 1;
}

.delete-btn-inline {
    background: #fee2e2;
    color: #ef4444;
    border: none;
    border-radius: 6px;
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.2s;
    margin-left: 4px;
}

.delete-btn-inline:hover {
    background: #fecaca;
}

/* ========== SIDEBAR TEXT EDITOR ========== */
#sidebar-text-editor {
    width: 100%;
    min-height: 80px;
    padding: 12px;
    font-family: 'NagwaTKText', system-ui, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: #1e293b;
    background: white;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    resize: vertical;
    transition: all 0.2s ease;
    box-sizing: border-box;
}

#sidebar-text-editor:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

#sidebar-text-editor::placeholder {
    color: #94a3b8;
    font-style: italic;
}

.sidebar-text-label {
    font-size: 12px;
    color: #64748b;
    font-weight: 600;
    display: block;
    margin-bottom: 8px;
}

.sidebar-text-hint {
    font-size: 11px;
    color: #94a3b8;
    margin-top: 6px;
    display: flex;
    align-items: center;
    gap: 4px;
}

/* ========== SIDEBAR COLOR PALETTE ========== */
.sidebar-color-swatch {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    border: 3px solid white;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-color-swatch:hover {
    transform: scale(1.2);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
}

.sidebar-color-swatch.active {
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.3), 0 2px 8px rgba(0, 0, 0, 0.15);
    transform: scale(1.1);
}

#sidebar-delete-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(239, 68, 68, 0.4);
}

#sidebar-delete-btn:active {
    transform: translateY(0);
}

/* === css/components.css === */
/* ==============================================
   Component Styles
   LaTeX Editor Lite
   ============================================== */

/* ========== HEADER ========== */
.header {
   background: var(--primary-gradient);
   color: white;
   padding: var(--space-3) var(--space-6);
   box-shadow: var(--shadow-lg);
   display: flex;
   align-items: center;
   gap: var(--space-4);
   position: relative;
   z-index: var(--z-sticky);
}

.header::after {
   content: '';
   position: absolute;
   bottom: 0;
   left: 0;
   right: 0;
   height: 1px;
   background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
}

/* Header Brand */
.header-brand {
   display: flex;
   align-items: center;
   gap: var(--space-2);
}

.header-logo {
   font-size: 24px;
}

.header-title {
   font-size: var(--font-size-lg);
   font-weight: 700;
   letter-spacing: -0.3px;
}

/* Session Badge */
.session-badge {
   display: flex;
   align-items: center;
   gap: var(--space-2);
   background: rgba(255, 255, 255, 0.15);
   padding: var(--space-2) var(--space-3);
   border-radius: var(--radius-full);
   font-size: var(--font-size-sm);
   border: 1px solid rgba(255, 255, 255, 0.2);
}

.session-badge-icon {
   font-size: 14px;
}

.session-badge-id {
   font-weight: 600;
   max-width: 150px;
   overflow: hidden;
   text-overflow: ellipsis;
   white-space: nowrap;
}

/* Slide Metadata */
.slide-metadata {
   display: flex;
   align-items: center;
   gap: var(--space-3);
}

.slide-number {
   font-weight: 700;
   font-size: var(--font-size-sm);
   background: rgba(255, 255, 255, 0.2);
   padding: var(--space-1) var(--space-3);
   border-radius: var(--radius-sm);
}

.slide-divider {
   width: 1px;
   height: 20px;
   background: rgba(255, 255, 255, 0.3);
}

.slide-title-text {
   font-weight: 600;
   font-size: var(--font-size-sm);
   max-width: 200px;
   overflow: hidden;
   text-overflow: ellipsis;
   white-space: nowrap;
}

.slide-type-badge {
   font-size: var(--font-size-xs);
   font-weight: 700;
   padding: var(--space-1) var(--space-2);
   border-radius: var(--radius-sm);
   background: rgba(255, 255, 255, 0.2);
   text-transform: uppercase;
   letter-spacing: 0.5px;
}

/* Header Input Group */
.header-input-group {
   display: flex;
   align-items: center;
   gap: var(--space-2);
}

.header-input {
   padding: var(--space-2) var(--space-3);
   border-radius: var(--radius-md);
   border: 2px solid rgba(255, 255, 255, 0.2);
   background: rgba(255, 255, 255, 0.1);
   color: white;
   font-size: var(--font-size-sm);
   width: 160px;
   transition: var(--transition-normal);
}

.header-input:focus {
   outline: none;
   border-color: rgba(255, 255, 255, 0.6);
   background: rgba(255, 255, 255, 0.15);
   box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.1);
}

.header-input::placeholder {
   color: rgba(255, 255, 255, 0.6);
}

/* Header Divider */
.header-divider {
   width: 1px;
   height: 28px;
   background: rgba(255, 255, 255, 0.2);
   margin: 0 var(--space-2);
}

/* Header Actions */
.header-actions {
   display: flex;
   gap: var(--space-2);
}

/* Header Buttons */
.header-btn {
   display: flex;
   align-items: center;
   gap: var(--space-2);
   padding: var(--space-2) var(--space-4);
   border: none;
   border-radius: var(--radius-md);
   font-weight: 600;
   font-size: var(--font-size-sm);
   cursor: pointer;
   transition: var(--transition-normal);
}

.header-btn span {
   font-size: 14px;
}

.header-btn-secondary {
   background: rgba(255, 255, 255, 0.15);
   color: white;
   border: 1px solid rgba(255, 255, 255, 0.2);
}

.header-btn-secondary:hover {
   background: rgba(255, 255, 255, 0.25);
   transform: translateY(-1px);
}

.header-btn-info {
   background: linear-gradient(135deg, #3b82f6, #2563eb);
   color: white;
   border: 1px solid rgba(59, 130, 246, 0.5);
}

.header-btn-info:hover {
   background: linear-gradient(135deg, #60a5fa, #3b82f6);
   transform: translateY(-1px);
   box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
}

.header-btn-primary {
   background: white;
   color: #667eea;
}

.header-btn-primary:hover {
   background: #f8f9ff;
   transform: translateY(-2px);
   box-shadow: var(--shadow-md);
}

.header-btn-success {
   background: var(--accent-green);
   color: white;
}

.header-btn-success:hover {
   background: #16a34a;
   transform: translateY(-2px);
   box-shadow: var(--shadow-success);
}

.header-btn:active {
   transform: translateY(0);
}

.header-btn:disabled {
   cursor: not-allowed;
   pointer-events: none;
}

/* ========== TABS ========== */
.tabs {
   display: flex;
   background: white;
   border-bottom: 1px solid var(--gray-200);
   padding: var(--space-2) var(--space-3) 0;
   gap: var(--space-1);
}

.tab {
   flex: 1;
   padding: var(--space-3) var(--space-4);
   text-align: center;
   cursor: pointer;
   background: transparent;
   border: none;
   border-radius: var(--radius-md) var(--radius-md) 0 0;
   font-size: var(--font-size-sm);
   font-weight: 600;
   transition: var(--transition-normal);
   color: var(--gray-500);
   position: relative;
   display: flex;
   align-items: center;
   justify-content: center;
   gap: var(--space-2);
}

.tab-icon {
   font-size: 16px;
}

.tab.active {
   background: var(--gray-50);
   color: var(--primary-600);
}

.tab.active::after {
   content: '';
   position: absolute;
   bottom: 0;
   left: var(--space-2);
   right: var(--space-2);
   height: 3px;
   background: var(--primary-gradient);
   border-radius: 3px 3px 0 0;
}

.tab:hover:not(.active) {
   background: var(--primary-50);
   color: var(--primary-600);
}

/* Tab Content */
.tab-content {
   display: none;
   flex: 1;
   overflow-y: auto;
   padding: var(--space-4);
   background: var(--gray-50);
}

.tab-content.active {
   display: flex;
   flex-direction: column;
}

/* ========== SESSION DASHBOARD ========== */
.session-status {
   background: linear-gradient(135deg, var(--accent-green-light) 0%, white 100%);
   padding: var(--space-3) var(--space-4);
   border-radius: var(--radius-md);
   margin-bottom: var(--space-4);
   font-size: var(--font-size-sm);
   color: #166534;
   text-align: center;
   font-weight: 600;
   box-shadow: var(--shadow-sm);
   border: 1px solid rgba(34, 197, 94, 0.3);
   display: flex;
   align-items: center;
   justify-content: center;
   gap: var(--space-2);
}

.session-status::before {
   content: '📋';
   font-size: 16px;
}

/* Slide Cards Container */
.slides-list {
   display: flex;
   flex-direction: column;
   gap: var(--space-2);
}

/* ========== SLIDE CARDS ========== */
.slide-card {
   background: white;
   border: none;
   border-radius: var(--radius-lg);
   padding: var(--space-4);
   cursor: pointer;
   transition: var(--transition-normal);
   position: relative;
   display: grid;
   grid-template-columns: auto 1fr auto;
   align-items: center;
   gap: var(--space-4);
   box-shadow: 0 1px 3px rgba(0, 0, 0, 0.04);
}

.slide-card:hover {
   background: linear-gradient(135deg, white 0%, var(--primary-50) 100%);
   box-shadow: var(--shadow-md);
   transform: translateY(-2px);
}

.slide-card.active {
   background: linear-gradient(135deg, var(--primary-50) 0%, white 100%);
   box-shadow: var(--shadow-lg), inset 0 0 0 2px var(--primary-400);
}

/* Slide Number Badge */
.slide-card-number {
   width: 40px;
   height: 40px;
   background: linear-gradient(135deg, var(--gray-100) 0%, var(--gray-50) 100%);
   border-radius: var(--radius-full);
   display: flex;
   align-items: center;
   justify-content: center;
   font-weight: 700;
   font-size: var(--font-size-md);
   color: var(--gray-500);
   flex-shrink: 0;
   border: 2px solid var(--gray-100);
   transition: var(--transition-fast);
}

.slide-card:hover .slide-card-number {
   background: var(--primary-100);
   border-color: var(--primary-200);
   color: var(--primary-600);
}

.slide-card.active .slide-card-number {
   background: var(--primary-gradient);
   border-color: transparent;
   color: white;
   box-shadow: 0 2px 8px rgba(102, 126, 234, 0.4);
}

/* Slide Content */
.slide-card-content {
   flex: 1;
   min-width: 0;
   display: flex;
   flex-direction: column;
   gap: 2px;
}

.slide-card .slide-title {
   font-weight: 600;
   color: var(--gray-800);
   font-size: var(--font-size-sm);
   white-space: nowrap;
   overflow: hidden;
   text-overflow: ellipsis;
   line-height: 1.4;
}

.slide-card:hover .slide-title {
   color: var(--primary-700);
}

.slide-card .slide-meta {
   font-size: 11px;
   color: var(--gray-400);
   font-family: var(--font-mono);
   letter-spacing: -0.3px;
}

/* PDF Badge */
.slide-card .pdf-badge {
   display: inline-flex;
   align-items: center;
   gap: 4px;
   background: linear-gradient(135deg, var(--accent-green) 0%, #059669 100%);
   color: white;
   padding: 4px 10px;
   border-radius: var(--radius-full);
   font-size: 10px;
   font-weight: 700;
   letter-spacing: 0.3px;
   flex-shrink: 0;
   box-shadow: 0 2px 4px rgba(16, 185, 129, 0.3);
}

.slide-card .pdf-badge::before {
   content: '✓';
   font-size: 9px;
}

.slide-card .no-pdf-badge {
   background: linear-gradient(135deg, var(--accent-orange) 0%, #ea580c 100%);
   box-shadow: 0 2px 4px rgba(249, 115, 22, 0.3);
}

.slide-card .no-pdf-badge::before {
   content: '○';
}

/* Slide Order Number Badge */
.slide-card .slide-order {
   width: 44px;
   height: 44px;
   background: linear-gradient(135deg, var(--gray-100) 0%, var(--gray-50) 100%);
   border-radius: var(--radius-full);
   display: flex;
   align-items: center;
   justify-content: center;
   font-weight: 700;
   font-size: var(--font-size-md);
   color: var(--gray-600);
   flex-shrink: 0;
   border: 2px solid var(--gray-100);
   transition: var(--transition-fast);
}

.slide-card:hover .slide-order {
   background: linear-gradient(135deg, var(--primary-100) 0%, var(--primary-50) 100%);
   border-color: var(--primary-200);
   color: var(--primary-700);
   transform: scale(1.05);
}

.slide-card.active .slide-order {
   background: var(--primary-gradient);
   border-color: transparent;
   color: white;
   box-shadow: 0 3px 10px rgba(102, 126, 234, 0.5);
}

/* ========== MANUAL NOTE SECTION ========== */
.manual-input-section {
   background: white;
   border: 1px solid var(--gray-200);
   border-radius: var(--radius-lg);
   padding: var(--space-5);
   margin-bottom: var(--space-4);
   box-shadow: var(--shadow-sm);
   position: relative;
}

.manual-input-section::before {
   content: '';
   position: absolute;
   top: 0;
   left: 0;
   right: 0;
   height: 4px;
   background: var(--primary-gradient);
   border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.manual-input-section h3 {
   font-size: var(--font-size-md);
   color: var(--gray-800);
   margin-bottom: var(--space-4);
   font-weight: 700;
   display: flex;
   align-items: center;
   gap: var(--space-2);
}

.manual-input-section h3::before {
   content: '✍️';
   font-size: 18px;
}

.manual-input-section .input-group {
   margin-bottom: var(--space-3);
}

.manual-input-section .input-label {
   font-size: var(--font-size-xs);
   font-weight: 600;
   color: var(--gray-500);
   text-transform: uppercase;
   letter-spacing: 0.5px;
   margin-bottom: var(--space-1);
   display: block;
}

.manual-input-section input {
   width: 100%;
   padding: var(--space-3);
   border: 1px solid var(--gray-200);
   border-radius: var(--radius-md);
   font-size: var(--font-size-sm);
   transition: var(--transition-fast);
   background: var(--gray-50);
   font-family: 'NagwaTKText', sans-serif;
}

.manual-input-section input:focus {
   outline: none;
   background: white;
   border-color: var(--primary-500);
   box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.15);
}

.manual-input-section input::placeholder {
   color: var(--gray-400);
}

.manual-input-section button {
   width: 100%;
   background: var(--primary-gradient);
   color: white;
   border: none;
   padding: var(--space-3) var(--space-4);
   border-radius: var(--radius-md);
   cursor: pointer;
   font-weight: 600;
   font-size: var(--font-size-sm);
   transition: var(--transition-normal);
   display: flex;
   align-items: center;
   justify-content: center;
   gap: var(--space-2);
   margin-top: var(--space-3);
}

.manual-input-section button::before {
   content: '➕';
   font-size: 14px;
}

.manual-input-section button:hover {
   transform: translateY(-2px);
   box-shadow: var(--shadow-primary);
}

.manual-input-section button:active {
   transform: translateY(0);
}

/* ========== RICH NOTE EDITOR ========== */
.rich-note-editor {
   min-height: 40px;
   padding: 12px 16px;
   border: 2px solid #e2e8f0;
   border-radius: 8px;
   font-family: 'NagwaTKText', sans-serif;
   font-size: 24px;
   line-height: 1.6;
   background: white;
   outline: none;
   color: #000000;
}

.rich-note-editor:focus {
   border-color: #667eea;
   box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.rich-note-editor:empty::before {
   content: attr(data-placeholder);
   color: #a0aec0;
   font-style: italic;
}

/* Inline Math Field in Rich Note Editor */
.rich-note-editor math-field {
   display: inline-block;
   min-width: 20px;
   width: auto;
   max-width: 100%;
   padding: 1px 6px;
   margin: 0 2px;
   border: 1.5px solid #93c5fd;
   border-radius: 4px;
   background: rgba(239, 246, 255, 0.8);
   font-size: 24px;
   vertical-align: middle;
   cursor: text;
   line-height: 1.4;
   height: auto;
   /* ✅ FIX BiDi: Isolate math (LTR) from Arabic (RTL) context */
   direction: ltr !important;
   unicode-bidi: isolate;
   text-align: left;
}

.rich-note-editor math-field:focus,
.rich-note-editor math-field:focus-within {
   outline: none;
   border-color: #667eea;
   background: white;
   box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

/* ========== RTL EDITOR SUPPORT ========== */
/* When editor is RTL (Arabic content), ensure proper styling */
.rich-note-editor[dir="rtl"] {
   text-align: right;
}

/* Math fields inside RTL editors stay LTR isolated */
.rich-note-editor[dir="rtl"] math-field {
   direction: ltr !important;
   unicode-bidi: isolate !important;
   text-align: left !important;
   display: inline-block;
}

/* ========== PDF TOOLBAR ========== */
.pdf-toolbar {
   display: flex;
   align-items: center;
   justify-content: center;
   gap: var(--space-3);
   background: white;
   padding: var(--space-3) var(--space-5);
   border-radius: var(--radius-lg) var(--radius-lg) 0 0;
   box-shadow: var(--shadow-sm);
   border: 1px solid var(--gray-200);
   border-bottom: none;
   margin-bottom: 0;
   width: 100%;
   max-width: 800px;
   z-index: 10;
}

.toolbar-nav-btn {
   padding: var(--space-2) var(--space-4);
   border: 2px solid var(--primary-500);
   background: white;
   color: var(--primary-600);
   border-radius: var(--radius-md);
   cursor: pointer;
   font-weight: 600;
   transition: var(--transition-normal);
   display: flex;
   align-items: center;
   gap: var(--space-1);
   font-size: var(--font-size-sm);
}

.toolbar-nav-btn:hover:not(:disabled) {
   background: var(--primary-gradient);
   color: white;
   transform: translateY(-1px);
   box-shadow: var(--shadow-primary);
}

.toolbar-nav-btn:active:not(:disabled) {
   transform: translateY(0);
}

.toolbar-nav-btn:disabled {
   opacity: 0.4;
   cursor: not-allowed;
   border-color: var(--gray-300);
   color: var(--gray-400);
}

.toolbar-slide-counter {
   font-family: var(--font-mono);
   font-weight: 700;
   color: var(--gray-700);
   font-size: var(--font-size-sm);
   min-width: 100px;
   text-align: center;
   background: var(--primary-50);
   padding: var(--space-2) var(--space-3);
   border-radius: var(--radius-md);
   border: 1px solid var(--primary-200);
}

/* Undo/Redo Action Buttons */
.toolbar-action-btn {
   padding: var(--space-2) var(--space-3);
   border: 1px solid var(--accent-green);
   background: white;
   color: var(--accent-green);
   border-radius: var(--radius-md);
   cursor: pointer;
   font-weight: 600;
   font-size: var(--font-size-sm);
   transition: var(--transition-normal);
   display: flex;
   align-items: center;
   gap: var(--space-1);
}

.toolbar-action-btn:hover {
   background: var(--accent-green);
   color: white;
   transform: translateY(-1px);
   box-shadow: var(--shadow-success);
}

.toolbar-action-btn:active {
   transform: translateY(0);
}

/* Toolbar Separator */
.toolbar-separator {
   width: 1px;
   height: 28px;
   background: var(--gray-200);
   margin: 0 var(--space-2);
}

/* ========== PAGE CONTROLS ========== */
.page-controls {
   display: none;
   align-items: center;
   gap: var(--space-4);
   background: white;
   padding: var(--space-3) var(--space-5);
   border-radius: var(--radius-lg);
   box-shadow: var(--shadow-md);
   border: 1px solid var(--gray-200);
   z-index: 10;
}

.page-controls.active {
   display: flex;
}

.page-nav-btn {
   padding: var(--space-2) var(--space-4);
   border: 2px solid var(--primary-500);
   background: white;
   color: var(--primary-600);
   border-radius: var(--radius-md);
   cursor: pointer;
   font-weight: 700;
   transition: var(--transition-normal);
   display: flex;
   align-items: center;
   gap: var(--space-2);
   font-size: var(--font-size-sm);
}

.page-nav-btn:hover:not(:disabled) {
   background: var(--primary-gradient);
   color: white;
   transform: translateY(-2px);
   box-shadow: var(--shadow-primary);
}

.page-nav-btn:active:not(:disabled) {
   transform: translateY(0);
}

.page-nav-btn:disabled {
   opacity: 0.4;
   cursor: not-allowed;
   border-color: var(--gray-300);
   color: var(--gray-400);
}

.page-counter {
   font-family: var(--font-mono);
   font-weight: 700;
   color: var(--gray-700);
   font-size: var(--font-size-md);
   min-width: 80px;
   text-align: center;
   background: var(--primary-50);
   padding: var(--space-2) var(--space-4);
   border-radius: var(--radius-md);
   border: 1px solid var(--primary-200);
}

/* ========== KEYBOARD SHORTCUTS PANEL ========== */
.shortcuts-help-btn {
   position: fixed;
   bottom: 20px;
   right: 20px;
   padding: 10px 16px;
   background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
   color: white;
   border: none;
   border-radius: 25px;
   cursor: pointer;
   font-weight: 600;
   font-size: 13px;
   box-shadow: 0 4px 15px rgba(99, 102, 241, 0.4);
   transition: all 0.3s ease;
   z-index: 1000;
}

.shortcuts-help-btn:hover {
   transform: translateY(-2px);
   box-shadow: 0 6px 20px rgba(99, 102, 241, 0.5);
}

.shortcuts-panel {
   position: fixed;
   bottom: 70px;
   right: 20px;
   width: 320px;
   background: white;
   border-radius: 16px;
   box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
   z-index: 1001;
   display: none;
   overflow: hidden;
}

.shortcuts-panel.show {
   display: block;
}

.shortcuts-header {
   display: flex;
   justify-content: space-between;
   align-items: center;
   padding: 16px 20px;
   background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
   color: white;
   font-weight: 700;
   font-size: 14px;
}

.shortcuts-close-btn {
   background: rgba(255, 255, 255, 0.2);
   border: none;
   color: white;
   width: 28px;
   height: 28px;
   border-radius: 50%;
   cursor: pointer;
   font-size: 14px;
   display: flex;
   align-items: center;
   justify-content: center;
   transition: background 0.2s;
}

.shortcuts-close-btn:hover {
   background: rgba(255, 255, 255, 0.3);
}

.shortcuts-content {
   padding: 16px 20px;
   max-height: 400px;
   overflow-y: auto;
}

.shortcut-group {
   margin-bottom: 16px;
}

.shortcut-group:last-child {
   margin-bottom: 0;
}

.shortcut-title {
   font-weight: 700;
   font-size: 12px;
   color: #6366f1;
   text-transform: uppercase;
   letter-spacing: 0.5px;
   margin-bottom: 8px;
   padding-bottom: 6px;
   border-bottom: 2px solid #e5e7eb;
}

.shortcut-item {
   display: flex;
   align-items: center;
   gap: 6px;
   padding: 6px 0;
   font-size: 13px;
   color: #374151;
}

.shortcut-item span {
   margin-left: auto;
   color: #6b7280;
   font-size: 12px;
}

.shortcut-item kbd {
   display: inline-flex;
   align-items: center;
   justify-content: center;
   min-width: 24px;
   height: 24px;
   padding: 0 6px;
   background: linear-gradient(180deg, #f9fafb, #e5e7eb);
   border: 1px solid #d1d5db;
   border-radius: 6px;
   font-family: 'SF Mono', Monaco, monospace;
   font-size: 11px;
   font-weight: 600;
   color: #374151;
   box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

/* ========== UPLOAD DROP ZONE ========== */
.upload-dropzone {
   border: 2px dashed var(--gray-300);
   border-radius: var(--radius-lg);
   padding: var(--space-6);
   text-align: center;
   transition: var(--transition-normal);
   background: var(--gray-50);
   cursor: pointer;
   margin-bottom: var(--space-4);
}

.upload-dropzone:hover,
.upload-dropzone.dragover {
   border-color: var(--primary-500);
   background: var(--primary-50);
}

.upload-dropzone.dragover {
   transform: scale(1.02);
   box-shadow: var(--shadow-lg);
   border-style: solid;
}

.dropzone-content {
   display: flex;
   flex-direction: column;
   align-items: center;
   gap: var(--space-2);
   pointer-events: none;
}

.dropzone-icon {
   font-size: 36px;
   opacity: 0.6;
   transition: var(--transition-normal);
}

.upload-dropzone:hover .dropzone-icon,
.upload-dropzone.dragover .dropzone-icon {
   opacity: 1;
   transform: scale(1.1);
}

.dropzone-text {
   font-weight: 600;
   color: var(--gray-600);
   font-size: var(--font-size-md);
}

.dropzone-subtext {
   font-size: var(--font-size-sm);
   color: var(--gray-400);
}

/* ========== CANCEL BUTTON ========== */
.header-btn-danger {
   background: linear-gradient(135deg, #ef4444, #dc2626);
   color: white;
   border: 1px solid rgba(239, 68, 68, 0.5);
}

.header-btn-danger:hover {
   background: linear-gradient(135deg, #f87171, #ef4444);
   transform: translateY(-1px);
   box-shadow: 0 4px 15px rgba(239, 68, 68, 0.4);
}

/* ========== UPLOAD PROGRESS ========== */
.upload-progress {
   display: inline-block;
   min-width: 45px;
   font-variant-numeric: tabular-nums;
}

/* === css/canvas.css === */
/* ==============================================
   Canvas & PDF Styles
   LaTeX Editor Lite
   ============================================== */

/* ========== CANVAS CONTAINER ========== */
.canvas-container {
   position: relative;
   width: 800px;
   height: 600px;
   background: white;
   border: 1px solid rgba(0, 0, 0, 0.08);
   border-radius: 0px;
   box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.08);
   overflow: hidden;
}

#pdf-canvas {
   position: absolute;
   top: 0;
   left: 0;
   width: 800px;
   height: 600px;
   z-index: 1;
}

#pdf-overlay {
   position: absolute;
   top: 0;
   left: 0;
   width: 800px;
   height: 600px;
   pointer-events: none;
   z-index: 100;
   transition: background 0.2s;
}

#pdf-overlay.drag-active {
   pointer-events: auto;
   background: rgba(102, 126, 234, 0.1);
   border: 3px dashed #667eea;
}

#hw-layer {
   position: absolute;
   top: 0;
   left: 0;
   width: 800px;
   height: 600px;
   pointer-events: none;
   z-index: 5;
}

/* ========== HANDWRITING ITEMS ========== */
.handwriting-item {
   position: absolute;
   pointer-events: auto;
   cursor: move;
   white-space: nowrap;
   font-family: 'NagwaTKText', 'Times New Roman', serif;
   user-select: none;
   transition: opacity 0.2s;
   line-height: 1.2;
}

/* Reset math-field styling inside handwriting items */
.handwriting-item math-field {
   display: inline-block !important;
   min-height: unset !important;
   height: auto !important;
   width: auto !important;
   font-size: inherit !important;
   padding: 0 !important;
   margin: 0 !important;
   border: none !important;
   border-width: 0 !important;
   border-radius: 0 !important;
   background: transparent !important;
   background-color: transparent !important;
   box-shadow: none !important;
   outline: none !important;
   pointer-events: none !important;
   cursor: move !important;
   vertical-align: baseline !important;
}

/* Disable pointer events on all descendants */
.handwriting-item math-field *,
.handwriting-item .math-ltr-isolate,
.handwriting-item .math-ltr-isolate * {
   pointer-events: none !important;
   cursor: move !important;
}

.handwriting-item:hover {
   opacity: 0.8;
}

.handwriting-item.selected {
   border: none;
   padding: 0;
   background: transparent;
}

/* Force math content to always be LTR */
[dir="rtl"] .ML__base,
[dir="rtl"] .ML__mathrm,
[dir="rtl"] .ML__sqrt,
[dir="rtl"] .ML__sqrt-sign,
[dir="rtl"] .ML__sqrt-line,
.handwriting-item[dir="rtl"] .ML__base,
.handwriting-item[dir="rtl"] span[class^="ML__"],
.handwriting-item[dir="rtl"] span[class*=" ML__"] {
   direction: ltr !important;
   unicode-bidi: embed;
}

/* Editing mode */
.handwriting-item[contenteditable="true"] {
   cursor: text !important;
   user-select: text !important;
   border: 2px solid #667eea !important;
   background: rgba(102, 126, 234, 0.1) !important;
   padding: 4px 8px !important;
   outline: none !important;
   box-shadow: 0 0 10px rgba(102, 126, 234, 0.3) !important;
   z-index: 200 !important;
}

/* ========== ANCHOR POINT ========== */
.anchor-point {
   position: absolute;
   width: 2px;
   height: 2px;
   background: #fcfcfc;
   border: 2px solid white;
   border-radius: 50%;
   pointer-events: auto;
   cursor: move;
   z-index: 1000;
   box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
   bottom: 0.4em;
   left: 0;
   display: none;
   opacity: 0.7;
   transition: opacity 0.2s, transform 0.2s;
}

/* Small red dot - only for selected TEXT items (not shapes) */
.handwriting-item.selected:not(.shape-item) .anchor-point {
   display: block;
   width: 6px;
   height: 6px;
   background: #ef4444;
   border: 2px solid white;
   border-radius: 50%;
   box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
   opacity: 1;
}

.handwriting-item.selected:not(.shape-item) .anchor-point:hover {
   transform: scale(1.3);
   box-shadow: 0 2px 6px rgba(239, 68, 68, 0.5);
}

/* ========== COORDINATES DISPLAY ========== */
#coords-display {
   position: fixed;
   display: none;
   background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
   color: white;
   padding: 6px 12px;
   border-radius: 6px;
   font-size: 13px;
   font-family: 'Courier New', monospace;
   font-weight: 600;
   pointer-events: none;
   z-index: 10000;
   box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3), 0 0 0 2px rgba(255, 255, 255, 0.5);
   opacity: 0;
   transform: scale(0.8);
   transition: opacity 0.15s ease, transform 0.15s ease;
   white-space: nowrap;
   letter-spacing: 0.5px;
}

#coords-display.show {
   opacity: 1;
   transform: scale(1);
}

#coords-display::before {
   content: '📍 ';
   font-size: 14px;
}

/* ========== CONTEXT MENU ========== */
#context-menu {
   position: absolute;
   z-index: 1000;
   background: white;
   padding: 8px;
   border-radius: 8px;
   box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
   display: none;
   flex-direction: row;
   align-items: center;
   gap: 10px;
   border: 1px solid rgba(0, 0, 0, 0.1);
}

.color-swatch {
   width: 24px;
   height: 24px;
   border-radius: 50%;
   cursor: pointer;
   border: 2px solid white;
   box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
   transition: transform 0.2s;
}

.color-swatch:hover {
   transform: scale(1.2);
}

.color-swatch.active {
   box-shadow: 0 0 0 2px #667eea;
}

/* ========== HANDWRITING COLOR MENU (HIDDEN) ========== */
.handwriting-color-menu {
   position: absolute;
   top: -45px;
   left: 50%;
   transform: translateX(-50%);
   background: white;
   padding: 6px 8px;
   border-radius: 8px;
   box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
   display: none;
   flex-direction: row;
   align-items: center;
   gap: 8px;
   border: 1px solid rgba(0, 0, 0, 0.1);
   z-index: 1000;
   white-space: nowrap;
}

.handwriting-item:hover .handwriting-color-menu,
.handwriting-item.selected .handwriting-color-menu {
   display: none;
}

.handwriting-item:hover .handwriting-color-menu.menu-below,
.handwriting-item.selected .handwriting-color-menu.menu-below {
   top: auto;
   bottom: -45px;
}

.handwriting-color-menu .color-swatch {
   width: 20px;
   height: 20px;
   border-radius: 50%;
   cursor: pointer;
   border: 2px solid white;
   box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1);
   transition: transform 0.2s;
}

.handwriting-color-menu .color-swatch:hover {
   transform: scale(1.3);
}

.handwriting-color-menu .color-swatch.active {
   box-shadow: 0 0 0 2px #667eea;
   border: 2px solid #667eea;
}

/* ========== PROPERTY PANEL ========== */
#property-panel {
   background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
   padding: 20px;
   border-radius: 12px;
   margin-bottom: 18px;
   box-shadow: 0 6px 20px rgba(102, 126, 234, 0.3);
   display: none;
}

#property-panel.active {
   display: block;
}

#property-panel h3 {
   margin: 0 0 18px 0;
   color: white;
   font-size: 17px;
   display: flex;
   align-items: center;
   gap: 10px;
   font-weight: 700;
   text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.property-grid {
   display: grid;
   grid-template-columns: repeat(2, 1fr);
   gap: 14px;
}

.property-item {
   background: white;
   padding: 14px;
   border-radius: 10px;
   box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.property-item label {
   display: block;
   font-size: 11px;
   font-weight: 700;
   color: #64748b;
   margin-bottom: 8px;
   text-transform: uppercase;
   letter-spacing: 0.8px;
}

.property-item input,
.property-item select {
   width: 100%;
   padding: 10px 12px;
   border: 2px solid #e2e8f0;
   border-radius: 8px;
   font-size: 14px;
   transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
   background: white;
   font-weight: 500;
}

.property-item input:focus,
.property-item select:focus {
   outline: none;
   border-color: #667eea;
   box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
   transform: translateY(-1px);
}

/* ========== STATUS MESSAGES ========== */
.status-message {
   position: fixed;
   top: 90px;
   right: 30px;
   background: white;
   padding: 18px 24px;
   border-radius: 12px;
   box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15), 0 2px 8px rgba(0, 0, 0, 0.1);
   z-index: 10000;
   display: none;
   max-width: 380px;
   border-left: 4px solid #667eea;
}

.status-message.success {
   border-left-color: #22c55e;
   background: linear-gradient(135deg, #f0fdf4 0%, white 100%);
}

.status-message.error {
   border-left-color: #ef4444;
   background: linear-gradient(135deg, #fef2f2 0%, white 100%);
}

.status-message.show {
   display: block;
}

/* ========== GRID OVERLAY ========== */
.grid-overlay {
   position: absolute;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   pointer-events: none;
   z-index: 100;
}

.grid-line {
   position: absolute;
   background: rgba(124, 58, 237, 0.4);
   pointer-events: none;
}

.grid-line.horizontal {
   left: 0;
   right: 0;
   height: 1px;
}

.grid-line.vertical {
   top: 0;
   bottom: 0;
   width: 1px;
}

.grid-point {
   position: absolute;
   width: 4px;
   height: 4px;
   background: rgba(124, 58, 237, 0.8);
   border-radius: 50%;
   transform: translate(-50%, -50%);
   cursor: crosshair;
   pointer-events: auto;
   transition: all 0.15s ease;
}

.grid-point:hover {
   background: #ef4444;
   transform: translate(-50%, -50%) scale(1.5);
}

.grid-tooltip {
   position: fixed;
   background: rgba(15, 15, 20, 0.95);
   color: #f8fafc;
   padding: 6px 12px;
   border-radius: 6px;
   font-family: monospace;
   font-size: 12px;
   z-index: 1000;
   pointer-events: none;
   border: 1px solid rgba(124, 58, 237, 0.5);
}

#grid-toggle-btn.active {
   background: linear-gradient(135deg, #7c3aed, #06b6d4) !important;
   color: white !important;
}

/* ========== SLIDE LOADER ========== */
.slide-loader {
   position: absolute;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   background: rgba(30, 41, 59, 0.85);
   /* Dark semi-transparent overlay */
   z-index: 2000;
   /* Above PDF overlay and handwriting */
   display: flex;
   flex-direction: column;
   align-items: center;
   justify-content: center;
   gap: 16px;
   backdrop-filter: blur(4px);
   transition: opacity 0.3s ease;
}

.slide-loader .spinner {
   width: 40px;
   height: 40px;
   border: 4px solid rgba(255, 255, 255, 0.3);
   border-radius: 50%;
   border-top-color: #667eea;
   animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
   to {
      transform: rotate(360deg);
   }
}

/* === css/modals.css === */
/* ==============================================
   Modal & Dialog Styles
   LaTeX Editor Lite
   ============================================== */

/* ========== MATHLIVE KEYBOARD POPUP ========== */
.mathlive-container {
   position: fixed;
   bottom: var(--space-6);
   left: 50%;
   transform: translateX(-50%);
   z-index: var(--z-modal);
   background: white;
   border-radius: var(--radius-xl);
   box-shadow: var(--shadow-xl);
   padding: var(--space-4);
   display: none;
   width: 90%;
   max-width: 600px;
   border: 1px solid var(--gray-200);
}

.mathlive-container.show {
   display: block;
}

.mathlive-header {
   display: flex;
   align-items: center;
   justify-content: space-between;
   margin-bottom: var(--space-3);
   padding-bottom: var(--space-3);
   border-bottom: 1px solid var(--gray-200);
}

.mathlive-title {
   font-weight: 700;
   color: var(--gray-800);
   font-size: var(--font-size-md);
   display: flex;
   align-items: center;
   gap: var(--space-2);
}

.mathlive-close {
   width: 32px;
   height: 32px;
   border: none;
   background: var(--gray-100);
   border-radius: var(--radius-full);
   cursor: pointer;
   display: flex;
   align-items: center;
   justify-content: center;
   font-size: 18px;
   color: var(--gray-500);
   transition: var(--transition-fast);
}

.mathlive-close:hover {
   background: var(--accent-red-light);
   color: var(--accent-red);
}

/* Math Field inside MathLive Popup only */
#mathlive-popup math-field,
.mathlive-container>math-field {
   display: block;
   width: 100%;
   min-height: 50px;
   font-size: 22px;
   padding: var(--space-3);
   border: 2px solid var(--gray-200);
   border-radius: var(--radius-md);
   background: var(--gray-50);
   margin-bottom: var(--space-3);
}

math-field:focus-within {
   border-color: var(--primary-400);
   background: white;
   box-shadow: 0 0 0 3px rgba(104, 148, 208, 0.2);
}

.mathlive-actions {
   display: flex;
   gap: var(--space-2);
   justify-content: flex-end;
}

.mathlive-insert-btn {
   padding: var(--space-2) var(--space-4);
   background: var(--primary-gradient);
   color: white;
   border: none;
   border-radius: var(--radius-md);
   font-weight: 600;
   cursor: pointer;
   transition: var(--transition-fast);
}

.mathlive-insert-btn:hover {
   transform: translateY(-1px);
   box-shadow: var(--shadow-primary);
}

.mathlive-toggle-btn {
   display: inline-flex;
   padding: 6px 12px;
   border: 2px solid var(--primary-400);
   background: linear-gradient(135deg, var(--primary-50) 0%, white 100%);
   border-radius: var(--radius-md);
   cursor: pointer;
   align-items: center;
   justify-content: center;
   font-size: 14px;
   font-weight: 600;
   color: var(--primary-600);
   transition: var(--transition-fast);
   flex-shrink: 0;
   gap: 4px;
}

.mathlive-toggle-btn:hover {
   background: var(--primary-100);
   border-color: var(--primary-400);
   color: var(--primary-600);
}

/* ========== SESSION EXISTS DIALOG ========== */
.session-dialog-overlay {
   position: fixed;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   background: rgba(65, 67, 70, 0.5);
   backdrop-filter: blur(8px);
   display: flex;
   align-items: center;
   justify-content: center;
   z-index: 10000;
   opacity: 0;
   visibility: hidden;
   transition: all 0.25s var(--transition-normal);
}

.session-dialog-overlay.visible {
   opacity: 1;
   visibility: visible;
}

.session-dialog {
   background: white;
   border: 1px solid var(--gray-200);
   border-radius: var(--radius-xl);
   padding: var(--space-6) var(--space-8);
   max-width: 440px;
   width: 90%;
   box-shadow: var(--shadow-xl), 0 0 0 1px rgba(104, 148, 208, 0.1);
   transform: scale(0.9) translateY(20px);
   transition: transform 0.25s var(--transition-normal);
}

.session-dialog-overlay.visible .session-dialog {
   transform: scale(1) translateY(0);
}

.session-dialog h3 {
   color: var(--accent-orange);
   margin: 0 0 var(--space-3) 0;
   font-size: var(--font-size-xl);
   font-weight: 700;
   display: flex;
   align-items: center;
   gap: var(--space-2);
}

.session-dialog p {
   color: var(--gray-600);
   margin: 0 0 var(--space-2) 0;
   font-size: var(--font-size-md);
   line-height: 1.6;
}

.session-dialog .session-info {
   background: linear-gradient(135deg, var(--primary-50) 0%, var(--gray-50) 100%);
   border: 1px solid var(--gray-200);
   border-radius: var(--radius-md);
   padding: var(--space-3) var(--space-4);
   margin: var(--space-4) 0;
}

.session-dialog .session-info strong {
   color: var(--gray-700);
   font-weight: 600;
}

.session-dialog .session-info span {
   color: var(--primary-600);
   font-weight: 500;
}

.session-dialog .dialog-buttons {
   display: flex;
   gap: var(--space-3);
   margin-top: var(--space-5);
}

.session-dialog button {
   flex: 1;
   padding: var(--space-3) var(--space-5);
   border-radius: var(--radius-md);
   font-size: var(--font-size-md);
   font-weight: 600;
   cursor: pointer;
   transition: all 0.15s ease;
   border: none;
}

.session-dialog .btn-continue {
   background: var(--accent-green);
   color: white;
   box-shadow: var(--shadow-success);
}

.session-dialog .btn-continue:hover {
   transform: translateY(-2px);
   box-shadow: 0 6px 20px rgba(34, 197, 94, 0.4);
   background: #1db954;
}

.session-dialog .btn-fresh {
   background: var(--accent-orange);
   color: white;
   box-shadow: 0 4px 12px rgba(224, 147, 39, 0.25);
}

.session-dialog .btn-fresh:hover {
   transform: translateY(-2px);
   box-shadow: 0 6px 20px rgba(224, 147, 39, 0.4);
   filter: brightness(1.05);
}

.session-dialog .btn-cancel {
   background: var(--gray-100);
   color: var(--gray-600);
   border: 1px solid var(--gray-200);
   flex: 0.6;
}

.session-dialog .btn-cancel:hover {
   background: var(--gray-200);
   color: var(--gray-700);
}

/* ========== GRID SETTINGS MODAL ========== */
.grid-modal-overlay {
   position: fixed;
   top: 0;
   left: 0;
   right: 0;
   bottom: 0;
   background: rgba(0, 0, 0, 0.7);
   display: flex;
   align-items: center;
   justify-content: center;
   z-index: 2000;
   backdrop-filter: blur(4px);
}

.grid-modal {
   background: linear-gradient(145deg, #1e1e2a, #16161e);
   border-radius: 16px;
   min-width: 320px;
   box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
   border: 1px solid rgba(255, 255, 255, 0.1);
}

.grid-modal-header {
   display: flex;
   align-items: center;
   gap: 10px;
   padding: 20px 24px 16px;
   border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.grid-modal-header span {
   font-size: 1.5rem;
}

.grid-modal-header h3 {
   margin: 0;
   color: #f8fafc;
   font-size: 1.1rem;
}

.grid-modal-body {
   padding: 20px 24px;
}

.grid-input-group {
   margin-bottom: 16px;
}

.grid-input-group label {
   display: block;
   color: #94a3b8;
   font-size: 0.85rem;
   margin-bottom: 6px;
}

.grid-input-group input {
   width: 100%;
   padding: 10px 14px;
   background: rgba(255, 255, 255, 0.05);
   border: 1px solid rgba(255, 255, 255, 0.15);
   border-radius: 8px;
   color: #f8fafc;
   font-size: 1rem;
   transition: border-color 0.2s;
}

.grid-input-group input:focus {
   outline: none;
   border-color: #7c3aed;
}

.grid-modal-footer {
   display: flex;
   gap: 12px;
   padding: 16px 24px 20px;
   border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.grid-modal-btn {
   flex: 1;
   padding: 10px 20px;
   border-radius: 8px;
   font-size: 0.95rem;
   font-weight: 600;
   cursor: pointer;
   transition: all 0.15s ease;
   border: none;
}

.grid-modal-btn.cancel {
   background: rgba(255, 255, 255, 0.1);
   color: #a0a0b0;
}

.grid-modal-btn.cancel:hover {
   background: rgba(255, 255, 255, 0.15);
}

.grid-modal-btn.confirm {
   background: linear-gradient(135deg, #7c3aed, #06b6d4);
   color: white;
}

.grid-modal-btn.confirm:hover {
   transform: translateY(-1px);
   box-shadow: 0 4px 20px rgba(124, 58, 237, 0.4);
}

/* ========== TOAST NOTIFICATIONS ========== */
.toast-container {
   position: fixed;
   top: var(--space-6);
   right: var(--space-6);
   z-index: var(--z-toast);
   display: flex;
   flex-direction: column;
   gap: var(--space-3);
   pointer-events: none;
}

.toast {
   background: white;
   border-radius: var(--radius-md);
   padding: var(--space-3) var(--space-4);
   box-shadow: var(--shadow-lg);
   display: flex;
   align-items: center;
   gap: var(--space-3);
   min-width: 280px;
   max-width: 400px;
   pointer-events: auto;
   border-left: 4px solid var(--gray-400);
}

.toast.success {
   border-left-color: var(--accent-green);
   background: linear-gradient(135deg, white 0%, var(--accent-green-light) 100%);
}

.toast.error {
   border-left-color: var(--accent-red);
   background: linear-gradient(135deg, white 0%, var(--accent-red-light) 100%);
}

.toast.warning {
   border-left-color: var(--accent-orange);
   background: linear-gradient(135deg, white 0%, var(--accent-orange-light) 100%);
}

.toast.info {
   border-left-color: var(--accent-blue);
   background: linear-gradient(135deg, white 0%, var(--accent-blue-light) 100%);
}

.toast-icon {
   font-size: 20px;
   flex-shrink: 0;
}

.toast-message {
   font-size: var(--font-size-sm);
   font-weight: 500;
   color: var(--gray-800);
   flex: 1;
}

.toast-close {
   background: none;
   border: none;
   color: var(--gray-400);
   cursor: pointer;
   padding: var(--space-1);
   font-size: 18px;
   line-height: 1;
   transition: var(--transition-fast);
}

.toast-close:hover {
   color: var(--gray-600);
}

.toast.hiding {
   animation: toastSlideOut 0.3s ease-in forwards;
}

/* ========== LOADING STATES ========== */
.loading-spinner {
   width: 20px;
   height: 20px;
   border: 2px solid transparent;
   border-top-color: currentColor;
   border-radius: 50%;
   animation: spin 0.8s linear infinite;
}

/* Button loading state */
.btn-loading {
   position: relative;
   pointer-events: none;
   opacity: 0.7;
}

.btn-loading::after {
   content: '';
   position: absolute;
   width: 16px;
   height: 16px;
   top: 50%;
   left: 50%;
   margin-top: -8px;
   margin-left: -8px;
   border: 2px solid transparent;
   border-top-color: currentColor;
   border-radius: 50%;
   animation: spin 0.8s linear infinite;
}

.btn-loading span {
   visibility: hidden;
}

/* Skeleton loading */
.skeleton {
   background: linear-gradient(90deg,
         var(--gray-200) 25%,
         var(--gray-100) 50%,
         var(--gray-200) 75%);
   background-size: 200% 100%;
   animation: skeleton-loading 1.5s infinite;
   border-radius: var(--radius-sm);
}

/* Pulse animation for status indicators */
.pulse {
   animation: pulse 2s infinite;
}

/* === css/animations.css === */
/* ==============================================
   Animations & Keyframes
   LaTeX Editor Lite
   ============================================== */

/* ========== SPINNER ANIMATION ========== */
.spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    border-top-color: #fff;
    animation: spin 0.8s ease-in-out infinite;
    vertical-align: middle;
    margin-right: 6px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ========== SLIDE ANIMATIONS ========== */
@keyframes slideUp {
    from {
        transform: translateX(-50%) translateY(20px);
        opacity: 0;
    }

    to {
        transform: translateX(-50%) translateY(0);
        opacity: 1;
    }
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideInRight {
    from {
        transform: translateX(450px);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}

/* ========== TOAST ANIMATIONS ========== */
@keyframes toastSlideIn {
    from {
        transform: translateX(100%);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes toastSlideOut {
    from {
        transform: translateX(0);
        opacity: 1;
    }

    to {
        transform: translateX(100%);
        opacity: 0;
    }
}

/* ========== FADE TRANSITIONS ========== */
.fade-in {
    animation: fadeIn 0.3s ease-out;
}

.fade-out {
    animation: fadeOut 0.3s ease-in forwards;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes fadeOut {
    from {
        opacity: 1;
    }

    to {
        opacity: 0;
    }
}

/* ========== SKELETON LOADING ========== */
@keyframes skeleton-loading {
    0% {
        background-position: 200% 0;
    }

    100% {
        background-position: -200% 0;
    }
}

/* ========== PULSE ANIMATION ========== */
@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.5;
    }
}

/* ========== MATH DIRECTION ISOLATION ========== */
/* Force ALL MathLive elements to ALWAYS be LTR - Maximum Priority */

.ML__mathlive,
[class*="ML__"],
span[class*="ML__"],
.math-ltr-isolate,
.handwriting-item .ML__mathlive,
.handwriting-item [class*="ML__"],
.palette-item .ML__mathlive,
.palette-item [class*="ML__"] {
    direction: ltr !important;
    unicode-bidi: isolate !important;
    text-align: left !important;
}

/* Handwriting items on PDF - STRONGEST isolation */
.handwriting-item {
    isolation: isolate;
    contain: layout style;
}

/* Force math content even in RTL containers */
.handwriting-item[dir="rtl"] .ML__mathlive,
.handwriting-item[dir="rtl"] [class*="ML__"],
[dir="rtl"] .handwriting-item .ML__mathlive,
[dir="rtl"] .math-ltr-isolate {
    direction: ltr !important;
    unicode-bidi: embed !important;
    display: inline-block !important;
}

/* Specific MathLive components */
.ML__base,
.ML__mathit,
.ML__mathrm,
.ML__cmr,
.ML__sqrt,
.ML__sqrt-sign,
.ML__sqrt-line {
    direction: ltr !important;
    unicode-bidi: isolate !important;
}

/* Force LTR for handwriting items containing math */
.handwriting-item:has([class*="ML__"]),
.handwriting-item:has(.math-ltr-isolate) {
    direction: ltr !important;
    text-align: left !important;
    unicode-bidi: isolate !important;
}

/* Prevent SVG mirroring - CRITICAL for √ symbol */
.handwriting-item svg,
.handwriting-item [class*="ML__"] svg,
.ML__mathlive svg,
[class*="ML__"] svg {
    direction: ltr !important;
    transform: scale(1, 1) !important;
}

/* Specific fix for MathLive square root */
.ML__sqrt,
.ML__sqrt-sign,
.ML__sqrt-line,
.ML__small-delim {
    direction: ltr !important;
    unicode-bidi: isolate !important;
}

/* Maximum priority isolation for RTL contexts */
[dir="rtl"] .handwriting-item:has([class*="ML__"]) {
    isolation: isolate !important;
}

/* ========== MIXED CONTENT SEGMENTS ========== */
/* For mixed Arabic+Math content: container stays RTL, segments handle their own direction */
.handwriting-item.has-mixed-content {
    isolation: isolate !important;
    writing-mode: horizontal-tb !important;
    /* NO direction override here - let segments control it */
}

/* Text segments: Use proper text font, respect their own direction */
.handwriting-item .text-segment {
    unicode-bidi: isolate !important;
    display: inline !important;
}

/* Arabic text segments: Use Arabic-compatible font */
.handwriting-item .text-segment.arabic-text {
    font-family: 'NagwaTKText', 'Noto Sans Arabic', 'Arial', sans-serif !important;
}

/* Math segments in mixed content: NO direction override to preserve BiDi ordering */
.handwriting-item .math-segment {
    unicode-bidi: isolate !important;
    font-family: 'NagwaTKMath', 'Latin Modern Math', serif !important;
    display: inline-block !important;
    /* Note: direction NOT set here - math-field handles its own LTR layout internally */
}

/* Pure math wrapper: forces LTR for standalone math content */
.handwriting-item .math-ltr-isolate {
    direction: ltr !important;
    unicode-bidi: isolate !important;
    font-family: 'NagwaTKMath', 'Latin Modern Math', serif !important;
    display: inline-block !important;
}

/* Pure math items (no mixed text): container is LTR */
.handwriting-item.has-math:not(.has-mixed-content) {
    isolation: isolate !important;
    writing-mode: horizontal-tb !important;
    direction: ltr !important;
    unicode-bidi: isolate !important;
    font-family: 'NagwaTKMath', 'Latin Modern Math', serif !important;
}

/* Absolute SVG coordinate system fix */
.handwriting-item svg,
.handwriting-item [class*="ML__"] svg,
.ML__sqrt-sign svg,
.math-ltr-isolate svg,
.math-segment svg,
.has-math svg {
    direction: ltr !important;
    transform: matrix(1, 0, 0, 1, 0, 0) !important;
    -webkit-transform: matrix(1, 0, 0, 1, 0, 0) !important;
    transform-origin: left top !important;
    writing-mode: horizontal-tb !important;
}

/* Extra protection for SVG in ANY RTL context eeee*/
[dir="rtl"] svg,
:dir(rtl) svg,
html[dir="rtl"] .handwriting-item svg,
body[dir="rtl"] .handwriting-item svg,
[dir="rtl"] .math-ltr-isolate svg {
    transform: matrix(1, 0, 0, 1, 0, 0) !important;
    -webkit-transform: matrix(1, 0, 0, 1, 0, 0) !important;
    direction: ltr !important;
}

/* Isolate math wrapper completely */
.math-ltr-isolate {
    display: inline-block !important;
    direction: ltr !important;
    unicode-bidi: isolate-override !important;
    writing-mode: horizontal-tb !important;
    isolation: isolate !important;
    position: relative !important;
}

/* === css/drawing.css === */
/* Drawing Tools Styles
 * ====================
 * Styles for the drawing canvas and shape rendering
 */

/* ============= Drawing Tools Dropdown ============= */

.drawing-tools-dropdown {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.drawing-toggle-btn {
    padding: 6px 12px;
    border: 1px solid #e2e8f0;
    border-radius: 6px;
    background: white;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.drawing-toggle-btn:hover {
    background: #f1f5f9;
    border-color: #cbd5e1;
}

.drawing-toggle-btn.active {
    background: linear-gradient(135deg, #3b82f6, #2563eb);
    color: white;
    border-color: #3b82f6;
}

.dropdown-arrow {
    font-size: 8px;
    transition: transform 0.2s ease;
}

.drawing-tools-dropdown.open .dropdown-arrow {
    transform: rotate(180deg);
}

/* Dropdown Menu */
.drawing-dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    margin-top: 4px;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    z-index: 1000;
    min-width: 140px;
    padding: 8px 0;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-10px);
    transition: all 0.2s ease;
}

.drawing-tools-dropdown.open .drawing-dropdown-menu {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.drawing-menu-section {
    padding: 0 8px;
}

.menu-section-title {
    display: block;
    font-size: 10px;
    font-weight: 600;
    color: #94a3b8;
    text-transform: uppercase;
    padding: 4px 8px;
    letter-spacing: 0.5px;
}

.dropdown-draw-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 12px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 12px;
    text-align: left;
    direction: ltr;
    transition: all 0.15s ease;
    border-radius: 4px;
    margin: 2px 0;
}

.dropdown-draw-btn:hover {
    background: #f1f5f9;
}

.dropdown-draw-btn.active {
    background: linear-gradient(135deg, #3b82f6, #2563eb);
    color: white;
}

.dropdown-draw-btn.toggle-option.active {
    background: #10b981;
    color: white;
}

.drawing-menu-divider {
    height: 1px;
    background: #e2e8f0;
    margin: 8px 0;
}

/* Cancel Button */
.draw-cancel {
    background: #fef2f2 !important;
    color: #dc2626 !important;
    border-color: #fecaca !important;
}

.draw-cancel:hover {
    background: #fee2e2 !important;
    border-color: #fca5a5 !important;
}

/* ============= Drawing Layer (SVG Overlay) ============= */

.drawing-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 15;
}

.drawing-layer.active {
    pointer-events: auto;
    cursor: crosshair;
}

/* Preview elements */
.drawing-preview {
    opacity: 0.7;
    stroke-dasharray: 5, 5;
    animation: dash-animation 0.5s linear infinite;
}

@keyframes dash-animation {
    to {
        stroke-dashoffset: 10;
    }
}

/* ============= Canvas Drawing Mode ============= */

.canvas-container.drawing-mode {
    cursor: crosshair;
}

.canvas-container.drawing-mode::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    border: 3px solid #3b82f6;
    border-radius: 8px;
    pointer-events: none;
    animation: drawing-mode-pulse 2s ease-in-out infinite;
    z-index: 100;
}

@keyframes drawing-mode-pulse {

    0%,
    100% {
        border-color: rgba(59, 130, 246, 0.5);
    }

    50% {
        border-color: rgba(59, 130, 246, 1);
    }
}

.canvas-container.drawing-mode .handwriting-item {
    pointer-events: none;
    opacity: 0.5;
}

.canvas-container.drawing-mode #drawing-layer {
    pointer-events: auto;
}

/* ============= Shape Items ============= */

.shape-item {
    background: transparent !important;
    box-shadow: none !important;
    border: none !important;
}

.shape-item:hover {
    outline: 2px dashed #3b82f6;
    outline-offset: 2px;
}

.shape-item.selected {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.2) !important;
}

.shape-svg {
    display: block;
    overflow: visible;
}

/* Shape anchor point */
.shape-anchor {
    width: 8px;
    height: 8px;
    background: #3b82f6;
    border: 2px solid white;
    border-radius: 50%;
    position: absolute;
    top: -4px;
    left: -4px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    opacity: 0;
    transition: opacity 0.2s;
}

.shape-item:hover .shape-anchor,
.shape-item.selected .shape-anchor {
    opacity: 1;
}

/* Shape type indicators */
.shape-item[data-shape-type="line"] .shape-anchor {
    background: #ef4444;
}

.shape-item[data-shape-type="rect"] .shape-anchor {
    background: #3b82f6;
}

.shape-item[data-shape-type="circle"] .shape-anchor {
    background: #10b981;
}

.shape-item[data-shape-type="freehand"] .shape-anchor {
    background: #f59e0b;
}

/* ============= Drawing Mode Indicator Badge ============= */

.drawing-mode-badge {
    position: fixed;
    top: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: linear-gradient(135deg, #3b82f6, #8b5cf6);
    color: white;
    padding: 8px 20px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
    box-shadow: 0 4px 15px rgba(59, 130, 246, 0.4);
    z-index: 1000;
    display: flex;
    align-items: center;
    gap: 8px;
    animation: badge-slide-in 0.3s ease-out;
}

@keyframes badge-slide-in {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(-10px);
    }

    to {
        opacity: 1;
        transform: translateX(-50%) translateY(0);
    }
}

.drawing-mode-badge .badge-icon {
    font-size: 16px;
}

.drawing-mode-badge .badge-text {
    border-left: 1px solid rgba(255, 255, 255, 0.3);
    padding-left: 8px;
}

/* ============= Responsive Adjustments ============= */

@media (max-width: 1200px) {
    .toolbar-draw-btn {
        padding: 5px 8px;
        font-size: 10px;
    }

    .toolbar-draw-btn span:not(.icon) {
        display: none;
    }

    .drawing-tools-group {
        gap: 2px;
    }
}

@media (max-width: 900px) {
    .drawing-tools-group {
        display: none;
    }
}
