/* T2: Đặt Tính Rồi Tính - Style */
body {
    background: #E8F4F8; /* Xanh lơ nhạt */
    font-family: 'Nunito', sans-serif;
    user-select: none; -webkit-user-select: none;
    touch-action: pan-y; /* Cho phép cuộn dọc nêú màn nhỏ */
    overflow-x: hidden;
}

h1, h2, h3 { font-family: 'Baloo 2', cursive; color: #4A90E2; }

.game-wrapper {
    max-width: 800px; margin: 0 auto; padding: 20px; display: flex; flex-direction: column;
}

.back-btn {
    display: inline-flex; align-items: center; gap: 8px; font-weight: 700;
    color: #4A90E2; background: #fff; padding: 8px 16px; border-radius: 99px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.1); align-self: flex-start; margin-bottom: 5px;
    transition: transform 0.2s;
}

.back-btn:active { transform: scale(0.95); }

.game-header { text-align: center; margin-bottom: 10px; }
.game-header h1 { font-size: 2.2rem; margin-bottom: 5px; }
.game-header p { font-size: 1.1rem; color: #7B8B9A; margin-bottom: 10px; }

.stats-container { display: flex; justify-content: center; gap: 20px; }

.score-board {
    display: inline-block; padding: 6px 20px; border-radius: 99px;
    font-weight: 800; font-size: 1.2rem;
    background: #A0DEFF; color: #1E6B9B; box-shadow: 0 4px 0px #70BBE4;
}

/* Khu vực Hiệp sĩ và Lâu đài */
.game-area { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 20px;}

.castle-scene {
    width: 100%; max-width: 500px; height: 160px;
    background: linear-gradient(to bottom, #87CEEB 0%, #E0F6FF 100%);
    border-radius: 20px; border: 4px solid #fff; box-shadow: 0 10px 20px rgba(0,0,0,0.1);
    position: relative; overflow: hidden; display: flex; align-items: flex-end; justify-content: space-between;
    padding: 0 20px 20px;
}

/* Nền cỏ */
.castle-scene::after {
    content: ''; position: absolute; bottom: 0; left: 0; width: 100%; height: 30px;
    background: #81C784; border-radius: 0 0 15px 15px; z-index: 1;
}

.knight {
    font-size: 3.5rem; z-index: 2; position: relative;
    transition: transform 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transform: translateX(0); /* Sẽ dịch chuyển dẩn sang phải */
}

/* Speech bubble tooltip for knight */
.knight::before {
    content: 'Cứu!'; position: absolute; top: -30px; left: 10px;
    background: white; font-size: 0.9rem; padding: 2px 8px; border-radius: 10px;
    font-weight: bold; color: #333; opacity: 0; transition: opacity 0.3s;
}
.knight.calling::before { opacity: 1; }

.castle {
    font-size: 4rem; z-index: 2; position: relative; display: flex; align-items: center; justify-content: center;
    background: #CFD8DC; width: 80px; height: 100px; border-radius: 10px 10px 0 0; border: 3px solid #90A4AE;
    border-bottom: none;
}
/* Mái lâu đài */
.castle::before {
    content: ''; position: absolute; top: -40px; left: -15px; border-left: 50px solid transparent;
    border-right: 50px solid transparent; border-bottom: 40px solid #EF5350;
}

.princess { position: absolute; font-size: 2.5rem; bottom: 5px; z-index: 1;}
.padlock {
    position: absolute; font-size: 3rem; bottom: -5px; z-index: 3;
    animation: lockShake 2s infinite; transform-origin: top center;
}

.padlock.unlocked {
    animation: unlock 0.5s forwards;
}

@keyframes lockShake {
    0%, 100% {transform: rotate(0deg);}
    50% {transform: rotate(10deg);}
}
@keyframes unlock {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.5) translateY(-20px); opacity: 1; }
    100% { transform: scale(0); opacity: 0; }
}

/* Bảng Toán - Đặt tính */
.calculation-board {
    background: #fff; border-radius: 20px; padding: 25px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.05); width: 100%; max-width: 400px;
    display: flex; flex-direction: column; gap: 20px;
}

.math-grid {
    display: flex; flex-direction: column; align-items: center;
    font-family: 'Baloo 2', cursive; font-size: 3.5rem; line-height: 1.1; color: #2D3748;
}

.row { display: flex; justify-content: flex-end; width: 180px; position: relative; gap: 15px;}
.digit { width: 45px; text-align: center; }
.operator { position: absolute; left: 0; top: 0; color: #FF6B8B; }

.divider { height: 4px; background: #2D3748; width: 180px; margin: 10px 0; border-radius: 2px;}

.result-row { justify-content: flex-end; }

/* Các ô input để điền (Custom giao diện) */
.digit-input {
    width: 45px; height: 55px; font-family: 'Baloo 2', cursive; font-size: 3rem;
    text-align: center; border: 3px solid #CBD5E0; border-radius: 12px;
    background: #F8FAFC; color: #4A90E2; outline: none; transition: border-color 0.2s;
    -moz-appearance: textfield; /* Ẩn spinners */
}
.digit-input::-webkit-outer-spin-button,
.digit-input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.digit-input:focus, .digit-input.active { border-color: #4A90E2; background: #fff; }
.digit-input.error { border-color: #EF5350; color: #EF5350; animation: horizontalShake 0.4s; }

/* Bàn phím ảo cho lứa tuổi nhỏ (tránh bàn phím điện thoại native lôn xộn) */
.virtual-keyboard {
    display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
}
.key {
    background: #F1F5F9; border: none; border-radius: 12px; padding: 15px 0;
    font-size: 1.8rem; font-family: 'Baloo 2', cursive; font-weight: 700; color: #475569;
    cursor: pointer; box-shadow: 0 4px 0px #CBD5E0; transition: transform 0.1s;
    user-select: none; -webkit-user-select: none; touch-action: manipulation;
}
.key:active { transform: translateY(4px); box-shadow: 0 0px 0px #CBD5E0; background: #E2E8F0;}
.key-action { font-size: 1.2rem; background: #E2E8F0; color: #64748B;}
#key-check { background: #4CAF50; color: white; box-shadow: 0 4px 0px #2E7D32; }
#key-check:active { box-shadow: 0 0px 0px #2E7D32; background: #43A047; }
#key-del { background: #EF5350; color: white; box-shadow: 0 4px 0px #C62828;}
#key-del:active { box-shadow: 0 0px 0px #C62828; background: #E53935; }

/* Modal */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.6); backdrop-filter: blur(5px);
    display: flex; align-items: center; justify-content: center;
    opacity: 0; pointer-events: none; transition: opacity 0.3s; z-index: 1000;
}
.modal-overlay.show { opacity: 1; pointer-events: auto; }
.modal-content {
    background: white; padding: 40px; border-radius: 30px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.2); transform: scale(0.8); transition: transform 0.3s;
    max-width: 90%; width: 400px;
}
.modal-overlay.show .modal-content { transform: scale(1); }
.modal-icon { font-size: 5rem; margin-bottom: 10px; animation: bounce 2s infinite; }

.btn-primary {
    background: #4A90E2; color: white; border: none; padding: 12px 30px;
    font-size: 1.2rem; font-family: 'Baloo 2', cursive; border-radius: 99px;
    cursor: pointer; box-shadow: 0 6px 0px #2A6EBB; margin: 5px;
}
.btn-secondary {
    display: inline-block; background: #E2E8F0; color: #4A5568; margin: 5px;
    border: none; padding: 12px 30px; font-size: 1.2rem; font-family: 'Baloo 2', cursive;
    border-radius: 99px; cursor: pointer; box-shadow: 0 6px 0px #CBD5E0;
}

@keyframes bounce {
    0%, 20%, 50%, 80%, 100% {transform: translateY(0);}
    40% {transform: translateY(-15px);}
    60% {transform: translateY(-7px);}
}
@keyframes horizontalShake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-5px); }
    40%, 80% { transform: translateX(5px); }
}

/* Mobile Adjustments */
@media (max-width: 600px) {
    .math-grid { font-size: 3rem; }
    .digit { width: 40px; }
    .digit-input { width: 40px; height: 50px; font-size: 2.5rem; }
    .key { padding: 10px 0; font-size: 1.5rem; }
    .key-action { font-size: 1rem; }
    .castle-scene { height: 130px; }
    .knight, .castle { font-size: 3rem; }
}
