:root {
  --bg: #f4f5fb;
  --ink: #1b1f3b;
  --muted: #5b6080;
  --primary: #4f46e5;
  --primary-dark: #3730a3;
  --ok: #15803d;
  --ok-bg: #dcfce7;
  --card-back: #ffffff;
  --out-bg: #fef9c3;
  --border: #d5d8ee;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar { background: var(--ink); color: #fff; }
.topbar-inner {
  max-width: 920px; margin: 0 auto; padding: .7rem 1rem;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}
.brand { font-weight: 800; }
.user-area { display: flex; align-items: center; gap: .6rem; }
.user-area img { width: 36px; height: 36px; border-radius: 50%; border: 2px solid #fff; }
.user-area .name { font-weight: 700; }
.user-area button {
  background: transparent; color: #fff; border: 1px solid #ffffff88;
  border-radius: 8px; padding: .3rem .7rem; cursor: pointer;
}

main { flex: 1; width: 100%; max-width: 920px; margin: 0 auto; padding: 1.2rem 1rem; }
.panel { background: #fff; border: 1px solid var(--border); border-radius: 14px; padding: 2rem 1.2rem; }
.center { text-align: center; }
h1 { margin-top: 0; font-size: 1.5rem; }

.btn {
  display: inline-block; background: var(--primary); color: #fff; border: 0;
  border-radius: 10px; padding: .65rem 1.1rem; font-weight: 700; font-size: 1rem;
  text-decoration: none; cursor: pointer;
}
.btn:hover { background: var(--primary-dark); }
.btn:disabled { opacity: .6; cursor: wait; }

.stats { display: flex; flex-wrap: wrap; gap: .8rem; align-items: stretch; margin-bottom: .6rem; }
.stat {
  background: #fff; border: 1px solid var(--border); border-radius: 12px;
  padding: .5rem 1rem; display: flex; flex-direction: column; min-width: 110px;
}
.stat-label { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 1.4rem; font-weight: 800; }
.stats .btn { margin-left: auto; align-self: center; }

.message { min-height: 1.5rem; font-weight: 700; margin: .4rem 0 .8rem; }
.message.ok { color: var(--ok); }
.message.err { color: #b91c1c; }

.board { display: grid; grid-template-columns: repeat(4, 1fr); gap: .8rem; }

.card {
  display: block; width: 100%; aspect-ratio: 5 / 4; padding: 0; border: 0;
  background: none; perspective: 900px; cursor: pointer; font: inherit; color: inherit;
}
.card:focus-visible { outline: 3px solid var(--primary); outline-offset: 3px; border-radius: 12px; }
.card-inner {
  position: relative; width: 100%; height: 100%;
  transform-style: preserve-3d; transition: transform .55s;
}
.card.flipped .card-inner { transform: rotateY(180deg); }

.face {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  border-radius: 12px; padding: .5rem; text-align: center;
  backface-visibility: hidden; -webkit-backface-visibility: hidden;
}
.front {
  background: linear-gradient(135deg, var(--primary), #7c3aed);
  color: #fff; font-size: 2rem; font-weight: 800;
}
.back {
  transform: rotateY(180deg); background: var(--card-back);
  border: 2px solid var(--border); overflow: hidden;
  overflow-wrap: anywhere; font-size: .8rem;
}
.back[data-kind="code"] { font-family: ui-monospace, Consolas, monospace; }
.back[data-kind="output"] { background: var(--out-bg); font-weight: 800; font-size: 1rem; }
.card.matched .back { border-color: var(--ok); background: var(--ok-bg); }

footer {
  text-align: center; padding: 1rem; font-size: .85rem; color: var(--muted);
  border-top: 1px solid var(--border); background: #fff;
}

@media (max-width: 700px) { .board { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 440px) { .board { grid-template-columns: repeat(2, 1fr); } }
@media (prefers-reduced-motion: reduce) { .card-inner { transition: none; } }