:root {
  --bg: #0f1226;
  --bg2: #171a35;
  --card-bg: #2a2f55;
  --card-face: #3a4078;
  --accent: #6c7bff;
  --accent2: #00d4a0;
  --text: #eef0ff;
  --muted: #9aa0c8;
  --gap: 10px;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

/* The hidden attribute must always win over any display rule below
   (e.g. .modal-backdrop / .unlock-box use display:flex). Without this,
   "hidden" modals stay visible and cover the board, eating all clicks. */
[hidden] { display: none !important; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(1200px 600px at 50% -10%, #1d2150 0%, var(--bg) 55%);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top) 14px 14px;
}

.topbar {
  width: 100%;
  max-width: 520px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 4px 8px;
}
.logo {
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: .5px;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(255,255,255,.05);
  color: var(--text);
  font-size: 1.05rem;
  cursor: pointer;
  transition: background .15s, transform .1s;
}
.icon-btn:hover { background: rgba(255,255,255,.12); }
.icon-btn:active { transform: scale(.92); }

main { width: 100%; max-width: 520px; display: flex; flex-direction: column; align-items: center; }

.subhead { color: var(--muted); font-size: .85rem; margin: 2px 0 14px; }
.subhead .dot { margin: 0 6px; }

.diffbar {
  display: flex;
  gap: 6px;
  width: 100%;
  margin-bottom: 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.diffbar::-webkit-scrollbar { display: none; }
.diff-chip {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 7px 10px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,.1);
  background: rgba(255,255,255,.05);
  color: var(--muted);
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, color .15s, border-color .15s, transform .1s;
}
.diff-chip small { font-size: .62rem; font-weight: 500; opacity: .8; }
.diff-chip:hover { background: rgba(255,255,255,.1); color: var(--text); }
.diff-chip:active { transform: scale(.95); }
.diff-chip.active {
  background: linear-gradient(145deg, var(--accent), #8a78ff);
  color: #fff;
  border-color: transparent;
}
.diff-chip.locked { opacity: .6; }
.diff-chip.locked.active { opacity: 1; }

.have-code-link {
  display: block;
  margin: 12px auto 0;
  background: none;
  border: none;
  color: var(--muted);
  font-size: .82rem;
  text-decoration: underline;
  cursor: pointer;
}
.have-code-link:hover { color: var(--text); }

.hud {
  display: flex;
  gap: 10px;
  width: 100%;
  margin-bottom: 14px;
}
.hud-item {
  flex: 1;
  background: var(--bg2);
  border: 1px solid rgba(255,255,255,.06);
  border-radius: var(--radius);
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.hud-num { font-size: 1.35rem; font-weight: 700; }
.hud-lbl { font-size: .7rem; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

.board {
  display: grid;
  gap: var(--gap);
  width: 100%;
  margin-bottom: 18px;
}

.cell { perspective: 700px; aspect-ratio: 1 / 1; }
.cell-inner {
  position: relative;
  width: 100%; height: 100%;
  transform-style: preserve-3d;
  transition: transform .35s cubic-bezier(.2,.8,.3,1.2);
  cursor: pointer;
}
.cell.flipped .cell-inner, .cell.matched .cell-inner { transform: rotateY(180deg); }
.cell-front, .cell-back {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  user-select: none;
}
.cell-front {
  background: linear-gradient(145deg, var(--card-bg), #1c2046);
  border: 1px solid rgba(255,255,255,.08);
  font-size: 1.4rem;
  color: rgba(255,255,255,.25);
}
.cell-back {
  transform: rotateY(180deg);
  background: linear-gradient(145deg, var(--card-face), #2b3168);
  border: 1px solid rgba(255,255,255,.12);
  font-size: clamp(1.6rem, 9vw, 2.6rem);
}
.cell-back img { width: 100%; height: 100%; object-fit: cover; }
.cell.matched .cell-inner { box-shadow: 0 0 0 2px var(--accent2) inset; border-radius: var(--radius); }
.cell.matched { animation: pop .4s ease; }
@keyframes pop { 0%{transform:scale(1)} 40%{transform:scale(1.06)} 100%{transform:scale(1)} }

.controls { display: flex; gap: 10px; width: 100%; }
.btn {
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: .95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform .1s, filter .15s, background .15s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(90deg, var(--accent), #8a78ff); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { flex: 1; background: rgba(255,255,255,.06); color: var(--text); border: 1px solid rgba(255,255,255,.1); }
.btn.ghost:hover { background: rgba(255,255,255,.12); }
.btn.buy { background: linear-gradient(90deg, #ffb347, #ff7e5f); color: #2a1500; text-decoration: none; text-align: center; }
.btn.block { display: block; width: 100%; margin-top: 14px; }

.footer { margin-top: 22px; }
.support { color: var(--muted); text-decoration: none; font-size: .9rem; border-bottom: 1px dashed rgba(255,255,255,.2); padding-bottom: 2px; }
.support:hover { color: var(--text); }
.support.inline { display: inline-block; margin-top: 14px; }

/* Modals */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(5,7,20,.7);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  padding: 18px; z-index: 50;
  animation: fade .2s ease;
}
@keyframes fade { from{opacity:0} to{opacity:1} }
.modal {
  position: relative;
  background: var(--bg2);
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 26px 22px 22px;
  width: 100%; max-width: 380px;
  text-align: center;
  animation: rise .25s cubic-bezier(.2,.8,.3,1);
}
@keyframes rise { from{transform:translateY(14px);opacity:0} to{transform:translateY(0);opacity:1} }
.modal h2 { margin-bottom: 12px; font-size: 1.4rem; }
.modal-close { position: absolute; top: 12px; right: 12px; background: none; border: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; }
.muted { color: var(--muted); font-size: .9rem; line-height: 1.5; }
.howto { text-align: left; margin: 8px 0 4px; padding-left: 18px; color: var(--text); }
.howto li { margin-bottom: 9px; line-height: 1.45; }

.result-grid { font-size: 1.6rem; letter-spacing: 4px; margin: 16px 0 6px; line-height: 1.3; }
.result-stats { color: var(--muted); font-size: .95rem; margin-bottom: 6px; }
.result-best { font-size: .92rem; margin: 6px 0 2px; line-height: 1.5; }
.result-best .record { color: var(--accent2); font-weight: 700; }
.result-best .pb { color: var(--text); }

.fx {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 90;
}
.next-puzzle { margin-top: 14px; font-size: .82rem; }

.stats-row { display: flex; justify-content: space-around; margin: 10px 0; }
.stat { display: flex; flex-direction: column; align-items: center; }
.stat b { font-size: 1.6rem; }
.stat span { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }

.theme-list { display: flex; flex-direction: column; gap: 8px; margin: 14px 0; }
.theme-item {
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(255,255,255,.05);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 12px; padding: 10px 14px; cursor: pointer;
}
.theme-item:hover { background: rgba(255,255,255,.1); }
.theme-item.active { border-color: var(--accent2); }
.theme-item .lock { font-size: .85rem; color: var(--muted); }
.theme-item .preview { font-size: 1.1rem; letter-spacing: 2px; }

.unlock-box { display: flex; gap: 8px; margin-top: 8px; }
.unlock-box input {
  flex: 1; border-radius: 10px; border: 1px solid rgba(255,255,255,.15);
  background: rgba(0,0,0,.25); color: var(--text); padding: 10px 12px; font-size: .95rem;
}
.unlock-box input:focus { outline: none; border-color: var(--accent); }
.unlock-msg { font-size: .85rem; margin-top: 8px; min-height: 18px; }
.unlock-msg.ok { color: var(--accent2); }
.unlock-msg.err { color: #ff7a90; }

.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%);
  background: var(--accent2); color: #032; font-weight: 600;
  padding: 10px 18px; border-radius: 999px; box-shadow: var(--shadow);
  z-index: 80; animation: rise .2s ease;
}

@media (max-width: 380px) { :root { --gap: 8px; } .logo { font-size: 1.3rem; } }
