:root {
  --bg: #05070d;
  --panel: #0c1420;
  --line: #1d3247;
  --text: #cfe9ff;
  --accent: #3be8ff;
  --hot: #ff5a3c;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  height: 100%;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, Menlo, Consolas, monospace;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#stage {
  position: fixed;
  inset: 0;
  touch-action: none;
  background: var(--bg);
}

#emulator,
#remote {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: #000;
}

#remote {
  object-fit: contain;
  /* the stream is <=320x240 pixel art: crisp nearest-neighbor upscale, and
     it stays legible if bandwidth adaptation lowers the encode resolution */
  image-rendering: pixelated;
}

/* EmulatorJS fills its container */
#emulator canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  image-rendering: pixelated; /* cosmetic parity with the guest's view */
}

/* ---- HUD ---- */
#hud {
  position: absolute;
  top: max(6px, env(safe-area-inset-top));
  left: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  pointer-events: none;
  font-size: 11px;
  letter-spacing: 1px;
  z-index: 5;
}

#room { color: rgba(207, 233, 255, 0.45); }
#rtc { color: rgba(207, 233, 255, 0.45); margin-left: auto; }
#rtc[data-s="connected"] { color: #3dff8a; }
#rtc[data-s="failed"], #rtc[data-s="disconnected"] { color: #ff3d3d; }

#conn {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ffb02e;
}
#conn[data-s="open"] { background: #3dff8a; box-shadow: 0 0 8px #3dff8a; }
#conn[data-s="closed"] { background: #ff3d3d; }

/* guest-only stream health: green = smooth, amber = strained, red = struggling */
#net {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #ffb02e;
}
#net[data-s="good"] { background: #3dff8a; box-shadow: 0 0 8px #3dff8a; }
#net[data-s="bad"] { background: #ff3d3d; }

/* ---- shared touch controls (identical for host and guest) ---- */
#touch-controls {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none; /* only the widgets themselves take touches */
}

/* 360-degree joystick: dark plastic mount, red ball top */
#touch-controls .joy {
  position: absolute;
  left: calc(22px + env(safe-area-inset-left));
  bottom: calc(26px + env(safe-area-inset-bottom));
  width: 136px;
  height: 136px;
  border-radius: 50%;
  background: radial-gradient(circle at 50% 42%, #34383f 0%, #222529 55%, #0f1114 100%);
  box-shadow:
    inset 0 2px 5px rgba(255, 255, 255, 0.08),
    inset 0 -8px 14px rgba(0, 0, 0, 0.6),
    0 6px 14px rgba(0, 0, 0, 0.55);
  pointer-events: auto;
  touch-action: none;
}

#touch-controls .joy-nub {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 28%, #ff9384 0%, #d8281c 45%, #7e1009 100%);
  box-shadow:
    0 6px 10px rgba(0, 0, 0, 0.55),
    inset 0 -6px 10px rgba(0, 0, 0, 0.35);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

#touch-controls .joy-nub.homing {
  transition: transform 0.1s ease-out;
}

/* two convex plastic fire buttons (red HARD + yellow SOFT) with bezels */
#touch-controls .btn {
  position: absolute;
  width: 72px;
  height: 72px;
  border-radius: 50%;
  border: 7px solid #16171b;
  box-shadow:
    0 5px 10px rgba(0, 0, 0, 0.55),
    inset 0 6px 9px rgba(255, 255, 255, 0.38),
    inset 0 -9px 14px rgba(0, 0, 0, 0.4);
  pointer-events: auto;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 1px;
}

#touch-controls .btn-fire1 {
  right: calc(98px + env(safe-area-inset-right));
  bottom: calc(28px + env(safe-area-inset-bottom));
  background: radial-gradient(circle at 38% 30%, #ff8274 0%, #d8261a 55%, #7e0f08 100%);
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

#touch-controls .btn-fire2 {
  right: calc(18px + env(safe-area-inset-right));
  bottom: calc(98px + env(safe-area-inset-bottom));
  background: radial-gradient(circle at 38% 30%, #ffe08a 0%, #e9a91f 55%, #8a5d07 100%);
  color: #5b3c06;
  text-shadow: 0 1px 1px rgba(255, 255, 255, 0.3);
}

#touch-controls .btn.pressed {
  transform: translateY(2px);
  filter: brightness(0.85);
  box-shadow:
    0 2px 5px rgba(0, 0, 0, 0.55),
    inset 0 3px 6px rgba(255, 255, 255, 0.25),
    inset 0 -5px 9px rgba(0, 0, 0, 0.4);
}

/* coin + start: rectangular cabinet buttons, bottom center */
#touch-controls .meta {
  position: absolute;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 9px;
}

#touch-controls .meta-row {
  display: flex;
  gap: 12px;
}

/* desktop host: presence/announcements/talk only, no game touch widgets */
#touch-controls.desktop .joy,
#touch-controls.desktop .btn,
#touch-controls.desktop .btn-coin,
#touch-controls.desktop .btn-start,
#touch-controls.desktop .coin-hint {
  display: none;
}

/* who's on court: P1/P2 names + waiting count */
#presence {
  display: flex;
  gap: 14px;
  align-items: center;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1px;
  white-space: nowrap;
  color: #dfe7ef;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(232, 228, 216, 0.2);
  padding: 6px 11px;
  border-radius: 6px;
  pointer-events: none;
}

#presence:empty {
  display: none;
}

#presence .dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  margin-right: 5px;
  vertical-align: 1px;
  background: #3dff8a;
  box-shadow: 0 0 6px rgba(61, 255, 138, 0.7);
}

#presence .dot.off {
  background: transparent;
  border: 1px solid rgba(223, 231, 239, 0.5);
  box-shadow: none;
}

#presence .wait {
  color: #ffb02e;
}

/* transient join/leave announcement, floats above the presence stack */
#announce {
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%);
  white-space: nowrap;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 2px;
  color: #fff;
  background: rgba(200, 16, 46, 0.85);
  padding: 8px 14px;
  border-radius: 8px;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
}

#announce.show {
  opacity: 1;
}

#touch-controls .coin-hint {
  font-size: 10px;
  letter-spacing: 1px;
  white-space: nowrap;
  color: #e8e4d8;
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(232, 228, 216, 0.25);
  padding: 6px 11px;
  border-radius: 6px;
  pointer-events: none;
  transition: opacity 0.5s;
}

#touch-controls .coin-hint.gone {
  opacity: 0;
}

/* in portrait the bottom center belongs to the fire buttons: lift the pills
   above the whole control cluster instead */
@media (orientation: portrait) {
  #touch-controls .meta {
    bottom: calc(196px + env(safe-area-inset-bottom));
  }
}

/* in landscape the game fills the full height, so the bottom center sits on
   top of the court: tuck the meta stack into the left side bar, above the
   joystick, where the letterbox is black anyway */
@media (orientation: landscape) {
  #touch-controls .meta {
    left: calc(14px + env(safe-area-inset-left));
    bottom: auto;
    top: 34px;
    transform: none;
    align-items: flex-start;
    max-width: 170px;
  }

  #touch-controls .meta-row {
    flex-wrap: wrap;
    max-width: 160px;
  }

  #touch-controls .coin-hint {
    white-space: normal;
    text-align: left;
    max-width: 160px;
  }

  #presence {
    flex-wrap: wrap;
    gap: 4px 12px;
    max-width: 160px;
  }

  #announce {
    /* float over the game top-center: transient, so overlap is fine */
    position: fixed;
    bottom: auto;
    top: calc(34px + env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
  }
}

#touch-controls .btn-small {
  padding: 10px 16px;
  border: 2px solid #0b0c0f;
  border-radius: 7px;
  background: linear-gradient(#3c4047, #1e2025 60%, #141519);
  box-shadow:
    0 4px 7px rgba(0, 0, 0, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.18);
  color: #e8e4d8;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2px;
  pointer-events: auto;
  touch-action: none;
}

#touch-controls .btn-small.pressed {
  transform: translateY(1px);
  filter: brightness(0.8);
}

/* walkie-talkie: tap to open the line, tap again to close */
#talk-btn.talking {
  background: linear-gradient(#e0301e, #8c1407);
  border-color: #5e0d04;
  color: #fff;
  transform: translateY(1px);
  box-shadow:
    0 1px 3px rgba(0, 0, 0, 0.5),
    0 0 14px rgba(224, 48, 30, 0.7);
}

#talk-btn.denied {
  opacity: 0.4;
}

/* ---- overlay: a squash court ---- */
/* Backdrop = darkened hall with a wooden court floor; card = the front wall
   with its red out-line on top and the tin at the bottom. */
:root {
  --court-red: #c8102e;
  --court-red-dark: #8e0b20;
  --wall: #f1ede1;
  --wall-text: #1c2733;
  --floor: #b07b3f;
}

#overlay {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(
      to bottom,
      rgba(3, 5, 10, 0.92) 0%,
      rgba(3, 5, 10, 0.88) 62%,
      rgba(176, 123, 63, 0.30) 63%,
      rgba(143, 98, 47, 0.38) 100%
    );
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

/* short line across the "floor" */
#overlay::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 22%;
  height: 3px;
  background: rgba(200, 16, 46, 0.55);
}

#overlay[hidden] { display: none; }

#overlay-card {
  position: relative;
  z-index: 1;
  max-width: 340px;
  width: 88%;
  padding: 24px 20px 64px;
  text-align: center;
  background: linear-gradient(#f6f3e9, var(--wall) 60%, #e7e1d0);
  color: var(--wall-text);
  border: none;
  border-top: 6px solid var(--court-red); /* the out line */
  border-radius: 8px 8px 4px 4px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 13px;
}

/* the tin */
#overlay-card::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 40px;
  background: repeating-linear-gradient(
    135deg,
    var(--court-red) 0 16px,
    var(--court-red-dark) 16px 32px
  );
  border-top: 3px solid #6d081a;
  border-radius: 0 0 4px 4px;
}

#overlay-card h1 {
  font-size: 30px;
  letter-spacing: 7px;
  color: var(--court-red);
  text-shadow: none;
}

#overlay-card p { font-size: 14px; line-height: 1.45; }
#overlay-card .sub { color: rgba(28, 39, 51, 0.55); font-size: 12px; letter-spacing: 1px; }
#overlay-card .warn { color: #a3540d; font-size: 12px; }

#overlay-card .link {
  font-size: 12px;
  word-break: break-all;
  color: var(--court-red);
  background: rgba(200, 16, 46, 0.06);
  border: 1px dashed rgba(200, 16, 46, 0.4);
  border-radius: 8px;
  padding: 8px;
}

#overlay-card .big {
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 2px;
  padding: 14px;
  color: #fff;
  background: var(--court-red);
  border: none;
  border-radius: 10px;
  cursor: pointer;
}

#overlay-card .manual {
  /* natural aspect ratio inside the card: the whole artwork, never cropped */
  display: block;
  max-width: 100%;
  max-height: 32dvh;
  margin: 0 auto;
  border-radius: 8px;
  border: 1px solid rgba(28, 39, 51, 0.25);
}

#overlay-card input {
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  text-align: center;
  letter-spacing: 2px;
  text-transform: uppercase;
  padding: 12px;
  border: 2px solid rgba(28, 39, 51, 0.3);
  border-radius: 10px;
  background: #fff;
  color: var(--wall-text);
  outline: none;
  width: 100%;
  user-select: text;
  -webkit-user-select: text;
}

#overlay-card input:focus {
  border-color: var(--court-red);
}

#overlay-card .tips {
  border-top: 1px dashed rgba(28, 39, 51, 0.25);
  padding-top: 12px;
  display: grid;
  gap: 5px;
  font-size: 12px;
  color: rgba(28, 39, 51, 0.7);
}

#overlay-card .tips p {
  font-size: 12px;
  line-height: 1.35;
}

#overlay-card .secondary {
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 2px;
  padding: 12px;
  color: var(--court-red);
  background: transparent;
  border: 2px solid var(--court-red);
  border-radius: 10px;
  cursor: pointer;
}

.pulse { animation: pulse 1.4s ease-in-out infinite; }

@keyframes pulse {
  0%, 100% { opacity: 0.35; }
  50% { opacity: 1; }
}
