:root {
  color-scheme: light;
  --paper: #f3efe7;
  --paper-2: #ebe4d8;
  --ink: #4d514b;
  --muted: #8a8d84;
  --line: rgba(74, 79, 72, .13);
  --sage: #a9b7a3;
  --sage-dark: #72836d;
  --cream: rgba(255, 253, 248, .72);
  --shadow: 0 20px 60px rgba(92, 84, 70, .10);
  font-family: Inter, ui-rounded, "SF Pro Rounded", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }

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

body {
  display: grid;
  place-items: center;
  overflow-x: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 16% 8%, rgba(255, 255, 255, .9), transparent 34%),
    radial-gradient(circle at 86% 84%, rgba(169, 183, 163, .18), transparent 38%),
    var(--paper);
}

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

.game-shell {
  width: min(100%, 560px);
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: max(22px, env(safe-area-inset-top)) 22px max(24px, env(safe-area-inset-bottom));
}

.game-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
}

.back-link {
  width: max-content;
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  transition: color 160ms ease;
}
.back-link:hover, .back-link:focus-visible { color: var(--sage-dark); outline: none; }

.title-block { text-align: center; }
.mode-label {
  display: block;
  margin: 0 auto 2px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 10px;
  letter-spacing: .19em;
  text-transform: uppercase;
  cursor: pointer;
}
.mode-label:disabled { cursor: default; }
.mode-label:not(:disabled):hover,
.mode-label:not(:disabled):focus-visible { color: var(--sage-dark); outline: none; }
.title-block h1 {
  margin: 0;
  color: #55584f;
  font-size: clamp(34px, 9vw, 48px);
  font-weight: 650;
  line-height: .95;
  letter-spacing: -.055em;
  cursor: default;
  user-select: none;
}

.soft-button {
  justify-self: end;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, .42);
  color: #6f746c;
  font-size: 12px;
  cursor: pointer;
  transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}
.soft-button:hover, .soft-button:focus-visible {
  border-color: rgba(114, 131, 109, .28);
  background: rgba(255, 255, 255, .72);
  outline: none;
  transform: translateY(-1px);
}

.score-row {
  min-height: 58px;
  display: grid;
  grid-template-columns: auto auto 1fr;
  align-items: center;
  gap: 22px;
  margin: 20px 0 12px;
  padding: 0 4px;
}
.score-row > div { display: grid; gap: 2px; }
.score-row span {
  color: var(--muted);
  font-size: 10px;
  letter-spacing: .1em;
}
.score-row strong { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }
.score-row p {
  justify-self: end;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  text-align: right;
}

.tools {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 9px;
  margin-bottom: 12px;
}
.tool {
  position: relative;
  min-width: 0;
  min-height: 62px;
  display: grid;
  grid-template-columns: 24px 1fr;
  align-items: center;
  gap: 7px;
  padding: 9px 22px 9px 10px;
  border: 1px solid var(--line);
  border-radius: 15px;
  background: var(--cream);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  box-shadow: 0 8px 25px rgba(92, 84, 70, .045);
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease, opacity 160ms ease;
}
.tool:hover, .tool:focus-visible {
  border-color: rgba(114, 131, 109, .3);
  background: rgba(255, 255, 255, .84);
  outline: none;
  transform: translateY(-1px);
}
.tool.active {
  border-color: rgba(114, 131, 109, .58);
  background: rgba(220, 229, 215, .86);
}
.tool.empty,
.tool:disabled { opacity: .43; cursor: default; transform: none; }
.extra-tool { cursor: default; }
.extra-tool:hover { border-color: var(--line); background: var(--cream); transform: none; }
.extra-tool.ready { border-color: rgba(114, 131, 109, .35); background: rgba(220, 229, 215, .6); }
.extra-tool.infinite { border-color: rgba(114, 131, 109, .58); }
.tool-icon {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  color: var(--sage-dark);
  font-size: 20px;
  font-weight: 500;
}
.tool > span:nth-child(2) { min-width: 0; display: grid; gap: 2px; }
.tool strong { font-size: 12px; font-weight: 600; }
.tool small {
  overflow: hidden;
  color: var(--muted);
  font-size: 9px;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tool b {
  position: absolute;
  top: 8px;
  right: 8px;
  min-width: 17px;
  height: 17px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(114, 131, 109, .11);
  color: var(--sage-dark);
  font-size: 9px;
  font-weight: 650;
}

.board-wrap { width: 100%; }
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border: 1px solid rgba(83, 89, 80, .08);
  border-radius: 24px;
  background: rgba(203, 197, 185, .58);
  box-shadow: var(--shadow), inset 0 1px 1px rgba(255, 255, 255, .55);
  touch-action: none;
  user-select: none;
}
.board-grid, .tile-layer {
  position: absolute;
  inset: 3.2%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 2.6%;
}
.board-grid i {
  border-radius: 18%;
  background: rgba(250, 247, 240, .42);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .24);
}
.tile-layer { pointer-events: none; }
.tile {
  min-width: 0;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 18%;
  color: #585c54;
  font-size: clamp(24px, 8vw, 43px);
  font-weight: 630;
  letter-spacing: -.055em;
  box-shadow: 0 8px 20px rgba(99, 91, 78, .07), inset 0 1px 0 rgba(255, 255, 255, .45);
  pointer-events: auto;
  cursor: default;
  animation: tileIn 170ms cubic-bezier(.2, .8, .25, 1) both;
}
.tile.selectable { cursor: pointer; }
.tile.selectable:hover, .tile.selectable:focus-visible {
  outline: 3px solid rgba(114, 131, 109, .34);
  outline-offset: -4px;
}
.tile.selected {
  outline: 3px solid rgba(97, 119, 93, .72);
  outline-offset: -4px;
  transform: scale(.94);
}
.tile[data-value="2"] { background: #f7f3eb; }
.tile[data-value="4"] { background: #eee8dc; }
.tile[data-value="8"] { background: #e6d9c6; }
.tile[data-value="16"] { background: #ddd0ba; }
.tile[data-value="32"] { background: #cfd1b9; }
.tile[data-value="64"] { background: #bcc6ad; }
.tile[data-value="128"] { background: #aebca4; color: #f9faf5; }
.tile[data-value="256"] { background: #9faf96; color: #fbfcf8; }
.tile[data-value="512"] { background: #8fa586; color: #fff; }
.tile[data-value="1024"] { background: #7e9877; color: #fff; font-size: clamp(20px, 6.8vw, 36px); }
.tile[data-value="2048"] { background: #6c8968; color: #fff; font-size: clamp(20px, 6.8vw, 36px); box-shadow: 0 0 34px rgba(108, 137, 104, .22); }
.tile.high { background: #61765f; color: #fff; font-size: clamp(17px, 5.5vw, 31px); }

.board-message {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  padding: 28px;
  background: rgba(243, 239, 231, .78);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  backdrop-filter: blur(5px);
  transition: opacity 220ms ease, visibility 220ms ease;
}
.board-message.active { opacity: 1; visibility: visible; pointer-events: auto; }
.board-message > div { text-align: center; }
.board-message p { margin: 0 0 7px; color: var(--muted); font-size: 12px; }
.board-message strong { display: block; margin-bottom: 18px; font-size: 26px; font-weight: 620; }
.board-message .soft-button { justify-self: center; }

.hint { margin: 14px 0 0; color: var(--muted); font-size: 10px; letter-spacing: .05em; text-align: center; }

.choice-modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(78, 78, 72, .18);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  backdrop-filter: blur(8px);
  transition: opacity 200ms ease, visibility 200ms ease;
}
.choice-modal.active { opacity: 1; visibility: visible; pointer-events: auto; }
.choice-card {
  width: min(100%, 410px);
  padding: 27px;
  border: 1px solid rgba(255, 255, 255, .58);
  border-radius: 24px;
  background: rgba(247, 244, 237, .96);
  box-shadow: 0 28px 80px rgba(71, 68, 59, .18);
  text-align: center;
}
.choice-card > p { margin: 0 0 5px; color: var(--sage-dark); font-size: 11px; letter-spacing: .08em; }
.choice-card h2 { margin: 0; font-size: 22px; font-weight: 620; }
.choice-card > small { display: block; margin-top: 5px; color: var(--muted); font-size: 10px; }
.choice-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 22px; }
.choice-options button {
  min-height: 88px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 7px;
  border: 1px solid var(--line);
  border-radius: 17px;
  background: rgba(255, 255, 255, .55);
  color: var(--ink);
  cursor: pointer;
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease;
}
.choice-options button:hover, .choice-options button:focus-visible {
  border-color: rgba(114, 131, 109, .4);
  background: #fff;
  outline: none;
  transform: translateY(-2px);
}
.choice-options button:disabled { opacity: .28; cursor: not-allowed; transform: none; }
.choice-options span { color: var(--sage-dark); font-size: 25px; }
.choice-options strong { font-size: 12px; font-weight: 600; }

.compact-card { width: min(100%, 380px); }
.dialog-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 22px;
}
.dialog-actions .soft-button { justify-self: auto; min-width: 105px; }
.dialog-actions .primary {
  border-color: rgba(114, 131, 109, .38);
  background: rgba(220, 229, 215, .8);
  color: #596955;
}
.single-action { margin-top: 16px; }
.value-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(58px, 1fr));
  gap: 8px;
  max-height: 220px;
  margin-top: 20px;
  overflow-y: auto;
}
.value-options button {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: rgba(255, 255, 255, .58);
  color: var(--ink);
  font-weight: 600;
  cursor: pointer;
}
.value-options button:hover,
.value-options button:focus-visible { border-color: rgba(114, 131, 109, .4); background: #fff; outline: none; }
.access-entry { display: grid; gap: 9px; margin-top: 20px; text-align: left; }
.access-entry[hidden], #access-confirm[hidden], #access-continue[hidden] { display: none; }
.access-entry label { color: var(--muted); font-size: 10px; letter-spacing: .1em; text-transform: uppercase; }
.access-entry input {
  width: 100%;
  height: 44px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: rgba(255, 255, 255, .7);
  color: var(--ink);
  font-size: 18px;
  letter-spacing: .18em;
  text-align: center;
}
.access-entry input:focus { border-color: rgba(114, 131, 109, .5); outline: 3px solid rgba(114, 131, 109, .1); }
.access-entry > p { min-height: 16px; margin: 0; color: #9b665e; font-size: 10px; text-align: center; }
.access-entry .dialog-actions { margin-top: 2px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(22px, env(safe-area-inset-bottom));
  z-index: 30;
  max-width: calc(100vw - 32px);
  padding: 9px 15px;
  border: 1px solid rgba(255, 255, 255, .55);
  border-radius: 999px;
  background: rgba(82, 91, 79, .88);
  color: #fff;
  font-size: 11px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity 180ms ease, transform 220ms ease;
  backdrop-filter: blur(8px);
}
.toast.active { opacity: 1; transform: translate(-50%, 0); }

@keyframes tileIn {
  from { opacity: .55; transform: scale(.88); }
  to { opacity: 1; transform: scale(1); }
}

@media (max-width: 520px) {
  .game-shell { justify-content: flex-start; padding-inline: 14px; }
  .game-header { grid-template-columns: 1fr auto 1fr; }
  .score-row { gap: 16px; margin-top: 16px; }
  .score-row p { font-size: 10px; }
  .tools { gap: 6px; }
  .tool { min-height: 56px; grid-template-columns: 20px 1fr; gap: 5px; padding: 7px 18px 7px 7px; border-radius: 13px; }
  .tool-icon { width: 20px; height: 20px; font-size: 17px; }
  .tool small { display: none; }
  .board { border-radius: 19px; }
}

@media (max-height: 710px) and (orientation: landscape) {
  body { display: block; }
  .game-shell { width: min(100%, 920px); min-height: 100svh; display: grid; grid-template-columns: 1fr min(58vh, 520px); grid-template-rows: auto auto 1fr; gap: 8px 24px; margin: auto; }
  .game-header, .score-row, .tools, .hint { grid-column: 1; }
  .board-wrap { grid-column: 2; grid-row: 1 / 5; align-self: center; }
  .score-row { margin: 8px 0; }
  .tools { grid-template-columns: repeat(2, 1fr); margin: 0; }
  .hint { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
