/* ==========================================================================
   1. شريط التنقل العلوي للعبة (Game Navbar)
   ========================================================================== */
.game-navbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    background-color: var(--bg-card);
    border-bottom: 1px solid rgba(250, 250, 250, 0.05);
    flex-shrink: 0;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}

.nav-back-btn, .nav-action-btn {
    background: none;
    border: none;
    color: var(--color-text-main);
    font-size: 24px;
    cursor: pointer;
    padding: 4px 8px;
    transition: all 0.2s ease;
    border-radius: 12px;
}

.nav-back-btn:hover, .nav-action-btn:hover {
    background-color: rgba(222, 255, 154, 0.1);
    transform: scale(1.1);
}

.nav-back-btn:active, .nav-action-btn:active {
    color: var(--color-accent);
}

.nav-help-btn {
    background: none;
    border: none;
    color: var(--color-text-main);
    font-size: 20px;
    cursor: pointer;
    padding: 4px 8px;
    transition: all 0.2s ease;
    border-radius: 12px;
    margin-left: 8px;
}

.nav-help-btn:hover {
    background-color: rgba(222, 255, 154, 0.1);
    transform: scale(1.1);
}

.nav-help-btn:active {
    color: var(--color-accent);
}

.game-meta {
    display: flex;
    align-items: center;
    gap: 12px;
}

.badge {
    background-color: var(--bg-button);
    color: var(--color-accent);
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 700;
    border: 1px solid rgba(222, 255, 154, 0.2);
}

#kenken-score {
    font-size: 14px;
    font-weight: 700;
    color: var(--color-accent);
    text-shadow: 0 0 10px rgba(222, 255, 154, 0.3);
}

#kenken-timer {
    font-size: 16px;
    font-weight: 700;
    color: var(--color-text-main);
    font-family: var(--font-display);
    letter-spacing: 1px;
}

/* ==========================================================================
   2. ساحة اللعب وهندسة الشبكة (Game Grid Arena)
   ========================================================================== */
.game-play-area {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-grow: 1;
    padding: 20px;
}

/* التنسيق الافتراضي للشبكة الاعتماد على CSS Grid */
#kenken-grid {
    display: grid;
    width: 100%;
    max-width: min(90vw, 400px);
    aspect-ratio: 1 / 1; /* يضمن بقاء الشبكة مربعة تماماً */
    background-color: #000000;
    gap: 2px; /* الفراغ الأسود بين الخلايا */
    border: 3px solid #000000;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 50px rgba(0,0,0,0.6), 0 0 30px rgba(222, 255, 154, 0.1);
    animation: gridFadeIn 0.6s ease-out;
}

@keyframes gridFadeIn {
    0% {
        opacity: 0;
        transform: scale(0.8) translateY(20px);
    }
    100% {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

/* التحكم الديناميكي بالأعمدة حسب نوع اللغز */
.grid-3x3 { grid-template-columns: repeat(3, 1fr); }
.grid-4x4 { grid-template-columns: repeat(4, 1fr); }

/* الستايل الداخلي لكل خلية */
.kenken-cell {
    position: relative;
    background-color: var(--bg-card);
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: var(--font-display);
    font-size: 28px;
    font-weight: 700;
    color: var(--color-text-main);
    cursor: pointer;
    transition: all 0.2s ease;
    overflow: hidden;
}

/* إشارة العملية الحسابية في زاوية القفص (مثل 3+) */
.cell-target {
    position: absolute;
    top: 4px;
    right: 6px;
    font-size: 11px;
    font-weight: 700;
    color: rgba(250, 250, 250, 0.85);
    font-family: var(--font-body);
    pointer-events: none;
}

/* حالات الخلايا عند التفاعل والتحقق */
.kenken-cell.selected {
    background-color: rgba(222, 255, 154, 0.2) !important;
    outline: 2px solid var(--color-accent);
    outline-offset: -2px;
    z-index: 5;
    animation: cellPulse 1.5s ease-in-out infinite;
    box-shadow: 0 0 20px rgba(222, 255, 154, 0.4), inset 0 0 15px rgba(222, 255, 154, 0.1);
}

@keyframes cellPulse {
    0%, 100% {
        box-shadow: 0 0 20px rgba(222, 255, 154, 0.4), inset 0 0 15px rgba(222, 255, 154, 0.1);
    }
    50% {
        box-shadow: 0 0 30px rgba(222, 255, 154, 0.6), inset 0 0 20px rgba(222, 255, 154, 0.2);
    }
}

.kenken-cell.correct {
    color: var(--color-success);
    text-shadow: 0 0 15px rgba(46, 204, 113, 0.6);
    animation: cellCorrect 0.5s ease;
}

@keyframes cellCorrect {
    0% { transform: scale(1); }
    50% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

.kenken-cell.error {
    animation: cell-shake 0.4s ease;
    background-color: rgba(231, 76, 60, 0.2) !important;
    color: var(--color-error);
    box-shadow: 0 0 20px rgba(231, 76, 60, 0.4);
}

/* ==========================================================================
   3. لوحة إدخال الأرقام والتحكم (Input Controls)
   ========================================================================== */
.game-controls {
    padding: 20px 24px calc(20px + env(safe-area-inset-bottom)) 24px;
    background-color: var(--bg-card);
    border-top: 1px solid rgba(250, 250, 250, 0.05);
    flex-shrink: 0;
}

.number-pad {
    display: flex;
    justify-content: center;
    gap: 12px;
    margin-bottom: 12px;
}

.pad-btn {
    flex: 1;
    max-width: 70px;
    aspect-ratio: 1 / 1;
    background-color: var(--bg-button);
    border: 1px solid rgba(250, 250, 250, 0.05);
    border-radius: 16px;
    color: var(--color-text-main);
    font-family: var(--font-display);
    font-size: 22px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background 0.1s ease;
}

.pad-btn:active {
    background-color: var(--color-accent);
    color: var(--bg-primary);
    transform: scale(0.95);
    box-shadow: 0 0 20px rgba(222, 255, 154, 0.5);
}

.clear-btn {
    width: 100%;
    background-color: rgba(231, 76, 60, 0.1);
    color: var(--color-error);
    border: 1px solid rgba(231, 76, 60, 0.2);
    padding: 14px;
    border-radius: 14px;
    font-family: var(--font-body);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
}

/* أنيميشن الاهتزاز عند حدوث خطأ حسابي */
@keyframes cell-shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-5px); }
    20%, 40%, 60%, 80% { transform: translateX(5px); }
}

/* تأثير الاحتفال عند الفوز */
@keyframes confettiBurst {
    0% {
        transform: scale(0) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: scale(1.5) rotate(720deg);
        opacity: 0;
    }
}

.confetti-piece {
    position: absolute;
    width: 10px;
    height: 10px;
    border-radius: 2px;
    animation: confettiBurst 1s ease-out forwards;
    pointer-events: none;
    z-index: 1000;
}

/* ==========================================================================
   4. شاشة اختيار المستويات (Level Selection Screen)
   ========================================================================== */
.level-selection-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 24px;
    flex-grow: 1;
    overflow-y: auto;
}

.grid-size-selector {
    display: flex;
    gap: 16px;
    margin-bottom: 32px;
    width: 100%;
    max-width: 400px;
}

.size-btn {
    flex: 1;
    background-color: var(--bg-button);
    border: 2px solid rgba(250, 250, 250, 0.1);
    border-radius: 16px;
    padding: 20px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.size-btn.active {
    border-color: var(--color-accent);
    background-color: rgba(222, 255, 154, 0.1);
    box-shadow: 0 0 20px rgba(222, 255, 154, 0.3);
}

.size-btn:hover:not(.active) {
    border-color: rgba(222, 255, 154, 0.3);
    transform: translateY(-2px);
}

.size-icon {
    font-family: var(--font-display);
    font-size: 28px;
    font-weight: 700;
    color: var(--color-accent);
}

.size-label {
    font-size: 14px;
    color: var(--color-text-muted);
}

.levels-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
    gap: 16px;
    width: 100%;
    max-width: 400px;
}

.level-card {
    background-color: var(--bg-card);
    border: 2px solid rgba(250, 250, 250, 0.1);
    border-radius: 16px;
    padding: 24px 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    position: relative;
}

.level-card:hover:not(.locked) {
    border-color: var(--color-accent);
    transform: translateY(-4px);
    box-shadow: 0 10px 30px rgba(222, 255, 154, 0.2);
}

.level-card.locked {
    opacity: 0.5;
    cursor: not-allowed;
}

.level-card.completed {
    border-color: var(--color-success);
}

.level-number {
    font-family: var(--font-display);
    font-size: 32px;
    font-weight: 700;
    color: var(--color-text-main);
}

.level-card.locked .level-number {
    color: var(--color-text-muted);
}

.level-card.completed .level-number {
    color: var(--color-success);
}

.level-difficulty {
    font-size: 12px;
    color: var(--color-text-muted);
}

.level-lock-icon {
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 16px;
}

.level-check-icon {
    position: absolute;
    top: 8px;
    right: 8px;
    font-size: 16px;
    color: var(--color-success);
}

/* ==========================================================================
   5. البطاقات المنبثقة (Modals)
   ========================================================================== */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

.modal-overlay.active {
    opacity: 1;
    pointer-events: auto;
}

.modal-overlay.hidden {
    display: none;
}

/* بطاقة الفوز */
.success-card {
    background-color: var(--bg-card);
    border: 2px solid var(--color-accent);
    border-radius: 24px;
    padding: 32px;
    width: 90%;
    max-width: 400px;
    text-align: center;
    transform: scale(0.8);
    transition: transform 0.3s ease;
    box-shadow: 0 20px 60px rgba(222, 255, 154, 0.3);
}

.modal-overlay.active .success-card {
    transform: scale(1);
}

.success-icon {
    font-size: 64px;
    margin-bottom: 16px;
    animation: bounce 0.6s ease;
}

@keyframes bounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.2); }
}

.success-title {
    font-family: var(--font-display);
    font-size: 28px;
    font-weight: 700;
    color: var(--color-accent);
    margin-bottom: 8px;
}

.success-message {
    font-size: 16px;
    color: var(--color-text-muted);
    margin-bottom: 24px;
}

.success-stats {
    display: flex;
    justify-content: center;
    gap: 32px;
    margin-bottom: 32px;
}

.stat-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.stat-label {
    font-size: 12px;
    color: var(--color-text-muted);
}

.stat-value {
    font-family: var(--font-display);
    font-size: 24px;
    font-weight: 700;
    color: var(--color-text-main);
}

.success-actions {
    display: flex;
    gap: 12px;
}

.success-btn {
    flex: 1;
    padding: 14px 24px;
    border-radius: 12px;
    font-family: var(--font-body);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s ease;
}

.success-btn.primary {
    background-color: var(--color-accent);
    color: var(--bg-primary);
    border: none;
}

.success-btn.primary:hover {
    transform: scale(1.05);
    box-shadow: 0 0 20px rgba(222, 255, 154, 0.5);
}

.success-btn.secondary {
    background-color: var(--bg-button);
    color: var(--color-text-main);
    border: 1px solid rgba(250, 250, 250, 0.1);
}

.success-btn.secondary:hover {
    border-color: var(--color-accent);
}

/* بطاقة الشرح */
.tutorial-card {
    background-color: var(--bg-card);
    border: 1px solid rgba(250, 250, 250, 0.1);
    border-radius: 20px;
    padding: 24px;
    width: 90%;
    max-width: 400px;
    transform: scale(0.9);
    transition: transform 0.3s ease;
}

.modal-overlay.active .tutorial-card {
    transform: scale(1);
}

.tutorial-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.tutorial-header h2 {
    font-family: var(--font-display);
    font-size: 20px;
    font-weight: 700;
    color: var(--color-accent);
}

.tutorial-close {
    background: none;
    border: none;
    color: var(--color-text-muted);
    font-size: 24px;
    cursor: pointer;
    padding: 4px;
    transition: color 0.2s ease;
}

.tutorial-close:hover {
    color: var(--color-accent);
}

.tutorial-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.tutorial-step {
    display: flex;
    gap: 12px;
    align-items: flex-start;
}

.step-number {
    background-color: var(--color-accent);
    color: var(--bg-primary);
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: var(--font-display);
    font-size: 14px;
    font-weight: 700;
    flex-shrink: 0;
}

.tutorial-step p {
    font-size: 14px;
    color: var(--color-text-muted);
    line-height: 1.6;
}

.tutorial-example {
    background-color: rgba(222, 255, 154, 0.1);
    border: 1px solid rgba(222, 255, 154, 0.2);
    border-radius: 12px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.example-label {
    font-size: 12px;
    color: var(--color-accent);
    font-weight: 700;
}

.example-text {
    font-size: 14px;
    color: var(--color-text-main);
}