:root {
  color-scheme: dark;
  --page: #10151f;
  --surface: #171e2a;
  --well: #101722;
  --slot: #202a39;
  --line: #313e51;
  --ink: #f3f6fa;
  --muted: #a9b7c8;
  --accent: #b8f18d;
  --accent-ink: #172615;
  --shadow: #06090f66;
  --padding: clamp(16px, 5vw, 24px);
  --safe-top: max(env(safe-area-inset-top, 0px), var(--telegram-safe-top, 0px));
  --safe-right: max(env(safe-area-inset-right, 0px), var(--telegram-safe-right, 0px));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--telegram-safe-bottom, 0px));
  --safe-left: max(env(safe-area-inset-left, 0px), var(--telegram-safe-left, 0px));
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #f1f4f0;
  --surface: #fbfcf9;
  --well: #e5ebe3;
  --slot: #d7e0d5;
  --line: #c3cfc1;
  --ink: #172421;
  --muted: #4f6159;
  --accent: #244e36;
  --accent-ink: #f4f9f1;
  --shadow: #344f331c;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 0;
  background: var(--telegram-page, var(--page));
  color: var(--ink);
  padding: calc(var(--safe-top) + var(--padding)) calc(var(--safe-right) + var(--padding))
    calc(var(--safe-bottom) + var(--padding)) calc(var(--safe-left) + var(--padding));
}

.game {
  width: min(100%, 448px);
  margin-inline: auto;
  display: grid;
  grid-template-areas: "header" "intro" "scores" "board" "actions" "hint" "version";
  gap: clamp(12px, 1.8vh, 18px);
  gap: clamp(12px, calc(var(--telegram-height, 100dvh) / 55), 18px);
}

.topbar, .settings, .brand, .theme-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar {
  justify-content: space-between;
  grid-area: header;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.settings { flex-wrap: wrap; justify-content: flex-end; }

.brand {
  font-size: 0.85rem;
  letter-spacing: 0.14em;
  font-weight: 780;
}

.brand-mark {
  display: grid;
  grid-template-columns: repeat(2, 6px);
  gap: 2px;
}

.brand-mark i { height: 6px; border-radius: 2px; background: var(--accent); }
.brand-mark i:nth-child(2) { opacity: 0.48; }
.brand-mark i:nth-child(3) { opacity: 0.68; }

.theme-control { font-size: 0.8rem; color: var(--muted); }

button, select {
  font: inherit;
  color: var(--ink);
  min-height: 44px;
  border-radius: 10px;
}

button {
  border: 1px solid transparent;
  padding: 8px 15px;
  font-size: 0.88rem;
  font-weight: 720;
  cursor: pointer;
  touch-action: manipulation;
}

.secondary, select {
  background: var(--surface);
  border: 1px solid var(--line);
}

select { padding-inline: 8px 6px; font-weight: 650; }
.primary { background: var(--accent); color: var(--accent-ink); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.5; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

@media (hover: hover) {
  .secondary:hover:not(:disabled), select:hover { border-color: var(--muted); }
  .primary:hover { filter: brightness(0.93); }
}

.intro { grid-area: intro; }
h1, h2, p, dl, dd { margin: 0; }

h1 {
  font-size: clamp(3.5rem, 14vw, 5.05rem);
  line-height: 0.94;
  font-weight: 820;
  letter-spacing: -0.075em;
  font-variant-numeric: lining-nums;
}

.title-dot { color: var(--accent); }
.intro p { margin-top: 10px; color: var(--muted); font-size: 0.96rem; }

.scores {
  grid-area: scores;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.scores > div {
  display: grid;
  gap: 2px;
  padding: 10px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}

dt {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.085em;
  text-transform: uppercase;
}

dd {
  font-size: clamp(1.15rem, 5vw, 1.55rem);
  line-height: 1.2;
  font-weight: 780;
  font-variant-numeric: tabular-nums;
  overflow-wrap: anywhere;
}

.scores > div:last-child dd { color: var(--accent); }

.board {
  --gap: clamp(6px, 2vw, 11px);
  --inset: clamp(8px, 2.5vw, 14px);
  grid-area: board;
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 19px;
  background: var(--well);
  box-shadow: 0 12px 28px var(--shadow);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  container-type: inline-size;
}

.cells, .tiles { position: absolute; inset: var(--inset); pointer-events: none; }

.cells {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: var(--gap);
}

.cell {
  border-radius: clamp(7px, 2vw, 12px);
  background: var(--slot);
  box-shadow: inset 0 1px 0 var(--line);
}

.tile {
  position: absolute;
  inset: 0 auto auto 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  border-radius: clamp(7px, 2vw, 12px);
  display: grid;
  place-items: center;
  background: var(--tile-fill, #b8f18d);
  color: var(--tile-ink, #18271b);
  box-shadow: inset 0 1px 0 #ffffff25, 0 2px 4px #0003;
  font-size: 8.5cqi;
  line-height: 1;
  letter-spacing: -0.055em;
  font-weight: 820;
  font-variant-numeric: tabular-nums;
}

.tile[data-digits="4"] { font-size: 6.3cqi; }
.tile[data-digits="5"] { font-size: 4.9cqi; }
.tile[data-digits="6"], .tile[data-digits="7"], .tile[data-digits="8"] { font-size: 4cqi; }

.tile[data-value="2"] { --tile-fill: #344157; --tile-ink: #f6f8fb; }
.tile[data-value="4"] { --tile-fill: #485f77; --tile-ink: #fff; }
.tile[data-value="8"] { --tile-fill: #506b70; --tile-ink: #fff; }
.tile[data-value="16"] { --tile-fill: #559988; }
.tile[data-value="32"] { --tile-fill: #67a188; }
.tile[data-value="64"] { --tile-fill: #84bc9c; }
.tile[data-value="128"] { --tile-fill: #a3c981; }
.tile[data-value="256"] { --tile-fill: #c5d47d; }
.tile[data-value="512"] { --tile-fill: #e3d680; }
.tile[data-value="1024"] { --tile-fill: #f1cd87; }
.tile[data-value="2048"] { --tile-fill: #b8f18d; }

:root[data-theme="light"] .tile[data-value="2"] { --tile-fill: #f6f9f4; --tile-ink: #354741; }
:root[data-theme="light"] .tile[data-value="4"] { --tile-fill: #d4e1eb; --tile-ink: #253b4c; }
:root[data-theme="light"] .tile[data-value="8"] { --tile-fill: #b7d0cc; --tile-ink: #243e3b; }

.actions {
  grid-area: actions;
  display: grid;
  grid-template-columns: 1fr 1.35fr;
  gap: 10px;
}

.undo span { margin-inline-end: 3px; font-size: 1rem; }
.hint { grid-area: hint; }
.version { grid-area: version; font-variant-numeric: tabular-nums; }
.hint, .version { text-align: center; color: var(--muted); font-size: 0.78rem; }

dialog {
  width: min(380px, calc(100% - var(--safe-left) - var(--safe-right) - 32px));
  max-height: calc(var(--telegram-height, 100dvh) - var(--safe-top) - var(--safe-bottom) - 32px);
  margin: auto;
  inset: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
  padding: clamp(17px, 5vw, 25px);
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 20px 70px #0006;
}

dialog::backdrop { background: #060b14ad; }
dialog h2 { letter-spacing: -0.035em; }
dialog p { color: var(--muted); margin-top: 7px; line-height: 1.5; }
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 20px; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-height: 750px) and (orientation: portrait) {
  :root { --padding: clamp(12px, 4vw, 22px); }
  .game { gap: 9px; }
  h1 { font-size: clamp(3.05rem, 12vw, 4rem); }
  .topbar { padding-bottom: 8px; }
  .scores > div { padding-block: 7px; }
  .board {
    width: min(100%, max(230px, calc(var(--telegram-height, 100dvh) - var(--safe-top) - var(--safe-bottom) - 342px)));
    justify-self: center;
  }
}

@media (orientation: landscape) and (max-height: 540px) and (min-width: 550px) {
  :root { --padding: 12px; }
  .game {
    width: min(100%, 720px);
    grid-template-columns: minmax(215px, 1fr) minmax(0, auto);
    grid-template-areas:
      "header board" "intro board" "scores board" "actions board" "hint board" "version board";
    align-items: center;
    gap: 9px 20px;
  }
  .board { width: clamp(220px, calc(var(--telegram-height, 100dvh) - var(--safe-top) - var(--safe-bottom) - 24px), 380px); max-width: 100%; }
  .topbar { padding-bottom: 6px; }
  h1 { font-size: 3.2rem; }
  .hint, .version { text-align: start; }
}
