:root {
  --bg: #f5efe2;
  --ink: #4a3f30;
  --ink-light: #8a7a5e;
  --board: #d6c6a5;
  --cell: #e5d9bd;
  --gold: #c9a227;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

body {
  background: var(--bg) radial-gradient(ellipse at 50% -20%, #fdf9ef 0%, var(--bg) 60%);
  color: var(--ink);
  font-family: -apple-system, sans-serif;
  display: flex;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

.wrap {
  width: min(94vw, 470px);
  padding: 18px 0 32px;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

h1 { font-size: 42px; letter-spacing: 2px; }
h1 span { color: var(--gold); font-size: 24px; letter-spacing: 0; }

.scores { display: flex; gap: 8px; }

.score-box {
  position: relative;
  background: #e2d5b8;
  border-radius: 8px;
  padding: 6px 14px 8px;
  text-align: center;
  min-width: 74px;
}
.score-box label { font-size: 12px; color: var(--ink-light); }
#score, #best { font-size: 22px; font-weight: 700; line-height: 1.15; }

#score-add {
  position: absolute;
  left: 0; right: 0; top: 4px;
  font-size: 20px;
  font-weight: 700;
  color: var(--gold);
  pointer-events: none;
}
#score-add.float { animation: floatUp .6s ease-out; }
@keyframes floatUp {
  0% { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-26px); }
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 12px 0 10px;
}
.hint { font-size: 14px; color: var(--ink-light); }
.hint strong { color: var(--gold); }

button {
  border: none;
  border-radius: 8px;
  background: #8f7a56;
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  padding: 9px 16px;
  cursor: pointer;
  font-family: inherit;
}
button:active { transform: scale(0.96); }

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: var(--board);
  border-radius: 12px;
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, .10);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}

.cells, .tiles { position: absolute; inset: 0; }
.cell { position: absolute; background: var(--cell); border-radius: 9px; }

.tile {
  position: absolute;
  transition: transform .11s ease-in-out;
  will-change: transform;
  z-index: 2;
}
.tiles.no-anim .tile { transition: none; }

.tile .inner {
  width: 100%;
  height: 100%;
  border-radius: 9px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12);
}
.tile .name { font-size: 1em; line-height: 1.05; }

.inner.appear { animation: appear .14s ease-out; }
@keyframes appear {
  0% { transform: scale(0); }
  100% { transform: scale(1); }
}
.inner.pop { animation: pop .18s ease-out; }
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

/* Realm colors: earth, wood, gold, water, thunder, violet, immortal gold. */
.t2    .inner { background: #dbd3bd; }
.t4    .inner { background: #ccd2ac; }
.t8    .inner { background: #e9c87f; }
.t16   .inner { background: #a9cbd8; }
.t32   .inner { background: #7fb4cf; color: #fff; }
.t64   .inner { background: #7f93d8; color: #fff; }
.t128  .inner { background: #9b82d8; color: #fff; }
.t256  .inner { background: #b06fc9; color: #fff; }
.t512  .inner { background: #c95f86; color: #fff; }
.t1024 .inner { background: #e08a3f; color: #fff; }
.t2048 .inner {
  background: linear-gradient(135deg, #f5c13d, #e8a20c);
  color: #fff;
  box-shadow: 0 0 20px rgba(245, 193, 61, .75);
}
.t4096 .inner {
  background: linear-gradient(135deg, #ffd75e, #ff9d00);
  color: #fff;
  box-shadow: 0 0 24px rgba(255, 165, 0, .8);
}
.t8192 .inner {
  background: linear-gradient(135deg, #b98ff5, #ffd700);
  color: #fff;
  box-shadow: 0 0 28px rgba(185, 143, 245, .85);
}

.overlay[hidden] { display: none; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: rgba(245, 239, 226, .86);
  backdrop-filter: blur(2px);
  border-radius: 12px;
  animation: fadeIn .35s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.overlay h2 { font-size: 40px; letter-spacing: 4px; }
.overlay.win h2 { color: var(--gold); text-shadow: 0 0 18px rgba(201, 162, 39, .6); }
.overlay.lose h2 { color: #a04545; }
.overlay p { font-size: 15px; color: var(--ink-light); }
.overlay-btns { display: flex; gap: 10px; margin-top: 8px; }
#keep-going { background: var(--gold); }

.ladder {
  margin-top: 14px;
  text-align: center;
  font-size: 13px;
  line-height: 1.7;
  color: var(--ink-light);
}
.ladder strong { color: var(--gold); }

/* Ascension Board dialog */
.btns { display: flex; gap: 8px; }
#lb-btn { background: var(--gold); }

.lb[hidden] { display: none; }
.lb {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(58, 50, 38, .45);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn .25s ease;
}
.lb-card {
  width: min(92vw, 420px);
  max-height: 82vh;
  background: var(--bg);
  border-radius: 14px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .3);
}
.lb-head { display: flex; justify-content: space-between; align-items: center; }
.lb-head h3 { font-size: 22px; letter-spacing: 2px; }
.lb-x { background: none; color: var(--ink-light); font-size: 24px; padding: 0 8px; }
.lb-me { font-size: 14px; color: var(--ink-light); min-height: 1.3em; }
.lb-rename { display: flex; gap: 8px; }
.lb-rename input {
  flex: 1;
  min-width: 0;
  border: 1px solid #cbbfa4;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  background: #fdf9ef;
  color: var(--ink);
  font-family: inherit;
}
.lb-rename button { padding: 8px 12px; font-size: 14px; }
.lb-list { list-style: none; overflow-y: auto; flex: 1; }
.lb-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 6px;
  border-bottom: 1px dashed #d8ccb0;
  font-size: 14px;
}
.lb-list li.me { background: #efe4c3; border-radius: 8px; }
.lb-rank { width: 34px; text-align: center; font-weight: 700; color: var(--ink-light); flex: none; }
.lb-rank.top { color: var(--gold); }
.lb-n { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-realm { color: var(--gold); font-size: 13px; flex: none; }
.lb-score { font-weight: 700; min-width: 56px; text-align: right; flex: none; }
.lb-empty { text-align: center; color: var(--ink-light); padding: 18px 0; }

/* English realm labels and controls must fit narrow screens. */
header { gap: 12px; }
h1 { font-size: clamp(23px, 6.5vw, 36px); letter-spacing: 0; line-height: 1.05; }
h1 span { display: block; margin-top: 4px; }
.score-box { min-width: 64px; padding-inline: 10px; }
.toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
.hint { text-align: center; line-height: 1.4; }
.btns { justify-content: center; }
.btns button { padding-inline: 12px; }
.tile .name { font-size: .66em; line-height: 1.1; text-align: center; padding: 3px; max-width: 100%; }
.t4 .name { font-size: .53em; }
.t256 .name { font-size: .60em; }
.overlay { padding: 16px; text-align: center; }
.overlay h2 { font-size: clamp(25px, 7vw, 36px); letter-spacing: 0; }
.overlay p { line-height: 1.45; }
.lb-head h3 { letter-spacing: 0; }
.lb-list li { gap: 6px; }
.lb-rank { width: 26px; }
.lb-realm { max-width: 84px; text-align: right; }
.lb-score { min-width: 44px; }
@media (max-width: 360px) {
  .scores { gap: 5px; }
  .score-box { min-width: 60px; padding-inline: 6px; }
  .btns { gap: 6px; }
  .btns button { font-size: 14px; padding-inline: 10px; }
}
