/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var CelebrationStar = Container.expand(function () {
	var self = Container.call(this);
	var starGraphic = self.attachAsset('star', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.startAnimation = function () {
		self.alpha = 1;
		self.scaleX = 0.1;
		self.scaleY = 0.1;
		tween(self, {
			scaleX: 1.5,
			scaleY: 1.5
		}, {
			duration: 500,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				tween(self, {
					alpha: 0,
					scaleX: 0.5,
					scaleY: 0.5
				}, {
					duration: 500,
					easing: tween.easeIn,
					onFinish: function onFinish() {
						self.destroy();
					}
				});
			}
		});
	};
	return self;
});
var MathProblem = Container.expand(function () {
	var self = Container.call(this);
	self.num1 = 0;
	self.num2 = 0;
	self.operation = '+';
	self.answer = 0;
	self.difficulty = 1;
	self.generateProblem = function () {
		var maxNum = Math.min(10, 5 + self.difficulty * 2);
		self.num1 = Math.floor(Math.random() * maxNum) + 1;
		self.num2 = Math.floor(Math.random() * maxNum) + 1;
		var operations = ['+', '-'];
		if (self.difficulty > 3) {
			operations.push('×');
			operations.push('×'); // Add multiplication twice to make it more likely
		}
		self.operation = operations[Math.floor(Math.random() * operations.length)];
		if (self.operation === '-' && self.num1 < self.num2) {
			var temp = self.num1;
			self.num1 = self.num2;
			self.num2 = temp;
		}
		switch (self.operation) {
			case '+':
				self.answer = self.num1 + self.num2;
				break;
			case '-':
				self.answer = self.num1 - self.num2;
				break;
			case '×':
				self.answer = self.num1 * self.num2;
				break;
		}
	};
	self.getProblemText = function () {
		return self.num1 + ' ' + self.operation + ' ' + self.num2 + ' = ?';
	};
	return self;
});
var WritingArea = Container.expand(function () {
	var self = Container.call(this);
	var areaBackground = self.attachAsset('writingArea', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.drawingPath = [];
	self.isDrawing = false;
	self.writtenAnswer = '';
	self.startDrawing = function (x, y) {
		self.isDrawing = true;
		self.drawingPath = [];
		var localPos = self.toLocal({
			x: x,
			y: y
		});
		self.drawingPath.push({
			x: localPos.x,
			y: localPos.y
		});
		self.addDrawingDot(localPos.x, localPos.y);
		// Show text bar when writing starts
		textBar.alpha = 1;
	};
	self.continueDrawing = function (x, y) {
		if (self.isDrawing) {
			var localPos = self.toLocal({
				x: x,
				y: y
			});
			self.drawingPath.push({
				x: localPos.x,
				y: localPos.y
			});
			self.addDrawingDot(localPos.x, localPos.y);
		}
	};
	self.stopDrawing = function () {
		self.isDrawing = false;
		self.recognizeNumber();
	};
	self.addDrawingDot = function (x, y) {
		var dot = self.addChild(LK.getAsset('drawingDot', {
			anchorX: 0.5,
			anchorY: 0.5
		}));
		dot.x = x;
		dot.y = y;
	};
	self.recognizeNumber = function () {
		if (self.drawingPath.length > 3) {
			var bounds = self.getDrawingBounds();
			var recognizedChar = self.simpleNumberRecognition(bounds);
			if (recognizedChar !== null) {
				self.writtenAnswer += recognizedChar; // Append characters to build text
				answerText.setText(self.writtenAnswer);
				// Show text bar when text is recognized
				textBar.alpha = 1;
				// Clear the drawing path after recognition to prepare for next character
				self.clearCurrentDrawing();
			}
		}
	};
	self.getDrawingBounds = function () {
		if (self.drawingPath.length === 0) return null;
		var minX = self.drawingPath[0].x;
		var maxX = self.drawingPath[0].x;
		var minY = self.drawingPath[0].y;
		var maxY = self.drawingPath[0].y;
		for (var i = 1; i < self.drawingPath.length; i++) {
			var point = self.drawingPath[i];
			minX = Math.min(minX, point.x);
			maxX = Math.max(maxX, point.x);
			minY = Math.min(minY, point.y);
			maxY = Math.max(maxY, point.y);
		}
		return {
			minX: minX,
			maxX: maxX,
			minY: minY,
			maxY: maxY,
			width: maxX - minX,
			height: maxY - minY
		};
	};
	self.simpleNumberRecognition = function (bounds) {
		if (!bounds || bounds.width < 10 || bounds.height < 10) {
			feedbackText.setText('Draw bigger');
			feedbackText.tint = 0xFFA500;
			LK.setTimeout(function () {
				feedbackText.setText('');
			}, 1000);
			return null;
		}
		var aspectRatio = bounds.width / bounds.height;
		var pathLength = self.drawingPath.length;
		var recognizedChar = null;
		// Numbers - more lenient recognition
		if (aspectRatio > 2.0 || aspectRatio < 0.8 && pathLength > 20) recognizedChar = '1';else if (aspectRatio > 0.7 && aspectRatio < 1.3 && pathLength > 25) recognizedChar = '0';else if (pathLength > 20 && pathLength < 55 && aspectRatio > 0.5) recognizedChar = '2';else if (pathLength > 15 && pathLength < 45 && aspectRatio > 0.6) recognizedChar = '3';else if (pathLength > 10 && pathLength < 40) recognizedChar = '4';else if (pathLength > 20 && pathLength < 60 && aspectRatio > 0.5) recognizedChar = '5';else if (pathLength > 25 && pathLength < 65 && aspectRatio > 0.6) recognizedChar = '6';else if (pathLength > 10 && pathLength < 35 && aspectRatio > 0.4) recognizedChar = '7';else if (pathLength > 30 && pathLength < 75) recognizedChar = '8';else if (pathLength > 25 && pathLength < 70 && aspectRatio > 0.5) recognizedChar = '9';
		if (recognizedChar) {
			feedbackText.setText('Recognized: ' + recognizedChar);
			feedbackText.tint = 0x32CD32;
			LK.setTimeout(function () {
				feedbackText.setText('');
			}, 500);
			return recognizedChar;
		}
		// Fallback - show a hint about drawing more clearly
		feedbackText.setText('Draw more clearly');
		feedbackText.tint = 0xFFA500;
		LK.setTimeout(function () {
			feedbackText.setText('');
		}, 1000);
		return null;
	};
	self.clearCurrentDrawing = function () {
		for (var i = self.children.length - 1; i >= 0; i--) {
			var child = self.children[i];
			if (child !== areaBackground) {
				child.destroy();
			}
		}
		self.drawingPath = [];
	};
	self.clearDrawing = function () {
		for (var i = self.children.length - 1; i >= 0; i--) {
			var child = self.children[i];
			if (child !== areaBackground) {
				child.destroy();
			}
		}
		self.drawingPath = [];
		self.writtenAnswer = '';
		answerText.setText('');
		// Hide text bar when clearing
		textBar.alpha = 0;
	};
	self.backspace = function () {
		if (self.writtenAnswer.length > 0) {
			self.writtenAnswer = self.writtenAnswer.slice(0, -1);
			answerText.setText(self.writtenAnswer);
			// Hide text bar if no text remains
			if (self.writtenAnswer.length === 0) {
				textBar.alpha = 0;
			}
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var currentProblem = new MathProblem();
var writingArea = new WritingArea();
var gameState = 'playing';
var screenState = 'home'; // Track current screen: 'home', 'game', 'lesson_menu', 'lesson_content'
var correctAnswers = 0;
var level = storage.level || 1;
var celebrationStars = [];
currentProblem.difficulty = level;
currentProblem.generateProblem();
var background = game.addChild(LK.getAsset('background', {
	anchorX: 0,
	anchorY: 0
}));
var problemBox = game.addChild(LK.getAsset('problemBox', {
	anchorX: 0.5,
	anchorY: 0.5
}));
problemBox.x = 1024;
problemBox.y = 600;
var problemText = new Text2(currentProblem.getProblemText(), {
	size: 120,
	fill: 0x333333
});
problemText.anchor.set(0.5, 0.5);
problemText.x = 1024;
problemText.y = 600;
game.addChild(problemText);
writingArea.x = 1024;
writingArea.y = 1200;
game.addChild(writingArea);
var instructionText = new Text2('Write your answer below:', {
	size: 80,
	fill: 0x444444
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 950;
game.addChild(instructionText);
var textBar = game.addChild(LK.getAsset('writingArea', {
	anchorX: 0.5,
	anchorY: 0.5
}));
textBar.x = 1024;
textBar.y = 1450;
textBar.alpha = 0;
textBar.tint = 0xE6F3FF;
var answerText = new Text2('', {
	size: 150,
	fill: 0x000000
});
answerText.anchor.set(0.5, 0.5);
answerText.x = 1024;
answerText.y = 1450;
game.addChild(answerText);
var submitButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
submitButton.x = 800;
submitButton.y = 1600;
var submitButtonText = new Text2('Check', {
	size: 60,
	fill: 0xFFFFFF
});
submitButtonText.anchor.set(0.5, 0.5);
submitButtonText.x = 800;
submitButtonText.y = 1600;
game.addChild(submitButtonText);
var clearButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
clearButton.x = 1150;
clearButton.y = 1600;
clearButton.tint = 0xFF6B6B;
var clearButtonText = new Text2('Clear', {
	size: 60,
	fill: 0xFFFFFF
});
clearButtonText.anchor.set(0.5, 0.5);
clearButtonText.x = 1150;
clearButtonText.y = 1600;
game.addChild(clearButtonText);
var nextButton = game.addChild(LK.getAsset('nextButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
nextButton.x = 1024;
nextButton.y = 1900;
nextButton.alpha = 0;
var nextButtonText = new Text2('Next Problem', {
	size: 60,
	fill: 0xFFFFFF
});
nextButtonText.anchor.set(0.5, 0.5);
nextButtonText.x = 1024;
nextButtonText.y = 1900;
nextButtonText.alpha = 0;
game.addChild(nextButtonText);
var feedbackText = new Text2('', {
	size: 80,
	fill: 0x32CD32
});
feedbackText.anchor.set(0.5, 0.5);
feedbackText.x = 1024;
feedbackText.y = 1750;
game.addChild(feedbackText);
var scoreText = new Text2('Score: 0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var levelText = new Text2('Level: ' + level, {
	size: 60,
	fill: 0xFFFFFF
});
levelText.anchor.set(1, 0);
LK.gui.topRight.addChild(levelText);
levelText.x = -120;
var howToReadButton = game.addChild(LK.getAsset('howToReadButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
howToReadButton.x = 1750;
howToReadButton.y = 1600;
var howToReadButtonText = new Text2('Right', {
	size: 50,
	fill: 0xFFFFFF
});
howToReadButtonText.anchor.set(0.5, 0.5);
howToReadButtonText.x = 1750;
howToReadButtonText.y = 1600;
game.addChild(howToReadButtonText);
var mathHomeButton = game.addChild(LK.getAsset('homeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
mathHomeButton.x = 1750;
mathHomeButton.y = 1700;
mathHomeButton.alpha = 0;
var mathHomeButtonText = new Text2('Home', {
	size: 40,
	fill: 0xFFFFFF
});
mathHomeButtonText.anchor.set(0.5, 0.5);
mathHomeButtonText.x = 1750;
mathHomeButtonText.y = 1700;
mathHomeButtonText.alpha = 0;
game.addChild(mathHomeButtonText);
var instructionPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
instructionPanel.x = 1024;
instructionPanel.y = 1366;
instructionPanel.alpha = 0;
var instructionTitle = new Text2('How to Read Math Problems', {
	size: 100,
	fill: 0x333333
});
instructionTitle.anchor.set(0.5, 0.5);
instructionTitle.x = 1024;
instructionTitle.y = 900;
instructionTitle.alpha = 0;
game.addChild(instructionTitle);
var instructionContent = new Text2('• Look at the math problem carefully\n• Read each number and symbol from left to right\n• + means add the numbers together\n• - means subtract the second from the first\n• × means multiply the numbers\n• = means "equals" - find what number comes after\n• Practice reading math problems out loud', {
	size: 70,
	fill: 0x444444
});
instructionContent.anchor.set(0.5, 0.5);
instructionContent.x = 1024;
instructionContent.y = 1300;
instructionContent.alpha = 0;
game.addChild(instructionContent);
var closeButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
closeButton.x = 1024;
closeButton.y = 1700;
closeButton.alpha = 0;
var closeButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
closeButtonText.anchor.set(0.5, 0.5);
closeButtonText.x = 1024;
closeButtonText.y = 1700;
closeButtonText.alpha = 0;
game.addChild(closeButtonText);
var showingInstructions = false;
var showingLessonMenu = false;
var currentLessonType = '';
var showingWritingLesson = false;
var writingPrompt = '';
var writingAnswer = '';
function checkAnswer() {
	var userAnswer = writingArea.writtenAnswer.trim();
	var correctAnswer = currentProblem.answer.toString();
	if (userAnswer === correctAnswer) {
		feedbackText.setText('Correct! Great job!');
		feedbackText.tint = 0x32CD32;
		LK.getSound('correct').play();
		correctAnswers++;
		LK.setScore(LK.getScore() + 10 * level);
		scoreText.setText('Score: ' + LK.getScore());
		createCelebrationStars();
		nextButton.alpha = 1;
		nextButtonText.alpha = 1;
		gameState = 'correct';
		if (correctAnswers >= 5) {
			level++;
			storage.level = level;
			levelText.setText('Level: ' + level);
			currentProblem.difficulty = level;
			correctAnswers = 0;
			LK.setTimeout(function () {
				feedbackText.setText('Level Up! Well done!');
				LK.getSound('celebration').play();
			}, 1000);
		}
	} else {
		feedbackText.setText('Try again! The answer is ' + correctAnswer);
		feedbackText.tint = 0xFF6B6B;
		LK.getSound('incorrect').play();
		LK.setTimeout(function () {
			feedbackText.setText('');
			writingArea.clearDrawing();
		}, 2000);
	}
}
function createCelebrationStars() {
	for (var i = 0; i < 5; i++) {
		var star = new CelebrationStar();
		star.x = 1024 + (Math.random() - 0.5) * 600;
		star.y = 1200 + (Math.random() - 0.5) * 400;
		celebrationStars.push(star);
		game.addChild(star);
		LK.setTimeout(function (starToAnimate) {
			return function () {
				starToAnimate.startAnimation();
			};
		}(star), i * 100);
	}
}
function nextProblem() {
	currentProblem.generateProblem();
	problemText.setText(currentProblem.getProblemText());
	writingArea.clearDrawing();
	feedbackText.setText('');
	nextButton.alpha = 0;
	nextButtonText.alpha = 0;
	gameState = 'playing';
}
game.down = function (x, y, obj) {
	if (gameState === 'playing') {
		// Check if touch is within writing area bounds using proper coordinate system
		var areaX = writingArea.x - 800; // writingArea width is 1600, so half is 800
		var areaY = writingArea.y - 200; // writingArea height is 400, so half is 200
		var areaWidth = 1600;
		var areaHeight = 400;
		if (x >= areaX && x <= areaX + areaWidth && y >= areaY && y <= areaY + areaHeight) {
			writingArea.startDrawing(x, y);
		}
	}
	if (showingWritingLesson) {
		// Check if touch is within writing lesson area bounds
		var lessonAreaX = writingLessonArea.x - 800;
		var lessonAreaY = writingLessonArea.y - 200;
		var lessonAreaWidth = 1600;
		var lessonAreaHeight = 400;
		if (x >= lessonAreaX && x <= lessonAreaX + lessonAreaWidth && y >= lessonAreaY && y <= lessonAreaY + lessonAreaHeight) {
			writingLessonArea.startDrawing(x, y);
		}
	}
};
game.move = function (x, y, obj) {
	if (gameState === 'playing') {
		writingArea.continueDrawing(x, y);
	}
	if (showingWritingLesson) {
		writingLessonArea.continueDrawing(x, y);
	}
};
game.up = function (x, y, obj) {
	if (gameState === 'playing') {
		writingArea.stopDrawing();
	}
	if (showingWritingLesson) {
		writingLessonArea.stopDrawing();
		writingLessonAnswerText.setText(writingLessonArea.writtenAnswer);
	}
};
submitButton.down = function (x, y, obj) {
	if (gameState === 'playing' && writingArea.writtenAnswer !== '') {
		checkAnswer();
	}
};
clearButton.down = function (x, y, obj) {
	if (gameState === 'playing') {
		writingArea.clearDrawing();
		feedbackText.setText('');
	}
};
nextButton.down = function (x, y, obj) {
	if (gameState === 'correct') {
		nextProblem();
	}
};
howToReadButton.down = function (x, y, obj) {
	if (!showingInstructions && !showingLessonMenu && !showingWritingLesson && screenState === 'game') {
		showWritingLesson();
	}
};
closeButton.down = function (x, y, obj) {
	if (showingInstructions) {
		showingInstructions = false;
		instructionPanel.alpha = 0;
		instructionTitle.alpha = 0;
		instructionContent.alpha = 0;
		closeButton.alpha = 0;
		closeButtonText.alpha = 0;
	}
};
// Create number keyboard
var keyboardNumbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
var keyboardButtons = [];
var keyboardTexts = [];
// Create letter keyboard
var keyboardLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var letterKeyboardButtons = [];
var letterKeyboardTexts = [];
// Create keyboard background
var keyboardBackground = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
keyboardBackground.x = 1024;
keyboardBackground.y = 2200;
keyboardBackground.width = 800;
keyboardBackground.height = 400;
keyboardBackground.tint = 0xE0E0E0;
// Create number buttons in a 3x4 grid (3 columns, 4 rows)
for (var i = 0; i < keyboardNumbers.length; i++) {
	var number = keyboardNumbers[i];
	var col = i % 3;
	var row = Math.floor(i / 3);
	// Special case for 0 - center it in the bottom row
	if (number === '0') {
		col = 1;
		row = 3;
	}
	var button = game.addChild(LK.getAsset('submitButton', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	button.x = 1024 + (col - 1) * 150;
	button.y = 2100 + row * 100;
	button.width = 120;
	button.height = 80;
	button.tint = 0x4169E1;
	var buttonText = new Text2(number, {
		size: 50,
		fill: 0xFFFFFF
	});
	buttonText.anchor.set(0.5, 0.5);
	buttonText.x = button.x;
	buttonText.y = button.y;
	game.addChild(buttonText);
	keyboardButtons.push(button);
	keyboardTexts.push(buttonText);
}
// Add event handlers for number buttons
for (var i = 0; i < keyboardButtons.length; i++) {
	(function (index) {
		keyboardButtons[index].down = function (x, y, obj) {
			if (gameState === 'playing') {
				var number = keyboardNumbers[index];
				writingArea.writtenAnswer += number;
				answerText.setText(writingArea.writtenAnswer);
				textBar.alpha = 1;
				feedbackText.setText('');
			}
		};
	})(i);
}
// Create letter keyboard background
var letterKeyboardBackground = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
letterKeyboardBackground.x = 1024;
letterKeyboardBackground.y = 2450;
letterKeyboardBackground.width = 1800;
letterKeyboardBackground.height = 300;
letterKeyboardBackground.tint = 0xE0E0E0;
letterKeyboardBackground.alpha = 0;
// Create letter buttons in rows (QWERTY-like layout but alphabetical)
for (var i = 0; i < keyboardLetters.length; i++) {
	var letter = keyboardLetters[i];
	var col = i % 9; // 9 letters per row
	var row = Math.floor(i / 9);
	var button = game.addChild(LK.getAsset('submitButton', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	button.x = 1024 + (col - 4) * 180; // Center the row
	button.y = 2350 + row * 80;
	button.width = 160;
	button.height = 70;
	button.tint = 0x9370DB;
	button.alpha = 0;
	var buttonText = new Text2(letter, {
		size: 40,
		fill: 0xFFFFFF
	});
	buttonText.anchor.set(0.5, 0.5);
	buttonText.x = button.x;
	buttonText.y = button.y;
	buttonText.alpha = 0;
	game.addChild(buttonText);
	letterKeyboardButtons.push(button);
	letterKeyboardTexts.push(buttonText);
}
// Add event handlers for letter buttons
for (var i = 0; i < letterKeyboardButtons.length; i++) {
	(function (index) {
		letterKeyboardButtons[index].down = function (x, y, obj) {
			if (showingWritingLesson) {
				var letter = keyboardLetters[index].toLowerCase();
				writingLessonArea.writtenAnswer += letter;
				writingLessonAnswerText.setText(writingLessonArea.writtenAnswer);
				writingLessonTextBar.alpha = 1;
				writingFeedbackText.setText('');
			}
		};
	})(i);
}
// Create lesson button in bottom right
var lessonButton = game.addChild(LK.getAsset('howToReadButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonButton.x = 1700;
lessonButton.y = 2500;
var lessonButtonText = new Text2('Lesson', {
	size: 50,
	fill: 0xFFFFFF
});
lessonButtonText.anchor.set(0.5, 0.5);
lessonButtonText.x = 1700;
lessonButtonText.y = 2500;
game.addChild(lessonButtonText);
// Create lesson menu UI elements
var lessonMenuPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonMenuPanel.x = 1024;
lessonMenuPanel.y = 1366;
lessonMenuPanel.alpha = 0;
var lessonMenuTitle = new Text2('Choose Your Lesson', {
	size: 100,
	fill: 0x333333
});
lessonMenuTitle.anchor.set(0.5, 0.5);
lessonMenuTitle.x = 1024;
lessonMenuTitle.y = 1000;
lessonMenuTitle.alpha = 0;
game.addChild(lessonMenuTitle);
var additionButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
additionButton.x = 800;
additionButton.y = 1300;
additionButton.alpha = 0;
additionButton.tint = 0x32CD32;
var additionButtonText = new Text2('Addition (+)', {
	size: 60,
	fill: 0xFFFFFF
});
additionButtonText.anchor.set(0.5, 0.5);
additionButtonText.x = 800;
additionButtonText.y = 1300;
additionButtonText.alpha = 0;
game.addChild(additionButtonText);
var multiplicationButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
multiplicationButton.x = 1248;
multiplicationButton.y = 1300;
multiplicationButton.alpha = 0;
multiplicationButton.tint = 0x4169E1;
var multiplicationButtonText = new Text2('Multiplication (×)', {
	size: 60,
	fill: 0xFFFFFF
});
multiplicationButtonText.anchor.set(0.5, 0.5);
multiplicationButtonText.x = 1248;
multiplicationButtonText.y = 1300;
multiplicationButtonText.alpha = 0;
game.addChild(multiplicationButtonText);
var lessonCloseButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonCloseButton.x = 1024;
lessonCloseButton.y = 1700;
lessonCloseButton.alpha = 0;
var lessonCloseButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
lessonCloseButtonText.anchor.set(0.5, 0.5);
lessonCloseButtonText.x = 1024;
lessonCloseButtonText.y = 1700;
lessonCloseButtonText.alpha = 0;
game.addChild(lessonCloseButtonText);
// Create lesson content UI elements
var lessonContentPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonContentPanel.x = 1024;
lessonContentPanel.y = 1366;
lessonContentPanel.alpha = 0;
var lessonContentTitle = new Text2('', {
	size: 100,
	fill: 0x333333
});
lessonContentTitle.anchor.set(0.5, 0.5);
lessonContentTitle.x = 1024;
lessonContentTitle.y = 900;
lessonContentTitle.alpha = 0;
game.addChild(lessonContentTitle);
var lessonContentText = new Text2('', {
	size: 70,
	fill: 0x444444
});
lessonContentText.anchor.set(0.5, 0.5);
lessonContentText.x = 1024;
lessonContentText.y = 1300;
lessonContentText.alpha = 0;
game.addChild(lessonContentText);
var lessonContentCloseButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonContentCloseButton.x = 1024;
lessonContentCloseButton.y = 1700;
lessonContentCloseButton.alpha = 0;
var lessonContentCloseButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
lessonContentCloseButtonText.anchor.set(0.5, 0.5);
lessonContentCloseButtonText.x = 1024;
lessonContentCloseButtonText.y = 1700;
lessonContentCloseButtonText.alpha = 0;
game.addChild(lessonContentCloseButtonText);
// Create writing lesson UI elements
var writingLessonPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingLessonPanel.x = 1024;
writingLessonPanel.y = 1366;
writingLessonPanel.alpha = 0;
var writingLessonTitle = new Text2('Writing Lesson', {
	size: 100,
	fill: 0x333333
});
writingLessonTitle.anchor.set(0.5, 0.5);
writingLessonTitle.x = 1024;
writingLessonTitle.y = 800;
writingLessonTitle.alpha = 0;
game.addChild(writingLessonTitle);
var writingPromptText = new Text2('', {
	size: 70,
	fill: 0x444444
});
writingPromptText.anchor.set(0.5, 0.5);
writingPromptText.x = 1024;
writingPromptText.y = 1000;
writingPromptText.alpha = 0;
game.addChild(writingPromptText);
var writingLessonArea = new WritingArea();
writingLessonArea.x = 1024;
writingLessonArea.y = 1300;
writingLessonArea.alpha = 0;
game.addChild(writingLessonArea);
var writingLessonTextBar = game.addChild(LK.getAsset('writingArea', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingLessonTextBar.x = 1024;
writingLessonTextBar.y = 1550;
writingLessonTextBar.alpha = 0;
writingLessonTextBar.tint = 0xE6F3FF;
var writingLessonAnswerText = new Text2('', {
	size: 80,
	fill: 0x000000
});
writingLessonAnswerText.anchor.set(0.5, 0.5);
writingLessonAnswerText.x = 1024;
writingLessonAnswerText.y = 1550;
writingLessonAnswerText.alpha = 0;
game.addChild(writingLessonAnswerText);
var writingCheckButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingCheckButton.x = 800;
writingCheckButton.y = 1650;
writingCheckButton.alpha = 0;
var writingCheckButtonText = new Text2('Check', {
	size: 60,
	fill: 0xFFFFFF
});
writingCheckButtonText.anchor.set(0.5, 0.5);
writingCheckButtonText.x = 800;
writingCheckButtonText.y = 1650;
writingCheckButtonText.alpha = 0;
game.addChild(writingCheckButtonText);
var writingClearButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingClearButton.x = 1248;
writingClearButton.y = 1650;
writingClearButton.alpha = 0;
writingClearButton.tint = 0xFF6B6B;
var writingClearButtonText = new Text2('Clear', {
	size: 60,
	fill: 0xFFFFFF
});
writingClearButtonText.anchor.set(0.5, 0.5);
writingClearButtonText.x = 1248;
writingClearButtonText.y = 1650;
writingClearButtonText.alpha = 0;
game.addChild(writingClearButtonText);
var writingLessonCloseButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingLessonCloseButton.x = 1024;
writingLessonCloseButton.y = 1800;
writingLessonCloseButton.alpha = 0;
var writingLessonCloseButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
writingLessonCloseButtonText.anchor.set(0.5, 0.5);
writingLessonCloseButtonText.x = 1024;
writingLessonCloseButtonText.y = 1800;
writingLessonCloseButtonText.alpha = 0;
game.addChild(writingLessonCloseButtonText);
var writingFeedbackText = new Text2('', {
	size: 70,
	fill: 0x32CD32
});
writingFeedbackText.anchor.set(0.5, 0.5);
writingFeedbackText.x = 1024;
writingFeedbackText.y = 1750;
writingFeedbackText.alpha = 0;
game.addChild(writingFeedbackText);
var writingHomeButton = game.addChild(LK.getAsset('homeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingHomeButton.x = 1700;
writingHomeButton.y = 1800;
writingHomeButton.alpha = 0;
var writingHomeButtonText = new Text2('Home', {
	size: 40,
	fill: 0xFFFFFF
});
writingHomeButtonText.anchor.set(0.5, 0.5);
writingHomeButtonText.x = 1700;
writingHomeButtonText.y = 1800;
writingHomeButtonText.alpha = 0;
game.addChild(writingHomeButtonText);
// Create home screen background shapes
var homeBackgroundShapes = [];
var homeBackgroundColors = [0xFF6B6B, 0x4ECDC4, 0x45B7D1, 0x96CEB4, 0xFECA57, 0xFF9FF3, 0x54A0FF];
// Create floating background shapes
for (var i = 0; i < 8; i++) {
	var shape = game.addChild(LK.getAsset('star', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	shape.x = Math.random() * 2048;
	shape.y = Math.random() * 2732;
	shape.alpha = 0.3;
	shape.tint = homeBackgroundColors[i % homeBackgroundColors.length];
	shape.scaleX = 0.5 + Math.random() * 1.5;
	shape.scaleY = shape.scaleX;
	shape.speedX = (Math.random() - 0.5) * 2;
	shape.speedY = (Math.random() - 0.5) * 2;
	shape.rotationSpeed = (Math.random() - 0.5) * 0.02;
	homeBackgroundShapes.push(shape);
}
// Create home screen UI elements
var homeScreenPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
homeScreenPanel.x = 1024;
homeScreenPanel.y = 1366;
homeScreenPanel.alpha = 1;
var homeScreenTitle = new Text2('Learning Center', {
	size: 120,
	fill: 0x333333
});
homeScreenTitle.anchor.set(0.5, 0.5);
homeScreenTitle.x = 1024;
homeScreenTitle.y = 1000;
homeScreenTitle.alpha = 1;
game.addChild(homeScreenTitle);
var homeRightButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
homeRightButton.x = 800;
homeRightButton.y = 1300;
homeRightButton.alpha = 1;
homeRightButton.tint = 0x9370DB;
var homeRightButtonText = new Text2('Write', {
	size: 70,
	fill: 0xFFFFFF
});
homeRightButtonText.anchor.set(0.5, 0.5);
homeRightButtonText.x = 800;
homeRightButtonText.y = 1300;
homeRightButtonText.alpha = 1;
game.addChild(homeRightButtonText);
var homeMathButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
homeMathButton.x = 1248;
homeMathButton.y = 1300;
homeMathButton.alpha = 1;
homeMathButton.tint = 0x32CD32;
var homeMathButtonText = new Text2('Math', {
	size: 70,
	fill: 0xFFFFFF
});
homeMathButtonText.anchor.set(0.5, 0.5);
homeMathButtonText.x = 1248;
homeMathButtonText.y = 1300;
homeMathButtonText.alpha = 1;
game.addChild(homeMathButtonText);
// Initialize with home screen visible
hideAllGameElements();
// Functions to show/hide lesson menu
function showLessonMenu() {
	showingLessonMenu = true;
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	// Show lesson menu
	lessonMenuPanel.alpha = 1;
	lessonMenuTitle.alpha = 1;
	additionButton.alpha = 1;
	additionButtonText.alpha = 1;
	multiplicationButton.alpha = 1;
	multiplicationButtonText.alpha = 1;
	lessonCloseButton.alpha = 1;
	lessonCloseButtonText.alpha = 1;
}
function hideLessonMenu() {
	showingLessonMenu = false;
	// Hide lesson menu
	lessonMenuPanel.alpha = 0;
	lessonMenuTitle.alpha = 0;
	additionButton.alpha = 0;
	additionButtonText.alpha = 0;
	multiplicationButton.alpha = 0;
	multiplicationButtonText.alpha = 0;
	lessonCloseButton.alpha = 0;
	lessonCloseButtonText.alpha = 0;
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 1;
	howToReadButtonText.alpha = 1;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
}
function showLessonContent(lessonType) {
	currentLessonType = lessonType;
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	// Show lesson content
	lessonContentPanel.alpha = 1;
	lessonContentTitle.alpha = 1;
	lessonContentText.alpha = 1;
	lessonContentCloseButton.alpha = 1;
	lessonContentCloseButtonText.alpha = 1;
	if (lessonType === 'addition') {
		lessonContentTitle.setText('Addition Lesson');
		lessonContentText.setText('• Addition means combining numbers together\n• The + symbol means "add" or "plus"\n• Example: 3 + 2 = 5\n• Think of it as counting forward\n• Start with the first number: 3\n• Count forward by the second number: 4, 5\n• Practice: 5 + 3 = ? (Start at 5, count: 6, 7, 8)\n• Try some problems to practice!');
	} else if (lessonType === 'multiplication') {
		lessonContentTitle.setText('Multiplication Lesson');
		lessonContentText.setText('• Multiplication means repeated addition\n• The × symbol means "times" or "multiply"\n• Example: 3 × 2 = 6 (same as 3 + 3 = 6)\n• Think of it as groups of numbers\n• 3 × 2 means "3 groups of 2" or "2 groups of 3"\n• Practice: 4 × 3 = ? (4 + 4 + 4 = 12)\n• Start with small numbers and work up!\n• Try some problems to practice!');
	}
}
function hideLessonContent() {
	// Hide lesson content
	lessonContentPanel.alpha = 0;
	lessonContentTitle.alpha = 0;
	lessonContentText.alpha = 0;
	lessonContentCloseButton.alpha = 0;
	lessonContentCloseButtonText.alpha = 0;
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 1;
	howToReadButtonText.alpha = 1;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
}
function showWritingLesson() {
	showingWritingLesson = true;
	screenState = 'writing_lesson';
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	lessonButton.alpha = 0;
	lessonButtonText.alpha = 0;
	// Show writing lesson elements
	writingLessonPanel.alpha = 1;
	writingLessonTitle.alpha = 1;
	writingPromptText.alpha = 1;
	writingLessonArea.alpha = 1;
	writingLessonTextBar.alpha = 1;
	writingLessonAnswerText.alpha = 1;
	writingCheckButton.alpha = 1;
	writingCheckButtonText.alpha = 1;
	writingClearButton.alpha = 1;
	writingClearButtonText.alpha = 1;
	writingLessonCloseButton.alpha = 1;
	writingLessonCloseButtonText.alpha = 1;
	writingFeedbackText.alpha = 1;
	writingHomeButton.alpha = 1;
	writingHomeButtonText.alpha = 1;
	// Show letter keyboard
	letterKeyboardBackground.alpha = 1;
	for (var i = 0; i < letterKeyboardButtons.length; i++) {
		letterKeyboardButtons[i].alpha = 1;
		letterKeyboardTexts[i].alpha = 1;
	}
	// Set a writing prompt
	var prompts = ['Hello', 'World', 'Learn', 'Write', 'Practice'];
	writingPrompt = prompts[Math.floor(Math.random() * prompts.length)];
	writingPromptText.setText('Write the word: ' + writingPrompt);
	writingAnswer = writingPrompt.toLowerCase();
	writingLessonArea.writtenAnswer = '';
	writingLessonAnswerText.setText('');
	writingFeedbackText.setText('');
}
function hideWritingLesson() {
	showingWritingLesson = false;
	screenState = 'game';
	// Hide writing lesson elements
	writingLessonPanel.alpha = 0;
	writingLessonTitle.alpha = 0;
	writingPromptText.alpha = 0;
	writingLessonArea.alpha = 0;
	writingLessonTextBar.alpha = 0;
	writingLessonAnswerText.alpha = 0;
	writingCheckButton.alpha = 0;
	writingCheckButtonText.alpha = 0;
	writingClearButton.alpha = 0;
	writingClearButtonText.alpha = 0;
	writingLessonCloseButton.alpha = 0;
	writingLessonCloseButtonText.alpha = 0;
	writingFeedbackText.alpha = 0;
	writingHomeButton.alpha = 0;
	writingHomeButtonText.alpha = 0;
	// Hide letter keyboard
	letterKeyboardBackground.alpha = 0;
	for (var i = 0; i < letterKeyboardButtons.length; i++) {
		letterKeyboardButtons[i].alpha = 0;
		letterKeyboardTexts[i].alpha = 0;
	}
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 1;
	howToReadButtonText.alpha = 1;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
	lessonButton.alpha = 1;
	lessonButtonText.alpha = 1;
}
function showHomeScreen() {
	// Show home screen elements
	homeScreenPanel.alpha = 1;
	homeScreenTitle.alpha = 1;
	homeRightButton.alpha = 1;
	homeRightButtonText.alpha = 1;
	homeMathButton.alpha = 1;
	homeMathButtonText.alpha = 1;
	// Show background shapes
	for (var i = 0; i < homeBackgroundShapes.length; i++) {
		homeBackgroundShapes[i].alpha = 0.3;
	}
	// Hide all other elements
	hideAllGameElements();
}
function hideHomeScreen() {
	// Hide home screen elements
	homeScreenPanel.alpha = 0;
	homeScreenTitle.alpha = 0;
	homeRightButton.alpha = 0;
	homeRightButtonText.alpha = 0;
	homeMathButton.alpha = 0;
	homeMathButtonText.alpha = 0;
	// Hide background shapes
	for (var i = 0; i < homeBackgroundShapes.length; i++) {
		homeBackgroundShapes[i].alpha = 0;
	}
}
function hideAllGameElements() {
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	lessonButton.alpha = 0;
	lessonButtonText.alpha = 0;
	nextButton.alpha = 0;
	nextButtonText.alpha = 0;
	mathHomeButton.alpha = 0;
	mathHomeButtonText.alpha = 0;
}
function showGameElements() {
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
	lessonButton.alpha = 1;
	lessonButtonText.alpha = 1;
	mathHomeButton.alpha = 1;
	mathHomeButtonText.alpha = 1;
}
function checkWritingAnswer() {
	var userAnswer = writingLessonArea.writtenAnswer.toLowerCase().trim();
	if (userAnswer === writingAnswer) {
		writingFeedbackText.setText('Correct! Well done!');
		writingFeedbackText.tint = 0x32CD32;
		LK.getSound('correct').play();
		LK.setTimeout(function () {
			// Generate new prompt
			var prompts = ['Hello', 'World', 'Learn', 'Write', 'Practice', 'Good', 'Nice', 'Great'];
			writingPrompt = prompts[Math.floor(Math.random() * prompts.length)];
			writingPromptText.setText('Write the word: ' + writingPrompt);
			writingAnswer = writingPrompt.toLowerCase();
			writingLessonArea.clearDrawing();
			writingLessonAnswerText.setText('');
			writingFeedbackText.setText('');
		}, 2000);
	} else {
		writingFeedbackText.setText('Try again! The word is: ' + writingPrompt);
		writingFeedbackText.tint = 0xFF6B6B;
		LK.getSound('incorrect').play();
		LK.setTimeout(function () {
			writingFeedbackText.setText('');
		}, 2000);
	}
}
lessonButton.down = function (x, y, obj) {
	if (!showingInstructions && !showingLessonMenu && screenState === 'game') {
		screenState = 'lesson_menu';
		showLessonMenu();
	}
};
additionButton.down = function (x, y, obj) {
	if (showingLessonMenu && screenState === 'lesson_menu') {
		hideLessonMenu();
		screenState = 'lesson_content';
		showLessonContent('addition');
	}
};
multiplicationButton.down = function (x, y, obj) {
	if (showingLessonMenu && screenState === 'lesson_menu') {
		hideLessonMenu();
		screenState = 'lesson_content';
		showLessonContent('multiplication');
	}
};
lessonCloseButton.down = function (x, y, obj) {
	if (showingLessonMenu && screenState === 'lesson_menu') {
		hideLessonMenu();
		screenState = 'game';
	}
};
lessonContentCloseButton.down = function (x, y, obj) {
	if (currentLessonType !== '' && screenState === 'lesson_content') {
		hideLessonContent();
		currentLessonType = '';
		screenState = 'game';
	}
};
writingCheckButton.down = function (x, y, obj) {
	if (showingWritingLesson && writingLessonArea.writtenAnswer !== '') {
		checkWritingAnswer();
	}
};
writingClearButton.down = function (x, y, obj) {
	if (showingWritingLesson) {
		writingLessonArea.clearDrawing();
		writingLessonAnswerText.setText('');
		writingFeedbackText.setText('');
	}
};
writingLessonCloseButton.down = function (x, y, obj) {
	if (showingWritingLesson && screenState === 'writing_lesson') {
		hideWritingLesson();
	}
};
homeRightButton.down = function (x, y, obj) {
	if (screenState === 'home') {
		hideHomeScreen();
		screenState = 'writing_lesson';
		showWritingLesson();
	}
};
homeMathButton.down = function (x, y, obj) {
	if (screenState === 'home') {
		hideHomeScreen();
		screenState = 'game';
		showGameElements();
	}
};
mathHomeButton.down = function (x, y, obj) {
	if (screenState === 'game') {
		hideAllGameElements();
		screenState = 'home';
		showHomeScreen();
	}
};
writingHomeButton.down = function (x, y, obj) {
	if (screenState === 'writing_lesson') {
		hideWritingLesson();
		screenState = 'home';
		showHomeScreen();
	}
};
game.update = function () {
	for (var i = celebrationStars.length - 1; i >= 0; i--) {
		var star = celebrationStars[i];
		if (star.alpha <= 0) {
			celebrationStars.splice(i, 1);
		}
	}
	// Animate home screen background shapes
	if (screenState === 'home') {
		for (var i = 0; i < homeBackgroundShapes.length; i++) {
			var shape = homeBackgroundShapes[i];
			// Move shapes
			shape.x += shape.speedX;
			shape.y += shape.speedY;
			// Rotate shapes
			shape.rotation += shape.rotationSpeed;
			// Bounce off edges
			if (shape.x < 0 || shape.x > 2048) {
				shape.speedX *= -1;
			}
			if (shape.y < 0 || shape.y > 2732) {
				shape.speedY *= -1;
			}
			// Keep shapes within bounds
			if (shape.x < 0) shape.x = 0;
			if (shape.x > 2048) shape.x = 2048;
			if (shape.y < 0) shape.y = 0;
			if (shape.y > 2732) shape.y = 2732;
		}
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var CelebrationStar = Container.expand(function () {
	var self = Container.call(this);
	var starGraphic = self.attachAsset('star', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.startAnimation = function () {
		self.alpha = 1;
		self.scaleX = 0.1;
		self.scaleY = 0.1;
		tween(self, {
			scaleX: 1.5,
			scaleY: 1.5
		}, {
			duration: 500,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				tween(self, {
					alpha: 0,
					scaleX: 0.5,
					scaleY: 0.5
				}, {
					duration: 500,
					easing: tween.easeIn,
					onFinish: function onFinish() {
						self.destroy();
					}
				});
			}
		});
	};
	return self;
});
var MathProblem = Container.expand(function () {
	var self = Container.call(this);
	self.num1 = 0;
	self.num2 = 0;
	self.operation = '+';
	self.answer = 0;
	self.difficulty = 1;
	self.generateProblem = function () {
		var maxNum = Math.min(10, 5 + self.difficulty * 2);
		self.num1 = Math.floor(Math.random() * maxNum) + 1;
		self.num2 = Math.floor(Math.random() * maxNum) + 1;
		var operations = ['+', '-'];
		if (self.difficulty > 3) {
			operations.push('×');
			operations.push('×'); // Add multiplication twice to make it more likely
		}
		self.operation = operations[Math.floor(Math.random() * operations.length)];
		if (self.operation === '-' && self.num1 < self.num2) {
			var temp = self.num1;
			self.num1 = self.num2;
			self.num2 = temp;
		}
		switch (self.operation) {
			case '+':
				self.answer = self.num1 + self.num2;
				break;
			case '-':
				self.answer = self.num1 - self.num2;
				break;
			case '×':
				self.answer = self.num1 * self.num2;
				break;
		}
	};
	self.getProblemText = function () {
		return self.num1 + ' ' + self.operation + ' ' + self.num2 + ' = ?';
	};
	return self;
});
var WritingArea = Container.expand(function () {
	var self = Container.call(this);
	var areaBackground = self.attachAsset('writingArea', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.drawingPath = [];
	self.isDrawing = false;
	self.writtenAnswer = '';
	self.startDrawing = function (x, y) {
		self.isDrawing = true;
		self.drawingPath = [];
		var localPos = self.toLocal({
			x: x,
			y: y
		});
		self.drawingPath.push({
			x: localPos.x,
			y: localPos.y
		});
		self.addDrawingDot(localPos.x, localPos.y);
		// Show text bar when writing starts
		textBar.alpha = 1;
	};
	self.continueDrawing = function (x, y) {
		if (self.isDrawing) {
			var localPos = self.toLocal({
				x: x,
				y: y
			});
			self.drawingPath.push({
				x: localPos.x,
				y: localPos.y
			});
			self.addDrawingDot(localPos.x, localPos.y);
		}
	};
	self.stopDrawing = function () {
		self.isDrawing = false;
		self.recognizeNumber();
	};
	self.addDrawingDot = function (x, y) {
		var dot = self.addChild(LK.getAsset('drawingDot', {
			anchorX: 0.5,
			anchorY: 0.5
		}));
		dot.x = x;
		dot.y = y;
	};
	self.recognizeNumber = function () {
		if (self.drawingPath.length > 3) {
			var bounds = self.getDrawingBounds();
			var recognizedChar = self.simpleNumberRecognition(bounds);
			if (recognizedChar !== null) {
				self.writtenAnswer += recognizedChar; // Append characters to build text
				answerText.setText(self.writtenAnswer);
				// Show text bar when text is recognized
				textBar.alpha = 1;
				// Clear the drawing path after recognition to prepare for next character
				self.clearCurrentDrawing();
			}
		}
	};
	self.getDrawingBounds = function () {
		if (self.drawingPath.length === 0) return null;
		var minX = self.drawingPath[0].x;
		var maxX = self.drawingPath[0].x;
		var minY = self.drawingPath[0].y;
		var maxY = self.drawingPath[0].y;
		for (var i = 1; i < self.drawingPath.length; i++) {
			var point = self.drawingPath[i];
			minX = Math.min(minX, point.x);
			maxX = Math.max(maxX, point.x);
			minY = Math.min(minY, point.y);
			maxY = Math.max(maxY, point.y);
		}
		return {
			minX: minX,
			maxX: maxX,
			minY: minY,
			maxY: maxY,
			width: maxX - minX,
			height: maxY - minY
		};
	};
	self.simpleNumberRecognition = function (bounds) {
		if (!bounds || bounds.width < 10 || bounds.height < 10) {
			feedbackText.setText('Draw bigger');
			feedbackText.tint = 0xFFA500;
			LK.setTimeout(function () {
				feedbackText.setText('');
			}, 1000);
			return null;
		}
		var aspectRatio = bounds.width / bounds.height;
		var pathLength = self.drawingPath.length;
		var recognizedChar = null;
		// Numbers - more lenient recognition
		if (aspectRatio > 2.0 || aspectRatio < 0.8 && pathLength > 20) recognizedChar = '1';else if (aspectRatio > 0.7 && aspectRatio < 1.3 && pathLength > 25) recognizedChar = '0';else if (pathLength > 20 && pathLength < 55 && aspectRatio > 0.5) recognizedChar = '2';else if (pathLength > 15 && pathLength < 45 && aspectRatio > 0.6) recognizedChar = '3';else if (pathLength > 10 && pathLength < 40) recognizedChar = '4';else if (pathLength > 20 && pathLength < 60 && aspectRatio > 0.5) recognizedChar = '5';else if (pathLength > 25 && pathLength < 65 && aspectRatio > 0.6) recognizedChar = '6';else if (pathLength > 10 && pathLength < 35 && aspectRatio > 0.4) recognizedChar = '7';else if (pathLength > 30 && pathLength < 75) recognizedChar = '8';else if (pathLength > 25 && pathLength < 70 && aspectRatio > 0.5) recognizedChar = '9';
		if (recognizedChar) {
			feedbackText.setText('Recognized: ' + recognizedChar);
			feedbackText.tint = 0x32CD32;
			LK.setTimeout(function () {
				feedbackText.setText('');
			}, 500);
			return recognizedChar;
		}
		// Fallback - show a hint about drawing more clearly
		feedbackText.setText('Draw more clearly');
		feedbackText.tint = 0xFFA500;
		LK.setTimeout(function () {
			feedbackText.setText('');
		}, 1000);
		return null;
	};
	self.clearCurrentDrawing = function () {
		for (var i = self.children.length - 1; i >= 0; i--) {
			var child = self.children[i];
			if (child !== areaBackground) {
				child.destroy();
			}
		}
		self.drawingPath = [];
	};
	self.clearDrawing = function () {
		for (var i = self.children.length - 1; i >= 0; i--) {
			var child = self.children[i];
			if (child !== areaBackground) {
				child.destroy();
			}
		}
		self.drawingPath = [];
		self.writtenAnswer = '';
		answerText.setText('');
		// Hide text bar when clearing
		textBar.alpha = 0;
	};
	self.backspace = function () {
		if (self.writtenAnswer.length > 0) {
			self.writtenAnswer = self.writtenAnswer.slice(0, -1);
			answerText.setText(self.writtenAnswer);
			// Hide text bar if no text remains
			if (self.writtenAnswer.length === 0) {
				textBar.alpha = 0;
			}
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var currentProblem = new MathProblem();
var writingArea = new WritingArea();
var gameState = 'playing';
var screenState = 'home'; // Track current screen: 'home', 'game', 'lesson_menu', 'lesson_content'
var correctAnswers = 0;
var level = storage.level || 1;
var celebrationStars = [];
currentProblem.difficulty = level;
currentProblem.generateProblem();
var background = game.addChild(LK.getAsset('background', {
	anchorX: 0,
	anchorY: 0
}));
var problemBox = game.addChild(LK.getAsset('problemBox', {
	anchorX: 0.5,
	anchorY: 0.5
}));
problemBox.x = 1024;
problemBox.y = 600;
var problemText = new Text2(currentProblem.getProblemText(), {
	size: 120,
	fill: 0x333333
});
problemText.anchor.set(0.5, 0.5);
problemText.x = 1024;
problemText.y = 600;
game.addChild(problemText);
writingArea.x = 1024;
writingArea.y = 1200;
game.addChild(writingArea);
var instructionText = new Text2('Write your answer below:', {
	size: 80,
	fill: 0x444444
});
instructionText.anchor.set(0.5, 0.5);
instructionText.x = 1024;
instructionText.y = 950;
game.addChild(instructionText);
var textBar = game.addChild(LK.getAsset('writingArea', {
	anchorX: 0.5,
	anchorY: 0.5
}));
textBar.x = 1024;
textBar.y = 1450;
textBar.alpha = 0;
textBar.tint = 0xE6F3FF;
var answerText = new Text2('', {
	size: 150,
	fill: 0x000000
});
answerText.anchor.set(0.5, 0.5);
answerText.x = 1024;
answerText.y = 1450;
game.addChild(answerText);
var submitButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
submitButton.x = 800;
submitButton.y = 1600;
var submitButtonText = new Text2('Check', {
	size: 60,
	fill: 0xFFFFFF
});
submitButtonText.anchor.set(0.5, 0.5);
submitButtonText.x = 800;
submitButtonText.y = 1600;
game.addChild(submitButtonText);
var clearButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
clearButton.x = 1150;
clearButton.y = 1600;
clearButton.tint = 0xFF6B6B;
var clearButtonText = new Text2('Clear', {
	size: 60,
	fill: 0xFFFFFF
});
clearButtonText.anchor.set(0.5, 0.5);
clearButtonText.x = 1150;
clearButtonText.y = 1600;
game.addChild(clearButtonText);
var nextButton = game.addChild(LK.getAsset('nextButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
nextButton.x = 1024;
nextButton.y = 1900;
nextButton.alpha = 0;
var nextButtonText = new Text2('Next Problem', {
	size: 60,
	fill: 0xFFFFFF
});
nextButtonText.anchor.set(0.5, 0.5);
nextButtonText.x = 1024;
nextButtonText.y = 1900;
nextButtonText.alpha = 0;
game.addChild(nextButtonText);
var feedbackText = new Text2('', {
	size: 80,
	fill: 0x32CD32
});
feedbackText.anchor.set(0.5, 0.5);
feedbackText.x = 1024;
feedbackText.y = 1750;
game.addChild(feedbackText);
var scoreText = new Text2('Score: 0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var levelText = new Text2('Level: ' + level, {
	size: 60,
	fill: 0xFFFFFF
});
levelText.anchor.set(1, 0);
LK.gui.topRight.addChild(levelText);
levelText.x = -120;
var howToReadButton = game.addChild(LK.getAsset('howToReadButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
howToReadButton.x = 1750;
howToReadButton.y = 1600;
var howToReadButtonText = new Text2('Right', {
	size: 50,
	fill: 0xFFFFFF
});
howToReadButtonText.anchor.set(0.5, 0.5);
howToReadButtonText.x = 1750;
howToReadButtonText.y = 1600;
game.addChild(howToReadButtonText);
var mathHomeButton = game.addChild(LK.getAsset('homeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
mathHomeButton.x = 1750;
mathHomeButton.y = 1700;
mathHomeButton.alpha = 0;
var mathHomeButtonText = new Text2('Home', {
	size: 40,
	fill: 0xFFFFFF
});
mathHomeButtonText.anchor.set(0.5, 0.5);
mathHomeButtonText.x = 1750;
mathHomeButtonText.y = 1700;
mathHomeButtonText.alpha = 0;
game.addChild(mathHomeButtonText);
var instructionPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
instructionPanel.x = 1024;
instructionPanel.y = 1366;
instructionPanel.alpha = 0;
var instructionTitle = new Text2('How to Read Math Problems', {
	size: 100,
	fill: 0x333333
});
instructionTitle.anchor.set(0.5, 0.5);
instructionTitle.x = 1024;
instructionTitle.y = 900;
instructionTitle.alpha = 0;
game.addChild(instructionTitle);
var instructionContent = new Text2('• Look at the math problem carefully\n• Read each number and symbol from left to right\n• + means add the numbers together\n• - means subtract the second from the first\n• × means multiply the numbers\n• = means "equals" - find what number comes after\n• Practice reading math problems out loud', {
	size: 70,
	fill: 0x444444
});
instructionContent.anchor.set(0.5, 0.5);
instructionContent.x = 1024;
instructionContent.y = 1300;
instructionContent.alpha = 0;
game.addChild(instructionContent);
var closeButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
closeButton.x = 1024;
closeButton.y = 1700;
closeButton.alpha = 0;
var closeButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
closeButtonText.anchor.set(0.5, 0.5);
closeButtonText.x = 1024;
closeButtonText.y = 1700;
closeButtonText.alpha = 0;
game.addChild(closeButtonText);
var showingInstructions = false;
var showingLessonMenu = false;
var currentLessonType = '';
var showingWritingLesson = false;
var writingPrompt = '';
var writingAnswer = '';
function checkAnswer() {
	var userAnswer = writingArea.writtenAnswer.trim();
	var correctAnswer = currentProblem.answer.toString();
	if (userAnswer === correctAnswer) {
		feedbackText.setText('Correct! Great job!');
		feedbackText.tint = 0x32CD32;
		LK.getSound('correct').play();
		correctAnswers++;
		LK.setScore(LK.getScore() + 10 * level);
		scoreText.setText('Score: ' + LK.getScore());
		createCelebrationStars();
		nextButton.alpha = 1;
		nextButtonText.alpha = 1;
		gameState = 'correct';
		if (correctAnswers >= 5) {
			level++;
			storage.level = level;
			levelText.setText('Level: ' + level);
			currentProblem.difficulty = level;
			correctAnswers = 0;
			LK.setTimeout(function () {
				feedbackText.setText('Level Up! Well done!');
				LK.getSound('celebration').play();
			}, 1000);
		}
	} else {
		feedbackText.setText('Try again! The answer is ' + correctAnswer);
		feedbackText.tint = 0xFF6B6B;
		LK.getSound('incorrect').play();
		LK.setTimeout(function () {
			feedbackText.setText('');
			writingArea.clearDrawing();
		}, 2000);
	}
}
function createCelebrationStars() {
	for (var i = 0; i < 5; i++) {
		var star = new CelebrationStar();
		star.x = 1024 + (Math.random() - 0.5) * 600;
		star.y = 1200 + (Math.random() - 0.5) * 400;
		celebrationStars.push(star);
		game.addChild(star);
		LK.setTimeout(function (starToAnimate) {
			return function () {
				starToAnimate.startAnimation();
			};
		}(star), i * 100);
	}
}
function nextProblem() {
	currentProblem.generateProblem();
	problemText.setText(currentProblem.getProblemText());
	writingArea.clearDrawing();
	feedbackText.setText('');
	nextButton.alpha = 0;
	nextButtonText.alpha = 0;
	gameState = 'playing';
}
game.down = function (x, y, obj) {
	if (gameState === 'playing') {
		// Check if touch is within writing area bounds using proper coordinate system
		var areaX = writingArea.x - 800; // writingArea width is 1600, so half is 800
		var areaY = writingArea.y - 200; // writingArea height is 400, so half is 200
		var areaWidth = 1600;
		var areaHeight = 400;
		if (x >= areaX && x <= areaX + areaWidth && y >= areaY && y <= areaY + areaHeight) {
			writingArea.startDrawing(x, y);
		}
	}
	if (showingWritingLesson) {
		// Check if touch is within writing lesson area bounds
		var lessonAreaX = writingLessonArea.x - 800;
		var lessonAreaY = writingLessonArea.y - 200;
		var lessonAreaWidth = 1600;
		var lessonAreaHeight = 400;
		if (x >= lessonAreaX && x <= lessonAreaX + lessonAreaWidth && y >= lessonAreaY && y <= lessonAreaY + lessonAreaHeight) {
			writingLessonArea.startDrawing(x, y);
		}
	}
};
game.move = function (x, y, obj) {
	if (gameState === 'playing') {
		writingArea.continueDrawing(x, y);
	}
	if (showingWritingLesson) {
		writingLessonArea.continueDrawing(x, y);
	}
};
game.up = function (x, y, obj) {
	if (gameState === 'playing') {
		writingArea.stopDrawing();
	}
	if (showingWritingLesson) {
		writingLessonArea.stopDrawing();
		writingLessonAnswerText.setText(writingLessonArea.writtenAnswer);
	}
};
submitButton.down = function (x, y, obj) {
	if (gameState === 'playing' && writingArea.writtenAnswer !== '') {
		checkAnswer();
	}
};
clearButton.down = function (x, y, obj) {
	if (gameState === 'playing') {
		writingArea.clearDrawing();
		feedbackText.setText('');
	}
};
nextButton.down = function (x, y, obj) {
	if (gameState === 'correct') {
		nextProblem();
	}
};
howToReadButton.down = function (x, y, obj) {
	if (!showingInstructions && !showingLessonMenu && !showingWritingLesson && screenState === 'game') {
		showWritingLesson();
	}
};
closeButton.down = function (x, y, obj) {
	if (showingInstructions) {
		showingInstructions = false;
		instructionPanel.alpha = 0;
		instructionTitle.alpha = 0;
		instructionContent.alpha = 0;
		closeButton.alpha = 0;
		closeButtonText.alpha = 0;
	}
};
// Create number keyboard
var keyboardNumbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0'];
var keyboardButtons = [];
var keyboardTexts = [];
// Create letter keyboard
var keyboardLetters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
var letterKeyboardButtons = [];
var letterKeyboardTexts = [];
// Create keyboard background
var keyboardBackground = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
keyboardBackground.x = 1024;
keyboardBackground.y = 2200;
keyboardBackground.width = 800;
keyboardBackground.height = 400;
keyboardBackground.tint = 0xE0E0E0;
// Create number buttons in a 3x4 grid (3 columns, 4 rows)
for (var i = 0; i < keyboardNumbers.length; i++) {
	var number = keyboardNumbers[i];
	var col = i % 3;
	var row = Math.floor(i / 3);
	// Special case for 0 - center it in the bottom row
	if (number === '0') {
		col = 1;
		row = 3;
	}
	var button = game.addChild(LK.getAsset('submitButton', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	button.x = 1024 + (col - 1) * 150;
	button.y = 2100 + row * 100;
	button.width = 120;
	button.height = 80;
	button.tint = 0x4169E1;
	var buttonText = new Text2(number, {
		size: 50,
		fill: 0xFFFFFF
	});
	buttonText.anchor.set(0.5, 0.5);
	buttonText.x = button.x;
	buttonText.y = button.y;
	game.addChild(buttonText);
	keyboardButtons.push(button);
	keyboardTexts.push(buttonText);
}
// Add event handlers for number buttons
for (var i = 0; i < keyboardButtons.length; i++) {
	(function (index) {
		keyboardButtons[index].down = function (x, y, obj) {
			if (gameState === 'playing') {
				var number = keyboardNumbers[index];
				writingArea.writtenAnswer += number;
				answerText.setText(writingArea.writtenAnswer);
				textBar.alpha = 1;
				feedbackText.setText('');
			}
		};
	})(i);
}
// Create letter keyboard background
var letterKeyboardBackground = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
letterKeyboardBackground.x = 1024;
letterKeyboardBackground.y = 2450;
letterKeyboardBackground.width = 1800;
letterKeyboardBackground.height = 300;
letterKeyboardBackground.tint = 0xE0E0E0;
letterKeyboardBackground.alpha = 0;
// Create letter buttons in rows (QWERTY-like layout but alphabetical)
for (var i = 0; i < keyboardLetters.length; i++) {
	var letter = keyboardLetters[i];
	var col = i % 9; // 9 letters per row
	var row = Math.floor(i / 9);
	var button = game.addChild(LK.getAsset('submitButton', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	button.x = 1024 + (col - 4) * 180; // Center the row
	button.y = 2350 + row * 80;
	button.width = 160;
	button.height = 70;
	button.tint = 0x9370DB;
	button.alpha = 0;
	var buttonText = new Text2(letter, {
		size: 40,
		fill: 0xFFFFFF
	});
	buttonText.anchor.set(0.5, 0.5);
	buttonText.x = button.x;
	buttonText.y = button.y;
	buttonText.alpha = 0;
	game.addChild(buttonText);
	letterKeyboardButtons.push(button);
	letterKeyboardTexts.push(buttonText);
}
// Add event handlers for letter buttons
for (var i = 0; i < letterKeyboardButtons.length; i++) {
	(function (index) {
		letterKeyboardButtons[index].down = function (x, y, obj) {
			if (showingWritingLesson) {
				var letter = keyboardLetters[index].toLowerCase();
				writingLessonArea.writtenAnswer += letter;
				writingLessonAnswerText.setText(writingLessonArea.writtenAnswer);
				writingLessonTextBar.alpha = 1;
				writingFeedbackText.setText('');
			}
		};
	})(i);
}
// Create lesson button in bottom right
var lessonButton = game.addChild(LK.getAsset('howToReadButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonButton.x = 1700;
lessonButton.y = 2500;
var lessonButtonText = new Text2('Lesson', {
	size: 50,
	fill: 0xFFFFFF
});
lessonButtonText.anchor.set(0.5, 0.5);
lessonButtonText.x = 1700;
lessonButtonText.y = 2500;
game.addChild(lessonButtonText);
// Create lesson menu UI elements
var lessonMenuPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonMenuPanel.x = 1024;
lessonMenuPanel.y = 1366;
lessonMenuPanel.alpha = 0;
var lessonMenuTitle = new Text2('Choose Your Lesson', {
	size: 100,
	fill: 0x333333
});
lessonMenuTitle.anchor.set(0.5, 0.5);
lessonMenuTitle.x = 1024;
lessonMenuTitle.y = 1000;
lessonMenuTitle.alpha = 0;
game.addChild(lessonMenuTitle);
var additionButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
additionButton.x = 800;
additionButton.y = 1300;
additionButton.alpha = 0;
additionButton.tint = 0x32CD32;
var additionButtonText = new Text2('Addition (+)', {
	size: 60,
	fill: 0xFFFFFF
});
additionButtonText.anchor.set(0.5, 0.5);
additionButtonText.x = 800;
additionButtonText.y = 1300;
additionButtonText.alpha = 0;
game.addChild(additionButtonText);
var multiplicationButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
multiplicationButton.x = 1248;
multiplicationButton.y = 1300;
multiplicationButton.alpha = 0;
multiplicationButton.tint = 0x4169E1;
var multiplicationButtonText = new Text2('Multiplication (×)', {
	size: 60,
	fill: 0xFFFFFF
});
multiplicationButtonText.anchor.set(0.5, 0.5);
multiplicationButtonText.x = 1248;
multiplicationButtonText.y = 1300;
multiplicationButtonText.alpha = 0;
game.addChild(multiplicationButtonText);
var lessonCloseButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonCloseButton.x = 1024;
lessonCloseButton.y = 1700;
lessonCloseButton.alpha = 0;
var lessonCloseButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
lessonCloseButtonText.anchor.set(0.5, 0.5);
lessonCloseButtonText.x = 1024;
lessonCloseButtonText.y = 1700;
lessonCloseButtonText.alpha = 0;
game.addChild(lessonCloseButtonText);
// Create lesson content UI elements
var lessonContentPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonContentPanel.x = 1024;
lessonContentPanel.y = 1366;
lessonContentPanel.alpha = 0;
var lessonContentTitle = new Text2('', {
	size: 100,
	fill: 0x333333
});
lessonContentTitle.anchor.set(0.5, 0.5);
lessonContentTitle.x = 1024;
lessonContentTitle.y = 900;
lessonContentTitle.alpha = 0;
game.addChild(lessonContentTitle);
var lessonContentText = new Text2('', {
	size: 70,
	fill: 0x444444
});
lessonContentText.anchor.set(0.5, 0.5);
lessonContentText.x = 1024;
lessonContentText.y = 1300;
lessonContentText.alpha = 0;
game.addChild(lessonContentText);
var lessonContentCloseButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
lessonContentCloseButton.x = 1024;
lessonContentCloseButton.y = 1700;
lessonContentCloseButton.alpha = 0;
var lessonContentCloseButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
lessonContentCloseButtonText.anchor.set(0.5, 0.5);
lessonContentCloseButtonText.x = 1024;
lessonContentCloseButtonText.y = 1700;
lessonContentCloseButtonText.alpha = 0;
game.addChild(lessonContentCloseButtonText);
// Create writing lesson UI elements
var writingLessonPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingLessonPanel.x = 1024;
writingLessonPanel.y = 1366;
writingLessonPanel.alpha = 0;
var writingLessonTitle = new Text2('Writing Lesson', {
	size: 100,
	fill: 0x333333
});
writingLessonTitle.anchor.set(0.5, 0.5);
writingLessonTitle.x = 1024;
writingLessonTitle.y = 800;
writingLessonTitle.alpha = 0;
game.addChild(writingLessonTitle);
var writingPromptText = new Text2('', {
	size: 70,
	fill: 0x444444
});
writingPromptText.anchor.set(0.5, 0.5);
writingPromptText.x = 1024;
writingPromptText.y = 1000;
writingPromptText.alpha = 0;
game.addChild(writingPromptText);
var writingLessonArea = new WritingArea();
writingLessonArea.x = 1024;
writingLessonArea.y = 1300;
writingLessonArea.alpha = 0;
game.addChild(writingLessonArea);
var writingLessonTextBar = game.addChild(LK.getAsset('writingArea', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingLessonTextBar.x = 1024;
writingLessonTextBar.y = 1550;
writingLessonTextBar.alpha = 0;
writingLessonTextBar.tint = 0xE6F3FF;
var writingLessonAnswerText = new Text2('', {
	size: 80,
	fill: 0x000000
});
writingLessonAnswerText.anchor.set(0.5, 0.5);
writingLessonAnswerText.x = 1024;
writingLessonAnswerText.y = 1550;
writingLessonAnswerText.alpha = 0;
game.addChild(writingLessonAnswerText);
var writingCheckButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingCheckButton.x = 800;
writingCheckButton.y = 1650;
writingCheckButton.alpha = 0;
var writingCheckButtonText = new Text2('Check', {
	size: 60,
	fill: 0xFFFFFF
});
writingCheckButtonText.anchor.set(0.5, 0.5);
writingCheckButtonText.x = 800;
writingCheckButtonText.y = 1650;
writingCheckButtonText.alpha = 0;
game.addChild(writingCheckButtonText);
var writingClearButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingClearButton.x = 1248;
writingClearButton.y = 1650;
writingClearButton.alpha = 0;
writingClearButton.tint = 0xFF6B6B;
var writingClearButtonText = new Text2('Clear', {
	size: 60,
	fill: 0xFFFFFF
});
writingClearButtonText.anchor.set(0.5, 0.5);
writingClearButtonText.x = 1248;
writingClearButtonText.y = 1650;
writingClearButtonText.alpha = 0;
game.addChild(writingClearButtonText);
var writingLessonCloseButton = game.addChild(LK.getAsset('closeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingLessonCloseButton.x = 1024;
writingLessonCloseButton.y = 1800;
writingLessonCloseButton.alpha = 0;
var writingLessonCloseButtonText = new Text2('Close', {
	size: 50,
	fill: 0xFFFFFF
});
writingLessonCloseButtonText.anchor.set(0.5, 0.5);
writingLessonCloseButtonText.x = 1024;
writingLessonCloseButtonText.y = 1800;
writingLessonCloseButtonText.alpha = 0;
game.addChild(writingLessonCloseButtonText);
var writingFeedbackText = new Text2('', {
	size: 70,
	fill: 0x32CD32
});
writingFeedbackText.anchor.set(0.5, 0.5);
writingFeedbackText.x = 1024;
writingFeedbackText.y = 1750;
writingFeedbackText.alpha = 0;
game.addChild(writingFeedbackText);
var writingHomeButton = game.addChild(LK.getAsset('homeButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
writingHomeButton.x = 1700;
writingHomeButton.y = 1800;
writingHomeButton.alpha = 0;
var writingHomeButtonText = new Text2('Home', {
	size: 40,
	fill: 0xFFFFFF
});
writingHomeButtonText.anchor.set(0.5, 0.5);
writingHomeButtonText.x = 1700;
writingHomeButtonText.y = 1800;
writingHomeButtonText.alpha = 0;
game.addChild(writingHomeButtonText);
// Create home screen background shapes
var homeBackgroundShapes = [];
var homeBackgroundColors = [0xFF6B6B, 0x4ECDC4, 0x45B7D1, 0x96CEB4, 0xFECA57, 0xFF9FF3, 0x54A0FF];
// Create floating background shapes
for (var i = 0; i < 8; i++) {
	var shape = game.addChild(LK.getAsset('star', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	shape.x = Math.random() * 2048;
	shape.y = Math.random() * 2732;
	shape.alpha = 0.3;
	shape.tint = homeBackgroundColors[i % homeBackgroundColors.length];
	shape.scaleX = 0.5 + Math.random() * 1.5;
	shape.scaleY = shape.scaleX;
	shape.speedX = (Math.random() - 0.5) * 2;
	shape.speedY = (Math.random() - 0.5) * 2;
	shape.rotationSpeed = (Math.random() - 0.5) * 0.02;
	homeBackgroundShapes.push(shape);
}
// Create home screen UI elements
var homeScreenPanel = game.addChild(LK.getAsset('instructionPanel', {
	anchorX: 0.5,
	anchorY: 0.5
}));
homeScreenPanel.x = 1024;
homeScreenPanel.y = 1366;
homeScreenPanel.alpha = 1;
var homeScreenTitle = new Text2('Learning Center', {
	size: 120,
	fill: 0x333333
});
homeScreenTitle.anchor.set(0.5, 0.5);
homeScreenTitle.x = 1024;
homeScreenTitle.y = 1000;
homeScreenTitle.alpha = 1;
game.addChild(homeScreenTitle);
var homeRightButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
homeRightButton.x = 800;
homeRightButton.y = 1300;
homeRightButton.alpha = 1;
homeRightButton.tint = 0x9370DB;
var homeRightButtonText = new Text2('Write', {
	size: 70,
	fill: 0xFFFFFF
});
homeRightButtonText.anchor.set(0.5, 0.5);
homeRightButtonText.x = 800;
homeRightButtonText.y = 1300;
homeRightButtonText.alpha = 1;
game.addChild(homeRightButtonText);
var homeMathButton = game.addChild(LK.getAsset('submitButton', {
	anchorX: 0.5,
	anchorY: 0.5
}));
homeMathButton.x = 1248;
homeMathButton.y = 1300;
homeMathButton.alpha = 1;
homeMathButton.tint = 0x32CD32;
var homeMathButtonText = new Text2('Math', {
	size: 70,
	fill: 0xFFFFFF
});
homeMathButtonText.anchor.set(0.5, 0.5);
homeMathButtonText.x = 1248;
homeMathButtonText.y = 1300;
homeMathButtonText.alpha = 1;
game.addChild(homeMathButtonText);
// Initialize with home screen visible
hideAllGameElements();
// Functions to show/hide lesson menu
function showLessonMenu() {
	showingLessonMenu = true;
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	// Show lesson menu
	lessonMenuPanel.alpha = 1;
	lessonMenuTitle.alpha = 1;
	additionButton.alpha = 1;
	additionButtonText.alpha = 1;
	multiplicationButton.alpha = 1;
	multiplicationButtonText.alpha = 1;
	lessonCloseButton.alpha = 1;
	lessonCloseButtonText.alpha = 1;
}
function hideLessonMenu() {
	showingLessonMenu = false;
	// Hide lesson menu
	lessonMenuPanel.alpha = 0;
	lessonMenuTitle.alpha = 0;
	additionButton.alpha = 0;
	additionButtonText.alpha = 0;
	multiplicationButton.alpha = 0;
	multiplicationButtonText.alpha = 0;
	lessonCloseButton.alpha = 0;
	lessonCloseButtonText.alpha = 0;
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 1;
	howToReadButtonText.alpha = 1;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
}
function showLessonContent(lessonType) {
	currentLessonType = lessonType;
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	// Show lesson content
	lessonContentPanel.alpha = 1;
	lessonContentTitle.alpha = 1;
	lessonContentText.alpha = 1;
	lessonContentCloseButton.alpha = 1;
	lessonContentCloseButtonText.alpha = 1;
	if (lessonType === 'addition') {
		lessonContentTitle.setText('Addition Lesson');
		lessonContentText.setText('• Addition means combining numbers together\n• The + symbol means "add" or "plus"\n• Example: 3 + 2 = 5\n• Think of it as counting forward\n• Start with the first number: 3\n• Count forward by the second number: 4, 5\n• Practice: 5 + 3 = ? (Start at 5, count: 6, 7, 8)\n• Try some problems to practice!');
	} else if (lessonType === 'multiplication') {
		lessonContentTitle.setText('Multiplication Lesson');
		lessonContentText.setText('• Multiplication means repeated addition\n• The × symbol means "times" or "multiply"\n• Example: 3 × 2 = 6 (same as 3 + 3 = 6)\n• Think of it as groups of numbers\n• 3 × 2 means "3 groups of 2" or "2 groups of 3"\n• Practice: 4 × 3 = ? (4 + 4 + 4 = 12)\n• Start with small numbers and work up!\n• Try some problems to practice!');
	}
}
function hideLessonContent() {
	// Hide lesson content
	lessonContentPanel.alpha = 0;
	lessonContentTitle.alpha = 0;
	lessonContentText.alpha = 0;
	lessonContentCloseButton.alpha = 0;
	lessonContentCloseButtonText.alpha = 0;
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 1;
	howToReadButtonText.alpha = 1;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
}
function showWritingLesson() {
	showingWritingLesson = true;
	screenState = 'writing_lesson';
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	lessonButton.alpha = 0;
	lessonButtonText.alpha = 0;
	// Show writing lesson elements
	writingLessonPanel.alpha = 1;
	writingLessonTitle.alpha = 1;
	writingPromptText.alpha = 1;
	writingLessonArea.alpha = 1;
	writingLessonTextBar.alpha = 1;
	writingLessonAnswerText.alpha = 1;
	writingCheckButton.alpha = 1;
	writingCheckButtonText.alpha = 1;
	writingClearButton.alpha = 1;
	writingClearButtonText.alpha = 1;
	writingLessonCloseButton.alpha = 1;
	writingLessonCloseButtonText.alpha = 1;
	writingFeedbackText.alpha = 1;
	writingHomeButton.alpha = 1;
	writingHomeButtonText.alpha = 1;
	// Show letter keyboard
	letterKeyboardBackground.alpha = 1;
	for (var i = 0; i < letterKeyboardButtons.length; i++) {
		letterKeyboardButtons[i].alpha = 1;
		letterKeyboardTexts[i].alpha = 1;
	}
	// Set a writing prompt
	var prompts = ['Hello', 'World', 'Learn', 'Write', 'Practice'];
	writingPrompt = prompts[Math.floor(Math.random() * prompts.length)];
	writingPromptText.setText('Write the word: ' + writingPrompt);
	writingAnswer = writingPrompt.toLowerCase();
	writingLessonArea.writtenAnswer = '';
	writingLessonAnswerText.setText('');
	writingFeedbackText.setText('');
}
function hideWritingLesson() {
	showingWritingLesson = false;
	screenState = 'game';
	// Hide writing lesson elements
	writingLessonPanel.alpha = 0;
	writingLessonTitle.alpha = 0;
	writingPromptText.alpha = 0;
	writingLessonArea.alpha = 0;
	writingLessonTextBar.alpha = 0;
	writingLessonAnswerText.alpha = 0;
	writingCheckButton.alpha = 0;
	writingCheckButtonText.alpha = 0;
	writingClearButton.alpha = 0;
	writingClearButtonText.alpha = 0;
	writingLessonCloseButton.alpha = 0;
	writingLessonCloseButtonText.alpha = 0;
	writingFeedbackText.alpha = 0;
	writingHomeButton.alpha = 0;
	writingHomeButtonText.alpha = 0;
	// Hide letter keyboard
	letterKeyboardBackground.alpha = 0;
	for (var i = 0; i < letterKeyboardButtons.length; i++) {
		letterKeyboardButtons[i].alpha = 0;
		letterKeyboardTexts[i].alpha = 0;
	}
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 1;
	howToReadButtonText.alpha = 1;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
	lessonButton.alpha = 1;
	lessonButtonText.alpha = 1;
}
function showHomeScreen() {
	// Show home screen elements
	homeScreenPanel.alpha = 1;
	homeScreenTitle.alpha = 1;
	homeRightButton.alpha = 1;
	homeRightButtonText.alpha = 1;
	homeMathButton.alpha = 1;
	homeMathButtonText.alpha = 1;
	// Show background shapes
	for (var i = 0; i < homeBackgroundShapes.length; i++) {
		homeBackgroundShapes[i].alpha = 0.3;
	}
	// Hide all other elements
	hideAllGameElements();
}
function hideHomeScreen() {
	// Hide home screen elements
	homeScreenPanel.alpha = 0;
	homeScreenTitle.alpha = 0;
	homeRightButton.alpha = 0;
	homeRightButtonText.alpha = 0;
	homeMathButton.alpha = 0;
	homeMathButtonText.alpha = 0;
	// Hide background shapes
	for (var i = 0; i < homeBackgroundShapes.length; i++) {
		homeBackgroundShapes[i].alpha = 0;
	}
}
function hideAllGameElements() {
	// Hide game elements
	problemBox.alpha = 0;
	problemText.alpha = 0;
	writingArea.alpha = 0;
	textBar.alpha = 0;
	answerText.alpha = 0;
	instructionText.alpha = 0;
	submitButton.alpha = 0;
	submitButtonText.alpha = 0;
	clearButton.alpha = 0;
	clearButtonText.alpha = 0;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 0;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 0;
		keyboardTexts[i].alpha = 0;
	}
	feedbackText.alpha = 0;
	lessonButton.alpha = 0;
	lessonButtonText.alpha = 0;
	nextButton.alpha = 0;
	nextButtonText.alpha = 0;
	mathHomeButton.alpha = 0;
	mathHomeButtonText.alpha = 0;
}
function showGameElements() {
	// Show game elements
	problemBox.alpha = 1;
	problemText.alpha = 1;
	writingArea.alpha = 1;
	if (writingArea.writtenAnswer.length > 0) {
		textBar.alpha = 1;
	}
	answerText.alpha = 1;
	instructionText.alpha = 1;
	submitButton.alpha = 1;
	submitButtonText.alpha = 1;
	clearButton.alpha = 1;
	clearButtonText.alpha = 1;
	howToReadButton.alpha = 0;
	howToReadButtonText.alpha = 0;
	keyboardBackground.alpha = 1;
	for (var i = 0; i < keyboardButtons.length; i++) {
		keyboardButtons[i].alpha = 1;
		keyboardTexts[i].alpha = 1;
	}
	feedbackText.alpha = 1;
	lessonButton.alpha = 1;
	lessonButtonText.alpha = 1;
	mathHomeButton.alpha = 1;
	mathHomeButtonText.alpha = 1;
}
function checkWritingAnswer() {
	var userAnswer = writingLessonArea.writtenAnswer.toLowerCase().trim();
	if (userAnswer === writingAnswer) {
		writingFeedbackText.setText('Correct! Well done!');
		writingFeedbackText.tint = 0x32CD32;
		LK.getSound('correct').play();
		LK.setTimeout(function () {
			// Generate new prompt
			var prompts = ['Hello', 'World', 'Learn', 'Write', 'Practice', 'Good', 'Nice', 'Great'];
			writingPrompt = prompts[Math.floor(Math.random() * prompts.length)];
			writingPromptText.setText('Write the word: ' + writingPrompt);
			writingAnswer = writingPrompt.toLowerCase();
			writingLessonArea.clearDrawing();
			writingLessonAnswerText.setText('');
			writingFeedbackText.setText('');
		}, 2000);
	} else {
		writingFeedbackText.setText('Try again! The word is: ' + writingPrompt);
		writingFeedbackText.tint = 0xFF6B6B;
		LK.getSound('incorrect').play();
		LK.setTimeout(function () {
			writingFeedbackText.setText('');
		}, 2000);
	}
}
lessonButton.down = function (x, y, obj) {
	if (!showingInstructions && !showingLessonMenu && screenState === 'game') {
		screenState = 'lesson_menu';
		showLessonMenu();
	}
};
additionButton.down = function (x, y, obj) {
	if (showingLessonMenu && screenState === 'lesson_menu') {
		hideLessonMenu();
		screenState = 'lesson_content';
		showLessonContent('addition');
	}
};
multiplicationButton.down = function (x, y, obj) {
	if (showingLessonMenu && screenState === 'lesson_menu') {
		hideLessonMenu();
		screenState = 'lesson_content';
		showLessonContent('multiplication');
	}
};
lessonCloseButton.down = function (x, y, obj) {
	if (showingLessonMenu && screenState === 'lesson_menu') {
		hideLessonMenu();
		screenState = 'game';
	}
};
lessonContentCloseButton.down = function (x, y, obj) {
	if (currentLessonType !== '' && screenState === 'lesson_content') {
		hideLessonContent();
		currentLessonType = '';
		screenState = 'game';
	}
};
writingCheckButton.down = function (x, y, obj) {
	if (showingWritingLesson && writingLessonArea.writtenAnswer !== '') {
		checkWritingAnswer();
	}
};
writingClearButton.down = function (x, y, obj) {
	if (showingWritingLesson) {
		writingLessonArea.clearDrawing();
		writingLessonAnswerText.setText('');
		writingFeedbackText.setText('');
	}
};
writingLessonCloseButton.down = function (x, y, obj) {
	if (showingWritingLesson && screenState === 'writing_lesson') {
		hideWritingLesson();
	}
};
homeRightButton.down = function (x, y, obj) {
	if (screenState === 'home') {
		hideHomeScreen();
		screenState = 'writing_lesson';
		showWritingLesson();
	}
};
homeMathButton.down = function (x, y, obj) {
	if (screenState === 'home') {
		hideHomeScreen();
		screenState = 'game';
		showGameElements();
	}
};
mathHomeButton.down = function (x, y, obj) {
	if (screenState === 'game') {
		hideAllGameElements();
		screenState = 'home';
		showHomeScreen();
	}
};
writingHomeButton.down = function (x, y, obj) {
	if (screenState === 'writing_lesson') {
		hideWritingLesson();
		screenState = 'home';
		showHomeScreen();
	}
};
game.update = function () {
	for (var i = celebrationStars.length - 1; i >= 0; i--) {
		var star = celebrationStars[i];
		if (star.alpha <= 0) {
			celebrationStars.splice(i, 1);
		}
	}
	// Animate home screen background shapes
	if (screenState === 'home') {
		for (var i = 0; i < homeBackgroundShapes.length; i++) {
			var shape = homeBackgroundShapes[i];
			// Move shapes
			shape.x += shape.speedX;
			shape.y += shape.speedY;
			// Rotate shapes
			shape.rotation += shape.rotationSpeed;
			// Bounce off edges
			if (shape.x < 0 || shape.x > 2048) {
				shape.speedX *= -1;
			}
			if (shape.y < 0 || shape.y > 2732) {
				shape.speedY *= -1;
			}
			// Keep shapes within bounds
			if (shape.x < 0) shape.x = 0;
			if (shape.x > 2048) shape.x = 2048;
			if (shape.y < 0) shape.y = 0;
			if (shape.y > 2732) shape.y = 2732;
		}
	}
};