* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #1f2120;
  color: #fbf3e4;
  font-family: ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

body {
  display: flex;
  flex-direction: column;
  background: radial-gradient(120% 80% at 50% 0%, #1f1847 0%, #1f2120 70%);
}

.defs {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- HUD ---------- */

.hud {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 10px 8px;
}

.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 4px 12px;
  border-radius: 16px;
  background: rgb(42 44 42 / 0.9);
  border: 1px solid #46443e;
}

.chip strong {
  font-size: clamp(18px, 3.4vmin, 24px);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.chip small {
  font-size: 13px;
  font-weight: 800;
  color: #d9cfbd;
}

.chip-icon {
  width: 24px;
  height: 24px;
}

.chip-pops strong {
  color: #ff6b8e;
  min-width: 2ch;
}

.chip-time strong {
  color: #ffc857;
  min-width: 3ch;
  text-align: right;
}

.hud-buttons {
  margin-left: auto;
  display: flex;
  gap: 6px;
}

.icon-btn {
  width: 46px;
  height: 46px;
  border: 1px solid #46443e;
  border-radius: 14px;
  background: rgb(42 44 42 / 0.9);
  color: #fbf3e4;
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
}

.icon-btn svg {
  width: 28px;
  height: 28px;
}

.icon-btn[aria-pressed="true"] {
  background: #27d6c1;
  border-color: #27d6c1;
}

.icon-btn[aria-pressed="true"] svg {
  filter: brightness(0.15);
}

.pause-glyph {
  width: 14px;
  height: 16px;
  border-left: 5px solid currentColor;
  border-right: 5px solid currentColor;
}

/* A circular "new board" arrow. */
.new-glyph {
  width: 20px;
  height: 20px;
  border: 3px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  position: relative;
}

.new-glyph::after {
  content: "";
  position: absolute;
  top: -5px;
  right: -3px;
  border: 5px solid transparent;
  border-left-color: currentColor;
  transform: rotate(35deg);
}

.mode-tag {
  padding: 2px 12px;
  border-radius: 999px;
  background: #27d6c1;
  color: #1f2120;
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

@media (max-width: 400px) {
  .hud {
    gap: 6px;
    padding-left: 8px;
    padding-right: 8px;
  }
  .chip {
    padding: 4px 9px;
  }
  .hud .mode-tag {
    display: none;
  }
  .hud-buttons {
    gap: 4px;
  }
  .icon-btn {
    width: 44px;
    height: 44px;
  }
}

/* ---------- Board ---------- */

.board-wrap {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  display: flex;
  padding: 6px 12px max(12px, env(safe-area-inset-bottom));
  touch-action: pan-x pan-y;
  -webkit-overflow-scrolling: touch;
}

.board {
  --cell: 36px;
  --gap: 3px;
  margin: auto;
  display: grid;
  gap: var(--gap);
  padding: 8px;
  border-radius: 18px;
  background: #2a2c2a;
  border: 1px solid #46443e;
  box-shadow: 0 18px 50px rgb(0 0 0 / 0.45);
}

.cell {
  width: var(--cell);
  height: var(--cell);
  padding: 0;
  border: 0;
  border-radius: calc(var(--cell) * 0.24);
  background: linear-gradient(160deg, #6a55e0 0%, #4a37b8 100%);
  box-shadow:
    inset 0 calc(var(--cell) * 0.08) 0 rgb(255 255 255 / 0.22),
    inset 0 calc(var(--cell) * -0.1) 0 rgb(0 0 0 / 0.25);
  color: #fbf3e4;
  font: inherit;
  font-size: calc(var(--cell) * 0.58);
  font-weight: 900;
  line-height: 1;
  display: grid;
  place-items: center;
  cursor: pointer;
  touch-action: manipulation;
}

@media (hover: hover) {
  .cell:not(.open):hover {
    background: linear-gradient(160deg, #7c66f0 0%, #5845c9 100%);
  }
}

.cell:focus-visible {
  outline: 3px solid #c6f432;
  outline-offset: 1px;
  position: relative;
  z-index: 1;
}

.cell svg {
  width: 74%;
  height: 74%;
  pointer-events: none;
}

.cell.open {
  background: #0f121a;
  box-shadow: inset 0 0 0 1px #232936;
  cursor: default;
  animation: open 0.16s ease-out;
}

.cell.open.n1,
.cell.open.n2,
.cell.open.n3,
.cell.open.n4,
.cell.open.n5,
.cell.open.n6,
.cell.open.n7,
.cell.open.n8 {
  cursor: pointer;
}

.n1 {
  color: #27d6c1;
}
.n2 {
  color: #b9f24a;
}
.n3 {
  color: #ff6b8e;
}
.n4 {
  color: #a78bfa;
}
.n5 {
  color: #ff9550;
}
.n6 {
  color: #ffc857;
}
.n7 {
  color: #fbf3e4;
}
.n8 {
  color: #d9cfbd;
}

.cell.flag {
  background: linear-gradient(160deg, #3d3480 0%, #2c2563 100%);
}

.cell.pop {
  background: #2a1830;
  animation: pop-in 0.32s ease-out backwards;
  animation-delay: var(--d, 0s);
}

.cell.boom {
  background: #ff5c7a;
}

.cell.wrong {
  position: relative;
  background: #2a1830;
}

.cell.wrong::after {
  content: "";
  position: absolute;
  inset: 18%;
  background:
    linear-gradient(45deg, transparent 44%, #ff5c7a 44% 56%, transparent 56%),
    linear-gradient(-45deg, transparent 44%, #ff5c7a 44% 56%, transparent 56%);
}

.board.won .cell.flag {
  background: linear-gradient(160deg, #1e8f80 0%, #12665b 100%);
}

.board.ended .cell {
  cursor: default;
}

@keyframes open {
  from {
    transform: scale(0.82);
    opacity: 0.4;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes pop-in {
  0% {
    transform: scale(0.3);
  }
  70% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}

/* ---------- Overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  text-align: center;
  background: rgb(31 33 32 / 0.7);
  overflow-y: auto;
  z-index: 5;
}

#pause-screen {
  background: rgb(31 33 32 / 0.96);
}

.card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(8px, 1.8vmin, 12px);
  width: min(460px, 100%);
  margin: auto 0;
  padding: clamp(16px, 4vmin, 28px) clamp(16px, 4vmin, 26px);
  border-radius: 28px;
  background: rgb(42 44 42 / 0.97);
  border: 1px solid #46443e;
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.45);
}

.badge {
  display: grid;
  grid-template-columns: repeat(3, 22px);
  gap: 3px;
  padding: 6px;
  border-radius: 12px;
  background: #0f121a;
}

.badge .t {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: linear-gradient(160deg, #6a55e0, #4a37b8);
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 900;
}

.badge .t.o,
.badge .t.n1,
.badge .t.n2 {
  background: #2a2c2a;
}

.badge .t.f {
  background: linear-gradient(160deg, #3d3480, #2c2563);
}

.badge .t svg {
  width: 16px;
  height: 16px;
}

.overlay h1,
.overlay h2 {
  margin: 0;
  font-size: clamp(30px, 8vmin, 52px);
  font-weight: 900;
  line-height: 1.05;
  color: #fbf3e4;
}

.overlay p {
  margin: 0;
  color: #d9cfbd;
  font-size: clamp(14px, 3.4vmin, 17px);
  line-height: 1.35;
}

.overlay .goal {
  color: #fbf3e4;
  max-width: 36ch;
}

.overlay .hint,
.overlay .sub {
  font-size: clamp(13px, 3vmin, 15px);
}

.hint kbd {
  display: inline-block;
  min-width: 22px;
  padding: 0 6px;
  border-radius: 6px;
  border: 1px solid #3a4250;
  border-bottom-width: 3px;
  background: #363836;
  color: #fbf3e4;
  font: inherit;
  font-size: 13px;
  font-weight: 800;
}

.overlay .result {
  font-size: clamp(30px, 8vmin, 46px);
  color: #ffc857;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.near-miss {
  margin: 4px 0 6px;
  padding: 6px 14px;
  font-size: 0.95rem;
  font-weight: 700;
  color: #ffb86c;
  background: rgba(255, 184, 108, 0.12);
  border: 1px solid rgba(255, 184, 108, 0.35);
  border-radius: 999px;
  text-align: center;
  animation: pulse-near-miss 1.8s ease-in-out infinite;
}

@keyframes pulse-near-miss {
  0%, 100% {
    transform: scale(1);
    opacity: 0.95;
  }
  50% {
    transform: scale(1.02);
    opacity: 1;
  }
}

.overlay strong {
  color: #27d6c1;
}

.levels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  width: 100%;
}

.mode-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  width: 100%;
}

.mode-buttons .btn {
  flex: 1 1 120px;
  min-width: 0;
  font-size: 17px;
}

.btn {
  min-height: 52px;
  padding: 10px 18px;
  border: 0;
  border-radius: 999px;
  background: #ee5e24;
  color: #15130f;
  font: inherit;
  font-size: 19px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 6px 0 #b8400f;
  transition: transform 0.08s ease;
}

.btn.level {
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  line-height: 1.1;
}

.btn small {
  font-size: 12px;
  font-weight: 700;
  opacity: 0.9;
}

.btn:hover {
  background: #8552f7;
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 #b8400f;
}

.btn.btn-alt {
  background: #363836;
  border: 1px solid #3a4250;
  box-shadow: 0 6px 0 #46443e;
}

.btn.btn-alt:hover {
  background: #2a3140;
}

.btn.btn-alt:active {
  box-shadow: 0 3px 0 #46443e;
}

.btn:focus-visible,
.icon-btn:focus-visible {
  outline: 3px solid #c6f432;
  outline-offset: 3px;
}

.peek-back {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 6;
}

.peek-back:active {
  transform: translate(-50%, 3px);
}

@media (max-height: 520px) {
  .badge,
  .overlay .goal {
    display: none;
  }
  .card {
    gap: 6px;
    padding: 12px 18px;
  }
  .btn {
    min-height: 46px;
  }
  .levels {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn {
    transition: none;
  }
  .cell.open,
  .cell.pop {
    animation: none;
  }
}

/* ---------- Canvas and Campaign Additions ---------- */
.board-canvas {
  display: block;
  width: min(calc(100vw - 24px), calc(100vh - 120px), 640px);
  height: auto;
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  touch-action: none;
  cursor: pointer;
  margin: auto;
}

.primary-accent {
  background: linear-gradient(135deg, #27d6c1, #0d9488) !important;
  color: #042f2e !important;
  font-weight: 800 !important;
  box-shadow: 0 4px 12px rgba(39, 214, 193, 0.35) !important;
}

.primary-accent:hover {
  background: linear-gradient(135deg, #5eead4, #27d6c1) !important;
  transform: translateY(-2px);
}

.campaign-card {
  max-width: 480px;
  width: 95%;
  max-height: 88vh;
  overflow-y: auto;
}

.campaign-tier-tabs {
  display: flex;
  gap: 6px;
  margin: 12px 0;
  justify-content: center;
  flex-wrap: wrap;
}

.tier-tab {
  padding: 6px 12px;
  border: 1px solid #3b3f58;
  border-radius: 8px;
  background: #1a1d2b;
  color: #c5ccdc;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
}

.tier-tab.active {
  background: #27d6c1;
  color: #042f2e;
  border-color: #5eead4;
}

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

.stage-node {
  padding: 8px 4px;
  background: #1a1d2b;
  border: 2px solid #2e334d;
  border-radius: 10px;
  color: #fff;
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: all 0.2s;
}

.stage-node.locked {
  opacity: 0.35;
  cursor: not-allowed;
  filter: grayscale(1);
}

.stage-node:not(.locked):hover {
  border-color: #27d6c1;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(39, 214, 193, 0.25);
}

.stage-node.current {
  border-color: #ff6b8e;
  background: #1e293b;
}

.stage-stars {
  font-size: 10px;
  color: #facc15;
}

.stars-row {
  display: flex;
  justify-content: center;
  gap: 8px;
  font-size: 28px;
  color: #facc15;
  margin: 8px 0;
}

