* {
  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%, #2a1650 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: square board scales with the iframe ---------- */

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 10px 16px 14px;
  --board: min(calc(100vw - 32px), calc(100vh - 150px), 540px);
}

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

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

.tally {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 84px;
  padding: 6px 10px;
  border: 2px solid #262b3f;
  border-radius: 16px;
  background: #1a1d2b;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.tally-head {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #d9cfbd;
  font-size: 13px;
  font-weight: 800;
  white-space: nowrap;
}

.tally strong {
  font-size: clamp(20px, 4vmin, 28px);
  line-height: 1.1;
}

.tally.x.active {
  border-color: #27d6c1;
  box-shadow: 0 0 0 3px rgb(34 211 238 / 0.22);
}

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

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

.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;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- Mini marks (HUD, menus) ---------- */

.mini {
  position: relative;
  display: inline-block;
  flex: none;
  width: 1em;
  height: 1em;
  font-size: 16px;
}

.mini.o {
  border: 0.24em solid #fb7185;
  border-radius: 50%;
}

.mini.x::before,
.mini.x::after {
  content: "";
  position: absolute;
  left: 50%;
  top: -0.06em;
  width: 0.26em;
  height: 1.12em;
  margin-left: -0.13em;
  border-radius: 0.13em;
  background: #27d6c1;
  transform: rotate(45deg);
}

.mini.x::after {
  transform: rotate(-45deg);
}

.status .mini {
  font-size: 18px;
}

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

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

.board {
  position: relative;
  width: var(--board);
  height: var(--board);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: calc(var(--board) * 0.03);
  border-radius: calc(var(--board) * 0.07);
  background: linear-gradient(160deg, #8b5cf6, #7c47f5 45%, #5b30cf);
  box-shadow:
    0 calc(var(--board) * 0.02) 0 #5b30cf,
    0 20px 50px rgb(0 0 0 / 0.4);
  font-size: calc(var(--board) * 0.313); /* 1em = one cell */
  touch-action: none;
}

.cell {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 0.045em;
  border-radius: 0.2em;
  background: linear-gradient(180deg, #1e1745, #150f33);
  box-shadow:
    inset 0 -0.05em 0 rgb(0 0 0 / 0.3),
    inset 0 0.03em 0 rgb(255 255 255 / 0.06);
  transition: background 0.15s;
}

.cell:focus {
  outline: none;
}

.cell:focus-visible::before {
  outline: 0.035em solid #facc15;
  outline-offset: 0.02em;
}

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

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

/* Ghost preview of the next mark on hover / keyboard focus. */
.cell::after {
  content: "";
  position: absolute;
  inset: 0.2em;
  background: center / contain no-repeat;
  opacity: 0;
  transition: opacity 0.12s;
  pointer-events: none;
}

.board[data-turn="x"] .cell::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cpath d='M22 22L78 78M78 22L22 78' stroke='%2322D3EE' stroke-width='16' stroke-linecap='round' fill='none'/%3E%3C/svg%3E");
}

.board[data-turn="o"] .cell::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Ccircle cx='50' cy='50' r='34' stroke='%23FB7185' stroke-width='15' fill='none'/%3E%3C/svg%3E");
}

@media (hover: hover) {
  .app.can-act .cell.empty:hover::before {
    background: linear-gradient(180deg, #2a2060, #1d1545);
  }

  .app.can-act .cell.empty:hover::after {
    opacity: 0.28;
  }
}

.app.can-act .cell.empty:focus-visible::after {
  opacity: 0.28;
}

/* The CPU's glance: a faint O on the square it is weighing. */
.cell.empty.eye::after {
  opacity: 0.22;
}

.cell.nope::before {
  animation: nope 0.3s ease;
}

.mark {
  position: absolute;
  inset: 0.15em;
  pointer-events: none;
}

.mark svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  filter: drop-shadow(0 0.035em 0 rgb(0 0 0 / 0.35));
}

.mark .stroke {
  fill: none;
  stroke-linecap: round;
  stroke-width: 14;
}

.mark.x .stroke {
  stroke: #27d6c1;
}

.mark.o .stroke {
  stroke: #fb7185;
  stroke-width: 13;
}

.mark.pop {
  animation: wobble-in 0.66s linear both;
}

.mark.pop .stroke {
  stroke-dasharray: 1 1;
  stroke-dashoffset: 1;
  animation: ink 0.26s ease-out forwards;
}

.mark.pop .s2 {
  animation-delay: 0.12s;
}

.cell.win::before {
  background: linear-gradient(180deg, #33237a, #26195e);
  box-shadow: inset 0 0 0 0.04em rgb(255 255 255 / 0.6);
}

.board.ended .cell:not(.win) .mark {
  opacity: 0.35;
  transition: opacity 0.3s;
}

.board.won .cell.win .mark svg {
  animation: jiggle 0.45s ease-in-out 0.35s infinite alternate;
}

.board.won {
  animation: jelly 0.6s ease;
}

.win-layer {
  position: absolute;
  left: 3%;
  top: 3%;
  width: 94%;
  height: 94%;
  overflow: visible;
  pointer-events: none;
}

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

.win-line.under {
  stroke: #1f2120;
  stroke-width: 0.16;
  opacity: 0.5;
}

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

/* ---------- 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, 8.5vmin, 62px);
}

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

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

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

.logo-marks {
  display: flex;
  gap: 14px;
}

.logo-marks .mini {
  font-size: clamp(26px, 5.5vmin, 38px);
  animation: bob 1.2s ease-in-out infinite alternate;
}

.logo-marks .mini:nth-child(2) {
  animation-delay: -0.4s;
}

.logo-marks .mini:nth-child(3) {
  animation-delay: -0.8s;
}

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

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

.chip .mini {
  font-size: 14px;
}

.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;
    gap: 18px;
    padding: 10px 12px;
    --board: min(calc(100vw - 250px), calc(100vh - 20px), 540px);
  }

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

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

  .tally {
    flex-direction: row;
    justify-content: space-between;
    min-height: 44px;
    padding: 4px 14px;
  }

  .tally strong {
    font-size: 22px;
  }

  .status {
    flex-wrap: wrap;
    min-height: 0;
  }

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

@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-marks {
    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 wobble-in {
  0% {
    transform: scale(0) rotate(-30deg);
    animation-timing-function: cubic-bezier(0.2, 0.7, 0.4, 1);
  }
  38% {
    transform: scale(1.24) rotate(12deg);
    animation-timing-function: ease-in-out;
  }
  58% {
    transform: scale(0.88) rotate(-8deg);
    animation-timing-function: ease-in-out;
  }
  76% {
    transform: scale(1.07) rotate(4deg);
    animation-timing-function: ease-in-out;
  }
  90% {
    transform: scale(0.98) rotate(-1.5deg);
  }
  100% {
    transform: scale(1) rotate(0);
  }
}

@keyframes ink {
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes jiggle {
  from {
    transform: rotate(-6deg) scale(1.02);
  }
  to {
    transform: rotate(6deg) scale(1.1);
  }
}

@keyframes jelly {
  0%,
  100% {
    transform: scale(1, 1);
  }
  30% {
    transform: scale(1.03, 0.97);
  }
  50% {
    transform: scale(0.98, 1.02);
  }
  70% {
    transform: scale(1.01, 0.99);
  }
}

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

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

@keyframes bob {
  from {
    transform: translateY(0) rotate(-6deg);
  }
  to {
    transform: translateY(-6px) rotate(6deg);
  }
}

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

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

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media (prefers-reduced-motion: reduce) {
  .mark.pop {
    animation: fade-in 0.15s ease-out both;
  }

  .mark.pop .stroke {
    stroke-dashoffset: 0;
    animation: none;
  }

  .board.won,
  .board.won .cell.win .mark svg,
  .logo-marks .mini,
  .app.cpu-turn .status .mini,
  .card {
    animation: none;
  }

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

/* ---------- Canvas and Campaign Additions ---------- */
.board-canvas {
  display: block;
  width: 100%;
  max-width: var(--board);
  height: auto;
  aspect-ratio: 1 / 1;
  border-radius: 20px;
  touch-action: none;
  cursor: pointer;
}

.primary-accent {
  background: linear-gradient(135deg, #06b6d4, #0891b2) !important;
  color: #082f49 !important;
  font-weight: 800 !important;
  box-shadow: 0 4px 12px rgba(6, 182, 212, 0.35) !important;
}

.primary-accent:hover {
  background: linear-gradient(135deg, #22d3ee, #06b6d4) !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: #06b6d4;
  color: #082f49;
  border-color: #67e8f9;
}

.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: #06b6d4;
  transform: translateY(-2px);
  box-shadow: 0 4px 10px rgba(6, 182, 212, 0.25);
}

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

