The most common route for backpackers going to and from Panama/South America, would be Panama City, San Blas, Bocas del Toro, cross border to Costa Rica at Sixoala caribe side (shuttles/boats between BDT and Pt Viejo.)
https://www.puertoviejosatellite.com
Surf on cairbe side varies by month, there are 3 areas, Pt Viejo Salsa Brava, not for the novice, Playa Cocles, and Playa Manzanillo at end of road...
www.crsurf.com
Costa Rica Pt Viejo is a euro backpacker scene and fun party town, stay in beaches south of town and rent bicycles. The raft the Rio Pacuare as a oneway day trip to San Jose, Top 10 river to raft in Americas, through indigenous indiands lands, gorgeous rainforest, jungle and waterfalls.
From San Jose, depends on interest, but I would get a bus straight to Jaco and surf there, not for beginners either, okay town, all the good surf towns and surf/beach scenes you don't have any time for IMO, maybe Tamarindo instead of Jaco, but thats 6 hours bus from SJ, though just 4 hours to Nica border, Tamarindo is a surf mecca for all levels and good nightlife to boot.
Once across Nica border, via busses from Liberia every 30 minutes to border, it depends on what you want, I would head to Ometepe for 3 nights, 2 nights in Granada as a base and then head out of MGA.
You really could spend the 3 weeks rushed in just 2 countries, so with long days of transport, you might want to consider internal flights, like from Panama City to Bocas, and from San Jose to Tamarindo.
The surf town in Nica is San Juan del Sur, but the good beaches and surf breaks you need to commute to, nothing in the town even to swim....Costa Rica has loads of great surf towns but you really don't have any time.
https://nicoyapeninsula.com
Adventure Travel to Colombia, Costa Rica, Nicaragua, Honduras, Guatemala, Mexico, Thailand, Burma, Laos, Cambodia, Vietnam, China, South Africa, Morocco, Turkey, EU, USA National Parks, enjoying culture, cuisine, motorcycling, scuba diving, surfing, sailing, rafting, hiking, fishing, camping, nature, wildlife. Get a Guidebook, and get lost!