* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #222;
  color: #eee;
  font-family: sans-serif;
}

/* ===== Login ===== */

#login-screen {
position: fixed;
inset: 0;
display: grid;
place-items: center;
}

#login-form {
background: #2e2e2e;
border: 1px solid #444;
padding: 20px;
width: 260px;
}

#login-form h1 {
font-size: 18px;
margin: 0 0 12px;
}

#login-logo {
display: block;
max-width: 160px;
width: 100%;
height: auto;
margin: 0 auto 14px;
}

#username-input {
width: 100%;
padding: 8px;
margin-bottom: 10px;
background: #1a1a1a;
border: 1px solid #444;
color: #eee;
}

#join-btn {
width: 100%;
padding: 8px;
background: #4a4a4a;
border: 1px solid #666;
color: #eee;
cursor: pointer;
}

#join-btn:hover { background: #5a5a5a; }

#login-error { color: #f66; font-size: 13px; }

.hint {
  color: #999;
  font-size: 12px;
  margin-top: 10px;
}

/* ===== Game ===== */

#game-screen {
position: fixed;
inset: 0;
overflow: hidden;
}

#game-canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
}

#player-count-box,
#connection-status {
position: absolute;
top: 10px;
left: 10px;
background: rgba(0,0,0,0.5);
padding: 4px 8px;
font-size: 12px;
}

#connection-status { top: 36px; }

/* ===== Chat ===== */

#chat-dock {
position: absolute;
left: 10px;
bottom: 10px;
width: 300px;
}

#chat-log {
max-height: 180px;
overflow-y: auto;
margin-bottom: 6px;
}

.chat-line {
  background: rgba(0,0,0,0.5);
  padding: 3px 6px;
  font-size: 13px;
  margin-bottom: 2px;
}

.chat-line--system {
  color: #999;
  background: none;
  font-size: 12px;
}

.chat-line .who { font-weight: bold; margin-right: 4px; }

#chat-input {
width: 100%;
padding: 6px;
background: rgba(0,0,0,0.6);
border: 1px solid #444;
color: #eee;
}

/* ===== Inventory bar ===== */

#inventory-bar {
position: fixed;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
display: flex;
gap: 8px;
z-index: 20;
}

.inv-slot {
  width: 52px;
  height: 52px;
  background: rgba(0,0,0,0.5);
  border: 1px solid #444;
  position: relative;
  cursor: pointer;
}

.inv-slot:hover {
  border-color: #666;
}

.inv-slot.active {
  border-color: #eee;
}

.inv-slot--hat {
  border-color: #555;
}

.inv-slot.pending {
  border-color: #ccc;
  box-shadow: 0 0 0 2px rgba(238,238,238,0.25);
}

.inv-slot-key {
  position: absolute;
  top: 2px;
  left: 5px;
  font-size: 10px;
  color: #999;
  pointer-events: none;
}

.inv-slot-icon {
  position: absolute;
  inset: 8px;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  opacity: 0;
  pointer-events: none;
}

.inv-slot-icon.filled { opacity: 1; }

.inv-slot-icon.swatch {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: bold;
  color: #eee;
  border: 1px solid #444;
}

html.is-touch #inventory-bar { bottom: 112px; }

/* ===== Backpack ===== */

#backpack-panel {
position: fixed;
left: 50%;
bottom: 90px;
transform: translateX(-50%);
z-index: 25;
background: #2e2e2e;
border: 1px solid #444;
padding: 14px 16px 16px;
width: min(360px, 90vw);
max-height: calc(100vh - 160px);
overflow-y: auto;
}

.backpack-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 13px;
  font-weight: bold;
  color: #eee;
  letter-spacing: 0.05em;
}

.backpack-trash, .backpack-close {
  cursor: pointer;
  padding: 2px 8px;
  opacity: 0.7;
}

.backpack-trash:hover, .backpack-close:hover {
  opacity: 1;
  background: #4a4a4a;
}

.backpack-grid {
  display: grid;
  grid-template-columns: repeat(5, 52px);
  gap: 8px;
  justify-content: center;
}

.backpack-hint {
  margin-top: 10px;
  font-size: 11px;
  color: #999;
  text-align: center;
}

html.is-touch #backpack-panel { width: min(320px, 92vw); bottom: 190px; }
html.is-touch .backpack-grid { grid-template-columns: repeat(4, 56px); }

/* ===== Position viewer ===== */

#position-viewer {
position: fixed;
top: max(12px, env(safe-area-inset-top));
right: max(12px, env(safe-area-inset-right));
z-index: 20;
display: flex;
flex-direction: column;
gap: 2px;
padding: 7px 12px;
background: rgba(0,0,0,0.5);
border: 1px solid #444;
font-size: 12px;
color: #eee;
pointer-events: none;
}

#position-viewer .pos-row {
display: flex;
align-items: baseline;
gap: 8px;
justify-content: space-between;
}

#position-viewer .pos-row--grid {
opacity: 0.6;
font-size: 11px;
}

#position-viewer .pos-label {
color: #999;
letter-spacing: 0.05em;
}

#position-viewer .pos-coords {
color: #eee;
font-weight: 600;
min-width: 72px;
text-align: right;
}

html.is-touch #position-viewer { font-size: 11px; padding: 6px 10px; }

/* ===== Mobile controls ===== */

#mobile-controls {
position: fixed;
inset: 0;
z-index: 15;
pointer-events: none;
}

#joystick-zone {
position: absolute;
left: 0;
bottom: 0;
width: 55%;
height: 50%;
pointer-events: auto;
touch-action: none;
}

#joystick-base {
position: fixed;
width: 96px;
height: 96px;
margin-left: -48px;
margin-top: -48px;
border-radius: 50%;
background: rgba(0,0,0,0.5);
border: 1px solid #666;
opacity: 0;
transition: opacity 0.12s ease;
pointer-events: none;
left: 90px;
top: -9999px;
}

#joystick-base.visible {
opacity: 1;
}

#joystick-knob {
position: absolute;
left: 50%;
top: 50%;
width: 46px;
height: 46px;
margin-left: -23px;
margin-top: -23px;
border-radius: 50%;
background: rgba(238,238,238,0.7);
border: 1px solid #eee;
}

#mobile-action-buttons {
position: absolute;
right: max(16px, env(safe-area-inset-right));
bottom: max(16px, env(safe-area-inset-bottom));
display: grid;
grid-template-columns: repeat(2, 62px);
grid-auto-rows: 62px;
gap: 12px;
pointer-events: auto;
}

.mobile-btn {
  width: 62px;
  height: 62px;
  border-radius: 50%;
  border: 1px solid #666;
  background: rgba(0,0,0,0.6);
  color: #eee;
  font-size: 16px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.mobile-btn:active,
.mobile-btn.active {
  border-color: #eee;
  background: #5a5a5a;
}

html.is-touch #inventory-bar { bottom: 132px; }
