* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --accent: #8b5cf6;
  --text: #1e293b;
  --glass: rgba(255, 255, 255, 0.9);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
}

html,
body {
  height: 100%;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: linear-gradient(135deg, #667eea, #764ba2);
  background-attachment: fixed;
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 14px 30px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #fff;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 54px;
  height: 54px;
  border-radius: 16px;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.28));
  background: #fff;
  flex-shrink: 0;
}

.brand h1 {
  font-size: 1.6rem;
  line-height: 1.05;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.22);
}

.tagline {
  font-size: 0.74rem;
  opacity: 0.85;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  font-weight: 600;
  margin-top: 2px;
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 46px;
  height: 46px;
  border-radius: 14px;
  border: none;
  cursor: pointer;
  background: rgba(255, 255, 255, 0.2);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  color: #fff;
  font-size: 1.2rem;
  transition: transform 0.15s ease, background 0.2s ease;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.34);
  transform: translateY(-1px);
}

.icon-btn:active {
  transform: scale(0.94);
}

/* ---------- Controls ---------- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 16px;
  align-items: flex-end;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.92);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  border: none;
  cursor: pointer;
  font-weight: 800;
  font-family: inherit;
  color: #fff;
  background: rgba(255, 255, 255, 0.22);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.28);
  transition: transform 0.12s ease, background 0.2s ease, box-shadow 0.2s ease;
}

.chip:hover:not(:disabled) {
  transform: translateY(-2px);
  background: rgba(255, 255, 255, 0.4);
}

.chip.active {
  background: var(--accent);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
}

.chip.op {
  min-width: 50px;
  height: 44px;
  padding: 0 12px;
  border-radius: 13px;
  font-size: 1.35rem;
  line-height: 1;
}

.chip.op.mixed {
  font-size: 0.88rem;
  letter-spacing: 0.02em;
}

.chip.level {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  font-size: 1.05rem;
}

/* ---------- Stats ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  background: var(--glass);
  border-radius: 18px;
  padding: 10px;
  box-shadow: var(--shadow);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-value {
  font-weight: 800;
  font-size: 1.25rem;
  color: #0f172a;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 0.64rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: #64748b;
}

/* ---------- How-to hint ---------- */
.howto {
  text-align: center;
  color: #fff;
  font-size: 0.98rem;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.16);
  border-radius: 12px;
  padding: 8px 12px;
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}

/* ---------- Board ---------- */
.board-wrap {
  position: relative;
}

.board {
  display: grid;
  gap: var(--gap, 8px);
  grid-template-columns: repeat(var(--cols, 3), var(--card-size, 150px));
  grid-auto-rows: var(--card-size, 150px);
  justify-content: center;
  perspective: 1000px;
}

.board-enter .card {
  animation: deal 0.45s ease both;
}

.card {
  position: relative;
  width: var(--card-size, 150px);
  height: var(--card-size, 150px);
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.34, 1.3, 0.64, 1);
}

.card.flipped .card-inner {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: calc(var(--card-size, 150px) * 0.14);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}

.card-back {
  background-color: #6366f1;
  background-image:
    radial-gradient(rgba(255, 255, 255, 0.16) 1.2px, transparent 1.2px),
    linear-gradient(145deg, #6366f1, #8b5cf6);
  background-size: 14px 14px, 100% 100%;
  color: rgba(255, 255, 255, 0.92);
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.28), 0 6px 16px rgba(0, 0, 0, 0.22);
}

.card-back-icon {
  font-size: calc(var(--card-size, 150px) * 0.4);
  font-weight: 800;
  text-shadow: 0 3px 8px rgba(0, 0, 0, 0.25);
}

.card-front {
  transform: rotateY(180deg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.22);
}

.card-front.eq {
  background: linear-gradient(145deg, #eef2ff, #e0e7ff);
}

.card-front.ans {
  background: linear-gradient(145deg, #ffffff, #f8fafc);
}

.card-expr {
  font-size: calc(var(--card-size, 150px) * 0.27);
  font-weight: 800;
  color: var(--accent);
  letter-spacing: 0.01em;
  padding: 0 8px;
  text-align: center;
}

.card-num {
  font-size: calc(var(--card-size, 150px) * 0.5);
  font-weight: 800;
  color: #0f172a;
  font-variant-numeric: tabular-nums;
  padding: 0 8px;
}

.op-badge,
.ans-badge {
  position: absolute;
  top: calc(var(--card-size, 150px) * 0.05);
  min-width: calc(var(--card-size, 150px) * 0.17);
  height: calc(var(--card-size, 150px) * 0.17);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  color: #fff;
  font-weight: 800;
  line-height: 1;
  padding: 0 5px;
}

.op-badge {
  left: calc(var(--card-size, 150px) * 0.05);
  background: var(--accent);
  font-size: calc(var(--card-size, 150px) * 0.1);
}

.ans-badge {
  right: calc(var(--card-size, 150px) * 0.05);
  background: #f59e0b;
  font-size: calc(var(--card-size, 150px) * 0.11);
}

.card:hover:not(.flipped):not(.matched) .card-back {
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.5), 0 0 18px rgba(255, 255, 255, 0.55), 0 6px 16px rgba(0, 0, 0, 0.22);
}

.card:focus-visible {
  outline: 3px solid #fff;
  outline-offset: 2px;
  border-radius: 16px;
}

.card.flipped {
  cursor: default;
}

.card.matched .card-inner {
  animation: matchPop 0.55s ease forwards;
  opacity: 0;
  pointer-events: none;
}

.card.shake {
  animation: shake 0.4s ease;
}

/* ---------- Footer ---------- */
.footer {
  text-align: center;
  color: rgba(255, 255, 255, 0.78);
  font-size: 0.78rem;
  margin-top: auto;
  padding-top: 10px;
}

/* ---------- Win overlay ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.55);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  padding: 18px;
  animation: fadeIn 0.25s ease both;
}

.overlay[hidden] {
  display: none;
}

.modal {
  width: min(92vw, 400px);
  background: #fff;
  border-radius: 24px;
  padding: 26px 22px;
  text-align: center;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
  animation: popIn 0.35s cubic-bezier(0.34, 1.4, 0.64, 1) both;
}

.modal-stars {
  font-size: 2.4rem;
  letter-spacing: 6px;
  line-height: 1;
}

.star {
  color: #cbd5e1;
}

.star.lit {
  color: #f59e0b;
  filter: drop-shadow(0 2px 5px rgba(245, 158, 11, 0.6));
}

.modal h2 {
  margin: 8px 0 2px;
  font-size: 1.5rem;
  color: #0f172a;
}

.modal-subtitle {
  color: #64748b;
  font-size: 0.9rem;
  margin-bottom: 14px;
}

.modal-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 16px 0 20px;
}

.modal-stats div {
  background: #f1f5f9;
  border-radius: 12px;
  padding: 8px 4px;
}

.modal-stats span {
  display: block;
  font-weight: 800;
  font-size: 1rem;
  color: #0f172a;
  font-variant-numeric: tabular-nums;
}

.modal-stats label {
  font-size: 0.62rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #64748b;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn {
  padding: 13px 16px;
  border: none;
  border-radius: 14px;
  font-weight: 700;
  font-size: 0.98rem;
  font-family: inherit;
  cursor: pointer;
  background: #f1f5f9;
  color: #334155;
  transition: transform 0.12s ease, box-shadow 0.2s ease, background 0.2s ease;
}

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

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
}

.btn-primary:hover {
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.28);
}

.btn-ghost {
  background: transparent;
  color: #64748b;
}

/* ---------- Effects canvas ---------- */
.fx-canvas {
  position: fixed;
  inset: 0;
  z-index: 60;
  pointer-events: none;
}

/* ---------- No JS ---------- */
.noscript {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
  background: #f8fafc;
  color: #334155;
  font-weight: 600;
}

/* ---------- Animations ---------- */
@keyframes deal {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.85);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@keyframes matchPop {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  30% {
    transform: scale(1.14);
    opacity: 1;
  }
  100% {
    transform: scale(0.8) rotateZ(8deg);
    opacity: 0;
  }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@keyframes popIn {
  from {
    transform: scale(0.8);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ---------- Small screens ---------- */
@media (max-width: 430px) {
  .stats {
    gap: 4px;
    padding: 8px 4px;
  }

  .stat-value {
    font-size: 1.05rem;
  }

  .brand h1 {
    font-size: 1.25rem;
  }

  .brand-logo {
    width: 46px;
    height: 46px;
  }

  .chip.op {
    min-width: 44px;
    height: 40px;
    font-size: 1.2rem;
  }
}
