/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	money: 100,
	followers: 0,
	memeLevel: 1,
	studioLevel: 1,
	staffLevel: 0,
	marketingLevel: 0,
	autoEarningRate: 0,
	lastPlayed: "undefined"
});
/**** 
* Classes
****/ 
var Button = Container.expand(function (text, width, height, color) {
	var self = Container.call(this);
	var buttonShape = self.attachAsset('button', {
		width: width || 600,
		height: height || 200,
		color: color || 0x4CAF50,
		anchorX: 0.5,
		anchorY: 0.5
	});
	var buttonText = new Text2(text, {
		size: 60,
		fill: 0xFFFFFF
	});
	buttonText.anchor.set(0.5, 0.5);
	self.addChild(buttonText);
	self.setText = function (newText) {
		buttonText.setText(newText);
	};
	self.setColor = function (newColor) {
		buttonShape.tint = newColor;
	};
	self.down = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100
		});
		LK.getSound('click').play();
	};
	self.up = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
	};
	return self;
});
var ConfettiParticle = Container.expand(function () {
	var self = Container.call(this);
	// Create a simple rectangular confetti graphic
	var confettiGraphic = self.attachAsset('bullet', {
		// Using 'bullet' shape asset for simplicity
		width: 20,
		height: 10,
		anchorX: 0.5,
		anchorY: 0.5,
		color: Math.random() * 0xFFFFFF // Random color
	});
	// Set random initial rotation and scale
	confettiGraphic.rotation = Math.random() * Math.PI * 2;
	confettiGraphic.scale.set(0.5 + Math.random() * 0.5);
	// Random initial speed and direction
	self.speedY = 5 + Math.random() * 5; // Falling speed
	self.speedX = -5 + Math.random() * 10; // Sideways speed
	// Gravity effect (optional, makes falling more realistic)
	var gravity = 0.2;
	// Rotation speed
	self.rotationSpeed = -0.1 + Math.random() * 0.2;
	// Update method for animation
	self.update = function () {
		self.y += self.speedY;
		self.x += self.speedX;
		self.speedY += gravity; // Apply gravity
		self.rotation += self.rotationSpeed; // Rotate
		// Remove particles when they fall off screen
		if (self.y > 2732 + 50) {
			self.destroy();
		}
	};
	return self;
});
var Connect4Game = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var COLS = 7;
	var ROWS = 6;
	var CELL_SIZE = 150;
	var PLAYER_COLOR = 0xff0000;
	var AI_COLOR = 0xffff00;
	var BOARD_COLOR = 0x0000ff;
	var EMPTY = 0;
	var PLAYER = 1;
	var AI = 2;
	// Game state
	var board = [];
	var currentPlayer = PLAYER;
	var gameOver = false;
	var gameResult = null;
	// Create board background
	var boardBackground = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: COLS * CELL_SIZE + 40,
		height: ROWS * CELL_SIZE + 40,
		color: BOARD_COLOR
	});
	// Initialize board data
	function initBoard() {
		board = [];
		for (var i = 0; i < COLS; i++) {
			board[i] = [];
			for (var j = 0; j < ROWS; j++) {
				board[i][j] = EMPTY;
			}
		}
		currentPlayer = PLAYER;
		gameOver = false;
		gameResult = null;
	}
	// Create visual board
	var cells = [];
	var circles = [];
	function createVisualBoard() {
		// Create cell containers
		for (var col = 0; col < COLS; col++) {
			cells[col] = [];
			circles[col] = [];
			for (var row = 0; row < ROWS; row++) {
				var cell = new Container();
				cell.x = (col - COLS / 2 + 0.5) * CELL_SIZE;
				cell.y = (row - ROWS / 2 + 0.5) * CELL_SIZE;
				var cellCircle = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: CELL_SIZE - 20,
					height: CELL_SIZE - 20,
					color: 0xffffff
				});
				cell.addChild(cellCircle);
				self.addChild(cell);
				cells[col][row] = cell;
				circles[col][row] = cellCircle;
			}
		}
	}
	// Create status text
	var statusText = new Text2("Your turn", {
		size: 60,
		fill: 0xffffff
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -500;
	self.addChild(statusText);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Drop piece in column
	function dropPiece(col) {
		if (gameOver) {
			return false;
		}
		// Find the first empty row
		var row = ROWS - 1;
		while (row >= 0 && board[col][row] !== EMPTY) {
			row--;
		}
		// If column is full
		if (row < 0) {
			return false;
		}
		// Drop piece
		board[col][row] = currentPlayer;
		updateVisualBoard();
		// Check for win
		if (checkWin(col, row)) {
			gameOver = true;
			gameResult = currentPlayer;
			finishButton.visible = true;
			statusText.setText(currentPlayer === PLAYER ? "You win!" : "AI wins!");
			return true;
		}
		// Check for draw
		if (checkDraw()) {
			gameOver = true;
			gameResult = 0;
			finishButton.visible = true;
			statusText.setText("It's a draw!");
			return true;
		}
		// Switch player
		currentPlayer = currentPlayer === PLAYER ? AI : PLAYER;
		statusText.setText(currentPlayer === PLAYER ? "Your turn" : "AI thinking...");
		return true;
	}
	// Update the visual representation of the board
	function updateVisualBoard() {
		for (var col = 0; col < COLS; col++) {
			for (var row = 0; row < ROWS; row++) {
				if (board[col][row] === EMPTY) {
					circles[col][row].tint = 0xffffff;
				} else if (board[col][row] === PLAYER) {
					circles[col][row].tint = PLAYER_COLOR;
				} else {
					circles[col][row].tint = AI_COLOR;
				}
			}
		}
	}
	// Check if current player won
	function checkWin(col, row) {
		var player = board[col][row];
		// Check horizontal
		var count = 0;
		for (var c = 0; c < COLS; c++) {
			if (board[c][row] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check vertical
		count = 0;
		for (var r = 0; r < ROWS; r++) {
			if (board[col][r] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check diagonal (/)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 3; r < ROWS; r++) {
				if (board[c][r] === player && board[c + 1][r - 1] === player && board[c + 2][r - 2] === player && board[c + 3][r - 3] === player) {
					return true;
				}
			}
		}
		// Check diagonal (\)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 0; r < ROWS - 3; r++) {
				if (board[c][r] === player && board[c + 1][r + 1] === player && board[c + 2][r + 2] === player && board[c + 3][r + 3] === player) {
					return true;
				}
			}
		}
		return false;
	}
	// Check if the game is a draw
	function checkDraw() {
		for (var col = 0; col < COLS; col++) {
			if (board[col][0] === EMPTY) {
				return false;
			}
		}
		return true;
	}
	// AI move
	function aiMove() {
		// Smarter AI using minimax with alpha-beta pruning
		var validColumns = [];
		var scores = [];
		// Get all valid columns
		for (var col = 0; col < COLS; col++) {
			if (board[col][0] === EMPTY) {
				validColumns.push(col);
			}
		}
		// Evaluate each possible move
		for (var i = 0; i < validColumns.length; i++) {
			var col = validColumns[i];
			var tempBoard = copyBoard(board);
			var row = dropPieceInTempBoard(tempBoard, col, AI);
			// Check for immediate win
			if (checkWinInBoard(tempBoard, col, row, AI)) {
				// AI can win in this move, take it immediately
				LK.setTimeout(function () {
					dropPiece(col);
				}, 1000);
				return;
			}
			// Otherwise, evaluate this move
			var score = evaluateMove(tempBoard, col, row, 4); // Look 4 moves ahead
			scores.push({
				col: col,
				score: score
			});
		}
		// Find best move
		var bestMoves = [];
		var bestScore = -Infinity;
		for (var i = 0; i < scores.length; i++) {
			if (scores[i].score > bestScore) {
				bestScore = scores[i].score;
				bestMoves = [scores[i].col];
			} else if (scores[i].score === bestScore) {
				bestMoves.push(scores[i].col);
			}
		}
		// Choose randomly among best moves for some unpredictability
		var bestCol = bestMoves[Math.floor(Math.random() * bestMoves.length)];
		LK.setTimeout(function () {
			dropPiece(bestCol);
		}, 1000);
	}
	// Creates a copy of the current board
	function copyBoard(originalBoard) {
		var newBoard = [];
		for (var i = 0; i < COLS; i++) {
			newBoard[i] = [];
			for (var j = 0; j < ROWS; j++) {
				newBoard[i][j] = originalBoard[i][j];
			}
		}
		return newBoard;
	}
	// Drops a piece in a temporary board for evaluation
	function dropPieceInTempBoard(tempBoard, col, player) {
		var row = ROWS - 1;
		while (row >= 0 && tempBoard[col][row] !== EMPTY) {
			row--;
		}
		if (row >= 0) {
			tempBoard[col][row] = player;
		}
		return row;
	}
	// Check for win in temporary board
	function checkWinInBoard(board, col, row, player) {
		// Check horizontal
		var count = 0;
		for (var c = 0; c < COLS; c++) {
			if (board[c][row] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check vertical
		count = 0;
		for (var r = 0; r < ROWS; r++) {
			if (board[col][r] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check diagonal (/)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 3; r < ROWS; r++) {
				if (board[c][r] === player && board[c + 1][r - 1] === player && board[c + 2][r - 2] === player && board[c + 3][r - 3] === player) {
					return true;
				}
			}
		}
		// Check diagonal (\)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 0; r < ROWS - 3; r++) {
				if (board[c][r] === player && board[c + 1][r + 1] === player && board[c + 2][r + 2] === player && board[c + 3][r + 3] === player) {
					return true;
				}
			}
		}
		return false;
	}
	// Evaluate the board position using heuristics
	function evaluateMove(tempBoard, col, row, depth) {
		// Base score
		var score = 0;
		// Check if this is a winning move
		if (checkWinInBoard(tempBoard, col, row, AI)) {
			return 1000;
		}
		// Look for opponent's winning move and block it
		for (var c = 0; c < COLS; c++) {
			if (tempBoard[c][0] === EMPTY) {
				var tempRow = dropPieceInTempBoard(copyBoard(tempBoard), c, PLAYER);
				if (tempRow >= 0 && checkWinInBoard(tempBoard, c, tempRow, PLAYER)) {
					score -= 800; // Prioritize blocking opponent win
				}
			}
		}
		// Evaluate center control (center column is strategically valuable)
		var centerCol = Math.floor(COLS / 2);
		var centerCount = 0;
		for (var r = 0; r < ROWS; r++) {
			if (tempBoard[centerCol][r] === AI) {
				centerCount++;
			}
		}
		score += centerCount * 30;
		// Evaluate connections (2 in a row, 3 in a row)
		score += evaluateConnections(tempBoard, AI) * 10;
		score -= evaluateConnections(tempBoard, PLAYER) * 8;
		// Prefer lower rows (more stable position)
		score += (ROWS - row) * 5;
		// If depth allows, do minimax for deeper evaluation
		if (depth > 0) {
			// Simulate player's best response
			var minScore = Infinity;
			for (var c = 0; c < COLS; c++) {
				if (tempBoard[c][0] === EMPTY) {
					var newBoard = copyBoard(tempBoard);
					var newRow = dropPieceInTempBoard(newBoard, c, PLAYER);
					if (newRow >= 0) {
						// Check if player wins
						if (checkWinInBoard(newBoard, c, newRow, PLAYER)) {
							return -900; // Very bad for AI
						}
						// Recursive evaluation with reduced depth
						var moveScore = evaluateMove(newBoard, c, newRow, depth - 1);
						if (moveScore < minScore) {
							minScore = moveScore;
						}
					}
				}
			}
			// Add minimax result with lower weight
			score += minScore * 0.5;
		}
		return score;
	}
	// Evaluate connected pieces (2 or 3 in a row)
	function evaluateConnections(board, player) {
		var score = 0;
		// Check horizontal connections
		for (var r = 0; r < ROWS; r++) {
			for (var c = 0; c < COLS - 1; c++) {
				if (board[c][r] === player && board[c + 1][r] === player) {
					score += 1; // 2 in a row
					if (c + 2 < COLS && board[c + 2][r] === player) {
						score += 5; // 3 in a row
					}
				}
			}
		}
		// Check vertical connections
		for (var c = 0; c < COLS; c++) {
			for (var r = 0; r < ROWS - 1; r++) {
				if (board[c][r] === player && board[c][r + 1] === player) {
					score += 1; // 2 in a row
					if (r + 2 < ROWS && board[c][r + 2] === player) {
						score += 5; // 3 in a row
					}
				}
			}
		}
		// Diagonal connections would be similar but more complex
		// Adding them would make the AI even smarter
		return score;
	}
	// Handle column click
	self.down = function (x, y) {
		if (currentPlayer !== PLAYER || gameOver) {
			return;
		}
		// Convert coordinates to column
		var boardX = x;
		var col = Math.floor((boardX + COLS / 2 * CELL_SIZE) / CELL_SIZE);
		// Check if valid column
		if (col >= 0 && col < COLS) {
			if (dropPiece(col) && !gameOver) {
				aiMove();
			}
		}
	};
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize
	initBoard();
	createVisualBoard();
	updateVisualBoard();
	return self;
});
var ControversialWarning = Container.expand(function () {
	var self = Container.call(this);
	// Background overlay
	var overlay = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0x000000,
		alpha: 0.8
	});
	// Warning box
	var warningBox = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 800,
		height: 500,
		color: 0xffffff
	});
	// Warning text
	var warningText = new Text2("You uploaded a meme that your fans didn't like :(", {
		size: 60,
		fill: 0xff0000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	warningText.anchor.set(0.5, 0.5);
	warningText.y = -80;
	self.addChild(warningText);
	// Lose followers text
	var loseFollowersText = new Text2("You lose a lot of followers!", {
		size: 50,
		fill: 0x000000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	loseFollowersText.anchor.set(0.5, 0.5);
	loseFollowersText.y = 50;
	self.addChild(loseFollowersText);
	// OK button
	var okButton = new Button("OK", 300, 100, 0x4CAF50);
	okButton.y = 180;
	self.addChild(okButton);
	self.setCloseCallback = function (callback) {
		okButton.up = function () {
			tween(okButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (callback) {
				callback();
			}
			self.visible = false;
		};
	};
	return self;
});
var CupShuffleGame = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var NUM_CUPS = 3;
	var CUP_SIZE = 200;
	var BALL_SIZE = 50;
	var SHUFFLE_SPEED = 500; // ms
	var SHUFFLE_ROUNDS = 3;
	// Game state
	var cups = [];
	var ball;
	var ballPosition = 0; // Index of the cup containing the ball
	var isShuffling = false;
	var gameOver = false;
	var gameResult = null; // 'win' or 'lose'
	// Create background
	var background = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 1000,
		color: 0xcccccc // Light grey
	});
	// Create status text
	var statusText = new Text2("Watch the ball!", {
		size: 60,
		fill: 0x000000
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -400;
	self.addChild(statusText);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Initialize game
	function initGame() {
		cups = [];
		isShuffling = false;
		gameOver = false;
		gameResult = null;
		statusText.setText("Watch the ball!");
		finishButton.visible = false;
		// Remove old cups and ball
		self.removeChildren();
		self.addChild(background);
		self.addChild(statusText);
		self.addChild(finishButton);
		self.addChild(returnHomeButton);
		// Create cups
		var startX = -(NUM_CUPS - 1) * CUP_SIZE / 2;
		for (var i = 0; i < NUM_CUPS; i++) {
			var cup = new Container();
			cup.x = startX + i * CUP_SIZE;
			cup.y = 100; // Position the cups
			cup.index = i;
			var cupGraphic = cup.attachAsset('Cup', {
				// Using paperChoice as a simple cup graphic
				anchorX: 0.5,
				anchorY: 1,
				// Anchor at the bottom
				width: CUP_SIZE,
				height: CUP_SIZE * 1.2 // Slightly taller
			});
			cup.addChild(cupGraphic);
			self.addChild(cup);
			cups.push(cup);
		}
		// Place the ball under a random cup
		ballPosition = Math.floor(Math.random() * NUM_CUPS);
		ball = self.attachAsset('moneyIcon', {
			// Using moneyIcon as a simple ball graphic
			anchorX: 0.5,
			anchorY: 0.5,
			width: BALL_SIZE,
			height: BALL_SIZE,
			color: 0xffd700 // Gold color for the ball
		});
		// Position ball below the initial cup
		ball.x = cups[ballPosition].x;
		ball.y = cups[ballPosition].y - CUP_SIZE / 2; // Position below the cup
		self.addChild(ball);
		// Make cups interactive after initial reveal
		LK.setTimeout(function () {
			showBall(true); // Reveal initial ball position briefly
			LK.setTimeout(function () {
				showBall(false); // Hide ball
				shuffleCups(); // Start shuffling
			}, 1000); // Reveal time
		}, 500); // Initial delay
	}
	// Show or hide the ball
	function showBall(visible) {
		if (ball) {
			ball.visible = visible;
		}
	}
	// Shuffle the cups
	function shuffleCups() {
		isShuffling = true;
		statusText.setText("Shuffling...");
		var shuffleSequence = [];
		// Generate a sequence of swaps
		for (var i = 0; i < SHUFFLE_ROUNDS; i++) {
			// Swap adjacent cups
			var swapIndex = Math.floor(Math.random() * (NUM_CUPS - 1));
			shuffleSequence.push(swapIndex);
		}
		performShuffleStep(0);
		function performShuffleStep(step) {
			if (step >= shuffleSequence.length) {
				// Shuffling finished
				isShuffling = false;
				statusText.setText("Which cup is the ball under?");
				// Make cups clickable to select
				for (var i = 0; i < NUM_CUPS; i++) {
					cups[i].interactive = true;
					cups[i].down = function (index) {
						// Using an IIFE to properly capture the cup index
						return function (x, y, obj) {
							if (!gameOver && !isShuffling) {
								// Call checkGuess with the proper context and the captured index
								checkGuess(index); // Using the local function directly instead of through 'self'
							}
						};
					}(i); // Immediately invoke the function with the current value of i
				}
				return;
			}
			var swapIndex = shuffleSequence[step];
			var cup1 = cups[swapIndex];
			var cup2 = cups[swapIndex + 1];
			// Swap positions visually
			var cup1X = cup1.x;
			var cup2X = cup2.x;
			tween(cup1, {
				x: cup2X
			}, {
				duration: SHUFFLE_SPEED / 2
			});
			tween(cup2, {
				x: cup1X
			}, {
				duration: SHUFFLE_SPEED / 2
			});
			// If ball is under one of the swapped cups, move it with the cup
			if (ballPosition === swapIndex) {
				tween(ball, {
					x: cup2X
				}, {
					duration: SHUFFLE_SPEED / 2
				});
				ballPosition = swapIndex + 1;
			} else if (ballPosition === swapIndex + 1) {
				tween(ball, {
					x: cup1X
				}, {
					duration: SHUFFLE_SPEED / 2
				});
				ballPosition = swapIndex;
			}
			// Continue to the next step after animation
			LK.setTimeout(function () {
				// Update logical positions in the cups array
				var temp = cups[swapIndex];
				cups[swapIndex] = cups[swapIndex + 1];
				cups[swapIndex + 1] = temp;
				performShuffleStep(step + 1);
			}, SHUFFLE_SPEED / 2); // Half the shuffle speed for each swap animation
		}
	}
	// Check the player's guess
	function checkGuess(selectedIndex) {
		if (gameOver || isShuffling) {
			return;
		}
		gameOver = true;
		// Disable cup interaction
		for (var i = 0; i < NUM_CUPS; i++) {
			cups[i].interactive = false;
		}
		// Reveal all cups and the ball
		showBall(true);
		// You might want to lift the cups or make them transparent briefly here
		if (selectedIndex === ballPosition) {
			statusText.setText("You found it! You win!");
			gameResult = 'win';
			finishButton.visible = true;
			// Play win sound
			LK.getSound('coin').play();
		} else {
			statusText.setText("Wrong cup! You lose.");
			gameResult = 'lose';
			finishButton.visible = true;
			// Play lose sound (if you have one)
			// LK.getSound('lose').play();
		}
	}
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback(gameResult); // Pass the result of the game
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize the game when created
	initGame();
	return self;
});
var DonationNotification = Container.expand(function () {
	var self = Container.call(this);
	// Background for notification
	var background = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 400,
		height: 100,
		color: 0x4CAF50
	});
	// Text for donation notification
	var notificationText = new Text2("Someone donated $0!", {
		size: 48,
		fill: 0x000000
	});
	notificationText.anchor.set(0.5, 0.5);
	self.addChild(notificationText);
	// Set donation amount
	self.setAmount = function (amount) {
		notificationText.setText("Someone donated $" + amount + "!");
	};
	// Start animation - slide in from right and then fade out
	self.animate = function () {
		// Reset position and opacity
		self.x = 2048 + 250; // Start off-screen
		self.alpha = 1;
		// Slide in animation
		tween(self, {
			x: 2048 - 250
		}, {
			duration: 500,
			onFinish: function onFinish() {
				// Wait a moment at the side of the screen
				LK.setTimeout(function () {
					// Then fade out
					tween(self, {
						alpha: 0
					}, {
						duration: 500,
						onFinish: function onFinish() {
							// Remove from parent when done
							if (self.parent) {
								self.parent.removeChild(self);
							}
						}
					});
				}, 2000);
			}
		});
	};
	return self;
});
var GameSelector = Container.expand(function () {
	var self = Container.call(this);
	var background = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0xdddddd
	});
	var titleText = new Text2("Choose A Game To Record", {
		size: 80,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0.5);
	titleText.y = -900;
	self.addChild(titleText);
	var connect4Button = new Button("Connect 4", 600, 200, 0x4CAF50);
	connect4Button.y = -700;
	self.addChild(connect4Button);
	var matchingButton = new Button("Memory Match", 600, 200, 0x2196F3);
	matchingButton.y = -400;
	self.addChild(matchingButton);
	var puzzleButton = new Button("Sliding Puzzle", 600, 200, 0x9C27B0);
	puzzleButton.y = -100;
	self.addChild(puzzleButton);
	var cupShuffleButton = new Button("Cup Shuffle", 600, 200, 0x4CAF50); // Standard button color
	cupShuffleButton.y = 200; // Position below the sliding puzzle button
	self.addChild(cupShuffleButton);
	// Create locked indicator for matching game
	var lockedText = new Text2("100 followers needed", {
		size: 60,
		fill: 0xFF5733
	});
	lockedText.anchor.set(0.5, 0.5);
	lockedText.y = -330;
	lockedText.visible = false;
	self.addChild(lockedText);
	// Create locked indicator for puzzle game
	var puzzleLockedText = new Text2("250 followers needed", {
		size: 60,
		fill: 0xFF5733
	});
	puzzleLockedText.anchor.set(0.5, 0.5);
	puzzleLockedText.y = -30;
	puzzleLockedText.visible = false;
	self.addChild(puzzleLockedText);
	// Create locked indicator for cup shuffle game
	var cupShuffleLockedText = new Text2("400 followers needed", {
		size: 60,
		fill: 0xFF5733
	});
	cupShuffleLockedText.anchor.set(0.5, 0.5);
	cupShuffleLockedText.y = 270; // Position below the button
	cupShuffleLockedText.visible = false;
	self.addChild(cupShuffleLockedText);
	var backButton = new Button("Go Back", 400, 130, 0xff5555);
	backButton.y = 900;
	self.addChild(backButton);
	// Update lock status based on follower count
	self.updateLockStatus = function (followerCount) {
		// Always show matching game as unlocked
		lockedText.visible = false;
		// Always show puzzle game as unlocked
		puzzleLockedText.visible = false;
		// Check and update Cup Shuffle lock status
		if (followerCount < 400) {
			cupShuffleButton.setColor(0x9E9E9E); // Grey out the button
			cupShuffleLockedText.visible = true;
		} else {
			cupShuffleButton.setColor(0x4CAF50); // Set normal color
			cupShuffleLockedText.visible = false;
		}
	};
	self.setGameCallback = function (callback) {
		connect4Button.up = function (x, y, obj) {
			tween(connect4Button, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback("connect4");
			}
		};
		matchingButton.up = function (x, y, obj) {
			tween(matchingButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				// Allow matching game for all players
				matchingButton.setColor(0x2196F3); // Set normal color
				callback("matching");
			}
		};
		puzzleButton.up = function (x, y, obj) {
			tween(puzzleButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				// Allow puzzle game for all players
				puzzleButton.setColor(0x9C27B0); // Set normal color
				callback("puzzle");
			}
		};
		cupShuffleButton.up = function (x, y, obj) {
			tween(cupShuffleButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			// Check if unlocked before calling the callback
			if (followers >= 400 && callback) {
				callback("cupShuffle");
			}
		};
	};
	self.setBackCallback = function (callback) {
		backButton.up = function (x, y, obj) {
			tween(backButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	return self;
});
var MatchingGame = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var GRID_SIZE = 4; // 4x4 grid
	var CARD_SIZE = 150;
	var CARD_MARGIN = 20;
	var CARD_COLOR = 0x2196F3;
	var CARD_BACK_COLOR = 0x607D8B;
	// Game state
	var cards = [];
	var flippedCards = [];
	var matchedPairs = 0;
	var totalPairs = 8;
	var canFlip = true;
	var gameOver = false;
	// Create background
	var boardBackground = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: (CARD_SIZE + CARD_MARGIN) * GRID_SIZE + 40,
		height: (CARD_SIZE + CARD_MARGIN) * GRID_SIZE + 40,
		color: 0x333333
	});
	// Create status text
	var statusText = new Text2("Match all pairs", {
		size: 60,
		fill: 0xffffff
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -500;
	self.addChild(statusText);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Symbols for matching (emojis represented as text)
	var symbols = ["๐", "๐", "๐", "๐ฎ", "๐", "โฝ", "๐ต", "๐จ"];
	// Initialize game
	function initGame() {
		cards = [];
		flippedCards = [];
		matchedPairs = 0;
		gameOver = false;
		canFlip = true;
		// Create all card pairs
		var cardValues = [];
		for (var i = 0; i < totalPairs; i++) {
			cardValues.push(i);
			cardValues.push(i);
		}
		// Shuffle the cards
		for (var i = cardValues.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = cardValues[i];
			cardValues[i] = cardValues[j];
			cardValues[j] = temp;
		}
		// Create card grid
		for (var row = 0; row < GRID_SIZE; row++) {
			for (var col = 0; col < GRID_SIZE; col++) {
				var index = row * GRID_SIZE + col;
				// Create card container
				var card = new Container();
				card.x = (col - GRID_SIZE / 2 + 0.5) * (CARD_SIZE + CARD_MARGIN);
				card.y = (row - GRID_SIZE / 2 + 0.5) * (CARD_SIZE + CARD_MARGIN);
				// Card back (shown initially)
				var cardBack = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: CARD_SIZE,
					height: CARD_SIZE,
					color: CARD_BACK_COLOR
				});
				// Card front (hidden initially)
				var cardFront = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: CARD_SIZE,
					height: CARD_SIZE,
					color: CARD_COLOR
				});
				cardFront.visible = false;
				// Symbol text
				var symbolText = new Text2(symbols[cardValues[index]], {
					size: 60,
					fill: 0xFFFFFF
				});
				symbolText.anchor.set(0.5, 0.5);
				symbolText.visible = false;
				// Add to card container
				card.addChild(cardBack);
				card.addChild(cardFront);
				card.addChild(symbolText);
				// Store card data
				card.value = cardValues[index];
				card.isFlipped = false;
				card.isMatched = false;
				// Add to game
				self.addChild(card);
				cards.push(card);
				// Make clickable
				card.interactive = true;
			}
		}
	}
	// Flip a card
	function flipCard(card) {
		if (!canFlip || card.isFlipped || card.isMatched || gameOver) {
			return;
		}
		// Flip animation
		card.isFlipped = true;
		card.children[0].visible = false; // Hide back
		card.children[1].visible = true; // Show front
		card.children[2].visible = true; // Show symbol
		// Add to flipped cards
		flippedCards.push(card);
		// Check for match if we have 2 cards flipped
		if (flippedCards.length === 2) {
			canFlip = false;
			// Check for match
			if (flippedCards[0].value === flippedCards[1].value) {
				// Cards match
				LK.setTimeout(function () {
					flippedCards[0].isMatched = true;
					flippedCards[1].isMatched = true;
					// Highlight matched cards
					tween(flippedCards[0].children[1], {
						tint: 0x4CAF50 // Green tint
					}, {
						duration: 300
					});
					tween(flippedCards[1].children[1], {
						tint: 0x4CAF50 // Green tint
					}, {
						duration: 300
					});
					matchedPairs++;
					flippedCards = [];
					canFlip = true;
					// Check for game over
					if (matchedPairs === totalPairs) {
						gameOver = true;
						statusText.setText("You win!");
						finishButton.visible = true;
					}
				}, 500);
			} else {
				// Cards don't match, flip back
				LK.setTimeout(function () {
					flippedCards[0].isFlipped = false;
					flippedCards[0].children[0].visible = true; // Show back
					flippedCards[0].children[1].visible = false; // Hide front
					flippedCards[0].children[2].visible = false; // Hide symbol
					flippedCards[1].isFlipped = false;
					flippedCards[1].children[0].visible = true; // Show back
					flippedCards[1].children[1].visible = false; // Hide front
					flippedCards[1].children[2].visible = false; // Hide symbol
					flippedCards = [];
					canFlip = true;
				}, 1000);
			}
		}
	}
	// Handle click
	self.down = function (x, y) {
		if (gameOver) {
			return;
		}
		// Convert coordinates to find clicked card
		for (var i = 0; i < cards.length; i++) {
			var card = cards[i];
			var cardBounds = {
				x: card.x - CARD_SIZE / 2,
				y: card.y - CARD_SIZE / 2,
				width: CARD_SIZE,
				height: CARD_SIZE
			};
			if (x >= cardBounds.x && x <= cardBounds.x + cardBounds.width && y >= cardBounds.y && y <= cardBounds.y + cardBounds.height) {
				flipCard(card);
				break;
			}
		}
	};
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize the game
	initGame();
	return self;
});
// MatchingGame instance (initially null)
var MemeStudio = Container.expand(function () {
	var self = Container.call(this);
	var studioBackground = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		height: 1800
	});
	var memeCanvas = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 400,
		height: 400,
		y: -250
	});
	var memeText = new Text2("Your Meme Here", {
		size: 54,
		fill: 0x000000
	});
	memeText.anchor.set(0.5, 0.5);
	memeText.y = -250;
	self.addChild(memeText);
	// Current displayed meme asset
	var currentMemeAsset = null;
	// Function to display a random meme
	function displayRandomMeme() {
		// Remove previous meme if exists
		if (currentMemeAsset) {
			self.removeChild(currentMemeAsset);
		}
		// Generate random number between 1-14 to select a meme (added Wheelmeme1 and Meme10-13)
		var memeNumber = Math.floor(Math.random() * 14) + 1;
		var memeId;
		if (memeNumber === 10) {
			memeId = 'Wheelmeme1';
		} else if (memeNumber >= 10 && memeNumber <= 13) {
			memeId = 'Meme' + (memeNumber + 0); // Map 10-13 to Meme10-13
		} else {
			memeId = 'Meme' + memeNumber;
		}
		// Create the new meme asset
		currentMemeAsset = LK.getAsset(memeId, {
			anchorX: 0.5,
			anchorY: 0.5,
			y: -250,
			width: 400,
			height: 400
		});
		// Add the meme to display
		self.addChild(currentMemeAsset);
	}
	var earningsText = new Text2("Earnings: $0", {
		size: 48,
		fill: 0x333333
	});
	earningsText.anchor.set(0.5, 0.5);
	earningsText.y = 20;
	self.addChild(earningsText);
	var createButton = new Button("Create Meme!", 500, 150, 0x4CAF50);
	createButton.y = 150;
	self.addChild(createButton);
	var createVideoButton = new Button("Create Video", 500, 150, 0x4CAF50);
	createVideoButton.y = 270;
	self.addChild(createVideoButton);
	var upgradesButton = new Button("Upgrades", 500, 150, 0x4CAF50);
	upgradesButton.y = 390;
	self.addChild(upgradesButton);
	// Add Spin button
	var spinButton = new Container();
	var spinButtonAsset = spinButton.attachAsset('Spin', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 270,
		// 180 * 1.5
		height: 270 // 180 * 1.5
	});
	spinButton.y = upgradesButton.y + upgradesButton.height / 2 + spinButtonAsset.height / 2 + 30 - 1030; // Position below upgrades, moved down 70px
	spinButton.x = 460; // Move 460 pixels to the right from the center (was 490)
	self.addChild(spinButton);
	// Add interactivity to Spin button
	spinButton.down = function (x, y, obj) {
		tween(spinButtonAsset, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100
		});
		LK.getSound('click').play();
	};
	// Note: Spin button 'up' handler will be set via setSpinButtonCallback
	var followerGainText = new Text2("+0 followers", {
		size: 42,
		fill: 0xFF5733
	});
	followerGainText.anchor.set(0.5, 0.5);
	followerGainText.y = 250; // Adjusted y position, original might conflict visually
	followerGainText.alpha = 0;
	self.addChild(followerGainText);
	// Update the meme canvas based on current studio level
	self.updateMemeQuality = function (level) {
		var quality = ["Potato Quality", "Standard Meme", "HD Meme", "Ultra HD Meme", "Viral-Ready Meme"];
		var colors = [0xCCCCCC, 0xFFFFFF, 0xFFFFDD, 0xDDFFFF, 0xFFDDFF];
		if (level > quality.length) {
			level = quality.length;
		}
		memeText.setText(quality[level - 1]);
		memeCanvas.tint = colors[level - 1];
		// Scale the meme canvas slightly with each level
		var scale = 1 + (level - 1) * 0.1;
		memeCanvas.scale.set(scale, scale);
	};
	// Show the earnings animation
	self.showEarnings = function (amount, followers) {
		earningsText.setText("Earnings: $" + amount);
		tween(earningsText, {
			scaleX: 1.3,
			scaleY: 1.3
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(earningsText, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 300
				});
			}
		});
		if (followers > 0) {
			followerGainText.setText("+" + followers + " followers");
			followerGainText.alpha = 1;
			tween(followerGainText, {
				y: 230,
				alpha: 0
			}, {
				duration: 1500
			});
		}
	};
	self.setCreateButtonCallback = function (callback) {
		createButton.up = function (x, y, obj) {
			tween(createButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			// Display a random meme when creating a new meme
			displayRandomMeme();
			LK.getSound('click').play();
			LK.getSound('click').play();
			if (callback) {
				callback();
			}
		};
	};
	self.setCreateVideoButtonCallback = function (callback) {
		createVideoButton.up = function (x, y, obj) {
			tween(createVideoButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.setUpgradesButtonCallback = function (callback) {
		upgradesButton.up = function (x, y, obj) {
			tween(upgradesButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.setSpinButtonCallback = function (callback) {
		spinButton.up = function (x, y, obj) {
			tween(spinButtonAsset, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	return self;
});
var PuzzleGame = Container.expand(function () {
	var self = Container.call(this);
	var GRID_SIZE = 4; // 4x4 grid
	var PUZZLE_SIZE = 600;
	var TILE_SIZE = PUZZLE_SIZE / GRID_SIZE;
	var TILE_MARGIN = 5;
	// Game state
	var tiles = [];
	var emptyPos = {
		row: GRID_SIZE - 1,
		col: GRID_SIZE - 1
	};
	var moves = 0;
	var gameOver = false;
	// Create board background
	var boardBackground = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: PUZZLE_SIZE + 40,
		height: PUZZLE_SIZE + 40,
		color: 0x333333
	});
	// Create status text
	var statusText = new Text2("Slide tiles to order 1-15", {
		size: 60,
		fill: 0xffffff
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -500;
	self.addChild(statusText);
	// Create moves counter
	var movesText = new Text2("Moves: 0", {
		size: 60,
		fill: 0xffffff
	});
	movesText.anchor.set(0.5, 0.5);
	movesText.y = -420;
	self.addChild(movesText);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Initialize board
	function initBoard() {
		tiles = [];
		moves = 0;
		gameOver = false;
		movesText.setText("Moves: 0");
		// Create all tiles
		var values = [];
		for (var i = 0; i < GRID_SIZE * GRID_SIZE - 1; i++) {
			values.push(i + 1);
		}
		values.push(0); // Empty tile
		// Shuffle tiles (ensuring puzzle is solvable)
		var isSolvable = false;
		while (!isSolvable) {
			shuffleArray(values);
			isSolvable = checkSolvable(values);
		}
		// Create visual tiles
		for (var row = 0; row < GRID_SIZE; row++) {
			tiles[row] = [];
			for (var col = 0; col < GRID_SIZE; col++) {
				var index = row * GRID_SIZE + col;
				var value = values[index];
				if (value === 0) {
					// Empty tile
					emptyPos = {
						row: row,
						col: col
					};
					tiles[row][col] = null;
					continue;
				}
				// Create tile
				var tile = new Container();
				tile.value = value;
				// Position tile in grid
				var x = (col - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				var y = (row - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				tile.x = x;
				tile.y = y;
				// Create tile background
				var tileBackground = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: TILE_SIZE - TILE_MARGIN * 2,
					height: TILE_SIZE - TILE_MARGIN * 2,
					color: 0x4CAF50
				});
				// Create tile number
				var tileText = new Text2(value.toString(), {
					size: TILE_SIZE / 3,
					fill: 0xFFFFFF
				});
				tileText.anchor.set(0.5, 0.5);
				// Add to tile container
				tile.addChild(tileBackground);
				tile.addChild(tileText);
				// Add tile to game
				self.addChild(tile);
				tiles[row][col] = tile;
			}
		}
	}
	// Check if puzzle is solvable
	function checkSolvable(values) {
		var inversions = 0;
		var emptyRow = 0;
		// Find empty tile row from the bottom
		for (var i = 0; i < values.length; i++) {
			if (values[i] === 0) {
				emptyRow = Math.floor(i / GRID_SIZE);
				break;
			}
		}
		// Count inversions
		for (var i = 0; i < values.length - 1; i++) {
			if (values[i] === 0) {
				continue;
			}
			for (var j = i + 1; j < values.length; j++) {
				if (values[j] === 0) {
					continue;
				}
				if (values[i] > values[j]) {
					inversions++;
				}
			}
		}
		// Check solvability conditions
		if (GRID_SIZE % 2 === 1) {
			// Odd grid size - solvable if inversions is even
			return inversions % 2 === 0;
		} else {
			// Even grid size - solvable if:
			// - empty on even row from bottom + odd inversions
			// - empty on odd row from bottom + even inversions
			var emptyRowFromBottom = GRID_SIZE - emptyRow - 1;
			return emptyRowFromBottom % 2 === 0 && inversions % 2 === 1 || emptyRowFromBottom % 2 === 1 && inversions % 2 === 0;
		}
	}
	// Shuffle array (Fisher-Yates algorithm)
	function shuffleArray(array) {
		for (var i = array.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
	}
	// Check if puzzle is solved
	function checkWin() {
		for (var row = 0; row < GRID_SIZE; row++) {
			for (var col = 0; col < GRID_SIZE; col++) {
				var expectedValue = row * GRID_SIZE + col + 1;
				// Skip the last tile (empty)
				if (row === GRID_SIZE - 1 && col === GRID_SIZE - 1) {
					continue;
				}
				// Check if tile is in correct position
				if (!tiles[row][col] || tiles[row][col].value !== expectedValue) {
					return false;
				}
			}
		}
		return true;
	}
	// Determine if a tile can be moved (adjacent to empty)
	function canMoveTile(row, col) {
		return row === emptyPos.row && Math.abs(col - emptyPos.col) === 1 || col === emptyPos.col && Math.abs(row - emptyPos.row) === 1;
	}
	// Move a tile to the empty position
	function moveTile(row, col) {
		if (gameOver || !canMoveTile(row, col)) {
			return;
		}
		// Get the tile
		var tile = tiles[row][col];
		// Calculate new position
		var newX = (emptyPos.col - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
		var newY = (emptyPos.row - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
		// Animate tile movement
		tween(tile, {
			x: newX,
			y: newY
		}, {
			duration: 150
		});
		// Update game state
		tiles[emptyPos.row][emptyPos.col] = tile;
		tiles[row][col] = null;
		// Update empty position
		var oldEmptyPos = {
			row: emptyPos.row,
			col: emptyPos.col
		};
		emptyPos.row = row;
		emptyPos.col = col;
		// Increment move counter
		moves++;
		movesText.setText("Moves: " + moves);
		// Check for win
		if (checkWin()) {
			gameOver = true;
			statusText.setText("Puzzle Solved!");
			finishButton.visible = true;
		}
	}
	// Handle click/touch on the board
	self.down = function (x, y) {
		if (gameOver) {
			return;
		}
		// Convert coordinates to board position
		for (var row = 0; row < GRID_SIZE; row++) {
			for (var col = 0; col < GRID_SIZE; col++) {
				var tileX = (col - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				var tileY = (row - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				var left = tileX - TILE_SIZE / 2;
				var right = tileX + TILE_SIZE / 2;
				var top = tileY - TILE_SIZE / 2;
				var bottom = tileY + TILE_SIZE / 2;
				if (x >= left && x < right && y >= top && y < bottom) {
					if (tiles[row][col]) {
						moveTile(row, col);
					}
				}
			}
		}
	};
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize the game
	initBoard();
	return self;
});
var ResetConfirmation = Container.expand(function () {
	var self = Container.call(this);
	// Background overlay to dim the screen
	var overlay = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0x000000,
		alpha: 0.7
	});
	// Dialog box
	var dialog = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 800,
		height: 400,
		color: 0xffffff
	});
	// Warning text
	var warningText = new Text2("Are you sure you want to reset your progress?", {
		size: 50,
		fill: 0x000000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	warningText.anchor.set(0.5, 0.5);
	warningText.y = -80;
	self.addChild(warningText);
	// Yes button
	var yesButton = new Button("Yes", 200, 80, 0xff0000);
	yesButton.x = -150;
	yesButton.y = 80;
	self.addChild(yesButton);
	// No button
	var noButton = new Button("No", 200, 80, 0x4CAF50);
	noButton.x = 150;
	noButton.y = 80;
	self.addChild(noButton);
	self.setCallbacks = function (onYes, onNo) {
		yesButton.up = function () {
			tween(yesButton.children[0], {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (onYes) {
				onYes();
			}
			self.visible = false; // Hide dialog
		};
		noButton.up = function () {
			tween(noButton.children[0], {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (onNo) {
				onNo();
			}
			self.visible = false; // Hide dialog
		};
	};
	return self;
});
var SpinWheel = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var WHEEL_RADIUS = 400;
	var NUM_SEGMENTS = 8;
	var SPIN_DURATION = 5000;
	var PRIZES = [{
		name: "100 coins",
		value: 100,
		color: 0xf44336,
		icon: "๐ฐ"
	}, {
		name: "200 coins",
		value: 200,
		color: 0x2196f3,
		icon: "๐ฐ"
	}, {
		name: "50 followers",
		value: 50,
		color: 0x4caf50,
		isFollowers: true,
		icon: "๐ฅ"
	}, {
		name: "500 coins",
		value: 500,
		color: 0xffeb3b,
		icon: "๐ฐ"
	}, {
		name: "25 followers",
		value: 25,
		color: 0xff9800,
		isFollowers: true,
		icon: "๐ฅ"
	}, {
		name: "150 coins",
		value: 150,
		color: 0x9c27b0,
		icon: "๐ฐ"
	}, {
		name: "300 coins",
		value: 300,
		color: 0x00bcd4,
		icon: "๐ฐ"
	}, {
		name: "75 followers",
		value: 75,
		color: 0x8bc34a,
		isFollowers: true,
		icon: "๐ฅ"
	}];
	// Wheel outer ring
	var wheelRim = self.attachAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: WHEEL_RADIUS * 2 + 20,
		height: WHEEL_RADIUS * 2 + 20,
		color: 0x222222
	});
	// Wheel background - use actual wheel asset
	var wheelBackground = self.attachAsset('Wheel', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: WHEEL_RADIUS * 2,
		height: WHEEL_RADIUS * 2
	});
	// Create wheel segments container
	var wheelSegmentsContainer = new Container();
	self.addChild(wheelSegmentsContainer);
	// Create wheel segments
	var segments = [];
	for (var i = 0; i < NUM_SEGMENTS; i++) {
		var segment = new Container();
		var angle = i / NUM_SEGMENTS * Math.PI * 2;
		// Calculate segment positions
		var segmentAngle = Math.PI * 2 / NUM_SEGMENTS;
		// Position the segment container
		segment.rotation = angle;
		// Prize icon removed
		// Segment text removed
		// Value text removed
		wheelSegmentsContainer.addChild(segment);
		segments.push(segment);
	}
	// Add decorative outer dots
	for (var i = 0; i < NUM_SEGMENTS * 2; i++) {
		var dotAngle = i / (NUM_SEGMENTS * 2) * Math.PI * 2;
		var dot = LK.getAsset('centerCircle', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 15,
			height: 15,
			color: 0xFFD700 // Gold dots
		});
		dot.x = Math.cos(dotAngle) * (WHEEL_RADIUS + 10);
		dot.y = Math.sin(dotAngle) * (WHEEL_RADIUS + 10);
		self.addChild(dot);
	}
	// Create center point with decorative rings
	var centerPointOuter = LK.getAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 80,
		height: 80,
		color: 0xFFD700 // Gold
	});
	self.addChild(centerPointOuter);
	var centerPoint = LK.getAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 60,
		height: 60,
		color: 0xFF5733 // Orange-red
	});
	self.addChild(centerPoint);
	// Add pointer (indicator)
	var pointerBase = LK.getAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 60,
		height: 60,
		color: 0xFF0000
	});
	pointerBase.y = -WHEEL_RADIUS - 20;
	self.addChild(pointerBase);
	var pointer = LK.getAsset('Girrafe', {
		anchorX: 0.5,
		anchorY: 0,
		width: 40,
		height: 80,
		color: 0xFF0000
	});
	pointer.y = -WHEEL_RADIUS - 50;
	self.addChild(pointer);
	// Add close button
	var closeButton = new Button("Close", 200, 80, 0xff5555);
	closeButton.y = WHEEL_RADIUS + 100;
	self.addChild(closeButton);
	// Add result text (initially hidden)
	var resultText = new Text2("", {
		size: 60,
		fill: 0xFFD700
	});
	resultText.anchor.set(0.5, 0.5);
	resultText.y = WHEEL_RADIUS + 180;
	resultText.alpha = 0;
	self.addChild(resultText);
	// Spinning state
	var isSpinning = false;
	// Spin the wheel
	self.spin = function (callback) {
		if (isSpinning) {
			return;
		}
		isSpinning = true;
		// Reset result text
		resultText.alpha = 0;
		// Play the click sound for the spin action initiation (if it's distinct from button press)
		LK.getSound('click').play(); //{gT} // This was the original second click sound, kept for fidelity
		// Play spinning sound as looping music
		LK.playMusic('SpinLoopMusic', {
			loop: true
		});
		// Determine winning segment (random)
		var targetSegment = Math.floor(Math.random() * NUM_SEGMENTS);
		var targetAngle = targetSegment / NUM_SEGMENTS * Math.PI * 2;
		// Always use the same fast speed and number of rotations
		var extraRotations = 8; // Always 8 full rotations for fast consistent speed
		var finalRotation = extraRotations * Math.PI * 2 + targetAngle;
		// Animate the wheel spinning (target the wheelBackground now)
		tween(wheelBackground, {
			rotation: finalRotation
		}, {
			duration: 2200,
			// Fast spin: 2.2 seconds
			easing: tween.easeOutCubic,
			onFinish: function onFinish() {
				isSpinning = false;
				// Show result
				var prize = PRIZES[targetSegment];
				resultText.setText("You won: " + prize.name + "!");
				// Flash the pointer
				tween(pointerBase, {
					scaleX: 1.5,
					scaleY: 1.5,
					alpha: 0.7
				}, {
					duration: 200,
					yoyo: true,
					repeat: 3
				});
				// Fade in result text
				tween(resultText, {
					alpha: 1
				}, {
					duration: 500
				});
				// Stop the spin music and resume background music
				LK.stopMusic(); // Stops SpinLoopMusic
				LK.playMusic('bgMusic', {
					fade: {
						start: 0,
						end: 0.4,
						duration: 1000
					}
				}); // Resume background music
				// Play reward sound
				LK.getSound('coin').play();
				// Callback with prize
				if (callback) {
					callback(prize);
				}
			}
		});
	};
	// Set close button callback
	self.setCloseCallback = function (callback) {
		closeButton.up = function () {
			tween(closeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	return self;
});
var TabSystem = Container.expand(function (tabs) {
	var self = Container.call(this);
	var tabButtons = [];
	var tabContents = [];
	var activeTab = 0;
	// Create tab buttons
	for (var i = 0; i < tabs.length; i++) {
		var tab = tabs[i];
		var index = i;
		var tabButton = new Button(tab.name, 240, 80, 0x607D8B);
		tabButton.x = i * 320 - (tabs.length - 1) * 160;
		tabButton.y = -1300 / 2 + 30;
		tabButton.up = function (x, y, obj) {
			tween(tabButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			setActiveTab(index);
		};
		self.addChild(tabButton);
		tabButtons.push(tabButton);
		// Create and add content container
		var content = tab.content;
		content.visible = i === 0; // Only first tab visible initially
		self.addChild(content);
		tabContents.push(content);
	}
	function setActiveTab(index) {
		if (index === activeTab) {
			return;
		}
		// Update button colors
		tabButtons[activeTab].setColor(0x607D8B);
		tabButtons[index].setColor(0x2196F3);
		// Hide old content, show new
		tabContents[activeTab].visible = false;
		tabContents[index].visible = true;
		activeTab = index;
	}
	// Initialize first tab as active
	setActiveTab(0);
	return self;
});
var UpgradeButton = Container.expand(function (title, description, price, level, onUpgrade) {
	var self = Container.call(this);
	var buttonShape = self.attachAsset('upgradeButton', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 420,
		height: 140
	});
	var titleText = new Text2(title, {
		size: 42,
		fill: 0xFFFFFF
	});
	titleText.anchor.set(0.5, 0);
	titleText.y = -buttonShape.height / 2 + 15;
	self.addChild(titleText);
	var descText = new Text2(description, {
		size: 30,
		fill: 0xFFFFFF
	});
	descText.anchor.set(0.5, 0);
	descText.y = -buttonShape.height / 2 + 50;
	self.addChild(descText);
	var levelText = new Text2("Level: " + level, {
		size: 34,
		fill: 0xFFD700
	});
	levelText.anchor.set(0, 1);
	levelText.x = -buttonShape.width / 2 + 15;
	levelText.y = buttonShape.height / 2 - 10;
	self.addChild(levelText);
	var priceText = new Text2("$" + price, {
		size: 36,
		fill: 0xFFD700
	});
	priceText.anchor.set(1, 1);
	priceText.x = buttonShape.width / 2 - 15;
	priceText.y = buttonShape.height / 2 - 10;
	self.addChild(priceText);
	self.updateLevel = function (newLevel) {
		levelText.setText("Level: " + newLevel);
	};
	self.updatePrice = function (newPrice) {
		priceText.setText("$" + newPrice);
	};
	self.disable = function () {
		buttonShape.tint = 0x888888;
		self.interactive = false;
	};
	self.enable = function () {
		buttonShape.tint = 0x2196F3;
		self.interactive = true;
	};
	self.down = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100
		});
		LK.getSound('click').play();
	};
	self.up = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
		if (onUpgrade) {
			onUpgrade();
		}
	};
	return self;
});
var UpgradePanel = Container.expand(function () {
	var self = Container.call(this);
	var background = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 1800,
		height: 1300,
		color: 0xdddddd
	});
	var titleText = new Text2("Upgrade Your Channel", {
		size: 64,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0);
	titleText.y = -background.height / 2 + 50;
	self.addChild(titleText);
	var closeButton = new Button("Close", 240, 80, 0xff5555);
	closeButton.x = background.width / 2 - 130;
	closeButton.y = -background.height / 2 + 50;
	self.addChild(closeButton);
	// Add next button
	var nextButton = new Button("Next", 240, 80, 0x2196F3);
	nextButton.x = -background.width / 2 + 130;
	nextButton.y = -background.height / 2 + 50;
	self.addChild(nextButton);
	self.setCloseCallback = function (callback) {
		closeButton.up = function (x, y, obj) {
			tween(closeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.setNextCallback = function (callback) {
		nextButton.up = function (x, y, obj) {
			tween(nextButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.addUpgradeButton = function (title, description, price, level, maxLevel, x, y, onUpgrade) {
		var button = new UpgradeButton(title, description, price, level, function () {
			if (onUpgrade) {
				onUpgrade(button);
			}
		});
		// Make upgrade buttons bigger
		button.scale.set(1.4, 1.4);
		button.x = x;
		button.y = y;
		if (maxLevel !== undefined && level >= maxLevel) {
			button.updatePrice("MAXED");
			button.disable();
		}
		self.addChild(button);
		return button;
	};
	return self;
});
var ViralMemeDialog = Container.expand(function () {
	var self = Container.call(this);
	// Background overlay
	var overlay = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0x000000,
		alpha: 0.8
	});
	// Dialog box
	var dialog = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 800,
		height: 500,
		color: 0xffffff
	});
	// Viral meme text
	var viralText = new Text2("YOUR MEME WENT VIRAL!!!", {
		size: 60,
		fill: 0x00ff00,
		// Green color
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	viralText.anchor.set(0.5, 0.5);
	viralText.y = -80;
	self.addChild(viralText);
	// Earnings text
	var earningsText = new Text2("", {
		size: 50,
		fill: 0x000000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	earningsText.anchor.set(0.5, 0.5);
	earningsText.y = 50;
	self.addChild(earningsText);
	// OK button
	var okButton = new Button("OK", 300, 100, 0x4CAF50);
	okButton.y = 180;
	self.addChild(okButton);
	self.setCloseCallback = function (callback) {
		okButton.up = function () {
			tween(okButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (callback) {
				callback();
			}
			self.visible = false;
		};
	};
	self.showResult = function (followersGained, moneyGained) {
		earningsText.setText("You earned " + followersGained + " followers and $" + moneyGained + "!");
		self.visible = true;
	};
	return self;
});
var WheelScreen = Container.expand(function () {
	var self = Container.call(this);
	// Add background
	var background = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0xdddddd
	});
	// Add title
	var titleText = new Text2("Spin the Wheel for Prizes!", {
		size: 80,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0.5);
	titleText.y = -900;
	self.addChild(titleText);
	// Create spin wheel
	var spinWheel = new SpinWheel();
	spinWheel.y = 0; // Center of screen
	self.addChild(spinWheel);
	// Create spin button
	// Create spin button
	var spinButton = new Button("SPIN!", 300, 100, 0x4CAF50);
	spinButton.y = 600;
	self.addChild(spinButton);
	// Add cost text below the spin button
	var costText = new Text2("Cost: 50 Followers", {
		size: 40,
		fill: 0xFF5733 // Orange-red color for emphasis
	});
	costText.anchor.set(0.5, 0.5);
	costText.y = spinButton.y + spinButton.height / 2 + 30; // Position below the button
	self.addChild(costText);
	// Set spin button callback
	spinButton.up = function () {
		// Reset button scale first
		tween(spinButton, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
		// Check if player has enough followers
		if (followers >= 50) {
			// Deduct followers
			followers -= 50;
			updateStorage(); // Update stored follower count
			// Disable spin button during spin
			spinButton.visible = false;
			// Spin the wheel
			spinWheel.spin(function (prize) {
				// Process the prize
				if (self.onPrizeWon) {
					self.onPrizeWon(prize);
				}
				// Re-enable spin button after result is shown and delay
				LK.setTimeout(function () {
					spinButton.visible = true;
				}, 1500); // Shortened delay slightly as spin ends sooner now
			});
		} else {
			// Not enough followers, flash the cost text
			LK.effects.flashObject(costText, 0xff0000, 500); // Flash red
			// Optionally play a 'fail' sound
			// LK.getSound('fail').play();
		}
	};
	// Store callback for prize handling
	self.setPrizeCallback = function (callback) {
		self.onPrizeWon = callback;
	};
	// Set close callback
	self.setCloseCallback = function (callback) {
		spinWheel.setCloseCallback(callback);
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
// Added for looping spin sound
// Game variables
if (!tween.stopTweensOf) {
	tween.stopTweensOf = function (target) {
		// Simple implementation of stopTweensOf if not provided by the tween library
		if (target && target._tweens) {
			target._tweens.forEach(function (t) {
				t.stop();
			});
			target._tweens = [];
		}
	};
}
var money = storage.money || 100;
var followers = storage.followers || 0;
var memeLevel = storage.memeLevel || 1;
var studioLevel = storage.studioLevel || 1;
var staffLevel = storage.staffLevel || 0;
var marketingLevel = storage.marketingLevel || 0;
var autoEarningRate = storage.autoEarningRate || 0;
var lastPlayed = storage.lastPlayed || Date.now();
var membershipLevel = storage.membershipLevel || 0;
var equipmentLevel = storage.equipmentLevel || 1;
var rentTimeLevel = storage.rentTimeLevel || 0; // Initialize rentTimeLevel
var gems = storage.gems || 0; // Initialize gems
// Upgrade prices
var upgradePrices = {
	quality: parseInt(calculateUpgradeCost(1, memeLevel)) || 100,
	membership: 500,
	staff: parseInt(calculateUpgradeCost(3, staffLevel)) || 300,
	equipment: parseInt(calculateUpgradeCost(2, studioLevel)) || 200
};
// Function to start Puzzle game
function startPuzzleGame() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new PuzzleGame or reset existing one
	if (!puzzleGame) {
		puzzleGame = new PuzzleGame();
		puzzleGame.x = 2048 / 2;
		puzzleGame.y = 2732 / 2;
		game.addChild(puzzleGame);
	} else {
		// Remove existing game and create a new one
		game.removeChild(puzzleGame);
		puzzleGame = new PuzzleGame();
		puzzleGame.x = 2048 / 2;
		puzzleGame.y = 2732 / 2;
		game.addChild(puzzleGame);
	}
	// Show the game
	puzzleGame.visible = true;
	// Set callback for when game finishes
	puzzleGame.setFinishCallback(function () {
		// Hide Puzzle game
		puzzleGame.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards for completing video game - puzzle games give balanced rewards
		var videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 6.4; // Doubled from 3.2 to 6.4
		var videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 3.5;
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play coin sound
		LK.getSound('coin').play();
		gems += 1; // Award 1 gem for winning
		// Update storage
		updateStorage();
	});
	// Set callback for return home button
	puzzleGame.setReturnHomeCallback(function () {
		// Hide Puzzle game
		puzzleGame.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
}
// Upgrade levels
var upgradeLevels = {
	quality: memeLevel,
	membership: membershipLevel,
	staff: staffLevel,
	equipment: studioLevel
};
// Constants
var MEME_BASE_INCOME = 10;
var MEME_BASE_FOLLOWERS = 5;
var UPGRADE_BASE_COST = 100;
var AUTO_EARNINGS_INTERVAL = 20000; // ms (20 seconds)
var OFFLINE_EARNINGS_MAX_HOURS = 24;
// Calculate offline earnings
function calculateOfflineEarnings() {
	var now = Date.now();
	var timeDiff = now - lastPlayed;
	var secondsDiff = timeDiff / 1000;
	var periods = secondsDiff / 20; // How many 20-second periods elapsed
	// Cap at max hours (converted to 20-second periods)
	var maxPeriods = OFFLINE_EARNINGS_MAX_HOURS * 60 * 60 / 20;
	if (periods > maxPeriods) {
		periods = maxPeriods;
	}
	// Only calculate if we have auto earnings
	if (autoEarningRate > 0 && periods > 0) {
		var hourlyRate = autoEarningRate;
		var periodRate = hourlyRate / 180; // Convert from hourly to per 20-second period
		var earnings = Math.floor(periodRate * periods);
		money += earnings;
		// Show welcome back message here if needed
		console.log("Welcome back! You earned $" + earnings + " while away.");
	}
	lastPlayed = now;
	storage.lastPlayed = now;
}
// Calculate costs for upgrades
function calculateUpgradeCost(baseLevel, currentLevel) {
	return Math.floor(UPGRADE_BASE_COST * baseLevel * Math.pow(1.5, currentLevel));
}
// Update storage
function updateStorage() {
	storage.money = money;
	storage.followers = followers;
	storage.memeLevel = memeLevel;
	storage.studioLevel = studioLevel;
	storage.staffLevel = staffLevel;
	storage.marketingLevel = marketingLevel;
	storage.autoEarningRate = autoEarningRate;
	storage.membershipLevel = membershipLevel;
	storage.rentTimeLevel = rentTimeLevel; // Add rentTimeLevel to storage
	storage.gems = gems; // Add gems to storage
}
// Calculate meme creation result
function createMeme() {
	// Base earnings affected by meme quality and studio level
	var earnings = MEME_BASE_INCOME * memeLevel * (1 + studioLevel * 0.5);
	// Followers gained affected by marketing level
	var newFollowers = Math.floor(MEME_BASE_FOLLOWERS * (1 + marketingLevel * 0.5));
	// Boost from followers (virality factor)
	var followerBoost = 1 + followers / 1000;
	earnings = Math.floor(earnings * followerBoost);
	// Apply membership bonus if active
	if (membershipLevel > 0) {
		earnings = Math.floor(earnings * 1.5);
	}
	// Apply randomness for variability (80% to 120% of calculated value)
	var randomFactor = 0.8 + Math.random() * 0.4;
	earnings = Math.floor(earnings * randomFactor);
	// Small chance for meme to go viral
	if (Math.random() < 0.05) {
		earnings *= 5;
		newFollowers *= 3;
		console.log("Your meme went viral!");
		// Show viral meme dialog
		viralMemeDialog.showResult(newFollowers, earnings);
		// Trigger confetti burst
		for (var i = 0; i < 100; i++) {
			// Create 100 confetti particles
			var confetti = new ConfettiParticle();
			confetti.x = 2048 / 2; // Start from the top center
			confetti.y = 0;
			game.addChild(confetti);
			confettiParticles.push(confetti);
		}
		// Skip the rest of the logic for this case
		return {
			earnings: earnings,
			followers: newFollowers
		};
	}
	// 2% chance of a controversial warning
	if (Math.random() < 0.02) {
		console.log("Controversial meme!");
		var percentageLost = 0.05; // Fixed 5% loss
		var lostFollowers = Math.floor(followers * percentageLost); // Calculate followers lost based on current followers
		newFollowers = -lostFollowers; // Update newFollowers to reflect the loss
		controversialWarningDialog.visible = true;
		controversialWarningDialog.children[2].setText("You uploaded a meme that your fans didn't like :(" + "\n" + "You lost " + Math.abs(newFollowers) + " followers!"); // Update warning text
	} else if (membershipLevel === 1 && Math.random() < 0.1) {
		// 10% chance for donations if upgrade is purchased
		var donationAmount = Math.floor(Math.random() * 100) + 50; // Random donation between $50 and $150
		earnings += donationAmount;
		console.log("You received a donation of $" + donationAmount + "!");
		// Show donation notification
		var notification = new DonationNotification();
		notification.setAmount(donationAmount);
		notification.y = 400; // Position on side of screen
		game.addChild(notification);
		notification.animate();
	} else {
		// Apply randomness for variability (80% to 120% of calculated value)
		var randomFactor = 0.8 + Math.random() * 0.4;
		earnings = Math.floor(earnings * randomFactor);
	}
	// Update game state
	money += earnings;
	followers += newFollowers;
	// Update storage
	updateStorage();
	// Return results
	return {
		earnings: earnings,
		followers: newFollowers
	};
}
// Update auto earnings rate based on staff
function updateAutoEarningRate() {
	autoEarningRate = staffLevel * 250 * memeLevel * studioLevel; // MASSIVELY increased earning rate
	storage.autoEarningRate = autoEarningRate;
}
// Create game selector screen (initially hidden)
var gameSelector = new GameSelector();
gameSelector.x = 2048 / 2;
gameSelector.y = 2732 / 2;
gameSelector.visible = false;
game.addChild(gameSelector);
// Function to show game selector
function showGameSelector() {
	gameSelector.visible = true;
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Update matching game lock status based on current follower count
	gameSelector.updateLockStatus(followers);
}
// Function to hide game selector and show the main screen
function hideGameSelector() {
	gameSelector.visible = false;
	memeStudio.visible = true;
	statsContainer.visible = true;
	studioContent.visible = true;
}
// Set callbacks for game selector
gameSelector.setGameCallback(function (gameType) {
	if (gameType === "connect4") {
		startConnect4Game();
	} else if (gameType === "matching") {
		startMatchingGame();
	} else if (gameType === "puzzle") {
		startPuzzleGame();
	} else if (gameType === "cupShuffle") {
		startCupShuffleGame();
	}
});
gameSelector.setBackCallback(function () {
	hideGameSelector();
});
// CupShuffle game instance (initially null)
var cupShuffleGame = null;
// Function to start CupShuffle game
function startCupShuffleGame() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new CupShuffle game or reset existing one
	if (!cupShuffleGame) {
		cupShuffleGame = new CupShuffleGame();
		cupShuffleGame.x = 2048 / 2;
		cupShuffleGame.y = 2732 / 2;
		game.addChild(cupShuffleGame);
	} else {
		// Remove existing game and create a new one
		game.removeChild(cupShuffleGame);
		cupShuffleGame = new CupShuffleGame();
		cupShuffleGame.x = 2048 / 2;
		cupShuffleGame.y = 2732 / 2;
		game.addChild(cupShuffleGame);
	}
	// Show the game
	cupShuffleGame.visible = true;
	// Set callback for return home button
	cupShuffleGame.setReturnHomeCallback(function () {
		// Hide CupShuffle game
		cupShuffleGame.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
	// Set callback for when game finishes
	cupShuffleGame.setFinishCallback(function (result) {
		// Hide CupShuffle game
		cupShuffleGame.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards based on game result
		var videoEarnings = 0;
		var videoFollowers = 0;
		var gemsAwarded = 0;
		if (result === 'win') {
			videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 8; // High earnings for winning
			videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 5; // High followers for winning
			gemsAwarded = 2; // Award 2 gems for winning
		} else {
			videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 2; // Lower earnings for losing
			videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 1; // Lower followers for losing
			gemsAwarded = 0; // No gems for losing
		}
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		gems += gemsAwarded;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play sound
		if (result === 'win') {
			LK.getSound('coin').play(); // Play coin sound for winning
		} else {
			// Optionally play a different sound for losing
			// LK.getSound('lose').play();
		}
		// Update storage
		updateStorage();
	});
}
// Connect4 game instance (initially null)
var connect4Game = null;
// Function to start Connect4 game
function startConnect4Game() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new Connect4 game or reset existing one
	if (!connect4Game) {
		connect4Game = new Connect4Game();
		connect4Game.x = 2048 / 2;
		connect4Game.y = 2732 / 2;
		game.addChild(connect4Game);
	} else {
		// Remove existing game and create a new one
		game.removeChild(connect4Game);
		connect4Game = new Connect4Game();
		connect4Game.x = 2048 / 2;
		connect4Game.y = 2732 / 2;
		game.addChild(connect4Game);
	}
	// Show the game
	connect4Game.visible = true;
	// Set callback for return home button
	connect4Game.setReturnHomeCallback(function () {
		// Hide Connect4 game
		connect4Game.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
	// Set callback for when game finishes
	connect4Game.setFinishCallback(function () {
		// Hide Connect4 game
		connect4Game.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards for completing video game
		var videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 6; // Doubled from 3 to 6
		var videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 3;
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play coin sound
		LK.getSound('coin').play();
		gems += 1; // Award 1 gem for winning
		// Update storage
		updateStorage();
	});
}
// Create UI elements
// Main studio
// Create rent button using the rent asset
var rentButton = new Container();
var rentButtonAsset = rentButton.attachAsset('Rent', {
	anchorX: 0.5,
	anchorY: 0.5,
	width: 600,
	height: 180
});
var rentButtonText = new Text2("Pay Rent: $200", {
	size: 54,
	fill: 0xFFFFFF
});
rentButtonText.anchor.set(0.5, 0.5);
rentButton.addChild(rentButtonText);
rentButton.x = 2048 / 2;
rentButton.y = 300;
// Create rent bar under the rent button
var rentBar = new Container();
var rentBarAsset = rentBar.attachAsset('Girrafe', {
	anchorX: 0.5,
	anchorY: 0.5,
	width: 600,
	height: 100
});
rentBar.x = 2048 / 2;
rentBar.y = 380;
game.addChild(rentBar);
// Animate the rent bar slowly draining to the left
tween(rentBarAsset, {
	scaleX: 0,
	x: -300
}, {
	duration: 60000,
	// 60 seconds (one minute) duration for slow drain
	easing: tween.linear,
	onFinish: function onFinish() {
		// When rent bar runs out, lose half the money
		money = Math.floor(money / 2);
		// Reset the rent bar after losing money
		tween.stopTweensOf(rentBarAsset);
		rentBarAsset.scaleX = 1;
		rentBarAsset.x = 0;
		// Calculate the next duration based on the upgraded rent time
		var nextDuration = 60000 + rentTimeLevel * 10000;
		// Start draining again
		tween(rentBarAsset, {
			scaleX: 0,
			x: -300
		}, {
			duration: nextDuration,
			easing: tween.linear,
			onFinish: arguments.callee
		});
		// Update storage after losing money
		updateStorage();
		// Play sound to indicate rent is due
		LK.getSound('click').play();
	}
});
// Calculate the initial rent duration based on the stored rentTimeLevel
var initialRentDuration = 60000 + rentTimeLevel * 10000;
// Start the initial rent bar tween
tween(rentBarAsset, {
	scaleX: 0,
	x: -300
}, {
	duration: initialRentDuration,
	easing: tween.linear,
	onFinish: rentBar.onFinish // Reference the onFinish function defined above
});
rentButton.down = function (x, y, obj) {
	tween(rentButtonAsset, {
		scaleX: 0.95,
		scaleY: 0.95
	}, {
		duration: 100
	});
	LK.getSound('click').play();
};
rentButton.up = function (x, y, obj) {
	tween(rentButtonAsset, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 100
	});
	if (money >= 200) {
		money -= 200;
		LK.getSound('coin').play();
		tween(rentButton, {
			y: 280,
			alpha: 0.5
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(rentButton, {
					y: 300,
					alpha: 1
				}, {
					duration: 300
				});
			}
		});
		// Reset the rent bar by creating a new tween
		// Stop any existing tween on the rent bar
		tween.stopTweensOf(rentBarAsset);
		// Reset the bar's scale and position
		rentBarAsset.scaleX = 1;
		rentBarAsset.x = 0;
		// Start a new tween to drain it again
		tween(rentBarAsset, {
			scaleX: 0,
			x: -300
		}, {
			duration: 60000,
			// 60 seconds (one minute) duration for slow drain
			easing: tween.linear
		});
		updateStorage();
	}
};
game.addChild(rentButton);
var memeStudio = new MemeStudio();
memeStudio.x = 2048 / 2;
memeStudio.y = 2732 / 2;
game.addChild(memeStudio);
// Initialize with current meme quality
memeStudio.updateMemeQuality(memeLevel);
// Reset Progress button
var resetButton = new Button("Reset Progress", 300, 100, 0xff5555);
resetButton.x = 200; // Position in bottom left
resetButton.y = 2732 - 100; // Position near bottom
resetButton.up = function (x, y, obj) {
	tween(resetButton, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 100
	});
	// Show the confirmation dialog instead of resetting immediately
	resetConfirmationDialog.visible = true;
};
game.addChild(resetButton);
// Stats display
// Create reset confirmation dialog (initially hidden)
var resetConfirmationDialog = new ResetConfirmation();
resetConfirmationDialog.x = 2048 / 2;
resetConfirmationDialog.y = 2732 / 2;
resetConfirmationDialog.visible = false;
game.addChild(resetConfirmationDialog);
// Set callbacks for the confirmation dialog
resetConfirmationDialog.setCallbacks(function onYes() {
	// This code runs if the player confirms the reset
	// Reset all progress
	money = 100;
	followers = 0;
	memeLevel = 1;
	studioLevel = 1;
	staffLevel = 0;
	marketingLevel = 0;
	autoEarningRate = 0;
	membershipLevel = 0;
	equipmentLevel = 1; // Reset equipment level as well
	rentTimeLevel = 0; // Reset rent time level
	gems = 0; // Reset gems
	// Reset upgrade levels
	upgradeLevels.quality = memeLevel;
	upgradeLevels.membership = membershipLevel;
	upgradeLevels.staff = staffLevel;
	upgradeLevels.equipment = studioLevel;
	// Reset upgrade prices
	upgradePrices.quality = calculateUpgradeCost(1, memeLevel);
	upgradePrices.membership = 500;
	upgradePrices.staff = calculateUpgradeCost(3, staffLevel);
	upgradePrices.equipment = calculateUpgradeCost(2, studioLevel);
	// Update storage
	updateStorage();
	// Update UI
	memeStudio.updateMemeQuality(memeLevel);
	// Play sound
	LK.getSound('levelUp').play();
}, function onNo() {
	// This code runs if the player cancels the reset
	console.log("Reset cancelled.");
});
// Stats display
var statsContainer = new Container();
statsContainer.x = 2048 / 2;
statsContainer.y = 180;
game.addChild(statsContainer);
var moneyIcon = LK.getAsset('moneyIcon', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: -600
});
statsContainer.addChild(moneyIcon);
var moneyText = new Text2("$" + money, {
	size: 54,
	fill: 0x000000
});
moneyText.anchor.set(0, 0.5);
moneyText.x = -570;
statsContainer.addChild(moneyText);
var followerIcon = LK.getAsset('followerIcon', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 400
});
statsContainer.addChild(followerIcon);
var followerText = new Text2(followers + " followers", {
	size: 54,
	fill: 0x000000
});
followerText.anchor.set(0, 0.5);
followerText.x = 430;
statsContainer.addChild(followerText);
var gemsIcon = LK.getAsset('Gems', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: -100,
	width: 80,
	// Reduced width
	height: 80 // Reduced height
});
statsContainer.addChild(gemsIcon);
var gemsText = new Text2(gems + " gems", {
	size: 54,
	fill: 0x000000
});
gemsText.anchor.set(0, 0.5);
gemsText.x = -70;
statsContainer.addChild(gemsText);
var autoEarningText = new Text2("", {
	size: 36,
	fill: 0x333333
});
autoEarningText.anchor.set(0.5, 0.5);
autoEarningText.y = 80;
statsContainer.addChild(autoEarningText);
// Create upgrade tab contents
var studioContent = new Container();
// Meme Quality Upgrade button removed
// No studio equipment upgrade
// Staff Upgrade removed
// Studio content is displayed directly without tabs
studioContent.x = 2048 / 2;
studioContent.y = 2732 / 2 + 400;
game.addChild(studioContent);
// Set callback for create meme button
memeStudio.setCreateButtonCallback(function () {
	var result = createMeme();
	memeStudio.showEarnings(result.earnings, result.followers);
	LK.getSound('coin').play();
});
// Create upgrade panel (initially hidden)
var upgradePanel = new UpgradePanel();
upgradePanel.x = 2048 / 2;
upgradePanel.y = 2732 / 2;
upgradePanel.visible = false;
game.addChild(upgradePanel);
// Function to show upgrade panel
function showUpgradePanel() {
	upgradePanel.visible = true;
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Add meme quality upgrade button
	var memeQualityUpgrade = upgradePanel.addUpgradeButton("Meme Quality", "Better memes = more $$$", upgradePrices.quality, upgradeLevels.quality, 10,
	// Max level
	0,
	// X position
	-300,
	// Y position
	function (button) {
		if (money >= upgradePrices.quality) {
			// Pay for upgrade
			money -= upgradePrices.quality;
			// Increase level
			memeLevel++;
			upgradeLevels.quality = memeLevel;
			// Update studio
			memeStudio.updateMemeQuality(memeLevel);
			// Update price
			upgradePrices.quality = calculateUpgradeCost(1, memeLevel);
			button.updateLevel(memeLevel);
			button.updatePrice(upgradePrices.quality);
			// Check if max level reached
			if (memeLevel >= 10) {
				button.updatePrice("MAXED");
				button.disable();
			}
			// Play level up sound
			LK.getSound('levelUp').play();
			// Update storage
			updateStorage();
		}
	});
	// Add staff upgrade button
	var staffUpgradeButton = upgradePanel.addUpgradeButton("Hire Staff", "Staff generates income", upgradePrices.staff, upgradeLevels.staff, 10,
	// Max level
	0,
	// X position
	0,
	// Y position
	function (button) {
		if (money >= upgradePrices.staff) {
			// Pay for upgrade
			money -= upgradePrices.staff;
			// Increase level
			staffLevel++;
			upgradeLevels.staff = staffLevel;
			// Update auto earning rate
			updateAutoEarningRate();
			// Update price
			upgradePrices.staff = calculateUpgradeCost(3, staffLevel);
			button.updateLevel(staffLevel);
			button.updatePrice(upgradePrices.staff);
			// Check if max level reached
			if (staffLevel >= 10) {
				button.updatePrice("MAXED");
				button.disable();
			}
			// Play level up sound
			LK.getSound('levelUp').play();
			// Update storage
			updateStorage();
		}
	});
	// Add Increase Rent Time upgrade button
	var rentTimeUpgradeButton = upgradePanel.addUpgradeButton("Increase Rent Time", "Increases time before rent is due", calculateUpgradeCost(4, rentTimeLevel), rentTimeLevel, 10,
	// Max level
	0,
	// X position
	300,
	// Y position
	function (button) {
		if (money >= calculateUpgradeCost(4, rentTimeLevel)) {
			money -= calculateUpgradeCost(4, rentTimeLevel);
			rentTimeLevel++;
			// Stop the current rent bar tween
			tween.stopTweensOf(rentBarAsset);
			// Reset and restart the rent bar tween with increased duration
			var newDuration = 60000 + rentTimeLevel * 10000; // Increase duration by 10 seconds per level
			rentBarAsset.scaleX = 1;
			rentBarAsset.x = 0;
			tween(rentBarAsset, {
				scaleX: 0,
				x: -300
			}, {
				duration: newDuration,
				easing: tween.linear,
				onFinish: arguments.callee
			});
			button.updateLevel(rentTimeLevel);
			button.updatePrice(calculateUpgradeCost(4, rentTimeLevel));
			if (rentTimeLevel >= 10) {
				button.updatePrice("MAXED");
				button.disable();
			}
			LK.getSound('levelUp').play();
			updateStorage();
		}
	});
	// Set up next callback for upgrade panel
	upgradePanel.setNextCallback(function () {
		hideUpgradePanel(); // Hide current panel
		showUpgradePanel2(); // Show the next panel
	});
}
// Function to hide upgrade panel
function hideUpgradePanel() {
	upgradePanel.visible = false;
	memeStudio.visible = true;
	statsContainer.visible = true;
	studioContent.visible = true;
}
// Set up close callback for upgrade panel
upgradePanel.setCloseCallback(function () {
	hideUpgradePanel();
});
// Set callback for create video button
memeStudio.setCreateVideoButtonCallback(function () {
	showGameSelector();
});
// Set callback for upgrades button
memeStudio.setUpgradesButtonCallback(function () {
	showUpgradePanel();
});
// Create a second upgrade panel (initially hidden)
var upgradePanel2 = new UpgradePanel();
upgradePanel2.x = 2048 / 2;
upgradePanel2.y = 2732 / 2;
upgradePanel2.visible = false;
game.addChild(upgradePanel2);
// Function to show the second upgrade panel
function showUpgradePanel2() {
	upgradePanel2.visible = true;
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Add specific upgrades for the second panel here
	// Add Donations upgrade button
	var donationsUpgradeButton = upgradePanel2.addUpgradeButton("Donations", "Chance for viewers to donate money (Costs 2 Diamonds)", 2,
	// Cost in gems
	membershipLevel,
	// Reusing membershipLevel for tracking if purchased (0 or 1)
	1,
	// Max level is 1 (can only buy once)
	0,
	// X position
	-300,
	// Y position
	function (button) {
		if (gems >= 2 && membershipLevel === 0) {
			// Check if player has enough gems and hasn't bought it
			// Pay for upgrade
			gems -= 2;
			// Set level to 1 (purchased)
			membershipLevel = 1; // Use membershipLevel to indicate donations purchased
			// Update price
			button.updatePrice("MAXED");
			button.disable();
			// Play level up sound
			LK.getSound('levelUp').play();
			// Update storage
			updateStorage();
		}
	});
	// Check if donations are already purchased and disable the button
	if (membershipLevel >= 1) {
		donationsUpgradeButton.updatePrice("MAXED");
		donationsUpgradeButton.disable();
	}
	// Add a 'Previous' button to go back to the first panel
	var previousButton = new Button("Previous", 240, 80, 0x607D8B);
	previousButton.x = -upgradePanel2.children[0].width / 2 + 130; // Position near the close button
	previousButton.y = -upgradePanel2.children[0].height / 2 + 50;
	upgradePanel2.addChild(previousButton);
	// Set callback for the previous button
	previousButton.up = function (x, y, obj) {
		tween(previousButton, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
		hideUpgradePanel2(); // Hide current panel
		showUpgradePanel(); // Show the previous panel
	};
	// Remove the 'Next' button from this panel
	upgradePanel2.setNextCallback(null); // Remove the next button callback
}
// Function to hide the second upgrade panel
function hideUpgradePanel2() {
	upgradePanel2.visible = false;
	memeStudio.visible = true;
	statsContainer.visible = true;
	studioContent.visible = true;
}
// Initialize wheel screen (initially null)
var wheelScreen = null;
// Function to show wheel screen
function showWheelScreen() {
	// Hide main elements
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Create wheel screen if it doesn't exist
	if (!wheelScreen) {
		wheelScreen = new WheelScreen();
		wheelScreen.x = 2048 / 2;
		wheelScreen.y = 2732 / 2;
		game.addChild(wheelScreen);
		// Set prize callback
		wheelScreen.setPrizeCallback(function (prize) {
			// Process the prize
			if (prize.isFollowers) {
				// Add followers
				followers += prize.value;
				// Show followers gained
				memeStudio.showEarnings(0, prize.value);
			} else {
				// Add money
				money += prize.value;
				// Show money gained
				memeStudio.showEarnings(prize.value, 0);
			}
			// Update storage
			updateStorage();
		});
		// Set close callback
		wheelScreen.setCloseCallback(function () {
			// Hide wheel screen
			wheelScreen.visible = false;
			// Show main elements
			memeStudio.visible = true;
			statsContainer.visible = true;
			studioContent.visible = true;
		});
	} else {
		// Show existing wheel screen
		wheelScreen.visible = true;
	}
}
// Set callback for spin button
memeStudio.setSpinButtonCallback(function () {
	showWheelScreen();
	LK.getSound('click').play();
});
// Start background music
LK.playMusic('bgMusic', {
	fade: {
		start: 0,
		end: 0.4,
		duration: 1000
	}
});
// Calculate any offline earnings when game starts
calculateOfflineEarnings();
// Set up auto earning timer
var lastAutoEarningTime = Date.now();
// Update UI with current values
function updateUI() {
	moneyText.setText("$" + Math.floor(money));
	followerText.setText(Math.floor(followers) + " followers");
	gemsText.setText(Math.floor(gems) + " gems");
	if (autoEarningRate > 0) {
		autoEarningText.setText("Auto earning: $" + Math.floor(autoEarningRate / 180) + " / 20 seconds");
	} else {
		autoEarningText.setText("");
	}
}
// Add game reference to global scope so it can be accessed from upgradeScreen
game.money = money;
game.memeLevel = memeLevel;
game.studioLevel = studioLevel;
game.staffLevel = staffLevel;
game.levels = upgradeLevels;
game.updateStorage = updateStorage;
game.updateAutoEarningRate = updateAutoEarningRate;
game.gems = gems; // Add gems to game reference
// Array to hold confetti particles
var confettiParticles = [];
// Main game update loop
game.update = function () {
	// Handle auto earnings
	var now = Date.now();
	var elapsed = now - lastAutoEarningTime;
	if (elapsed >= AUTO_EARNINGS_INTERVAL && autoEarningRate > 0) {
		// Calculate earnings for this period (20 seconds is 1/180th of an hour)
		var earnings = Math.floor(autoEarningRate / 180);
		if (earnings > 0) {
			money += earnings;
			lastAutoEarningTime = now;
			updateStorage();
		}
	}
	// Update UI elements
	updateUI();
	// Sync game reference values
	game.money = money;
	game.gems = gems; // Sync gems to game reference
	// Update and clean up confetti particles
	for (var i = confettiParticles.length - 1; i >= 0; i--) {
		var particle = confettiParticles[i];
		if (particle && particle.update) {
			particle.update();
			if (!particle.parent) {
				// Check if the particle has been removed from the stage
				confettiParticles.splice(i, 1);
			}
		} else if (!particle) {
			// Remove null/undefined entries
			confettiParticles.splice(i, 1);
		}
	}
	// Enable/disable upgrade buttons based on available money
	// All upgrade buttons removed
};
// MatchingGame instance (initially null)
var matchingGame = null;
// Create controversial warning dialog (initially hidden)
var controversialWarningDialog = new ControversialWarning();
controversialWarningDialog.x = 2048 / 2;
controversialWarningDialog.y = 2732 / 2;
controversialWarningDialog.visible = false;
game.addChild(controversialWarningDialog);
// Create viral meme dialog (initially hidden)
var viralMemeDialog = new ViralMemeDialog();
viralMemeDialog.x = 2048 / 2;
viralMemeDialog.y = 2732 / 2;
viralMemeDialog.visible = false;
game.addChild(viralMemeDialog);
// Set close callback for controversial warning dialog
controversialWarningDialog.setCloseCallback(function () {
	controversialWarningDialog.visible = false;
});
// Set close callback for viral meme dialog
viralMemeDialog.setCloseCallback(function () {
	viralMemeDialog.visible = false;
});
// PuzzleGame instance (initially null)
var puzzleGame = null;
// Function to start Matching game
function startMatchingGame() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new MatchingGame or reset existing one
	if (!matchingGame) {
		matchingGame = new MatchingGame();
		matchingGame.x = 2048 / 2;
		matchingGame.y = 2732 / 2;
		game.addChild(matchingGame);
	} else {
		// Remove existing game and create a new one
		game.removeChild(matchingGame);
		matchingGame = new MatchingGame();
		matchingGame.x = 2048 / 2;
		matchingGame.y = 2732 / 2;
		game.addChild(matchingGame);
	}
	// Show the game
	matchingGame.visible = true;
	// Set callback for return home button
	matchingGame.setReturnHomeCallback(function () {
		// Hide MatchingGame
		matchingGame.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
	// Set callback for when game finishes
	matchingGame.setFinishCallback(function () {
		// Hide Matching game
		matchingGame.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards for completing video game - memory games give a bit more followers
		var videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 7; // Doubled from 3.5 to 7
		var videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 4;
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play coin sound
		LK.getSound('coin').play();
		gems += 1; // Award 1 gem for winning
		// Update storage
		updateStorage();
	});
} /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	money: 100,
	followers: 0,
	memeLevel: 1,
	studioLevel: 1,
	staffLevel: 0,
	marketingLevel: 0,
	autoEarningRate: 0,
	lastPlayed: "undefined"
});
/**** 
* Classes
****/ 
var Button = Container.expand(function (text, width, height, color) {
	var self = Container.call(this);
	var buttonShape = self.attachAsset('button', {
		width: width || 600,
		height: height || 200,
		color: color || 0x4CAF50,
		anchorX: 0.5,
		anchorY: 0.5
	});
	var buttonText = new Text2(text, {
		size: 60,
		fill: 0xFFFFFF
	});
	buttonText.anchor.set(0.5, 0.5);
	self.addChild(buttonText);
	self.setText = function (newText) {
		buttonText.setText(newText);
	};
	self.setColor = function (newColor) {
		buttonShape.tint = newColor;
	};
	self.down = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100
		});
		LK.getSound('click').play();
	};
	self.up = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
	};
	return self;
});
var ConfettiParticle = Container.expand(function () {
	var self = Container.call(this);
	// Create a simple rectangular confetti graphic
	var confettiGraphic = self.attachAsset('bullet', {
		// Using 'bullet' shape asset for simplicity
		width: 20,
		height: 10,
		anchorX: 0.5,
		anchorY: 0.5,
		color: Math.random() * 0xFFFFFF // Random color
	});
	// Set random initial rotation and scale
	confettiGraphic.rotation = Math.random() * Math.PI * 2;
	confettiGraphic.scale.set(0.5 + Math.random() * 0.5);
	// Random initial speed and direction
	self.speedY = 5 + Math.random() * 5; // Falling speed
	self.speedX = -5 + Math.random() * 10; // Sideways speed
	// Gravity effect (optional, makes falling more realistic)
	var gravity = 0.2;
	// Rotation speed
	self.rotationSpeed = -0.1 + Math.random() * 0.2;
	// Update method for animation
	self.update = function () {
		self.y += self.speedY;
		self.x += self.speedX;
		self.speedY += gravity; // Apply gravity
		self.rotation += self.rotationSpeed; // Rotate
		// Remove particles when they fall off screen
		if (self.y > 2732 + 50) {
			self.destroy();
		}
	};
	return self;
});
var Connect4Game = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var COLS = 7;
	var ROWS = 6;
	var CELL_SIZE = 150;
	var PLAYER_COLOR = 0xff0000;
	var AI_COLOR = 0xffff00;
	var BOARD_COLOR = 0x0000ff;
	var EMPTY = 0;
	var PLAYER = 1;
	var AI = 2;
	// Game state
	var board = [];
	var currentPlayer = PLAYER;
	var gameOver = false;
	var gameResult = null;
	// Create board background
	var boardBackground = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: COLS * CELL_SIZE + 40,
		height: ROWS * CELL_SIZE + 40,
		color: BOARD_COLOR
	});
	// Initialize board data
	function initBoard() {
		board = [];
		for (var i = 0; i < COLS; i++) {
			board[i] = [];
			for (var j = 0; j < ROWS; j++) {
				board[i][j] = EMPTY;
			}
		}
		currentPlayer = PLAYER;
		gameOver = false;
		gameResult = null;
	}
	// Create visual board
	var cells = [];
	var circles = [];
	function createVisualBoard() {
		// Create cell containers
		for (var col = 0; col < COLS; col++) {
			cells[col] = [];
			circles[col] = [];
			for (var row = 0; row < ROWS; row++) {
				var cell = new Container();
				cell.x = (col - COLS / 2 + 0.5) * CELL_SIZE;
				cell.y = (row - ROWS / 2 + 0.5) * CELL_SIZE;
				var cellCircle = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: CELL_SIZE - 20,
					height: CELL_SIZE - 20,
					color: 0xffffff
				});
				cell.addChild(cellCircle);
				self.addChild(cell);
				cells[col][row] = cell;
				circles[col][row] = cellCircle;
			}
		}
	}
	// Create status text
	var statusText = new Text2("Your turn", {
		size: 60,
		fill: 0xffffff
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -500;
	self.addChild(statusText);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Drop piece in column
	function dropPiece(col) {
		if (gameOver) {
			return false;
		}
		// Find the first empty row
		var row = ROWS - 1;
		while (row >= 0 && board[col][row] !== EMPTY) {
			row--;
		}
		// If column is full
		if (row < 0) {
			return false;
		}
		// Drop piece
		board[col][row] = currentPlayer;
		updateVisualBoard();
		// Check for win
		if (checkWin(col, row)) {
			gameOver = true;
			gameResult = currentPlayer;
			finishButton.visible = true;
			statusText.setText(currentPlayer === PLAYER ? "You win!" : "AI wins!");
			return true;
		}
		// Check for draw
		if (checkDraw()) {
			gameOver = true;
			gameResult = 0;
			finishButton.visible = true;
			statusText.setText("It's a draw!");
			return true;
		}
		// Switch player
		currentPlayer = currentPlayer === PLAYER ? AI : PLAYER;
		statusText.setText(currentPlayer === PLAYER ? "Your turn" : "AI thinking...");
		return true;
	}
	// Update the visual representation of the board
	function updateVisualBoard() {
		for (var col = 0; col < COLS; col++) {
			for (var row = 0; row < ROWS; row++) {
				if (board[col][row] === EMPTY) {
					circles[col][row].tint = 0xffffff;
				} else if (board[col][row] === PLAYER) {
					circles[col][row].tint = PLAYER_COLOR;
				} else {
					circles[col][row].tint = AI_COLOR;
				}
			}
		}
	}
	// Check if current player won
	function checkWin(col, row) {
		var player = board[col][row];
		// Check horizontal
		var count = 0;
		for (var c = 0; c < COLS; c++) {
			if (board[c][row] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check vertical
		count = 0;
		for (var r = 0; r < ROWS; r++) {
			if (board[col][r] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check diagonal (/)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 3; r < ROWS; r++) {
				if (board[c][r] === player && board[c + 1][r - 1] === player && board[c + 2][r - 2] === player && board[c + 3][r - 3] === player) {
					return true;
				}
			}
		}
		// Check diagonal (\)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 0; r < ROWS - 3; r++) {
				if (board[c][r] === player && board[c + 1][r + 1] === player && board[c + 2][r + 2] === player && board[c + 3][r + 3] === player) {
					return true;
				}
			}
		}
		return false;
	}
	// Check if the game is a draw
	function checkDraw() {
		for (var col = 0; col < COLS; col++) {
			if (board[col][0] === EMPTY) {
				return false;
			}
		}
		return true;
	}
	// AI move
	function aiMove() {
		// Smarter AI using minimax with alpha-beta pruning
		var validColumns = [];
		var scores = [];
		// Get all valid columns
		for (var col = 0; col < COLS; col++) {
			if (board[col][0] === EMPTY) {
				validColumns.push(col);
			}
		}
		// Evaluate each possible move
		for (var i = 0; i < validColumns.length; i++) {
			var col = validColumns[i];
			var tempBoard = copyBoard(board);
			var row = dropPieceInTempBoard(tempBoard, col, AI);
			// Check for immediate win
			if (checkWinInBoard(tempBoard, col, row, AI)) {
				// AI can win in this move, take it immediately
				LK.setTimeout(function () {
					dropPiece(col);
				}, 1000);
				return;
			}
			// Otherwise, evaluate this move
			var score = evaluateMove(tempBoard, col, row, 4); // Look 4 moves ahead
			scores.push({
				col: col,
				score: score
			});
		}
		// Find best move
		var bestMoves = [];
		var bestScore = -Infinity;
		for (var i = 0; i < scores.length; i++) {
			if (scores[i].score > bestScore) {
				bestScore = scores[i].score;
				bestMoves = [scores[i].col];
			} else if (scores[i].score === bestScore) {
				bestMoves.push(scores[i].col);
			}
		}
		// Choose randomly among best moves for some unpredictability
		var bestCol = bestMoves[Math.floor(Math.random() * bestMoves.length)];
		LK.setTimeout(function () {
			dropPiece(bestCol);
		}, 1000);
	}
	// Creates a copy of the current board
	function copyBoard(originalBoard) {
		var newBoard = [];
		for (var i = 0; i < COLS; i++) {
			newBoard[i] = [];
			for (var j = 0; j < ROWS; j++) {
				newBoard[i][j] = originalBoard[i][j];
			}
		}
		return newBoard;
	}
	// Drops a piece in a temporary board for evaluation
	function dropPieceInTempBoard(tempBoard, col, player) {
		var row = ROWS - 1;
		while (row >= 0 && tempBoard[col][row] !== EMPTY) {
			row--;
		}
		if (row >= 0) {
			tempBoard[col][row] = player;
		}
		return row;
	}
	// Check for win in temporary board
	function checkWinInBoard(board, col, row, player) {
		// Check horizontal
		var count = 0;
		for (var c = 0; c < COLS; c++) {
			if (board[c][row] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check vertical
		count = 0;
		for (var r = 0; r < ROWS; r++) {
			if (board[col][r] === player) {
				count++;
				if (count >= 4) {
					return true;
				}
			} else {
				count = 0;
			}
		}
		// Check diagonal (/)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 3; r < ROWS; r++) {
				if (board[c][r] === player && board[c + 1][r - 1] === player && board[c + 2][r - 2] === player && board[c + 3][r - 3] === player) {
					return true;
				}
			}
		}
		// Check diagonal (\)
		for (var c = 0; c < COLS - 3; c++) {
			for (var r = 0; r < ROWS - 3; r++) {
				if (board[c][r] === player && board[c + 1][r + 1] === player && board[c + 2][r + 2] === player && board[c + 3][r + 3] === player) {
					return true;
				}
			}
		}
		return false;
	}
	// Evaluate the board position using heuristics
	function evaluateMove(tempBoard, col, row, depth) {
		// Base score
		var score = 0;
		// Check if this is a winning move
		if (checkWinInBoard(tempBoard, col, row, AI)) {
			return 1000;
		}
		// Look for opponent's winning move and block it
		for (var c = 0; c < COLS; c++) {
			if (tempBoard[c][0] === EMPTY) {
				var tempRow = dropPieceInTempBoard(copyBoard(tempBoard), c, PLAYER);
				if (tempRow >= 0 && checkWinInBoard(tempBoard, c, tempRow, PLAYER)) {
					score -= 800; // Prioritize blocking opponent win
				}
			}
		}
		// Evaluate center control (center column is strategically valuable)
		var centerCol = Math.floor(COLS / 2);
		var centerCount = 0;
		for (var r = 0; r < ROWS; r++) {
			if (tempBoard[centerCol][r] === AI) {
				centerCount++;
			}
		}
		score += centerCount * 30;
		// Evaluate connections (2 in a row, 3 in a row)
		score += evaluateConnections(tempBoard, AI) * 10;
		score -= evaluateConnections(tempBoard, PLAYER) * 8;
		// Prefer lower rows (more stable position)
		score += (ROWS - row) * 5;
		// If depth allows, do minimax for deeper evaluation
		if (depth > 0) {
			// Simulate player's best response
			var minScore = Infinity;
			for (var c = 0; c < COLS; c++) {
				if (tempBoard[c][0] === EMPTY) {
					var newBoard = copyBoard(tempBoard);
					var newRow = dropPieceInTempBoard(newBoard, c, PLAYER);
					if (newRow >= 0) {
						// Check if player wins
						if (checkWinInBoard(newBoard, c, newRow, PLAYER)) {
							return -900; // Very bad for AI
						}
						// Recursive evaluation with reduced depth
						var moveScore = evaluateMove(newBoard, c, newRow, depth - 1);
						if (moveScore < minScore) {
							minScore = moveScore;
						}
					}
				}
			}
			// Add minimax result with lower weight
			score += minScore * 0.5;
		}
		return score;
	}
	// Evaluate connected pieces (2 or 3 in a row)
	function evaluateConnections(board, player) {
		var score = 0;
		// Check horizontal connections
		for (var r = 0; r < ROWS; r++) {
			for (var c = 0; c < COLS - 1; c++) {
				if (board[c][r] === player && board[c + 1][r] === player) {
					score += 1; // 2 in a row
					if (c + 2 < COLS && board[c + 2][r] === player) {
						score += 5; // 3 in a row
					}
				}
			}
		}
		// Check vertical connections
		for (var c = 0; c < COLS; c++) {
			for (var r = 0; r < ROWS - 1; r++) {
				if (board[c][r] === player && board[c][r + 1] === player) {
					score += 1; // 2 in a row
					if (r + 2 < ROWS && board[c][r + 2] === player) {
						score += 5; // 3 in a row
					}
				}
			}
		}
		// Diagonal connections would be similar but more complex
		// Adding them would make the AI even smarter
		return score;
	}
	// Handle column click
	self.down = function (x, y) {
		if (currentPlayer !== PLAYER || gameOver) {
			return;
		}
		// Convert coordinates to column
		var boardX = x;
		var col = Math.floor((boardX + COLS / 2 * CELL_SIZE) / CELL_SIZE);
		// Check if valid column
		if (col >= 0 && col < COLS) {
			if (dropPiece(col) && !gameOver) {
				aiMove();
			}
		}
	};
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize
	initBoard();
	createVisualBoard();
	updateVisualBoard();
	return self;
});
var ControversialWarning = Container.expand(function () {
	var self = Container.call(this);
	// Background overlay
	var overlay = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0x000000,
		alpha: 0.8
	});
	// Warning box
	var warningBox = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 800,
		height: 500,
		color: 0xffffff
	});
	// Warning text
	var warningText = new Text2("You uploaded a meme that your fans didn't like :(", {
		size: 60,
		fill: 0xff0000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	warningText.anchor.set(0.5, 0.5);
	warningText.y = -80;
	self.addChild(warningText);
	// Lose followers text
	var loseFollowersText = new Text2("You lose a lot of followers!", {
		size: 50,
		fill: 0x000000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	loseFollowersText.anchor.set(0.5, 0.5);
	loseFollowersText.y = 50;
	self.addChild(loseFollowersText);
	// OK button
	var okButton = new Button("OK", 300, 100, 0x4CAF50);
	okButton.y = 180;
	self.addChild(okButton);
	self.setCloseCallback = function (callback) {
		okButton.up = function () {
			tween(okButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (callback) {
				callback();
			}
			self.visible = false;
		};
	};
	return self;
});
var CupShuffleGame = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var NUM_CUPS = 3;
	var CUP_SIZE = 200;
	var BALL_SIZE = 50;
	var SHUFFLE_SPEED = 500; // ms
	var SHUFFLE_ROUNDS = 3;
	// Game state
	var cups = [];
	var ball;
	var ballPosition = 0; // Index of the cup containing the ball
	var isShuffling = false;
	var gameOver = false;
	var gameResult = null; // 'win' or 'lose'
	// Create background
	var background = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 1000,
		color: 0xcccccc // Light grey
	});
	// Create status text
	var statusText = new Text2("Watch the ball!", {
		size: 60,
		fill: 0x000000
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -400;
	self.addChild(statusText);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Initialize game
	function initGame() {
		cups = [];
		isShuffling = false;
		gameOver = false;
		gameResult = null;
		statusText.setText("Watch the ball!");
		finishButton.visible = false;
		// Remove old cups and ball
		self.removeChildren();
		self.addChild(background);
		self.addChild(statusText);
		self.addChild(finishButton);
		self.addChild(returnHomeButton);
		// Create cups
		var startX = -(NUM_CUPS - 1) * CUP_SIZE / 2;
		for (var i = 0; i < NUM_CUPS; i++) {
			var cup = new Container();
			cup.x = startX + i * CUP_SIZE;
			cup.y = 100; // Position the cups
			cup.index = i;
			var cupGraphic = cup.attachAsset('Cup', {
				// Using paperChoice as a simple cup graphic
				anchorX: 0.5,
				anchorY: 1,
				// Anchor at the bottom
				width: CUP_SIZE,
				height: CUP_SIZE * 1.2 // Slightly taller
			});
			cup.addChild(cupGraphic);
			self.addChild(cup);
			cups.push(cup);
		}
		// Place the ball under a random cup
		ballPosition = Math.floor(Math.random() * NUM_CUPS);
		ball = self.attachAsset('moneyIcon', {
			// Using moneyIcon as a simple ball graphic
			anchorX: 0.5,
			anchorY: 0.5,
			width: BALL_SIZE,
			height: BALL_SIZE,
			color: 0xffd700 // Gold color for the ball
		});
		// Position ball below the initial cup
		ball.x = cups[ballPosition].x;
		ball.y = cups[ballPosition].y - CUP_SIZE / 2; // Position below the cup
		self.addChild(ball);
		// Make cups interactive after initial reveal
		LK.setTimeout(function () {
			showBall(true); // Reveal initial ball position briefly
			LK.setTimeout(function () {
				showBall(false); // Hide ball
				shuffleCups(); // Start shuffling
			}, 1000); // Reveal time
		}, 500); // Initial delay
	}
	// Show or hide the ball
	function showBall(visible) {
		if (ball) {
			ball.visible = visible;
		}
	}
	// Shuffle the cups
	function shuffleCups() {
		isShuffling = true;
		statusText.setText("Shuffling...");
		var shuffleSequence = [];
		// Generate a sequence of swaps
		for (var i = 0; i < SHUFFLE_ROUNDS; i++) {
			// Swap adjacent cups
			var swapIndex = Math.floor(Math.random() * (NUM_CUPS - 1));
			shuffleSequence.push(swapIndex);
		}
		performShuffleStep(0);
		function performShuffleStep(step) {
			if (step >= shuffleSequence.length) {
				// Shuffling finished
				isShuffling = false;
				statusText.setText("Which cup is the ball under?");
				// Make cups clickable to select
				for (var i = 0; i < NUM_CUPS; i++) {
					cups[i].interactive = true;
					cups[i].down = function (index) {
						// Using an IIFE to properly capture the cup index
						return function (x, y, obj) {
							if (!gameOver && !isShuffling) {
								// Call checkGuess with the proper context and the captured index
								checkGuess(index); // Using the local function directly instead of through 'self'
							}
						};
					}(i); // Immediately invoke the function with the current value of i
				}
				return;
			}
			var swapIndex = shuffleSequence[step];
			var cup1 = cups[swapIndex];
			var cup2 = cups[swapIndex + 1];
			// Swap positions visually
			var cup1X = cup1.x;
			var cup2X = cup2.x;
			tween(cup1, {
				x: cup2X
			}, {
				duration: SHUFFLE_SPEED / 2
			});
			tween(cup2, {
				x: cup1X
			}, {
				duration: SHUFFLE_SPEED / 2
			});
			// If ball is under one of the swapped cups, move it with the cup
			if (ballPosition === swapIndex) {
				tween(ball, {
					x: cup2X
				}, {
					duration: SHUFFLE_SPEED / 2
				});
				ballPosition = swapIndex + 1;
			} else if (ballPosition === swapIndex + 1) {
				tween(ball, {
					x: cup1X
				}, {
					duration: SHUFFLE_SPEED / 2
				});
				ballPosition = swapIndex;
			}
			// Continue to the next step after animation
			LK.setTimeout(function () {
				// Update logical positions in the cups array
				var temp = cups[swapIndex];
				cups[swapIndex] = cups[swapIndex + 1];
				cups[swapIndex + 1] = temp;
				performShuffleStep(step + 1);
			}, SHUFFLE_SPEED / 2); // Half the shuffle speed for each swap animation
		}
	}
	// Check the player's guess
	function checkGuess(selectedIndex) {
		if (gameOver || isShuffling) {
			return;
		}
		gameOver = true;
		// Disable cup interaction
		for (var i = 0; i < NUM_CUPS; i++) {
			cups[i].interactive = false;
		}
		// Reveal all cups and the ball
		showBall(true);
		// You might want to lift the cups or make them transparent briefly here
		if (selectedIndex === ballPosition) {
			statusText.setText("You found it! You win!");
			gameResult = 'win';
			finishButton.visible = true;
			// Play win sound
			LK.getSound('coin').play();
		} else {
			statusText.setText("Wrong cup! You lose.");
			gameResult = 'lose';
			finishButton.visible = true;
			// Play lose sound (if you have one)
			// LK.getSound('lose').play();
		}
	}
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback(gameResult); // Pass the result of the game
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize the game when created
	initGame();
	return self;
});
var DonationNotification = Container.expand(function () {
	var self = Container.call(this);
	// Background for notification
	var background = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 400,
		height: 100,
		color: 0x4CAF50
	});
	// Text for donation notification
	var notificationText = new Text2("Someone donated $0!", {
		size: 48,
		fill: 0x000000
	});
	notificationText.anchor.set(0.5, 0.5);
	self.addChild(notificationText);
	// Set donation amount
	self.setAmount = function (amount) {
		notificationText.setText("Someone donated $" + amount + "!");
	};
	// Start animation - slide in from right and then fade out
	self.animate = function () {
		// Reset position and opacity
		self.x = 2048 + 250; // Start off-screen
		self.alpha = 1;
		// Slide in animation
		tween(self, {
			x: 2048 - 250
		}, {
			duration: 500,
			onFinish: function onFinish() {
				// Wait a moment at the side of the screen
				LK.setTimeout(function () {
					// Then fade out
					tween(self, {
						alpha: 0
					}, {
						duration: 500,
						onFinish: function onFinish() {
							// Remove from parent when done
							if (self.parent) {
								self.parent.removeChild(self);
							}
						}
					});
				}, 2000);
			}
		});
	};
	return self;
});
var GameSelector = Container.expand(function () {
	var self = Container.call(this);
	var background = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0xdddddd
	});
	var titleText = new Text2("Choose A Game To Record", {
		size: 80,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0.5);
	titleText.y = -900;
	self.addChild(titleText);
	var connect4Button = new Button("Connect 4", 600, 200, 0x4CAF50);
	connect4Button.y = -700;
	self.addChild(connect4Button);
	var matchingButton = new Button("Memory Match", 600, 200, 0x2196F3);
	matchingButton.y = -400;
	self.addChild(matchingButton);
	var puzzleButton = new Button("Sliding Puzzle", 600, 200, 0x9C27B0);
	puzzleButton.y = -100;
	self.addChild(puzzleButton);
	var cupShuffleButton = new Button("Cup Shuffle", 600, 200, 0x4CAF50); // Standard button color
	cupShuffleButton.y = 200; // Position below the sliding puzzle button
	self.addChild(cupShuffleButton);
	// Create locked indicator for matching game
	var lockedText = new Text2("100 followers needed", {
		size: 60,
		fill: 0xFF5733
	});
	lockedText.anchor.set(0.5, 0.5);
	lockedText.y = -330;
	lockedText.visible = false;
	self.addChild(lockedText);
	// Create locked indicator for puzzle game
	var puzzleLockedText = new Text2("250 followers needed", {
		size: 60,
		fill: 0xFF5733
	});
	puzzleLockedText.anchor.set(0.5, 0.5);
	puzzleLockedText.y = -30;
	puzzleLockedText.visible = false;
	self.addChild(puzzleLockedText);
	// Create locked indicator for cup shuffle game
	var cupShuffleLockedText = new Text2("400 followers needed", {
		size: 60,
		fill: 0xFF5733
	});
	cupShuffleLockedText.anchor.set(0.5, 0.5);
	cupShuffleLockedText.y = 270; // Position below the button
	cupShuffleLockedText.visible = false;
	self.addChild(cupShuffleLockedText);
	var backButton = new Button("Go Back", 400, 130, 0xff5555);
	backButton.y = 900;
	self.addChild(backButton);
	// Update lock status based on follower count
	self.updateLockStatus = function (followerCount) {
		// Always show matching game as unlocked
		lockedText.visible = false;
		// Always show puzzle game as unlocked
		puzzleLockedText.visible = false;
		// Check and update Cup Shuffle lock status
		if (followerCount < 400) {
			cupShuffleButton.setColor(0x9E9E9E); // Grey out the button
			cupShuffleLockedText.visible = true;
		} else {
			cupShuffleButton.setColor(0x4CAF50); // Set normal color
			cupShuffleLockedText.visible = false;
		}
	};
	self.setGameCallback = function (callback) {
		connect4Button.up = function (x, y, obj) {
			tween(connect4Button, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback("connect4");
			}
		};
		matchingButton.up = function (x, y, obj) {
			tween(matchingButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				// Allow matching game for all players
				matchingButton.setColor(0x2196F3); // Set normal color
				callback("matching");
			}
		};
		puzzleButton.up = function (x, y, obj) {
			tween(puzzleButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				// Allow puzzle game for all players
				puzzleButton.setColor(0x9C27B0); // Set normal color
				callback("puzzle");
			}
		};
		cupShuffleButton.up = function (x, y, obj) {
			tween(cupShuffleButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			// Check if unlocked before calling the callback
			if (followers >= 400 && callback) {
				callback("cupShuffle");
			}
		};
	};
	self.setBackCallback = function (callback) {
		backButton.up = function (x, y, obj) {
			tween(backButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	return self;
});
var MatchingGame = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var GRID_SIZE = 4; // 4x4 grid
	var CARD_SIZE = 150;
	var CARD_MARGIN = 20;
	var CARD_COLOR = 0x2196F3;
	var CARD_BACK_COLOR = 0x607D8B;
	// Game state
	var cards = [];
	var flippedCards = [];
	var matchedPairs = 0;
	var totalPairs = 8;
	var canFlip = true;
	var gameOver = false;
	// Create background
	var boardBackground = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: (CARD_SIZE + CARD_MARGIN) * GRID_SIZE + 40,
		height: (CARD_SIZE + CARD_MARGIN) * GRID_SIZE + 40,
		color: 0x333333
	});
	// Create status text
	var statusText = new Text2("Match all pairs", {
		size: 60,
		fill: 0xffffff
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -500;
	self.addChild(statusText);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Symbols for matching (emojis represented as text)
	var symbols = ["๐", "๐", "๐", "๐ฎ", "๐", "โฝ", "๐ต", "๐จ"];
	// Initialize game
	function initGame() {
		cards = [];
		flippedCards = [];
		matchedPairs = 0;
		gameOver = false;
		canFlip = true;
		// Create all card pairs
		var cardValues = [];
		for (var i = 0; i < totalPairs; i++) {
			cardValues.push(i);
			cardValues.push(i);
		}
		// Shuffle the cards
		for (var i = cardValues.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = cardValues[i];
			cardValues[i] = cardValues[j];
			cardValues[j] = temp;
		}
		// Create card grid
		for (var row = 0; row < GRID_SIZE; row++) {
			for (var col = 0; col < GRID_SIZE; col++) {
				var index = row * GRID_SIZE + col;
				// Create card container
				var card = new Container();
				card.x = (col - GRID_SIZE / 2 + 0.5) * (CARD_SIZE + CARD_MARGIN);
				card.y = (row - GRID_SIZE / 2 + 0.5) * (CARD_SIZE + CARD_MARGIN);
				// Card back (shown initially)
				var cardBack = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: CARD_SIZE,
					height: CARD_SIZE,
					color: CARD_BACK_COLOR
				});
				// Card front (hidden initially)
				var cardFront = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: CARD_SIZE,
					height: CARD_SIZE,
					color: CARD_COLOR
				});
				cardFront.visible = false;
				// Symbol text
				var symbolText = new Text2(symbols[cardValues[index]], {
					size: 60,
					fill: 0xFFFFFF
				});
				symbolText.anchor.set(0.5, 0.5);
				symbolText.visible = false;
				// Add to card container
				card.addChild(cardBack);
				card.addChild(cardFront);
				card.addChild(symbolText);
				// Store card data
				card.value = cardValues[index];
				card.isFlipped = false;
				card.isMatched = false;
				// Add to game
				self.addChild(card);
				cards.push(card);
				// Make clickable
				card.interactive = true;
			}
		}
	}
	// Flip a card
	function flipCard(card) {
		if (!canFlip || card.isFlipped || card.isMatched || gameOver) {
			return;
		}
		// Flip animation
		card.isFlipped = true;
		card.children[0].visible = false; // Hide back
		card.children[1].visible = true; // Show front
		card.children[2].visible = true; // Show symbol
		// Add to flipped cards
		flippedCards.push(card);
		// Check for match if we have 2 cards flipped
		if (flippedCards.length === 2) {
			canFlip = false;
			// Check for match
			if (flippedCards[0].value === flippedCards[1].value) {
				// Cards match
				LK.setTimeout(function () {
					flippedCards[0].isMatched = true;
					flippedCards[1].isMatched = true;
					// Highlight matched cards
					tween(flippedCards[0].children[1], {
						tint: 0x4CAF50 // Green tint
					}, {
						duration: 300
					});
					tween(flippedCards[1].children[1], {
						tint: 0x4CAF50 // Green tint
					}, {
						duration: 300
					});
					matchedPairs++;
					flippedCards = [];
					canFlip = true;
					// Check for game over
					if (matchedPairs === totalPairs) {
						gameOver = true;
						statusText.setText("You win!");
						finishButton.visible = true;
					}
				}, 500);
			} else {
				// Cards don't match, flip back
				LK.setTimeout(function () {
					flippedCards[0].isFlipped = false;
					flippedCards[0].children[0].visible = true; // Show back
					flippedCards[0].children[1].visible = false; // Hide front
					flippedCards[0].children[2].visible = false; // Hide symbol
					flippedCards[1].isFlipped = false;
					flippedCards[1].children[0].visible = true; // Show back
					flippedCards[1].children[1].visible = false; // Hide front
					flippedCards[1].children[2].visible = false; // Hide symbol
					flippedCards = [];
					canFlip = true;
				}, 1000);
			}
		}
	}
	// Handle click
	self.down = function (x, y) {
		if (gameOver) {
			return;
		}
		// Convert coordinates to find clicked card
		for (var i = 0; i < cards.length; i++) {
			var card = cards[i];
			var cardBounds = {
				x: card.x - CARD_SIZE / 2,
				y: card.y - CARD_SIZE / 2,
				width: CARD_SIZE,
				height: CARD_SIZE
			};
			if (x >= cardBounds.x && x <= cardBounds.x + cardBounds.width && y >= cardBounds.y && y <= cardBounds.y + cardBounds.height) {
				flipCard(card);
				break;
			}
		}
	};
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize the game
	initGame();
	return self;
});
// MatchingGame instance (initially null)
var MemeStudio = Container.expand(function () {
	var self = Container.call(this);
	var studioBackground = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		height: 1800
	});
	var memeCanvas = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 400,
		height: 400,
		y: -250
	});
	var memeText = new Text2("Your Meme Here", {
		size: 54,
		fill: 0x000000
	});
	memeText.anchor.set(0.5, 0.5);
	memeText.y = -250;
	self.addChild(memeText);
	// Current displayed meme asset
	var currentMemeAsset = null;
	// Function to display a random meme
	function displayRandomMeme() {
		// Remove previous meme if exists
		if (currentMemeAsset) {
			self.removeChild(currentMemeAsset);
		}
		// Generate random number between 1-14 to select a meme (added Wheelmeme1 and Meme10-13)
		var memeNumber = Math.floor(Math.random() * 14) + 1;
		var memeId;
		if (memeNumber === 10) {
			memeId = 'Wheelmeme1';
		} else if (memeNumber >= 10 && memeNumber <= 13) {
			memeId = 'Meme' + (memeNumber + 0); // Map 10-13 to Meme10-13
		} else {
			memeId = 'Meme' + memeNumber;
		}
		// Create the new meme asset
		currentMemeAsset = LK.getAsset(memeId, {
			anchorX: 0.5,
			anchorY: 0.5,
			y: -250,
			width: 400,
			height: 400
		});
		// Add the meme to display
		self.addChild(currentMemeAsset);
	}
	var earningsText = new Text2("Earnings: $0", {
		size: 48,
		fill: 0x333333
	});
	earningsText.anchor.set(0.5, 0.5);
	earningsText.y = 20;
	self.addChild(earningsText);
	var createButton = new Button("Create Meme!", 500, 150, 0x4CAF50);
	createButton.y = 150;
	self.addChild(createButton);
	var createVideoButton = new Button("Create Video", 500, 150, 0x4CAF50);
	createVideoButton.y = 270;
	self.addChild(createVideoButton);
	var upgradesButton = new Button("Upgrades", 500, 150, 0x4CAF50);
	upgradesButton.y = 390;
	self.addChild(upgradesButton);
	// Add Spin button
	var spinButton = new Container();
	var spinButtonAsset = spinButton.attachAsset('Spin', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 270,
		// 180 * 1.5
		height: 270 // 180 * 1.5
	});
	spinButton.y = upgradesButton.y + upgradesButton.height / 2 + spinButtonAsset.height / 2 + 30 - 1030; // Position below upgrades, moved down 70px
	spinButton.x = 460; // Move 460 pixels to the right from the center (was 490)
	self.addChild(spinButton);
	// Add interactivity to Spin button
	spinButton.down = function (x, y, obj) {
		tween(spinButtonAsset, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100
		});
		LK.getSound('click').play();
	};
	// Note: Spin button 'up' handler will be set via setSpinButtonCallback
	var followerGainText = new Text2("+0 followers", {
		size: 42,
		fill: 0xFF5733
	});
	followerGainText.anchor.set(0.5, 0.5);
	followerGainText.y = 250; // Adjusted y position, original might conflict visually
	followerGainText.alpha = 0;
	self.addChild(followerGainText);
	// Update the meme canvas based on current studio level
	self.updateMemeQuality = function (level) {
		var quality = ["Potato Quality", "Standard Meme", "HD Meme", "Ultra HD Meme", "Viral-Ready Meme"];
		var colors = [0xCCCCCC, 0xFFFFFF, 0xFFFFDD, 0xDDFFFF, 0xFFDDFF];
		if (level > quality.length) {
			level = quality.length;
		}
		memeText.setText(quality[level - 1]);
		memeCanvas.tint = colors[level - 1];
		// Scale the meme canvas slightly with each level
		var scale = 1 + (level - 1) * 0.1;
		memeCanvas.scale.set(scale, scale);
	};
	// Show the earnings animation
	self.showEarnings = function (amount, followers) {
		earningsText.setText("Earnings: $" + amount);
		tween(earningsText, {
			scaleX: 1.3,
			scaleY: 1.3
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(earningsText, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 300
				});
			}
		});
		if (followers > 0) {
			followerGainText.setText("+" + followers + " followers");
			followerGainText.alpha = 1;
			tween(followerGainText, {
				y: 230,
				alpha: 0
			}, {
				duration: 1500
			});
		}
	};
	self.setCreateButtonCallback = function (callback) {
		createButton.up = function (x, y, obj) {
			tween(createButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			// Display a random meme when creating a new meme
			displayRandomMeme();
			LK.getSound('click').play();
			LK.getSound('click').play();
			if (callback) {
				callback();
			}
		};
	};
	self.setCreateVideoButtonCallback = function (callback) {
		createVideoButton.up = function (x, y, obj) {
			tween(createVideoButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.setUpgradesButtonCallback = function (callback) {
		upgradesButton.up = function (x, y, obj) {
			tween(upgradesButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.setSpinButtonCallback = function (callback) {
		spinButton.up = function (x, y, obj) {
			tween(spinButtonAsset, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	return self;
});
var PuzzleGame = Container.expand(function () {
	var self = Container.call(this);
	var GRID_SIZE = 4; // 4x4 grid
	var PUZZLE_SIZE = 600;
	var TILE_SIZE = PUZZLE_SIZE / GRID_SIZE;
	var TILE_MARGIN = 5;
	// Game state
	var tiles = [];
	var emptyPos = {
		row: GRID_SIZE - 1,
		col: GRID_SIZE - 1
	};
	var moves = 0;
	var gameOver = false;
	// Create board background
	var boardBackground = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: PUZZLE_SIZE + 40,
		height: PUZZLE_SIZE + 40,
		color: 0x333333
	});
	// Create status text
	var statusText = new Text2("Slide tiles to order 1-15", {
		size: 60,
		fill: 0xffffff
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = -500;
	self.addChild(statusText);
	// Create moves counter
	var movesText = new Text2("Moves: 0", {
		size: 60,
		fill: 0xffffff
	});
	movesText.anchor.set(0.5, 0.5);
	movesText.y = -420;
	self.addChild(movesText);
	// Create finish button
	var finishButton = new Button("Finish Recording", 600, 150, 0xff5555);
	finishButton.y = 600;
	finishButton.visible = false;
	self.addChild(finishButton);
	// Create Return Home button
	var returnHomeButton = new Button("Return Home", 400, 130, 0x9E9E9E); // Grey color
	returnHomeButton.y = 750; // Position below finish button
	self.addChild(returnHomeButton);
	// Initialize board
	function initBoard() {
		tiles = [];
		moves = 0;
		gameOver = false;
		movesText.setText("Moves: 0");
		// Create all tiles
		var values = [];
		for (var i = 0; i < GRID_SIZE * GRID_SIZE - 1; i++) {
			values.push(i + 1);
		}
		values.push(0); // Empty tile
		// Shuffle tiles (ensuring puzzle is solvable)
		var isSolvable = false;
		while (!isSolvable) {
			shuffleArray(values);
			isSolvable = checkSolvable(values);
		}
		// Create visual tiles
		for (var row = 0; row < GRID_SIZE; row++) {
			tiles[row] = [];
			for (var col = 0; col < GRID_SIZE; col++) {
				var index = row * GRID_SIZE + col;
				var value = values[index];
				if (value === 0) {
					// Empty tile
					emptyPos = {
						row: row,
						col: col
					};
					tiles[row][col] = null;
					continue;
				}
				// Create tile
				var tile = new Container();
				tile.value = value;
				// Position tile in grid
				var x = (col - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				var y = (row - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				tile.x = x;
				tile.y = y;
				// Create tile background
				var tileBackground = LK.getAsset('centerCircle', {
					anchorX: 0.5,
					anchorY: 0.5,
					width: TILE_SIZE - TILE_MARGIN * 2,
					height: TILE_SIZE - TILE_MARGIN * 2,
					color: 0x4CAF50
				});
				// Create tile number
				var tileText = new Text2(value.toString(), {
					size: TILE_SIZE / 3,
					fill: 0xFFFFFF
				});
				tileText.anchor.set(0.5, 0.5);
				// Add to tile container
				tile.addChild(tileBackground);
				tile.addChild(tileText);
				// Add tile to game
				self.addChild(tile);
				tiles[row][col] = tile;
			}
		}
	}
	// Check if puzzle is solvable
	function checkSolvable(values) {
		var inversions = 0;
		var emptyRow = 0;
		// Find empty tile row from the bottom
		for (var i = 0; i < values.length; i++) {
			if (values[i] === 0) {
				emptyRow = Math.floor(i / GRID_SIZE);
				break;
			}
		}
		// Count inversions
		for (var i = 0; i < values.length - 1; i++) {
			if (values[i] === 0) {
				continue;
			}
			for (var j = i + 1; j < values.length; j++) {
				if (values[j] === 0) {
					continue;
				}
				if (values[i] > values[j]) {
					inversions++;
				}
			}
		}
		// Check solvability conditions
		if (GRID_SIZE % 2 === 1) {
			// Odd grid size - solvable if inversions is even
			return inversions % 2 === 0;
		} else {
			// Even grid size - solvable if:
			// - empty on even row from bottom + odd inversions
			// - empty on odd row from bottom + even inversions
			var emptyRowFromBottom = GRID_SIZE - emptyRow - 1;
			return emptyRowFromBottom % 2 === 0 && inversions % 2 === 1 || emptyRowFromBottom % 2 === 1 && inversions % 2 === 0;
		}
	}
	// Shuffle array (Fisher-Yates algorithm)
	function shuffleArray(array) {
		for (var i = array.length - 1; i > 0; i--) {
			var j = Math.floor(Math.random() * (i + 1));
			var temp = array[i];
			array[i] = array[j];
			array[j] = temp;
		}
	}
	// Check if puzzle is solved
	function checkWin() {
		for (var row = 0; row < GRID_SIZE; row++) {
			for (var col = 0; col < GRID_SIZE; col++) {
				var expectedValue = row * GRID_SIZE + col + 1;
				// Skip the last tile (empty)
				if (row === GRID_SIZE - 1 && col === GRID_SIZE - 1) {
					continue;
				}
				// Check if tile is in correct position
				if (!tiles[row][col] || tiles[row][col].value !== expectedValue) {
					return false;
				}
			}
		}
		return true;
	}
	// Determine if a tile can be moved (adjacent to empty)
	function canMoveTile(row, col) {
		return row === emptyPos.row && Math.abs(col - emptyPos.col) === 1 || col === emptyPos.col && Math.abs(row - emptyPos.row) === 1;
	}
	// Move a tile to the empty position
	function moveTile(row, col) {
		if (gameOver || !canMoveTile(row, col)) {
			return;
		}
		// Get the tile
		var tile = tiles[row][col];
		// Calculate new position
		var newX = (emptyPos.col - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
		var newY = (emptyPos.row - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
		// Animate tile movement
		tween(tile, {
			x: newX,
			y: newY
		}, {
			duration: 150
		});
		// Update game state
		tiles[emptyPos.row][emptyPos.col] = tile;
		tiles[row][col] = null;
		// Update empty position
		var oldEmptyPos = {
			row: emptyPos.row,
			col: emptyPos.col
		};
		emptyPos.row = row;
		emptyPos.col = col;
		// Increment move counter
		moves++;
		movesText.setText("Moves: " + moves);
		// Check for win
		if (checkWin()) {
			gameOver = true;
			statusText.setText("Puzzle Solved!");
			finishButton.visible = true;
		}
	}
	// Handle click/touch on the board
	self.down = function (x, y) {
		if (gameOver) {
			return;
		}
		// Convert coordinates to board position
		for (var row = 0; row < GRID_SIZE; row++) {
			for (var col = 0; col < GRID_SIZE; col++) {
				var tileX = (col - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				var tileY = (row - GRID_SIZE / 2 + 0.5) * TILE_SIZE;
				var left = tileX - TILE_SIZE / 2;
				var right = tileX + TILE_SIZE / 2;
				var top = tileY - TILE_SIZE / 2;
				var bottom = tileY + TILE_SIZE / 2;
				if (x >= left && x < right && y >= top && y < bottom) {
					if (tiles[row][col]) {
						moveTile(row, col);
					}
				}
			}
		}
	};
	// Set finish callback
	self.setFinishCallback = function (callback) {
		finishButton.up = function () {
			tween(finishButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Set return home callback
	self.setReturnHomeCallback = function (callback) {
		returnHomeButton.up = function () {
			tween(returnHomeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	// Initialize the game
	initBoard();
	return self;
});
var ResetConfirmation = Container.expand(function () {
	var self = Container.call(this);
	// Background overlay to dim the screen
	var overlay = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0x000000,
		alpha: 0.7
	});
	// Dialog box
	var dialog = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 800,
		height: 400,
		color: 0xffffff
	});
	// Warning text
	var warningText = new Text2("Are you sure you want to reset your progress?", {
		size: 50,
		fill: 0x000000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	warningText.anchor.set(0.5, 0.5);
	warningText.y = -80;
	self.addChild(warningText);
	// Yes button
	var yesButton = new Button("Yes", 200, 80, 0xff0000);
	yesButton.x = -150;
	yesButton.y = 80;
	self.addChild(yesButton);
	// No button
	var noButton = new Button("No", 200, 80, 0x4CAF50);
	noButton.x = 150;
	noButton.y = 80;
	self.addChild(noButton);
	self.setCallbacks = function (onYes, onNo) {
		yesButton.up = function () {
			tween(yesButton.children[0], {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (onYes) {
				onYes();
			}
			self.visible = false; // Hide dialog
		};
		noButton.up = function () {
			tween(noButton.children[0], {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (onNo) {
				onNo();
			}
			self.visible = false; // Hide dialog
		};
	};
	return self;
});
var SpinWheel = Container.expand(function () {
	var self = Container.call(this);
	// Constants
	var WHEEL_RADIUS = 400;
	var NUM_SEGMENTS = 8;
	var SPIN_DURATION = 5000;
	var PRIZES = [{
		name: "100 coins",
		value: 100,
		color: 0xf44336,
		icon: "๐ฐ"
	}, {
		name: "200 coins",
		value: 200,
		color: 0x2196f3,
		icon: "๐ฐ"
	}, {
		name: "50 followers",
		value: 50,
		color: 0x4caf50,
		isFollowers: true,
		icon: "๐ฅ"
	}, {
		name: "500 coins",
		value: 500,
		color: 0xffeb3b,
		icon: "๐ฐ"
	}, {
		name: "25 followers",
		value: 25,
		color: 0xff9800,
		isFollowers: true,
		icon: "๐ฅ"
	}, {
		name: "150 coins",
		value: 150,
		color: 0x9c27b0,
		icon: "๐ฐ"
	}, {
		name: "300 coins",
		value: 300,
		color: 0x00bcd4,
		icon: "๐ฐ"
	}, {
		name: "75 followers",
		value: 75,
		color: 0x8bc34a,
		isFollowers: true,
		icon: "๐ฅ"
	}];
	// Wheel outer ring
	var wheelRim = self.attachAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: WHEEL_RADIUS * 2 + 20,
		height: WHEEL_RADIUS * 2 + 20,
		color: 0x222222
	});
	// Wheel background - use actual wheel asset
	var wheelBackground = self.attachAsset('Wheel', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: WHEEL_RADIUS * 2,
		height: WHEEL_RADIUS * 2
	});
	// Create wheel segments container
	var wheelSegmentsContainer = new Container();
	self.addChild(wheelSegmentsContainer);
	// Create wheel segments
	var segments = [];
	for (var i = 0; i < NUM_SEGMENTS; i++) {
		var segment = new Container();
		var angle = i / NUM_SEGMENTS * Math.PI * 2;
		// Calculate segment positions
		var segmentAngle = Math.PI * 2 / NUM_SEGMENTS;
		// Position the segment container
		segment.rotation = angle;
		// Prize icon removed
		// Segment text removed
		// Value text removed
		wheelSegmentsContainer.addChild(segment);
		segments.push(segment);
	}
	// Add decorative outer dots
	for (var i = 0; i < NUM_SEGMENTS * 2; i++) {
		var dotAngle = i / (NUM_SEGMENTS * 2) * Math.PI * 2;
		var dot = LK.getAsset('centerCircle', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 15,
			height: 15,
			color: 0xFFD700 // Gold dots
		});
		dot.x = Math.cos(dotAngle) * (WHEEL_RADIUS + 10);
		dot.y = Math.sin(dotAngle) * (WHEEL_RADIUS + 10);
		self.addChild(dot);
	}
	// Create center point with decorative rings
	var centerPointOuter = LK.getAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 80,
		height: 80,
		color: 0xFFD700 // Gold
	});
	self.addChild(centerPointOuter);
	var centerPoint = LK.getAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 60,
		height: 60,
		color: 0xFF5733 // Orange-red
	});
	self.addChild(centerPoint);
	// Add pointer (indicator)
	var pointerBase = LK.getAsset('centerCircle', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 60,
		height: 60,
		color: 0xFF0000
	});
	pointerBase.y = -WHEEL_RADIUS - 20;
	self.addChild(pointerBase);
	var pointer = LK.getAsset('Girrafe', {
		anchorX: 0.5,
		anchorY: 0,
		width: 40,
		height: 80,
		color: 0xFF0000
	});
	pointer.y = -WHEEL_RADIUS - 50;
	self.addChild(pointer);
	// Add close button
	var closeButton = new Button("Close", 200, 80, 0xff5555);
	closeButton.y = WHEEL_RADIUS + 100;
	self.addChild(closeButton);
	// Add result text (initially hidden)
	var resultText = new Text2("", {
		size: 60,
		fill: 0xFFD700
	});
	resultText.anchor.set(0.5, 0.5);
	resultText.y = WHEEL_RADIUS + 180;
	resultText.alpha = 0;
	self.addChild(resultText);
	// Spinning state
	var isSpinning = false;
	// Spin the wheel
	self.spin = function (callback) {
		if (isSpinning) {
			return;
		}
		isSpinning = true;
		// Reset result text
		resultText.alpha = 0;
		// Play the click sound for the spin action initiation (if it's distinct from button press)
		LK.getSound('click').play(); //{gT} // This was the original second click sound, kept for fidelity
		// Play spinning sound as looping music
		LK.playMusic('SpinLoopMusic', {
			loop: true
		});
		// Determine winning segment (random)
		var targetSegment = Math.floor(Math.random() * NUM_SEGMENTS);
		var targetAngle = targetSegment / NUM_SEGMENTS * Math.PI * 2;
		// Always use the same fast speed and number of rotations
		var extraRotations = 8; // Always 8 full rotations for fast consistent speed
		var finalRotation = extraRotations * Math.PI * 2 + targetAngle;
		// Animate the wheel spinning (target the wheelBackground now)
		tween(wheelBackground, {
			rotation: finalRotation
		}, {
			duration: 2200,
			// Fast spin: 2.2 seconds
			easing: tween.easeOutCubic,
			onFinish: function onFinish() {
				isSpinning = false;
				// Show result
				var prize = PRIZES[targetSegment];
				resultText.setText("You won: " + prize.name + "!");
				// Flash the pointer
				tween(pointerBase, {
					scaleX: 1.5,
					scaleY: 1.5,
					alpha: 0.7
				}, {
					duration: 200,
					yoyo: true,
					repeat: 3
				});
				// Fade in result text
				tween(resultText, {
					alpha: 1
				}, {
					duration: 500
				});
				// Stop the spin music and resume background music
				LK.stopMusic(); // Stops SpinLoopMusic
				LK.playMusic('bgMusic', {
					fade: {
						start: 0,
						end: 0.4,
						duration: 1000
					}
				}); // Resume background music
				// Play reward sound
				LK.getSound('coin').play();
				// Callback with prize
				if (callback) {
					callback(prize);
				}
			}
		});
	};
	// Set close button callback
	self.setCloseCallback = function (callback) {
		closeButton.up = function () {
			tween(closeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	return self;
});
var TabSystem = Container.expand(function (tabs) {
	var self = Container.call(this);
	var tabButtons = [];
	var tabContents = [];
	var activeTab = 0;
	// Create tab buttons
	for (var i = 0; i < tabs.length; i++) {
		var tab = tabs[i];
		var index = i;
		var tabButton = new Button(tab.name, 240, 80, 0x607D8B);
		tabButton.x = i * 320 - (tabs.length - 1) * 160;
		tabButton.y = -1300 / 2 + 30;
		tabButton.up = function (x, y, obj) {
			tween(tabButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			setActiveTab(index);
		};
		self.addChild(tabButton);
		tabButtons.push(tabButton);
		// Create and add content container
		var content = tab.content;
		content.visible = i === 0; // Only first tab visible initially
		self.addChild(content);
		tabContents.push(content);
	}
	function setActiveTab(index) {
		if (index === activeTab) {
			return;
		}
		// Update button colors
		tabButtons[activeTab].setColor(0x607D8B);
		tabButtons[index].setColor(0x2196F3);
		// Hide old content, show new
		tabContents[activeTab].visible = false;
		tabContents[index].visible = true;
		activeTab = index;
	}
	// Initialize first tab as active
	setActiveTab(0);
	return self;
});
var UpgradeButton = Container.expand(function (title, description, price, level, onUpgrade) {
	var self = Container.call(this);
	var buttonShape = self.attachAsset('upgradeButton', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 420,
		height: 140
	});
	var titleText = new Text2(title, {
		size: 42,
		fill: 0xFFFFFF
	});
	titleText.anchor.set(0.5, 0);
	titleText.y = -buttonShape.height / 2 + 15;
	self.addChild(titleText);
	var descText = new Text2(description, {
		size: 30,
		fill: 0xFFFFFF
	});
	descText.anchor.set(0.5, 0);
	descText.y = -buttonShape.height / 2 + 50;
	self.addChild(descText);
	var levelText = new Text2("Level: " + level, {
		size: 34,
		fill: 0xFFD700
	});
	levelText.anchor.set(0, 1);
	levelText.x = -buttonShape.width / 2 + 15;
	levelText.y = buttonShape.height / 2 - 10;
	self.addChild(levelText);
	var priceText = new Text2("$" + price, {
		size: 36,
		fill: 0xFFD700
	});
	priceText.anchor.set(1, 1);
	priceText.x = buttonShape.width / 2 - 15;
	priceText.y = buttonShape.height / 2 - 10;
	self.addChild(priceText);
	self.updateLevel = function (newLevel) {
		levelText.setText("Level: " + newLevel);
	};
	self.updatePrice = function (newPrice) {
		priceText.setText("$" + newPrice);
	};
	self.disable = function () {
		buttonShape.tint = 0x888888;
		self.interactive = false;
	};
	self.enable = function () {
		buttonShape.tint = 0x2196F3;
		self.interactive = true;
	};
	self.down = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100
		});
		LK.getSound('click').play();
	};
	self.up = function (x, y, obj) {
		tween(buttonShape, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
		if (onUpgrade) {
			onUpgrade();
		}
	};
	return self;
});
var UpgradePanel = Container.expand(function () {
	var self = Container.call(this);
	var background = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 1800,
		height: 1300,
		color: 0xdddddd
	});
	var titleText = new Text2("Upgrade Your Channel", {
		size: 64,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0);
	titleText.y = -background.height / 2 + 50;
	self.addChild(titleText);
	var closeButton = new Button("Close", 240, 80, 0xff5555);
	closeButton.x = background.width / 2 - 130;
	closeButton.y = -background.height / 2 + 50;
	self.addChild(closeButton);
	// Add next button
	var nextButton = new Button("Next", 240, 80, 0x2196F3);
	nextButton.x = -background.width / 2 + 130;
	nextButton.y = -background.height / 2 + 50;
	self.addChild(nextButton);
	self.setCloseCallback = function (callback) {
		closeButton.up = function (x, y, obj) {
			tween(closeButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.setNextCallback = function (callback) {
		nextButton.up = function (x, y, obj) {
			tween(nextButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			if (callback) {
				callback();
			}
		};
	};
	self.addUpgradeButton = function (title, description, price, level, maxLevel, x, y, onUpgrade) {
		var button = new UpgradeButton(title, description, price, level, function () {
			if (onUpgrade) {
				onUpgrade(button);
			}
		});
		// Make upgrade buttons bigger
		button.scale.set(1.4, 1.4);
		button.x = x;
		button.y = y;
		if (maxLevel !== undefined && level >= maxLevel) {
			button.updatePrice("MAXED");
			button.disable();
		}
		self.addChild(button);
		return button;
	};
	return self;
});
var ViralMemeDialog = Container.expand(function () {
	var self = Container.call(this);
	// Background overlay
	var overlay = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0x000000,
		alpha: 0.8
	});
	// Dialog box
	var dialog = self.attachAsset('memeCanvas', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 800,
		height: 500,
		color: 0xffffff
	});
	// Viral meme text
	var viralText = new Text2("YOUR MEME WENT VIRAL!!!", {
		size: 60,
		fill: 0x00ff00,
		// Green color
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	viralText.anchor.set(0.5, 0.5);
	viralText.y = -80;
	self.addChild(viralText);
	// Earnings text
	var earningsText = new Text2("", {
		size: 50,
		fill: 0x000000,
		align: 'center',
		wordWrap: true,
		wordWrapWidth: 700
	});
	earningsText.anchor.set(0.5, 0.5);
	earningsText.y = 50;
	self.addChild(earningsText);
	// OK button
	var okButton = new Button("OK", 300, 100, 0x4CAF50);
	okButton.y = 180;
	self.addChild(okButton);
	self.setCloseCallback = function (callback) {
		okButton.up = function () {
			tween(okButton, {
				scaleX: 1,
				scaleY: 1
			}, {
				duration: 100
			});
			LK.getSound('click').play();
			if (callback) {
				callback();
			}
			self.visible = false;
		};
	};
	self.showResult = function (followersGained, moneyGained) {
		earningsText.setText("You earned " + followersGained + " followers and $" + moneyGained + "!");
		self.visible = true;
	};
	return self;
});
var WheelScreen = Container.expand(function () {
	var self = Container.call(this);
	// Add background
	var background = self.attachAsset('studioBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 2048,
		height: 2732,
		color: 0xdddddd
	});
	// Add title
	var titleText = new Text2("Spin the Wheel for Prizes!", {
		size: 80,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0.5);
	titleText.y = -900;
	self.addChild(titleText);
	// Create spin wheel
	var spinWheel = new SpinWheel();
	spinWheel.y = 0; // Center of screen
	self.addChild(spinWheel);
	// Create spin button
	// Create spin button
	var spinButton = new Button("SPIN!", 300, 100, 0x4CAF50);
	spinButton.y = 600;
	self.addChild(spinButton);
	// Add cost text below the spin button
	var costText = new Text2("Cost: 50 Followers", {
		size: 40,
		fill: 0xFF5733 // Orange-red color for emphasis
	});
	costText.anchor.set(0.5, 0.5);
	costText.y = spinButton.y + spinButton.height / 2 + 30; // Position below the button
	self.addChild(costText);
	// Set spin button callback
	spinButton.up = function () {
		// Reset button scale first
		tween(spinButton, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
		// Check if player has enough followers
		if (followers >= 50) {
			// Deduct followers
			followers -= 50;
			updateStorage(); // Update stored follower count
			// Disable spin button during spin
			spinButton.visible = false;
			// Spin the wheel
			spinWheel.spin(function (prize) {
				// Process the prize
				if (self.onPrizeWon) {
					self.onPrizeWon(prize);
				}
				// Re-enable spin button after result is shown and delay
				LK.setTimeout(function () {
					spinButton.visible = true;
				}, 1500); // Shortened delay slightly as spin ends sooner now
			});
		} else {
			// Not enough followers, flash the cost text
			LK.effects.flashObject(costText, 0xff0000, 500); // Flash red
			// Optionally play a 'fail' sound
			// LK.getSound('fail').play();
		}
	};
	// Store callback for prize handling
	self.setPrizeCallback = function (callback) {
		self.onPrizeWon = callback;
	};
	// Set close callback
	self.setCloseCallback = function (callback) {
		spinWheel.setCloseCallback(callback);
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
// Added for looping spin sound
// Game variables
if (!tween.stopTweensOf) {
	tween.stopTweensOf = function (target) {
		// Simple implementation of stopTweensOf if not provided by the tween library
		if (target && target._tweens) {
			target._tweens.forEach(function (t) {
				t.stop();
			});
			target._tweens = [];
		}
	};
}
var money = storage.money || 100;
var followers = storage.followers || 0;
var memeLevel = storage.memeLevel || 1;
var studioLevel = storage.studioLevel || 1;
var staffLevel = storage.staffLevel || 0;
var marketingLevel = storage.marketingLevel || 0;
var autoEarningRate = storage.autoEarningRate || 0;
var lastPlayed = storage.lastPlayed || Date.now();
var membershipLevel = storage.membershipLevel || 0;
var equipmentLevel = storage.equipmentLevel || 1;
var rentTimeLevel = storage.rentTimeLevel || 0; // Initialize rentTimeLevel
var gems = storage.gems || 0; // Initialize gems
// Upgrade prices
var upgradePrices = {
	quality: parseInt(calculateUpgradeCost(1, memeLevel)) || 100,
	membership: 500,
	staff: parseInt(calculateUpgradeCost(3, staffLevel)) || 300,
	equipment: parseInt(calculateUpgradeCost(2, studioLevel)) || 200
};
// Function to start Puzzle game
function startPuzzleGame() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new PuzzleGame or reset existing one
	if (!puzzleGame) {
		puzzleGame = new PuzzleGame();
		puzzleGame.x = 2048 / 2;
		puzzleGame.y = 2732 / 2;
		game.addChild(puzzleGame);
	} else {
		// Remove existing game and create a new one
		game.removeChild(puzzleGame);
		puzzleGame = new PuzzleGame();
		puzzleGame.x = 2048 / 2;
		puzzleGame.y = 2732 / 2;
		game.addChild(puzzleGame);
	}
	// Show the game
	puzzleGame.visible = true;
	// Set callback for when game finishes
	puzzleGame.setFinishCallback(function () {
		// Hide Puzzle game
		puzzleGame.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards for completing video game - puzzle games give balanced rewards
		var videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 6.4; // Doubled from 3.2 to 6.4
		var videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 3.5;
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play coin sound
		LK.getSound('coin').play();
		gems += 1; // Award 1 gem for winning
		// Update storage
		updateStorage();
	});
	// Set callback for return home button
	puzzleGame.setReturnHomeCallback(function () {
		// Hide Puzzle game
		puzzleGame.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
}
// Upgrade levels
var upgradeLevels = {
	quality: memeLevel,
	membership: membershipLevel,
	staff: staffLevel,
	equipment: studioLevel
};
// Constants
var MEME_BASE_INCOME = 10;
var MEME_BASE_FOLLOWERS = 5;
var UPGRADE_BASE_COST = 100;
var AUTO_EARNINGS_INTERVAL = 20000; // ms (20 seconds)
var OFFLINE_EARNINGS_MAX_HOURS = 24;
// Calculate offline earnings
function calculateOfflineEarnings() {
	var now = Date.now();
	var timeDiff = now - lastPlayed;
	var secondsDiff = timeDiff / 1000;
	var periods = secondsDiff / 20; // How many 20-second periods elapsed
	// Cap at max hours (converted to 20-second periods)
	var maxPeriods = OFFLINE_EARNINGS_MAX_HOURS * 60 * 60 / 20;
	if (periods > maxPeriods) {
		periods = maxPeriods;
	}
	// Only calculate if we have auto earnings
	if (autoEarningRate > 0 && periods > 0) {
		var hourlyRate = autoEarningRate;
		var periodRate = hourlyRate / 180; // Convert from hourly to per 20-second period
		var earnings = Math.floor(periodRate * periods);
		money += earnings;
		// Show welcome back message here if needed
		console.log("Welcome back! You earned $" + earnings + " while away.");
	}
	lastPlayed = now;
	storage.lastPlayed = now;
}
// Calculate costs for upgrades
function calculateUpgradeCost(baseLevel, currentLevel) {
	return Math.floor(UPGRADE_BASE_COST * baseLevel * Math.pow(1.5, currentLevel));
}
// Update storage
function updateStorage() {
	storage.money = money;
	storage.followers = followers;
	storage.memeLevel = memeLevel;
	storage.studioLevel = studioLevel;
	storage.staffLevel = staffLevel;
	storage.marketingLevel = marketingLevel;
	storage.autoEarningRate = autoEarningRate;
	storage.membershipLevel = membershipLevel;
	storage.rentTimeLevel = rentTimeLevel; // Add rentTimeLevel to storage
	storage.gems = gems; // Add gems to storage
}
// Calculate meme creation result
function createMeme() {
	// Base earnings affected by meme quality and studio level
	var earnings = MEME_BASE_INCOME * memeLevel * (1 + studioLevel * 0.5);
	// Followers gained affected by marketing level
	var newFollowers = Math.floor(MEME_BASE_FOLLOWERS * (1 + marketingLevel * 0.5));
	// Boost from followers (virality factor)
	var followerBoost = 1 + followers / 1000;
	earnings = Math.floor(earnings * followerBoost);
	// Apply membership bonus if active
	if (membershipLevel > 0) {
		earnings = Math.floor(earnings * 1.5);
	}
	// Apply randomness for variability (80% to 120% of calculated value)
	var randomFactor = 0.8 + Math.random() * 0.4;
	earnings = Math.floor(earnings * randomFactor);
	// Small chance for meme to go viral
	if (Math.random() < 0.05) {
		earnings *= 5;
		newFollowers *= 3;
		console.log("Your meme went viral!");
		// Show viral meme dialog
		viralMemeDialog.showResult(newFollowers, earnings);
		// Trigger confetti burst
		for (var i = 0; i < 100; i++) {
			// Create 100 confetti particles
			var confetti = new ConfettiParticle();
			confetti.x = 2048 / 2; // Start from the top center
			confetti.y = 0;
			game.addChild(confetti);
			confettiParticles.push(confetti);
		}
		// Skip the rest of the logic for this case
		return {
			earnings: earnings,
			followers: newFollowers
		};
	}
	// 2% chance of a controversial warning
	if (Math.random() < 0.02) {
		console.log("Controversial meme!");
		var percentageLost = 0.05; // Fixed 5% loss
		var lostFollowers = Math.floor(followers * percentageLost); // Calculate followers lost based on current followers
		newFollowers = -lostFollowers; // Update newFollowers to reflect the loss
		controversialWarningDialog.visible = true;
		controversialWarningDialog.children[2].setText("You uploaded a meme that your fans didn't like :(" + "\n" + "You lost " + Math.abs(newFollowers) + " followers!"); // Update warning text
	} else if (membershipLevel === 1 && Math.random() < 0.1) {
		// 10% chance for donations if upgrade is purchased
		var donationAmount = Math.floor(Math.random() * 100) + 50; // Random donation between $50 and $150
		earnings += donationAmount;
		console.log("You received a donation of $" + donationAmount + "!");
		// Show donation notification
		var notification = new DonationNotification();
		notification.setAmount(donationAmount);
		notification.y = 400; // Position on side of screen
		game.addChild(notification);
		notification.animate();
	} else {
		// Apply randomness for variability (80% to 120% of calculated value)
		var randomFactor = 0.8 + Math.random() * 0.4;
		earnings = Math.floor(earnings * randomFactor);
	}
	// Update game state
	money += earnings;
	followers += newFollowers;
	// Update storage
	updateStorage();
	// Return results
	return {
		earnings: earnings,
		followers: newFollowers
	};
}
// Update auto earnings rate based on staff
function updateAutoEarningRate() {
	autoEarningRate = staffLevel * 250 * memeLevel * studioLevel; // MASSIVELY increased earning rate
	storage.autoEarningRate = autoEarningRate;
}
// Create game selector screen (initially hidden)
var gameSelector = new GameSelector();
gameSelector.x = 2048 / 2;
gameSelector.y = 2732 / 2;
gameSelector.visible = false;
game.addChild(gameSelector);
// Function to show game selector
function showGameSelector() {
	gameSelector.visible = true;
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Update matching game lock status based on current follower count
	gameSelector.updateLockStatus(followers);
}
// Function to hide game selector and show the main screen
function hideGameSelector() {
	gameSelector.visible = false;
	memeStudio.visible = true;
	statsContainer.visible = true;
	studioContent.visible = true;
}
// Set callbacks for game selector
gameSelector.setGameCallback(function (gameType) {
	if (gameType === "connect4") {
		startConnect4Game();
	} else if (gameType === "matching") {
		startMatchingGame();
	} else if (gameType === "puzzle") {
		startPuzzleGame();
	} else if (gameType === "cupShuffle") {
		startCupShuffleGame();
	}
});
gameSelector.setBackCallback(function () {
	hideGameSelector();
});
// CupShuffle game instance (initially null)
var cupShuffleGame = null;
// Function to start CupShuffle game
function startCupShuffleGame() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new CupShuffle game or reset existing one
	if (!cupShuffleGame) {
		cupShuffleGame = new CupShuffleGame();
		cupShuffleGame.x = 2048 / 2;
		cupShuffleGame.y = 2732 / 2;
		game.addChild(cupShuffleGame);
	} else {
		// Remove existing game and create a new one
		game.removeChild(cupShuffleGame);
		cupShuffleGame = new CupShuffleGame();
		cupShuffleGame.x = 2048 / 2;
		cupShuffleGame.y = 2732 / 2;
		game.addChild(cupShuffleGame);
	}
	// Show the game
	cupShuffleGame.visible = true;
	// Set callback for return home button
	cupShuffleGame.setReturnHomeCallback(function () {
		// Hide CupShuffle game
		cupShuffleGame.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
	// Set callback for when game finishes
	cupShuffleGame.setFinishCallback(function (result) {
		// Hide CupShuffle game
		cupShuffleGame.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards based on game result
		var videoEarnings = 0;
		var videoFollowers = 0;
		var gemsAwarded = 0;
		if (result === 'win') {
			videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 8; // High earnings for winning
			videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 5; // High followers for winning
			gemsAwarded = 2; // Award 2 gems for winning
		} else {
			videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 2; // Lower earnings for losing
			videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 1; // Lower followers for losing
			gemsAwarded = 0; // No gems for losing
		}
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		gems += gemsAwarded;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play sound
		if (result === 'win') {
			LK.getSound('coin').play(); // Play coin sound for winning
		} else {
			// Optionally play a different sound for losing
			// LK.getSound('lose').play();
		}
		// Update storage
		updateStorage();
	});
}
// Connect4 game instance (initially null)
var connect4Game = null;
// Function to start Connect4 game
function startConnect4Game() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new Connect4 game or reset existing one
	if (!connect4Game) {
		connect4Game = new Connect4Game();
		connect4Game.x = 2048 / 2;
		connect4Game.y = 2732 / 2;
		game.addChild(connect4Game);
	} else {
		// Remove existing game and create a new one
		game.removeChild(connect4Game);
		connect4Game = new Connect4Game();
		connect4Game.x = 2048 / 2;
		connect4Game.y = 2732 / 2;
		game.addChild(connect4Game);
	}
	// Show the game
	connect4Game.visible = true;
	// Set callback for return home button
	connect4Game.setReturnHomeCallback(function () {
		// Hide Connect4 game
		connect4Game.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
	// Set callback for when game finishes
	connect4Game.setFinishCallback(function () {
		// Hide Connect4 game
		connect4Game.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards for completing video game
		var videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 6; // Doubled from 3 to 6
		var videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 3;
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play coin sound
		LK.getSound('coin').play();
		gems += 1; // Award 1 gem for winning
		// Update storage
		updateStorage();
	});
}
// Create UI elements
// Main studio
// Create rent button using the rent asset
var rentButton = new Container();
var rentButtonAsset = rentButton.attachAsset('Rent', {
	anchorX: 0.5,
	anchorY: 0.5,
	width: 600,
	height: 180
});
var rentButtonText = new Text2("Pay Rent: $200", {
	size: 54,
	fill: 0xFFFFFF
});
rentButtonText.anchor.set(0.5, 0.5);
rentButton.addChild(rentButtonText);
rentButton.x = 2048 / 2;
rentButton.y = 300;
// Create rent bar under the rent button
var rentBar = new Container();
var rentBarAsset = rentBar.attachAsset('Girrafe', {
	anchorX: 0.5,
	anchorY: 0.5,
	width: 600,
	height: 100
});
rentBar.x = 2048 / 2;
rentBar.y = 380;
game.addChild(rentBar);
// Animate the rent bar slowly draining to the left
tween(rentBarAsset, {
	scaleX: 0,
	x: -300
}, {
	duration: 60000,
	// 60 seconds (one minute) duration for slow drain
	easing: tween.linear,
	onFinish: function onFinish() {
		// When rent bar runs out, lose half the money
		money = Math.floor(money / 2);
		// Reset the rent bar after losing money
		tween.stopTweensOf(rentBarAsset);
		rentBarAsset.scaleX = 1;
		rentBarAsset.x = 0;
		// Calculate the next duration based on the upgraded rent time
		var nextDuration = 60000 + rentTimeLevel * 10000;
		// Start draining again
		tween(rentBarAsset, {
			scaleX: 0,
			x: -300
		}, {
			duration: nextDuration,
			easing: tween.linear,
			onFinish: arguments.callee
		});
		// Update storage after losing money
		updateStorage();
		// Play sound to indicate rent is due
		LK.getSound('click').play();
	}
});
// Calculate the initial rent duration based on the stored rentTimeLevel
var initialRentDuration = 60000 + rentTimeLevel * 10000;
// Start the initial rent bar tween
tween(rentBarAsset, {
	scaleX: 0,
	x: -300
}, {
	duration: initialRentDuration,
	easing: tween.linear,
	onFinish: rentBar.onFinish // Reference the onFinish function defined above
});
rentButton.down = function (x, y, obj) {
	tween(rentButtonAsset, {
		scaleX: 0.95,
		scaleY: 0.95
	}, {
		duration: 100
	});
	LK.getSound('click').play();
};
rentButton.up = function (x, y, obj) {
	tween(rentButtonAsset, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 100
	});
	if (money >= 200) {
		money -= 200;
		LK.getSound('coin').play();
		tween(rentButton, {
			y: 280,
			alpha: 0.5
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(rentButton, {
					y: 300,
					alpha: 1
				}, {
					duration: 300
				});
			}
		});
		// Reset the rent bar by creating a new tween
		// Stop any existing tween on the rent bar
		tween.stopTweensOf(rentBarAsset);
		// Reset the bar's scale and position
		rentBarAsset.scaleX = 1;
		rentBarAsset.x = 0;
		// Start a new tween to drain it again
		tween(rentBarAsset, {
			scaleX: 0,
			x: -300
		}, {
			duration: 60000,
			// 60 seconds (one minute) duration for slow drain
			easing: tween.linear
		});
		updateStorage();
	}
};
game.addChild(rentButton);
var memeStudio = new MemeStudio();
memeStudio.x = 2048 / 2;
memeStudio.y = 2732 / 2;
game.addChild(memeStudio);
// Initialize with current meme quality
memeStudio.updateMemeQuality(memeLevel);
// Reset Progress button
var resetButton = new Button("Reset Progress", 300, 100, 0xff5555);
resetButton.x = 200; // Position in bottom left
resetButton.y = 2732 - 100; // Position near bottom
resetButton.up = function (x, y, obj) {
	tween(resetButton, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 100
	});
	// Show the confirmation dialog instead of resetting immediately
	resetConfirmationDialog.visible = true;
};
game.addChild(resetButton);
// Stats display
// Create reset confirmation dialog (initially hidden)
var resetConfirmationDialog = new ResetConfirmation();
resetConfirmationDialog.x = 2048 / 2;
resetConfirmationDialog.y = 2732 / 2;
resetConfirmationDialog.visible = false;
game.addChild(resetConfirmationDialog);
// Set callbacks for the confirmation dialog
resetConfirmationDialog.setCallbacks(function onYes() {
	// This code runs if the player confirms the reset
	// Reset all progress
	money = 100;
	followers = 0;
	memeLevel = 1;
	studioLevel = 1;
	staffLevel = 0;
	marketingLevel = 0;
	autoEarningRate = 0;
	membershipLevel = 0;
	equipmentLevel = 1; // Reset equipment level as well
	rentTimeLevel = 0; // Reset rent time level
	gems = 0; // Reset gems
	// Reset upgrade levels
	upgradeLevels.quality = memeLevel;
	upgradeLevels.membership = membershipLevel;
	upgradeLevels.staff = staffLevel;
	upgradeLevels.equipment = studioLevel;
	// Reset upgrade prices
	upgradePrices.quality = calculateUpgradeCost(1, memeLevel);
	upgradePrices.membership = 500;
	upgradePrices.staff = calculateUpgradeCost(3, staffLevel);
	upgradePrices.equipment = calculateUpgradeCost(2, studioLevel);
	// Update storage
	updateStorage();
	// Update UI
	memeStudio.updateMemeQuality(memeLevel);
	// Play sound
	LK.getSound('levelUp').play();
}, function onNo() {
	// This code runs if the player cancels the reset
	console.log("Reset cancelled.");
});
// Stats display
var statsContainer = new Container();
statsContainer.x = 2048 / 2;
statsContainer.y = 180;
game.addChild(statsContainer);
var moneyIcon = LK.getAsset('moneyIcon', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: -600
});
statsContainer.addChild(moneyIcon);
var moneyText = new Text2("$" + money, {
	size: 54,
	fill: 0x000000
});
moneyText.anchor.set(0, 0.5);
moneyText.x = -570;
statsContainer.addChild(moneyText);
var followerIcon = LK.getAsset('followerIcon', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 400
});
statsContainer.addChild(followerIcon);
var followerText = new Text2(followers + " followers", {
	size: 54,
	fill: 0x000000
});
followerText.anchor.set(0, 0.5);
followerText.x = 430;
statsContainer.addChild(followerText);
var gemsIcon = LK.getAsset('Gems', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: -100,
	width: 80,
	// Reduced width
	height: 80 // Reduced height
});
statsContainer.addChild(gemsIcon);
var gemsText = new Text2(gems + " gems", {
	size: 54,
	fill: 0x000000
});
gemsText.anchor.set(0, 0.5);
gemsText.x = -70;
statsContainer.addChild(gemsText);
var autoEarningText = new Text2("", {
	size: 36,
	fill: 0x333333
});
autoEarningText.anchor.set(0.5, 0.5);
autoEarningText.y = 80;
statsContainer.addChild(autoEarningText);
// Create upgrade tab contents
var studioContent = new Container();
// Meme Quality Upgrade button removed
// No studio equipment upgrade
// Staff Upgrade removed
// Studio content is displayed directly without tabs
studioContent.x = 2048 / 2;
studioContent.y = 2732 / 2 + 400;
game.addChild(studioContent);
// Set callback for create meme button
memeStudio.setCreateButtonCallback(function () {
	var result = createMeme();
	memeStudio.showEarnings(result.earnings, result.followers);
	LK.getSound('coin').play();
});
// Create upgrade panel (initially hidden)
var upgradePanel = new UpgradePanel();
upgradePanel.x = 2048 / 2;
upgradePanel.y = 2732 / 2;
upgradePanel.visible = false;
game.addChild(upgradePanel);
// Function to show upgrade panel
function showUpgradePanel() {
	upgradePanel.visible = true;
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Add meme quality upgrade button
	var memeQualityUpgrade = upgradePanel.addUpgradeButton("Meme Quality", "Better memes = more $$$", upgradePrices.quality, upgradeLevels.quality, 10,
	// Max level
	0,
	// X position
	-300,
	// Y position
	function (button) {
		if (money >= upgradePrices.quality) {
			// Pay for upgrade
			money -= upgradePrices.quality;
			// Increase level
			memeLevel++;
			upgradeLevels.quality = memeLevel;
			// Update studio
			memeStudio.updateMemeQuality(memeLevel);
			// Update price
			upgradePrices.quality = calculateUpgradeCost(1, memeLevel);
			button.updateLevel(memeLevel);
			button.updatePrice(upgradePrices.quality);
			// Check if max level reached
			if (memeLevel >= 10) {
				button.updatePrice("MAXED");
				button.disable();
			}
			// Play level up sound
			LK.getSound('levelUp').play();
			// Update storage
			updateStorage();
		}
	});
	// Add staff upgrade button
	var staffUpgradeButton = upgradePanel.addUpgradeButton("Hire Staff", "Staff generates income", upgradePrices.staff, upgradeLevels.staff, 10,
	// Max level
	0,
	// X position
	0,
	// Y position
	function (button) {
		if (money >= upgradePrices.staff) {
			// Pay for upgrade
			money -= upgradePrices.staff;
			// Increase level
			staffLevel++;
			upgradeLevels.staff = staffLevel;
			// Update auto earning rate
			updateAutoEarningRate();
			// Update price
			upgradePrices.staff = calculateUpgradeCost(3, staffLevel);
			button.updateLevel(staffLevel);
			button.updatePrice(upgradePrices.staff);
			// Check if max level reached
			if (staffLevel >= 10) {
				button.updatePrice("MAXED");
				button.disable();
			}
			// Play level up sound
			LK.getSound('levelUp').play();
			// Update storage
			updateStorage();
		}
	});
	// Add Increase Rent Time upgrade button
	var rentTimeUpgradeButton = upgradePanel.addUpgradeButton("Increase Rent Time", "Increases time before rent is due", calculateUpgradeCost(4, rentTimeLevel), rentTimeLevel, 10,
	// Max level
	0,
	// X position
	300,
	// Y position
	function (button) {
		if (money >= calculateUpgradeCost(4, rentTimeLevel)) {
			money -= calculateUpgradeCost(4, rentTimeLevel);
			rentTimeLevel++;
			// Stop the current rent bar tween
			tween.stopTweensOf(rentBarAsset);
			// Reset and restart the rent bar tween with increased duration
			var newDuration = 60000 + rentTimeLevel * 10000; // Increase duration by 10 seconds per level
			rentBarAsset.scaleX = 1;
			rentBarAsset.x = 0;
			tween(rentBarAsset, {
				scaleX: 0,
				x: -300
			}, {
				duration: newDuration,
				easing: tween.linear,
				onFinish: arguments.callee
			});
			button.updateLevel(rentTimeLevel);
			button.updatePrice(calculateUpgradeCost(4, rentTimeLevel));
			if (rentTimeLevel >= 10) {
				button.updatePrice("MAXED");
				button.disable();
			}
			LK.getSound('levelUp').play();
			updateStorage();
		}
	});
	// Set up next callback for upgrade panel
	upgradePanel.setNextCallback(function () {
		hideUpgradePanel(); // Hide current panel
		showUpgradePanel2(); // Show the next panel
	});
}
// Function to hide upgrade panel
function hideUpgradePanel() {
	upgradePanel.visible = false;
	memeStudio.visible = true;
	statsContainer.visible = true;
	studioContent.visible = true;
}
// Set up close callback for upgrade panel
upgradePanel.setCloseCallback(function () {
	hideUpgradePanel();
});
// Set callback for create video button
memeStudio.setCreateVideoButtonCallback(function () {
	showGameSelector();
});
// Set callback for upgrades button
memeStudio.setUpgradesButtonCallback(function () {
	showUpgradePanel();
});
// Create a second upgrade panel (initially hidden)
var upgradePanel2 = new UpgradePanel();
upgradePanel2.x = 2048 / 2;
upgradePanel2.y = 2732 / 2;
upgradePanel2.visible = false;
game.addChild(upgradePanel2);
// Function to show the second upgrade panel
function showUpgradePanel2() {
	upgradePanel2.visible = true;
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Add specific upgrades for the second panel here
	// Add Donations upgrade button
	var donationsUpgradeButton = upgradePanel2.addUpgradeButton("Donations", "Chance for viewers to donate money (Costs 2 Diamonds)", 2,
	// Cost in gems
	membershipLevel,
	// Reusing membershipLevel for tracking if purchased (0 or 1)
	1,
	// Max level is 1 (can only buy once)
	0,
	// X position
	-300,
	// Y position
	function (button) {
		if (gems >= 2 && membershipLevel === 0) {
			// Check if player has enough gems and hasn't bought it
			// Pay for upgrade
			gems -= 2;
			// Set level to 1 (purchased)
			membershipLevel = 1; // Use membershipLevel to indicate donations purchased
			// Update price
			button.updatePrice("MAXED");
			button.disable();
			// Play level up sound
			LK.getSound('levelUp').play();
			// Update storage
			updateStorage();
		}
	});
	// Check if donations are already purchased and disable the button
	if (membershipLevel >= 1) {
		donationsUpgradeButton.updatePrice("MAXED");
		donationsUpgradeButton.disable();
	}
	// Add a 'Previous' button to go back to the first panel
	var previousButton = new Button("Previous", 240, 80, 0x607D8B);
	previousButton.x = -upgradePanel2.children[0].width / 2 + 130; // Position near the close button
	previousButton.y = -upgradePanel2.children[0].height / 2 + 50;
	upgradePanel2.addChild(previousButton);
	// Set callback for the previous button
	previousButton.up = function (x, y, obj) {
		tween(previousButton, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100
		});
		hideUpgradePanel2(); // Hide current panel
		showUpgradePanel(); // Show the previous panel
	};
	// Remove the 'Next' button from this panel
	upgradePanel2.setNextCallback(null); // Remove the next button callback
}
// Function to hide the second upgrade panel
function hideUpgradePanel2() {
	upgradePanel2.visible = false;
	memeStudio.visible = true;
	statsContainer.visible = true;
	studioContent.visible = true;
}
// Initialize wheel screen (initially null)
var wheelScreen = null;
// Function to show wheel screen
function showWheelScreen() {
	// Hide main elements
	memeStudio.visible = false;
	statsContainer.visible = false;
	studioContent.visible = false;
	// Create wheel screen if it doesn't exist
	if (!wheelScreen) {
		wheelScreen = new WheelScreen();
		wheelScreen.x = 2048 / 2;
		wheelScreen.y = 2732 / 2;
		game.addChild(wheelScreen);
		// Set prize callback
		wheelScreen.setPrizeCallback(function (prize) {
			// Process the prize
			if (prize.isFollowers) {
				// Add followers
				followers += prize.value;
				// Show followers gained
				memeStudio.showEarnings(0, prize.value);
			} else {
				// Add money
				money += prize.value;
				// Show money gained
				memeStudio.showEarnings(prize.value, 0);
			}
			// Update storage
			updateStorage();
		});
		// Set close callback
		wheelScreen.setCloseCallback(function () {
			// Hide wheel screen
			wheelScreen.visible = false;
			// Show main elements
			memeStudio.visible = true;
			statsContainer.visible = true;
			studioContent.visible = true;
		});
	} else {
		// Show existing wheel screen
		wheelScreen.visible = true;
	}
}
// Set callback for spin button
memeStudio.setSpinButtonCallback(function () {
	showWheelScreen();
	LK.getSound('click').play();
});
// Start background music
LK.playMusic('bgMusic', {
	fade: {
		start: 0,
		end: 0.4,
		duration: 1000
	}
});
// Calculate any offline earnings when game starts
calculateOfflineEarnings();
// Set up auto earning timer
var lastAutoEarningTime = Date.now();
// Update UI with current values
function updateUI() {
	moneyText.setText("$" + Math.floor(money));
	followerText.setText(Math.floor(followers) + " followers");
	gemsText.setText(Math.floor(gems) + " gems");
	if (autoEarningRate > 0) {
		autoEarningText.setText("Auto earning: $" + Math.floor(autoEarningRate / 180) + " / 20 seconds");
	} else {
		autoEarningText.setText("");
	}
}
// Add game reference to global scope so it can be accessed from upgradeScreen
game.money = money;
game.memeLevel = memeLevel;
game.studioLevel = studioLevel;
game.staffLevel = staffLevel;
game.levels = upgradeLevels;
game.updateStorage = updateStorage;
game.updateAutoEarningRate = updateAutoEarningRate;
game.gems = gems; // Add gems to game reference
// Array to hold confetti particles
var confettiParticles = [];
// Main game update loop
game.update = function () {
	// Handle auto earnings
	var now = Date.now();
	var elapsed = now - lastAutoEarningTime;
	if (elapsed >= AUTO_EARNINGS_INTERVAL && autoEarningRate > 0) {
		// Calculate earnings for this period (20 seconds is 1/180th of an hour)
		var earnings = Math.floor(autoEarningRate / 180);
		if (earnings > 0) {
			money += earnings;
			lastAutoEarningTime = now;
			updateStorage();
		}
	}
	// Update UI elements
	updateUI();
	// Sync game reference values
	game.money = money;
	game.gems = gems; // Sync gems to game reference
	// Update and clean up confetti particles
	for (var i = confettiParticles.length - 1; i >= 0; i--) {
		var particle = confettiParticles[i];
		if (particle && particle.update) {
			particle.update();
			if (!particle.parent) {
				// Check if the particle has been removed from the stage
				confettiParticles.splice(i, 1);
			}
		} else if (!particle) {
			// Remove null/undefined entries
			confettiParticles.splice(i, 1);
		}
	}
	// Enable/disable upgrade buttons based on available money
	// All upgrade buttons removed
};
// MatchingGame instance (initially null)
var matchingGame = null;
// Create controversial warning dialog (initially hidden)
var controversialWarningDialog = new ControversialWarning();
controversialWarningDialog.x = 2048 / 2;
controversialWarningDialog.y = 2732 / 2;
controversialWarningDialog.visible = false;
game.addChild(controversialWarningDialog);
// Create viral meme dialog (initially hidden)
var viralMemeDialog = new ViralMemeDialog();
viralMemeDialog.x = 2048 / 2;
viralMemeDialog.y = 2732 / 2;
viralMemeDialog.visible = false;
game.addChild(viralMemeDialog);
// Set close callback for controversial warning dialog
controversialWarningDialog.setCloseCallback(function () {
	controversialWarningDialog.visible = false;
});
// Set close callback for viral meme dialog
viralMemeDialog.setCloseCallback(function () {
	viralMemeDialog.visible = false;
});
// PuzzleGame instance (initially null)
var puzzleGame = null;
// Function to start Matching game
function startMatchingGame() {
	// Hide game selector
	gameSelector.visible = false;
	// Create new MatchingGame or reset existing one
	if (!matchingGame) {
		matchingGame = new MatchingGame();
		matchingGame.x = 2048 / 2;
		matchingGame.y = 2732 / 2;
		game.addChild(matchingGame);
	} else {
		// Remove existing game and create a new one
		game.removeChild(matchingGame);
		matchingGame = new MatchingGame();
		matchingGame.x = 2048 / 2;
		matchingGame.y = 2732 / 2;
		game.addChild(matchingGame);
	}
	// Show the game
	matchingGame.visible = true;
	// Set callback for return home button
	matchingGame.setReturnHomeCallback(function () {
		// Hide MatchingGame
		matchingGame.visible = false;
		// Show main screen
		hideGameSelector(); // Use existing function to return to main view
	});
	// Set callback for when game finishes
	matchingGame.setFinishCallback(function () {
		// Hide Matching game
		matchingGame.visible = false;
		// Show main screen
		hideGameSelector();
		// Give rewards for completing video game - memory games give a bit more followers
		var videoEarnings = MEME_BASE_INCOME * memeLevel * studioLevel * 7; // Doubled from 3.5 to 7
		var videoFollowers = MEME_BASE_FOLLOWERS * memeLevel * 4;
		// Apply membership bonus if active
		if (membershipLevel > 0) {
			videoEarnings = Math.floor(videoEarnings * 1.5);
		}
		// Apply randomness
		var randomFactor = 0.9 + Math.random() * 0.2;
		videoEarnings = Math.floor(videoEarnings * randomFactor);
		// Update game state
		money += videoEarnings;
		followers += videoFollowers;
		// Show earnings
		memeStudio.showEarnings(videoEarnings, videoFollowers);
		// Play coin sound
		LK.getSound('coin').play();
		gems += 1; // Award 1 gem for winning
		// Update storage
		updateStorage();
	});
}
:quality(85)/https://cdn.frvr.ai/6810df98886a67ee710bc15c.png%3F3) 
 Modern App Store icon, high definition, square with rounded corners, for a game titled "Meme Empire Tycoon" and with the description "Build your meme empire from scratch in this addictive tycoon game! Create viral content, hire talented staff, upgrade your studio, and strategically expand your meme business across multiple platforms to become the internet's most influential meme creator.". No text on icon!
:quality(85)/https://cdn.frvr.ai/6810e039886a67ee710bc163.png%3F3) 
 Green button. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6810e68d9dcd06d468edb764.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6810e6ad9dcd06d468edb766.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6810e6c89dcd06d468edb768.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6810e6e79dcd06d468edb76a.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6810e81a886a67ee710bc196.png%3F3) 
 Youtube silver play button. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68128e8631e90df6e3a79797.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/68128e9d31e90df6e3a79799.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6812903d31e90df6e3a7979b.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6812904331e90df6e3a7979d.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6812904931e90df6e3a7979f.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6812d1c021b2d4faceef26c8.png%3F3) 
 Wheel of fortune. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68136aad8a910fef600d6a58.png%3F3) 
 Wheel of fortune without the stand. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68136d0031e90df6e3a797c5.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/68162d7ff64e46cb4179fd88.png%3F3) 
 Coin potion. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68181bcdb2bf21f1a424646e.png%3F3) 
 Diamond. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6818cbcc34fc36a2e12fd931.png%3F3) 
 Archery target. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6818cdd834fc36a2e12fd940.png%3F3) 
 Arrow. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6818ce7534fc36a2e12fd947.png%3F3) 
 Fish. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6818cebb34fc36a2e12fd951.png%3F3) 
 Purple fish. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6818cef534fc36a2e12fd957.png%3F3) 
 Golden fish. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/681b8815f25587d7a0654099.png%3F3) 
 Red cup. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/681bdde6a31d107d050d11fc.png%3F3) 
 Mute music logo. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68236e2403c085db02359a5c.png%3F3)