:root {
  color-scheme: light;
  --ink: #182022;
  --muted: #63706b;
  --paper: #f7f3e8;
  --line: #263a38;
  --board: #8a8174;
  --cell: #c9bda9;
  --panel: #fffaf0;
  --shell: #fff9ea;
  --accent: #217c78;
  --accent-dark: #175d5a;
  --danger: #b84b3a;
  --bg-a: rgba(33, 124, 120, 0.12);
  --bg-b: rgba(184, 75, 58, 0.12);
  --overlay-bg: rgba(255, 249, 234, 0.86);
  --tile-2: #f5ead7;
  --tile-4: #ead9bc;
  --tile-8: #f1ae62;
  --tile-16: #e9804d;
  --tile-32: #d95743;
  --tile-64: #b84b3a;
  --tile-128: #65a384;
  --tile-256: #3d927f;
  --tile-512: #217c78;
  --tile-1024: #174b54;
  --tile-super: #1d2733;
  --tile-light-text: #fffaf0;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  color: var(--ink);
  background:
    linear-gradient(135deg, var(--bg-a), transparent 36%),
    linear-gradient(315deg, var(--bg-b), transparent 40%),
    var(--paper);
  display: grid;
  place-items: center;
  padding: 24px;
}

.app {
  width: min(100%, 520px);
}

.app[hidden] {
  display: none;
}

.lock-screen {
  width: min(100%, 420px);
}

.lock-screen[hidden] {
  display: none;
}

.lock-box {
  border: 3px solid var(--line);
  background: var(--shell);
  box-shadow: 8px 8px 0 var(--line);
  padding: 18px;
}

.lock-box h2 {
  margin: 0 0 16px;
  font-size: 2rem;
}

.lock-field {
  display: grid;
  gap: 6px;
  margin-bottom: 10px;
  border: 2px solid var(--line);
  background: var(--panel);
  box-shadow: 3px 3px 0 var(--line);
  padding: 9px 11px;
}

.lock-field span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
}

.lock-error {
  margin: 0 0 12px;
  color: var(--danger);
  font-weight: 900;
}

#lock-submit {
  width: 100%;
  height: 44px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: end;
  margin-bottom: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(3.6rem, 18vw, 6.4rem);
  line-height: 0.82;
  letter-spacing: 0;
}

.scores {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.score-box {
  min-width: 78px;
  padding: 10px 12px;
  border: 2px solid var(--line);
  background: var(--panel);
  box-shadow: 4px 4px 0 var(--line);
  text-align: center;
}

.score-box span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
}

.score-box strong {
  display: block;
  margin-top: 2px;
  font-size: 1.35rem;
}

.game-shell {
  border: 3px solid var(--line);
  background: var(--shell);
  box-shadow: 8px 8px 0 var(--line);
  padding: 14px;
}

.player-strip {
  display: grid;
  grid-template-columns: 1fr 104px 104px;
  gap: 10px;
  margin-bottom: 12px;
}

.player-field,
.rank-badge,
.network-badge {
  min-width: 0;
  border: 2px solid var(--line);
  background: var(--panel);
  box-shadow: 3px 3px 0 var(--line);
  padding: 8px 10px;
}

.player-field {
  display: grid;
  gap: 4px;
}

.player-field span,
.rank-badge span,
.network-badge span {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 900;
}

input {
  width: 100%;
  min-width: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  font-weight: 900;
  padding: 0;
}

.rank-badge,
.network-badge {
  display: grid;
  align-content: center;
  gap: 4px;
  text-align: center;
}

.rank-badge strong,
.network-badge strong {
  font-size: 1.18rem;
  line-height: 1;
}

.network-badge[data-state="online"] strong {
  color: var(--accent);
}

.network-badge[data-state="offline"] strong {
  color: var(--danger);
}

.toolbar {
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.toolbar p {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.controls {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.theme-picker {
  position: relative;
}

.theme-trigger {
  min-width: 176px;
  height: 42px;
  background: var(--panel);
  color: var(--ink);
  display: grid;
  grid-template-columns: 28px 1fr 18px;
  align-items: center;
  gap: 8px;
  padding: 6px 9px;
  text-align: left;
}

.theme-chip {
  width: 26px;
  height: 26px;
  border: 2px solid var(--line);
  background:
    linear-gradient(135deg, var(--tile-8) 0 50%, transparent 50%),
    linear-gradient(315deg, var(--accent) 0 50%, var(--tile-2) 50%);
}

.theme-text {
  min-width: 0;
  display: grid;
  gap: 1px;
}

.theme-kicker {
  color: var(--muted);
  font-size: 0.64rem;
  font-weight: 900;
  line-height: 1;
}

#theme-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.88rem;
  line-height: 1.1;
}

.theme-arrow {
  color: var(--muted);
  font-size: 1rem;
  text-align: center;
}

.theme-panel {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  z-index: 5;
  width: min(88vw, 376px);
  max-height: min(520px, 70vh);
  border: 3px solid var(--line);
  background: var(--shell);
  box-shadow: 7px 7px 0 var(--line);
  padding: 10px;
}

.theme-panel[hidden] {
  display: none;
}

.theme-panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 9px;
}

.theme-panel-head strong {
  font-size: 0.98rem;
}

.theme-panel-head span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 900;
}

.theme-options {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
  max-height: calc(min(520px, 70vh) - 56px);
  overflow: auto;
  padding: 1px 4px 5px 1px;
}

.theme-option {
  min-width: 0;
  height: 42px;
  display: grid;
  grid-template-columns: 28px 1fr;
  align-items: center;
  gap: 8px;
  border: 2px solid color-mix(in srgb, var(--line) 66%, transparent);
  background: var(--panel);
  color: var(--ink);
  box-shadow: none;
  padding: 6px 8px;
}

.theme-option:hover,
.theme-option[aria-selected="true"] {
  border-color: var(--line);
  background: color-mix(in srgb, var(--accent) 12%, var(--panel));
}

.theme-option[aria-selected="true"] {
  box-shadow: 3px 3px 0 var(--line);
}

.theme-option-swatch {
  width: 24px;
  height: 24px;
  border: 2px solid var(--line);
}

.theme-option-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.82rem;
  font-weight: 900;
}

button {
  border: 2px solid var(--line);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-weight: 900;
  box-shadow: 3px 3px 0 var(--line);
  transition:
    transform 140ms ease,
    box-shadow 140ms ease,
    background 140ms ease;
}

button:hover {
  background: var(--accent-dark);
}

button:active {
  transform: translate(3px, 3px);
  box-shadow: 0 0 0 var(--line);
}

#restart {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  font-size: 1.45rem;
  flex: 0 0 auto;
}

#sound-toggle {
  width: 42px;
  height: 42px;
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  flex: 0 0 auto;
}

.board-wrap {
  position: relative;
}

.board {
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(8px, 2vw, 14px);
  padding: clamp(8px, 2vw, 14px);
  border: 3px solid var(--line);
  background: var(--board);
  touch-action: none;
  user-select: none;
}

.tile {
  display: grid;
  place-items: center;
  min-width: 0;
  border: 2px solid rgba(24, 32, 34, 0.7);
  background: var(--cell);
  color: var(--ink);
  font-size: clamp(1.55rem, 10vw, 3.8rem);
  font-weight: 950;
  line-height: 1;
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.08);
}

.tile[data-value="0"] {
  color: transparent;
  background: color-mix(in srgb, var(--panel) 32%, transparent);
  border-color: rgba(24, 32, 34, 0.28);
  box-shadow: none;
}

.tile[data-value="2"] {
  background: var(--tile-2);
}

.tile[data-value="4"] {
  background: var(--tile-4);
}

.tile[data-value="8"] {
  background: var(--tile-8);
  color: var(--tile-light-text);
}

.tile[data-value="16"] {
  background: var(--tile-16);
  color: var(--tile-light-text);
}

.tile[data-value="32"] {
  background: var(--tile-32);
  color: var(--tile-light-text);
}

.tile[data-value="64"] {
  background: var(--tile-64);
  color: var(--tile-light-text);
}

.tile[data-value="128"] {
  background: var(--tile-128);
  color: var(--tile-light-text);
  font-size: clamp(1.35rem, 8vw, 3rem);
}

.tile[data-value="256"] {
  background: var(--tile-256);
  color: var(--tile-light-text);
  font-size: clamp(1.35rem, 8vw, 3rem);
}

.tile[data-value="512"] {
  background: var(--tile-512);
  color: var(--tile-light-text);
  font-size: clamp(1.35rem, 8vw, 3rem);
}

.tile[data-value="1024"],
.tile[data-value="2048"] {
  background: var(--tile-1024);
  color: var(--tile-light-text);
  font-size: clamp(1.05rem, 6.6vw, 2.35rem);
}

.tile[data-value="4096"],
.tile[data-value="8192"] {
  background: var(--tile-super);
  color: var(--tile-light-text);
  font-size: clamp(1.05rem, 6.6vw, 2.35rem);
}

.tile.pop {
  animation: pop 160ms ease-out;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--overlay-bg);
  text-align: center;
}

.overlay[hidden] {
  display: none;
}

.overlay > div {
  width: min(100%, 280px);
}

.overlay strong {
  display: block;
  font-size: 2rem;
}

.overlay p {
  color: var(--muted);
  font-weight: 700;
}

#overlay-button {
  padding: 10px 16px;
}

.leaderboard-shell {
  margin-top: 18px;
  border: 3px solid var(--line);
  background: var(--shell);
  box-shadow: 8px 8px 0 var(--line);
  padding: 14px;
}

.leaderboard-head {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

h2 {
  margin: 0;
  font-size: 1.35rem;
  letter-spacing: 0;
}

.leaderboard-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

#clear-leaderboard,
#refresh-leaderboard {
  min-width: 58px;
  height: 36px;
  padding: 0 10px;
  background: var(--panel);
  color: var(--ink);
  font-size: 0.84rem;
}

.leaderboard-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard-row {
  display: grid;
  grid-template-columns: 38px 1fr auto;
  align-items: center;
  gap: 10px;
  min-height: 42px;
  border: 2px solid color-mix(in srgb, var(--line) 70%, transparent);
  background: var(--panel);
  padding: 7px 9px;
}

.leaderboard-row.is-current {
  border-color: var(--line);
  background: color-mix(in srgb, var(--accent) 14%, var(--panel));
  box-shadow: 3px 3px 0 var(--line);
}

.leaderboard-place {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border: 2px solid var(--line);
  background: var(--accent);
  color: var(--tile-light-text);
  font-size: 0.78rem;
  font-weight: 950;
}

.leaderboard-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 900;
}

.leaderboard-score {
  font-weight: 950;
}

.leaderboard-empty {
  margin: 0;
  color: var(--muted);
  font-weight: 800;
}

@keyframes pop {
  0% {
    transform: scale(0.82);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 460px) {
  body {
    padding: 14px;
    align-items: start;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .player-strip {
    grid-template-columns: 1fr;
  }

  .controls {
    width: 100%;
  }

  .theme-picker {
    flex: 1;
  }

  .theme-trigger {
    min-width: 0;
    width: 100%;
  }

  .theme-panel {
    left: 0;
    right: auto;
    width: 100%;
  }

  .theme-options {
    grid-template-columns: 1fr;
  }

  .scores {
    width: 100%;
  }

  .score-box {
    flex: 1;
  }

  .leaderboard-head {
    align-items: stretch;
    flex-direction: column;
  }

  .leaderboard-actions,
  #clear-leaderboard,
  #refresh-leaderboard {
    width: 100%;
  }

  .leaderboard-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
}
