* {
  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;
}

body {
  background: radial-gradient(120% 70% at 50% 0%, #22164a 0%, #1f2120 62%);
}

.hidden {
  display: none !important;
}

.invisible {
  visibility: hidden;
}

.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;
}

/* ---------- Layout: board scales with the iframe ---------- */

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px 12px;
  /* One board cell; the board is 7.36 cells wide and 7.36 tall (with the drop lane). */
  --cell: min(calc((100vw - 24px) / 7.4), calc((100vh - 132px) / 7.45), 92px);
}

.hud {
  flex: none;
  width: 100%;
  max-width: max(300px, calc(var(--cell) * 7.36));
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  grid-template-areas:
    ". scores pause"
    "status status status";
  align-items: center;
  row-gap: 4px;
}

.scores {
  grid-area: scores;
  display: flex;
  justify-content: center;
  gap: 10px;
}

.chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 4px 14px 4px 8px;
  border: 2px solid #262b3f;
  border-radius: 999px;
  background: #1a1d2b;
  font-weight: 800;
  font-size: clamp(15px, 3.4vmin, 19px);
  transition: border-color 0.2s, box-shadow 0.2s, transform 0.2s;
}

.chip.active {
  transform: translateY(-1px);
}

.chip.p1.active {
  border-color: #facc15;
  box-shadow: 0 0 0 3px rgb(250 204 21 / 0.22);
}

.chip.p2.active {
  border-color: #fb7185;
  box-shadow: 0 0 0 3px rgb(251 113 133 / 0.22);
}

.chip-score {
  min-width: 1.1em;
  text-align: right;
  font-size: 1.15em;
}

.status {
  grid-area: status;
  margin: 0;
  min-height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 700;
  font-size: clamp(16px, 3.6vmin, 21px);
  color: #f1e8d6;
}

.icon-btn {
  grid-area: pause;
  justify-self: end;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 14px;
  background: #1a1d2b;
  color: #e0d6c3;
  cursor: pointer;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.icon-btn:hover {
  background: #262b3f;
}

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

.stage {
  flex: 0 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.board-wrap {
  position: relative;
  width: min(calc(100vw - 20px), calc((100vh - 120px) * (700 / 680)), 680px);
  max-width: 680px;
  margin: 0 auto;
  touch-action: none;
}

.lane {
  position: relative;
  height: 1em;
}

.board {
  position: relative;
  height: 6.36em;
}

.board-back {
  position: absolute;
  inset: 0.12em;
  border-radius: 0.2em;
  background: linear-gradient(180deg, #1b1240, #120c2c);
}

.discs {
  position: absolute;
  inset: 0.18em;
}

.plate {
  position: absolute;
  inset: 0.04em; /* runs under the frame so no seam shows at fractional sizes */
  border-radius: 0.2em;
  background-position: 0.14em 0.14em;
  background-image: radial-gradient(
    circle at 50% 50%,
    transparent 0.4em,
    #5b30cf 0.412em,
    #5b30cf 0.44em,
    #7c47f5 0.462em
  );
  background-size: 1em 1em;
  pointer-events: none;
}

.frame {
  position: absolute;
  inset: 0;
  border: 0.18em solid #7c47f5;
  border-radius: 0.28em;
  box-shadow:
    0 0.1em 0 #5b30cf,
    0 0.3em 0.7em rgb(0 0 0 / 0.4);
  pointer-events: none;
}

.win-layer {
  position: absolute;
  left: 0.18em;
  top: 0.18em;
  width: 7em;
  height: 6em;
  overflow: visible;
  pointer-events: none;
}

.columns {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0.18em;
  right: 0.18em;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
}

.col {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0.22em;
  background: transparent;
  font: inherit;
  color: inherit;
  cursor: pointer;
  transition: background-color 0.12s;
}

.app:not(.can-act) .col {
  cursor: default;
}

.app.can-act .col.selected {
  background: rgb(255 255 255 / 0.07);
}

.col:focus-visible {
  outline: 3px solid #c6f432;
  outline-offset: -3px;
}

.col.nope {
  animation: nope 0.32s ease;
}

.app.cpu-turn .col {
  cursor: progress;
}

/* ---------- Discs ---------- */

.disc {
  position: absolute;
  width: 0.88em;
  height: 0.88em;
  border-radius: 50%;
  left: calc(var(--c, 0) * 1em + 0.06em);
  top: calc(var(--r, 0) * 1em + 0.06em);
}

.disc.p1 {
  background: radial-gradient(circle at 50% 38%, #fde68a 0 16%, #facc15 50%, #eab308 100%);
  box-shadow:
    inset 0 -0.06em 0 rgb(161 98 7 / 0.45),
    inset 0 0.04em 0 rgb(255 255 255 / 0.35);
}

.disc.p2 {
  background: radial-gradient(circle at 50% 38%, #fecdd3 0 12%, #fb7185 50%, #e11d48 100%);
  box-shadow:
    inset 0 -0.06em 0 rgb(136 19 55 / 0.45),
    inset 0 0.04em 0 rgb(255 255 255 / 0.3);
}

/* Shape marks so the two sides never rely on color alone: ring vs. diamond. */
.disc::after {
  content: "";
  position: absolute;
}

.disc.p1::after {
  inset: 22%;
  border-radius: 50%;
  background: radial-gradient(circle closest-side, transparent 56%, rgb(146 64 14 / 0.55) 61% 86%, transparent 91%);
}

.disc.p2::after {
  inset: 27%;
  background: rgb(136 19 55 / 0.5);
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}

.disc.mini {
  position: relative;
  left: auto;
  top: auto;
  display: inline-block;
  flex: none;
  font-size: 22px;
}

.status .disc.mini {
  font-size: 20px;
}

.app.cpu-turn .status .disc.mini {
  animation: think 0.55s ease-in-out infinite alternate;
}

.preview {
  left: 0.24em;
  top: 0.06em;
  transform: translateX(calc(var(--c, 3) * 1em));
  transition: transform 0.14s ease;
  animation: hover-bob 1.1s ease-in-out infinite alternate;
  pointer-events: none;
}

.disc.falling {
  animation-name: fall;
  animation-fill-mode: both;
  animation-timing-function: linear;
}

.board.ended .disc:not(.win) {
  filter: brightness(0.55) saturate(0.7);
  transition: filter 0.4s ease;
}

.disc.win {
  animation: glow 0.8s ease-in-out infinite alternate;
}

.win-line {
  fill: none;
  stroke-linecap: round;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: draw-line 0.45s ease-out 0.1s forwards;
}

.win-line.under {
  stroke: #1f2120;
  stroke-width: 0.3;
  opacity: 0.6;
}

.win-line.over {
  stroke: #fff;
  stroke-width: 0.14;
}

.win-mark {
  transform-box: fill-box;
  transform-origin: center;
  animation: pulse 0.75s ease-in-out infinite alternate;
}

.win-mark circle {
  fill: none;
}

.win-mark .under {
  stroke: #1f2120;
  stroke-width: 0.17;
}

.win-mark .over {
  stroke: #fff;
  stroke-width: 0.09;
}

/* ---------- Screens ---------- */

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

.overlay h1,
.overlay h2 {
  margin: 0;
  line-height: 1.05;
  letter-spacing: -0.01em;
}

.overlay h1 {
  font-size: clamp(34px, 9vmin, 64px);
}

.overlay h2 {
  font-size: clamp(28px, 7vmin, 48px);
}

.goal {
  margin: 0;
  max-width: 34ch;
  color: #d9cfbd;
  font-size: clamp(15px, 2.8vmin, 19px);
  text-wrap: balance;
}

.hint {
  margin: 0;
  color: #8b93a7;
  font-size: 14px;
}

.logo-discs {
  display: flex;
  gap: 6px;
}

.logo-discs .disc.mini {
  font-size: clamp(24px, 5vmin, 36px);
}

.menu {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 10px;
  width: min(100%, 300px);
}

.btn {
  min-width: 160px;
  min-height: 52px;
  padding: 12px 24px;
  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, box-shadow 0.08s ease;
}

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

.btn.alt {
  background: #27d6c1;
  color: #0b1220;
  box-shadow: 0 6px 0 #0e7490;
}

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

.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  border-radius: 999px;
  background: #1a1d2b;
}

.seg {
  flex: 1;
  min-height: 44px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: #e0d6c3;
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
}

.seg[aria-pressed="true"] {
  background: #facc15;
  color: #1f1300;
}

.seg[aria-pressed="true"]::before {
  content: "\2713\00a0";
}

.overlay.result {
  justify-content: flex-start;
  padding-top: 12px;
  background: rgb(31 33 32 / 0.35);
}

.card {
  width: min(100%, 340px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px 18px 18px;
  border: 2px solid #2a2f45;
  border-radius: 24px;
  background: #171a28;
  box-shadow: 0 18px 50px rgb(0 0 0 / 0.55);
  animation: card-in 0.35s cubic-bezier(0.2, 1.4, 0.4, 1);
}

.card h2 {
  font-size: clamp(24px, 4.6vmin, 34px);
}

.sub {
  margin: 0;
  color: #d9cfbd;
  font-size: 15px;
  text-wrap: balance;
}

.near-miss {
  margin: -4px 0 0;
  font-size: 14px;
  font-weight: 800;
  color: #ff6b4a;
  letter-spacing: 0.02em;
  animation: pulse-near 1.6s ease-in-out infinite;
}

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

.match {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.tally {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  background: #1f2120;
  font-size: 15px;
  font-weight: 700;
}

.tally .disc.mini {
  font-size: 18px;
}

.next {
  margin: 0;
  color: #8b93a7;
  font-size: 13px;
}

.row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  width: 100%;
  margin-top: 4px;
}

.btn.small {
  flex: 1 1 0;
  min-width: 0;
  min-height: 48px;
  padding: 10px;
  font-size: 16px;
  white-space: nowrap;
}

@media (max-width: 360px) {
  .overlay.result {
    padding-left: 12px;
    padding-right: 12px;
  }

  .card {
    padding: 14px 14px 16px;
  }
}

/* Landscape phones: scores move to a side column so the board can grow. */
@media (orientation: landscape) and (max-height: 560px) {
  .app {
    flex-direction: row;
    justify-content: center;
    gap: 16px;
    padding: 8px 12px;
    --cell: min(calc((100vw - 250px) / 7.4), calc((100vh - 16px) / 7.45), 92px);
  }

  .hud {
    width: 190px;
    max-width: none;
    grid-template-columns: 1fr;
    grid-template-areas:
      "scores"
      "status"
      "pause";
    justify-items: center;
    row-gap: 12px;
  }

  .scores {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .chip-score {
    margin-left: auto;
  }

  .status {
    flex-wrap: wrap;
    text-align: center;
  }

  .icon-btn {
    justify-self: center;
  }

  .stage {
    flex: 0 1 auto;
    width: auto;
  }
}

@media (orientation: landscape) {
  .overlay.result {
    justify-content: center;
    align-items: flex-start;
    padding: 12px;
  }

  .card {
    width: min(100%, 280px);
  }

  .row {
    flex-direction: column;
  }

  .btn.small {
    flex: none;
  }
}

@media (max-height: 440px) {
  .overlay {
    gap: 8px;
    padding: 10px 16px;
  }

  .logo-discs {
    display: none;
  }

  .btn {
    min-height: 46px;
    padding: 8px 20px;
  }
}

/* Pause freezes every running CSS animation. */
.is-paused *,
.is-paused *::before,
.is-paused *::after {
  animation-play-state: paused !important;
}

/* ---------- Animations ---------- */

@keyframes fall {
  0% {
    transform: translateY(calc(var(--fall) * -1em));
    animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45);
  }
  60% {
    transform: translateY(0);
    animation-timing-function: cubic-bezier(0, 0.55, 0.45, 1);
  }
  78% {
    transform: translateY(-0.14em);
    animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45);
  }
  100% {
    transform: translateY(0);
  }
}

@keyframes fall-simple {
  from {
    transform: translateY(calc(var(--fall) * -1em));
  }
  to {
    transform: translateY(0);
  }
}

@keyframes hover-bob {
  from {
    margin-top: 0;
  }
  to {
    margin-top: -0.05em;
  }
}

@keyframes glow {
  from {
    filter: brightness(1);
  }
  to {
    filter: brightness(1.22);
  }
}

@keyframes pulse {
  from {
    transform: scale(0.94);
  }
  to {
    transform: scale(1.08);
  }
}

@keyframes draw-line {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes think {
  from {
    transform: translateY(0);
  }
  to {
    transform: translateY(-4px);
  }
}

@keyframes nope {
  0%,
  100% {
    transform: translateX(0);
  }
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

@keyframes card-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .disc.falling {
    animation-name: fall-simple;
  }

  .preview,
  .disc.win,
  .win-mark,
  .app.cpu-turn .status .disc.mini,
  .card {
    animation: none;
  }

  .win-line {
    animation-duration: 0.01s;
  }
}

/* ---------- Canvas and Campaign Additions ---------- */
#board-canvas,
.board-canvas {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100vh - 120px);
  aspect-ratio: 700 / 680;
  border-radius: 18px;
  touch-action: none;
  cursor: pointer;
  margin: 0 auto;
}

@media (orientation: landscape) and (max-height: 560px) {
  .board-wrap {
    width: min(calc(100vw - 220px), calc((100vh - 24px) * (700 / 680)), 680px);
    max-height: calc(100vh - 24px);
  }
}

.primary-accent {
  background: linear-gradient(135deg, #f59e0b, #d97706) !important;
  color: #1a1005 !important;
  font-weight: 800 !important;
  box-shadow: 0 4px 12px rgba(245, 158, 11, 0.35) !important;
}

.primary-accent:hover {
  background: linear-gradient(135deg, #fbbf24, #f59e0b) !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: #7c47f5;
  color: #ffffff;
  border-color: #a78bfa;
}

.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: #facc15;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(250, 204, 21, 0.25);
}

.stage-node.current {
  border-color: #38bdf8;
  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;
}

