:root {
  --bg: #1c1714;
  --paper: #f4ecdf;
  --ink: #1d1612;
  --muted: #5c4e44;
  --amber: #ef9f27;
  --foam: #f4ecdf;
  --link: #f6c56b;
  --jade: #1f8a62;
  --sea: #1f6f8a;
  --cinnabar: #c9432c;
  --shadow: 0 18px 50px rgba(0, 0, 0, .28);
  --sans: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;
  --serif: "Songti SC", "STSong", "Noto Serif SC", "PingFang SC", "Microsoft YaHei", serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { margin: 0; padding: 0; }

body {
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--sans);
  color: var(--foam);
  background:
    radial-gradient(1000px 480px at 0% -10%, rgba(239, 159, 39, .16), transparent 55%),
    radial-gradient(800px 520px at 100% 0%, rgba(201, 67, 44, .14), transparent 50%),
    var(--bg);
}

body.modal-open { overflow: hidden; }

a { color: var(--link); }

a:focus-visible,
button:focus-visible {
  outline: 3px solid var(--amber);
  outline-offset: 2px;
}

.page {
  width: min(560px, 100%);
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
}

.portal { width: min(1080px, 100%); }

.hero { padding: 28px 18px 6px; }

.eyebrow {
  margin: 0;
  letter-spacing: .16em;
  text-transform: uppercase;
  font-size: 12px;
  opacity: .72;
}

.hero h1,
.panel h1,
.sheet-card h2,
.card h3,
.bar h1 {
  font-family: var(--serif);
  font-weight: 600;
}

.hero h1 {
  margin: 6px 0 10px;
  font-size: clamp(40px, 8vw, 76px);
  line-height: .95;
}

.lede,
.panel p,
.sheet-card p,
.card p {
  line-height: 1.6;
}

.lede {
  max-width: 40rem;
  margin: 0 0 16px;
  color: rgba(244, 236, 223, .88);
}

.hero-actions { display: flex; flex-wrap: wrap; gap: 10px; }

.section-title {
  margin: 18px 18px 8px;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: .04em;
}

.cards {
  display: grid;
  gap: 14px;
  padding: 0 16px 20px;
}

@media (min-width: 860px) {
  .cards { grid-template-columns: repeat(3, 1fr); }
}

.card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-decoration: none;
  color: var(--ink);
  background: var(--paper);
  border-radius: 22px;
  padding: 14px 14px 16px;
  box-shadow: var(--shadow);
  min-height: 100%;
}

.card p { margin: 0; color: var(--muted); }

.card h3 { margin: 2px 0 0; font-size: 30px; }

.art {
  height: 92px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: #2a211c;
}

.card.merge .art { background: #3a2a22; }
.card.reflex .art { background: #1d2c31; }
.card.snake .art { background: #1c2b24; }

.meta { font-size: 13px; color: var(--muted); }

.play {
  margin-top: auto;
  align-self: flex-start;
  background: var(--ink);
  color: var(--paper);
  border-radius: 999px;
  padding: 8px 14px;
  font-weight: 750;
  font-size: 14px;
}

@media (hover: hover) {
  .card:hover { transform: translateY(-3px); }
}

.foot {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  align-items: center;
  padding: 4px 18px 24px;
  color: rgba(244, 236, 223, .72);
  font-size: 14px;
}

.foot p { margin: 0; flex-basis: 100%; }

.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 0;
}

.bar h1 {
  margin: 0;
  text-align: center;
  font-size: 22px;
}

.back,
.textbtn,
.btn,
.dpad button,
.pad {
  font: inherit;
  cursor: pointer;
}

.back,
.textbtn {
  color: var(--foam);
  text-decoration: none;
  background: transparent;
  border: 0;
  min-height: 44px;
  padding: 0 6px;
}

.back { justify-self: start; }
.textbtn { justify-self: end; }

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  min-height: 48px;
  padding: 12px 18px;
  font-weight: 750;
  color: var(--ink);
  background: var(--amber);
}

.btn:active { transform: translateY(1px); }

.btn.secondary {
  background: transparent;
  color: var(--foam);
  box-shadow: inset 0 0 0 1.5px rgba(244, 236, 223, .45);
}

.btn.ghost {
  background: transparent;
  color: var(--ink);
  box-shadow: inset 0 0 0 1.5px rgba(29, 22, 18, .28);
}

.btn:disabled { opacity: .5; cursor: default; }

.ad-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 56px;
  margin: 10px 14px 0;
  padding: 8px 12px;
  border: 1.5px dashed rgba(244, 236, 223, .42);
  border-radius: 14px;
  color: rgba(244, 236, 223, .8);
  background: rgba(255, 255, 255, .03);
  font-size: 13px;
  text-align: center;
}

.ad-label {
  flex: none;
  font-size: 11px;
  letter-spacing: .14em;
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 2px 6px;
}

.ad-live { border-style: solid; }

.ad-h5-holder { flex: 1; min-height: 50px; }

.ad-modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(12, 9, 7, .72);
  display: grid;
  place-items: center;
  padding: 18px;
}

.ad-dialog,
.sheet-card,
.panel {
  width: min(100%, 420px);
  background: var(--paper);
  color: var(--ink);
  border-radius: 22px;
  padding: 18px 16px 16px;
  box-shadow: var(--shadow);
  color-scheme: light;
}

.ad-kicker {
  margin: 0;
  font-size: 12px;
  letter-spacing: .14em;
  color: var(--cinnabar);
  font-weight: 750;
}

.ad-dialog h2,
.panel h1 { margin: 4px 0 8px; font-size: 28px; }

.ad-dialog p,
.panel p { margin: 0 0 10px; color: var(--muted); }

.ad-unit {
  min-height: 160px;
  margin: 4px 0 12px;
  border: 1.5px dashed var(--cinnabar);
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: repeating-linear-gradient(-45deg, transparent, transparent 8px, rgba(201, 67, 44, .06) 8px, rgba(201, 67, 44, .06) 16px);
  overflow: hidden;
}

.ad-unit-note { margin: 0; padding: 12px; text-align: center; color: #8a4032; font-weight: 700; }

.ad-actions,
.sheet-actions { display: flex; flex-direction: column; gap: 8px; }

.ad-actions .btn,
.sheet-actions .btn { width: 100%; }

.stage {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 8px 14px 0;
  user-select: none;
  -webkit-user-select: none;
}

.hud {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: stretch;
  padding: 8px 0 10px;
}

.pill {
  background: rgba(255, 255, 255, .06);
  border-radius: 14px;
  padding: 7px 10px;
  min-width: 68px;
}

.pill > span:not(.lives) { display: block; font-size: 12px; opacity: .72; }

.pill b {
  display: block;
  margin-top: 2px;
  font-size: 22px;
  font-variant-numeric: tabular-nums;
}

.hud .textbtn { margin-left: auto; align-self: center; }

.lives { display: flex; gap: 5px; align-items: center; min-height: 22px; }

.lamp {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: rgba(244, 236, 223, .2);
}

.lamp.on { background: var(--amber); box-shadow: 0 0 8px rgba(239, 159, 39, .8); }

.status {
  margin: 0 0 8px;
  min-height: 24px;
  text-align: center;
  font-weight: 700;
}

.meter {
  height: 8px;
  margin: 0 auto 12px;
  width: min(100%, 460px);
  background: rgba(255, 255, 255, .12);
  border-radius: 99px;
  overflow: hidden;
}

.meter span {
  display: block;
  height: 100%;
  width: 100%;
  background: var(--amber);
  transform: scaleX(0);
  transform-origin: left center;
}

.board {
  position: relative;
  width: min(100%, calc(100dvh - 370px), 440px);
  aspect-ratio: 1;
  margin: 0 auto;
  touch-action: none;
  border-radius: 18px;
  background: #3a2a22;
}

.cells, .tiles { position: absolute; inset: 0; }

.cell {
  position: absolute;
  left: 0;
  top: 0;
  border-radius: 10px;
  background: rgba(0, 0, 0, .2);
}

.tile {
  position: absolute;
  left: 0;
  top: 0;
  transition: transform 110ms ease-out;
}

.tile-face {
  width: 100%;
  height: 100%;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #efe6d6;
  color: var(--ink);
  font-weight: 800;
}

.tile-face .n {
  font-size: clamp(16px, 5vw, 28px);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.tile-face .nick { margin-top: 2px; font-size: 11px; font-weight: 650; opacity: .75; }

.tile[data-value="4"] .tile-face { background: #f0d7b4; }
.tile[data-value="8"] .tile-face { background: #f0b56a; }
.tile[data-value="16"] .tile-face { background: #ef9f27; }
.tile[data-value="32"] .tile-face { background: #e07a3d; color: #fffaf3; }
.tile[data-value="64"] .tile-face { background: #d85a32; color: #fffaf3; }
.tile[data-value="128"] .tile-face { background: #c9432c; color: #fffaf3; }
.tile[data-value="256"] .tile-face { background: #8f2d1f; color: #fffaf3; }
.tile[data-value="512"] .tile-face { background: #5d3b78; color: #fffaf3; }
.tile[data-value="1024"] .tile-face { background: #1f6f62; color: #fffaf3; }
.tile[data-value="2048"] .tile-face { background: #f0d56a; color: #1d1612; }
.tile[data-value="4096"] .tile-face,
.tile[data-value="8192"] .tile-face { background: #1d1612; color: #f4ecdf; }

.tile.pop .tile-face { animation: pop .16s ease-out; }

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

.dpad {
  display: grid;
  grid-template-columns: 58px 58px 58px;
  grid-template-areas:
    ". up ."
    "left down right";
  justify-content: center;
  gap: 8px;
  margin: 12px auto 0;
  touch-action: manipulation;
}

.dpad [data-dir="up"] { grid-area: up; }
.dpad [data-dir="down"] { grid-area: down; }
.dpad [data-dir="left"] { grid-area: left; }
.dpad [data-dir="right"] { grid-area: right; }

.dpad button {
  width: 58px;
  height: 58px;
  border-radius: 16px;
  border: 0;
  background: rgba(244, 236, 223, .1);
  color: var(--foam);
  font-size: 22px;
}

.pads {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  width: min(100%, 460px, calc(100dvh - 360px));
  margin: 0 auto;
  aspect-ratio: 1;
}

.pad {
  border: 0;
  border-radius: 24px;
  color: var(--foam);
  position: relative;
  touch-action: manipulation;
  background: #3a312a;
}

.pad .shape { font-size: clamp(32px, 8vw, 48px); display: block; }
.pad .keyhint {
  position: absolute;
  right: 12px;
  bottom: 10px;
  font-size: 12px;
  font-style: normal;
  opacity: .55;
}

.pad[data-i="0"] { background: #3d2a28; }
.pad[data-i="1"] { background: #24383a; }
.pad[data-i="2"] { background: #2c3324; }
.pad[data-i="3"] { background: #342c40; }
.pad[data-i="0"].lit { background: #ef9f27; color: var(--ink); }
.pad[data-i="1"].lit { background: #7fd3e2; color: var(--ink); }
.pad[data-i="2"].lit { background: #9ad67a; color: var(--ink); }
.pad[data-i="3"].lit { background: #f0a0c4; color: var(--ink); }

.dots { display: flex; justify-content: center; gap: 6px; margin-top: 12px; }

.dots i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(244, 236, 223, .25);
  display: block;
}

.dots i.on { background: var(--amber); }

.board-wrap {
  width: min(100%, calc(100dvh - 410px), 420px);
  margin: 0 auto;
}

canvas.snake {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 16px;
  background: #14110f;
  touch-action: none;
}

.sheet {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  align-content: center;
  justify-items: center;
  padding: 16px 8px;
  overflow: auto;
  background: rgba(28, 23, 20, .62);
  backdrop-filter: blur(5px);
}

.sheet[hidden] { display: none !important; }

.sheet-card { text-align: center; }

.sheet-card h2 { margin: 0 0 8px; font-size: 34px; }

.sheet-card p { margin: 0 0 8px; }

.scorebig {
  margin: 4px 0;
  font-size: 48px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.badge {
  display: inline-block;
  margin-bottom: 8px;
  background: #8f2d1f;
  color: #fffaf3;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 12px;
  font-weight: 750;
}

.panel-wrap {
  padding: 18px 16px 28px;
  display: grid;
  justify-items: center;
}

.panel { width: min(100%, 460px); }

.panel .btn { width: 100%; margin: 4px 0 12px; }

pre.json {
  margin: 0;
  text-align: left;
  background: #1d1612;
  color: var(--foam);
  padding: 12px;
  border-radius: 12px;
  overflow: auto;
  font-size: 13px;
  line-height: 1.45;
}

.ok { color: #1f7a45; font-weight: 750; }
.bad { color: #a92f22; font-weight: 750; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 90;
  width: min(92vw, 420px);
  background: var(--paper);
  color: var(--ink);
  color-scheme: light;
  padding: 12px 14px;
  border-radius: 16px;
  box-shadow: var(--shadow);
}

.toast[hidden] { display: none !important; }

.toast p { margin: 0 0 6px; }

.toast input {
  width: 100%;
  font: inherit;
  font-size: 16px;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid rgba(29, 22, 18, .2);
}

.noscript {
  margin: 12px 16px;
  padding: 12px;
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
}

.legal { padding: 12px 18px 32px; max-width: 42rem; }

.legal h1 { font-family: var(--serif); font-size: 40px; margin: 12px 0; }

.legal h2 { font-size: 18px; margin: 22px 0 6px; }

.legal p,
.legal li { color: rgba(244, 236, 223, .86); line-height: 1.65; }

@media (max-width: 360px) {
  .bar h1 { font-size: 18px; }
  .tile-face .nick { display: none; }
  .dpad { grid-template-columns: 52px 52px 52px; }
  .dpad button { width: 52px; height: 52px; }
}

@media (max-height: 760px) {
  .ad-slot { min-height: 42px; margin-top: 6px; }
  .hero { padding-top: 18px; }
}

@media (pointer: coarse), (max-width: 800px) {
  .keyhint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .card,
  .btn,
  .tile.pop .tile-face { transition: none; animation: none; }
}
