* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  background: #faf8ef; font-family: -apple-system, BlinkMacSystemFont, Arial, sans-serif;
  display: flex; flex-direction: column; align-items: center;
  min-height: 100vh; padding: 6px 8px 15px;
  color: #776e65; max-width: 420px; margin: 0 auto; position: relative;
}
.theme-toggle {
  position: absolute; top: 10px; right: 10px;
  background: transparent; border: none; font-size: 16px;
  cursor: pointer; padding: 4px; opacity: 0.5;
  transition: opacity 0.2s;
}
.theme-toggle:hover { opacity: 0.8; }
.hd { text-align: center; margin-bottom: 2px; }
.hd h1 { font-size: 32px; font-weight: 900; color: #776e65; }
.hd .subtitle { font-size: 10px; color: #9f8f79; margin-top: 0px; }
.scores { display: flex; gap: 6px; margin-bottom: 6px; width: 100%; }
.score-box {
  flex: 1; background: #bbada0; border-radius: 6px;
  padding: 5px 4px; text-align: center; min-width: 0;
}
.score-box .label { font-size: 9px; color: #eee4da; font-weight: 700; text-transform: uppercase; }
.score-box .value { font-size: 18px; font-weight: 800; color: #fff; }
.compact-row { display: flex; gap: 6px; margin-bottom: 6px; width: 100%; align-items: center; }
.sound-row { flex: 1; display: flex; align-items: center; gap: 4px; }
.sound-row .lbl { font-size: 10px; font-weight: 600; color: #9f8f79; white-space: nowrap; }
.sound-select {
  flex: 1; padding: 4px 22px 4px 8px; border: 1px solid #bbada0; border-radius: 12px;
  background: #fff; font-size: 11px; font-weight: 600; color: #776e65;
  cursor: pointer; appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 12 8'%3E%3Cpath fill='%23776e65' d='M6 8L0 0h12z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 6px center;
}
.sound-select:focus { outline: none; border-color: #f67c5f; }
.theme-bar { display: none; }
.controls { display: flex; gap: 6px; margin-bottom: 8px; width: 100%; }
.btn {
  border: none; border-radius: 6px; font-size: 13px; font-weight: 600;
  cursor: pointer; padding: 7px 8px; transition: all 0.12s ease;
  min-width: 0;
}
.btn:nth-child(-n+4) { flex: 2; }
.btn:nth-child(n+5) { flex: 1; }
.btn:active { transform: scale(0.96); }
.btn-new     { background: #8f7a66; color: #fff; }
.btn-auto    { background: #f67c5f; color: #fff; }
.btn-pause   { background: #edcf72; color: #776e65; }
.btn-step    { background: #bbada0; color: #fff; }
.btn-undo    { background: #9b59b6; color: #fff; }
.btn-trophy  { background: #ffffff !important; color: #f39c12 !important; border: 1px solid #f39c12 !important; }
.btn-history { background: #ffffff !important; color: #3498db !important; border: 1px solid #3498db !important; }
.btn-sound   { background: #ffffff !important; color: #1abc9c !important; border: 1px solid #1abc9c !important; }
.speed-row { display: flex; align-items: center; gap: 4px; flex: 1; }
.speed-row .lbl { font-size: 10px; font-weight: 600; color: #9f8f79; white-space: nowrap; }
.speed-row input[type=range] { flex: 1; accent-color: #f67c5f; height: 16px; }
.speed-row .val { font-size: 10px; font-weight: 600; color: #776e65; width: 35px; text-align: right; }
.ai-status { font-size: 12px; color: #f67c5f; font-weight: 700; min-height: 18px; margin-bottom: 6px; text-align: center; }
.thinking-dots span { display: inline-block; animation: blink 1.2s infinite; font-size: 16px; color: #f67c5f; }
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,80%,100%{opacity:0;} 40%{opacity:1;} }
.board-wrap { position: relative; width: 100%; display: flex; justify-content: center; }
#board {
  background: #bbada0; border-radius: 10px; padding: 8px;
  display: grid; grid-template-columns: repeat(4, minmax(70px, 1fr));
  gap: 8px; width: min(88vw, 360px); aspect-ratio: 1;
  touch-action: none;
}
.cell {
  border-radius: 6px; background: rgba(238,228,218,0.35);
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(16px, 5vw, 30px); font-weight: 800; color: #776e65;
  transition: transform 0.08s ease, background 0.08s ease;
  position: relative; overflow: hidden;
  width: 100%; aspect-ratio: 1;
}
.cell.pop { animation: pop 0.12s ease; }
.cell.new-tile { animation: appear 0.15s ease; }
@keyframes pop { 0%{transform:scale(1)} 50%{transform:scale(1.18)} 100%{transform:scale(1)} }
@keyframes appear { 0%{transform:scale(0);opacity:0} 80%{transform:scale(1.12)} 100%{transform:scale(1);opacity:1} }
.v0    { background: rgba(238,228,218,0.35); }
.v2    { background: #eee4da; color: #776e65; }
.v4    { background: #ede0c8; color: #776e65; }
.v8    { background: #f2b179; color: #f9f6f2; }
.v16   { background: #f59563; color: #f9f6f2; }
.v32   { background: #f67c5f; color: #f9f6f2; }
.v64   { background: #f65e3b; color: #f9f6f2; }
.v128  { background: #edcf72; color: #f9f6f2; }
.v256  { background: #edcc61; color: #f9f6f2; }
.v512  { background: #edc850; color: #f9f6f2; }
.v1024 { background: #edc53f; color: #f9f6f2; }
.v2048 { background: #edc22e; color: #f9f6f2; }
.vbig  { background: #3c3a32; color: #f9f6f2; }
#overlay { display: none; position: absolute; inset: 0; background: rgba(238,228,218,0.82); border-radius: 10px; align-items: center; justify-content: center; flex-direction: column; gap: 12px; z-index: 10; padding: 20px; text-align: center; }
#overlay.show { display: flex; animation: fadeIn 0.3s ease; }
@keyframes fadeIn { from { opacity: 0; transform: scale(0.9); } to { opacity: 1; transform: scale(1); } }
#overlayTitle { font-size: 28px; font-weight: 800; color: #776e65; line-height: 1.2; }
#overlayMsg { font-size: 14px; color: #776e65; line-height: 1.5; }
#overlayStats { display: flex; gap: 20px; margin: 8px 0; }
.stat-item { text-align: center; }
.stat-label { font-size: 11px; color: #9f8f79; text-transform: uppercase; font-weight: 600; }
.stat-value { font-size: 20px; font-weight: 800; color: #f67c5f; }
#overlay .btn-group { display: flex; gap: 8px; margin-top: 4px; flex-wrap: wrap; justify-content: center; }
#overlay .btn { padding: 10px 20px !important; border: none !important; border-radius: 8px !important; font-size: 14px !important; font-weight: 700 !important; cursor: pointer; color: #fff; display: inline-block !important; line-height: 1.4 !important; white-space: nowrap; flex: none !important; transition: all 0.15s; }
#overlay .btn-primary { background: #f67c5f; }
#overlay .btn-secondary { background: #8f7a66; }
#overlay .btn-share { background: linear-gradient(135deg, #3b5998, #1da1f2); }
#overlay .btn:hover { filter: brightness(1.1); transform: translateY(-1px); }
#overlay .btn:active { transform: scale(0.97); }
.gameover-emoji { font-size: 50px; margin-bottom: 8px; }
.win-celebration { font-size: 60px; animation: bounce 0.5s ease infinite alternate; }
@keyframes bounce { from { transform: translateY(0); } to { transform: translateY(-10px); } }
.ach-row { display: flex; gap: 6px; margin-top: 8px; width: 100%; justify-content: center; }
#achBtn { background: linear-gradient(135deg,#f5d76e,#f39c12); color: #fff; border: none; border-radius: 6px; padding: 8px 14px; font-size: 13px; font-weight: 700; cursor: pointer; }
#achBtn:active { transform: scale(0.96); }
#achPanel { display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%); background: #fff; border-radius: 12px; padding: 16px; width: min(340px, 90vw); max-height: 70vh; overflow-y: auto; box-shadow: 0 8px 32px rgba(0,0,0,0.2); z-index: 100; }
#achPanel.show { display: block; }
#achPanel h3 { font-size: 15px; color: #776e65; margin-bottom: 10px; display: flex; align-items: center; gap: 8px; }
.ach-item { display: flex; align-items: center; gap: 10px; padding: 7px; border-radius: 8px; margin-bottom: 5px; background: #faf8ef; }
.ach-item.locked { opacity: 0.45; }
.ach-icon { font-size: 22px; width: 34px; text-align: center; }
.ach-info { flex: 1; }
.ach-name { font-size: 12px; font-weight: 700; color: #776e65; }
.ach-desc { font-size: 10px; color: #9f8f79; margin-top: 1px; }
.ach-unlocked { font-size: 10px; color: #f67c5f; font-weight: 700; }
#achToast {
  position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%) translateY(100px);
  background: linear-gradient(135deg,#f5d76e,#f39c12); color: #fff;
  padding: 12px 20px; border-radius: 12px; font-weight: 800; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.2); transition: transform 0.4s cubic-bezier(0.34,1.56,0.64,1);
  z-index: 200; white-space: nowrap;
}
#achToast.show { transform: translateX(-50%) translateY(0); }
#milestone { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.72); align-items: center; justify-content: center; flex-direction: column; z-index: 300; }
#milestone.show { display: flex; }
#milestone h2 { font-size: 36px; color: #fff; margin-bottom: 14px; text-shadow: 0 4px 12px rgba(0,0,0,0.4); }
#milestone .trophy { font-size: 70px; margin-bottom: 14px; animation: bounce 0.6s ease infinite alternate; }
#milestone .btn { background: #f67c5f; color: #fff; margin-top: 14px; padding: 12px 28px; border: none; border-radius: 8px; font-size: 15px; font-weight: 700; cursor: pointer; }
#confetti-canvas { position: fixed; inset: 0; pointer-events: none; z-index: 301; }
body.dark {
  background: #1a1a2e;
  color: #c8c0b8;
}
body.dark .score-box { background: #2d2d44; }
body.dark .score-box .label { color: #aaa; }
body.dark .score-box .value { color: #e8e0d0; }
body.dark .hd h1 { color: #e8e0d0; }
body.dark .hd .subtitle { color: #88807a; }
body.dark .sound-row .lbl { color: #88807a; }
body.dark .speed-row .lbl { color: #88807a; }
body.dark .speed-row .val { color: #c8c0b8; }
body.dark .speed-row input[type=range] { accent-color: #f67c5f; }
body.dark #board { background: #2d2d44; }
body.dark .cell { color: #e8e0d0; }
body.dark #overlay { background: rgba(26,26,46,0.92); }
body.dark #overlayTitle { color: #e8e0d0; }
body.dark #overlayMsg { color: #aaa; }
body.dark .stat-label { color: #88807a; }
body.dark #achPanel { background: #252540; }
body.dark #achPanel h3 { color: #e8e0d0; }
body.dark .ach-item { background: #1e1e36; }
body.dark .ach-item.locked { opacity: 0.3; }
body.dark .ach-name { color: #e8e0d0; }
body.dark .ach-desc { color: #88807a; }
body.dark #milestone { background: rgba(0,0,0,0.85); }
body.dark .ai-status { color: #f67c5f; }
body.dark .thinking-dots span { color: #f67c5f; }
body.dark .v0    { background: rgba(45,45,68,0.8); }
body.dark .v2    { background: #4a4a6a; color: #e8e0d0; }
body.dark .v4    { background: #5a5a8a; color: #e8e0d0; }
body.dark .v8    { background: #6a4a2a; color: #f9f6f2; }
body.dark .v16   { background: #8a5a2a; color: #f9f6f2; }
body.dark .v32   { background: #9a4a3a; color: #f9f6f2; }
body.dark .v64   { background: #aa3a1a; color: #f9f6f2; }
body.dark .v128  { background: #8a7a2a; color: #f9f6f2; }
body.dark .v256  { background: #9a8a3a; color: #f9f6f2; }
body.dark .v512  { background: #aa9a4a; color: #f9f6f2; }
body.dark .v1024 { background: #baa040; color: #1a1a2e; }
body.dark .v2048 { background: #dab010; color: #1a1a2e; }
body.dark .vbig  { background: #dab010; color: #1a1a2e; }
body.dark .btn-new     { background: #6a5a4a; color: #fff; }
body.dark .btn-auto    { background: #c0503f; color: #fff; }
body.dark .btn-pause   { background: #9a8a2a; color: #1a1a2e; }
body.dark .btn-step    { background: #4a4a6a; color: #fff; }
body.dark .btn-undo    { background: #6c3483; color: #fff; }
body.dark .btn-trophy  { background: transparent !important; color: #f39c12 !important; border-color: #f39c12 !important; }
body.dark .btn-history { background: transparent !important; color: #3498db !important; border-color: #3498db !important; }
body.dark .btn-sound   { background: transparent !important; color: #1abc9c !important; border-color: #1abc9c !important; }
body.dark .sound-select { border-color: #4a4a6a; background-color: #252540; color: #e8e0d0; }
body.dark .theme-bar { display: none; }

.history-panel {
  position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%) scale(0.9);
  background: #fff; border-radius: 12px; padding: 16px; width: min(360px, 90vw);
  max-height: 80vh; overflow-y: auto; box-shadow: 0 8px 32px rgba(0,0,0,0.3);
  z-index: 150; opacity: 0; visibility: hidden; transition: all 0.25s ease;
}
.history-panel.show { opacity: 1; visibility: visible; transform: translate(-50%,-50%) scale(1); }
.history-panel h3 { font-size: 15px; color: #776e65; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.history-panel h3 button { margin-left: auto; background: none; border: none; font-size: 18px; cursor: pointer; color: #776e65; padding: 4px; }
.history-item {
  display: grid; grid-template-columns: 50px 1fr 70px; gap: 8px; align-items: center;
  padding: 10px; border-radius: 8px; margin-bottom: 6px; background: #faf8ef;
}
.history-item.empty { text-align: center; color: #9f8f79; padding: 20px; display: block; }
.history-rank { font-size: 18px; font-weight: 800; text-align: center; }
.history-rank.gold { color: #f39c12; }
.history-rank.silver { color: #95a5a6; }
.history-rank.bronze { color: #cd7f32; }
.history-rank.normal { color: #776e65; }
.history-info { display: flex; flex-direction: column; gap: 2px; }
.history-score { font-size: 14px; font-weight: 700; color: #776e65; }
.history-detail { font-size: 11px; color: #9f8f79; }
.history-date { font-size: 10px; color: #9f8f79; text-align: right; }
.history-actions { display: flex; gap: 6px; margin-top: 10px; }
.history-actions .btn { flex: 1; }
body.dark .history-panel { background: #252540; }
body.dark .history-panel h3 { color: #e8e0d0; }
body.dark .history-item { background: #1e1e36; }
body.dark .history-score { color: #e8e0d0; }

