.game-head {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}
.game-head div {
  display: flex;
  flex: 1;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
.game-head button {
  font-size: 24px;
  color: var(--muted);
}
.game-head .eyebrow {
  margin: 0;
  white-space: nowrap;
}
.game-head h2,
.game-head h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: -1px;
}
.game-head h1 {
  font-size: 29px;
}
.game-head h2 {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.game-meta {
  display: grid;
  grid-template-columns: auto auto minmax(0, 1fr);
  justify-content: stretch;
  align-items: end;
  gap: 12px;
  margin: 12px 0 10px;
}
.game-meta small,
.game-meta p {
  font: 10px monospace;
  color: var(--muted);
  line-height: 1.8;
}
.game-meta > div:first-child,
.game-meta > div:nth-child(2) {
  min-width: 58px;
}
.game-meta strong {
  display: block;
  font-size: 32px;
}
#gameScore {
  color: var(--coral);
}
.preview {
  height: 42px;
  background: #ebe9e2;
  display: grid;
  place-items: center;
  font: 24px monospace;
  font-weight: bold;
  color: var(--ink);
  margin-bottom: 8px;
}
.preview.found {
  background: var(--lime);
  color: var(--ink);
  font-size: 24px;
}
.chain-status {
  display: grid;
  gap: 3px;
  margin: -4px 0 9px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper) 88%, #b9f2dc);
  font: 11px monospace;
}
.chain-status[hidden] {
  display: none;
}
.chain-status div {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.chain-status span {
  color: var(--muted);
}
.chain-status strong {
  overflow-wrap: anywhere;
  text-align: right;
}
.chain-status p {
  margin: 2px 0 0;
  color: var(--coral);
  overflow-wrap: anywhere;
}
.challenge-status {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin: -4px 0 9px;
  padding: 7px 10px;
  border: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper) 88%, #ffd4e3);
  font: 11px monospace;
}
.challenge-status[hidden] {
  display: none;
}
.challenge-status div {
  display: grid;
  gap: 2px;
}
.challenge-status span,
.challenge-status small {
  color: var(--muted);
}
.challenge-status strong {
  overflow-wrap: anywhere;
}
.challenge-status button {
  align-self: center;
  min-height: 32px;
  padding: 4px 8px;
  font-size: 10px;
}
.board {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
  background:
    radial-gradient(circle at 10% 8%, #5f4b9b 0 2px, transparent 3px),
    radial-gradient(circle at 88% 18%, #2dd4bf 0 2px, transparent 3px),
    linear-gradient(135deg, #25234c, #3c2a67 52%, #1d4d67);
  border-radius: 20px;
  padding: 10px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  width: min(100%, calc(100svh - 420px), calc(100vw - 56px));
  height: auto;
  aspect-ratio: 1 / 1;
  flex: 0 0 auto;
  max-width: 100%;
  max-height: 100%;
  position: relative;
  z-index: 1;
  grid-auto-rows: 1fr;
}
.tile {
  aspect-ratio: 1;
  background: #f7dfe4;
  display: grid;
  place-items: center;
  font-size: 26px;
  font-weight: bold;
  user-select: none;
  transition:
    background-color 0.2s ease,
    color 0.2s ease;
}
.tile.selected {
  animation: tile-selected 0.38s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  transform: scale(1);
  transition: transform 0.08s ease;
  background-color: #45f27d;
  color: #1d1d1b;
}
.game-foot {
  display: flex;
  justify-content: space-between;
  padding-top: 12px;
  padding-bottom: 4px;
  color: var(--muted);
  font: 11px monospace;
}

.round-intro-screen {
  min-height: calc(100svh - 86px);
  padding-top: 22px;
  text-align: center;
  overflow: hidden;
}
.intro-orbit {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
  min-height: 112px;
  margin: 0 -10px 8px;
  position: relative;
}
.intro-orbit i,
.intro-orbit b {
  display: grid;
  place-items: center;
  width: 32px;
  height: 42px;
  border-radius: 7px;
  background: var(--ink);
  color: var(--lime);
  font: 900 20px monospace;
  font-style: normal;
  box-shadow: 0 5px 0 var(--coral);
  animation: intro-letter-bob 1.8s ease-in-out infinite;
}
.intro-orbit i:nth-child(2n) { animation-delay: -0.35s; transform: rotate(6deg); }
.intro-orbit i:nth-child(3n) { animation-delay: -0.7s; transform: rotate(-7deg); }
.intro-orbit b {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--coral);
  color: var(--ink);
  box-shadow: 0 5px 0 var(--ink);
  animation-delay: -1s;
}
@keyframes intro-letter-bob {
  0%, 100% { translate: 0 4px; }
  50% { translate: 0 -8px; }
}
.round-intro-screen h1 {
  margin: 14px 0 20px;
  font-size: clamp(38px, 12vw, 58px);
  letter-spacing: -3px;
}
.intro-rule-card {
  margin: 0 auto;
  padding: 20px 16px;
  border: 2px solid var(--ink);
  border-radius: 18px;
  background: linear-gradient(135deg, #fff9f0, #e6f6d0);
  box-shadow: 0 7px 0 var(--ink);
  animation: intro-card-pop 0.5s ease both;
}
.intro-rule-card small,
.intro-auto {
  color: var(--muted);
  font: 10px monospace;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.intro-rule-card h2 {
  margin: 9px 0 6px;
  font-size: 22px;
  line-height: 1.1;
}
.intro-rule-card p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}
.intro-countdown {
  display: grid;
  place-items: center;
  width: 70px;
  height: 70px;
  margin: 27px auto 15px;
  border: 3px solid var(--coral);
  border-radius: 50%;
  color: var(--coral);
  font: 900 30px monospace;
  animation: intro-countdown-pulse 1s ease-in-out infinite;
}
.intro-start {
  width: 100%;
  justify-content: center;
  border: 0;
  font-size: 15px;
}
.intro-auto {
  display: block;
  margin-top: 12px;
  letter-spacing: 0;
  text-transform: none;
}
@keyframes intro-card-pop {
  from { opacity: 0; transform: translateY(12px) rotate(1deg); }
  to { opacity: 1; transform: translateY(0) rotate(0); }
}
@keyframes intro-countdown-pulse {
  0%, 100% { transform: scale(0.96); }
  50% { transform: scale(1.06); }
}
html[data-theme="dark"] .intro-rule-card {
  background: linear-gradient(135deg, #292e28, #39482f);
}

html[data-theme="dark"] .preview {
  background: #292e28;
}
html[data-theme="dark"] .tile {
  background: #e8e5da;
  color: #181a17;
}
@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) .preview {
    background: #292e28;
  }
  html:not([data-theme="light"]) .tile {
    background: #e8e5da;
    color: #181a17;
  }
}

@media (max-height: 650px) {
  .game-head h2 {
    font-size: 17px;
  }
  .game-meta strong {
    font-size: 28px;
  }
  .preview {
    height: 34px;
    font-size: 21px;
    margin-bottom: 5px;
  }
  .preview.found {
    font-size: 21px;
  }
  .grid {
    width: min(100%, calc(100svh - 380px), calc(100vw - 56px));
  }
  .game-foot {
    padding-top: 6px;
  }
}

.trace-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none;
  overflow: visible;
  /*
   * Deliberately hidden for now: app.js still records and smooths trace paths
   * so the visual trail can be restored without rebuilding input handling.
   * Use visibility (rather than display) to preserve the SVG's dimensions.
   */
  visibility: hidden;
}
.trace-layer path {
  fill: none;
  stroke: var(--coral);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 2px 2px #0003);
  opacity: 0.92;
}
.board .tile {
  touch-action: none;
}

body:has(#gameScreen.active) nav {
  display: none;
}
body:has(#gameScreen.active) .app-shell {
  padding-bottom: 0;
}
#gameScreen.active {
  height: 100svh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding:
    max(8px, env(safe-area-inset-top))
    max(0px, env(safe-area-inset-right))
    max(4px, env(safe-area-inset-bottom))
    max(0px, env(safe-area-inset-left));
}
.game-head,
.rule-banner,
.game-meta,
.preview,
.chain-status,
.challenge-status,
.game-foot {
  flex: none;
}
#gameScreen.active .game-head {
  margin-bottom: 2px;
}
#gameScreen.active #gameMode {
  font-size: 14px;
  letter-spacing: 0.5px;
}
#gameScreen.active #gameTitle {
  font-size: 18px;
}
#gameScreen.active .rule-banner {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  min-height: 0;
  padding-inline: 12px;
  font-size: 18px;
  font-weight: 800;
  line-height: 1.2;
  text-align: left;
  white-space: normal;
  padding-block: 5px;
  margin-block: 6px;
}
#gameScreen.active .rule-banner > span {
  min-width: 0;
  overflow: visible;
  text-overflow: clip;
}
#gameScreen.active .rule-divider {
  flex: none;
  display: none;
  color: var(--coral);
}
#gameScreen.active #gameHint,
#gameScreen.active .game-hint {
  flex: none;
  min-height: 14px;
  margin-top: 4px;
  color: var(--muted);
  font-family: monospace;
  font-size: 10px;
  line-height: 1.2;
}
#gameScreen.active .game-meta {
  margin-block: 4px;
}
#gameScreen.active .game-meta strong {
  font-size: 32px;
}
#gameScreen.active .preview {
  transition: opacity .22s ease, transform .22s ease;
  font-size: 18px;
  height: 36px;
  margin-bottom: 4px;
}
#gameScreen.active .preview.is-tracing {
  opacity: 0;
  transform: translateY(-4px);
}
#gameScreen.active .board {
  padding: 6px;
}
#gameScreen.active .game-foot {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(0, 1fr));
  gap: 8px;
  min-height: 56px;
  padding: 8px 0 4px;
}
#gameScreen.active .game-foot .game-action-tile {
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 8px 6px;
  border: 2px solid #2c1c37;
  border-radius: 14px;
  background: #b7f5ff;
  color: #2c1c37;
  box-shadow: 0 4px 0 #2c1c37;
  font: 900 11px/1.05 monospace;
  text-align: center;
  text-transform: uppercase;
}
#gameScreen.active .game-foot .game-action-tile:hover,
#gameScreen.active .game-foot .game-action-tile:focus-visible {
  transform: translateY(-2px);
  box-shadow: 0 6px 0 #2c1c37;
}
#gameScreen.active .game-foot .game-action-tile:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #2c1c37;
}
#gameScreen.active .game-foot .game-action-rush { background: #c8f36a; }
#gameScreen.active .game-foot .game-action-series { background: #e3c8ff; }
#gameScreen.active .game-foot .game-action-round { background: #f7b2a8; }
#gameScreen.active .game-foot .game-action-cast { background: #ffe278; }

@media (max-height: 650px) {
  #gameScreen.active {
    padding-top: max(4px, env(safe-area-inset-top));
  }
  #gameScreen.active .game-meta strong,
  #gameScreen.active .live-player b {
    font-size: 28px;
  }
  #gameScreen.active .preview {
    height: 30px;
    font-size: 18px;
    margin-bottom: 4px;
  }
  #gameScreen.active .rule-banner {
    margin-block: 2px;
    padding-block: 2px;
    font-size: 16px;
    line-height: 1.1;
  }
  #gameScreen.active #gameHint,
  #gameScreen.active .game-hint {
    min-height: 12px;
    margin-top: 2px;
  }
}

.avatar {
  display: grid;
  place-items: center;
  font-size: 20px;
}
.avatar img,
.avatar-photo,
.player-avatar img,
.scoreboard-avatar img,
.profile-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: inherit;
}
.avatar-inline {
  display: inline-grid;
  width: 1.3em;
  height: 1.3em;
  place-items: center;
  flex: none;
  overflow: hidden;
  border-radius: 50%;
  vertical-align: -.25em;
}
.avatar-inline .avatar-photo { width: 100%; height: 100%; border-radius: 50%; }
.avatar-inline .avatar-emoji { line-height: 1; }
.live-players {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  justify-content: flex-end;
  max-width: none;
  overflow-x: auto;
}
.live-player {
  display: grid;
  grid-template-columns: 20px minmax(42px, auto);
  grid-template-rows: auto auto;
  gap: 2px 5px;
  align-items: center;
  background: #ebe9e2;
  padding: 4px 7px;
  font-size: 11px;
  flex: none;
  min-width: 88px;
}
.player-avatar {
  font-size: 16px;
}
.player-name {
  max-width: 82px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.live-player b {
  grid-column: 1 / -1;
  color: var(--coral);
  font-family: monospace;
  font-size: 32px;
  line-height: 1;
  text-align: right;
}
.live-player.is-you {
  outline: 1px solid var(--coral);
}
.live-player.is-opponent {
  outline: 1px solid #5268c7;
}
.live-player.is-opponent b {
  color: #5268c7;
}
.live-player.is-reconnecting {
  opacity: 0.7;
  outline-style: dashed;
}
.player-connection-status {
  color: var(--muted);
  font-size: 10px;
  white-space: nowrap;
}
@media (max-height: 650px) {
  .live-player b {
    font-size: 28px;
  }
}

@keyframes tile-selected {
  0% {
    transform: scale(1);
    box-shadow: 0 3px 0 #1e9f58;
  }
  52% {
    transform: scale(1.18) rotate(-3deg);
    box-shadow: 0 10px 20px #45f27db3;
  }
  100% {
    transform: scale(1);
    box-shadow: 0 5px 12px #45f27d88;
  }
}
.tile.word-correct {
  animation: word-correct 0.65s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  background-color: #36ef78;
  color: var(--ink);
}
@keyframes word-correct {
  0% {
    transform: scale(1);
    background-color: #36ef78;
    box-shadow: 0 3px 0 #1c9c4e;
  }
  42% {
    transform: scale(1.25) rotate(-5deg);
    background-color: #c5ff35;
    box-shadow: 0 13px 24px #9cff3db3;
  }
  70% {
    transform: scale(1.13) rotate(5deg);
    background-color: #00e68a;
  }
  100% {
    transform: scale(1);
    background-color: #36ef78;
    box-shadow: 0 4px 0 #1c9c4e;
  }
}
.tile.word-incorrect {
  animation: word-incorrect 1.05s cubic-bezier(0.18, 0.89, 0.32, 1.28);
  background-color: #ff345e;
  color: #fff;
  outline: 4px solid #ff174e;
  outline-offset: 2px;
}
@keyframes word-incorrect {
  0% {
    transform: scale(1);
    background-color: #ff174e;
    box-shadow: 0 3px 0 #b51f42;
  }
  25% {
    transform: scale(1.24) rotate(8deg);
    background-color: #ff003d;
    box-shadow: 0 0 0 7px #ff174e66, 0 14px 28px #ff345ecc;
  }
  45% {
    transform: scale(1.08) rotate(-8deg);
    background-color: #ff633f;
    box-shadow: 0 0 0 4px #ff174e55, 0 8px 18px #ff345eaa;
  }
  65% {
    transform: scale(1.18) rotate(5deg);
    background-color: #ff174e;
  }
  100% {
    transform: scale(1);
    background-color: #ff345e;
    box-shadow: 0 4px 0 #b51f42;
  }
}
.tile.word-duplicate {
  animation: word-duplicate .48s ease-in-out;
  background-color: #f6c85f;
  color: #2c1c37;
}
@keyframes word-duplicate {
  0%, 100% { transform: scale(1); }
  30% { transform: scale(1.08) rotate(-2deg); box-shadow: 0 0 0 4px #f6c85f66; }
  60% { transform: scale(.96) rotate(2deg); }
}

body[data-mode="classic"] .tile {
  background: #dcecf7;
}
body[data-mode="minimum"] .tile {
  background: #e6def8;
}
body[data-mode="sudden"] .tile {
  background: #ffe1d2;
}
body[data-mode="race"] .tile {
  background: #dcefdc;
}
body[data-mode="dirty"] .tile {
  background: #f8e2bc;
}
body[data-mode="blitz"] .tile {
  background: #fff0c4;
}
body[data-mode="longhaul"] .tile {
  background: #e6def8;
}
body[data-mode="scoreattack"] .tile {
  background: #ffe2d1;
}
body[data-mode="chain"] .tile {
  background: #d9f2e7;
}

#grid .tile {
  position: relative;
  background-image: linear-gradient(145deg, #ffffff6b, transparent 60%);
  border: 1px solid #ffffffa8;
  border-radius: 22px !important;
  box-shadow:
    inset 0 1px 0 #ffffffa8,
    0 3px 0 #17233c40;
  overflow: hidden;
  transition:
    background-color 0.2s ease,
    color 0.2s ease,
    box-shadow 0.2s ease;
}
#grid .tile:nth-child(4n + 1) {
  background-color: #ffd2ad;
}
#grid .tile:nth-child(4n + 2) {
  background-color: #b9e6ff;
}
#grid .tile:nth-child(4n + 3) {
  background-color: #d8c4ff;
}
#grid .tile:nth-child(4n) {
  background-color: #ffc4d5;
}
#grid .tile.selected {
  background-color: #45f27d;
  box-shadow:
    inset 0 1px 0 #ffffffb8,
    0 5px 14px #45f27d99;
}
#grid .tile.word-correct {
  background-color: #36ef78;
  box-shadow:
    inset 0 1px 0 #ffffffb8,
    0 6px 18px #36ef78aa;
}
#grid .tile.word-incorrect {
  background-color: #ff345e;
  box-shadow:
    inset 0 1px 0 #ffffffb8,
    0 6px 18px #ff345eaa;
}
#grid .tile.word-duplicate {
  background-color: #f6c85f;
  box-shadow:
    inset 0 1px 0 #ffffffb8,
    0 4px 12px #f6c85f99;
}
.tile.tile-frozen {
  color: #eef3ff;
  border-color: #6675a8;
  background: repeating-linear-gradient(135deg, #59668e 0 6px, #6978a7 6px 12px);
  cursor: not-allowed;
  opacity: .78;
}
.tile.tile-bounty {
  outline: 3px solid #ffe767;
  outline-offset: -5px;
}
.tile.tile-bounty-claimed {
  outline-color: #8af3bf;
  background-image: linear-gradient(135deg, #c5ffe0, #70d99d);
}
@media (prefers-reduced-motion: reduce) {
  #grid .tile,
  #grid .tile.selected,
  #grid .tile.word-correct,
  #grid .tile.word-incorrect {
    animation-duration: 0.01ms;
    transition-duration: 0.01ms;
  }
}
