:root {
  --bg: #06060f;
  --surface: rgba(255, 255, 255, 0.04);
  --border: rgba(255, 255, 255, 0.08);
  --text: #f4f6ff;
  --muted: #9aa3c0;
  --accent: #7c5cff;
  --accent-2: #ff4d8d;
  --board-bg: rgba(124, 92, 255, 0.12);
  --tile-bg: rgba(255, 255, 255, 0.06);
  --tile-covered: rgba(0, 0, 0, 0.72);
  --tile-layer-step: 5px;
  --layer-colors:
    #4ecdc4,
    #7c5cff,
    #ff4d8d,
    #ffd93d,
    #3dffb5,
    #ff8c42;
  --danger: #ff4466;
  --radius-lg: 16px;
  --radius-md: 12px;
  --width: 3rem;
  --height: 3.6rem;
  --mapWidth: 7;
  --mapHeight: 9;
  --ms: 600ms;
  --initEndMs: 600ms;
  --radius: calc(var(--width) * 0.12);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: "Noto Sans SC", system-ui, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow-x: hidden;
}

html[lang="en"] body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

.page-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}

.bg-grid {
  position: absolute;
  inset: -40%;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.02) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.02) 1px, transparent 1px);
  background-size: 36px 36px;
  transform: perspective(500px) rotateX(58deg);
  animation: grid-drift 24s linear infinite;
}

.bg-vignette {
  position: absolute;
  inset: 0;
  background: radial-gradient(ellipse 80% 60% at 50% 20%, transparent 30%, rgba(6, 6, 15, 0.85) 100%);
}

.orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(60px);
  opacity: 0.35;
}

.orb-1 { width: 220px; height: 220px; background: var(--accent); top: 8%; left: -4%; }
.orb-2 { width: 180px; height: 180px; background: var(--accent-2); top: 40%; right: -6%; }
.orb-3 { width: 140px; height: 140px; background: #3dffb5; bottom: 10%; left: 30%; opacity: 0.2; }

@keyframes grid-drift {
  to { transform: perspective(500px) rotateX(58deg) translateY(40px); }
}

.wrapper {
  position: relative;
  z-index: 1;
  width: min(100%, 480px);
  margin: 0 auto;
  padding: 0 12px 16px;
}

.arcadebox-top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
}

.back-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  width: 36px;
  height: 36px;
  cursor: pointer;
}

.glass-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  backdrop-filter: blur(12px);
}

.game-shell.hidden {
  display: none;
}

.hud-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  margin-bottom: 10px;
}

.hud-title {
  font-weight: 700;
  font-size: 1rem;
}

.hud-chips {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.hud-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
  font-size: 0.72rem;
}

.hud-label {
  color: var(--muted);
}

.hud-chip strong {
  font-size: 1rem;
}

.game-main {
  width: calc(var(--width) * var(--mapWidth) + 24px);
  max-width: 100%;
  margin: 0 auto;
}

.container {
  width: calc(var(--width) * var(--mapWidth));
  max-width: 100%;
  padding: 10px;
  margin-bottom: 10px;
  border: none;
}

.game-map-container {
  height: calc(var(--height) * var(--mapHeight) + 28px);
  position: relative;
  z-index: 2;
  padding: 14px 10px 18px;
  background:
    radial-gradient(ellipse 90% 70% at 50% 45%, rgba(124, 92, 255, 0.14), transparent 70%),
    linear-gradient(180deg, rgba(20, 18, 40, 0.9) 0%, rgba(8, 8, 18, 0.95) 100%);
  border-radius: var(--radius-md);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.06),
    inset 0 -32px 64px rgba(0, 0, 0, 0.35),
    0 8px 32px rgba(0, 0, 0, 0.25);
  perspective: 900px;
}

.game-map {
  height: calc(var(--height) * var(--mapHeight));
  background: transparent;
  position: relative;
  transform: rotateX(4deg);
  transform-origin: 50% 60%;
}

.item {
  width: var(--width);
  height: var(--height);
  border-radius: var(--radius);
  background: var(--tile-bg);
  border: 1px solid var(--border);
  position: absolute;
  overflow: visible;
  cursor: pointer;
  transition: var(--ms);
  transition-property: top, left, transform, box-shadow, filter, border-color, opacity;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

/* ── Board tiles: physical stack + layer color ── */
.game-map .item {
  --layer: 0;
  --layer-color: #4ecdc4;
  --stack-x: calc(var(--layer) * -5px);
  --stack-y: calc(var(--layer) * -6px);
  --face-inset: 0px;
  border: 2px solid color-mix(in srgb, var(--layer-color) 55%, white 12%);
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.14) 0%, rgba(255, 255, 255, 0.02) 42%, rgba(0, 0, 0, 0.08) 100%),
    linear-gradient(180deg, color-mix(in srgb, var(--layer-color) 22%, #1a1830) 0%, #12101f 100%);
  transform: translate(var(--stack-x), var(--stack-y));
  box-shadow:
    calc(var(--layer) * 2px + 2px) calc(var(--layer) * 2px + 4px) calc(var(--layer) * 3px + 10px) rgba(0, 0, 0, 0.45),
    0 0 0 1px rgba(0, 0, 0, 0.35);
}

.game-map .item[data-layer="0"] { --layer-color: #4ecdc4; }
.game-map .item[data-layer="1"] { --layer-color: #7c5cff; }
.game-map .item[data-layer="2"] { --layer-color: #ff4d8d; }
.game-map .item[data-layer="3"] { --layer-color: #ffd93d; }
.game-map .item[data-layer="4"] { --layer-color: #3dffb5; }
.game-map .item[data-layer="5"] { --layer-color: #ff8c42; }
.game-map .item[data-layer="6"] { --layer-color: #4ecdc4; }
.game-map .item[data-layer="7"] { --layer-color: #7c5cff; }

/* Card thickness — right & bottom edges */
.game-map .item::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  z-index: 1;
  box-shadow:
    inset 4px 0 0 var(--layer-color),
    inset 0 -5px 0 color-mix(in srgb, var(--layer-color) 70%, black 35%),
    inset -5px 0 0 color-mix(in srgb, var(--layer-color) 55%, black 25%);
}

/* Layer badge */
.game-map .item::after {
  content: attr(data-layer);
  display: flex;
  align-items: center;
  justify-content: center;
  position: absolute;
  top: 3px;
  right: 3px;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  font-size: 0.58rem;
  font-weight: 800;
  line-height: 1;
  color: #0a0a12;
  background: var(--layer-color);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  z-index: 4;
  pointer-events: none;
  transition: opacity var(--ms), transform var(--ms);
}

/* Playable = top of stack, bright & clickable */
.game-map .item.playable {
  z-index: calc(100 + var(--layer));
  filter: brightness(1.08) saturate(1.1);
  border-color: color-mix(in srgb, var(--layer-color) 80%, white 25%);
  animation: playable-pulse 2.4s ease-in-out infinite;
}

.game-map .item.playable::after {
  content: "L" attr(data-layer-label);
  font-size: 0.52rem;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #fff 0%, var(--layer-color) 100%);
  color: #1a1030;
  width: auto;
  min-width: 18px;
  padding: 0 3px;
  height: 16px;
}

.game-map .item.playable:hover {
  transform: translate(var(--stack-x), calc(var(--stack-y) - 4px)) scale(1.04);
  border-color: #fff;
  box-shadow:
    0 0 0 3px color-mix(in srgb, var(--layer-color) 65%, white 20%),
    0 0 24px color-mix(in srgb, var(--layer-color) 50%, transparent),
    calc(var(--layer) * 2px + 4px) calc(var(--layer) * 2px + 8px) calc(var(--layer) * 3px + 14px) rgba(0, 0, 0, 0.5);
  filter: brightness(1.15) saturate(1.2);
  animation: none;
}

@keyframes playable-pulse {
  0%, 100% {
    box-shadow:
      0 0 0 0 color-mix(in srgb, var(--layer-color) 45%, transparent),
      calc(var(--layer) * 2px + 2px) calc(var(--layer) * 2px + 4px) calc(var(--layer) * 3px + 10px) rgba(0, 0, 0, 0.45);
  }
  50% {
    box-shadow:
      0 0 0 3px color-mix(in srgb, var(--layer-color) 35%, transparent),
      calc(var(--layer) * 2px + 2px) calc(var(--layer) * 2px + 4px) calc(var(--layer) * 3px + 10px) rgba(0, 0, 0, 0.45);
  }
}

/* Covered = buried under other tiles */
.game-map .item.covered {
  z-index: var(--layer);
  cursor: not-allowed;
  filter: brightness(0.42) saturate(0.25) contrast(0.9);
  transform: translate(calc(var(--stack-x) + 3px), calc(var(--stack-y) + 4px)) scale(0.97);
  border-color: rgba(80, 80, 100, 0.5);
  opacity: 0.88;
}

.game-map .item.covered::after {
  content: "L" attr(data-layer-label);
  font-size: 0.52rem;
  background: rgba(30, 30, 40, 0.9);
  color: #6a7088;
  width: auto;
  min-width: 18px;
  padding: 0 3px;
  height: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

/* Dark veil over covered tile face */
.game-map .item.covered .item-img::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background:
    repeating-linear-gradient(
      -45deg,
      rgba(0, 0, 0, 0.15) 0px,
      rgba(0, 0, 0, 0.15) 3px,
      rgba(0, 0, 0, 0.28) 3px,
      rgba(0, 0, 0, 0.28) 6px
    ),
    linear-gradient(165deg, rgba(0, 0, 0, 0.35) 0%, rgba(0, 0, 0, 0.72) 100%);
  pointer-events: none;
  z-index: 3;
}

.item.move {
  z-index: 999 !important;
  animation: none !important;
}

.item .item-img {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: center no-repeat;
  background-size: 78%;
  border-radius: calc(var(--radius) - 2px);
  box-sizing: border-box;
  position: relative;
  z-index: 2;
}

.game-map .item .item-img {
  margin: 3px 3px 5px 5px;
  width: calc(100% - 8px);
  height: calc(100% - 8px);
  background-color: rgba(255, 255, 255, 0.04);
}

.item .item-img.item-emoji {
  font-size: calc(var(--width) * 0.55);
  line-height: 1;
}

.item.pop {
  animation: pop-out 0.35s ease forwards;
}

@keyframes pop-out {
  to {
    transform: scale(0);
    opacity: 0;
  }
}

.game-candidate-container {
  min-height: var(--height);
}

.game-candidate {
  background: rgba(255, 255, 255, 0.03);
  width: 100%;
  min-height: var(--height);
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px;
  border-radius: var(--radius-md);
  border: 1px dashed rgba(255, 255, 255, 0.12);
}

.game-candidate.slot-full {
  border-color: var(--danger);
  animation: slot-pulse 1s ease infinite;
}

@keyframes slot-pulse {
  50% { box-shadow: 0 0 0 2px rgba(255, 68, 102, 0.35); }
}

.game-candidate .item {
  position: relative;
  flex: 0 0 auto;
  overflow: hidden;
  transform: none;
  filter: none;
  opacity: 1;
  animation: none;
  border: 1px solid var(--border);
  background: var(--tile-bg);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}

.game-candidate .item::before,
.game-candidate .item::after {
  display: none;
}

.game-candidate .item .item-img {
  margin: 0;
  width: 100%;
  height: 100%;
}

.game-toolbar {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.ui-button,
.menu-button {
  flex: 1;
  padding: 10px 14px;
  border-radius: var(--radius-md);
  border: none;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
}

.ui-button.secondary,
.menu-button.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.ui-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.game-hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
  margin: 10px 0 0;
}

.attribution {
  text-align: center;
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 14px;
  padding-bottom: 8px;
}

.attribution a {
  color: var(--accent);
}

.menu {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(4, 4, 12, 0.72);
  backdrop-filter: blur(6px);
}

.menu.active {
  display: flex;
}

.menu-content {
  width: min(100%, 400px);
  max-height: 90vh;
  overflow-y: auto;
  padding: 22px 18px;
}

.menu-hero {
  text-align: center;
  margin-bottom: 16px;
}

.menu-hero-badge {
  font-size: 2.4rem;
  margin-bottom: 6px;
}

.menu-hero h1 {
  margin: 0;
  font-size: 1.6rem;
}

.subtitle {
  margin: 4px 0 0;
  color: var(--muted);
}

.mode-label {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0 0 8px;
}

.mode-cards {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
}

.mode-card {
  text-align: left;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  cursor: pointer;
}

.mode-card.active {
  border-color: rgba(124, 92, 255, 0.55);
  box-shadow: 0 0 0 1px rgba(124, 92, 255, 0.25);
}

.mode-card-name {
  display: block;
  font-weight: 700;
}

.mode-card-hint,
.mode-best {
  display: block;
  font-size: 0.78rem;
  color: var(--muted);
  margin-top: 2px;
}

.daily-meta {
  font-size: 0.82rem;
  color: var(--muted);
  margin: 0 0 10px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.level-chip {
  aspect-ratio: 1;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-weight: 700;
  cursor: pointer;
}

.level-chip.locked {
  opacity: 0.35;
  cursor: not-allowed;
}

.menu-actions {
  display: grid;
  gap: 8px;
}

.menu-button.primary {
  background: linear-gradient(135deg, var(--accent), #3dffb5);
  color: #061018;
}

.summary-text {
  color: var(--muted);
  text-align: center;
}

.leaderboard-mount {
  margin-top: 14px;
}

#settingsPanel .settings-backdrop {
  position: fixed;
  inset: 0;
}

#settingsPanel:not(.active) {
  display: none;
}

#settingsPanel.active {
  display: flex;
}

.settings-panel {
  position: relative;
  z-index: 2;
}

#statsBody p {
  margin: 6px 0;
  color: var(--muted);
}

.item,
.container,
.game-main {
  transition: var(--ms);
  transition-property: top, left, width, height;
}

@media (max-width: 380px) {
  .hud-bar {
    flex-direction: column;
    align-items: flex-start;
  }
}
