:root {
  --bg: #faf8ef;
  --fg: #776e65;
  --panel: #bbada0;
  --cell: #cdc1b4;
  --btn: #8f7a66;
  --btnfg: #f9f6f2;
}

html[data-theme="dark"] {
  --bg: #1d1b19;
  --fg: #e6ded5;
  --panel: #3a3632;
  --cell: #4a453f;
  --btn: #5b5047;
  --btnfg: #f9f6f2;
}

@media (prefers-color-scheme: dark) {
  html[data-theme="system"] {
    --bg: #1d1b19;
    --fg: #e6ded5;
    --panel: #3a3632;
    --cell: #4a453f;
    --btn: #5b5047;
    --btnfg: #f9f6f2;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Clear Sans", "Helvetica Neue", Arial, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  transition: background 0.25s ease, color 0.25s ease;
}

/* ── LAYOUT CHÍNH & TỐI ƯU RESPONSIVE TỶ LỆ VÀNG ───────────── */
#app {
  --c: 4;
  --r: 4;
  --rr: 4;
  --top-space: 170px;
  --bw: min(94vw, 520px, calc((100dvh - var(--top-space)) * var(--c) / var(--rr)));
  width: min(94vw, max(270px, var(--bw)));
  height: 100%;
  max-height: 100dvh;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 8px max(12px, env(safe-area-inset-bottom)) 8px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: clamp(6px, 1.6vh, 12px);
}

/* ── HEADER ────────────────────────────────────────────────── */
header {
  display: flex;
  align-items: center;
  gap: clamp(5px, 1.8vw, 10px);
  width: 100%;
  flex-shrink: 0;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  width: clamp(32px, 8vw, 40px);
  height: clamp(32px, 8vw, 40px);
  flex: none;
  text-decoration: none;
  transition: transform 0.2s ease;
}

.logo:hover {
  transform: scale(1.05);
}

.logo img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
}

h1 {
  margin: 0 auto 0 0;
  font-size: clamp(24px, 7vw, 40px);
  font-weight: 800;
  line-height: 1;
  color: var(--fg);
  letter-spacing: 0.5px;
}

/* Các nút tròn trên header (New, Undo, Mute, Theme) */
.circle {
  flex: none;
  width: clamp(32px, 8.5vw, 40px);
  height: clamp(32px, 8.5vw, 40px);
  border-radius: 50%;
  border: 0;
  background: var(--btn);
  color: var(--btnfg);
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  outline: none !important;
  transition: transform 0.15s ease, background 0.2s ease, opacity 0.2s ease;
}

.circle:hover {
  transform: scale(1.08);
}

.circle:active {
  transform: scale(0.94);
}

.circle:disabled {
  opacity: 0.35;
  cursor: default;
  pointer-events: none;
  transform: none !important;
}

/* Icon New Game (+) */
.i-new {
  position: relative;
  width: 16px;
  height: 16px;
}
.i-new::before, .i-new::after {
  content: "";
  position: absolute;
  background: currentColor;
  border-radius: 2px;
}
.i-new::before {
  left: 0;
  right: 0;
  top: 6.5px;
  height: 3px;
}
.i-new::after {
  top: 0;
  bottom: 0;
  left: 6.5px;
  width: 3px;
}

/* Icon Undo (mũi tên quay ngược) */
.i-undo {
  position: relative;
  width: 16px;
  height: 16px;
  border: 3px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
}
.i-undo::after {
  content: "";
  position: absolute;
  right: -6px;
  top: -2px;
  border: 5px solid transparent;
  border-left: 7px solid currentColor;
  border-right: 0;
  transform: rotate(40deg);
}

/* ── NÚT THEME (LIGHT / DARK / SYSTEM) ─────────────────────── */
.i-theme {
  display: block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  box-sizing: border-box;
  transition: all 0.2s ease;
}

/* Chế độ Sáng: Mặt trời viền tròn sáng */
html[data-theme="light"] .i-theme {
  background: #f59e0b;
  box-shadow: 0 0 0 2px var(--btn), 0 0 0 4px #f59e0b;
  transform: scale(0.72);
}

/* Chế độ Tối: Trăng khuyết bóng đổ */
html[data-theme="dark"] .i-theme {
  background: transparent;
  box-shadow: inset -5px -3px 0 0 #f8fafc;
  transform: rotate(-15deg) scale(0.95);
}

/* Chế độ Tự động (System): Nửa sáng nửa tối */
html[data-theme="system"] .i-theme {
  border: 2px solid currentColor;
  background: linear-gradient(90deg, currentColor 50%, transparent 50%);
}

/* ── NÚT MUTE (SOUND ON / OFF) ─────────────────────────────── */
.i-mute {
  width: 20px;
  height: 20px;
  display: block;
}

.i-mute .x {
  display: none;
  stroke: #ef4444;
  stroke-width: 3;
}

.circle.muted {
  background: var(--btn);
}

.circle.muted .i-mute .w {
  display: none;
}

.circle.muted .i-mute .x {
  display: block;
}

.circle.muted .i-mute path:first-child {
  opacity: 0.7;
}

/* ── CÁC NÚT CHỌN CHẾ ĐỘ (4x4, 4x6, 5x8, SHOOTER) ────────── */
#modes {
  display: flex;
  gap: 6px;
  width: 100%;
  flex-shrink: 0;
}

#modes button {
  flex: 1;
  padding: 8px 4px;
  border: 0;
  border-radius: 6px;
  background: var(--panel);
  color: #ffffff;
  font: 700 clamp(12px, 3.2vw, 15px) inherit;
  font-family: inherit;
  cursor: pointer;
  opacity: 0.65;
  transition: all 0.15s ease;
  white-space: nowrap;
  outline: none !important;
}

#modes button:hover {
  opacity: 0.9;
  transform: translateY(-1px);
}

#modes button.on {
  opacity: 1;
  background: var(--btn);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

/* ── THANH THỐNG KÊ (SCORE, BEST, NEXT TILE) ───────────────── */
.bar {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  width: 100%;
  flex-shrink: 0;
}

.sc {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 6px;
  background: var(--panel);
  color: #ffffff;
  font-weight: 800;
  line-height: 1;
}

.sc.best {
  font-size: clamp(14px, 3.6vw, 16px);
  min-width: 80px;
  height: 100%;
  padding: 0 10px;
}

#score {
  font-size: clamp(20px, 5.5vw, 28px);
  min-width: 90px;
  height: 100%;
  padding: 0 12px;
}

/* Icon Cúp kỷ lục */
.cup {
  position: relative;
  isolation: isolate;
  flex: none;
  width: 12px;
  height: 10px;
  margin: 0 4px 4px 2px;
  background: #f7d154;
  border-radius: 1px 1px 6px 6px;
}
.cup::before {
  content: "";
  position: absolute;
  left: -5px;
  right: -5px;
  top: 1px;
  height: 5px;
  border: 2px solid #f7d154;
  border-radius: 3px;
  z-index: -1;
}
.cup::after {
  content: "";
  position: absolute;
  left: 4px;
  width: 4px;
  top: 10px;
  height: 2px;
  background: #f7d154;
  box-shadow: -2px 2px 0 0 #f7d154, 2px 2px 0 0 #f7d154;
}

/* Ô bóng/gạch kế tiếp (chế độ Shooter) */
#next {
  margin-left: auto;
  width: 44px;
  height: 44px;
  position: relative;
  display: none;
  flex-shrink: 0;
}
#next .tile {
  position: static;
  width: 100%;
  height: 100%;
  padding: 0;
  transform: none;
}
#next i {
  font-size: 15px !important;
}

/* ── BÀN CỜ CHÍNH (BOARD) ─────────────────────────────────── */
#board {
  --k: 1;
  position: relative;
  width: 100%;
  aspect-ratio: var(--c) / var(--rr);
  background: var(--panel);
  border-radius: 8px;
  container-type: inline-size;
  touch-action: none;
  flex-shrink: 0;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.15);
}

#cells {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: calc(100% * var(--r) / var(--rr));
  display: grid;
  grid-template-columns: repeat(var(--c), 1fr);
  grid-template-rows: repeat(var(--r), 1fr);
}

#cells div {
  margin: 4px;
  border-radius: 6px;
  background: var(--cell);
  opacity: 0.65;
}

/* Từng ô số 2048 (Tile) */
.tile {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(100% / var(--c));
  height: calc(100% / var(--rr));
  padding: 4px;
  transform: translate(calc(var(--x) * 100%), calc(var(--y) * 100%));
  transition: transform 0.16s ease;
  pointer-events: none;
}

.tile i {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  background: var(--tb);
  color: var(--tf);
  font-style: normal;
  font-weight: 800;
  font-size: calc(100cqw / var(--c) * 0.42 * var(--k));
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.tile[data-l="3"] { --k: 0.82; }
.tile[data-l="4"] { --k: 0.65; }
.tile[data-l="5"] { --k: 0.52; }

.tile.new i {
  animation: nw 0.18s ease-out;
}
.tile.pop i {
  animation: pp 0.16s ease-in-out;
}

@keyframes nw {
  from { transform: scale(0.2); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes pp {
  50% { transform: scale(1.18); }
}

/* Tia ngắm & bóng mờ (Shooter mode) */
#guide {
  position: absolute;
  left: 0;
  width: calc(100% / var(--c));
  transform: translateX(calc(var(--gx, 2) * 100%));
  transition: transform 0.1s ease;
  pointer-events: none;
  display: none;
}
#guide::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 6px;
  bottom: 6px;
  border-left: 3px dotted var(--fg);
  opacity: 0.45;
}

#ghost {
  display: none;
  pointer-events: none;
  transition: transform 0.08s ease;
  opacity: 0.45;
  z-index: 3;
}
#ghost i {
  background: var(--tb);
  color: var(--tf);
  border: 2px dashed rgba(119, 110, 101, 0.65);
}
html[data-theme="dark"] #ghost i {
  border-color: rgba(255, 255, 255, 0.65);
}

/* ── MODAL GAME OVER ───────────────────────────────────────── */
#over {
  position: absolute;
  inset: 0;
  z-index: 9;
  background: rgba(238, 228, 218, 0.88);
  color: #776e65;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  font-size: 32px;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}
html[data-theme="dark"] #over {
  background: rgba(29, 27, 25, 0.9);
  color: #e6ded5;
}
#over[hidden] {
  display: none;
}

.btn {
  height: 40px;
  min-width: 120px;
  padding: 0 16px;
  border: 0;
  border-radius: 6px;
  background: var(--btn);
  color: var(--btnfg);
  font: 700 16px inherit;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
  transition: transform 0.15s ease;
}
.btn:hover {
  transform: scale(1.04);
}
.btn:active {
  transform: scale(0.96);
}

/* Lời khen ngợi khi combo gộp ô số (Good job, Amazing...) */
#toast {
  position: absolute;
  left: 0;
  right: 0;
  top: 38%;
  text-align: center;
  font-size: clamp(26px, 8.5cqw, 48px);
  font-weight: 800;
  color: #f9f6f2;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  opacity: 0;
  z-index: 8;
}
#toast.show {
  animation: ts 1s ease-out;
}

@keyframes ts {
  0% { opacity: 0; transform: scale(0.6); }
  20% { opacity: 1; transform: scale(1.1); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-30px); }
}

.shake {
  animation: sk 0.2s ease-in-out;
}
@keyframes sk {
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

/* ── MÀN HÌNH NHỎ & MÀN HÌNH THẤP (LANDSCAPE) ───────────────── */
@media (max-width: 380px) {
  #app {
    padding-left: 6px;
    padding-right: 6px;
    gap: 8px;
  }
  .circle {
    width: 32px;
    height: 32px;
  }
  h1 {
    font-size: 24px;
  }
  .logo {
    width: 30px;
    height: 30px;
  }
  #modes button {
    padding: 6px 2px;
    font-size: 12px;
  }
  .sc.best {
    min-width: 72px;
    padding: 0 6px;
  }
  #score {
    min-width: 76px;
    font-size: 20px;
  }
}

@media (max-height: 600px) {
  #app {
    --top-space: 120px;
    gap: 4px;
    padding-top: 4px;
    padding-bottom: 6px;
  }
  header {
    gap: 4px;
  }
  .circle {
    width: 30px;
    height: 30px;
  }
  h1 {
    font-size: 20px;
  }
  #modes button {
    padding: 4px 0;
    font-size: 12px;
  }
  .bar {
    height: 34px;
    gap: 6px;
  }
  .sc {
    padding: 2px 8px;
  }
  #score {
    font-size: 18px;
  }
  #next {
    width: 34px;
    height: 34px;
  }
}
