/* Memory Cards — Game CMB
   Premium casual game visual style: dark background, neon accents,
   glassmorphism panels, white playing cards. System font stack only —
   no external web font (avoids GDPR pre-consent IP transmission and
   keeps first paint fast). */

:root {
  --bg-0: #090c16;
  --bg-1: #0f1428;
  --bg-2: #161d3a;
  --panel: rgba(255, 255, 255, 0.045);
  --panel-border: rgba(255, 255, 255, 0.09);
  --panel-strong: rgba(255, 255, 255, 0.08);
  --text-0: #f4f6fb;
  --text-1: #aeb6d4;
  --text-2: #767fa3;
  --accent-cyan: #3aa0ff;
  --accent-violet: #b06bff;
  --accent-gold: #ffd23d;
  --accent-green: #3ddc84;
  --accent-red: #ff4d6d;
  --card-face: #f7f5ef;
  --card-suit-red: #d81f3c;
  --card-suit-black: #17172a;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 9px;
  --shadow-lg: 0 20px 60px rgba(0, 0, 0, 0.45);
  --safe-touch: 44px;
  font-size: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg-0);
  color: var(--text-0);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background:
    radial-gradient(ellipse 80% 60% at 20% -10%, rgba(58, 160, 255, 0.16), transparent 60%),
    radial-gradient(ellipse 70% 50% at 100% 0%, rgba(176, 107, 255, 0.14), transparent 60%),
    linear-gradient(180deg, var(--bg-0), var(--bg-1) 40%, var(--bg-2));
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

a { color: var(--accent-cyan); }

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  color: var(--text-0);
}
button:disabled { opacity: 0.5; cursor: default; }

img { max-width: 100%; }

.container {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 16px 32px;
}

/* --- topbar --------------------------------------------------------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  max-width: 720px;
  margin: 0 auto;
  padding: 14px 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  letter-spacing: 0.3px;
  font-size: 1.05rem;
}
.brand-mark {
  width: 30px; height: 30px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent-cyan), var(--accent-violet));
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem;
  box-shadow: 0 0 18px rgba(58, 160, 255, 0.5);
}

.icon-btn {
  width: var(--safe-touch);
  height: var(--safe-touch);
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  font-size: 1.15rem;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--panel-strong); }

/* --- panel/glass ------------------------------------------------------ */

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  box-shadow: var(--shadow-lg);
}

/* --- home screen ----------------------------------------------------- */

.screen { width: 100%; }

.home-hero {
  text-align: center;
  padding: 28px 20px 22px;
  margin: 8px auto 18px;
}

.home-title {
  font-size: clamp(2rem, 8vw, 2.7rem);
  font-weight: 900;
  letter-spacing: 0.5px;
  margin: 0 0 6px;
  background: linear-gradient(135deg, #fff, var(--accent-cyan) 60%, var(--accent-violet));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.home-subtitle {
  color: var(--text-1);
  margin: 0 0 18px;
  font-size: 0.98rem;
}

.streak-badge {
  display: inline-block;
  background: linear-gradient(135deg, rgba(255, 210, 61, 0.18), rgba(255, 77, 109, 0.14));
  border: 1px solid rgba(255, 210, 61, 0.35);
  color: var(--accent-gold);
  font-weight: 700;
  font-size: 0.82rem;
  padding: 5px 12px;
  border-radius: 999px;
  margin-bottom: 14px;
}

.difficulty-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin: 0 0 18px;
}

.difficulty-btn {
  background: var(--panel);
  border: 1.5px solid var(--panel-border);
  border-radius: var(--radius-md);
  padding: 14px 10px;
  min-height: 64px;
  text-align: center;
  transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.difficulty-btn .diff-name {
  display: block;
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: 0.5px;
}
.difficulty-btn .diff-meta {
  display: block;
  font-size: 0.75rem;
  color: var(--text-1);
  margin-top: 3px;
}
.difficulty-btn:active { transform: scale(0.97); }
.difficulty-btn.active {
  border-color: var(--accent-cyan);
  background: rgba(58, 160, 255, 0.12);
  box-shadow: 0 0 0 1px rgba(58, 160, 255, 0.3) inset, 0 0 24px rgba(58, 160, 255, 0.18);
}

.btn-primary {
  display: block;
  width: 100%;
  min-height: 56px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--accent-cyan), var(--accent-violet));
  color: #06070d;
  font-weight: 900;
  font-size: 1.08rem;
  letter-spacing: 0.6px;
  box-shadow: 0 10px 30px rgba(58, 160, 255, 0.35);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.btn-primary:active { transform: scale(0.98); }

.btn-secondary {
  display: block;
  width: 100%;
  min-height: 50px;
  border-radius: var(--radius-md);
  background: var(--panel);
  border: 1.5px solid var(--panel-border);
  color: var(--text-0);
  font-weight: 700;
  font-size: 0.95rem;
  margin-top: 10px;
}
.btn-secondary:active { transform: scale(0.98); }

.home-stats {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin: 16px 0 0;
}
.stat-chip {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 108px;
}
.stat-chip-label { font-size: 0.62rem; color: var(--text-2); letter-spacing: 0.6px; }
.stat-chip-value { font-weight: 800; font-size: 0.95rem; }
.stat-chip-muted { color: var(--text-1); font-size: 0.82rem; padding: 8px 16px; }

/* --- SEO content below the fold --------------------------------------- */

.seo-section {
  padding: 22px 20px;
  margin: 14px auto;
}
.seo-section h2 {
  font-size: 1.15rem;
  margin: 0 0 10px;
}
.seo-section p, .seo-section li {
  color: var(--text-1);
  line-height: 1.65;
  font-size: 0.92rem;
}
.seo-section ol, .seo-section ul { padding-left: 20px; }

.faq-item { border-top: 1px solid var(--panel-border); padding: 12px 0; }
.faq-item:first-of-type { border-top: none; padding-top: 0; }
.faq-item summary {
  font-weight: 700;
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.faq-item summary::-webkit-details-marker { display: none; }
.faq-item summary::after { content: '+'; font-size: 1.2rem; color: var(--accent-cyan); }
.faq-item[open] summary::after { content: '−'; }
.faq-item p { margin: 8px 0 0; }

.related-games { display: flex; flex-wrap: wrap; gap: 8px; }
.related-games a {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.85rem;
  text-decoration: none;
  color: var(--text-0);
}

.ad-slot {
  min-height: 90px;
  border-radius: var(--radius-md);
  border: 1px dashed var(--panel-border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-2);
  font-size: 0.75rem;
  margin: 16px auto;
  max-width: 720px;
}

/* --- game screen ------------------------------------------------------ */

#screen-game { position: relative; padding-top: 4px; }

.game-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px 10px;
}

.game-difficulty-pill {
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.6px;
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--accent-cyan);
}

.hud-row {
  display: grid;
  /* SCORE (last cell) gets extra width — its value tends to run longer
     (4-6 digits with a comma) than TIME/MOVES/PAIRS/COMBO, which merged
     into this row from what used to be their own full-width row below
     it. */
  grid-template-columns: repeat(4, 1fr) 1.3fr;
  gap: 6px;
  margin-bottom: 12px;
}
.hud-cell {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 8px 4px;
  text-align: center;
}
.hud-label { display: block; font-size: 0.6rem; color: var(--text-2); letter-spacing: 0.5px; }
/* clamp() instead of a fixed 1rem: with 5 cells now sharing one row
   (score used to have a full-width row to itself), the narrowest phones
   need the value to shrink a little rather than overflow/wrap. */
.hud-value { display: block; font-weight: 800; font-size: clamp(0.78rem, 3.6vw, 1rem); font-variant-numeric: tabular-nums; }
.hud-cell-score { border-color: rgba(255, 210, 61, 0.35); }
.hud-cell-score .hud-value { color: var(--accent-gold); }
#hud-combo.pulse { animation: comboPulse 0.4s ease; }

@keyframes comboPulse {
  0% { transform: scale(1); color: var(--text-0); }
  50% { transform: scale(1.25); color: var(--accent-gold); }
  100% { transform: scale(1); }
}

.board-wrap {
  position: relative;
  padding: 4px;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), 1fr);
  gap: 7px;
  perspective: 800px;
}

.card {
  background: none;
  display: block;
  position: relative;
  width: 100%;
  /* render.js::_syncCardHeights() sets an explicit inline `height` in px
     on every single `.card` element directly (not via a shared CSS
     custom property — see below for why that changed). The padding-
     bottom here is only the same-ratio fallback for the brief instant
     before JS runs (138.9% = 1/0.72); an inline style always wins over
     it once set. */
  padding: 0 0 138.9% 0;
  border-radius: var(--radius-sm);
  touch-action: manipulation;
}

.card-inner {
  /* Absolutely positioned against .card's PADDING box (inset: 0), not a
     percentage width/height against its content box — the latter would
     resolve to 0 given .card's own content height is 0 by design (see
     the comment on .card above). This is the other half of the
     percentage-padding aspect-ratio trick: it only works end-to-end if
     every layer inside the ratio box is offset-positioned like this
     rather than sized with percentage height. */
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 300ms cubic-bezier(0.4, 0.2, 0.2, 1);
  /* CONFIRMED (not just theorized) as a paint/compositor bug, not a
     layout bug: a diagnostic page had the user measure a flipped card
     with getBoundingClientRect() — it reported the CORRECT height,
     matching its face-down neighbors exactly — while the same card
     still visibly rendered short on screen at that exact moment. The
     browser's own layout math was right; what actually got painted to
     the screen wasn't. `will-change: transform` alone (a *hint* the
     browser is still free to ignore/apply lazily) turned out not to be
     enough on the browser that showed this (Chrome 153 / Windows). Now
     forcing an always-on compositing layer via a permanent
     `translateZ(0)` baseline instead of only hinting at one — every
     transform value below (flip, match-pop, mismatch-shake) keeps that
     translateZ(0) in the same `transform` value so the layer is never
     torn down and recreated right as the visible transform changes,
     which is the exact moment this bug occurs. */
  will-change: transform;
  transform: translateZ(0);
}
.card.is-flipped .card-inner { transform: translateZ(0) rotateY(180deg); }
.card.is-matched .card-inner { animation: matchPop 400ms ease; }
.card.is-mismatch .card-inner { animation: mismatchShake 400ms ease; }

@keyframes matchPop {
  0% { transform: translateZ(0) rotateY(180deg) scale(1); }
  40% { transform: translateZ(0) rotateY(180deg) scale(1.08); filter: drop-shadow(0 0 14px rgba(61, 220, 132, 0.7)); }
  100% { transform: translateZ(0) rotateY(180deg) scale(1); }
}
@keyframes mismatchShake {
  0%, 100% { transform: translateZ(0) rotateY(180deg) translateX(0); }
  20% { transform: translateZ(0) rotateY(180deg) translateX(-5px); }
  40% { transform: translateZ(0) rotateY(180deg) translateX(5px); }
  60% { transform: translateZ(0) rotateY(180deg) translateX(-4px); }
  80% { transform: translateZ(0) rotateY(180deg) translateX(4px); }
}

.card-face {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-sm);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-back {
  background:
    linear-gradient(145deg, rgba(58, 160, 255, 0.14), rgba(176, 107, 255, 0.14)),
    linear-gradient(160deg, #131a30, #0a0e1c);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 0 0 1px rgba(58, 160, 255, 0.08) inset, 0 6px 16px rgba(0, 0, 0, 0.4);
  flex-direction: column;
  gap: 2px;
}
.card-back-sparkle { color: var(--accent-cyan); font-size: 0.85em; opacity: 0.85; }
.card-back-brand {
  font-size: 0.52em;
  font-weight: 800;
  letter-spacing: 1px;
  color: var(--text-1);
}

.card-front {
  background: var(--card-face);
  transform: rotateY(180deg);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
  position: relative;
  padding: 6%;
  height: 100%;
}
.card-front.suit-red .card-rank,
.card-front.suit-red .card-suit-big { color: var(--card-suit-red); }
.card-front.suit-black .card-rank,
.card-front.suit-black .card-suit-big { color: var(--card-suit-black); }

.card-rank {
  position: absolute;
  font-weight: 800;
  line-height: 1;
  font-size: clamp(0.55rem, 2.6vw, 0.85rem);
  display: flex;
  flex-direction: column;
  align-items: center;
}
.card-rank-tl { top: 6%; left: 8%; }
.card-rank-br { bottom: 6%; right: 8%; transform: rotate(180deg); }
.card-suit-mini { font-size: 0.9em; }
.card-suit-big { font-size: clamp(1.1rem, 7vw, 2.1rem); }

/* preview phase: reveal every card without the flip animation delay */
.card.is-preview .card-inner { transition: none; }

.preview-banner {
  position: absolute;
  top: 8px; left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  background: rgba(9, 12, 22, 0.85);
  border: 1px solid var(--accent-gold);
  color: var(--accent-gold);
  font-weight: 800;
  letter-spacing: 1px;
  padding: 6px 16px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  gap: 8px;
  box-shadow: 0 0 20px rgba(255, 210, 61, 0.3);
}
.preview-timer-track {
  width: 46px; height: 4px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  overflow: hidden;
}
.preview-timer-fill { height: 100%; width: 100%; background: var(--accent-gold); }

.countdown-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 8, 16, 0.82);
  backdrop-filter: blur(6px);
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
}
.countdown-value {
  font-size: 5rem;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 0 40px rgba(58, 160, 255, 0.8);
}
.countdown-value.pop { animation: countPop 0.5s ease; }
@keyframes countPop {
  0% { transform: scale(0.4); opacity: 0; }
  60% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}

.combo-toast {
  position: fixed;
  top: 22%;
  left: 50%;
  transform: translate(-50%, -10px) scale(0.85);
  opacity: 0;
  z-index: 30;
  font-weight: 900;
  font-size: 1.5rem;
  letter-spacing: 1px;
  color: var(--accent-gold);
  text-shadow: 0 0 24px rgba(255, 210, 61, 0.65);
  pointer-events: none;
}
.combo-toast.show { animation: comboToast 900ms ease forwards; }
@keyframes comboToast {
  0% { transform: translate(-50%, 10px) scale(0.7); opacity: 0; }
  20% { transform: translate(-50%, -10px) scale(1.1); opacity: 1; }
  35% { transform: translate(-50%, -14px) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -34px) scale(1); opacity: 0; }
}

.particle-canvas {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 6;
}

.pause-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 8, 16, 0.9);
  z-index: 35;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  text-align: center;
  padding: 20px;
}
.pause-overlay h2 { font-size: 1.6rem; margin: 0; }
.pause-overlay .btn-primary { max-width: 260px; }

/* --- result screen ------------------------------------------------- */

.result-panel {
  padding: 28px 22px;
  text-align: center;
  margin-top: 12px;
}
.result-emoji { font-size: 2.8rem; display: block; margin-bottom: 4px; }
.result-title { font-size: 1.7rem; font-weight: 900; margin: 0 0 4px; }
.result-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 18px 0;
}
.result-stat-value { font-weight: 900; font-size: 1.15rem; display: block; }
.result-stat-label { font-size: 0.62rem; color: var(--text-2); letter-spacing: 0.5px; }

.perfect-banner, .new-best-banner {
  display: inline-block;
  font-weight: 800;
  font-size: 0.85rem;
  padding: 6px 14px;
  border-radius: 999px;
  margin: 4px 4px 10px;
}
.perfect-banner {
  background: rgba(255, 77, 109, 0.16);
  border: 1px solid rgba(255, 77, 109, 0.4);
  color: var(--accent-red);
}
.new-best-banner {
  background: rgba(255, 210, 61, 0.16);
  border: 1px solid rgba(255, 210, 61, 0.4);
  color: var(--accent-gold);
}

.rank-badge {
  display: block;
  margin: 8px auto 4px;
  font-weight: 800;
  color: var(--accent-cyan);
}

.nickname-section { margin: 16px 0; text-align: left; }
.nickname-section label { display: block; font-size: 0.78rem; color: var(--text-1); margin-bottom: 6px; }
.nickname-row { display: flex; gap: 8px; }
.nickname-input {
  flex: 1;
  min-height: var(--safe-touch);
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-0);
  padding: 0 12px;
  font-size: 16px; /* prevents iOS auto-zoom on focus */
}
.nickname-submit-btn {
  min-width: 92px;
  min-height: var(--safe-touch);
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent-cyan), var(--accent-violet));
  color: #06070d;
  font-weight: 800;
}
.field-error { color: var(--accent-red); font-size: 0.8rem; margin-top: 6px; }
.field-success { color: var(--accent-green); font-size: 0.85rem; font-weight: 700; margin-top: 6px; }

.result-actions { margin-top: 8px; }
.result-actions-row { display: flex; gap: 8px; margin-top: 10px; }
.result-actions-row .btn-secondary { margin-top: 0; }

/* --- leaderboard modal ------------------------------------------------ */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 12, 0.72);
  backdrop-filter: blur(4px);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
@media (min-width: 560px) {
  .modal { align-items: center; }
}

.modal-content {
  width: 100%;
  max-width: 480px;
  max-height: 84vh;
  overflow-y: auto;
  border-radius: 22px 22px 0 0;
  padding: 18px 18px 26px;
}
@media (min-width: 560px) {
  .modal-content { border-radius: var(--radius-lg); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.modal-header h2 { margin: 0; font-size: 1.2rem; }

.tab-row { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 10px; }
.tab-btn {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-1);
  white-space: nowrap;
}
.tab-btn.active { color: #06070d; background: linear-gradient(135deg, var(--accent-cyan), var(--accent-violet)); }

.leaderboard-list { display: flex; flex-direction: column; gap: 6px; }
.leaderboard-row {
  display: grid;
  grid-template-columns: 40px 1fr auto auto;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 0.85rem;
}
.leaderboard-row.medal-1 { border-color: rgba(255, 210, 61, 0.5); background: rgba(255, 210, 61, 0.08); }
.leaderboard-row.medal-2 { border-color: rgba(200, 210, 230, 0.4); }
.leaderboard-row.medal-3 { border-color: rgba(205, 145, 90, 0.4); }
.lb-rank { font-weight: 800; color: var(--text-1); }
.lb-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-score { font-weight: 800; color: var(--accent-gold); }
.lb-time { color: var(--text-1); font-size: 0.78rem; }
.leaderboard-empty { text-align: center; color: var(--text-1); padding: 26px 10px; }

/* --- footer ------------------------------------------------------- */

.site-footer {
  text-align: center;
  padding: 20px;
  color: var(--text-2);
  font-size: 0.78rem;
}

/* --- responsive: small phones ---------------------------------------- */

@media (max-width: 380px) {
  .board { gap: 5px; }
  .difficulty-grid { gap: 8px; }
  .result-stats { gap: 6px; }
  .hud-row { gap: 4px; }
  .hud-cell { padding: 7px 2px; }
}

@media (min-width: 640px) {
  .home-hero { padding: 40px 20px 30px; }
}

/* --- reduced motion ---------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .card-inner,
  .card.is-matched .card-inner,
  .card.is-mismatch .card-inner,
  .countdown-value.pop,
  .combo-toast.show,
  #hud-combo.pulse {
    animation: none !important;
    transition-duration: 60ms !important;
  }
}

/* --- focus visibility (accessibility) --------------------------------- */

button:focus-visible,
.card:focus-visible,
.nickname-input:focus-visible {
  outline: 2px solid var(--accent-cyan);
  outline-offset: 2px;
}
