/* ============================================================
   2048 Game - CSS with Dark/Light Theme & RTL Support
   ============================================================ */

:root {
    --primary: #edc22e;
    --primary-hover: #dbb320;
    --primary-light: rgba(237, 194, 46, 0.2);
    --bg-primary: #faf8ef;
    --bg-secondary: #bbada0;
    --bg-card: #cdc1b4;
    --bg-card-empty: rgba(238, 228, 218, 0.45);
    --bg-overlay: rgba(238, 228, 218, 0.85);
    --text-primary: #776e65;
    --text-secondary: #8f7a66;
    --text-white: #ffffff;
    --text-on-dark: #f9f6f2;
    --text-dark: #776e65;
    --border: rgba(196, 185, 168, 0.6);
    --shadow: 0 3px 10px rgba(0, 0, 0, 0.12);
    --shadow-hover: 0 6px 20px rgba(0, 0, 0, 0.18);
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-full: 9999px;
    /* 浅色主题 - 经典 2048 配色 */
    --tile-2: #eee4da;
    --tile-4: #ede0c8;
    --tile-8: #f2b179;
    --tile-16: #f59563;
    --tile-32: #f67c5f;
    --tile-64: #f65e3b;
    --tile-128: #edcf72;
    --tile-256: #edcc61;
    --tile-512: #edc850;
    --tile-1024: #edc53f;
    --tile-2048: #edc22e;
    --tile-super: #3c3a32;
    --tile-text-light: #776e65;
    --tile-text-dark: #f9f6f2;
}

[data-theme="dark"] {
    --primary: #f5c542;
    --primary-hover: #e0b335;
    --primary-light: rgba(245, 197, 66, 0.15);
    --bg-primary: #121212;
    --bg-secondary: #2c2c2c;
    --bg-card: #1e1e1e;
    --bg-card-empty: rgba(60, 60, 60, 0.4);
    --bg-overlay: rgba(18, 18, 18, 0.9);
    --text-primary: #e0e0e0;
    --text-secondary: #a0a0a0;
    --text-white: #ffffff;
    --text-on-dark: #ffffff;
    --text-dark: #e0e0e0;
    --border: rgba(80, 80, 80, 0.6);
    --shadow: 0 3px 10px rgba(0, 0, 0, 0.4);
    --shadow-hover: 0 6px 20px rgba(0, 0, 0, 0.6);
    /* 深色主题 - 高对比度霓虹风格 */
    --tile-2: #3a3a3a;
    --tile-4: #4a4a4a;
    --tile-8: #ff6b6b;
    --tile-16: #ff8787;
    --tile-32: #ffa8a8;
    --tile-64: #fa5252;
    --tile-128: #fcc419;
    --tile-256: #fab005;
    --tile-512: #f08c00;
    --tile-1024: #e67700;
    --tile-2048: #5a9cf5;
    --tile-super: #845ef7;
    --tile-text-light: #1a1a1a;
    --tile-text-dark: #ffffff;
}

/* RTL */
[dir="rtl"] .header { flex-direction: row-reverse; }
[dir="rtl"] .header-left { flex-direction: row-reverse; }
[dir="rtl"] .header-right { flex-direction: row-reverse; }
[dir="rtl"] .game-toolbar { flex-direction: row-reverse; }
[dir="rtl"] .game-controls { flex-direction: row-reverse; }
[dir="rtl"] .overlay-buttons { flex-direction: row-reverse; }

/* Base */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif; background-color: var(--bg-primary); color: var(--text-primary); transition: background-color 0.3s, color 0.3s; }
body { display: flex; flex-direction: column; }
#app { max-width: 520px; margin: 0 auto; padding: 1rem; width: 100%; }

/* Header */
.header { display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 0; margin-bottom: 1rem; flex-wrap: wrap; gap: 0.75rem; }
.header-left { display: flex; align-items: center; }
.logo { font-size: 1.75rem; font-weight: 700; color: var(--text-primary); display: flex; align-items: center; gap: 0.5rem; }
.logo-icon { display: inline-flex; align-items: center; }
.header-right { display: flex; align-items: center; gap: 0.75rem; }
.game-controls { display: flex; gap: 0.5rem; }
.score-box { background: var(--bg-secondary); color: var(--text-white); padding: 0.25rem 0.75rem; border-radius: var(--radius-md); text-align: center; min-width: 70px; }
.score-label { font-size: 0.65rem; font-weight: 600; text-transform: uppercase; opacity: 0.8; }
.score-box div:last-child { font-size: 1.25rem; font-weight: 700; }

.lang-select select { appearance: auto; padding: 0.25rem 0.5rem; border-radius: var(--radius-sm); border: 1px solid var(--border); background-color: var(--bg-card); color: var(--text-primary); cursor: pointer; }
.theme-toggle { display: flex; align-items: center; cursor: pointer; padding: 0.25rem; border-radius: var(--radius-full); user-select: none; font-size: 1.1rem; }
.theme-toggle:hover { background-color: var(--primary-light); }
.user-profile { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--text-primary); }

/* Tabs */
.tabs { display: flex; gap: 4px; background: var(--bg-secondary); border-radius: var(--radius-lg); padding: 4px; margin: 1rem 0; }
.tab-btn { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 0.5rem 0.25rem; border: none; border-radius: var(--radius-md); background: transparent; color: var(--text-secondary); font-size: 0.75rem; font-weight: 600; cursor: pointer; transition: all 0.2s; }
.tab-btn.active { background: var(--bg-card); color: var(--text-primary); box-shadow: var(--shadow); }
.tab-btn:hover:not(.active) { background: rgba(128,128,128,0.1); }
.tab-icon { font-size: 1.25rem; }
.tab-content { position: relative; }
.tab-panel { display: none; }
.tab-panel.active { display: block; padding: 0.5rem 0; }

/* Game Area */
.game-toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; gap: 0.5rem; }
.new-game-btn { background: var(--primary); color: var(--text-white); border: none; padding: 0.6rem 1.2rem; border-radius: var(--radius-md); font-weight: 700; cursor: pointer; }
.new-game-btn:hover { background: var(--primary-hover); }
.game-hint { font-size: 0.85rem; color: var(--text-secondary); }
.game-container { position: relative; width: 100%; max-width: 500px; margin: 1rem auto 0; touch-action: none; aspect-ratio: 1/1; min-height: 320px; }
.grid { display: grid; gap: 10px; width: 100%; height: 100%; background: var(--bg-secondary); border-radius: var(--radius-lg); padding: 10px; grid-template-columns: repeat(4, 1fr); }
.tile {
    background: var(--bg-card-empty);
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(1.5rem, 6vw, 3rem);
    font-weight: 700;
    color: var(--text-dark);
    user-select: none;
    /* 强制保持正方形 */
    aspect-ratio: 1 / 1;
    width: 100%;
}
.tile-2 { background: var(--tile-2); color: var(--tile-text-light); } 
.tile-4 { background: var(--tile-4); color: var(--tile-text-light); } 
.tile-8 { background: var(--tile-8); color: var(--tile-text-dark); } 
.tile-16 { background: var(--tile-16); color: var(--tile-text-dark); }
.tile-32 { background: var(--tile-32); color: var(--tile-text-dark); } 
.tile-64 { background: var(--tile-64); color: var(--tile-text-dark); } 
.tile-128 { background: var(--tile-128); color: var(--tile-text-dark); font-size: clamp(1.25rem, 5vw, 2.75rem); box-shadow: 0 0 12px rgba(237, 207, 114, 0.4); }
.tile-256 { background: var(--tile-256); color: var(--tile-text-dark); font-size: clamp(1.25rem, 5vw, 2.5rem); box-shadow: 0 0 15px rgba(237, 204, 97, 0.5); }
.tile-512 { background: var(--tile-512); color: var(--tile-text-dark); font-size: clamp(1.25rem, 5vw, 2.5rem); box-shadow: 0 0 20px rgba(237, 200, 80, 0.6); }
.tile-1024 { background: var(--tile-1024); color: var(--tile-text-dark); font-size: clamp(1rem, 4vw, 2rem); box-shadow: 0 0 25px rgba(237, 197, 63, 0.7); }
.tile-2048 { background: var(--tile-2048); color: var(--tile-text-dark); font-size: clamp(1rem, 4vw, 2rem); box-shadow: 0 0 30px rgba(237, 194, 46, 0.8); }
.tile-super { background: var(--tile-super); color: var(--tile-text-dark); font-size: clamp(0.9rem, 3.5vw, 1.75rem); box-shadow: 0 0 35px rgba(60, 58, 50, 0.9); }
.tile-new { animation: tileAppear 0.2s ease; } .tile-merge { animation: tilePop 0.2s ease; }
@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.15); } 100% { transform: scale(1); } }
.game-overlay { position: absolute; inset: 0; background: var(--bg-overlay); border-radius: var(--radius-lg); display: flex; align-items: center; justify-content: center; z-index: 10; backdrop-filter: blur(3px); }
.overlay-content { text-align: center; padding: 2rem; }
.overlay-content h2 { font-size: clamp(1.5rem, 5vw, 2.5rem); margin-bottom: 0.75rem; color: var(--text-primary); }
.overlay-buttons { display: flex; gap: 0.75rem; justify-content: center; flex-wrap: wrap; }
.btn { padding: 0.6rem 1.2rem; border: none; border-radius: var(--radius-md); font-weight: 600; cursor: pointer; }
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: var(--text-white); }
.btn-secondary { background: var(--bg-secondary); color: var(--text-white); }
.btn-danger { background: #e74c3c; color: #fff; }
.btn-danger:hover { background: #c0392b; }

/* Stats */
.stats-grid { display: grid; gap: 0.75rem; margin: 1rem 0; grid-template-columns: repeat(3, 1fr); }
.stat-card { background: var(--bg-card); border-radius: var(--radius-md); padding: 1rem; text-align: center; box-shadow: var(--shadow); }
.stat-icon { font-size: 1.5rem; margin-bottom: 0.25rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; color: var(--text-primary); }
.stat-label { font-size: 0.75rem; color: var(--text-secondary); margin-top: 0.25rem; }
.stat-unit { font-size: 0.7rem; color: var(--text-secondary); }
.level-card { background: var(--bg-card); border-radius: var(--radius-md); padding: 1rem; margin: 1rem 0; box-shadow: var(--shadow); }
.level-header { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.5rem; }
.level-badge { background: var(--primary); color: var(--text-white); padding: 0.25rem 0.75rem; border-radius: var(--radius-full); font-weight: 700; }
.xp-bar { width: 100%; height: 8px; background: var(--bg-card-empty); border-radius: var(--radius-full); overflow: hidden; margin-top: 0.5rem; }
.xp-progress { height: 100%; background: linear-gradient(90deg, var(--primary), #f5c542); border-radius: var(--radius-full); transition: width 0.5s; }

/* Rankings */
.panel-title { font-size: 1.1rem; font-weight: 700; margin: 0.5rem 0 1rem; }
.rankings-toggle { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.rankings-type-btn { padding: 0.4rem 0.8rem; border: 1px solid var(--border); border-radius: var(--radius-full); background: var(--bg-card); color: var(--text-secondary); font-size: 0.8rem; font-weight: 600; cursor: pointer; transition: all 0.2s; }
.rankings-type-btn.active { background: var(--primary); color: var(--text-white); border-color: var(--primary); }
.ranking-item { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem; background: var(--bg-card); margin-bottom: 0.5rem; transition: transform 0.1s; }
.ranking-item:hover { transform: translateX(2px); }
[dir="rtl"] .ranking-item:hover { transform: translateX(-2px); }
.ranking-item.is-current-user { background: var(--primary-light); }
.rank-position { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border-radius: var(--radius-full); font-weight: 700; flex-shrink: 0; }
.rank-1 { background: gold; color: #333; } .rank-2 { background: silver; color: #333; } .rank-3 { background: #cd7f32; color: #fff; }
.rank-avatar { width: 36px; height: 36px; border-radius: var(--radius-full); background: var(--bg-secondary); display: flex; align-items: center; justify-content: center; font-size: 1.1rem; flex-shrink: 0; }
.rank-info { flex: 1; min-width: 0; }
.rank-nickname { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rank-meta { font-size: 0.75rem; color: var(--text-secondary); }
.rank-metric { font-weight: 700; font-size: 1rem; flex-shrink: 0; }
.loading { text-align: center; padding: 2rem; color: var(--text-secondary); }

/* Profile & Custom Dropdown */
.profile-panel { padding-bottom: 2rem; }
.settings-section { display: flex; flex-direction: column; gap: 1.5rem; margin: 1rem 0; }
.setting-item { display: flex; flex-direction: column; gap: 0.4rem; }
.setting-label { font-size: 0.9rem; font-weight: 600; color: var(--text-secondary); }
.setting-input, .custom-select-display { width: 100%; padding: 0.75rem 1rem; border-radius: var(--radius-md); background: var(--bg-primary); color: var(--text-primary); font-size: 1rem; box-sizing: border-box; cursor: pointer; display: flex; align-items: center; gap: 0.75rem; font-weight: 600; }
.display-uuid { padding: 0.75rem 1rem; border-radius: var(--radius-md); background: var(--bg-secondary); color: var(--text-secondary); font-family: monospace; font-size: 0.8rem; word-break: break-all; }
.settings-actions .btn { width: 100%; padding: 0.8rem; margin-top: 0.5rem; }
.settings-danger { margin-top: 2rem; padding-top: 1.5rem; border-top: 1px dashed var(--border); display: flex; gap: 1rem; }
.settings-danger .btn { flex: 1; }

.custom-select-wrapper { position: relative; user-select: none; }
.custom-select-wrapper.open .custom-select-display { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); }
.custom-select-dropdown { display: none; position: absolute; top: calc(100% + 4px); left: 0; width: 100%; max-height: 240px; overflow-y: auto; background: var(--bg-card); border-radius: var(--radius-md); box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 100; }
.custom-select-wrapper.open .custom-select-dropdown { display: block; }
.custom-select-dropdown::-webkit-scrollbar { width: 6px; } .custom-select-dropdown::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 3px; }
.custom-option { display: flex; align-items: center; gap: 0.75rem; padding: 0.65rem 1rem; cursor: pointer; }
.custom-option:hover { background: var(--bg-secondary); }
.custom-option img, .custom-select-display img { width: 24px; height: 18px; border-radius: 2px; box-shadow: 0 1px 2px rgba(0,0,0,0.1); }
.custom-select-display img { height: 18px; object-fit: cover; }

/* Modal & Toast */
.modal { position: fixed; inset: 0; z-index: 1000; } .modal-overlay { position: absolute; inset: 0; background: rgba(0,0,0,0.5); }
.modal-content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: var(--bg-card); border-radius: var(--radius-lg); padding: 1.5rem; max-width: 400px; width: 90%; }
.modal-actions { display: flex; gap: 0.75rem; justify-content: flex-end; margin-top: 1rem; }
.toast-container { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); z-index: 2000; display: flex; flex-direction: column; gap: 0.5rem; pointer-events: none; }
.toast { padding: 0.75rem 1.5rem; border-radius: var(--radius-full); color: #fff; font-size: 0.9rem; animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards; }
.toast-success { background: #27ae60; } .toast-error { background: #e74c3c; } .toast-info { background: #3498db; }
@keyframes toastIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; transform: translateY(-10px); } }

/* Responsive */
@media (max-width: 500px) {
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .tab-btn { padding: 0.4rem 0.15rem; font-size: 0.7rem; }
    .setting-item { gap: 0.25rem; }
    .user-profile { display: none; }
    .settings-danger { flex-direction: column; }
}