* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  color: #f5f2ec;
  background:
    linear-gradient(rgba(15, 17, 20, 0.8), rgba(15, 17, 20, 0.92)),
    radial-gradient(circle at top left, #715d43, transparent 34%),
    #101114;
  -webkit-tap-highlight-color: transparent;
}

#game-container {
  width: min(100%, 820px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
}

.topbar {
  width: min(100%, 620px);
  display: grid;
  grid-template-columns: 96px 1fr 96px;
  align-items: center;
  gap: 16px;
}

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

h1 {
  margin: 0;
  font-size: clamp(2rem, 6vw, 4rem);
}

#winner-message {
  min-height: 28px;
  margin: 6px 0 0;
  color: #d6c3a4;
  font-weight: 800;
}

.back-link,
#restart-button {
  min-height: 42px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 8px;
  color: #f5f2ec;
  background: rgba(32, 34, 38, 0.82);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}

.back-link:hover,
#restart-button:hover {
  background: rgba(92, 76, 52, 0.9);
}

#scoreboard {
  width: min(100%, 520px);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  text-align: center;
  font-weight: 800;
}

#scoreboard span {
  padding: 10px 8px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
}

#player-turn {
  display: flex;
  justify-content: center;
  gap: 12px;
}

.ai-toggle {
  min-height: 42px;
  padding: 9px 14px;
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: #f5f2ec;
  background: rgba(32, 34, 38, 0.82);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}

.ai-toggle input {
  width: 18px;
  height: 18px;
  accent-color: #e6c978;
}

.player {
  width: 58px;
  height: 42px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-size: 1.5rem;
  font-weight: 900;
}

.player.active {
  background: #e6c978;
  color: #151515;
}

.player.inactive {
  background: rgba(255, 255, 255, 0.08);
  color: #d7d1c7;
}

#game-board {
  width: min(86vw, 520px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 10px;
  padding: 12px;
  border-radius: 8px;
  background: linear-gradient(135deg, #5d3921, #231713);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.46);
  touch-action: manipulation;
  user-select: none;
}

.cell {
  width: 100%;
  aspect-ratio: 1;
  border: 0;
  border-radius: 8px;
  background: linear-gradient(135deg, #eee0c4, #bb905d);
  color: #1c1711;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(3rem, 16vw, 7rem);
  font-weight: 900;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.18);
}

.cell:hover:not(.taken) {
  filter: brightness(1.08);
}

.cell.taken {
  cursor: not-allowed;
}

.cell.o {
  color: #2b5c8a;
}

.cell.x {
  color: #8f2c26;
}

.cell.winner {
  background: linear-gradient(135deg, #f5d56f, #c88f2a);
}

@media (max-width: 620px) {
  #game-container {
    padding: 14px;
    justify-content: start;
  }

  .topbar {
    grid-template-columns: 1fr 1fr;
  }

  .title-block {
    grid-column: 1 / -1;
    grid-row: 1;
  }

  .back-link,
  #restart-button {
    grid-row: 2;
  }
}
