@import url("https://fonts.googleapis.com/css2?family=Merriweather:wght@700&family=Nunito:wght@500;700&display=swap");

:root {
  --bg: #f9f3f0;
  --panel: #ffffff;
  --ink: #3c3a37;
  --accent: #f2c6c0;
  --accent-2: #cfe8e6;
  --grid: #f0e2df;
  --shadow: 0 12px 30px rgba(60, 58, 55, 0.12);
  --radius: 20px;
  --font-display: "Merriweather", "Georgia", serif;
  --font-ui: "Nunito", "Trebuchet MS", sans-serif;
  --viewport-height: 100vh;
  --app-scale: 1;
  --ad-safe-bottom: 0px;
  --mobile-ui-scale: 1;
}

* {
  box-sizing: border-box;
}

html,
body,
.app,
.topbar,
.title-block,
.scores,
.score-box,
.board-wrap,
.board,
.cell,
.tile,
.tile-layer,
.overlay,
.overlay-card,
.footer,
.hint,
.btn,
.fact-row,
.fact,
.subtitle,
.title-row,
.brand-mark,
.tile .num {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
}

body {
  margin: 0;
  min-height: var(--viewport-height);
  width: 100%;
  height: 100%;
  padding: 0;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at top, #fff8f6 0%, #f9f3f0 55%),
    repeating-linear-gradient(135deg, rgba(255, 255, 255, 0.6) 0 18px, rgba(243, 226, 221, 0.6) 18px 36px);
  color: var(--ink);
  font-family: var(--font-ui);
  touch-action: none;
  overscroll-behavior: none;
  overflow: hidden;
}

html {
  width: 100%;
  height: 100%;
  overscroll-behavior: none;
  overflow: hidden;
}

img {
  -webkit-user-drag: none;
  user-select: none;
}

.app {
  position: fixed;
  left: 50%;
  top: calc((var(--viewport-height) - var(--ad-safe-bottom, 0px)) / 2);
  width: 580px;
  height: 736px;
  max-width: none;
  min-width: 0;
  max-height: none;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
  transform: translate(-50%, -50%) scale(var(--app-scale));
  transform-origin: center center;
  overflow: hidden;
}

.app.is-booting {
  visibility: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "title scores"
    "fact fact"
    "language language";
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
  max-width: 100%;
  transition: gap 120ms ease;
}

.title-block h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 32px;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.title-block {
  grid-area: title;
  min-width: 0;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  flex-wrap: nowrap;
}

.brand-mark {
  width: 44px;
  height: 44px;
  object-fit: contain;
  flex: none;
  filter: drop-shadow(0 3px 8px rgba(60, 58, 55, 0.12));
}

.subtitle {
  margin: 6px 0 0;
  min-height: 38px;
  font-size: 14px;
  line-height: 1.35;
  color: #7a716b;
  max-width: 100%;
  display: -webkit-box;
  overflow-wrap: break-word;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.fact {
  min-width: 0;
  margin: 0;
  height: 64px;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid #ead9d6;
  background: linear-gradient(135deg, #fffaf8 0%, #fff2ed 100%);
  color: #5f524c;
  font-size: 13px;
  line-height: 1.45;
  display: -webkit-box;
  overflow-wrap: break-word;
  overflow: hidden;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.fact-row {
  grid-area: fact;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-width: 0;
  width: 100%;
}

.rules-btn {
  flex: none;
  border: none;
  padding: 8px 13px;
  border-radius: 999px;
  background: var(--accent-2);
  color: #4f6664;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.rules-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.rules-btn:focus-visible {
  outline: 2px solid #8fc0bc;
  outline-offset: 2px;
}

.scores {
  grid-area: scores;
  display: flex;
  gap: 8px;
}

.topbar .lang-switch {
  grid-area: language;
  justify-self: end;
}

.score-box {
  background: var(--accent-2);
  border-radius: 14px;
  padding: 10px 14px;
  text-align: center;
  min-width: 80px;
}

.score-box .label {
  display: block;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #5d6c6a;
}

.score-box .value {
  display: block;
  font-size: 20px;
  font-weight: 700;
}

.board-wrap {
  position: relative;
  margin: 24px 0 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  transition: margin 120ms ease;
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  width: var(--board-size, 380px);
  aspect-ratio: 1;
  background: var(--grid);
  padding: 12px;
  border-radius: 18px;
  touch-action: none;
  overscroll-behavior: contain;
  transition: gap 120ms ease, padding 120ms ease, width 120ms ease, transform 120ms ease;
  contain: layout paint style;
}

.motion-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.cell {
  position: relative;
  width: 100%;
  padding-top: 100%;
  background: #f8eeeb;
  border: 1px solid #ead9d6;
  border-radius: 16px;
  contain: layout paint style;
}

.tile {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-family: var(--font-display);
  font-size: 28px;
  font-weight: 700;
  color: #5f524c;
  transform: translate3d(0, 0, 0) scale(0.98);
  opacity: 1;
  transition: transform 120ms ease;
  background-position: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  backface-visibility: hidden;
  contain: layout paint style;
}

.tile.spawn {
  animation: tileReveal 140ms ease;
}

.tile.arrive {
  animation: tileArrive 150ms ease 130ms both;
}

.tile.merge {
  animation: tileMerge 190ms cubic-bezier(0.2, 1.15, 0.35, 1) 130ms both;
}

.tile-layer {
  position: absolute;
  inset: 0;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100% 100%;
  pointer-events: none;
  z-index: 0;
}

.tile-layer.outline {
  z-index: 1;
}

.tile-art {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate3d(-50%, -50%, 0);
  object-fit: fill;
  pointer-events: none;
  z-index: 0;
  backface-visibility: hidden;
}

.tile-art-composite {
  width: 100%;
  height: 100%;
}

.tile-art.outline-art {
  z-index: 1;
}

.tile .num {
  position: relative;
  z-index: 2;
}

.tile.pop {
  transform: translate3d(0, 0, 0) scale(1.05);
}

.motion-clone {
  position: absolute;
  pointer-events: none;
  z-index: 6;
  will-change: opacity, transform;
}

@keyframes tileReveal {
  from {
    transform: translate3d(0, 0, 0) scale(0.92);
  }

  to {
    transform: translate3d(0, 0, 0) scale(0.98);
  }
}

@keyframes tileArrive {
  from {
    opacity: 0;
    transform: translate3d(0, 0, 0) scale(0.88);
  }

  70% {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(1.04);
  }

  to {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(0.98);
  }
}

@keyframes tileMerge {
  from {
    opacity: 0;
    transform: translate3d(0, 0, 0) scale(0.84);
  }

  50% {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(1.08);
  }

  100% {
    opacity: 1;
    transform: translate3d(0, 0, 0) scale(0.98);
  }
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(249, 243, 240, 0.7);
  border-radius: 18px;
  z-index: 20;
  pointer-events: auto;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  background: var(--panel);
  padding: 20px 26px;
  border-radius: 18px;
  box-shadow: var(--shadow);
  text-align: center;
}

.overlay-text {
  font-size: 20px;
  margin-bottom: 12px;
}

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

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

.rules-overlay {
  position: fixed;
  inset: 12px;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(249, 243, 240, 0.76);
  border-radius: 18px;
  z-index: 40;
  touch-action: pan-y;
}

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

.rules-card {
  position: relative;
  width: min(100%, 460px);
  max-height: calc(var(--viewport-height) - 24px - var(--ad-safe-bottom, 0px));
  overflow: auto;
  touch-action: pan-y;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 20px;
}

.rules-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: #f6e6e2;
  color: #6f5b55;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.rules-header {
  padding-right: 42px;
}

.rules-header h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 26px;
}

.rules-intro {
  margin: 10px 0 0;
  color: #6f625c;
  line-height: 1.45;
}

.rules-examples {
  display: grid;
  gap: 12px;
  margin-top: 16px;
}

.rule-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.rule-slot {
  position: relative;
  width: 76px;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 14px;
  background: #f8eeeb;
  border: 1px solid #ead9d6;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.35);
}

.rule-slot .tile {
  font-size: 18px;
}

.rule-symbol {
  font-size: 22px;
  font-weight: 700;
  color: #8b7a73;
}

.rules-texts {
  margin-top: 16px;
  display: grid;
  gap: 8px;
}

.rules-texts p {
  margin: 0;
  padding: 0;
  color: #5f524c;
  line-height: 1.45;
}

@media (pointer: fine) {
  .rules-overlay {
    inset: 0;
    padding: 0;
  }

  .rules-card {
    position: fixed;
    left: 50%;
    top: calc((var(--viewport-height) - var(--ad-safe-bottom, 0px)) / 2);
    width: 460px;
    max-height: none;
    transform: translate(-50%, -50%) scale(var(--app-scale));
    transform-origin: center center;
  }
}

.btn {
  border: none;
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--accent);
  color: #5b4944;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.footer {
  display: grid;
  grid-template-columns: auto minmax(178px, 1fr) auto;
  align-items: center;
  column-gap: 14px;
  row-gap: 8px;
  min-width: 0;
  max-width: 100%;
}

.lang-switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border-radius: 999px;
  background: #fff8f6;
  border: 1px solid #ead9d6;
  box-shadow: 0 6px 16px rgba(60, 58, 55, 0.06);
}

.lang-btn {
  border: none;
  min-width: 44px;
  padding: 8px 12px;
  border-radius: 999px;
  background: transparent;
  color: #7a716b;
  font-weight: 700;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, transform 120ms ease;
}

.lang-btn:hover {
  transform: translateY(-1px);
}

.lang-btn.is-active {
  background: var(--accent-2);
  color: #4f6664;
}

.sound-control {
  display: flex;
  align-items: center;
  justify-self: center;
  gap: 10px;
  width: min(100%, 232px);
  min-width: 0;
  padding: 8px 12px;
  border-radius: 999px;
  background: #fff8f6;
  border: 1px solid #ead9d6;
  box-shadow: 0 6px 16px rgba(60, 58, 55, 0.06);
}

.mute-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: var(--accent-2);
  color: #4f6664;
  cursor: pointer;
  padding: 0;
  flex: none;
  transition: transform 120ms ease, background 120ms ease, opacity 120ms ease;
}

.mute-btn:hover {
  transform: translateY(-1px);
}

.mute-btn.is-muted {
  background: #ead9d6;
  color: #8a7d78;
}

.mute-icon {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.mute-icon .wave {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.mute-btn.is-muted .wave {
  opacity: 0.2;
}

.volume-slider {
  flex: 1 1 90px;
  width: 100%;
  min-width: 80px;
  max-width: 160px;
  accent-color: var(--accent);
  cursor: pointer;
}

.volume-slider:focus {
  outline: none;
}

.volume-slider::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 999px;
}

.volume-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ffffff;
  border: 2px solid #d9b6b0;
  margin-top: -5px;
  box-shadow: 0 2px 5px rgba(60, 58, 55, 0.16);
}

.volume-slider::-moz-range-track {
  height: 8px;
  border-radius: 999px;
  background: #edded9;
}

.volume-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #ffffff;
  border: 2px solid #d9b6b0;
  box-shadow: 0 2px 5px rgba(60, 58, 55, 0.16);
}

.hint {
  font-size: 12px;
  color: #7a716b;
  justify-self: end;
  text-align: right;
  white-space: nowrap;
}

.app.is-compact-height {
  padding: 16px;
}

.app.is-compact-height .topbar {
  gap: 10px;
}

.app.is-compact-height .title-block h1 {
  font-size: 28px;
  line-height: 1.08;
}

.app.is-compact-height .brand-mark {
  width: 34px;
  height: 34px;
}

.app.is-compact-height .subtitle {
  min-height: 32px;
  margin-top: 4px;
  font-size: 12px;
}

.app.is-compact-height .fact {
  height: 52px;
  padding: 8px 10px;
  font-size: 12px;
  line-height: 1.35;
}

.app.is-compact-height .score-box {
  min-width: 70px;
  padding: 8px 10px;
}

.app.is-compact-height .score-box .value {
  font-size: 18px;
}

.app.is-compact-height .board-wrap {
  margin: 10px 0 10px;
}

.app.is-compact-height .board {
  gap: 8px;
  padding: 8px;
}

.app.is-compact-height .footer {
  column-gap: 10px;
  row-gap: 8px;
}

.app.is-compact-height .sound-control {
  width: min(100%, 210px);
  padding: 6px 10px;
}

.app.is-compact-height .volume-slider {
  max-width: 130px;
}

.app.is-tiny-height {
  padding: 12px;
}

.app.is-tiny-height .title-block h1 {
  font-size: 23px;
}

.app.is-tiny-height .title-row {
  gap: 8px;
}

.app.is-tiny-height .brand-mark {
  width: 26px;
  height: 26px;
}

.app.is-tiny-height .rules-btn,
.app.is-tiny-height .btn,
.app.is-tiny-height .lang-btn {
  padding: 6px 9px;
  font-size: 12px;
}

.app.is-tiny-height .fact {
  height: 40px;
  padding: 6px 8px;
  font-size: 11px;
}

.app.is-tiny-height .board-wrap {
  margin: 6px 0;
}

.app.is-tiny-height .board {
  gap: 6px;
  padding: 6px;
  border-radius: 14px;
}

.app.is-tiny-height .cell {
  border-radius: 10px;
}

.app.is-tiny-height .tile {
  font-size: 18px;
}

.app.is-tiny-height .score-box {
  min-width: 62px;
  padding: 6px 8px;
}

.app.is-tiny-height .score-box .label {
  font-size: 10px;
}

.app.is-tiny-height .score-box .value {
  font-size: 16px;
}

.app.is-tiny-height .mute-btn {
  width: 30px;
  height: 30px;
}

.app.is-tiny-height .hint {
  font-size: 11px;
}

@media (max-width: 520px) and (pointer: coarse) {
  .app {
    position: relative;
    left: auto;
    top: auto;
    width: calc(100vw - 24px);
    height: auto;
    max-width: calc(100vw - 24px);
    min-width: 0;
    padding: 14px;
  }

  .topbar {
    gap: 8px;
  }

  .title-block {
    flex: 1 1 100%;
    width: 100%;
  }

  .title-row {
    gap: 10px;
    min-width: 0;
  }

  .brand-mark {
    width: 32px;
    height: 32px;
  }

  .title-block h1 {
    font-size: 22px;
  }

  html[lang="ru"] .title-block h1 {
    font-size: 20px;
  }

  .tile {
    font-size: 18px;
  }

  .subtitle {
    min-height: 32px;
    font-size: 12px;
    margin-top: 4px;
  }

  .fact {
    height: 50px;
    padding: 8px 10px;
    font-size: 11px;
  }

  .fact-row {
    gap: 8px;
  }

  .rules-btn {
    padding: 7px 10px;
    font-size: 12px;
  }

  .scores {
    gap: 6px;
  }

  .score-box {
    min-width: 68px;
    padding: 8px 10px;
  }

  .score-box .value {
    font-size: 18px;
  }

  .board-wrap {
    margin: 14px 0 12px;
  }

  .board {
    gap: 8px;
    padding: 8px;
  }

  .cell {
    border-radius: 12px;
  }

  .overlay-card {
    padding: 16px 20px;
  }

  .overlay-text {
    font-size: 18px;
  }

  .btn {
    padding: 8px 12px;
  }

  .rules-overlay {
    padding: 12px;
  }

  .rules-card {
    padding: 16px;
  }

  .rules-header h2 {
    font-size: 22px;
  }

  .rule-slot {
    width: 62px;
  }

  .rule-symbol {
    font-size: 18px;
  }

  .lang-switch {
    padding: 4px;
    gap: 4px;
  }

  .lang-btn {
    min-width: 38px;
    padding: 7px 10px;
  }

  .footer {
    grid-template-columns: auto minmax(150px, 1fr);
  }

  .footer .hint {
    grid-column: 1 / -1;
    justify-self: start;
    text-align: left;
  }

  .sound-control {
    width: 100%;
    justify-content: center;
    padding: 6px 10px;
    gap: 8px;
  }

  .volume-slider {
    max-width: 140px;
  }
}

@media (max-width: 380px) and (pointer: coarse) {
  .title-row {
    gap: 6px;
  }

  .brand-mark {
    width: 24px;
    height: 24px;
  }

  .title-block h1 {
    font-size: 18px;
  }

  html[lang="ru"] .title-block h1 {
    font-size: 17px;
  }

  .rules-btn {
    padding: 6px 9px;
    font-size: 11px;
  }

  .score-box {
    min-width: 56px;
    padding: 6px 7px;
  }

  .score-box .label {
    font-size: 9px;
  }

  .score-box .value {
    font-size: 16px;
  }

  .fact {
    height: 44px;
    padding: 7px 9px;
    font-size: 10px;
    line-height: 1.35;
  }
}

@media (max-width: 640px) and (pointer: coarse) {
  body {
    padding: 0;
    place-items: stretch;
  }

  .app {
    position: relative;
    left: auto;
    top: auto;
    width: min(100vw, 580px);
    max-width: none;
    height: auto;
    min-height: calc(var(--viewport-height) - var(--ad-safe-bottom, 0px));
    max-height: none;
    display: flex;
    flex-direction: column;
    border-radius: 0;
    box-shadow: none;
    overflow: visible;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    transform: scale(var(--mobile-ui-scale));
    transform-origin: top center;
  }

  .board-wrap {
    flex: 1 1 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 12px 0 8px;
    width: 100%;
  }

  .footer {
    margin-top: auto;
    grid-template-columns: auto minmax(150px, 1fr);
  }

  .footer .hint {
    grid-column: 1 / -1;
    justify-self: start;
    text-align: left;
  }

  .topbar .lang-switch {
    margin-left: 0;
  }

  .board {
    width: min(100%, var(--board-size, 420px));
    max-width: 100%;
  }
}
