:root {
  --bg: #10070d;
  --panel: #1a0d15;
  --panel-soft: #24121d;
  --pink: #e84d91;
  --pink-light: #ff80b9;
  --gold: #d6ac5c;
  --cream: #fff7f0;
  --muted: #ad929f;
  --line: rgba(232, 77, 145, .19);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-width: 300px;
  min-height: 100vh;
  margin: 0;
  color: var(--cream);
  background:
    radial-gradient(circle at 50% -12%, rgba(232, 77, 145, .2), transparent 37%),
    linear-gradient(180deg, #160912 0%, var(--bg) 48%, #0b0508 100%);
  font-family: Manrope, system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }

.app-shell {
  width: min(100%, 540px);
  min-height: 100vh;
  margin: 0 auto;
  padding: calc(16px + var(--safe-top)) 16px calc(22px + var(--safe-bottom));
}

.game-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 15px;
  border-bottom: 1px solid var(--line);
}

.brand-mark {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid rgba(214, 172, 92, .65);
  border-radius: 50%;
  color: var(--gold);
  background: radial-gradient(circle at 35% 30%, #3b1a2d, #170a11 70%);
  box-shadow: inset 0 0 0 3px #13080e, 0 7px 25px rgba(0, 0, 0, .35);
  font: 700 22px/1 Cinzel, serif;
}

.header-copy { min-width: 0; }

.eyebrow,
.chapter {
  margin: 0;
  color: var(--pink-light);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .2em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  font-family: Cinzel, Georgia, serif;
}

h1 {
  margin-top: 2px;
  font-size: clamp(19px, 5.4vw, 26px);
  line-height: 1.1;
}

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  margin-left: auto;
  padding: 10px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 13px;
  color: var(--muted);
  background: rgba(255, 255, 255, .025);
  cursor: pointer;
}

.icon-button svg {
  width: 21px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.icon-button:active { transform: scale(.95); }

.intro {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  padding: 22px 2px 16px;
}

.intro h2 {
  max-width: 290px;
  margin-top: 5px;
  font-size: clamp(23px, 6.8vw, 34px);
  line-height: 1.05;
}

.intro > p {
  max-width: 170px;
  margin: 0 0 2px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.55;
  text-align: right;
}

.scorebar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-bottom: 15px;
  padding: 10px 5px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(51, 24, 40, .8), rgba(24, 11, 18, .92));
  box-shadow: 0 12px 38px rgba(0, 0, 0, .2);
}

.stat {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 0;
}

.stat-center { border-inline: 1px solid var(--line); }

.stat-icon {
  color: var(--pink);
  font: 700 17px/1 Cinzel, serif;
}

.stat small,
.stat strong { display: block; }

.stat small {
  margin-bottom: 1px;
  color: var(--muted);
  font-size: 8px;
  font-weight: 700;
  letter-spacing: .13em;
  text-transform: uppercase;
}

.stat strong {
  color: var(--cream);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.stat em {
  color: #745868;
  font-size: 10px;
  font-style: normal;
}

.board-wrap {
  padding: 9px;
  border: 1px solid rgba(214, 172, 92, .12);
  border-radius: 22px;
  background: rgba(255, 255, 255, .025);
  box-shadow: inset 0 1px rgba(255, 255, 255, .03), 0 22px 50px rgba(0, 0, 0, .2);
}

.board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(6px, 2vw, 10px);
  perspective: 900px;
}

.memory-card {
  position: relative;
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 13px;
  aspect-ratio: .78;
  background: transparent;
  cursor: pointer;
  transform-style: preserve-3d;
  transition: transform .52s cubic-bezier(.2, .72, .25, 1), filter .25s ease;
}

.memory-card.is-flipped,
.memory-card.is-matched { transform: rotateY(180deg); }

.memory-card.is-matched { cursor: default; }

.memory-card.is-matched .card-face-back {
  border-color: rgba(214, 172, 92, .75);
  box-shadow: 0 0 0 1px rgba(214, 172, 92, .18), 0 0 20px rgba(214, 172, 92, .14);
}

.card-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: inherit;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.card-face-front {
  display: grid;
  place-items: center;
  border: 1px solid rgba(232, 77, 145, .28);
  background:
    linear-gradient(135deg, transparent 18%, rgba(232, 77, 145, .08) 18%, rgba(232, 77, 145, .08) 21%, transparent 21%) 0 0 / 18px 18px,
    radial-gradient(circle at 50% 35%, #432039, #1d0c16 67%);
  box-shadow: inset 0 0 0 4px #170b12, inset 0 0 0 5px rgba(214, 172, 92, .24), 0 8px 20px rgba(0, 0, 0, .3);
}

.card-face-front::before {
  content: "S";
  display: grid;
  width: 43%;
  aspect-ratio: 1;
  place-items: center;
  border: 1px solid rgba(214, 172, 92, .55);
  border-radius: 50%;
  color: var(--gold);
  background: #180a12;
  box-shadow: 0 0 0 4px rgba(10, 4, 8, .75);
  font: 700 clamp(16px, 6vw, 27px)/1 Cinzel, serif;
}

.card-face-back {
  transform: rotateY(180deg);
  border: 1px solid rgba(232, 77, 145, .45);
  background: #26101c;
  box-shadow: 0 8px 20px rgba(0, 0, 0, .28);
}

.card-face-back img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  pointer-events: none;
}

.memory-card:focus-visible {
  outline: 2px solid var(--pink-light);
  outline-offset: 3px;
}

.game-message {
  min-height: 18px;
  margin: 11px 0 1px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .05em;
  text-align: center;
}

.game-message.is-match { color: var(--gold); }
.game-message.is-miss { color: var(--pink-light); }

.best-result {
  display: flex;
  justify-content: space-between;
  margin: 13px 4px 0;
  color: var(--muted);
  font-size: 11px;
}

.best-result strong { color: var(--gold); }

.modal-backdrop {
  position: fixed;
  z-index: 20;
  inset: 0;
  display: grid;
  place-items: end center;
  padding: 18px 16px calc(18px + var(--safe-bottom));
  background: rgba(5, 2, 4, .78);
  backdrop-filter: blur(9px);
}

.modal-backdrop[hidden] { display: none; }

.finish-card {
  width: min(100%, 480px);
  padding: 23px;
  border: 1px solid rgba(214, 172, 92, .35);
  border-radius: 25px;
  background:
    radial-gradient(circle at 50% 0, rgba(232, 77, 145, .19), transparent 45%),
    #1c0d16;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .6);
  text-align: center;
  animation: finish-in .42s cubic-bezier(.2, .8, .2, 1) both;
}

.finish-emblem {
  display: grid;
  width: 58px;
  height: 58px;
  margin: 0 auto 13px;
  place-items: center;
  border: 1px solid rgba(214, 172, 92, .6);
  border-radius: 50%;
  color: var(--gold);
  background: #12070d;
  font-size: 27px;
}

.finish-card h2 {
  margin-top: 6px;
  font-size: 29px;
}

.finish-copy {
  margin: 7px 0 20px;
  color: var(--muted);
  font-size: 13px;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-block: 1px solid var(--line);
}

.result-grid div { padding: 15px 5px; }
.result-grid div + div { border-left: 1px solid var(--line); }

.result-grid small,
.result-grid strong { display: block; }

.result-grid small {
  color: var(--muted);
  font-size: 8px;
  font-weight: 700;
  letter-spacing: .14em;
  text-transform: uppercase;
}

.result-grid strong {
  margin-top: 3px;
  color: var(--cream);
  font-size: 19px;
  font-variant-numeric: tabular-nums;
}

.rank-line {
  margin: 14px 0;
  color: var(--muted);
  font-size: 12px;
}

.rank-line strong {
  margin-left: 5px;
  color: var(--gold);
  font-family: Cinzel, serif;
  font-size: 15px;
}

.primary-button {
  width: 100%;
  min-height: 51px;
  border: 0;
  border-radius: 14px;
  color: #190910;
  background: linear-gradient(135deg, #ff79b6, #d83b7f);
  box-shadow: 0 10px 28px rgba(232, 77, 145, .23);
  font-weight: 800;
  cursor: pointer;
}

.primary-button:active { transform: translateY(1px); }

@keyframes finish-in {
  from { opacity: 0; transform: translateY(25px) scale(.97); }
  to { opacity: 1; transform: none; }
}

@media (max-width: 380px) {
  .app-shell { padding-inline: 11px; }
  .intro { padding-top: 17px; }
  .intro > p { display: none; }
  .scorebar { margin-bottom: 10px; }
  .board-wrap { padding: 7px; }
  .stat { gap: 5px; }
}

@media (min-width: 700px) {
  .modal-backdrop { place-items: center; }
}

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

/* Compact Telegram Mini App layout. */
.app-shell {
  width: min(100%, 430px);
  padding: calc(9px + var(--safe-top)) 11px calc(12px + var(--safe-bottom));
}

.game-header { gap: 9px; padding-bottom: 8px; }
.brand-mark { width: 36px; height: 36px; font-size: 18px; }
h1 { margin-top: 1px; font-size: clamp(16px, 4.8vw, 21px); }
.icon-button { width: 36px; height: 36px; padding: 8px; }
.icon-button svg { width: 18px; }
.intro { gap: 12px; padding: 11px 2px 9px; }
.intro h2 { margin-top: 3px; font-size: clamp(19px, 5.7vw, 25px); }
.intro > p { max-width: 145px; font-size: 9px; line-height: 1.4; }
.scorebar { margin-top: 9px; margin-bottom: 8px; padding: 7px 4px; border-radius: 13px; }
.stat { gap: 5px; }
.stat-icon { font-size: 14px; }
.stat small { font-size: 7px; }
.stat strong { font-size: 13px; }
.board-wrap { padding: 7px; border-radius: 17px; }
.board { gap: clamp(5px, 1.5vw, 7px); }
.memory-card { border-radius: 10px; aspect-ratio: .88; }
.game-message { margin: 7px 0 0; font-size: 10px; }
.best-result { margin: 7px 4px 0; }

@media (max-width: 380px) {
  .app-shell { padding-inline: 8px; }
  .intro { padding-top: 9px; }
  .scorebar { margin-bottom: 7px; }
  .board-wrap { padding: 6px; }
}
