What's a good way to be hardware independent?

Compiler optimizations might also help resolve things like pin offsets, turning "PINB & (1 << PB5)" into "*(uint_8t *0x18) & (1 << 5)" into the cheapest assembly instructions that would achieve that I/O request.

The compiler is very good at that. For example:

void setup ()
  {
  DDRD |= 1 << 4;  // pinMode (4, OUTPUT);
  PORTD |= 1 << 4;  // digitalWrite (4, HIGH);  
  }  // end of setup
  
void loop () {}

Generates:

void setup ()
  {
  DDRD |= 1 << 4;  // pinMode (4, OUTPUT);
  a6:	54 9a       	sbi	0x0a, 4	; 10
  PORTD |= 1 << 4;  // digitalWrite (4, HIGH);  
  a8:	5c 9a       	sbi	0x0b, 4	; 11
  }  // end of setup
  aa:	08 95       	ret

A "shift" and an "or" has been optimized into the "SBI" assembler instruction. Can't get more efficient than that.