/* إعدادات الألوان العصرية (ثيم دلال) */
        :root {
            --paper-bg: #f8fafc;      /* Slate 50 */
            --paper-dark: #e2e8f0;    /* Slate 200 */
            --primary-color: #6366f1; /* Indigo 500 */
            --primary-hover: #4f46e5; /* Indigo 600 */
            --accent-color: #ec4899;  /* Pink 500 */
            --text-dark: #1e293b;     /* Slate 800 */
            --border-color: #cbd5e1;  /* Slate 300 */
        }

        /* إعدادات أساسية */
        body { 
            font-family: 'Cairo', sans-serif; 
            background-color: var(--paper-bg); 
            color: var(--text-dark); 
            user-select: none; 
            -webkit-tap-highlight-color: transparent; 
            overscroll-behavior: none;
            background-image: radial-gradient(var(--border-color) 1px, transparent 1px);
            background-size: 20px 20px;
        }
        
        /* شاشة التحميل */
        #startup-overlay { 
            position: fixed; inset: 0; 
            background: #1e1b4b; 
            z-index: 99999; 
            display: flex; flex-direction: column; justify-content: center; align-items: center; 
            transition: opacity 0.3s; 
        }
        #startup-overlay .spinner {
            width: 50px; height: 50px; 
            border: 5px solid #312e81; 
            border-top-color: var(--primary-color); 
            border-radius: 50%; 
            animation: spin 1s linear infinite;
        }
        #startup-overlay .loading-text {
            color: white; 
            font-weight: bold; 
            font-family: 'Cairo', sans-serif;
            margin-top: 10px;
        }

        /* شاشة التصدير */
        #export-overlay { 
            position: fixed; inset: 0; 
            background: rgba(30, 27, 75, 0.95); 
            z-index: 100000; 
            display: none; 
            flex-direction: column; justify-content: center; align-items: center; 
            transition: opacity 0.3s; 
        }
        #export-overlay .spinner { 
            width: 50px; height: 50px; 
            border: 5px solid rgba(255,255,255,0.1); 
            border-top-color: var(--accent-color); 
            border-radius: 50%; 
            animation: spin 1s linear infinite; 
        }
        
        @keyframes spin { to { transform: rotate(360deg); } }

        /* منطقة العمل */
        .preview-area {
            background-color: white;
            border-radius: 20px; min-height: 400px; display: flex; justify-content: center; align-items: flex-start;
            overflow: auto; 
            border: 1px solid var(--border-color);
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05), 0 2px 4px -1px rgba(0, 0, 0, 0.03);
            padding: 50px; position: relative; /* increased padding for rulers */
        }

        /* حاوي البطاقة والمسطرة */
        #card-wrapper {
            position: relative;
            margin: 0 auto 40px auto;
            transform-origin: center top;
            transform: scale(var(--card-zoom, 1));
            transition: transform 0.2s ease;
            display: inline-block;
        }

        /* Crop Overlay */
        #crop-overlay {
            z-index: 50000;
        }

        /* البطاقة الرئيسية */
        #card {
            background-color: white; 
            position: relative; 
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04); 
            overflow: hidden; 
            /* هامش وتحويل تم نقلهما للغلاف للحفاظ على المسطرة */
            margin: 0;
            transform: none;
            direction: rtl;
            width: 100%; height: 100%;
            flex-shrink: 0; 
        }

        /* تنسيق المساطر */
        .ruler {
            position: absolute;
            background-color: #f1f5f9;
            pointer-events: none;
            overflow: visible;
            z-index: 50;
            font-family: sans-serif;
            font-size: 8px;
            color: #64748b;
        }
        .ruler-h {
            top: -20px;
            left: 0;
            right: 0;
            height: 20px;
            border-bottom: 1px solid #cbd5e1;
        }
        .ruler-v {
            top: 0;
            left: -20px;
            bottom: 0;
            width: 20px;
            border-right: 1px solid #cbd5e1;
        }
        .tick {
            position: absolute;
            background-color: #cbd5e1;
        }
        .tick-num {
            position: absolute;
            font-size: 10px; /* تم التكبير */
            font-weight: 900; /* خط أعرض */
            color: #475569;
        }
        /* Vertical Numbers */
        .ruler-v .tick-num {
            transform: rotate(-90deg);
            transform-origin: center;
        }

        /* التدرج اللوني */
        #card-gradient {
            position: absolute; 
            bottom: 0; left: 0; right: 0; 
            height: 50%; 
            background: linear-gradient(to top, rgba(0,0,0,0.8), transparent);
            z-index: 10; 
            pointer-events: none; 
            display: none;
        }

        /* العناصر القابلة للسحب */
        .draggable-el { 
            position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); 
            cursor: grab; touch-action: none; 
            border: 1px solid transparent;
        }
        .draggable-el:active { cursor: grabbing; }
        
        /* حالة القفل */
        .draggable-el.is-locked { cursor: default; }

        /* طبقات الصور والنصوص والإطارات */
        .image-layer { /* z-index يتم التحكم به من JavaScript */ }
        .image-layer.bg-image { width: 100% !important; height: 100% !important; top: 0 !important; left: 0 !important; transform: none !important; z-index: 1 !important; }
        .image-layer img { width: 100%; height: 100%; object-fit: fill; pointer-events: auto; }
        
        /* العناصر القابلة للتلوين */
        .image-layer[data-colorable="true"] { 
            background-color: transparent;
            overflow: visible;
        }
        .image-layer[data-colorable="true"]::before {
            content: '';
            position: absolute;
            inset: 0;
            z-index: 0;
            background: transparent;
        }
        .image-layer[data-colorable="true"] .content-wrapper {
            width: 100%;
            height: 100%;
            position: relative;
            pointer-events: auto;
            z-index: 1;
        }
        
        .frame-layer { width: 80%; height: 80%; background: transparent; border-style: solid; border-width: 2px; border-color: #334155; pointer-events: auto; box-shadow: 0 0 0 3px transparent; transition: box-shadow 0.2s; }
        
        /* النصوص */
        .text-layer { width: auto; min-width: 100px; text-align: center; padding: 5px; cursor: text; }
        .user-text { 
            line-height: 1.6; display: block; white-space: pre-wrap; outline: none; 
            background: transparent; border: none; text-align: center; width: 100%; 
            resize: none; overflow: visible; 
            user-select: text !important;
            -webkit-user-select: text !important;
            cursor: text;
            touch-action: manipulation; 
            pointer-events: auto;
            -webkit-touch-callout: none;
        }
        
        /* أدوات التحكم (المقابض) */
        .draggable-el.selected { border: 1px dashed var(--primary-color); z-index: 100; }
        .draggable-el.selected.is-locked { border-color: #ef4444; border-style: dotted; }
        
        .handle { position: absolute; background: white; border: 2.5px solid var(--primary-color); width: 26px; height: 26px; border-radius: 50%; z-index: 101; display: none; box-shadow: 0 2px 5px rgba(0,0,0,0.3); }
        .draggable-el.selected .handle { display: block; }
        
        /* اتجاهات المقابض */
        .resize-nw { top: -13px; left: -13px; cursor: nw-resize; }
        .resize-ne { top: -13px; right: -13px; cursor: ne-resize; }
        .resize-sw { bottom: -13px; left: -13px; cursor: sw-resize; }
        .resize-se { bottom: -13px; right: -13px; cursor: se-resize; }
        .resize-n { top: -13px; left: 50%; transform: translateX(-50%); cursor: n-resize; }
        .resize-e { top: 50%; right: -13px; transform: translateY(-50%); cursor: e-resize; }
        .resize-s { bottom: -13px; left: 50%; transform: translateX(-50%); cursor: s-resize; }
        .resize-w { top: 50%; left: -13px; transform: translateY(-50%); cursor: w-resize; }
        
        .control-btn { position: absolute; width: 36px; height: 36px; border-radius: 50%; color: white; display: none; align-items: center; justify-content: center; font-size: 16px; cursor: pointer; z-index: 102; box-shadow: 0 2px 6px rgba(0,0,0,0.3); transition: transform 0.2s; transform: scale(max(0.5, min(1.5, calc(1 / var(--card-zoom, 1))))); transform-origin: center center; }
        .control-btn:hover { transform: scale(max(0.55, min(1.65, calc(1.1 / var(--card-zoom, 1))))); }
        .delete-btn { top: -50px; left: -18px; background: #ef4444; }
        .duplicate-btn { top: -50px; left: 28px; background: #3b82f6; }
        .rotate-btn { top: -50px; left: 74px; background: #8b5cf6; }
        .layer-up-btn { top: -50px; left: 120px; background: #64748b; }
        .layer-down-btn { top: -50px; left: 166px; background: #64748b; }
        .move-handle { position: absolute; top: -50px; left: 50%; transform: translateX(-50%) scale(max(0.5, min(1.5, calc(1 / var(--card-zoom, 1))))); transform-origin: center center; width: 56px; height: 40px; background: var(--primary-color); border-radius: 8px; cursor: move; display: none; align-items: center; justify-content: center; z-index: 102; color: white; box-shadow: 0 2px 6px rgba(0,0,0,0.3); transition: transform 0.2s; font-size: 16px; }
        .move-handle:hover { transform: translateX(-50%) scale(max(0.55, min(1.65, calc(1.1 / var(--card-zoom, 1))))); }
        .handle { transform: scale(max(0.5, min(1.5, calc(1 / var(--card-zoom, 1))))); transform-origin: center center; }

        
        .draggable-el.selected .control-btn, .draggable-el.selected .move-handle { display: flex; }
        .draggable-el.is-locked .control-btn, .draggable-el.is-locked .handle, .draggable-el.is-locked .move-handle { display: none !important; }

        /* عناصر مكتبة الأصول */
        .asset-item { transition: all 0.2s ease; border: 2px solid transparent; }
        .asset-item:hover { border-color: #6366f1; transform: scale(1.05); }
        #assets-grid::-webkit-scrollbar { width: 6px; }
        #assets-grid::-webkit-scrollbar-track { background: #f1f5f9; border-radius: 3px; }
        #assets-grid::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
        #assets-grid::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

        /* مودال الحفظ */
        #save-modal { display: none; position: fixed; inset: 0; background: rgba(15, 23, 42, 0.98); z-index: 100000; align-items: center; justify-content: center; padding: 0; }
        #save-img { border: none; border-radius: 0; box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
        
        /* النافذة العائمة للأصول (Assets) */
        #asset-window {
            position: fixed;
            top: 20%; left: 20%;
            width: 250px;
            max-height: 400px;
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
            border: 1px solid #cbd5e1;
            z-index: 5000;
            display: none;
            flex-direction: column;
            overflow: hidden;
        }
        /* نمط الشطرنج للخلفية الشفافة */
        .transparent-pattern {
            background-image: linear-gradient(45deg, #e0e0e0 25%, transparent 25%), 
                              linear-gradient(-45deg, #e0e0e0 25%, transparent 25%), 
                              linear-gradient(45deg, transparent 75%, #e0e0e0 75%), 
                              linear-gradient(-45deg, transparent 75%, #e0e0e0 75%) !important;
            background-size: 20px 20px !important;
            background-position: 0 0, 0 10px, 10px -10px, -10px 0px !important;
            background-color: #ffffff !important;
        }
        #asset-header {
            background: #1e293b;
            color: white;
            padding: 10px;
            font-size: 12px;
            font-weight: bold;
            display: flex;
            justify-content: space-between;
            align-items: center;
            cursor: move;
        }
        #asset-content {
            padding: 10px;
            overflow-y: auto;
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 8px;
        }
        .asset-item {
            border: 1px solid #e2e8f0;
            border-radius: 6px;
            overflow: hidden;
            cursor: pointer;
            transition: all 0.2s;
            height: 80px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: #f8fafc;
        }
        .asset-item:hover {
            border-color: #6366f1;
            transform: scale(1.05);
        }
        .asset-item img {
            max-width: 100%;
            max-height: 100%;
            object-fit: contain;
        }
        .asset-item-text {
            font-size: 10px;
            color: #1e293b;
            text-align: center;
            padding: 4px;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        /* تحميل الخطوط المخفي */
        #hidden-font-loader { opacity: 0; pointer-events: none; position: absolute; top: -9999px; }

        /* Custom Scrollbar */
        ::-webkit-scrollbar { width: 6px; }
        ::-webkit-scrollbar-track { background: var(--paper-bg); }
        ::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }
        ::-webkit-scrollbar-thumb:hover { background: var(--primary-color); }
        
        /* تنسيق أدوات التحكم المرنة */
        .controls-row {
            width: 100%;
            margin-top: 0.5rem;
            padding-top: 0.5rem;
            border-top: 1px dashed var(--border-color);
            display: none; 
            flex-wrap: wrap;
            align-items: center;
            gap: 0.5rem;
            order: 99; 
        }
        .controls-row.active { display: flex; }
        
        /* تنسيق الحقول */
        input[type="number"], input[type="text"], textarea, select {
            transition: all 0.2s;
        }
        input[type="number"]:focus, input[type="text"]:focus, textarea:focus, select:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.1);
        }

        /* تنسيق شاشة تسجيل الدخول */
        #login-overlay {
            position: fixed;
            inset: 0;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            z-index: 99999;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .login-container {
            background: white;
            border-radius: 20px;
            padding: 40px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            max-width: 400px;
            width: 100%;
            text-align: center;
            animation: slideUp 0.5s ease-out;
        }

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

        .login-title {
            font-size: 28px;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 10px;
            font-family: 'Cairo', sans-serif;
        }

        .login-subtitle {
            color: #94a3b8;
            margin-bottom: 30px;
            font-family: 'Cairo', sans-serif;
            font-size: 14px;
        }

        .login-input {
            width: 100%;
            padding: 12px 15px;
            font-size: 16px;
            border: 2px solid #e2e8f0;
            border-radius: 10px;
            margin-bottom: 15px;
            transition: all 0.3s;
            font-family: 'Cairo', sans-serif;
            text-align: center;
            direction: ltr;
        }

        .login-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s;
            font-family: 'Cairo', sans-serif;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
        }

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

        .login-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .login-error {
            color: #dc2626;
            font-size: 14px;
            margin-top: 10px;
            font-family: 'Cairo', sans-serif;
            display: none;
        }

        .login-loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }

        .login-spinner {
            width: 30px;
            height: 30px;
            border: 3px solid #e2e8f0;
            border-top-color: #667eea;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }

        /* ==========================================
           نظام الـ Freemium - أنماط العناصر المقفلة
           ========================================== */
        
        .locked-item {
            position: relative;
            transition: all 0.3s ease;
            cursor: pointer !important;
            opacity: 0.7 !important;
        }

        .locked-item:hover {
            opacity: 0.9 !important;
            transform: scale(1.02);
        }

        .locked-item::after {
            content: '\f023';
            font-family: 'Font Awesome 6 Free';
            font-weight: 900;
            position: absolute;
            top: 6px;
            right: 6px;
            font-size: 14px;
            pointer-events: none;
            color: #6366f1;
            opacity: 0.7;
            text-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        }

        .locked-item:hover::after {
            opacity: 0.9;
            color: #4f46e5;
        }
            background: rgba(99, 102, 241, 0.15);
        }

        /* تنسيق الخيارات المعطلة في الـ Select */
        select option:disabled {
            color: #cbd5e1;
            background: #f1f5f9;
        }

        /* animation للبوب أب */
        @keyframes slideIn {
            from {
                transform: scale(0.9) translateY(-20px);
                opacity: 0;
            }
            to {
                transform: scale(1) translateY(0);
                opacity: 1;
            }
        }

        @keyframes fadeOut {
            to {
                opacity: 0;
                transform: scale(0.95);
            }
        }