:root {
  --bg-0: #070a1a;
  --bg-1: #0e1430;
  --ink: #e6ecff;
  --ink-dim: #8896c2;
  --accent: #4dd0ff;
  --accent-2: #a06cff;
  --danger: #ff6a8a;
  --ok: #66e4a6;
  --panel: rgba(20, 28, 58, 0.72);
  --panel-line: rgba(140, 180, 255, 0.18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 800px at 20% -10%, #1a1f5e 0%, transparent 55%),
    radial-gradient(1000px 700px at 110% 10%, #3a1a5e 0%, transparent 55%),
    linear-gradient(180deg, var(--bg-0), var(--bg-1));
  background-attachment: fixed;
  min-height: 100dvh;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app {
  max-width: 720px;
  margin: 0 auto;
  padding:
    calc(12px + var(--safe-top))
    calc(12px + var(--safe-right))
    calc(12px + var(--safe-bot))
    calc(12px + var(--safe-left));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.screen {
  display: flex;
  flex-direction: column;
  gap: 16px;
  animation: fadeIn 220ms ease both;
  flex: 1;
}
.screen[hidden] { display: none; }
.screen.game { gap: 10px; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

h1 {
  font-size: 44px;
  letter-spacing: -0.02em;
  margin: 8px 0 0;
  font-weight: 800;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
h2 {
  font-size: 22px;
  margin: 4px 0 0;
  font-weight: 700;
}
.tag { color: var(--ink-dim); margin: 0; }

.hero { text-align: center; padding: 24px 0 8px; position: relative; }
.logo {
  width: 96px; height: 96px;
  margin: 0 auto 4px;
  position: relative;
}
.logo .planet {
  position: absolute; inset: 10px;
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 28%, #6ac9ff 0%, #3a6fd8 45%, #1a2c78 100%);
  box-shadow:
    inset -8px -10px 24px rgba(0,0,0,0.45),
    inset 6px 4px 8px rgba(255,255,255,0.15),
    0 0 40px rgba(77, 208, 255, 0.25);
}
.logo .ring {
  position: absolute; inset: 26px 0;
  border-top: 3px solid rgba(160, 108, 255, 0.8);
  border-radius: 50%;
  transform: rotate(-18deg);
  box-shadow: 0 0 12px rgba(160, 108, 255, 0.5);
}

.field { display: block; }
.field span {
  display: block;
  font-size: 12px;
  color: var(--ink-dim);
  margin: 0 0 6px 4px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
input[type="text"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--panel-line);
  color: var(--ink);
  font-size: 18px;
  padding: 14px 16px;
  border-radius: 12px;
  outline: none;
  transition: border-color 150ms, background 150ms;
}
input[type="text"]:focus {
  border-color: var(--accent);
  background: rgba(77, 208, 255, 0.08);
}

.actions { display: flex; flex-direction: column; gap: 10px; }
.actions-row { flex-direction: row; }
.actions-row .btn { flex: 1; }

.btn {
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid var(--panel-line);
  background: rgba(255,255,255,0.04);
  color: var(--ink);
  padding: 15px 18px;
  font-size: 16px;
  font-weight: 700;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 80ms ease, background 150ms, border-color 150ms;
  letter-spacing: 0.02em;
}
.btn:active { transform: scale(0.98); }
.btn.primary {
  background: linear-gradient(180deg, var(--accent), #2a9ecf);
  border-color: var(--accent);
  color: #06101e;
  box-shadow: 0 10px 24px rgba(77, 208, 255, 0.28);
}
.btn.ghost { background: transparent; color: var(--ink-dim); }
.btn:disabled { opacity: 0.45; pointer-events: none; }

.back {
  background: none; border: none; color: var(--ink-dim); font-size: 15px;
  align-self: flex-start; padding: 4px 0; cursor: pointer;
}

.code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 38px;
  font-weight: 800;
  letter-spacing: 0.22em;
  text-align: center;
  padding: 18px 12px;
  background: rgba(255,255,255,0.04);
  border: 1px dashed var(--panel-line);
  border-radius: 14px;
  color: var(--accent);
}
.code-input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 30px;
  letter-spacing: 0.22em;
  text-align: center;
  text-transform: uppercase;
}

.status {
  color: var(--ink-dim);
  text-align: center;
  margin: 4px 0;
  font-size: 14px;
  min-height: 18px;
}

.spinner {
  width: 28px; height: 28px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.12);
  border-top-color: var(--accent);
  margin: 6px auto;
  animation: spin 900ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* LOBBY */
.players { display: flex; flex-direction: column; gap: 8px; }
.player-row {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--panel-line);
  border-radius: 12px;
}
.player-row .swatch {
  width: 18px; height: 18px; border-radius: 50%;
  box-shadow: 0 0 8px currentColor;
}
.player-row .pname { font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-row .pname input {
  background: transparent; border: none; outline: none;
  color: var(--ink); font-weight: 700; font-size: 16px;
  width: 100%;
}
.player-row .ptag {
  font-size: 11px; color: var(--ink-dim);
  letter-spacing: 0.08em; text-transform: uppercase;
}

/* GAME HUD */
.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  backdrop-filter: blur(6px);
}
.hud-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.hud-right { display: flex; align-items: center; gap: 10px; justify-content: flex-end; min-width: 0; }
.hud-center { text-align: center; }
.turnName {
  font-weight: 800;
  color: var(--ink);
  text-shadow: 0 0 12px currentColor;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}
.turnTimer {
  font-family: ui-monospace, monospace;
  font-weight: 800;
  font-size: 20px;
  padding: 2px 8px;
  background: rgba(255,255,255,0.08);
  border-radius: 8px;
  min-width: 32px;
  text-align: center;
}
.turnTimer.low { color: var(--danger); }
.wind {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,0.06);
  padding: 4px 10px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--ink-dim);
}
.wind-arrow {
  display: inline-block;
  font-size: 18px;
  color: var(--accent);
  font-weight: 800;
  transition: transform 300ms;
}
.weapon-label { font-weight: 700; font-size: 14px; color: var(--accent-2); }
.weapon-ammo { font-family: ui-monospace, monospace; color: var(--ink-dim); font-size: 13px; }

.canvas-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #04060f;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--panel-line);
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  touch-action: none;
}
#game {
  width: 100%;
  height: 100%;
  display: block;
}

.power-bar {
  position: absolute;
  left: 16px; right: 16px; bottom: 14px;
  height: 10px;
  background: rgba(0,0,0,0.4);
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--panel-line);
}
.power-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--ok), #ffd056, var(--danger));
  width: 0%;
}

.banner {
  position: absolute;
  top: 12px; left: 50%;
  transform: translateX(-50%);
  background: rgba(6, 10, 24, 0.85);
  border: 1px solid var(--panel-line);
  padding: 8px 16px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 14px;
  color: var(--accent);
  backdrop-filter: blur(4px);
}

.weapon-row {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 4px 2px;
  scrollbar-width: none;
}
.weapon-row::-webkit-scrollbar { display: none; }
.wbtn {
  flex: 0 0 auto;
  min-width: 64px;
  padding: 10px 8px;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--panel-line);
  border-radius: 10px;
  color: var(--ink);
  font-size: 11px;
  text-align: center;
  cursor: pointer;
  transition: background 120ms, border-color 120ms;
  font-weight: 700;
}
.wbtn .wico { font-size: 22px; display: block; margin-bottom: 2px; }
.wbtn .wammo { color: var(--ink-dim); font-size: 10px; font-family: ui-monospace, monospace; }
.wbtn.active {
  background: rgba(77, 208, 255, 0.12);
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(77, 208, 255, 0.3);
}
.wbtn:disabled { opacity: 0.35; cursor: not-allowed; }

.controls {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: end;
}
.ctrl-left { display: flex; gap: 6px; }
.ctrl-aim { display: flex; flex-direction: column; gap: 6px; align-items: center; justify-self: center; }
.ctrl-fire { display: flex; }

.cbtn {
  -webkit-appearance: none;
  appearance: none;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--panel-line);
  color: var(--ink);
  font-size: 20px;
  font-weight: 800;
  padding: 14px 18px;
  border-radius: 12px;
  cursor: pointer;
  min-width: 56px;
  transition: background 100ms;
}
.cbtn:active { background: rgba(77, 208, 255, 0.22); }
.cbtn.small { padding: 8px 14px; min-width: 44px; font-size: 15px; }
.cbtn.fire {
  background: linear-gradient(180deg, #ff6a8a, #c93f5d);
  border-color: #ff6a8a;
  color: #1a0510;
  padding: 16px 24px;
  font-size: 15px;
  box-shadow: 0 6px 18px rgba(255, 106, 138, 0.38);
}
.cbtn.fire.charging {
  animation: chargePulse 600ms ease-in-out infinite alternate;
}
@keyframes chargePulse {
  from { box-shadow: 0 6px 18px rgba(255, 106, 138, 0.38); }
  to { box-shadow: 0 10px 28px rgba(255, 200, 120, 0.8); }
}
.cbtn:disabled { opacity: 0.35; pointer-events: none; }

.endbar {
  text-align: center;
  padding: 12px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--panel-line);
  border-radius: 14px;
  animation: pop 260ms ease both;
}
@keyframes pop {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}
.endtext { font-size: 22px; font-weight: 800; margin-bottom: 10px; }
.endtext.win { color: var(--ok); }
.endtext.lose { color: var(--danger); }

.gamefoot {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  color: var(--ink-dim);
  font-size: 12px;
  padding-top: 2px;
}
.conn { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); box-shadow: 0 0 6px currentColor; }
.conn.bad { background: var(--danger); }

@media (min-width: 600px) {
  h1 { font-size: 56px; }
  .controls { gap: 12px; }
  .cbtn { padding: 16px 22px; }
}

/* Hide touch controls on wide screens if mouse present — keep for now, users may still tap */
@media (hover: hover) and (pointer: fine) {
  .controls { opacity: 0.85; }
}
