:root {
  color-scheme: dark;
  --void: #000604;
  --panel: rgba(0, 16, 12, 0.88);
  --panel-strong: rgba(0, 24, 18, 0.96);
  --green: #00ff9d;
  --green-soft: #59ffc0;
  --cyan: #00f6ff;
  --text: #ddfff4;
  --muted: rgba(205, 255, 239, 0.58);
  --faint: rgba(205, 255, 239, 0.32);
  --line: rgba(0, 255, 157, 0.2);
  --line-strong: rgba(0, 255, 157, 0.46);
  --danger: #ff4f83;
  --display: "Orbitron", sans-serif;
  --mono: "Roboto Mono", monospace;
  --board-size: min(58vmin, 510px);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--void);
  scroll-behavior: smooth;
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  color: var(--text);
  background: var(--void);
  font-family: var(--mono);
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

button,
select,
input {
  color: inherit;
  font: inherit;
}

button,
a,
select {
  -webkit-tap-highlight-color: transparent;
}

button {
  cursor: pointer;
  touch-action: manipulation;
}

a {
  color: inherit;
}

[hidden] {
  display: none !important;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 12px;
  left: 12px;
  padding: 12px 16px;
  color: #00100b;
  background: var(--green);
  font-weight: 700;
  transform: translateY(-180%);
  transition: transform 160ms ease;
}

.skip-link:focus {
  transform: translateY(0);
}

.signal-atmosphere,
.signal-atmosphere > div {
  position: fixed;
  inset: 0;
  pointer-events: none;
}

.signal-atmosphere {
  z-index: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 40%, rgba(0, 255, 157, 0.075), transparent 34rem),
    radial-gradient(circle at 12% 85%, rgba(0, 246, 255, 0.045), transparent 24rem),
    #000604;
}

.signal-grid-plane {
  opacity: 0.3;
  background-image:
    linear-gradient(rgba(0, 255, 157, 0.055) 1px, transparent 1px),
    linear-gradient(90deg, rgba(0, 255, 157, 0.055) 1px, transparent 1px);
  background-size: 56px 56px;
  mask-image: linear-gradient(to bottom, transparent 0%, black 18%, black 82%, transparent 100%);
}

.signal-scan {
  opacity: 0.18;
  background: repeating-linear-gradient(to bottom, transparent 0 3px, rgba(122, 255, 215, 0.08) 4px);
}

.signal-vignette {
  box-shadow: inset 0 0 14vw rgba(0, 0, 0, 0.92);
}

.grid-shell {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.grid-header,
.grid-footer {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-color: var(--line);
  background: rgba(0, 8, 6, 0.84);
  backdrop-filter: blur(18px);
}

.grid-header {
  min-height: 78px;
  padding:
    max(14px, env(safe-area-inset-top))
    max(26px, env(safe-area-inset-right))
    14px
    max(26px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
}

.grid-brand {
  display: inline-flex;
  align-items: center;
  gap: 13px;
  text-decoration: none;
}

.grid-brand > span:last-child {
  display: grid;
  gap: 2px;
}

.grid-brand strong {
  color: var(--text);
  font-family: var(--display);
  font-size: 0.86rem;
  letter-spacing: 0.22em;
}

.grid-brand small {
  color: var(--faint);
  font-size: 0.58rem;
  letter-spacing: 0.2em;
}

.brand-core {
  position: relative;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(0, 255, 157, 0.28);
  border-radius: 50%;
  box-shadow: 0 0 24px rgba(0, 255, 157, 0.1);
}

.brand-core::before,
.brand-core::after {
  content: "";
  position: absolute;
  border: 1px solid rgba(0, 255, 157, 0.18);
  border-radius: inherit;
}

.brand-core::before { inset: 4px; }
.brand-core::after { inset: 9px; }

.brand-core i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 9px var(--green), 0 0 24px rgba(0, 255, 157, 0.8);
}

.grid-system-state {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(0, 255, 157, 0.035);
  font-size: 0.68rem;
  letter-spacing: 0.14em;
}

.grid-system-state > span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 12px var(--green);
}

.grid-system-state[data-state="connecting"] > span {
  animation: signalPulse 850ms ease-in-out infinite alternate;
}

.grid-system-state[data-state="error"] {
  color: #ffc0d2;
  border-color: rgba(255, 79, 131, 0.4);
}

.grid-system-state[data-state="error"] > span {
  background: var(--danger);
  box-shadow: 0 0 12px var(--danger);
}

.grid-main {
  width: min(1480px, 100%);
  margin: 0 auto;
  padding: clamp(24px, 4vw, 58px) max(22px, env(safe-area-inset-right)) clamp(24px, 4vw, 52px) max(22px, env(safe-area-inset-left));
  display: grid;
  align-items: center;
}

.mode-screen {
  width: 100%;
  display: grid;
  gap: clamp(28px, 4vw, 48px);
}

.mode-heading {
  max-width: 880px;
}

.mode-back {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 38px;
  margin: 0 0 18px;
  padding: 0 13px;
  border: 1px solid rgba(205, 255, 239, 0.16);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(0, 18, 13, 0.62);
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-decoration: none;
  transition: color 160ms ease, border-color 160ms ease, background 160ms ease, transform 160ms ease;
}

.mode-back span {
  color: var(--green);
  font-size: 0.9rem;
  transition: transform 160ms ease;
}

.mode-back:hover,
.mode-back:focus-visible {
  border-color: var(--line-strong);
  color: var(--text);
  background: rgba(0, 255, 157, 0.07);
  outline: 0;
}

.mode-back:hover span,
.mode-back:focus-visible span { transform: translateX(-3px); }

.grid-eyebrow {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 14px;
  color: var(--green);
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.19em;
}

.grid-eyebrow span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 14px var(--green);
}

.mode-heading h1 {
  margin: 0;
  color: var(--text);
  font-family: var(--display);
  font-size: clamp(3rem, 8.2vw, 8rem);
  line-height: 0.88;
  letter-spacing: -0.055em;
  text-shadow: 0 0 42px rgba(0, 255, 157, 0.14);
}

.mode-heading h1 span {
  color: transparent;
  -webkit-text-stroke: 1px rgba(215, 255, 243, 0.72);
}

.mode-heading > p:last-child {
  max-width: 660px;
  margin: 22px 0 0;
  color: var(--muted);
  font-size: clamp(0.9rem, 1.5vw, 1.08rem);
  line-height: 1.8;
}

.mode-selector {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px;
}

.mode-card {
  position: relative;
  min-height: 295px;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr auto;
  gap: 18px 20px;
  padding: clamp(20px, 2.4vw, 32px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 2px 22px 2px 22px;
  background:
    linear-gradient(145deg, rgba(0, 255, 157, 0.055), transparent 50%),
    rgba(0, 15, 11, 0.82);
  box-shadow: inset 0 1px rgba(220, 255, 244, 0.025), 0 22px 50px rgba(0, 0, 0, 0.2);
  transition: transform 220ms ease, border-color 220ms ease, box-shadow 220ms ease;
}

.mode-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: -45%;
  width: 34%;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--green), transparent);
  opacity: 0;
  transition: left 520ms ease, opacity 220ms ease;
}

.mode-index {
  color: rgba(0, 255, 157, 0.3);
  font-family: var(--display);
  font-size: 1.1rem;
}

.mode-copy {
  min-width: 0;
}

.mode-copy > span,
.mode-copy label {
  display: block;
  color: var(--green);
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0.17em;
}

.mode-copy h2 {
  margin: 12px 0 13px;
  font-family: var(--display);
  font-size: clamp(1.15rem, 2vw, 1.65rem);
  line-height: 1.18;
}

.mode-copy p {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.7;
}

.mode-copy label {
  margin-top: 22px;
  color: var(--faint);
}

.mode-copy select {
  width: 100%;
  min-height: 42px;
  margin-top: 8px;
  padding: 0 36px 0 12px;
  border: 1px solid var(--line);
  border-radius: 0;
  outline: 0;
  color: var(--text);
  background: #00120d;
  font-size: 0.75rem;
}

.mode-action {
  grid-column: 1 / -1;
  min-height: 51px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border: 1px solid rgba(0, 255, 157, 0.26);
  color: var(--green-soft);
  background: rgba(0, 255, 157, 0.04);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  transition: color 160ms ease, background 160ms ease, border-color 160ms ease;
}

.mode-action i {
  font-size: 1.1rem;
  font-style: normal;
  transition: transform 160ms ease;
}

.mode-card--private[data-availability="checking"],
.mode-card--private[data-availability="disabled"] {
  border-color: rgba(220, 255, 244, 0.1);
  background: rgba(4, 13, 10, 0.74);
  box-shadow: none;
}

.mode-card--private[data-availability="checking"] .mode-copy,
.mode-card--private[data-availability="checking"] .mode-index,
.mode-card--private[data-availability="disabled"] .mode-copy,
.mode-card--private[data-availability="disabled"] .mode-index {
  opacity: 0.48;
}

.mode-card--private .mode-action:disabled {
  cursor: not-allowed;
  border-color: rgba(220, 255, 244, 0.12);
  color: rgba(220, 255, 244, 0.42);
  background: rgba(220, 255, 244, 0.025);
}

.mode-note {
  margin: -14px 0 0;
  color: var(--faint);
  font-size: 0.7rem;
  line-height: 1.6;
  text-align: right;
}

.mode-note a {
  color: var(--green);
  text-underline-offset: 3px;
}

.arena {
  width: 100%;
  animation: arenaIn 420ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.arena-header {
  min-height: 70px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 18px;
  margin-bottom: clamp(18px, 3vh, 34px);
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.arena-title {
  text-align: center;
}

.arena-title > span {
  color: var(--green);
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.17em;
}

.arena-actions {
  justify-self: end;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}

.arena-header h2 {
  margin: 5px 0 0;
  font-family: var(--display);
  font-size: 1.25rem;
  letter-spacing: 0.08em;
}

.text-button,
.sound-toggle,
.room-code-button {
  justify-self: start;
  min-height: 44px;
  padding: 0;
  border: 0;
  color: var(--muted);
  background: transparent;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.12em;
}

.sound-toggle,
.room-code-button {
  justify-self: end;
}

.room-code-button {
  min-width: 130px;
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  text-align: right;
}

.room-code-button small {
  color: var(--faint);
  font-size: 0.52rem;
  letter-spacing: 0.16em;
}

.room-code-button strong {
  color: var(--green);
  font-size: 0.9rem;
  letter-spacing: 0.2em;
}

.match-layout {
  display: grid;
  grid-template-columns: minmax(150px, 1fr) auto minmax(150px, 1fr);
  align-items: center;
  gap: clamp(22px, 4vw, 72px);
}

.player-node {
  min-height: 126px;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 16px;
  padding: 18px;
  border: 1px solid rgba(207, 255, 241, 0.11);
  background: rgba(0, 15, 11, 0.62);
  opacity: 0.56;
  transition: opacity 220ms ease, border-color 220ms ease, box-shadow 220ms ease, transform 220ms ease;
}

.player-node--x { border-radius: 2px 18px 2px 2px; }
.player-node--o { border-radius: 18px 2px 2px 2px; }

.player-node[data-active="true"] {
  opacity: 1;
  border-color: rgba(0, 255, 157, 0.42);
  box-shadow: 0 0 34px rgba(0, 255, 157, 0.08), inset 0 0 26px rgba(0, 255, 157, 0.025);
  transform: translateY(-3px);
}

.player-node--o[data-active="true"] {
  border-color: rgba(0, 246, 255, 0.42);
  box-shadow: 0 0 34px rgba(0, 246, 255, 0.08), inset 0 0 26px rgba(0, 246, 255, 0.025);
}

.player-mark {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--green);
  font-family: var(--display);
  font-size: 1.4rem;
  text-shadow: 0 0 14px currentColor;
}

.player-node--o .player-mark {
  color: var(--cyan);
  border-color: rgba(0, 246, 255, 0.25);
}

.player-node > div:last-child {
  min-width: 0;
  display: grid;
  gap: 4px;
}

.player-node small,
.player-node span {
  color: var(--faint);
  font-size: 0.55rem;
  letter-spacing: 0.15em;
}

.player-node strong {
  overflow: hidden;
  font-family: var(--display);
  font-size: clamp(0.72rem, 1.4vw, 0.95rem);
  letter-spacing: 0.08em;
  text-overflow: ellipsis;
}

.board-stage {
  width: var(--board-size);
  display: grid;
  justify-items: center;
  gap: 15px;
}

.turn-status {
  width: 100%;
  min-height: 48px;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  column-gap: 10px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  background: rgba(0, 20, 14, 0.7);
}

.turn-status strong {
  font-family: var(--display);
  font-size: 0.74rem;
  letter-spacing: 0.12em;
}

.turn-status small {
  grid-column: 2;
  color: var(--faint);
  font-size: 0.55rem;
  letter-spacing: 0.13em;
}

.turn-pulse {
  grid-row: 1 / 3;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 12px var(--green);
  animation: signalPulse 900ms ease-in-out infinite alternate;
}

.turn-status[data-state="waiting"] .turn-pulse { background: var(--cyan); box-shadow: 0 0 12px var(--cyan); }
.turn-status[data-state="complete"] .turn-pulse { animation: none; }
.turn-status[data-state="error"] .turn-pulse { background: var(--danger); box-shadow: 0 0 12px var(--danger); }
.turn-status[data-state="request"] {
  border-color: rgba(0, 246, 255, 0.48);
  background: rgba(0, 246, 255, 0.065);
  box-shadow: 0 0 26px rgba(0, 246, 255, 0.08);
}
.turn-status[data-state="request"] .turn-pulse { background: var(--cyan); box-shadow: 0 0 14px var(--cyan); }

.game-board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: clamp(6px, 1.1vmin, 10px);
  padding: clamp(7px, 1.2vmin, 12px);
  border: 1px solid var(--line-strong);
  border-radius: 4px 28px 4px 28px;
  background:
    radial-gradient(circle at center, rgba(0, 255, 157, 0.06), transparent 58%),
    rgba(0, 12, 9, 0.92);
  box-shadow: 0 0 60px rgba(0, 255, 157, 0.09), inset 0 0 45px rgba(0, 0, 0, 0.5);
  isolation: isolate;
}

.game-board::before,
.game-board::after {
  content: "";
  position: absolute;
  z-index: -1;
  border: 1px solid rgba(0, 255, 157, 0.09);
  border-radius: inherit;
  pointer-events: none;
}

.game-board::before { inset: -8px; }
.game-board::after { inset: -16px; opacity: 0.5; }

.grid-cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid rgba(0, 255, 157, 0.16);
  border-radius: 2px 12px 2px 12px;
  outline: 0;
  background:
    linear-gradient(135deg, rgba(0, 255, 157, 0.04), transparent 62%),
    rgba(0, 20, 14, 0.78);
  transition: border-color 140ms ease, background 140ms ease, box-shadow 140ms ease, transform 140ms ease;
}

.grid-cell::before,
.grid-cell::after {
  content: "";
  position: absolute;
  opacity: 0;
  transition: opacity 140ms ease;
}

.grid-cell[data-mark="X"]::before,
.grid-cell[data-mark="X"]::after {
  width: 56%;
  height: clamp(3px, 0.75vmin, 6px);
  border-radius: 999px;
  background: var(--green);
  box-shadow: 0 0 14px rgba(0, 255, 157, 0.9), 0 0 30px rgba(0, 255, 157, 0.28);
  opacity: 1;
}

.grid-cell[data-mark="X"]::before { transform: rotate(45deg); }
.grid-cell[data-mark="X"]::after { transform: rotate(-45deg); }

.grid-cell[data-mark="O"]::before {
  width: 48%;
  aspect-ratio: 1;
  border: clamp(3px, 0.75vmin, 6px) solid var(--cyan);
  border-radius: 50%;
  box-shadow: 0 0 14px rgba(0, 246, 255, 0.8), inset 0 0 14px rgba(0, 246, 255, 0.24);
  opacity: 1;
}

.grid-cell.is-new[data-mark="X"]::before,
.grid-cell.is-new[data-mark="X"]::after {
  animation: markXIn 320ms cubic-bezier(0.2, 0.9, 0.2, 1.25) both;
}

.grid-cell.is-new[data-mark="O"]::before {
  animation: markOIn 360ms cubic-bezier(0.2, 0.9, 0.2, 1.2) both;
}

.grid-cell.is-winning {
  border-color: rgba(0, 255, 157, 0.78);
  background: rgba(0, 255, 157, 0.105);
  box-shadow: inset 0 0 28px rgba(0, 255, 157, 0.08), 0 0 26px rgba(0, 255, 157, 0.12);
}

.grid-cell:disabled {
  cursor: default;
}

.grid-cell:focus-visible {
  border-color: var(--text);
  box-shadow: 0 0 0 2px #000, 0 0 0 4px var(--green);
}

.win-beam {
  position: absolute;
  z-index: 5;
  top: 0;
  left: 0;
  width: 0;
  height: 2px;
  pointer-events: none;
  background: linear-gradient(90deg, transparent, var(--green) 12%, #effff9 50%, var(--green) 88%, transparent);
  box-shadow: 0 0 9px var(--green), 0 0 24px rgba(0, 255, 157, 0.8);
  opacity: 0;
  transform-origin: 0 50%;
}

.win-beam.is-visible {
  opacity: 1;
  animation: beamIn 480ms cubic-bezier(0.2, 0.9, 0.2, 1) both;
}

.board-sweep {
  position: absolute;
  z-index: 4;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  opacity: 0;
}

.board-sweep::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(to bottom, transparent 0%, rgba(0, 255, 157, 0.08) 42%, rgba(0, 255, 157, 0.34) 50%, rgba(0, 255, 157, 0.08) 58%, transparent 100%);
  transform: translateY(-105%);
}

.game-board.is-resolving .board-sweep {
  opacity: 1;
}

.game-board.is-resolving .board-sweep::before {
  animation: boardSweep 700ms ease-out both;
}

.coin-flip {
  position: absolute;
  z-index: 8;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 13px;
  padding: 24px;
  border-radius: inherit;
  overflow: hidden;
  text-align: center;
  background:
    radial-gradient(circle at center, rgba(0, 255, 157, 0.18), transparent 43%),
    rgba(0, 10, 7, 0.94);
  backdrop-filter: blur(7px);
}

.coin-flip::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, transparent 0 4px, rgba(0, 255, 157, 0.06) 5px);
}

.signal-coin {
  position: relative;
  width: clamp(72px, 18vmin, 118px);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  transform-style: preserve-3d;
}

.signal-coin::before,
.signal-coin::after {
  content: "";
  position: absolute;
  border: 1px solid rgba(0, 255, 157, 0.32);
  border-radius: 50%;
}

.signal-coin::before { inset: -9px; }
.signal-coin::after { inset: -17px; opacity: 0.45; }

.signal-coin i {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border: 2px solid var(--green);
  border-radius: 50%;
  color: var(--text);
  background: radial-gradient(circle, rgba(0, 255, 157, 0.19), rgba(0, 20, 14, 0.98) 68%);
  box-shadow: 0 0 28px rgba(0, 255, 157, 0.24), inset 0 0 22px rgba(0, 255, 157, 0.1);
  font-family: var(--display);
  font-size: clamp(1.75rem, 6vmin, 3.4rem);
  font-style: normal;
  backface-visibility: hidden;
}

.signal-coin i:last-child {
  color: var(--cyan);
  border-color: var(--cyan);
  transform: rotateY(180deg);
}

.coin-flip[data-phase="flipping"] .signal-coin {
  animation: signalCoinFlip 1.25s cubic-bezier(0.18, 0.7, 0.2, 1) both;
}

.coin-flip[data-result="O"] .signal-coin {
  transform: rotateY(180deg);
}

.coin-flip strong,
.coin-flip small {
  position: relative;
  z-index: 1;
}

.coin-flip strong {
  color: var(--green-soft);
  font-family: var(--display);
  font-size: clamp(0.9rem, 2.4vmin, 1.25rem);
  letter-spacing: 0.14em;
}

.coin-flip small {
  color: var(--muted);
  font-size: clamp(0.58rem, 1.6vmin, 0.72rem);
  letter-spacing: 0.12em;
}

.coin-flip[data-phase="result"] {
  animation: flipResolve 240ms ease both;
}

.match-controls {
  position: relative;
  z-index: 20;
  width: 100%;
  min-height: 46px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.primary-control {
  position: relative;
  z-index: 1;
  pointer-events: auto;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  min-width: 142px;
  min-height: 44px;
  padding: 0 17px;
  border: 1px solid var(--line-strong);
  color: var(--green-soft);
  background: rgba(0, 255, 157, 0.06);
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.13em;
}

.primary-control[data-requested="true"] {
  color: var(--cyan);
  border-color: rgba(0, 246, 255, 0.4);
}

.primary-control[data-incoming="true"] {
  color: #00120c;
  border-color: var(--cyan);
  background: var(--cyan);
  box-shadow: 0 0 18px rgba(0, 246, 255, 0.32);
  animation: requestGlow 850ms ease-in-out infinite alternate;
}

.primary-control:disabled {
  cursor: default;
  opacity: 0.52;
}

.primary-control:not(:disabled):active {
  filter: brightness(1.3);
}

.match-meta {
  display: grid;
  justify-items: end;
  gap: 4px;
}

.match-meta > span {
  color: var(--faint);
  font-size: 0.6rem;
  letter-spacing: 0.14em;
}

.match-score {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  color: var(--muted);
  font-size: 0.56rem;
  letter-spacing: 0.1em;
  white-space: nowrap;
}

.match-score span[data-score="primary"] strong { color: var(--green-soft); }
.match-score span[data-score="secondary"] strong { color: var(--danger); }
.match-score b { font-weight: 600; }
.match-score strong { font-family: var(--display); font-size: 0.68rem; }
.match-score i { color: rgba(0, 246, 255, 0.38); font-style: normal; }

.room-transmission {
  width: min(620px, 100%);
  min-height: 43px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: clamp(18px, 3vh, 32px) auto 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.64rem;
  letter-spacing: 0.12em;
}

.room-transmission span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 12px var(--green);
}

.room-transmission p { margin: 0; }
.room-transmission[data-state="waiting"] span { background: var(--cyan); box-shadow: 0 0 12px var(--cyan); animation: signalPulse 850ms ease-in-out infinite alternate; }
.room-transmission[data-state="offline"] span { background: var(--danger); box-shadow: 0 0 12px var(--danger); }
.room-transmission[data-state="request"] {
  color: var(--text);
  border-color: rgba(0, 246, 255, 0.42);
  background: rgba(0, 246, 255, 0.045);
}
.room-transmission[data-state="request"] span { background: var(--cyan); box-shadow: 0 0 14px var(--cyan); animation: signalPulse 720ms ease-in-out infinite alternate; }

.grid-footer {
  min-height: 52px;
  padding:
    10px
    max(26px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom))
    max(26px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  color: var(--faint);
  font-size: 0.58rem;
  letter-spacing: 0.12em;
}

.grid-footer nav {
  display: flex;
  gap: 20px;
}

.grid-footer a {
  color: var(--muted);
  text-decoration: none;
}

.room-dialog {
  width: min(540px, calc(100% - 28px));
  max-height: calc(100dvh - 28px);
  padding: 0;
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 3px 24px 3px 24px;
  color: var(--text);
  background: rgba(0, 16, 12, 0.98);
  box-shadow: 0 40px 100px rgba(0, 0, 0, 0.74), 0 0 50px rgba(0, 255, 157, 0.08);
}

.room-dialog::backdrop {
  background: rgba(0, 4, 3, 0.82);
  backdrop-filter: blur(10px);
}

.room-dialog[open] {
  animation: dialogIn 240ms cubic-bezier(0.2, 0.8, 0.2, 1) both;
}

.dialog-scan {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.22;
  background: repeating-linear-gradient(to bottom, transparent 0 4px, rgba(0, 255, 157, 0.045) 5px);
}

.room-dialog > header,
.room-create,
.room-join,
.dialog-status,
.dialog-privacy {
  position: relative;
  z-index: 1;
}

.room-dialog > header {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  padding: 24px 26px 20px;
  border-bottom: 1px solid var(--line);
}

.room-dialog > header span,
.room-join label {
  color: var(--green);
  font-size: 0.59rem;
  font-weight: 700;
  letter-spacing: 0.17em;
}

.room-dialog h2 {
  margin: 6px 0 0;
  font-family: var(--display);
  font-size: 1.45rem;
  letter-spacing: 0.08em;
}

#closeRoomDialog {
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: transparent;
  font-size: 1.5rem;
}

.room-create,
.room-join {
  padding: 24px 26px;
}

.room-create p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.65;
}

.dialog-primary,
.room-join button {
  min-height: 50px;
  border: 1px solid var(--line-strong);
  color: #00120c;
  background: var(--green);
  font-size: 0.67rem;
  font-weight: 700;
  letter-spacing: 0.13em;
}

.dialog-primary {
  width: 100%;
}

.dialog-divider {
  position: relative;
  display: grid;
  place-items: center;
  color: var(--faint);
  font-size: 0.58rem;
  letter-spacing: 0.15em;
}

.dialog-divider::before {
  content: "";
  position: absolute;
  left: 26px;
  right: 26px;
  height: 1px;
  background: var(--line);
}

.dialog-divider span {
  position: relative;
  z-index: 1;
  padding: 0 12px;
  background: #00100c;
}

.room-join label {
  display: block;
  margin-bottom: 9px;
  color: var(--faint);
}

.room-join > div {
  display: grid;
  grid-template-columns: 1fr auto;
}

.room-join input {
  min-width: 0;
  height: 54px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-right: 0;
  border-radius: 0;
  outline: 0;
  color: var(--text);
  background: rgba(0, 5, 4, 0.72);
  font-family: var(--display);
  font-size: 1.05rem;
  letter-spacing: 0.24em;
  text-transform: uppercase;
}

.room-join input:focus {
  border-color: var(--green);
  box-shadow: inset 0 0 0 1px var(--green);
}

.room-join button {
  min-width: 92px;
  height: 54px;
}

.dialog-status {
  margin: 0 26px 14px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  color: var(--muted);
  background: rgba(0, 255, 157, 0.035);
  font-size: 0.7rem;
  line-height: 1.5;
}

.dialog-status[data-state="working"] { color: var(--green-soft); }
.dialog-status[data-state="error"] { color: #ffc0d2; border-color: rgba(255, 79, 131, 0.38); }

.dialog-privacy {
  display: block;
  padding: 0 26px 26px;
  color: var(--faint);
  font-size: 0.62rem;
  line-height: 1.6;
}

.grid-toast {
  position: fixed;
  z-index: 90;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  max-width: min(360px, calc(100% - 36px));
  padding: 13px 16px;
  border: 1px solid var(--line-strong);
  color: var(--text);
  background: rgba(0, 20, 14, 0.96);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.45);
  font-size: 0.7rem;
  line-height: 1.5;
  animation: toastIn 220ms ease both;
}

.noscript-panel {
  max-width: 620px;
  margin: auto;
  padding: 34px;
  border: 1px solid var(--line);
  background: var(--panel);
  text-align: center;
}

.noscript-panel h1 { font-family: var(--display); }
.noscript-panel p { color: var(--muted); }
.noscript-panel a { color: var(--green); }
.no-js .mode-screen { display: none; }

@media (hover: hover) and (pointer: fine) {
  .mode-card:hover {
    border-color: var(--line-strong);
    box-shadow: 0 28px 70px rgba(0, 0, 0, 0.28), 0 0 34px rgba(0, 255, 157, 0.045);
    transform: translateY(-4px);
  }

  .mode-card:hover::before { left: 110%; opacity: 1; }
  .mode-action:hover,
  .primary-control:hover { color: #00120c; background: var(--green); border-color: var(--green); }
  .mode-action:hover i { transform: translateX(4px); }
  .grid-cell:not(:disabled):hover { border-color: rgba(0, 255, 157, 0.55); background: rgba(0, 255, 157, 0.08); transform: scale(0.975); }
  .text-button:hover,
  .sound-toggle:hover,
  .grid-footer a:hover { color: var(--green); }
}

@media (max-width: 1050px) {
  :root { --board-size: min(54vw, 470px); }

  .match-layout {
    grid-template-columns: minmax(120px, 1fr) auto minmax(120px, 1fr);
    gap: 24px;
  }

  .player-node {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
  }
}

@media (max-width: 820px) {
  :root { --board-size: min(84vw, 52dvh, 440px); }

  .grid-main {
    align-items: start;
    padding-top: 26px;
  }

  .mode-selector {
    grid-template-columns: 1fr;
  }

  .mode-card {
    min-height: 0;
    grid-template-columns: 42px 1fr auto;
    grid-template-rows: auto;
    align-items: center;
    padding: 20px;
  }

  .mode-action {
    grid-column: auto;
    width: 52px;
    min-width: 52px;
    padding: 0;
    justify-content: center;
  }

  .mode-action span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  .mode-copy h2 { margin: 8px 0; }
  .mode-copy label { margin-top: 14px; }
  .mode-note { margin-top: -8px; text-align: left; }

  .arena-header {
    grid-template-columns: 1fr auto 1fr;
    margin-bottom: 16px;
  }

  .match-layout {
    grid-template-columns: 1fr 1fr;
    gap: 12px;
  }

  .player-node {
    min-height: 68px;
    grid-template-columns: auto 1fr;
    justify-items: initial;
    padding: 10px 12px;
    text-align: left;
  }

  .player-mark {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }

  .board-stage {
    grid-column: 1 / -1;
    grid-row: 2;
    margin: 0 auto;
    gap: 11px;
  }

  .room-transmission { margin-top: 16px; }
}

@media (max-width: 620px) {
  :root { --board-size: min(88vw, 51dvh, 390px); }

  .grid-header {
    min-height: 70px;
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }

  .grid-brand strong { font-size: 0.72rem; }
  .grid-brand small { display: none; }
  .brand-core { width: 34px; height: 34px; }

  .grid-system-state {
    min-width: 42px;
    width: 42px;
    padding: 0;
    justify-content: center;
  }

  .grid-system-state strong {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }

  .grid-main {
    padding-left: max(14px, env(safe-area-inset-left));
    padding-right: max(14px, env(safe-area-inset-right));
  }

  .mode-heading h1 { font-size: clamp(3.25rem, 17vw, 5rem); }
  .mode-heading > p:last-child { margin-top: 16px; line-height: 1.65; }
  .mode-screen { gap: 25px; }

  .mode-card {
    grid-template-columns: 34px 1fr 48px;
    gap: 12px;
    border-radius: 2px 16px 2px 16px;
  }

  .mode-copy p { font-size: 0.72rem; }
  .mode-copy select { min-height: 44px; font-size: 0.69rem; }

  .arena-header {
    grid-template-columns: 44px 1fr 96px;
    gap: 8px;
  }

  .arena-actions {
    width: 96px;
    display: grid;
    gap: 4px;
    justify-items: end;
  }

  .text-button {
    width: 44px;
    overflow: hidden;
    color: transparent;
  }

  .text-button::before {
    content: "←";
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    color: var(--muted);
    border: 1px solid var(--line);
    font-size: 1rem;
  }

  .arena-header h2 { font-size: 1rem; }
  .sound-toggle { max-width: 96px; font-size: 0.58rem; text-align: right; }
  .room-code-button { min-width: 96px; }
  .room-code-button strong { font-size: 0.75rem; }

  .player-node {
    min-width: 0;
    gap: 9px;
  }

  .player-node strong { font-size: 0.67rem; }
  .player-node span { font-size: 0.48rem; }

  .turn-status { min-height: 44px; }
  .turn-status strong { font-size: 0.65rem; }
  .turn-status small { font-size: 0.48rem; }

  .game-board {
    gap: 6px;
    padding: 7px;
    border-radius: 3px 20px 3px 20px;
  }

  .match-controls { min-height: 44px; }
  .primary-control { min-width: 134px; }

  .grid-footer > span { display: none; }
  .grid-footer { justify-content: center; }

  .room-dialog > header,
  .room-create,
  .room-join { padding-left: 20px; padding-right: 20px; }
  .dialog-divider::before { left: 20px; right: 20px; }
  .dialog-status { margin-left: 20px; margin-right: 20px; }
  .dialog-privacy { padding-left: 20px; padding-right: 20px; }
}

@media (max-height: 720px) and (max-width: 820px) {
  :root { --board-size: min(78vw, 42dvh); }
  .grid-main { padding-top: 14px; padding-bottom: 14px; }
  .arena-header { min-height: 54px; padding-bottom: 10px; margin-bottom: 10px; }
  .player-node { min-height: 56px; padding-top: 6px; padding-bottom: 6px; }
  .player-mark { width: 34px; height: 34px; }
  .board-stage { gap: 7px; }
  .room-transmission { min-height: 35px; margin-top: 9px; }
}

/* Active matches behave like a contained game surface. The mode selector
   remains a normal document, while gameplay is locked to the stable mobile
   viewport so browser chrome and match animations cannot create page scroll. */
html.is-playing {
  --game-viewport-height: 100vh;
  height: 100vh;
  height: 100svh;
  overflow: hidden;
  overscroll-behavior: none;
}

@supports (height: 100svh) {
  html.is-playing {
    --game-viewport-height: 100svh;
  }
}

html.is-playing body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100vh;
  height: 100svh;
  min-height: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

html.is-playing .grid-shell {
  height: 100%;
  min-height: 0;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

html.is-playing .grid-main {
  height: 100%;
  min-height: 0;
  padding-top: clamp(10px, 2vh, 22px);
  padding-bottom: clamp(10px, 2vh, 22px);
  overflow: hidden;
  align-items: stretch;
}

html.is-playing .grid-footer {
  display: none;
}

html.is-playing .arena {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

html.is-playing .arena-header {
  margin-bottom: clamp(8px, 1.6vh, 18px);
}

html.is-playing .match-layout {
  height: 100%;
  min-height: 0;
  align-content: center;
}

html.is-playing .board-stage {
  --board-size: min(58vmin, calc(var(--game-viewport-height) - 330px), 510px);
  gap: clamp(8px, 1.4vh, 15px);
}

html.is-playing[data-signal-grid-mode="online"] .board-stage {
  --board-size: min(58vmin, calc(var(--game-viewport-height) - 390px), 510px);
}

@media (max-width: 820px) {
  html.is-playing .grid-header {
    min-height: 62px;
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: 8px;
  }

  html.is-playing .grid-main {
    padding-top: 8px;
    padding-bottom: 8px;
  }

  html.is-playing .arena-header {
    min-height: 52px;
    margin-bottom: 6px;
    padding-bottom: 6px;
  }

  html.is-playing .match-layout {
    gap: 8px;
  }

  html.is-playing .player-node {
    min-height: 52px;
    padding-top: 6px;
    padding-bottom: 6px;
  }

  html.is-playing .board-stage {
    --board-size: min(88vw, calc(var(--game-viewport-height) - 300px), 390px);
    gap: 8px;
  }

  html.is-playing[data-signal-grid-mode="online"] .board-stage {
    --board-size: min(88vw, calc(var(--game-viewport-height) - 340px), 390px);
  }

  html.is-playing .turn-status {
    min-height: 40px;
    padding-top: 6px;
    padding-bottom: 6px;
  }

  html.is-playing .match-controls {
    min-height: 38px;
  }

  html.is-playing .room-transmission {
    min-height: 34px;
    margin-top: 6px;
  }
}

@media (max-height: 720px) {
  html.is-playing .grid-header {
    min-height: 54px;
    padding-top: max(6px, env(safe-area-inset-top));
    padding-bottom: 6px;
  }

  html.is-playing .grid-main {
    padding-top: 6px;
    padding-bottom: 6px;
  }

  html.is-playing .arena-header {
    min-height: 46px;
    margin-bottom: 5px;
    padding-bottom: 5px;
  }

  html.is-playing .match-layout {
    gap: 6px;
  }

  html.is-playing .player-node {
    min-height: 48px;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  html.is-playing .board-stage {
    --board-size: min(54vmin, calc(var(--game-viewport-height) - 190px), 390px);
    gap: 6px;
  }

  html.is-playing[data-signal-grid-mode="online"] .board-stage {
    --board-size: min(54vmin, calc(var(--game-viewport-height) - 220px), 390px);
  }

  html.is-playing .turn-status,
  html.is-playing .match-controls {
    min-height: 36px;
  }

  html.is-playing .room-transmission {
    min-height: 30px;
    margin-top: 4px;
  }
}

@media (max-width: 820px) and (max-height: 720px) {
  html.is-playing .board-stage {
    --board-size: min(78vw, 42vh, calc(var(--game-viewport-height) - 270px), 390px);
  }

  html.is-playing[data-signal-grid-mode="online"] .board-stage {
    --board-size: min(78vw, 42vh, calc(var(--game-viewport-height) - 300px), 390px);
  }
}

@media (orientation: landscape) and (max-height: 540px) {
  html.is-playing .grid-header {
    min-height: 46px;
  }

  html.is-playing .grid-main {
    padding-top: 4px;
    padding-bottom: 4px;
  }

  html.is-playing .arena-header {
    min-height: 42px;
    margin-bottom: 4px;
    padding-bottom: 4px;
  }

  html.is-playing .match-layout {
    grid-template-columns: minmax(88px, 1fr) auto minmax(88px, 1fr);
    gap: 8px;
  }

  html.is-playing .board-stage {
    --board-size: min(44vw, calc(var(--game-viewport-height) - 190px), 330px);
    grid-column: auto;
    grid-row: auto;
    gap: 5px;
    margin: 0 auto;
  }

  html.is-playing[data-signal-grid-mode="online"] .board-stage {
    --board-size: min(44vw, calc(var(--game-viewport-height) - 220px), 330px);
  }

  html.is-playing .player-node {
    min-height: 44px;
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 5px;
    padding: 5px;
    text-align: center;
  }

  html.is-playing .player-mark {
    width: 32px;
    height: 32px;
    font-size: 0.9rem;
  }

  html.is-playing .turn-status,
  html.is-playing .match-controls {
    min-height: 34px;
  }

  html.is-playing .room-transmission {
    min-height: 26px;
    margin-top: 2px;
  }
}

/* Extremely short or heavily zoomed viewports retain access to every control
   through the arena itself without returning the document to page scrolling. */
@media (max-height: 340px) {
  html.is-playing .grid-main {
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  html.is-playing .arena {
    height: auto;
    min-height: 100%;
    overflow: visible;
  }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}

@keyframes signalPulse {
  from { opacity: 0.45; transform: scale(0.82); }
  to { opacity: 1; transform: scale(1.1); }
}

@keyframes arenaIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes markXIn {
  from { opacity: 0; scale: 0.3 1; }
  to { opacity: 1; scale: 1 1; }
}

@keyframes markOIn {
  from { opacity: 0; scale: 0.25; rotate: -70deg; }
  to { opacity: 1; scale: 1; rotate: 0deg; }
}

@keyframes beamIn {
  from { clip-path: inset(0 100% 0 0); filter: brightness(2); }
  to { clip-path: inset(0); filter: brightness(1); }
}

@keyframes boardSweep {
  from { transform: translateY(-105%); }
  to { transform: translateY(105%); }
}

@keyframes signalCoinFlip {
  0% { transform: translateY(0) rotateY(0deg) rotateZ(0deg) scale(0.84); }
  42% { transform: translateY(-15%) rotateY(900deg) rotateZ(12deg) scale(1.08); }
  100% { transform: translateY(0) rotateY(1800deg) rotateZ(0deg) scale(1); }
}

@keyframes flipResolve {
  from { filter: brightness(1.8); }
  to { filter: brightness(1); }
}

@keyframes requestGlow {
  from { box-shadow: 0 0 10px rgba(0, 246, 255, 0.22); }
  to { box-shadow: 0 0 28px rgba(0, 246, 255, 0.52); }
}

@keyframes dialogIn {
  from { opacity: 0; transform: translateY(14px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
