:root {
  --fg: #f3efe0;
  --muted: #9d958a;
  --accent: #ffb347;
  --danger: #ef4444;
  --ok: #22c55e;
  --rock: #2a2520;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--fg);
  background: #0a0805;
  user-select: none;
}

/* ---- TITLE ---- */
/* Title page is scrollable when content exceeds viewport (small screens / iPad portrait). */
body.title-bg { overflow-y: auto; height: auto; min-height: 100vh; }
.title-bg {
  display: grid; place-items: start center;
  min-height: 100vh;
  padding: 1rem 0;
  background:
    radial-gradient(circle at 30% 20%, #3a2a1a 0%, #0a0805 65%),
    repeating-linear-gradient(135deg, rgba(255,255,255,0.015) 0 8px, transparent 8px 22px);
}
.title {
  width: min(560px, 94vw);
  background: rgba(20, 16, 10, 0.85);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 18px;
  padding: 1.4rem 1.6rem;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  text-align: center;
  margin: 1rem 0;
}
.title h1 {
  margin: 0 0 0.3rem; font-size: clamp(1.6rem, 6vw, 2.4rem);
  letter-spacing: 4px;
  text-shadow: 0 0 30px rgba(255, 179, 71, 0.4);
}
.title .tagline { color: var(--muted); margin-bottom: 1.6rem; }
.title form { display: flex; flex-direction: column; gap: 0.7rem; margin-bottom: 1.5rem; }
.title input[type="text"] {
  width: 100%; padding: 0.75rem 1rem; font-size: 1rem;
  border-radius: 10px; border: 1px solid rgba(255,255,255,0.15);
  background: rgba(0,0,0,0.4); color: var(--fg);
}
.title button.primary {
  padding: 0.85rem 1.4rem; font-size: 1.05rem; font-weight: 700;
  background: var(--accent); color: #1a0e00;
  border: 0; border-radius: 10px; cursor: pointer;
  letter-spacing: 1px;
}
.title button:hover { filter: brightness(1.1); }
.title button { cursor: pointer; }
.title .error { color: #ff8a8a; min-height: 1.2em; margin: 0.5rem 0 0; }
.mode-picker { margin: 0.5rem 0 0.9rem; padding: 0.6rem; background: rgba(0,0,0,0.3); border-radius: 10px; text-align: left; }
.mode-choices { display: flex; gap: 8px; margin-top: 0.4rem; }
.mode-btn {
  flex: 1; padding: 0.7rem 0.5rem; border-radius: 10px; cursor: pointer;
  background: rgba(255,255,255,0.05); color: var(--fg); font-weight: 600;
  border: 2px solid rgba(255,255,255,0.12); font-size: 0.95rem; line-height: 1.2;
  text-align: center; transition: all 120ms;
}
.mode-btn .mode-sub { display: block; font-size: 0.72rem; color: var(--muted); font-weight: 400; margin-top: 3px; }
.mode-btn:hover { background: rgba(255,255,255,0.1); }
.mode-btn.active { border-color: var(--accent); background: rgba(245,185,69,0.18); box-shadow: 0 0 12px rgba(245,185,69,0.35); }
.skin { text-align: left; margin-bottom: 0.9rem; padding: 0.6rem; background: rgba(0,0,0,0.3); border-radius: 10px; }
.skin-preview-wrap {
  display: grid; place-items: center;
  background: linear-gradient(180deg, #2a1a3a 0%, #15101e 100%);
  border-radius: 8px;
  margin-bottom: 0.5rem;
}
.preview-canvas {
  width: 100%; max-width: 200px; aspect-ratio: 1 / 1; height: auto;
  display: block;
}
.title .tagline { color: var(--muted); margin-bottom: 0.9rem; font-size: 0.92rem; }
.title .rules h2 { font-size: 1rem; margin: 0.3rem 0 0.4rem; }
.title .rules ul { padding-left: 1.0rem; font-size: 0.82rem; margin: 0; }
.title .rules li { margin: 0.2rem 0; }
.title .hint { font-size: 0.78rem; }
@media (max-width: 480px), (max-height: 720px) {
  .title { padding: 1rem 1.1rem; }
  .preview-canvas { max-width: 150px; }
  .swatch { width: 24px; height: 24px; }
  .helmet { font-size: 0.78rem; padding: 3px 7px; }
  .skin-label { font-size: 0.85rem; min-width: 42px; }
}
.skin-row { display: flex; align-items: center; gap: 0.7rem; margin: 0.4rem 0; flex-wrap: wrap; }
.skin-label { color: var(--muted); font-size: 0.9rem; min-width: 50px; }
.swatches { display: flex; gap: 6px; flex-wrap: wrap; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.15); cursor: pointer; padding: 0; transition: transform 100ms; }
.swatch:hover { transform: scale(1.15); }
.swatch.active { border-color: var(--accent); box-shadow: 0 0 8px var(--accent); }
.helmets { display: flex; gap: 6px; flex-wrap: wrap; }
.helmet { background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.15); color: var(--fg); padding: 4px 10px; border-radius: 8px; cursor: pointer; font-size: 0.85rem; }
.helmet.active { background: var(--accent); color: #1a0e00; border-color: var(--accent); }
.title .rules { text-align: left; }
.title .rules ul { padding-left: 1.2rem; }
.title .rules li { margin: 0.4rem 0; }
.title .hint { color: var(--muted); font-size: 0.9rem; }
kbd {
  background: rgba(255,255,255,0.1); padding: 1px 6px; border-radius: 4px;
  font-family: ui-monospace, Menlo, monospace; font-size: 0.85em;
}

/* ---- GAME ---- */
.game-body { background: #000; }
#game-root { position: fixed; inset: 0; }
#game-root canvas { display: block; width: 100%; height: 100%; }

.hud {
  position: fixed; inset: 0;
  pointer-events: none;
  z-index: 5;
  font-feature-settings: "tnum";
}
.hud-top {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 8px; flex-wrap: wrap; justify-content: center;
  max-width: 95vw;
}
.hud-pill {
  background: rgba(0,0,0,0.55);
  border: 1px solid rgba(255,255,255,0.1);
  padding: 0.4rem 0.9rem; border-radius: 999px;
  font-size: 0.95rem; font-weight: 600;
  color: var(--fg);
}
#wave-pill { color: var(--accent); }
#weapon-pill.special { background: linear-gradient(90deg, #ff44aa, #ff8866); color: #1a0e00; font-weight: 700; box-shadow: 0 0 12px rgba(255,68,170,0.4); }

.hud-bottom {
  position: absolute; bottom: 24px; left: 24px; right: 24px;
  display: flex; align-items: end;
}
.hp-wrap { width: 280px; }
.hp-label {
  font-size: 0.85rem; color: var(--fg);
  margin-bottom: 4px; text-shadow: 0 1px 2px rgba(0,0,0,0.8);
}
.hp-bar {
  height: 16px; background: rgba(0,0,0,0.55);
  border: 1px solid rgba(255,255,255,0.15); border-radius: 8px;
  overflow: hidden;
}
.hp-fill {
  height: 100%; width: 100%;
  background: linear-gradient(90deg, var(--ok), #aaff7a);
  transition: width 200ms ease;
}
.hp-fill.low  { background: linear-gradient(90deg, var(--accent), #ffd87a); }
.hp-fill.crit { background: linear-gradient(90deg, var(--danger), #ff8888); }

#crosshair {
  position: absolute; left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  font-size: 22px; line-height: 1;
  color: rgba(255,255,255,0.85);
  text-shadow: 0 0 4px rgba(0,0,0,0.9);
}
/* Sniper scope overlay — black vignette with crosshair circle when scoping. */
body.scoped::before {
  content: ''; position: fixed; inset: 0; pointer-events: none; z-index: 4;
  background: radial-gradient(circle at center, transparent 0 22vh, transparent 22vh, rgba(0,0,0,0.97) 28vh);
}
body.scoped::after {
  content: ''; position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 44vh; height: 44vh; border-radius: 50%; pointer-events: none; z-index: 5;
  border: 2px solid rgba(255,255,255,0.5);
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.8);
  background:
    linear-gradient(rgba(255,0,0,0.8), rgba(255,0,0,0.8)) center / 1px 100% no-repeat,
    linear-gradient(rgba(255,0,0,0.8), rgba(255,0,0,0.8)) center / 100% 1px no-repeat;
}
body.scoped #crosshair { display: none; }

.events {
  position: absolute; top: 80px; right: 16px;
  width: 260px; max-height: 30vh; overflow: hidden;
  display: flex; flex-direction: column; gap: 4px;
  pointer-events: none;
}
.events div {
  background: rgba(0,0,0,0.5);
  border-left: 3px solid var(--muted);
  padding: 6px 10px; border-radius: 4px;
  font-size: 0.85rem;
  animation: fadein 200ms ease;
}
.events div.wave  { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.events div.death { border-color: var(--danger); }
.events div.kill  { border-color: var(--ok); }
.events div.join  { border-color: #88ccff; }
@keyframes fadein {
  from { opacity: 0; transform: translateX(8px); }
  to   { opacity: 1; transform: translateX(0); }
}

.overlay {
  position: fixed; inset: 0; z-index: 10;
  background: rgba(0,0,0,0.78);
  display: grid; place-items: center;
  pointer-events: auto;
}
.overlay.hidden { display: none; }
.overlay-card {
  background: rgba(20,16,10,0.95);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 14px; padding: 2rem 2.5rem;
  text-align: center; max-width: 420px;
}
.overlay-card h2 { margin: 0 0 0.5rem; font-size: 1.8rem; }
.overlay-card p { color: var(--muted); margin: 0.6rem 0; }
.overlay-card button {
  padding: 0.75rem 1.5rem; font-size: 1.05rem; font-weight: 700;
  background: var(--accent); color: #1a0e00;
  border: 0; border-radius: 10px; cursor: pointer;
  margin-top: 0.5rem;
}
.overlay-card button.ghost {
  background: transparent; color: var(--muted); font-weight: 400;
  border: 1px solid rgba(255,255,255,0.15); margin-top: 1.2rem;
  display: block; margin-left: auto; margin-right: auto;
}
.overlay-card .hint { color: var(--muted); font-size: 0.85rem; margin-top: 0.4rem; }

/* ---- TOUCH CONTROLS ---- */
.touch-controls {
  position: fixed; inset: 0; z-index: 8;
  pointer-events: none;
}
.touch-controls.hidden { display: none; }
.joystick {
  position: absolute; left: 24px; bottom: 24px;
  width: 150px; height: 150px;
  pointer-events: auto;
  touch-action: none;
}
.joystick-base {
  position: absolute; inset: 0; border-radius: 50%;
  background: rgba(0,0,0,0.4);
  border: 2px solid rgba(255,255,255,0.25);
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
.joystick-knob {
  position: absolute; width: 64px; height: 64px;
  left: 43px; top: 43px;
  border-radius: 50%;
  background: rgba(245,185,69,0.7);
  border: 2px solid rgba(255,255,255,0.6);
  pointer-events: none;
}
.fire-btn {
  position: absolute; right: 30px; bottom: 60px;
  width: 110px; height: 110px;
  border-radius: 50%;
  background: rgba(239,68,68,0.55);
  border: 3px solid rgba(255,255,255,0.45);
  color: white; font-size: 38px;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  box-shadow: 0 4px 16px rgba(0,0,0,0.4);
}
.fire-btn:active { background: rgba(239,68,68,0.85); transform: scale(0.95); }
.jump-btn {
  position: absolute; right: 30px; bottom: 190px;
  width: 70px; height: 70px;
  border-radius: 50%;
  background: rgba(34,197,94,0.55);
  border: 2px solid rgba(255,255,255,0.4);
  color: white; font-size: 30px;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
.jump-btn:active { background: rgba(34,197,94,0.85); transform: scale(0.95); }
/* Side stack of smaller utility buttons (drop weapon, build structures). */
.side-btn {
  position: absolute; right: 30px;
  min-width: 90px; height: 44px;
  padding: 0 12px;
  border-radius: 22px;
  background: rgba(15, 23, 42, 0.55);
  border: 2px solid rgba(255,255,255,0.35);
  color: white; font-size: 14px; font-weight: 600;
  pointer-events: auto;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  box-shadow: 0 4px 12px rgba(0,0,0,0.35);
}
.side-btn:active { background: rgba(15,23,42,0.9); transform: scale(0.96); }
.side-btn.hidden { display: none; }
.drop-btn        { bottom: 280px; background: rgba(245,158,11,0.55); border-color: rgba(255,255,255,0.5); }
#build-wall-btn  { bottom: 340px; background: rgba(139,92,246,0.55); }
#build-door-btn  { bottom: 400px; background: rgba(59,130,246,0.55); }
#build-roof-btn  { bottom: 460px; background: rgba(20,184,166,0.55); }
#build-torch-btn { bottom: 520px; background: rgba(251,146,60,0.55); }
#open-door-btn   { bottom: 580px; background: rgba(234,179,8,0.55); }
#remove-btn      { bottom: 640px; background: rgba(239,68,68,0.55); }

/* PC-only build-key reference panel. Hidden on touch (they have buttons). */
.build-help {
  position: fixed;
  top: 58px; left: 12px;
  z-index: 7;
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid rgba(255,255,255,0.18);
  border-radius: 10px;
  padding: 10px 12px;
  color: white;
  font: 12px system-ui, sans-serif;
  max-width: 230px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.35);
  pointer-events: none;
}
.build-help.hidden { display: none; }
body.is-touch .build-help { display: none; }
.build-help-title { font-weight: 700; margin-bottom: 6px; font-size: 13px; }
.build-help-list  { list-style: none; margin: 0; padding: 0; }
.build-help-list li {
  display: flex; align-items: center; gap: 8px;
  padding: 2px 0;
}
.build-help-list kbd {
  display: inline-block; min-width: 20px;
  padding: 1px 6px; text-align: center;
  background: rgba(255,255,255,0.12);
  border: 1px solid rgba(255,255,255,0.35);
  border-bottom-width: 2px;
  border-radius: 4px;
  font: 600 11px ui-monospace, monospace;
}
.build-help-hint {
  margin-top: 6px;
  font-size: 11px;
  opacity: 0.75;
  line-height: 1.35;
}
.chat-btn {
  position: absolute; left: 0; bottom: -40px;
  display: flex; align-items: center; gap: 6px;
  background: rgba(59,130,246,0.45);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 6px;
  color: white; font-size: 14px;
  padding: 4px 10px;
  pointer-events: auto;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.chat-btn:hover { background: rgba(59,130,246,0.75); }
.chat-hint { color: rgba(255,255,255,0.85); font-weight: 600; font-size: 12px; }
.touch-controls:not(.hidden) ~ * .chat-btn { /* on touch, override */ }
body.is-touch .chat-btn {
  position: fixed; left: auto; right: 160px; bottom: 60px;
  width: 60px; height: 60px;
  border-radius: 50%; padding: 0;
  background: rgba(59,130,246,0.55);
  font-size: 26px; gap: 0;
  border-width: 2px;
}
body.is-touch .chat-hint { display: none; }

/* ---- CHAT ---- */
.chat {
  position: fixed; left: 16px; bottom: 110px;
  width: min(360px, 60vw);
  z-index: 7;
  pointer-events: none;
}
.chat-messages {
  list-style: none; margin: 0 0 6px; padding: 0;
  display: flex; flex-direction: column-reverse; gap: 3px;
  max-height: 30vh; overflow: hidden;
}
.chat-messages li {
  background: rgba(0,0,0,0.55);
  padding: 4px 9px; border-radius: 6px;
  font-size: 0.9rem; color: var(--fg);
  text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  animation: chatfade 250ms ease;
}
.chat-messages li .who { font-weight: 700; margin-right: 6px; }
@keyframes chatfade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }
/* Chat modal — centered popup when T is pressed */
.chat-modal {
  position: fixed; inset: 0;
  display: grid; place-items: center;
  background: rgba(0,0,0,0.55);
  z-index: 12;
  pointer-events: auto;
}
.chat-modal.hidden { display: none; }
.chat-form {
  background: rgba(20,16,10,0.96);
  border: 1px solid var(--accent);
  border-radius: 14px;
  padding: 1.4rem 1.6rem;
  width: min(520px, 92vw);
  box-shadow: 0 20px 60px rgba(0,0,0,0.7);
}
.chat-modal-label { display: block; font-weight: 700; color: var(--accent); margin-bottom: 0.6rem; font-size: 1.05rem; }
.chat-form input {
  width: 100%; padding: 0.7rem 1rem; font-size: 1.05rem;
  background: rgba(0,0,0,0.6); color: var(--fg);
  border: 1px solid rgba(255,255,255,0.2); border-radius: 8px; outline: none;
  box-sizing: border-box;
}
.chat-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(245,185,69,0.25); }
.chat-modal-help { color: var(--muted); font-size: 0.82rem; margin-top: 0.5rem; text-align: center; }

/* Inventory HUD */
.inventory {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  display: flex; gap: 8px;
  z-index: 6; pointer-events: none;
}
.inv-slot {
  display: flex; align-items: center; gap: 6px;
  background: rgba(0,0,0,0.55);
  border: 2px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  padding: 6px 12px;
  color: var(--fg); font-weight: 600; font-size: 0.9rem;
  pointer-events: auto;
  cursor: pointer;
}
.inv-slot .inv-key { background: rgba(255,255,255,0.12); padding: 1px 5px; border-radius: 3px; font-size: 0.75rem; color: var(--muted); }
.inv-slot.active { border-color: var(--accent); background: rgba(245,185,69,0.18); box-shadow: 0 0 12px rgba(245,185,69,0.4); }

/* Weapon waypoint marker */
.weapon-marker {
  position: fixed;
  z-index: 6;
  display: flex; flex-direction: column; align-items: center;
  gap: 4px;
  pointer-events: none;
  transform: translate(-50%, -50%);
  font-weight: 600;
  color: white;
  text-shadow: 0 1px 3px rgba(0,0,0,0.9);
  transition: opacity 200ms;
}
.weapon-marker.hidden { display: none; }
.wm-arrow {
  font-size: 22px;
  color: #ffb347;
  text-shadow: 0 0 6px rgba(255,179,71,0.7), 0 1px 3px #000;
  animation: wm-pulse 1.2s ease-in-out infinite;
}
.wm-label {
  background: rgba(0,0,0,0.6);
  border: 1px solid rgba(255,179,71,0.6);
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.85rem;
  white-space: nowrap;
}
@keyframes wm-pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.25); } }
