:root {
  --bg-0: #050914;
  --bg-1: #0d1630;
  --hud-bg: rgba(7, 15, 30, 0.6);
  --hud-border: rgba(120, 190, 255, 0.25);
  --hud-text: #e7f3ff;
  --accent: #5fd8ff;
  --danger: #ff6a7a;
  --ok: #9dff6f;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  -ms-touch-action: none;
  background:
    radial-gradient(circle at 20% 10%, rgba(95, 216, 255, 0.1), transparent 28%),
    radial-gradient(circle at 80% 90%, rgba(157, 255, 111, 0.08), transparent 30%),
    linear-gradient(160deg, var(--bg-0), var(--bg-1));
  color: var(--hud-text);
  font-family: "Trebuchet MS", "Avenir Next", sans-serif;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
}

#game-root {
  width: 100%;
  height: 100%;
}

.overlay {
  position: absolute;
  z-index: 20;
}

.hidden {
  display: none !important;
}

#menu-ui {
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(circle, rgba(7, 15, 30, 0.72), rgba(4, 8, 16, 0.95));
}

#menu-card {
  width: min(90vw, 440px);
  border: 1px solid var(--hud-border);
  border-radius: 16px;
  background: rgba(8, 20, 41, 0.92);
  padding: 22px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.35);
}

#menu-card h1 {
  margin: 0 0 8px;
  letter-spacing: 0.06em;
  font-size: clamp(22px, 3.2vw, 30px);
  text-transform: uppercase;
}

#menu-card p {
  margin: 0 0 14px;
  opacity: 0.8;
}

#name-input {
  width: 100%;
  border: 1px solid var(--hud-border);
  border-radius: 10px;
  padding: 12px;
  font-size: 16px;
  color: var(--hud-text);
  background: rgba(4, 11, 24, 0.8);
}

#start-btn {
  width: 100%;
  margin-top: 12px;
  border: 0;
  border-radius: 10px;
  padding: 12px;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #00101f;
  background: linear-gradient(135deg, var(--accent), #84f7ff);
  cursor: pointer;
}

#menu-note {
  margin-top: 10px;
  font-size: 13px;
  opacity: 0.75;
}

#ship-color-picker {
  margin-top: 12px;
}

#ship-color-label {
  margin-bottom: 8px;
  font-size: 12px;
  letter-spacing: 0.06em;
  opacity: 0.82;
  text-transform: uppercase;
}

#ship-color-options {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}

.ship-color-swatch {
  height: 30px;
  border-radius: 8px;
  border: 1px solid rgba(214, 240, 255, 0.3);
  background: var(--swatch-color);
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, border-color 120ms ease;
}

.ship-color-swatch.active {
  border-color: rgba(255, 255, 255, 0.95);
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

#hud {
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  right: max(10px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  pointer-events: none;
}

.hud-left,
.hud-right {
  display: flex;
  gap: 8px;
  align-items: center;
}

.hud-block {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--hud-border);
  background: var(--hud-bg);
  font-size: 13px;
}

#status-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--danger);
}

#status-dot.connected {
  background: var(--ok);
}

/* Health bar */
#hp-bar-wrap {
  position: relative;
  width: 100px;
  height: 16px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

#hp-bar-fill {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--ok), #6fe8a0);
  transition: width 200ms ease, background 200ms ease;
}

#hp-bar-wrap.hp-low #hp-bar-fill {
  background: linear-gradient(90deg, var(--danger), #ff9e5c);
}

#hp-bar-wrap.hp-crit #hp-bar-fill {
  background: linear-gradient(90deg, #ff3344, var(--danger));
}

#hud-hp {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.6);
  z-index: 1;
}

/* Color cycle button — small swatch circle */
#color-cycle-btn {
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.5);
  background: var(--ship-color, var(--accent));
  cursor: pointer;
  pointer-events: auto;
  padding: 0;
  transition: transform 120ms ease, border-color 120ms ease;
}

@media (pointer: coarse) {
  #color-cycle-btn {
    width: 44px;
    height: 44px;
  }
}

#color-cycle-btn:hover {
  transform: scale(1.15);
  border-color: #fff;
}

#update-banner {
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 12px;
  border-radius: 999px;
  border: 1px solid var(--hud-border);
  background: rgba(11, 28, 57, 0.95);
  display: flex;
  align-items: center;
  gap: 8px;
}

#update-btn {
  border: 0;
  border-radius: 999px;
  padding: 6px 10px;
  font-weight: 700;
  background: var(--accent);
  color: #00111f;
}


/* Leaderboard */
#leaderboard {
  top: max(10px, env(safe-area-inset-top));
  right: max(10px, env(safe-area-inset-right));
  width: 220px;
  border: 1px solid var(--hud-border);
  border-radius: 10px;
  background: rgba(7, 15, 30, 0.82);
  backdrop-filter: blur(6px);
  font-size: 12px;
  pointer-events: auto;
  overflow: hidden;
}

#lb-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  border-bottom: 1px solid var(--hud-border);
}

#lb-title {
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-size: 11px;
  opacity: 0.85;
}

#lb-toggle {
  border: none;
  background: none;
  color: var(--hud-text);
  font-size: 16px;
  cursor: pointer;
  padding: 0 4px;
  opacity: 0.7;
  line-height: 1;
}

#lb-table {
  width: 100%;
  border-collapse: collapse;
}

#lb-table th {
  padding: 4px 6px;
  text-align: left;
  font-size: 10px;
  text-transform: uppercase;
  opacity: 0.5;
  font-weight: 600;
  border-bottom: 1px solid rgba(120, 190, 255, 0.1);
}

#lb-table th:first-child { width: 18px; text-align: center; }
#lb-table th:nth-child(3),
#lb-table th:nth-child(4),
#lb-table th:nth-child(5) { text-align: right; }

#lb-body tr {
  transition: background 200ms;
}

#lb-body tr:nth-child(even) {
  background: rgba(120, 190, 255, 0.04);
}

#lb-body tr.lb-self {
  background: rgba(95, 216, 255, 0.12);
}

#lb-body td {
  padding: 4px 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#lb-body td:first-child { text-align: center; opacity: 0.5; }
#lb-body td:nth-child(2) { max-width: 80px; }
#lb-body td:nth-child(3) { text-align: right; color: var(--accent); font-weight: 600; }
#lb-body td:nth-child(4) { text-align: right; color: var(--ok); }
#lb-body td:nth-child(5) { text-align: right; color: var(--danger); }

#leaderboard.collapsed #lb-table { display: none; }

#touch-ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 19;
}

.stick-zone {
  position: absolute;
  bottom: max(18px, env(safe-area-inset-bottom));
  width: clamp(110px, 30vw, 150px);
  height: clamp(110px, 30vw, 150px);
  border-radius: 999px;
  border: 1px dashed rgba(180, 225, 255, 0.15);
  background: rgba(95, 216, 255, 0.04);
  pointer-events: auto;
  touch-action: none;
}

#left-stick {
  left: max(14px, env(safe-area-inset-left));
}

.stick-thumb {
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 999px;
  transform: translate(-50%, -50%);
  border: 1px solid rgba(95, 216, 255, 0.55);
  background: rgba(95, 216, 255, 0.22);
  top: 50%;
  left: 50%;
  /* Spring-back animation when finger lifts */
  transition: left 80ms ease-out, top 80ms ease-out;
  pointer-events: none;
}

/* Disable transition during active drag for instant response.
   Uses JS class instead of :active — :active can drop unpredictably
   when pointer capture moves the finger outside element bounds. */
.stick-zone.dragging .stick-thumb {
  transition: none;
}

/* Inner ring — aim-only deadzone boundary (matches THRUST_THRESHOLD = 0.4) */
.stick-zone::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40%;
  height: 40%;
  transform: translate(-50%, -50%);
  border-radius: 999px;
  border: 1px dashed rgba(180, 225, 255, 0.18);
  pointer-events: none;
  transition: border-color 150ms ease;
}

/* Visual feedback: inner ring brightens + border shifts when thrusting */
.stick-zone.thrusting {
  border-color: rgba(120, 255, 180, 0.25);
}

.stick-zone.thrusting::after {
  border-color: rgba(120, 255, 180, 0.3);
}

.stick-zone.thrusting .stick-thumb {
  border-color: rgba(120, 255, 180, 0.7);
  background: rgba(120, 255, 180, 0.25);
}

/* First-time tutorial hint inside joystick zone */
.stick-hint {
  position: absolute;
  bottom: -22px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 9px;
  color: rgba(180, 225, 255, 0.5);
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 400ms ease;
}

.stick-zone.thrusting .stick-hint {
  opacity: 0;
}

/* Leaderboard */
#leaderboard {
  top: max(48px, calc(env(safe-area-inset-top) + 42px));
  right: max(10px, env(safe-area-inset-right));
  width: 160px;
  border: 1px solid var(--hud-border);
  border-radius: 10px;
  background: rgba(5, 12, 25, 0.65);
  backdrop-filter: blur(6px);
  padding: 8px 10px;
  pointer-events: none;
}

#lb-title {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.45;
  margin-bottom: 4px;
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 2px 0;
  font-size: 11px;
  line-height: 1.2;
}

.lb-rank {
  width: 14px;
  text-align: right;
  opacity: 0.4;
  font-size: 10px;
  font-variant-numeric: tabular-nums;
}

.lb-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  opacity: 0.8;
}

.lb-score {
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  font-size: 11px;
}

.lb-row.lb-self .lb-name {
  color: var(--ok);
  opacity: 1;
}

.lb-row.lb-self .lb-score {
  color: var(--ok);
}

/* Touch action buttons (fire / missile) */
#touch-buttons {
  position: absolute;
  bottom: max(18px, env(safe-area-inset-bottom));
  right: max(14px, env(safe-area-inset-right));
  pointer-events: auto;
}

#btn-fire {
  position: absolute;
  bottom: 0;
  right: 0;
  width: clamp(68px, 10vw, 88px);
  height: clamp(68px, 10vw, 88px);
  border-radius: 999px;
  border: 2px solid rgba(255, 100, 100, 0.5);
  background: rgba(255, 80, 80, 0.18);
  color: rgba(255, 200, 200, 0.9);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 0 20px rgba(255, 80, 80, 0.15);
}

#btn-fire:active {
  background: rgba(255, 80, 80, 0.45);
  border-color: rgba(255, 120, 120, 0.9);
  box-shadow: 0 0 30px rgba(255, 80, 80, 0.35);
}


@media (max-width: 480px) {
  .hud-block {
    padding: 6px 8px;
    font-size: 11px;
    gap: 6px;
  }

  .hud-left,
  .hud-right {
    gap: 4px;
  }

  #hp-bar-wrap {
    width: 70px;
    height: 14px;
  }

  #leaderboard {
    width: 130px;
  }

  .lb-row {
    font-size: 10px;
  }
}

@media (pointer: fine) {
  #touch-ui {
    display: none;
  }
}

/* Portrait orientation warning — only shown on touch devices in portrait */
#portrait-warning {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--bg-0);
  color: var(--hud-text);
  justify-content: center;
  align-items: center;
  flex-direction: column;
  gap: 12px;
  font-family: "Trebuchet MS", system-ui, sans-serif;
  font-size: 18px;
  text-align: center;
  padding: 20px;
}

#portrait-dismiss {
  margin-top: 8px;
  padding: 8px 20px;
  border: 1px solid rgba(180, 225, 255, 0.2);
  border-radius: 6px;
  background: transparent;
  color: rgba(180, 225, 255, 0.5);
  font-size: 13px;
  cursor: pointer;
}

@media (pointer: coarse) and (orientation: portrait) {
  #portrait-warning:not(.dismissed) {
    display: flex;
  }
}

/* Respect reduced-motion preferences */
@media (prefers-reduced-motion: reduce) {
  .stick-thumb {
    transition: none;
  }
  .stick-zone::after {
    transition: none;
  }
  #color-cycle-btn {
    transition: none;
  }
  .stick-hint {
    transition: none;
  }
}
