/* ── Vocabulary matching game UI ──────────────────────────────────── */

.mg-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: clamp(10px, 1.5vh, 16px);
}
.mg-title {
  font-size: clamp(1.2rem, 2vh, 1.45rem);
  font-weight: 800;
  color: var(--text);
}
.mg-round-info {
  font-size: clamp(0.9rem, 1.4vh, 1.05rem);
  color: var(--muted);
  font-weight: 500;
}
.mg-view-toggle {
  display: flex;
  background: #111;
  border-radius: 8px;
  padding: 2px;
  gap: 2px;
  align-self: center;
}
.mg-view-toggle .mg-vt-btn {
  border: none;
  background: transparent;
  color: #ddd;
  font-family: var(--font-sans);
  font-size: clamp(0.8rem, 1.4vh, 0.95rem);
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 6px;
  cursor: pointer;
  line-height: 1.5;
  transition: background 0.18s, color 0.18s;
  white-space: nowrap;
}
.mg-vt-btn.mg-vt-active {
  background: #fff;
  color: #111;
}
/* Arena: holds background image + tile grid */
.mg-arena {
  position: relative;
  width: 100%;
  border-radius: 16px;
  overflow: hidden;
  background: var(--surface2);
}
.mg-bg-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 16px;
  z-index: 0;
  opacity: 0.12;
  transition: opacity 0.6s ease;
}
.mg-grid {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: clamp(14px, 2.8vmin, 22px);
  padding: clamp(6px, 1.2vmin, 14px) calc(clamp(6px, 1.2vmin, 14px) + 9px) calc(clamp(6px, 1.2vmin, 14px) + 9px) clamp(6px, 1.2vmin, 14px);
  width: 100%;
  max-width: min(480px, 100%);
  margin: 0 auto;
  box-sizing: border-box;
}

/* Mahjong tile shadow wrapper */
.mg-tile-shadow {
  position: relative;
  border-radius: 2px 2px 0 2px;
  height: clamp(50px, calc((100vh - 240px) / 6), 120px);
  min-width: 0;
  width: 100%;
  transition: transform 0.15s;
}
.mg-tile-shadow:not(.mg-matched):hover { transform: translateY(-2px); }
.mg-tile-shadow:has(.mg-tile.mg-selected) { transform: translateY(-1px); }
.mg-tile-shadow::before {
  content: '';
  position: absolute;
  inset: 0;
  clip-path: polygon(4px 0, calc(100% - 4px) 0, 100% 4px, 100% calc(100% - 4px), calc(100% - 4px) 100%, 4px 100%, 0 calc(100% - 4px), 0 4px);
  background: #2d6b47;
  transform: translate(9px, 9px);
}
.mg-tile-shadow::after {
  content: '';
  position: absolute;
  inset: 0;
  clip-path: polygon(4px 0, calc(100% - 4px) 0, 100% 4px, 100% calc(100% - 4px), calc(100% - 4px) 100%, 4px 100%, 0 calc(100% - 4px), 0 4px);
  background: #4A9E6B;
  transform: translate(5px, 5px);
}

/* Mahjong tile */
.mg-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(3px, 1vmin, 8px) clamp(2px, 0.8vmin, 6px);
  clip-path: polygon(4px 0, calc(100% - 4px) 0, 100% 4px, 100% calc(100% - 4px), calc(100% - 4px) 100%, 4px 100%, 0 calc(100% - 4px), 0 4px);
  background: linear-gradient(160deg, #ffffff 0%, #f5f0e8 100%);
  border: none;
  position: relative;
  z-index: 1;
  box-shadow:
    0 2px 4px rgba(0,0,0,0.12),
    inset 0 1px 0 rgba(255,255,255,0.9),
    inset 0 -1px 2px rgba(0,0,0,0.04);
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  height: 100%;
  user-select: none;
  overflow: hidden;
  width: 100%;
  perspective: 600px;
  container-type: size;
}
.mg-tile::before {
  content: '';
  position: absolute;
  inset: 4px;
  border-radius: 2px;
  border: 1px solid rgba(0,0,0,0.1);
  pointer-events: none;
  z-index: -1;
}
.mg-tile:hover:not(.mg-matched):not(.mg-selected) {
  box-shadow:
    0 4px 8px rgba(0,0,0,0.15),
    inset 0 1px 0 rgba(255,255,255,0.9);
}
.mg-tile.mg-selected {
  border: 4px solid rgba(106,90,205,0.95);
  box-shadow:
    0 0 20px rgba(106,90,205,0.65),
    inset 0 1px 0 rgba(255,255,255,0.9);
}
.mg-tile-shadow:has(.mg-tile.mg-wrong) {
  animation: mg-shake 0.4s ease;
}
.mg-tile.mg-matched {
  pointer-events: none;
}
.mg-tile-shadow.mg-matched {
  animation: mg-vanish 0.65s ease forwards;
  pointer-events: none;
}

/* Tile text */
.mg-tile-char {
  font-family: 'Noto Serif SC', serif;
  font-size: clamp(1rem, 22cqmin, 2.2rem);
  font-weight: 700;
  color: #C41E1E !important;
  line-height: 1.1;
  text-shadow: 0 1px 0 rgba(140,20,20,0.08);
  white-space: nowrap;
}
.mg-tile-english {
  font-family: var(--font-sans);
  font-size: clamp(0.55rem, 7cqmin, 0.85rem);
  font-weight: 600;
  color: #666;
  line-height: 1.15;
  margin-top: 2px;
  padding: 0 clamp(3px, 0.8vmin, 6px);
  text-align: center;
  word-break: break-word;
}
.mg-tile-pinyin {
  font-family: var(--font-sans);
  font-size: clamp(0.7rem, 14cqmin, 1.2rem);
  font-weight: 700;
  color: #4A4A4A;
  line-height: 1.15;
}
.mg-tile-front .mg-tile-pinyin {
  color: #C41E1E;
}
.mg-tile-meaning {
  font-family: var(--font-sans);
  font-size: clamp(0.85rem, 20cqmin, 1.4rem);
  font-weight: 700;
  color: #1A1A1A;
  line-height: 1.2;
  text-align: center;
  padding: 0 clamp(2px, 0.8vmin, 6px);
  word-break: normal;
  overflow-wrap: normal;
  hyphens: none;
}

/* Flip container for Chinese tiles */
.mg-tile-flip {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.45s ease;
}
.mg-tile.mg-flipped .mg-tile-flip {
  transform: rotateY(180deg);
}
.mg-tile-front, .mg-tile-back {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.mg-tile-back {
  transform: rotateY(180deg);
}
.mg-tile.mg-flipped { perspective: 600px; }

/* Char-first mode: character visible by default, flip reveals pinyin */
.mg-char-first .mg-tile-flip { transform: rotateY(180deg); }
.mg-char-first .mg-tile.mg-flipped .mg-tile-flip { transform: rotateY(0deg); }

@keyframes mg-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(2px); }
}
@keyframes mg-vanish {
  0%   { opacity: 1; transform: scale(1); }
  55%  { opacity: 1; transform: scale(1.28); }
  100% { opacity: 0; transform: scale(1.45); visibility: hidden; }
}

@media (max-width: 640px) {
  .mg-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
