/* ==== VARIABLES & THEME ==== */
:root {
    /* Base Colors */
    --bg-main: #0f172a;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    
    /* Board Styling */
    --board-bg: rgba(30, 41, 59, 0.7);
    --grid-cell-bg: rgba(15, 23, 42, 0.6);
    --board-radius: 12px;
    --board-shadow: 0 20px 40px rgba(0,0,0,0.5), inset 0 1px 1px rgba(255,255,255,0.1);
    
    /* Tile Sizing - dynamic via JS but default fallback */
    --grid-size: 4;
    --gap: 10px;
    --board-padding: 10px;
    /* Calculated in JS, fallback here */
    --cell-size: 70px; 
    
    /* UI components */
    --accent: #3b82f6;
    --accent-hover: #2563eb;
    --success: #10b981;
    --danger: #ef4444;
}

/* Level Themes (Set by JS on body) */
body.level-1 {
    --bg-main: #0f141e;
    --board-bg: rgba(30, 40, 60, 0.7);
}
body.level-2 {
    --bg-main: #1e1b2e;
    --board-bg: rgba(50, 40, 70, 0.7);
    --accent: #8b5cf6;
}
body.level-3 {
    --bg-main: #142828;
    --board-bg: rgba(30, 60, 60, 0.7);
    --accent: #14b8a6;
}
body.level-4 {
    --bg-main: #2e1818;
    --board-bg: rgba(70, 30, 30, 0.7);
    --accent: #f43f5e;
}
body.level-5, body.level-6, body.level-7 {
    --bg-main: #0a0a0a;
    --board-bg: rgba(30, 30, 30, 0.7);
    --accent: #f59e0b;
}

/* ==== RESET ==== */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg-main);
    color: var(--text-main);
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: background-color 1s ease;
}

/* ==== BACKGROUND PARALLAX ==== */
.parallax-bg {
    position: absolute;
    top: -10%; left: -10%; right: -10%; bottom: -10%;
    z-index: -1;
    background: radial-gradient(circle at center, rgba(255,255,255,0.05) 0%, transparent 60%);
    pointer-events: none;
    transition: transform 0.1s ease-out;
}

/* ==== LAYOUT ==== */
#app-container {
    width: 100%;
    max-width: 500px;
    height: 100%;
    max-height: 800px;
    display: flex;
    flex-direction: column;
    position: relative;
    padding: 20px;
}

.hidden-initially {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.5s ease;
}
.game-active .hidden-initially {
    opacity: 1;
    pointer-events: auto;
}

/* ==== HUD HEADER ==== */
#hud {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 20px;
    width: 100%;
}

.hud-topline {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.hud-bottomline {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.hud-scores {
    display: flex;
    gap: 10px;
}

.game-title {
    font-size: 32px;
    font-weight: 800;
    letter-spacing: 1px;
    display: flex;
    align-items: center;
    gap: 10px;
    text-shadow: 0 2px 10px rgba(255,255,255,0.2);
}

.badge {
    background: var(--accent);
    font-size: 14px;
    padding: 4px 8px;
    border-radius: 6px;
    font-weight: bold;
    box-shadow: 0 0 10px var(--accent);
}

.target-container {
    font-size: 14px;
    color: var(--text-muted);
    font-weight: 600;
}
#target-value {
    color: var(--text-main);
    font-weight: 800;
}

.score-card {
    background: var(--board-bg);
    padding: 8px 16px;
    border-radius: 8px;
    text-align: center;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
    border: 1px solid rgba(255,255,255,0.05);
}

.score-label {
    font-size: 11px;
    color: var(--text-muted);
    font-weight: 700;
    letter-spacing: 1px;
}

.score-val {
    font-size: 20px;
    font-weight: 800;
}

/* ==== CONTROLS ==== */
#controls-top {
    display: flex;
    gap: 10px;
}

.icon-btn {
    background: rgba(255,255,255,0.1);
    border: none;
    color: #fff;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    font-size: 18px;
    cursor: pointer;
    backdrop-filter: blur(4px);
    transition: 0.2s;
}
.icon-btn:hover { background: rgba(255,255,255,0.2); transform: scale(1.1); }

/* ==== GAME BOARD 3D ==== */
#game-stage {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    perspective: 1000px;
}

.board-container {
    background: var(--board-bg);
    padding: var(--board-padding);
    border-radius: var(--board-radius);
    box-shadow: var(--board-shadow);
    position: relative;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255,255,255,0.1);
    transform-style: preserve-3d;
    transition: transform 0.1s ease-out;
}

.board-grid {
    display: grid;
    grid-template-columns: repeat(var(--grid-size), var(--cell-size));
    grid-template-rows: repeat(var(--grid-size), var(--cell-size));
    gap: var(--gap);
}

.grid-cell {
    width: var(--cell-size);
    height: var(--cell-size);
    background: var(--grid-cell-bg);
    border-radius: 6px;
    box-shadow: inset 0 2px 5px rgba(0,0,0,0.3);
}

.tiles-layer {
    position: absolute;
    top: var(--board-padding);
    left: var(--board-padding);
    width: calc(100% - var(--board-padding)*2);
    height: calc(100% - var(--board-padding)*2);
    pointer-events: none;
}

/* ==== TILES ==== */
.tile {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 28px;
    font-weight: bold;
    color: #fff;
    transition: transform 0.15s ease-in-out;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2), inset 0 1px 2px rgba(255,255,255,0.2);
    z-index: 10;
}

/* Tile Appearance Colors */
.tile[data-val="2"]    { background: #3b82f6; box-shadow: 0 0 10px rgba(59, 130, 246, 0.4), inset 0 2px 2px rgba(255,255,255,0.3); }
.tile[data-val="4"]    { background: #8b5cf6; box-shadow: 0 0 10px rgba(139, 92, 246, 0.4), inset 0 2px 2px rgba(255,255,255,0.3); }
.tile[data-val="8"]    { background: #10b981; box-shadow: 0 0 15px rgba(16, 185, 129, 0.5), inset 0 2px 2px rgba(255,255,255,0.3); }
.tile[data-val="16"]   { background: #f59e0b; box-shadow: 0 0 15px rgba(245, 158, 11, 0.5), inset 0 2px 2px rgba(255,255,255,0.3); }
.tile[data-val="32"]   { background: #ef4444; box-shadow: 0 0 20px rgba(239, 68, 68, 0.6), inset 0 2px 2px rgba(255,255,255,0.3); }
.tile[data-val="64"]   { background: #ec4899; box-shadow: 0 0 20px rgba(236, 72, 153, 0.6), inset 0 2px 2px rgba(255,255,255,0.3); }
.tile[data-val="128"]  { background: #facc15; color: #1f2937; box-shadow: 0 0 25px rgba(250, 204, 21, 0.7), inset 0 2px 4px rgba(255,255,255,0.5); font-size: 24px; }
.tile[data-val="256"]  { background: #fb923c; box-shadow: 0 0 25px rgba(251, 146, 60, 0.7), inset 0 2px 4px rgba(255,255,255,0.5); font-size: 24px; }
.tile[data-val="512"]  { background: #fb7185; box-shadow: 0 0 30px rgba(251, 113, 133, 0.8), inset 0 2px 4px rgba(255,255,255,0.5); font-size: 24px; }
.tile[data-val="1024"] { background: #c084fc; box-shadow: 0 0 35px rgba(192, 132, 252, 0.9), inset 0 2px 4px rgba(255,255,255,0.5); font-size: 20px; }
.tile[data-val="2048"] { background: #2dd4bf; box-shadow: 0 0 40px rgba(45, 212, 191, 1), inset 0 2px 4px rgba(255,255,255,0.5); font-size: 20px; }
.tile[data-val="4096"] { background: #000; box-shadow: 0 0 50px rgba(255, 255, 255, 0.5), inset 0 2px 4px rgba(255,255,255,0.5); font-size: 20px; border: 2px solid #fff; }

.tile[data-val="-1"]   { background: #475569; color: transparent; box-shadow: inset 0 5px 15px rgba(0,0,0,0.8); border: 2px solid #334155; }
.tile[data-val="-1"]::after {
    content: "✖";
    color: #94a3b8;
    position: absolute;
    font-size: 30px;
}

/* Animations */
.tile.new { animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); }
.tile.merged { animation: mergePop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275); z-index: 11; }

@keyframes popIn {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

@keyframes mergePop {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); box-shadow: 0 0 30px #fff; }
    100% { transform: scale(1); }
}

/* ==== SCREENS / MENUS ==== */
.screen {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
    z-index: 100;
}
.screen.active {
    opacity: 1;
    pointer-events: auto;
}

.full-screen {
    background: var(--bg-main);
    z-index: 200;
}

.overlay {
    background: rgba(0,0,0,0.7);
    backdrop-filter: blur(5px);
}

.card {
    background: rgba(30, 41, 59, 0.9);
    padding: 30px;
    border-radius: 16px;
    text-align: center;
    box-shadow: 0 20px 40px rgba(0,0,0,0.5);
    border: 1px solid rgba(255,255,255,0.1);
    max-width: 90%;
    width: 350px;
}

.logo-3d {
    font-size: 64px;
    font-weight: 900;
    text-align: center;
    margin-bottom: 20px;
    color: #fff;
    text-shadow: 0 5px 15px var(--accent), 0 10px 0 #1e3a8a;
    line-height: 1;
}
.logo-sub {
    font-size: 20px;
    letter-spacing: 5px;
    color: var(--accent);
    text-shadow: none;
    font-weight: 600;
}

/* DEMO ANIMATION */
.animated-demo {
    margin: 10px 0 20px 0;
    width: 170px;
    height: 80px;
    background: var(--board-bg);
    border-radius: 8px;
    position: relative;
    overflow: hidden;
    box-shadow: 0 10px 20px rgba(0,0,0,0.5);
    border: 1px solid rgba(255,255,255,0.1);
}

.demo-board {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0; left: 0;
}

.demo-tile {
    width: 60px;
    height: 60px;
    border-radius: 6px;
    background: #3b82f6; 
    color: white;
    font-size: 24px;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 10px;
    box-shadow: 0 0 10px rgba(59, 130, 246, 0.4), inset 0 2px 2px rgba(255,255,255,0.3);
}

.t-left {
    left: 10px;
    animation: slideLeft 2.5s infinite ease-in-out;
}

.t-right {
    left: 100px;
    color: transparent;
}
.t-right::after {
    content: "2";
    animation: changeValue 2.5s infinite ease-in-out;
}

.demo-hand {
    position: absolute;
    font-size: 35px;
    animation: swipeHand 2.5s infinite ease-in-out;
    filter: drop-shadow(0 5px 5px rgba(0,0,0,0.5));
    z-index: 10;
}

@keyframes slideLeft {
    0%, 20% { transform: translateX(0); opacity: 1; left: 10px; }
    40% { transform: translateX(90px); opacity: 0; }
    41%, 100% { opacity: 0; }
}

@keyframes changeValue {
    0%, 35% { content: "2"; color: white; background: #3b82f6; }
    40% { content: "4"; color: white; transform: scale(1.1); box-shadow: 0 0 15px #fff; }
    50%, 80% { content: "4"; color: white; transform: scale(1); background: #8b5cf6;}
    81%, 100% { content: "2"; color: white; background: #3b82f6; }
}
/* Ensure the t-right base styling changes too */
.t-right {
    animation: cycleBg 2.5s infinite ease-in-out;
}
@keyframes cycleBg {
    0%, 35% { background: #3b82f6; }
    40%, 80% { background: #8b5cf6; }
    81%, 100% { background: #3b82f6; }
}

@keyframes swipeHand {
    0%, 10% { transform: translate(30px, 30px) scale(1); opacity: 0; }
    15% { transform: translate(30px, 30px) scale(0.9); opacity: 1; }
    35% { transform: translate(120px, 30px) scale(0.9); opacity: 1; }
    40%, 100% { transform: translate(120px, 30px) scale(1); opacity: 0; }
}

.tutorial-card ul {
    text-align: left;
    margin: 20px 0;
    padding-left: 20px;
    color: var(--text-muted);
    font-size: 15px;
    line-height: 1.6;
}
.tutorial-card .hl { color: var(--accent); }
.tutorial-card .hl-red { color: var(--danger); }

h2 { font-size: 28px; margin-bottom: 10px; }
.hdr-success { color: var(--success); text-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }

.final-score {
    font-size: 20px;
    margin: 15px 0;
    color: var(--text-muted);
}
.final-score span {
    font-size: 28px;
    color: #fff;
    font-weight: bold;
}

/* BUTTONS */
button { font-family: inherit; cursor: pointer; outline: none; }

.primary-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 12px 24px;
    font-size: 16px;
    font-weight: bold;
    border-radius: 8px;
    width: 100%;
    box-shadow: 0 4px 0 #1e3a8a, 0 5px 15px rgba(59, 130, 246, 0.4);
    transition: all 0.1s;
}
.primary-btn:active {
    transform: translateY(4px);
    box-shadow: 0 0 0 #1e3a8a;
}
.primary-btn.glow {
    box-shadow: 0 4px 0 #065f46, 0 0 20px rgba(16, 185, 129, 0.6);
    background: var(--success);
}

.secondary-btn {
    background: transparent;
    color: var(--text-muted);
    border: 1px solid var(--text-muted);
    padding: 12px 24px;
    font-size: 14px;
    font-weight: bold;
    border-radius: 8px;
    width: 100%;
    margin-top: 10px;
    transition: 0.2s;
}
.secondary-btn:hover { background: rgba(255,255,255,0.1); color: #fff; }

.pulse {
    animation: pulseAnim 2s infinite;
}
@keyframes pulseAnim {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.stars-container {
    font-size: 30px;
    margin: 15px 0;
}
.star { opacity: 0; display: inline-block; }
.pop-in-1 { animation: popIn 0.5s 0.2s forwards; }
.pop-in-2 { animation: popIn 0.5s 0.4s forwards; }
.pop-in-3 { animation: popIn 0.5s 0.6s forwards; }

.desc { margin-bottom: 20px; color: var(--text-muted); }

/* ==== MEDIA QUERIES ==== */
@media (max-width: 400px) {
    :root {
        --cell-size: 60px;
        --gap: 8px;
        --board-padding: 8px;
    }
    .hud-left .game-title { font-size: 24px; }
}
