:root {
  --bg-1: #f6f8f4;
  --bg-2: #eef3ea;
  --panel: rgba(255, 255, 255, 0.72);
  --panel-strong: rgba(255, 255, 255, 0.88);
  --text: #233127;
  --muted: #66756b;
  --grid: #dbe4d7;
  --cell: rgba(255, 255, 255, 0.42);
  --shadow: 0 12px 30px rgba(63, 90, 68, 0.12);
  --radius-xl: 28px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --page-gap: 12px;
  --page-pad-x: 12px;
  --page-pad-y-top: calc(10px + var(--safe-top));
  --page-pad-y-bottom: calc(12px + var(--safe-bottom));
  --desktop-left-col: 360px;
  --board-frame-pad: 10px;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top, #ffffff 0%, rgba(255, 255, 255, 0.72) 16%, transparent 44%),
    linear-gradient(180deg, var(--bg-1) 0%, var(--bg-2) 100%);
}

body {
  display: flex;
  justify-content: center;
  align-items: stretch;
}

.app-shell {
  width: 100%;
  max-width: 1440px;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: var(--page-gap);
  padding:
    var(--page-pad-y-top)
    var(--page-pad-x)
    var(--page-pad-y-bottom)
    var(--page-pad-x);
}

.side-panel {
  display: grid;
  grid-template-rows: auto auto;
  gap: var(--page-gap);
  min-height: 0;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  align-items: start;
  padding: 12px 14px;
  border-radius: var(--radius-xl);
  background: var(--panel);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
}

.title-wrap h1 {
  margin: 0;
  font-size: clamp(1.55rem, 3.8vw, 2rem);
  line-height: 1;
  letter-spacing: -0.03em;
}

.title-wrap p {
  margin: 6px 0 0;
  font-size: 0.9rem;
  color: var(--muted);
  line-height: 1.2;
  max-width: 32ch;
}

.topbar-right {
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
    display: grid;
    grid-template-columns: 1fr 1fr;
}

.lang-box {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 12px;
  border-radius: 18px;
  background: var(--panel-strong);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.65);
}

.lang-box label {
  font-size: 0.72rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.lang-box select {
  appearance: none;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 700;
  padding: 0;
}

.score-panel {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.score-box {
  min-width: 82px;
  padding: 10px 12px;
  border-radius: 18px;
  background: var(--panel-strong);
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.65);
}

.score-label {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 4px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.score-box strong {
  font-size: 1.18rem;
  line-height: 1;
}

.game-area {
  min-height: 0;
  display: flex;
}

.board-frame {
  position: relative;
  width: 100%;
  min-height: 0;
  border-radius: 30px;
  padding: var(--board-frame-pad);
  background: rgba(255, 255, 255, 0.45);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.board {
  position: relative;
  width: 100%;
  max-width: min(100%, calc(100dvh - 220px));
  aspect-ratio: 1 / 1;
  border-radius: 26px;
  background: var(--grid);
  box-shadow:
    inset 0 2px 6px rgba(255,255,255,0.3),
    inset 0 -2px 8px rgba(110, 130, 114, 0.08);
}

.cell,
.tile {
  position: absolute;
  border-radius: 18px;
}

.cell {
  background: var(--cell);
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  font-weight: 700;
  user-select: none;
  box-shadow:
    0 8px 18px rgba(80, 110, 88, 0.12),
    inset 0 1px 0 rgba(255,255,255,0.45);
  transition:
    left 0.18s ease,
    top 0.18s ease,
    transform 0.16s ease,
    opacity 0.16s ease;
  overflow: hidden;
  text-align: center;
  padding: 4px;
  will-change: left, top, transform, opacity;
}

.tile .emoji {
  line-height: 1;
  margin-bottom: 3px;
  filter: saturate(1.05);
}

.tile .name {
  font-size: 0.68em;
  line-height: 1.05;
  letter-spacing: -0.02em;
}

.tile.spawn {
  animation: pop-in 0.18s ease;
}

.tile.merge {
  animation: merge-bounce 0.22s ease;
}

.tile.fade-out {
  opacity: 0;
  transform: scale(0.86);
}

@keyframes pop-in {
  0% {
    opacity: 0;
    transform: scale(0.55);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes merge-bounce {
  0% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.12);
  }
  100% {
    transform: scale(1);
  }
}

.overlay {
  position: absolute;
  inset: 0;
  border-radius: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(241, 246, 239, 0.72);
  backdrop-filter: blur(8px);
  z-index: 20;
}

.hidden {
  display: none;
}

.overlay-card {
  width: min(82%, 320px);
  padding: 24px 20px;
  border-radius: 24px;
  background: rgba(255,255,255,0.9);
  box-shadow: var(--shadow);
  text-align: center;
}

.overlay-card h2 {
  margin: 0 0 8px;
  font-size: 1.5rem;
}

.overlay-card p {
  margin: 0 0 18px;
  color: var(--muted);
}

.bottombar {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 12px;
  align-items: center;
  padding: 10px 12px;
  border-radius: var(--radius-xl);
  background: var(--panel);
  backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
}

.primary-btn,
.secondary-btn {
  appearance: none;
  border: none;
  border-radius: 16px;
  padding: 14px 18px;
  font-size: 0.98rem;
  font-weight: 700;
  cursor: pointer;
  color: var(--text);
}

.primary-btn {
  background: linear-gradient(180deg, #8ed09e 0%, #78c18a 100%);
}

.secondary-btn {
    background: black;
    color: white;
}

.hint {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.2;
  text-align: right;
}

@media (max-width: 560px) {
  .topbar {
    grid-template-columns: 1fr;
  }

  .topbar-right {
    width: 100%;
  }

  .score-panel {
    justify-content: flex-start;
  }
}

@media (max-width: 420px) {
  :root {
    --page-gap: 10px;
    --page-pad-x: 8px;
    --page-pad-y-top: calc(8px + var(--safe-top));
    --page-pad-y-bottom: calc(10px + var(--safe-bottom));
  }

  .topbar,
  .bottombar {
    padding: 10px;
  }

  .score-box {
    min-width: 74px;
  }

  .hint {
    font-size: 0.8rem;
  }
}

/* Desktop: only one true horizontal layout */
@media (min-width: 900px) {
  .app-shell {
    height: 100dvh;
    grid-template-columns: minmax(300px, var(--desktop-left-col)) minmax(0, 1fr);
    grid-template-rows: 1fr;
    align-items: stretch;
  }

  .side-panel {
    height: 100%;
    grid-template-rows: 1fr auto;
  }

  .topbar {
    min-height: 0;
    height: 100%;
    align-content: start;
    grid-template-columns: 1fr;
  }

  .topbar-right {
    align-items: stretch;
  }

  .score-panel {
    justify-content: flex-start;
  }

  .bottombar {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .hint {
    text-align: left;
  }

  .game-area {
    min-width: 0;
    min-height: 0;
    height: 100%;
  }

  .board-frame {
    width: 100%;
    height: 100%;
  }

  .board {
    width: min(
      calc(100vh - 48px),
      calc(100vw - var(--desktop-left-col) - (var(--page-pad-x) * 2) - var(--page-gap) - 24px)
    );
    max-width: 100%;
    max-height: 100%;
  }
}