/* ==========================================================
   AlienX – Retro-Styling, responsiv für PC und Handy
   ========================================================== */

:root {
  --bg-dark: #0f0a1e;
  --bg-mid: #1d1340;
  --bg-card: #1a1138;
  --accent: #ff77a8;
  --accent-2: #29adff;
  --text: #f4ecff;
  --muted: #a99bc9;
  --frame: #3b2a6b;
  --danger: #ff5252;
  --pixel-font: "Press Start 2P", ui-monospace, "Courier New", monospace;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 14px;
  color: var(--text);
  font-family: var(--pixel-font);
  background-color: var(--bg-dark);
  background-image:
    radial-gradient(ellipse at top, rgba(126, 37, 83, 0.45), transparent 60%),
    radial-gradient(ellipse at bottom, rgba(41, 173, 255, 0.18), transparent 55%),
    repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.025) 0 1px, transparent 1px 4px);
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Titel ---------- */

.title-bar {
  text-align: center;
}

.title-bar h1 {
  margin: 0;
  font-size: clamp(1.2rem, 3.2vw, 2rem);
  letter-spacing: 0.15em;
  text-shadow: 3px 3px 0 var(--accent), 6px 6px 0 rgba(0, 0, 0, 0.5);
}

.title-bar h1 span {
  color: var(--accent-2);
}

.subtitle {
  margin: 8px 0 0;
  font-size: clamp(0.45rem, 1.2vw, 0.6rem);
  color: var(--muted);
}

/* ---------- Spielfeld ---------- */

.stage {
  display: flex;
  justify-content: center;
  width: 100%;
}

.screen {
  position: relative;
  padding: 8px;
  background: var(--frame);
  border: 4px solid #000;
  border-radius: 6px;
  box-shadow:
    0 0 0 4px var(--accent),
    0 0 40px rgba(255, 119, 168, 0.35),
    8px 8px 0 4px rgba(0, 0, 0, 0.6);
}

.screen::after {
  content: "";
  position: absolute;
  inset: 8px;
  z-index: 3;
  pointer-events: none;
  /* Bewusst ohne mix-blend-mode: das würde jedes Bild teuer neu mischen */
  background: repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.08) 0 1px, transparent 1px 3px);
}

#game {
  display: block;
  width: min(1920px, calc(100vw - 56px), calc((100vh - 190px) * 16 / 9));
  width: min(1920px, calc(100vw - 56px), calc((100dvh - 190px) * 16 / 9));
  min-width: 280px;
  height: auto;
  aspect-ratio: 16 / 9;
  background: #000;
  outline: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* ---------- Overlays & Menüs ---------- */

.overlay {
  position: absolute;
  inset: 8px;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow: auto;
  background: rgba(10, 5, 25, 0.8);
}

.hidden {
  display: none !important;
}

.menu-card {
  width: 100%;
  max-width: 780px;
  max-height: 100%;
  overflow: auto;
  margin: auto;
  padding: 18px 20px;
  background: var(--bg-card);
  border: 3px solid #000;
  border-radius: 6px;
  box-shadow: 0 0 0 3px var(--accent), 6px 6px 0 3px rgba(0, 0, 0, 0.6);
  font-size: 0.6rem;
  line-height: 1.6;
}

.menu-card.small {
  max-width: 420px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  text-align: center;
}

.menu-card h2 {
  margin: 0 0 12px;
  font-size: 0.95rem;
  text-align: center;
  color: var(--accent);
  text-shadow: 2px 2px 0 #000;
}

fieldset {
  margin: 0 0 12px;
  padding: 10px 12px 12px;
  border: 2px solid var(--frame);
  border-radius: 4px;
}

legend {
  padding: 0 6px;
  color: var(--accent-2);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px 14px;
}

label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  color: var(--muted);
}

label.wide {
  margin-top: 10px;
}

label small {
  font-size: 0.85em;
  opacity: 0.8;
}

.row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.row input {
  flex: 1;
  min-width: 0;
}

input,
select {
  font-family: inherit;
  font-size: 0.6rem;
  color: var(--text);
  background: var(--bg-dark);
  border: 2px solid var(--frame);
  border-radius: 3px;
  padding: 7px 8px;
  min-height: 34px;
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--accent-2);
}

input[type="file"] {
  padding: 5px;
  font-size: 0.5rem;
}

input[type="checkbox"] {
  width: 20px;
  height: 20px;
  min-height: 0;
  accent-color: var(--accent);
}

.team-rows {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.team-row {
  display: grid;
  grid-template-columns: 22px 14px 1fr minmax(110px, 0.8fr);
  gap: 8px;
  align-items: center;
}

.team-row .swatch {
  width: 14px;
  height: 14px;
  background: var(--team);
  border: 2px solid #000;
}

.team-row.off input[type="text"],
.team-row.off select,
.team-row.off .swatch {
  opacity: 0.35;
}

.btn {
  font-family: inherit;
  font-size: 0.65rem;
  color: var(--text);
  background: var(--bg-mid);
  border: 2px solid #000;
  border-radius: 4px;
  padding: 10px 14px;
  cursor: pointer;
  box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.4), 3px 3px 0 var(--frame);
}

.btn:hover {
  filter: brightness(1.2);
}

.btn:active {
  transform: translate(2px, 2px);
  box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.4);
}

.btn.primary {
  background: var(--accent);
  color: #1a0a20;
}

.btn.danger {
  background: #5a1a2a;
}

.btn.icon {
  padding: 6px 10px;
  min-height: 34px;
  font-size: 0.8rem;
}

.btn.start {
  display: block;
  width: 100%;
  padding: 14px;
  font-size: 0.85rem;
}

.help {
  margin-top: 14px;
  color: var(--muted);
}

.help summary {
  cursor: pointer;
  color: var(--accent-2);
}

.help-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}

.help h3 {
  font-size: 0.65rem;
  color: var(--text);
}

.help ul {
  margin: 0;
  padding-left: 16px;
}

.help li {
  margin-bottom: 6px;
}

.map-code code {
  font-size: 0.65rem;
  color: var(--accent-2);
  word-break: break-all;
}

.stats {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.55rem;
}

.stats th,
.stats td {
  padding: 6px 4px;
  border-bottom: 1px solid var(--frame);
}

.stats th {
  color: var(--muted);
}

.rotate-icon {
  font-size: 2.5rem;
}

/* ---------- Waffenmenü ---------- */

.menu-card.weapons {
  max-width: 720px;
}

.weapon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  gap: 8px;
}

.weapon-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 4px 8px;
  font-family: inherit;
  font-size: 0.45rem;
  line-height: 1.4;
  color: var(--text);
  background: var(--bg-mid);
  border: 2px solid #000;
  border-radius: 4px;
  cursor: pointer;
}

.weapon-btn:hover,
.weapon-btn:focus-visible {
  border-color: var(--accent-2);
  outline: none;
}

.weapon-btn.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.weapon-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.w-icon {
  font-size: 1.6rem;
  line-height: 1;
}

.w-ammo {
  position: absolute;
  top: 4px;
  right: 5px;
  color: #ffec27;
}

.w-key {
  position: absolute;
  top: 4px;
  left: 5px;
  color: var(--muted);
}

.weapon-hint {
  min-height: 2.6em;
  margin: 12px 0 0;
  text-align: center;
  color: var(--muted);
}

/* ---------- Steuerungs-Legende (PC) ---------- */

.controls ul {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 16px;
  margin: 0;
  padding: 0;
  list-style: none;
  font-size: clamp(0.4rem, 1vw, 0.52rem);
  color: var(--muted);
}

.controls li {
  display: flex;
  align-items: center;
  gap: 4px;
}

kbd {
  display: inline-block;
  min-width: 1.8em;
  padding: 3px 5px;
  font-family: inherit;
  font-size: 1em;
  text-align: center;
  color: var(--text);
  background: var(--bg-mid);
  border: 2px solid #000;
  border-radius: 3px;
  box-shadow: inset -2px -2px 0 rgba(0, 0, 0, 0.45), 2px 2px 0 var(--accent);
}

.footer {
  margin-top: auto;
  font-size: 0.45rem;
  color: var(--muted);
  opacity: 0.7;
}

/* ==========================================================
   Touch-Modus (Handy & Tablet): Vollbild + Bildschirm-Buttons
   ========================================================== */

.touch-ui {
  display: none;
}

body.touch {
  padding: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body.touch .title-bar,
body.touch .controls,
body.touch .footer {
  display: none;
}

body.touch .stage {
  position: fixed;
  inset: 0;
}

body.touch .screen {
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}

body.touch .screen::after {
  display: none;
}

body.touch #game {
  width: 100%;
  height: 100%;
  min-width: 0;
  aspect-ratio: auto;
}

body.touch .overlay {
  inset: 0;
  padding: 10px calc(10px + var(--safe-r)) 10px calc(10px + var(--safe-l));
}

body.touch .menu-card {
  padding: 14px;
}

body.touch.playing .touch-ui {
  display: block;
  position: absolute;
  inset: 0;
  z-index: 5;
  pointer-events: none;
}

.tb {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  padding: 0;
  font-family: inherit;
  font-size: 1.1rem;
  color: #fff;
  background: rgba(29, 19, 64, 0.55);
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-radius: 50%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.tb span {
  font-size: 0.32rem;
  margin-top: 2px;
}

.tb.pressed,
.tb:active {
  background: rgba(255, 119, 168, 0.6);
  border-color: #fff;
}

.touch-pad {
  position: absolute;
  left: calc(14px + var(--safe-l));
  bottom: calc(14px + var(--safe-b));
  display: grid;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 4px;
}

.touch-pad .up { grid-area: 1 / 2; }
.touch-pad .left { grid-area: 2 / 1; }
.touch-pad .right { grid-area: 2 / 3; }
.touch-pad .down { grid-area: 3 / 2; }

.touch-actions {
  position: absolute;
  right: calc(14px + var(--safe-r));
  bottom: calc(14px + var(--safe-b));
  width: 190px;
  height: 180px;
}

.touch-actions .fire {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 92px;
  height: 92px;
  font-size: 0.6rem;
  background: rgba(229, 57, 53, 0.65);
  border-color: rgba(255, 255, 255, 0.6);
}

.touch-actions .jump { position: absolute; right: 100px; bottom: 0; }
.touch-actions .flip { position: absolute; right: 100px; bottom: 62px; width: 48px; height: 48px; }
.touch-actions .weapon { position: absolute; right: 18px; bottom: 102px; font-size: 1.4rem; }
.touch-actions .alien { position: absolute; right: 150px; bottom: 112px; width: 44px; height: 44px; font-size: 0.9rem; }

.touch-side {
  position: absolute;
  left: calc(14px + var(--safe-l));
  top: 50%;
  transform: translateY(-60%);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tb.small {
  width: 40px;
  height: 40px;
  font-size: 0.85rem;
  border-radius: 8px;
}

body.cpu-turn .touch-pad,
body.cpu-turn .touch-actions {
  opacity: 0.25;
  pointer-events: none;
}

/* Niedrige Höhe (Handy quer): Buttons etwas kleiner */
@media (max-height: 420px) {
  .tb { width: 48px; height: 48px; font-size: 0.95rem; }
  .touch-pad { grid-template-columns: repeat(3, 48px); grid-template-rows: repeat(3, 48px); }
  .touch-actions .fire { width: 78px; height: 78px; }
  .touch-actions .jump { right: 86px; }
  .touch-actions .flip { right: 86px; bottom: 54px; width: 42px; height: 42px; }
  .touch-actions .weapon { bottom: 86px; right: 14px; }
  .touch-actions .alien { right: 134px; bottom: 98px; }
  .touch-side { transform: translateY(-75%); }
  .tb.small { width: 36px; height: 36px; }
}

@media (max-width: 640px) {
  body:not(.touch) {
    padding: 10px;
  }

  .screen {
    padding: 4px;
    border-width: 2px;
  }

  .screen::after,
  .overlay {
    inset: 4px;
  }

  #game {
    width: calc(100vw - 32px);
  }
}

/* ---------- Online ---------- */

.start-row {
  display: flex;
  gap: 10px;
}

.start-row .start {
  flex: 1;
}

.online-btn {
  background: var(--accent-2);
  color: #04121f;
}

.divider {
  text-align: center;
  color: var(--muted);
  font-size: 0.5rem;
}

.net-error {
  min-height: 1em;
  margin: 0;
  color: var(--danger);
}

.hint {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.5rem;
}

.room-code {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.room-code strong {
  font-size: 1.2rem;
  letter-spacing: 0.2em;
  color: var(--accent-2);
}

.lobby-slot {
  display: grid;
  grid-template-columns: 14px 1fr auto;
  gap: 10px;
  align-items: center;
  min-height: 34px;
}

.lobby-slot .swatch {
  width: 14px;
  height: 14px;
  background: var(--team);
  border: 2px solid #000;
}

.lobby-slot .tag {
  color: var(--muted);
  font-size: 0.85em;
}

.lobby-slot.offline .who {
  opacity: 0.5;
}

#lobby-form fieldset:disabled {
  opacity: 0.7;
}

.net-status {
  position: absolute;
  top: 70px;
  left: 50%;
  z-index: 6;
  transform: translateX(-50%);
  padding: 5px 10px;
  font-size: 0.45rem;
  white-space: nowrap;
  color: var(--text);
  background: rgba(15, 10, 30, 0.8);
  border: 2px solid var(--accent-2);
  border-radius: 4px;
  pointer-events: none;
}

.net-status.bad {
  border-color: var(--danger);
  color: #ffcdd2;
}

.net-status.mine {
  border-color: #69f0ae;
  color: #b9f6ca;
}

body:not(.online) .online-only,
body.online .offline-only,
body:not(.is-host) .host-only {
  display: none !important;
}

body.touch .net-status {
  top: auto;
  bottom: 70px;
}
