:root {
  --ink: #f8f2e6;
  --deep: #162326;
  --deep-2: #22353a;
  --panel: #2d4748;
  --panel-2: #f6ecd8;
  --muted: #a9bab7;
  --line: rgba(255, 255, 255, 0.14);
  --board: #7a6758;
  --cell: rgba(244, 232, 210, 0.2);
  --accent: #f0b23d;
  --accent-2: #2fb39d;
  --danger: #d9594a;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 18%, rgba(47, 179, 157, 0.34), transparent 34rem),
    linear-gradient(140deg, #101b1e 0%, #17292e 48%, #352b25 100%);
  overflow-x: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.025) 1px, transparent 1px);
  background-size: 32px 32px;
  mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.7), transparent 78%);
}

button,
a {
  -webkit-tap-highlight-color: transparent;
}

button {
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  padding: 0.78rem 0.95rem;
  color: #1c231f;
  background: linear-gradient(180deg, #ffd76e, var(--accent));
  font: inherit;
  font-weight: 900;
  cursor: pointer;
  box-shadow: 0 8px 0 #9d6724, 0 18px 34px rgba(0, 0, 0, 0.26);
  transition: transform 0.12s ease, filter 0.12s ease, opacity 0.12s ease;
}

button:hover {
  filter: brightness(1.05);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(2px);
  box-shadow: 0 5px 0 #9d6724, 0 14px 26px rgba(0, 0, 0, 0.25);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
}

.app-shell {
  position: relative;
  width: min(1160px, calc(100vw - 28px));
  min-height: 100svh;
  margin: 0 auto;
  padding: 18px 0 26px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 16px;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(160px, 1fr) auto auto;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: rgba(13, 24, 27, 0.74);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 22px 48px rgba(0, 0, 0, 0.24);
  backdrop-filter: blur(12px);
}

.portal-link {
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  color: #f7d573;
  background: #20373a;
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.22);
}

.portal-link svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand {
  display: grid;
  gap: 2px;
}

.brand span,
.score-card span,
.panel-label,
.hud-chip span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

.brand h1 {
  margin: 0;
  color: #fff5d9;
  font-size: 3.05rem;
  line-height: 0.9;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.28), 0 0 26px rgba(240, 178, 61, 0.34);
}

.hud-chip,
.score-card,
.side-panel {
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.1), rgba(255, 255, 255, 0.045));
  border: 1px solid var(--line);
  border-radius: 8px;
}

.hud-chip {
  min-width: 112px;
  padding: 9px 12px;
  text-align: center;
}

.hud-chip strong {
  display: block;
  color: #ffe17a;
  font-size: 1.32rem;
  line-height: 1.05;
}

.score-row {
  display: flex;
  gap: 8px;
}

.score-card {
  min-width: 108px;
  padding: 9px 12px;
  text-align: right;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.17);
}

.score-card strong {
  display: block;
  margin-top: 3px;
  color: #fff9e8;
  font-size: 1.48rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(190px, 235px) minmax(330px, 560px) minmax(190px, 235px);
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.side-panel {
  padding: 14px;
  display: grid;
  gap: 14px;
  align-self: stretch;
  background-color: rgba(21, 35, 38, 0.86);
  box-shadow: 0 20px 42px rgba(0, 0, 0, 0.22), inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.panel-block {
  display: grid;
  gap: 7px;
  padding: 12px;
  background: rgba(6, 14, 16, 0.24);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
}

.panel-block strong {
  color: #fff5d9;
  font-size: 1.42rem;
  line-height: 1.05;
}

.panel-block p {
  margin: 0;
  color: #c8d5d1;
  font-size: 0.95rem;
  line-height: 1.42;
}

.button-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  align-self: end;
}

#undoButton,
#continueButton {
  color: #eef8f5;
  background: linear-gradient(180deg, #3ebca8, #238775);
  box-shadow: 0 8px 0 #11564d, 0 18px 34px rgba(0, 0, 0, 0.24);
}

.board-section {
  position: relative;
  width: min(100%, 560px);
  aspect-ratio: 1;
}

.cabinet {
  width: 100%;
  height: 100%;
  padding: clamp(10px, 2.2vw, 18px);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.13), transparent 22%),
    linear-gradient(145deg, #304d50, #18282c 62%, #101719);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  box-shadow:
    0 24px 0 #0b1113,
    0 46px 72px rgba(0, 0, 0, 0.42),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
}

.cabinet-screen {
  position: relative;
  width: 100%;
  height: 100%;
  padding: clamp(8px, 1.7vw, 13px);
  background: #0b1416;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: inset 0 0 32px rgba(0, 0, 0, 0.6);
}

.screen-glow {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(115deg, rgba(255, 255, 255, 0.08), transparent 24%),
    radial-gradient(circle at 50% 50%, transparent 54%, rgba(0, 0, 0, 0.2));
  z-index: 2;
}

.board {
  position: relative;
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: clamp(8px, 2vw, 14px);
  padding: clamp(8px, 2vw, 14px);
  background:
    linear-gradient(160deg, rgba(255, 255, 255, 0.08), transparent 34%),
    var(--board);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  touch-action: none;
  outline: none;
}

.cell {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  color: #5b4e42;
  background: var(--cell);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 7px;
  font-size: 3.45rem;
  font-weight: 1000;
  line-height: 1;
  user-select: none;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.18);
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}

.tile-new {
  animation: pop 0.16s ease-out;
}

.tile-merge {
  animation: merge 0.18s ease-out;
}

.tile-2 { background: #fff1d9; }
.tile-4 { background: #f2deb7; }
.tile-8 { color: #fffdf7; background: #f1963e; }
.tile-16 { color: #fffdf7; background: #e46f42; }
.tile-32 { color: #fffdf7; background: #d9534a; }
.tile-64 { color: #fffdf7; background: #be3f45; }
.tile-128 { color: #172326; background: #ffd76e; font-size: 2.9rem; }
.tile-256 { color: #172326; background: #f0b23d; font-size: 2.9rem; }
.tile-512 { color: #fffdf7; background: #cf862c; font-size: 2.9rem; }
.tile-1024 { color: #fffdf7; background: #44b9a7; font-size: 2.28rem; }
.tile-2048 { color: #fffdf7; background: #1e8f7f; font-size: 2.28rem; box-shadow: 0 0 24px rgba(47, 179, 157, 0.38), inset 0 -4px 0 rgba(0, 0, 0, 0.18); }
.tile-super { color: #fffdf7; background: #11181a; font-size: 2rem; }

.overlay {
  position: absolute;
  inset: clamp(8px, 1.7vw, 13px);
  z-index: 3;
  display: grid;
  place-content: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
  background: rgba(13, 24, 27, 0.86);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  backdrop-filter: blur(6px);
}

.overlay.is-hidden {
  display: none;
}

.overlay strong {
  color: #fff5d9;
  font-size: 2.5rem;
  line-height: 1;
}

.overlay span {
  color: #d6e2de;
  font-weight: 800;
}

.overlay-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.key-grid,
.touch-controls {
  display: grid;
  grid-template-columns: repeat(3, 44px);
  gap: 8px;
  justify-content: start;
}

.key-grid kbd,
.touch-controls button {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
}

.key-grid kbd {
  color: #203035;
  background: #f6ecd8;
  border: 1px solid rgba(255, 255, 255, 0.2);
  font-weight: 1000;
  box-shadow: 0 4px 0 #b7a27d;
}

.key-grid kbd:first-child,
.touch-controls button:first-child {
  grid-column: 2;
}

.key-grid kbd:nth-child(2),
.touch-controls button:nth-child(2) {
  grid-column: 1;
}

.touch-controls button:nth-child(3) {
  grid-column: 2;
}

.touch-controls button:nth-child(4) {
  grid-column: 3;
}

@keyframes pop {
  from { transform: scale(0.65); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

@keyframes merge {
  0% { transform: scale(1); }
  55% { transform: scale(1.14); }
  100% { transform: scale(1); }
}

@media (max-width: 960px) {
  .app-shell {
    width: min(760px, calc(100vw - 22px));
  }

  .topbar {
    grid-template-columns: auto 1fr auto;
  }

  .hud-chip {
    display: none;
  }

  .game-layout {
    grid-template-columns: 1fr;
    align-items: start;
  }

  .side-panel {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    align-self: auto;
  }

  .help-panel {
    grid-row: 2;
  }

  .status-panel {
    grid-row: 3;
  }

  .help-panel {
    grid-template-columns: 1fr auto;
  }

  .board-section {
    width: min(100%, 620px);
    margin: 0 auto;
  }

  .button-grid {
    align-self: stretch;
  }
}

@media (max-width: 620px) {
  .app-shell {
    width: min(100vw - 16px, 560px);
    padding-top: 10px;
    gap: 12px;
  }

  .topbar {
    grid-template-columns: auto 1fr;
    padding: 10px;
  }

  .brand h1 {
    font-size: 2.5rem;
  }

  .score-row {
    grid-column: 1 / -1;
    width: 100%;
  }

  .score-card {
    flex: 1;
    min-width: 0;
  }

  .side-panel,
  .help-panel {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .status-panel {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .status-panel .button-grid {
    grid-column: 1 / -1;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .help-panel .panel-block {
    display: none;
  }

  .help-panel {
    padding: 10px;
    background: rgba(13, 24, 27, 0.58);
  }

  .touch-controls {
    justify-content: center;
    grid-template-columns: repeat(3, 50px);
  }

  .touch-controls button {
    width: 50px;
    height: 50px;
  }

  .cell {
    font-size: 2.35rem;
  }

  .tile-128,
  .tile-256,
  .tile-512 {
    font-size: 2rem;
  }

  .tile-1024,
  .tile-2048 {
    font-size: 1.55rem;
  }

  .tile-super {
    font-size: 1.36rem;
  }

  .cabinet {
    box-shadow: 0 14px 0 #0b1113, 0 30px 48px rgba(0, 0, 0, 0.38);
  }
}
