/* Base Layout & Typography */
body { font-family: 'Poppins', sans-serif; background-color: #f1f5f9; color: #334155; margin: 0; padding: 15px; transition: background-color 0.4s ease, color 0.4s ease; }
.hidden { display: none !important; }

/* Top Bar */
.top-bar { display: flex; justify-content: space-between; align-items: center; max-width: 1200px; margin: 0 auto 10px auto; }
.top-bar button { background: #ffffff; border: none; box-shadow: 0 2px 10px rgba(0,0,0,0.05); color: #475569; padding: 8px 16px; border-radius: 30px; cursor: pointer; font-size: 14px; font-weight: 600; transition: all 0.2s; }
.top-bar button:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.1); color: #0f172a; }

/* --- SIDE-BY-SIDE LAYOUT --- */
/* Changed align-items to flex-start to push the board to the top */
.layout-wrapper { display: flex; gap: 40px; max-width: 900px; margin: 0 auto; align-items: flex-start; justify-content: center;}

/* Sidebar (Left) */
.sidebar { width: 320px; flex-shrink: 0; background: #ffffff; padding: 20px; border-radius: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.06); box-sizing: border-box;}
.main-content { display: flex; justify-content: center; align-items: flex-start;}

/* Headers & Score */
.header-container { text-align: center; margin-bottom: 20px; }
h1 { font-size: 38px; margin: 0 0 5px 0; font-weight: 700; color: #0f172a; letter-spacing: -1px; }
.score-board { font-size: 16px; font-weight: 600; color: #3b82f6; background: #eff6ff; padding: 8px 15px; border-radius: 12px; display: inline-block; }
.quick-tip { font-size: 12px; color: #64748b; background: #f8fafc; padding: 10px; border-radius: 8px; margin: 15px 0 0 0; text-align: left; line-height: 1.4; border: 1px solid #e2e8f0; }

/* Controls */
.controls { display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }
button { padding: 10px; font-size: 14px; cursor: pointer; border-radius: 10px; border: 1px solid #cbd5e1; width: 100%; box-sizing: border-box; font-family: 'Poppins', sans-serif; outline: none; }
button.action-btn { background-color: #3b82f6; color: white; border: none; font-weight: 600; transition: all 0.2s; box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3); }
button.action-btn:hover { background-color: #2563eb; transform: translateY(-2px); }
button.undo-btn { background-color: #f59e0b; color: white; border: none; font-weight: 600; transition: all 0.2s; box-shadow: 0 4px 15px rgba(245, 158, 11, 0.3); }
button.undo-btn:hover { background-color: #d97706; transform: translateY(-2px); }

/* On-Screen D-Pad */
.d-pad-container { display: flex; flex-direction: column; align-items: center; margin-bottom: 20px; background: #f8fafc; padding: 12px; border-radius: 14px; border: 1px solid #e2e8f0;}
.d-pad-row { display: flex; gap: 8px; margin-bottom: 8px; }
.d-pad-btn { background-color: #ffffff; color: #64748b; font-size: 18px; padding: 12px 20px; border: 1px solid #e2e8f0; border-radius: 10px; cursor: pointer; box-shadow: 0 2px 5px rgba(0,0,0,0.03); transition: all 0.1s; width: auto;}
.d-pad-btn:hover { background-color: #f1f5f9; color: #3b82f6; }
.d-pad-btn:active { transform: scale(0.92); box-shadow: none; }
.controls-hint { font-size: 11px; color: #94a3b8; margin-top: 2px; text-align: center; }

/* History Panel */
#history-panel { background: #f8fafc; padding: 12px; border-radius: 12px; font-size: 12px; border: 1px solid #e2e8f0; }
#history-list { list-style: none; padding: 0; margin: 8px 0 0 0; color: #475569; }
#history-list li { padding: 6px 0; border-bottom: 1px dashed #cbd5e1; }
#history-list li:last-child { border-bottom: none; padding-bottom: 0; }

/* --- MAIN GAME AREA --- */
.board-container { display: flex; flex-direction: column; align-items: center; background: #ffffff; padding: 20px; border-radius: 20px; box-shadow: 0 10px 40px rgba(0,0,0,0.08); margin-top: 5px;}
.board-header { display: flex; justify-content: center; width: 100%; margin-bottom: 15px; font-weight: 700; font-size: 18px; color: #334155; }

/* Modern 2048 Grid */
.game-board { background-color: #e2e8f0; width: 340px; height: 340px; padding: 14px; border-radius: 16px; display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); gap: 14px; position: relative; box-shadow: inset 0 4px 10px rgba(0,0,0,0.05); }
.cell { background-color: rgba(255, 255, 255, 0.4); border-radius: 10px; }
.tile { position: absolute; width: 74.5px; height: 74.5px; border-radius: 10px; display: flex; justify-content: center; align-items: center; font-size: 30px; font-weight: 700; transition: all 0.15s ease-in-out; box-shadow: 0 4px 12px rgba(0,0,0,0.15); }
.board-overlay { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: rgba(255, 255, 255, 0.85); display: flex; flex-direction: column; justify-content: center; align-items: center; border-radius: 16px; z-index: 10; font-size: 36px; font-weight: 700; color: #0f172a; backdrop-filter: blur(4px); }

/* Tutorial Modal */
#tutorial-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(15, 23, 42, 0.7); display: flex; justify-content: center; align-items: center; z-index: 1000; backdrop-filter: blur(3px); }
.tutorial-content { background: #fff; padding: 30px; border-radius: 20px; max-width: 500px; width: 90%; position: relative; box-shadow: 0 20px 50px rgba(0,0,0,0.2); }
.close-btn { position: absolute; top: 15px; right: 20px; cursor: pointer; font-size: 24px; font-weight: bold; color: #64748b; }

/* Responsive Mobile Fallback */
@media (max-width: 850px) {
    .layout-wrapper { flex-direction: column-reverse; align-items: center; gap: 20px;}
    .sidebar { width: 100%; max-width: 450px; margin-bottom: 20px; }
    .game-board { width: 300px; height: 300px; padding: 12px; gap: 12px;}
    .tile { width: 66px; height: 66px; font-size: 26px;}
}

/* --- DARK MODE (Sleek Slate/Navy Theme) --- */
body.dark-mode { background-color: #0f172a; color: #f8fafc; }
body.dark-mode .top-bar button { background: #1e293b; color: #cbd5e1; box-shadow: 0 2px 10px rgba(0,0,0,0.2); }
body.dark-mode .top-bar button:hover { background: #334155; color: #fff; }
body.dark-mode .sidebar, body.dark-mode .board-container, body.dark-mode .tutorial-content { background-color: #1e293b; color: #f8fafc; box-shadow: 0 10px 30px rgba(0,0,0,0.3); border: 1px solid #334155; }
body.dark-mode h1, body.dark-mode .board-header { color: #f8fafc; }
body.dark-mode .score-board { background: #0f172a; color: #60a5fa; }
body.dark-mode .quick-tip { background: #0f172a; border-color: #334155; color: #94a3b8; }
body.dark-mode button.action-btn, body.dark-mode button.undo-btn { border: none;}
body.dark-mode #history-panel, body.dark-mode .d-pad-container { background: #0f172a; border-color: #334155; }
body.dark-mode #history-list li { border-color: #334155; color: #94a3b8; }
body.dark-mode .d-pad-btn { background-color: #1e293b; color: #94a3b8; border-color: #334155; }
body.dark-mode .d-pad-btn:hover { background-color: #334155; color: #38bdf8; }
body.dark-mode .game-board { background-color: #334155; box-shadow: inset 0 4px 10px rgba(0,0,0,0.2); }
body.dark-mode .cell { background-color: rgba(15, 23, 42, 0.5); }
body.dark-mode .board-overlay { background-color: rgba(15, 23, 42, 0.85); color: #f8fafc; }