/* ============================================
   2048 Puzzle Game - CSS Styles
   Dark Mode First, Glassmorphism, 2026 Trends
   ============================================ */

:root {
    /* Colors */
    --bg-primary: #0f0f23;
    --bg-secondary: #1a1a2e;
    --text-primary: #ffffff;
    --text-secondary: #b0b0b0;
    --accent: #f39c12;
    --accent-dark: #d68910;
    --accent-light: #f5b041;

    /* Grid */
    --grid-spacing: 10px;
    --grid-cell-size: 65px;

    /* Transitions */
    --transition-fast: 100ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-normal: 200ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 350ms cubic-bezier(0.4, 0, 0.2, 1);

    /* Tile Colors */
    --tile-2: #ffeaa7;
    --tile-4: #fdcb6e;
    --tile-8: #f9ca24;
    --tile-16: #f39c12;
    --tile-32: #e67e22;
    --tile-64: #d35400;
    --tile-128: #c0392b;
    --tile-256: #9b59b6;
    --tile-512: #8e44ad;
    --tile-1024: #6c3483;
    --tile-2048: #2c3e50;
    --tile-super: #1a252f;
}

/* App Loader */
.app-loader {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: var(--bg-primary, #0f0f23);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: opacity 0.3s;
}
.app-loader.hidden {
    opacity: 0;
    pointer-events: none;
}
.loader-spinner {
    width: 40px; height: 40px;
    border: 3px solid rgba(255,255,255,0.1);
    border-top-color: var(--accent, #f39c12);
    border-radius: 50%;
    animation: loader-spin 0.8s linear infinite;
}
@keyframes loader-spin { to { transform: rotate(360deg); } }

/* ============================================
   Responsive Layout
   ============================================ */

@media (max-width: 768px) {
    :root {
        --grid-cell-size: 55px;
        --grid-spacing: 8px;
    }
}

@media (max-width: 480px) {
    :root {
        --grid-cell-size: 45px;
        --grid-spacing: 6px;
    }
}

/* ============================================
   Base Styles
   ============================================ */

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

/* Touch Optimization */
button, a, input, [role="button"], [role="link"] {
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
}

input[type="text"], textarea {
    -webkit-user-select: text;
    user-select: text;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
                 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ============================================
   Ad Containers
   ============================================ */

.ad-container {
    padding: 12px 0;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 60px;
    background: rgba(26, 26, 46, 0.5);
    border-bottom: 1px solid rgba(243, 156, 18, 0.1);
}

.ad-top {
    border-top: 1px solid rgba(243, 156, 18, 0.1);
}

.ad-interstitial {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 15, 35, 0.95);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(4px);
}

.ad-interstitial.hidden {
    display: none;
}

/* ============================================
   Sound Toggle
   ============================================ */

.sound-toggle {
    position: fixed;
    top: 12px;
    right: 64px;
    z-index: 100;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid var(--accent);
    background: rgba(243, 156, 18, 0.1);
    color: var(--accent);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-normal);
    backdrop-filter: blur(10px);
}

.sound-toggle:hover {
    background: rgba(243, 156, 18, 0.2);
    transform: scale(1.05);
}

.sound-toggle:active {
    transform: scale(0.95);
}

/* ============================================
   Language Selector
   ============================================ */

.language-selector {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 100;
}

.lang-toggle {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid var(--accent);
    background: rgba(243, 156, 18, 0.1);
    color: var(--accent);
    font-size: 20px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-normal);
    backdrop-filter: blur(10px);
}

.lang-toggle:hover {
    background: rgba(243, 156, 18, 0.2);
    transform: scale(1.05);
}

.lang-toggle:active {
    transform: scale(0.95);
}

.lang-menu {
    position: absolute;
    top: 54px;
    right: 0;
    background: rgba(26, 26, 46, 0.95);
    border: 1px solid rgba(243, 156, 18, 0.2);
    border-radius: 12px;
    backdrop-filter: blur(10px);
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    min-width: 150px;
    transition: all var(--transition-normal);
}

.lang-menu.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(-10px);
}

.lang-option {
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    text-align: left;
    cursor: pointer;
    font-size: 13px;
    transition: all var(--transition-fast);
}

.lang-option:hover {
    background: rgba(243, 156, 18, 0.15);
    color: var(--accent);
}

.lang-option:active {
    background: rgba(243, 156, 18, 0.25);
}

/* ============================================
   Game Wrapper
   ============================================ */

.game-wrapper {
    max-width: 420px;
    margin: 0 auto;
    padding: 20px;
}

/* ============================================
   Header
   ============================================ */

.game-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 24px;
    gap: 20px;
}

.header-left h1 {
    font-size: 42px;
    font-weight: 700;
    margin-bottom: 4px;
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent-light) 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.subtitle {
    font-size: 12px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 1px;
}

.header-right {
    display: flex;
    gap: 12px;
}

.score-board {
    display: flex;
    gap: 12px;
}

.score-item {
    background: rgba(243, 156, 18, 0.1);
    border: 1px solid rgba(243, 156, 18, 0.2);
    border-radius: 8px;
    padding: 12px 16px;
    min-width: 90px;
    text-align: center;
    backdrop-filter: blur(10px);
}

.score-label {
    display: block;
    font-size: 11px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 6px;
}

.score-value {
    font-size: 24px;
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

/* ============================================
   Game Container & Grid
   ============================================ */

.game-container {
    position: relative;
    margin-bottom: 24px;
}

.grid-background {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: var(--grid-spacing);
    background: rgba(26, 26, 46, 0.3);
    padding: var(--grid-spacing);
    border-radius: 12px;
    aspect-ratio: 1;
    margin-bottom: 12px;
    border: 2px solid rgba(243, 156, 18, 0.15);
    backdrop-filter: blur(10px);
}

.grid-cell {
    background: rgba(176, 176, 176, 0.05);
    border-radius: 8px;
    aspect-ratio: 1;
}

.grid {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
}

/* ============================================
   Tiles
   ============================================ */

.tile {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    border-radius: 8px;
    transition: left 150ms ease-in-out, top 150ms ease-in-out;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    z-index: 10;
    will-change: left, top;
}

.tile-new {
    animation: tileAppear 200ms ease 50ms both;
}

.tile-merged {
    animation: tilePop 200ms ease 50ms both;
    z-index: 20;
}

@keyframes tileAppear {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes tilePop {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
    }
}

/* Tile Colors - 2 to 2048 */
.tile[data-value="2"] { background: linear-gradient(135deg, var(--tile-2) 0%, #ffd699 100%); color: #2c3e50; }
.tile[data-value="4"] { background: linear-gradient(135deg, var(--tile-4) 0%, #ffc94d 100%); color: #2c3e50; }
.tile[data-value="8"] { background: linear-gradient(135deg, var(--tile-8) 0%, #f7b801 100%); color: #ffffff; }
.tile[data-value="16"] { background: linear-gradient(135deg, var(--tile-16) 0%, #d68910 100%); color: #ffffff; }
.tile[data-value="32"] { background: linear-gradient(135deg, var(--tile-32) 0%, #c0392b 100%); color: #ffffff; }
.tile[data-value="64"] { background: linear-gradient(135deg, var(--tile-64) 0%, #b5270f 100%); color: #ffffff; }
.tile[data-value="128"] { background: linear-gradient(135deg, var(--tile-128) 0%, #922b21 100%); color: #ffffff; }
.tile[data-value="256"] { background: linear-gradient(135deg, var(--tile-256) 0%, #76448a 100%); color: #ffffff; }
.tile[data-value="512"] { background: linear-gradient(135deg, var(--tile-512) 0%, #6c3483 100%); color: #ffffff; }
.tile[data-value="1024"] { background: linear-gradient(135deg, var(--tile-1024) 0%, #4a235a 100%); color: #ffffff; }
.tile[data-value="2048"] { background: linear-gradient(135deg, var(--tile-2048) 0%, #1a252f 100%); color: var(--accent); font-weight: 800; }

/* Super Tiles (> 2048) */
.tile[data-value^="4096"],
.tile[data-value^="8192"],
.tile[data-value^="16384"],
.tile[data-value^="32768"],
.tile[data-value^="65536"] {
    background: linear-gradient(135deg, var(--tile-super) 0%, #0f1419 100%);
    color: var(--accent-light);
}

/* ============================================
   Controls
   ============================================ */

.controls {
    display: flex;
    gap: 12px;
    margin-bottom: 20px;
}

.btn {
    flex: 1;
    padding: 14px 20px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-normal);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent-dark) 100%);
    color: #ffffff;
    box-shadow: 0 4px 12px rgba(243, 156, 18, 0.3);
}

.btn-primary:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(243, 156, 18, 0.4);
}

.btn-primary:active:not(:disabled) {
    transform: translateY(0);
}

.btn-secondary {
    background: rgba(243, 156, 18, 0.1);
    color: var(--accent);
    border: 1px solid rgba(243, 156, 18, 0.3);
    backdrop-filter: blur(10px);
}

.btn-secondary:hover:not(:disabled) {
    background: rgba(243, 156, 18, 0.2);
    border-color: rgba(243, 156, 18, 0.5);
}

.btn-secondary:active:not(:disabled) {
    background: rgba(243, 156, 18, 0.15);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* ============================================
   Modals
   ============================================ */

.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 15, 35, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 500;
    padding: 20px;
    backdrop-filter: blur(8px);
    animation: fadeIn var(--transition-normal) ease-out;
}

.modal.hidden {
    display: none;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.modal-content {
    background: rgba(26, 26, 46, 0.95);
    border: 2px solid rgba(243, 156, 18, 0.3);
    border-radius: 16px;
    padding: 32px 24px;
    max-width: 300px;
    width: 100%;
    text-align: center;
    backdrop-filter: blur(10px);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
    animation: slideUp var(--transition-normal) ease-out;
}

@keyframes slideUp {
    from {
        transform: translateY(20px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.modal-content h2 {
    font-size: 28px;
    margin-bottom: 12px;
    color: var(--accent);
}

.modal-message {
    color: var(--text-secondary);
    font-size: 14px;
    margin-bottom: 20px;
}

.modal-stats {
    background: rgba(243, 156, 18, 0.05);
    border-radius: 12px;
    padding: 16px;
    margin-bottom: 20px;
    border: 1px solid rgba(243, 156, 18, 0.1);
}

.stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    font-size: 14px;
}

.stat-label {
    color: var(--text-secondary);
}

.stat-value {
    font-weight: 700;
    color: var(--accent);
    font-variant-numeric: tabular-nums;
}

.modal-buttons {
    display: flex;
    gap: 12px;
    flex-direction: column;
}

.modal-buttons .btn {
    margin: 0;
}

/* ============================================
   Responsive Design
   ============================================ */

@media (max-width: 480px) {
    .game-wrapper {
        padding: 16px;
    }

    .game-header {
        flex-direction: column;
        margin-bottom: 16px;
    }

    .header-left h1 {
        font-size: 32px;
    }

    .header-right {
        width: 100%;
    }

    .score-board {
        width: 100%;
    }

    .score-item {
        flex: 1;
    }

    /* font-size is set dynamically via JS based on tile size */

    .btn {
        font-size: 12px;
        padding: 12px 16px;
    }

    .modal-content {
        padding: 24px 20px;
        max-width: 280px;
    }

    .modal-content h2 {
        font-size: 24px;
    }
}

@media (max-width: 320px) {
    .game-header {
        gap: 12px;
    }

    .header-left h1 {
        font-size: 28px;
    }

    .score-item {
        padding: 10px 12px;
        min-width: 80px;
    }

    .score-value {
        font-size: 18px;
    }

    .controls {
        gap: 8px;
    }

    .btn {
        font-size: 11px;
        padding: 10px 12px;
        height: 40px;
    }
}

/* ============================================
   Touch & Interaction Feedback
   ============================================ */

@media (hover: none) {
    .btn:active {
        transform: scale(0.95);
    }

    .lang-toggle:active {
        transform: scale(0.9);
    }
}

/* ============================================
   Animation Performance
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* ============================================
   WCAG 2.1 AA Accessibility Improvements
   ============================================ */

/* Focus Visible Styles */
button:focus-visible,
a:focus-visible,
input:focus-visible,
[role="button"]:focus-visible {
    outline: 3px solid #f39c12;
    outline-offset: 2px;
    border-radius: 2px;
}

/* Fallback for older browsers */
button:focus,
a:focus,
input:focus {
    outline: 2px solid #f39c12;
    outline-offset: 2px;
}

/* Minimum Touch Target Size (44x44px) */
button, a[href], input[type="button"],
input[type="submit"], [role="button"] {
    min-width: 44px;
    min-height: 44px;
}

/* Text Color Contrast */
.score-value, .high-score-value {
    color: var(--text-primary);
    font-weight: 600;
}

/* High Contrast Mode Support */
@media (prefers-contrast: more) {
    body {
        color: #ffffff;
        background-color: #000000;
    }

    button {
        border: 2px solid #ffffff;
    }
}

/* Print Styles
   ============================================ */

/* Light Mode */
[data-theme="light"] {
    --bg-primary: #f5f5fa;
    --bg-secondary: #ffffff;
    --text-primary: #1a1a2e;
    --text-secondary: #5a5a70;
}

[data-theme="light"] body {
    background-color: var(--bg-primary);
}

[data-theme="light"] .app-loader {
    background: var(--bg-primary);
}

[data-theme="light"] .lang-menu {
    background: var(--bg-secondary);
    border-color: rgba(243, 156, 18, 0.2);
}

[data-theme="light"] .lang-option {
    color: var(--text-primary);
}

[data-theme="light"] .lang-option:hover {
    background: rgba(243, 156, 18, 0.1);
}

[data-theme="light"] .game-wrapper {
    background: var(--bg-primary);
}

[data-theme="light"] .score-board {
    background: rgba(243, 156, 18, 0.05);
    border-color: rgba(243, 156, 18, 0.15);
}

[data-theme="light"] .grid-background {
    background: rgba(243, 156, 18, 0.05);
}

[data-theme="light"] .grid-cell {
    background: rgba(243, 156, 18, 0.08);
}

[data-theme="light"] .modal-overlay {
    background: rgba(0, 0, 0, 0.5);
}

[data-theme="light"] .modal-content {
    background: var(--bg-secondary);
    border-color: rgba(243, 156, 18, 0.2);
}

[data-theme="light"] .btn-secondary {
    background: rgba(243, 156, 18, 0.1);
    border-color: rgba(243, 156, 18, 0.2);
    color: var(--text-primary);
}

[data-theme="light"] .btn-secondary:hover {
    background: rgba(243, 156, 18, 0.15);
}

/* Theme Toggle */
.theme-toggle {
    position: fixed;
    top: 12px;
    right: 56px;
    z-index: 1000;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.theme-toggle:hover {
    background: rgba(255, 255, 255, 0.15);
}

.theme-toggle:active {
    transform: scale(0.95);
}

[data-theme="light"] .theme-toggle {
    background: rgba(243, 156, 18, 0.1);
    border-color: rgba(243, 156, 18, 0.2);
    color: var(--text-primary);
}

[data-theme="light"] .theme-toggle:hover {
    background: rgba(243, 156, 18, 0.15);
}

@media print {
    .ad-container,
    .language-selector,
    .sound-toggle,
    .controls {
        display: none;
    }
}

/* Keyboard shortcut hints */
.keyboard-hints {
    display: none;
    flex-wrap: wrap;
    justify-content: center;
    gap: 6px 14px;
    margin-bottom: 16px;
    font-size: 11px;
    color: var(--text-secondary);
}
@media (min-width: 768px) {
    .keyboard-hints { display: flex; }
}
.kbd-hint { white-space: nowrap; }
.kbd-hint kbd {
    display: inline-block;
    padding: 1px 5px;
    border: 1px solid rgba(243, 156, 18, 0.25);
    border-radius: 3px;
    background: rgba(243, 156, 18, 0.08);
    font-family: inherit;
    font-size: 10px;
    line-height: 1.4;
    color: var(--text-secondary);
}
