:root {
  --primary: #6c4fd6;
  --primary-dark: #4f36a8;
  --black: #171325;
  --cream: #f7f4fe;
  --panel: #ffffff;
  --border: #e3ddf7;
  --text: #241d3a;
  --muted: #8074a3;
  --green: #3fa34d;
  --red: #d9483f;
  --gold: #e0a83f;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: linear-gradient(180deg, #efe9fb, #e4dbf7 400px, #d8ccf0);
  color: var(--text);
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 20px;
  background: var(--black);
  color: white;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; }
.logo {
  width: 52px; height: 52px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 1.8rem; background: var(--primary); box-shadow: 0 0 0 3px var(--gold);
}
.brand-text h1 { margin: 0; font-size: 1.3rem; letter-spacing: 0.5px; }
.tagline { font-size: 0.75rem; color: var(--gold); text-transform: uppercase; letter-spacing: 1px; }

.stats-strip { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; min-width: 90px; }
.stat-label { font-size: 0.7rem; text-transform: uppercase; color: #cbc3e8; opacity: 0.8; }
.stat-value { font-size: 1.2rem; font-weight: 700; color: var(--gold); }

.bonus-btn, .sound-btn {
  background: #2c2542; border: 1px solid #4a3f6e; color: white; padding: 8px 12px; border-radius: 8px;
  cursor: pointer; font-size: 0.85rem; font-weight: 600;
}
.bonus-btn:hover, .sound-btn:hover { background: #3a3157; }
.bonus-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 10px 16px 0;
  flex-wrap: wrap;
}
.tab-btn {
  border: none;
  background: #d9cdf3;
  color: var(--text);
  padding: 10px 18px;
  border-radius: 10px 10px 0 0;
  font-size: 0.95rem;
  cursor: pointer;
  font-weight: 600;
  opacity: 0.7;
}
.tab-btn.active { background: var(--panel); opacity: 1; box-shadow: 0 -2px 6px rgba(0,0,0,0.05); }
.tab-btn:hover { opacity: 1; }

main { padding: 20px; max-width: 900px; margin: 0 auto; }

.tab-panel { display: none; background: var(--panel); border-radius: 0 12px 12px 12px; padding: 20px; box-shadow: 0 2px 10px rgba(0,0,0,0.06); }
.tab-panel.active { display: block; }

h2 { margin-top: 0; color: var(--black); }
h3 { color: var(--black); margin-bottom: 8px; }
.shop-intro { color: var(--muted); margin-top: -8px; }

/* ---------- Play tab ---------- */
.level-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.level-title { font-size: 1.1rem; font-weight: 700; display: flex; align-items: center; gap: 8px; }
.level-num { font-weight: 500; color: var(--muted); font-size: 0.85rem; }
.play-hud { display: flex; gap: 14px; flex-wrap: wrap; }
.hud-item { background: #f1ecfb; padding: 6px 12px; border-radius: 20px; font-size: 0.85rem; font-weight: 700; }
.hud-item.warn { background: #fde3df; color: var(--red); }

.board-wrap { position: relative; }
.board-grid { display: grid; gap: 8px; max-width: 640px; margin: 0 auto; }

.mem-card {
  aspect-ratio: 1; border-radius: 10px; display: flex; align-items: center; justify-content: center;
  font-size: clamp(1rem, 4.5vw, 2rem); cursor: pointer; user-select: none;
  transition: transform 0.12s; color: white; position: relative;
}
.board-grid.cb-default .mem-card { background: linear-gradient(135deg, var(--primary), #9a7ff0); }
.board-grid.cb-gold .mem-card { background: linear-gradient(135deg, #e0a83f, #f5d485); }
.board-grid.cb-space .mem-card { background: linear-gradient(135deg, #1b1440, #4a2e8f); }
.board-grid.cb-treasure .mem-card { background: linear-gradient(135deg, #7a4b1e, #d8a84e); }
.mem-card:active { transform: scale(0.94); }
.mem-card:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.mem-card.flipped, .mem-card.matched, .mem-card.peeking {
  background: var(--panel); color: var(--text); border: 2px solid var(--border);
}
.mem-card.matched { opacity: 0.5; cursor: default; }
.mem-card.wrong { animation: shake 0.4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.prep-overlay, .level-overlay {
  position: absolute; inset: 0; background: rgba(20, 15, 40, 0.82);
  display: flex; align-items: center; justify-content: center; border-radius: 12px;
}
.prep-overlay.hidden, .level-overlay.hidden { display: none; }
.prep-inner { text-align: center; color: white; }
.prep-inner p { font-size: 1.3rem; font-weight: 700; margin: 0 0 10px; }
.prep-timer { font-size: 3rem; font-weight: 800; color: var(--gold); }

.overlay-inner {
  background: var(--cream); border-radius: 14px; padding: 26px 32px; text-align: center;
  border: 3px solid var(--gold); animation: pop 0.25s ease-out; max-width: 90%;
}
@keyframes pop { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.overlay-inner h2 { margin: 0 0 10px; }
.stars-display { font-size: 2.2rem; margin: 6px 0; letter-spacing: 4px; }
.overlay-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }

.primary-btn {
  background: var(--primary); border: none; color: white; padding: 12px 24px; font-size: 1rem;
  border-radius: 10px; font-weight: 700; cursor: pointer; box-shadow: 0 3px 0 var(--primary-dark);
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--primary-dark); }
.primary-btn:disabled { background: #cabfe6; box-shadow: none; cursor: not-allowed; }

.secondary-btn {
  background: #e8e0f9; border: none; color: var(--text); padding: 12px 20px; font-size: 0.9rem;
  border-radius: 10px; font-weight: 700; cursor: pointer;
}
.secondary-btn:hover { background: #ddd1f5; }

.hint-bar { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 16px; }
.hint-btn {
  background: var(--panel); border: 2px solid var(--border); padding: 8px 14px; border-radius: 10px;
  cursor: pointer; font-weight: 600; font-size: 0.85rem; display: flex; flex-direction: column; align-items: flex-start; gap: 2px;
}
.hint-btn:hover { border-color: var(--primary); }
.hint-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.hint-btn .hint-cost { font-size: 0.75rem; color: var(--gold); font-weight: 800; }

.event-log { margin-top: 14px; max-height: 110px; overflow-y: auto; font-size: 0.85rem; color: var(--muted); border-top: 1px dashed var(--border); padding-top: 8px; }
.event-log div { padding: 2px 0; }

/* ---------- Levels tab ---------- */
.world-block { margin-bottom: 22px; }
.world-header { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 1.05rem; margin-bottom: 8px; }
.world-header .locked-tag { font-size: 0.75rem; color: var(--red); font-weight: 700; background: #fde3df; padding: 2px 8px; border-radius: 10px; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 10px; }
.level-btn {
  border: 2px solid var(--border); border-radius: 12px; padding: 10px; background: var(--panel);
  cursor: pointer; text-align: center; font-weight: 700;
}
.level-btn:hover { border-color: var(--primary); }
.level-btn.locked { opacity: 0.45; cursor: not-allowed; filter: grayscale(1); }
.level-btn .lv-name { display: block; font-size: 0.75rem; font-weight: 500; color: var(--muted); margin: 2px 0 4px; min-height: 2em; }
.level-btn .lv-stars { font-size: 0.9rem; letter-spacing: 2px; color: var(--gold); }
.level-btn.current { border-color: var(--primary); box-shadow: 0 0 0 2px rgba(108,79,214,0.25); }

/* ---------- Shop ---------- */
.shop-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin-bottom: 22px; }
.shop-card { border: 2px solid var(--border); border-radius: 12px; padding: 14px; }
.shop-card h3 { margin: 0 0 6px; font-size: 1rem; display: flex; align-items: center; gap: 6px; }
.shop-card .tier-name { font-weight: 700; color: var(--black); }
.shop-card .tier-current { font-size: 0.8rem; color: var(--muted); margin-bottom: 8px; }
.shop-card button {
  width: 100%; padding: 9px; border: none; border-radius: 8px; background: var(--primary); color: white;
  font-weight: 700; cursor: pointer; margin-top: 6px;
}
.shop-card button.active-btn { background: var(--green); cursor: default; }
.shop-card button:disabled { background: #d8d0e8; color: #8a8370; cursor: not-allowed; }
.shop-card .maxed { color: var(--green); font-weight: 700; font-size: 0.85rem; }

/* ---------- Achievements ---------- */
.dex-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; }
.dex-card { border: 2px solid var(--border); border-radius: 10px; padding: 12px; text-align: center; }
.dex-card.locked { opacity: 0.5; filter: grayscale(0.6); }
.dex-emoji { font-size: 2rem; }
.dex-name { font-weight: 700; font-size: 0.85rem; margin-top: 4px; }
.dex-desc { font-size: 0.72rem; color: var(--muted); margin-top: 2px; min-height: 2.4em; }
.dex-reward { font-size: 0.72rem; color: var(--gold); font-weight: 800; margin-top: 4px; }
.dex-progress { font-size: 0.7rem; color: var(--muted); margin-top: 4px; }
.dex-unlocked-badge { font-size: 0.7rem; color: var(--green); font-weight: 800; margin-top: 4px; }

/* ---------- Stats ---------- */
.stats-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 20px; }
.stats-grid .stat-box { background: #f1ecfb; border-radius: 10px; padding: 14px; }
.stats-grid .stat-box .num { font-size: 1.3rem; font-weight: 800; color: var(--primary-dark); }
.stats-grid .stat-box .lbl { font-size: 0.8rem; color: var(--muted); }

.danger-btn { background: var(--red); border: none; color: white; padding: 10px 18px; border-radius: 8px; font-weight: 700; cursor: pointer; }

@media (max-width: 600px) {
  .stats-strip { width: 100%; justify-content: space-between; }
  main { padding: 10px; }
  .overlay-inner { padding: 20px; }
}
