:root {
  --bg: #0b1020;
  --text: #e8eefc;
  --muted: #8fa3c8;
  --accent: #b07bff;
  --accent-dim: #6a3fb0;
  --gold: #ffd24d;
  --panel: rgba(8, 12, 26, 0.94);
  --grid: #2a2f49;
}

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

html, body {
  height: 100%;
  width: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-rounded, "Segoe UI", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  overflow: hidden;
}

.wrap {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}
.wrap:fullscreen, .wrap:-webkit-full-screen { width: 100vw; height: 100vh; }

.bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  gap: 8px;
}
.bar h1 { font-size: 16px; font-weight: 800; letter-spacing: 0.2px; }
.scores { display: flex; align-items: center; gap: 12px; }
.score { font-size: 14px; color: var(--muted); }
.score strong { color: var(--text); margin-left: 2px; }
#hearts { font-size: 14px; letter-spacing: 1px; }

.icon {
  background: transparent;
  border: 1px solid var(--grid);
  border-radius: 8px;
  color: var(--text);
  font-size: 15px;
  width: 34px;
  height: 30px;
  cursor: pointer;
}
.icon:active { background: var(--grid); }
.icon.home { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

.stage {
  flex: 0 0 auto;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
  padding-bottom: 6px;
}
.stage strong { color: var(--accent); }

.board {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  margin: 0 8px 6px;
  border-radius: 14px;
  overflow: hidden;
  border: 2px solid var(--grid);
  background: #0d1426;
}
#game { display: block; width: 100%; height: 100%; touch-action: none; }

/* Overlays ------------------------------------------------------------------ */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: safe center;
  gap: 12px;
  padding: 16px;
  text-align: center;
  background: var(--panel);
  backdrop-filter: blur(3px);
  overflow-y: auto;
  z-index: 5;
}
.overlay.hidden { display: none; }
.overlay.screen { justify-content: flex-start; }

.big-title {
  font-size: 30px; font-weight: 800; color: var(--accent);
  text-shadow: 0 2px 0 rgba(0,0,0,0.5), 0 0 18px rgba(176,123,255,0.5);
}
#overlay-title { font-size: 26px; font-weight: 800; color: var(--accent); text-shadow: 0 2px 0 rgba(0,0,0,0.5); }
.sub, #overlay-sub { font-size: 13px; color: var(--muted); max-width: 460px; line-height: 1.35; }
.pick-hint { font-size: 13px; color: var(--gold); font-weight: 700; }

/* Main menu grid ------------------------------------------------------------ */
.menu-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  width: 100%;
  max-width: 360px;
}
.menu-btn {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  background: rgba(255,255,255,0.05);
  border: 2px solid var(--grid);
  border-radius: 16px;
  padding: 18px 10px;
  cursor: pointer;
  color: var(--text);
  transition: transform .08s, border-color .12s, background .12s;
}
.menu-btn:hover, .menu-btn:active { transform: translateY(-2px); border-color: var(--accent); background: rgba(176,123,255,0.14); }
.mb-emoji { font-size: 34px; }
.mb-label { font-size: 15px; font-weight: 800; }

/* Screen headers ------------------------------------------------------------ */
.screen-head {
  display: flex; align-items: center; gap: 10px; width: 100%; max-width: 560px;
  flex: 0 0 auto;
}
.screen-title { font-size: 22px; font-weight: 800; color: var(--accent); margin: 0 auto; }
.back {
  background: transparent; border: 1px solid var(--grid); border-radius: 8px;
  color: var(--text); font-size: 13px; font-weight: 700; padding: 6px 12px; cursor: pointer;
}
.back:active { background: var(--grid); }

/* Character cards ----------------------------------------------------------- */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  gap: 10px;
  width: 100%;
  max-width: 620px;
  align-content: start;
}
.card {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: rgba(255,255,255,0.05);
  border: 2px solid var(--grid);
  border-radius: 12px;
  padding: 8px 6px;
  cursor: pointer;
  color: var(--text);
  transition: transform .08s, border-color .12s;
}
.card.locked { opacity: 0.4; filter: grayscale(0.8); cursor: default; }
.card.selected { border-color: var(--gold); background: rgba(255,210,77,0.12); transform: translateY(-2px); }
.card.rarity-common { box-shadow: inset 0 0 0 1px rgba(255,255,255,0.06); }
.card.rarity-normal { box-shadow: inset 0 0 0 1px rgba(90,211,106,0.4); }
.card.rarity-rare { box-shadow: inset 0 0 0 1px rgba(77,177,255,0.55); }
.card.rarity-superrare { box-shadow: inset 0 0 12px rgba(255,210,77,0.55); border-color: var(--gold); }
.card canvas { image-rendering: pixelated; }
.card-name { font-size: 12px; font-weight: 800; }
.card-stars { font-size: 11px; color: var(--gold); }
.card-rarity { font-size: 9px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.card-ability { font-size: 9px; color: var(--muted); line-height: 1.2; }
.card-fav { position: absolute; top: 3px; right: 5px; font-size: 12px; }
.card-count { position: absolute; top: 3px; left: 5px; font-size: 10px; color: var(--gold); font-weight: 800; }

/* Gacha --------------------------------------------------------------------- */
.gacha-canvas { background: radial-gradient(circle at 50% 40%, #1a1430, #0d1426); border-radius: 14px; border: 2px solid var(--grid); max-width: 92%; }
.reveal {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 14px 18px; border-radius: 16px; border: 2px solid var(--gold);
  background: rgba(255,210,77,0.08); max-width: 92%;
}
.reveal.hidden { display: none; }
.reveal-bang { font-size: 26px; font-weight: 800; color: var(--gold); text-shadow: 0 0 16px rgba(255,210,77,0.7); }
.reveal canvas { image-rendering: pixelated; }
.reveal-name { font-size: 20px; font-weight: 800; }
.reveal-stars { font-size: 16px; color: var(--gold); }
.reveal-rarity { font-size: 13px; font-weight: 800; text-transform: uppercase; letter-spacing: 1px; }
.reveal-ability { font-size: 12px; color: var(--muted); }
.reveal-dupe { font-size: 12px; color: var(--accent); font-weight: 700; }

/* Level picker -------------------------------------------------------------- */
.level-pick {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  width: 100%; max-width: 420px;
}
.lvl-btn {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: rgba(255,255,255,0.05); border: 2px solid var(--grid);
  border-radius: 10px; padding: 8px 4px; cursor: pointer; color: var(--text);
}
.lvl-btn .lvl-emoji { font-size: 20px; }
.lvl-btn .lvl-n { font-size: 10px; color: var(--muted); }
.lvl-btn.selected { border-color: var(--accent); background: rgba(176,123,255,0.18); }
.lvl-btn.locked { opacity: 0.35; cursor: default; }
.lvl-btn.cleared .lvl-n { color: #7fdc86; }

/* Progress list ------------------------------------------------------------- */
.tasks-list {
  list-style: none; display: flex; flex-direction: column; gap: 8px;
  text-align: left; max-width: 460px; width: 100%;
}
.tasks-list li {
  display: flex; align-items: center; gap: 10px; font-size: 14px;
  padding: 9px 14px; border: 2px solid var(--grid); border-radius: 12px;
  background: rgba(255,255,255,0.04);
}
.tasks-list li.done { border-color: #4caf50; color: #bdf0c0; }
.tasks-list .task-mark { font-size: 17px; }
.tasks-list .task-count { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.tasks-list li.done .task-count { color: #7fdc86; }

/* Play button --------------------------------------------------------------- */
.play {
  background: var(--accent); border: none; border-radius: 12px;
  color: #1a0b2e; font-size: 17px; font-weight: 800; padding: 12px 34px;
  cursor: pointer; box-shadow: 0 4px 0 var(--accent-dim); transition: transform .08s;
  flex: 0 0 auto;
}
.play:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--accent-dim); }
.play:disabled { background: var(--grid); color: var(--muted); box-shadow: none; cursor: default; }

/* Touch controls ------------------------------------------------------------ */
.touch { position: absolute; inset: 0; pointer-events: none; z-index: 6; }
.touch.hidden { display: none; }
.dpad {
  position: absolute;
  left: clamp(12px, 3vw, 28px);
  bottom: clamp(12px, 3vh, 28px);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: clamp(4px, 1vw, 8px);
  width: clamp(150px, 38vmin, 220px);
  height: clamp(150px, 38vmin, 220px);
  pointer-events: none;
}
.tbtn {
  pointer-events: auto;
  border-radius: 14px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  background: rgba(12, 18, 38, 0.5);
  color: #fff;
  font-size: clamp(20px, 5vmin, 32px);
  font-weight: 700;
  line-height: 1;
  display: flex; align-items: center; justify-content: center;
  user-select: none; -webkit-user-select: none; touch-action: none;
  backdrop-filter: blur(2px);
}
.tbtn:active { background: rgba(176, 123, 255, 0.6); }
.tbtn.up    { grid-column: 2; grid-row: 1; }
.tbtn.left  { grid-column: 1; grid-row: 2; }
.tbtn.right { grid-column: 3; grid-row: 2; }
.tbtn.down  { grid-column: 2; grid-row: 3; }
.tbtn.attack {
  position: absolute;
  right: clamp(12px, 3vw, 28px);
  bottom: clamp(12px, 3vh, 28px);
  width: clamp(74px, 17vmin, 110px);
  height: clamp(74px, 17vmin, 110px);
  border-radius: 50%;
  font-size: clamp(28px, 7vmin, 44px);
  background: rgba(255, 90, 90, 0.34);
}
.tbtn.jump {
  position: absolute;
  right: clamp(12px, 3vw, 28px);
  bottom: calc(clamp(12px, 3vh, 28px) + clamp(82px, 18vmin, 122px));
  width: clamp(58px, 13vmin, 84px);
  height: clamp(58px, 13vmin, 84px);
  border-radius: 50%;
  font-size: clamp(26px, 6.5vmin, 40px);
  background: rgba(77, 177, 255, 0.32);
}

.hint {
  flex: 0 0 auto; text-align: center; font-size: 11px; color: var(--muted); padding: 2px 8px 4px;
}
.foot { flex: 0 0 auto; text-align: center; font-size: 10px; color: var(--muted); padding-bottom: 4px; }
