/**
 * Eduplayify Game Mode: Wheel of Questions & Fortune (عجلة الأسئلة وعجلة الحظ)
 * تنسيقات راقية، خفيفة، متجاوبة لجميع الشاشات، ومتوافقة مع جميع المتصفحات
 */

/* الحاوية الخارجية لمسرح العجلة */
/* ملاحظة: كل سطر px قبل clamp()/min() هو fallback مقصود للمتصفحات القديمة
   التي تتجاهل الدوال الحديثة وتأخذ القيمة الثابتة */
.q-wheel-stage,
.wheel-mode-wrap {
    position: relative;
    width: 300px;
    height: 300px;
    width: min(390px, 84vw, 62vh);
    height: min(390px, 84vw, 62vh);
    margin: 8px auto 14px;
    margin: clamp(6px, 1.5vh, 16px) auto clamp(12px, 2vh, 22px);
    aspect-ratio: 1 / 1;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-user-select: none;
}

/* هالة ضوئية خلف العجلة وحلقة دوارة أنيقة */
.q-wheel-stage::before {
    content: '';
    pointer-events: none;
    position: absolute;
    border-radius: 50%;
    top: -20px;
    right: -20px;
    bottom: -20px;
    left: -20px;
    inset: clamp(-20px, -4vw, -28px);
    background: radial-gradient(circle, rgba(255, 197, 66, 0.28) 0%, rgba(255, 197, 66, 0.08) 48%, transparent 70%);
    background-color: transparent;
    z-index: 1;
}

.q-wheel-stage::after {
    content: '';
    pointer-events: none;
    position: absolute;
    border-radius: 50%;
    top: -10px;
    right: -10px;
    bottom: -10px;
    left: -10px;
    inset: clamp(-10px, -2vw, -15px);
    border: 2px dashed rgba(245, 180, 41, 0.45);
    animation: qWheelRingSpin 45s linear infinite;
    z-index: 1;
}

@keyframes qWheelRingSpin {
    from {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg);
    }
    to {
        -webkit-transform: rotate(360deg);
        transform: rotate(360deg);
    }
}

/* الحاوية الدوارة للعجلة والكانفاس */
.q-wheel-wrap {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    inset: 0;
    width: 100%;
    height: 100%;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0 16px 36px -10px rgba(15, 23, 42, 0.35), 0 0 0 5px #ffffff;
    cursor: pointer;
    z-index: 2;
    transition: transform 3s cubic-bezier(.15, .85, .15, 1);
    -webkit-transition: -webkit-transform 3s cubic-bezier(.15, .85, .15, 1);
    outline: none;
}

.q-wheel-canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
    border-radius: 50%;
}

/* مؤشر السهم العلوي الذهبي الفاخر */
.q-wheel-pointer,
.wheel-pointer {
    position: absolute;
    left: 50%;
    top: -18px;
    top: clamp(-22px, -3.5vw, -16px);
    width: 36px;
    height: 46px;
    width: clamp(32px, 5vw, 42px);
    height: clamp(42px, 6.5vw, 54px);
    transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    background: #f5b429;
    background: linear-gradient(180deg, #ffe9a8 0%, #ffc542 40%, #e89600 100%);
    -webkit-clip-path: polygon(50% 100%, 0 25%, 12% 0, 88% 0, 100% 25%);
    clip-path: polygon(50% 100%, 0 25%, 12% 0, 88% 0, 100% 25%);
    filter: drop-shadow(0 4px 8px rgba(15, 23, 42, 0.45));
    -webkit-filter: drop-shadow(0 4px 8px rgba(15, 23, 42, 0.45));
    z-index: 10;
    pointer-events: none;
    border-radius: 6px;
}

/* بديل المتصفحات التي لا تدعم clip-path: شارة دائرية بدل السهم */
@supports not ((clip-path: polygon(0 0)) or (-webkit-clip-path: polygon(0 0))) {
    .q-wheel-pointer,
    .wheel-pointer {
        width: 22px;
        height: 22px;
        top: -11px;
        border-radius: 50%;
        border: 3px solid #ffffff;
    }
    .q-wheel-pointer::after,
    .wheel-pointer::after {
        display: none;
    }
}

.q-wheel-pointer::after,
.wheel-pointer::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 7px;
    width: 10px;
    height: 10px;
    border-radius: 50%;
    transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    background: radial-gradient(circle at 35% 30%, #ffffff, #d97706);
    box-shadow: 0 0 0 2px rgba(122, 86, 10, 0.6);
}

/* المحور الدائري المركزي */
.q-wheel-hub {
    position: absolute;
    left: 50%;
    top: 50%;
    z-index: 6;
    border-radius: 50%;
    width: 52px;
    height: 52px;
    width: clamp(46px, 9vw, 62px);
    height: clamp(46px, 9vw, 62px);
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    background: #ffe9b8;
    background: radial-gradient(circle at 32% 28%, #ffffff 0%, #ffe9b8 48%, #e8a80c 100%);
    border: 3px solid rgba(122, 86, 10, 0.65);
    box-shadow: 0 0 0 3px #ffffff, 0 8px 18px rgba(15, 23, 42, 0.35);
    pointer-events: none;
}

.q-wheel-hub::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    border-radius: 50%;
    width: 12px;
    height: 12px;
    width: clamp(10px, 2vw, 15px);
    height: clamp(10px, 2vw, 15px);
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    background: #d19a17;
    background: radial-gradient(circle at 35% 30%, #fffdf2, #d19a17);
}

/* ========================================================
   عناصر ومحتويات أقسام العجلة (Thumbnails & Text)
   ======================================================== */

.q-wheel-thumb {
    pointer-events: none;
    position: absolute;
    z-index: 5;
    transform-origin: center center;
    -webkit-transform-origin: center center;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

/* صورة السؤال داخل قسم العجلة */
.q-wheel-thumb-img-wrap {
    width: 40px;
    height: 40px;
    width: clamp(34px, 7.5vw, 48px);
    height: clamp(34px, 7.5vw, 48px);
    border-radius: 50%;
    border: 2.5px solid #ffffff;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.35);
    background: #ffffff;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.q-wheel-thumb-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    border-radius: 50%;
    image-rendering: -webkit-optimize-contrast;
}

/* نص السؤال عند عدم وجود صورة (خلفية شفافة تماماً مع ظل نصي واضح) */
.q-wheel-thumb-text {
    max-width: 100px;
    max-width: clamp(75px, 17vw, 120px);
    background: transparent !important;
    color: #ffffff;
    font-weight: 800;
    font-size: 0.85rem;
    font-size: clamp(0.74rem, 1.65vw, 0.96rem);
    line-height: 1.25;
    padding: 2px 4px;
    border: none !important;
    border-radius: 0;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    text-shadow: 0 1px 3px rgba(15, 23, 42, 0.95), 0 0 8px rgba(15, 23, 42, 0.85), 0 0 14px rgba(15, 23, 42, 0.65);
    box-shadow: none !important;
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
}

/* زر تدوير العجلة */
.q-wheel-spin-wrap {
    text-align: center;
    margin-top: 12px;
    margin-top: clamp(8px, 1.8vh, 18px);
    position: relative;
    z-index: 10;
}

.wheel-spin-btn {
    min-width: 170px;
    padding: 12px 28px;
    padding: clamp(10px, 1.5vh, 14px) clamp(24px, 3vw, 36px);
    font-size: 1.1rem;
    font-size: clamp(1.05rem, 1.8vw, 1.25rem);
    font-weight: 800;
    border-radius: 9999px;
    background: linear-gradient(135deg, #4f46e5 0%, #3730a3 100%);
    color: #ffffff;
    border: none;
    box-shadow: 0 4px 16px rgba(79, 70, 229, 0.38);
    cursor: pointer;
    transition: all 0.22s cubic-bezier(0.4, 0, 0.2, 1);
    outline: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.wheel-spin-btn:hover:not(:disabled) {
    transform: translateY(-2px);
    -webkit-transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(79, 70, 229, 0.48);
    background: linear-gradient(135deg, #4338ca 0%, #312e81 100%);
}

.wheel-spin-btn:active:not(:disabled) {
    transform: scale(0.98);
    -webkit-transform: scale(0.98);
}

.wheel-spin-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* كانفاس قصاصات الاحتفال بالفائز */
.wheel-confetti {
    position: absolute;
    top: 0;
    left: 0;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 20;
}

/* نبضة فوز احتفالية */
.wheel-winner-pop {
    animation: winnerPopAnim 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both;
    -webkit-animation: winnerPopAnim 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes winnerPopAnim {
    0% {
        transform: scale(0.4);
        opacity: 0;
    }
    70% {
        transform: scale(1.12);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ========================================================
   وضع ملء الشاشة والشاشات الكبيرة (Fullscreen Mode)
   ======================================================== */
.gpa-fullscreen .q-wheel-stage,
.gpa-fullscreen .wheel-mode-wrap {
    width: 420px;
    height: 420px;
    width: min(520px, 86vw, 72vh);
    height: min(520px, 86vw, 72vh);
    margin-top: 14px;
    margin-bottom: 20px;
}

.gpa-fullscreen .q-wheel-thumb-img-wrap {
    width: 56px;
    height: 56px;
}

.gpa-fullscreen .q-wheel-thumb-text {
    max-width: 140px;
    font-size: 1.05rem;
    padding: 2px 6px;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
}

.gpa-fullscreen .wheel-spin-btn {
    padding: 14px 44px;
    font-size: 1.35rem;
}

/* زر اختيار طالب داخل شريط الوقت يظهر فقط عند ملء الشاشة لمنع التكرار */
.sw-fs-btn {
    display: none !important;
}

.gpa-fullscreen .sw-fs-btn {
    display: inline-flex !important;
}

/* ========================================================
   نافذة عجلة اختيار الطلاب للمعلم (Student Picker Modal)
   ======================================================== */
#studentWheelModal {
    display: none;
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    inset: 0;
    z-index: 100000;
    background: rgba(15, 23, 42, 0.78);
    background-color: #1e293b;
    background-color: rgba(15, 23, 42, 0.78);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    align-items: center;
    justify-content: center;
    padding: 12px;
    padding: clamp(8px, 2vw, 20px);
    user-select: none;
    -webkit-user-select: none;
    opacity: 0;
    transition: opacity 0.25s ease-out;
    -webkit-transition: opacity 0.25s ease-out;
}

#studentWheelModal.sw-show {
    opacity: 1;
}

.sw-dialog {
    background: #ffffff;
    width: 100%;
    max-width: 960px;
    max-height: 94vh;
    border-radius: 20px;
    border: 1px solid rgba(255, 255, 255, 0.6);
    box-shadow: 0 25px 50px -12px rgba(15, 23, 42, 0.45);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.sw-header {
    background: linear-gradient(135deg, #1e1b4b 0%, #312e81 100%);
    color: #ffffff;
    padding: 14px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 2px solid #f59e0b;
}

.sw-title {
    font-size: clamp(1.1rem, 2vw, 1.35rem);
    font-weight: 800;
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 0;
    color: #ffffff;
}

.sw-close-btn {
    background: rgba(255, 255, 255, 0.12);
    border: none;
    color: #ffffff;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.sw-close-btn:hover {
    background: rgba(239, 68, 68, 0.85);
    transform: scale(1.06);
}

.sw-toolbar {
    background: #f8fafc;
    border-bottom: 1px solid #e2e8f0;
    padding: 10px 18px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 10px;
}

.sw-class-group {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sw-class-label {
    font-weight: 700;
    font-size: 0.9rem;
    color: #334155;
    margin: 0;
}

.sw-class-select {
    padding: 6px 14px;
    border-radius: 9999px;
    border: 1.5px solid #cbd5e1;
    background: #ffffff;
    font-weight: 700;
    font-size: 0.92rem;
    color: #0f172a;
    outline: none;
    cursor: pointer;
    transition: border-color 0.2s;
}

.sw-class-select:focus {
    border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.sw-stats-group {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sw-stat-pill {
    font-size: 0.82rem;
    font-weight: 700;
    padding: 4px 10px;
    border-radius: 9999px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.sw-stat-remaining {
    background: #ecfdf5;
    color: #059669;
    border: 1px solid #a7f3d0;
}

.sw-stat-picked {
    background: #f1f5f9;
    color: #64748b;
    border: 1px solid #e2e8f0;
}

.sw-reset-btn {
    background: #ffffff;
    border: 1.5px solid #cbd5e1;
    color: #475569;
    padding: 4px 12px;
    border-radius: 9999px;
    font-size: 0.82rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
}

.sw-reset-btn:hover {
    background: #fee2e2;
    border-color: #fca5a5;
    color: #dc2626;
}

.sw-body {
    display: flex;
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    gap: 1.5rem;
    align-items: center;
    justify-content: center;
    position: relative;
    min-height: 400px;
}

@media (max-width: 768px) {
    .sw-body {
        flex-direction: column;
        gap: 1rem;
    }
}

.sw-wheel-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 0;
}

.sw-list-col {
    width: 280px;
    max-width: 100%;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 14px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    max-height: 420px;
}

.sw-list-header {
    font-size: 0.92rem;
    font-weight: 800;
    color: #1e293b;
    margin-bottom: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sw-students-scroll {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-right: 4px;
}

.sw-student-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 10px;
    background: #ffffff;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    transition: all 0.2s;
    font-size: 0.88rem;
}

.sw-student-info {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.sw-student-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    background: #e2e8f0;
    border: 1.5px solid #cbd5e1;
    flex-shrink: 0;
}

.sw-student-avatar-placeholder {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: #e0e7ff;
    color: #4338ca;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 800;
    flex-shrink: 0;
}

.sw-student-name {
    font-weight: 700;
    color: #1e293b;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sw-student-badge {
    font-size: 0.72rem;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 9999px;
    flex-shrink: 0;
}

.sw-student-item.is-picked {
    opacity: 0.6;
    background: #f1f5f9;
    border-color: #cbd5e1;
}

.sw-student-item.is-picked .sw-student-name {
    text-decoration: line-through;
    color: #64748b;
}

.sw-student-item.is-picked .sw-student-badge {
    background: #e2e8f0;
    color: #64748b;
}

.sw-student-item.is-eligible .sw-student-badge {
    background: #dcfce7;
    color: #15803d;
}

/* بطاقة الفائز المنبثقة فوق العجلة */
.sw-winner-overlay {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    inset: 0;
    z-index: 50;
    background: rgba(15, 23, 42, 0.7);
    background-color: #1e293b;
    background-color: rgba(15, 23, 42, 0.7);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
    padding: 1rem;
    animation: swFadeIn 0.3s ease-out;
}

.sw-winner-card {
    background: #ffffff;
    border-radius: 20px;
    padding: 1.75rem 2rem;
    text-align: center;
    max-width: 380px;
    width: 100%;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.35);
    border: 3px solid #f59e0b;
    animation: winnerPopAnim 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.sw-winner-avatar {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    margin: 0 auto 12px;
    border: 4px solid #f59e0b;
    box-shadow: 0 4px 16px rgba(245, 158, 11, 0.4);
    object-fit: cover;
    display: block;
}

.sw-winner-avatar-placeholder {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    margin: 0 auto 12px;
    background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
    color: #b45309;
    border: 4px solid #f59e0b;
    box-shadow: 0 4px 16px rgba(245, 158, 11, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.2rem;
    font-weight: 800;
}

.sw-winner-tag {
    font-size: 0.92rem;
    font-weight: 800;
    color: #d97706;
    margin-bottom: 4px;
}

.sw-winner-name {
    font-size: 1.6rem;
    font-weight: 900;
    color: #0f172a;
    margin: 0 0 16px;
    line-height: 1.25;
}

.sw-winner-continue-btn {
    background: linear-gradient(135deg, #4f46e5 0%, #3730a3 100%);
    color: #ffffff;
    border: none;
    padding: 10px 24px;
    border-radius: 9999px;
    font-size: 1rem;
    font-weight: 800;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(79, 70, 229, 0.35);
    transition: all 0.2s;
}

.sw-winner-continue-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 18px rgba(79, 70, 229, 0.45);
}

@keyframes swFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* الأجهزة الضعيفة / تقليل الحركة: إيقاف الحلقة الدوارة والنبضات لتوفير البطارية والمعالج */
@media (prefers-reduced-motion: reduce) {
    .q-wheel-stage::after {
        animation: none;
        -webkit-animation: none;
    }
    .wheel-winner-pop,
    .sw-winner-overlay,
    .sw-winner-card {
        animation: none;
        -webkit-animation: none;
    }
    .q-wheel-wrap {
        transition-duration: 0.01s;
        -webkit-transition-duration: 0.01s;
    }
}