:root {
  --p1: #ff4d4d;
  --p1-dark: #c81e1e;
  --p2: #4d9bff;
  --p2-dark: #1e5fc8;
  --accent: #ffd23f;
  --bg-0: #070b18;
  --bg-1: #0f1733;
  --ink: #f3f6ff;
  --muted: #9aa6c7;
  --panel: rgba(14, 21, 48, 0.82);
  --panel-line: rgba(120, 150, 230, 0.22);
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(120% 120% at 50% 0%, #16224d 0%, var(--bg-0) 60%);
  color: var(--ink);
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  user-select: none;
  touch-action: none;
}

#game-shell {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0;
}

/* The stage holds the canvas at a fixed 16:9 aspect and letterboxes it. */
#stage {
  position: relative;
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border-radius: clamp(0px, 1.4vw, 18px);
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.6), 0 0 0 1px rgba(120, 150, 230, 0.12);
  background: #0a1230;
}

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: optimizeQuality;
  cursor: crosshair;
}

/* ----------------------------- Screens ----------------------------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2.2vh, 24px);
  padding: 4%;
  z-index: 10;
  background: radial-gradient(120% 120% at 50% 0%, rgba(22, 34, 77, 0.86), rgba(7, 11, 24, 0.94));
  backdrop-filter: blur(3px);
  animation: fade-in 0.35s ease both;
}
.screen.hidden, .hidden { display: none !important; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* Logo */
.menu-glow {
  position: absolute;
  width: 60%;
  height: 40%;
  top: 8%;
  background: radial-gradient(closest-side, rgba(255, 210, 63, 0.22), transparent);
  filter: blur(20px);
  pointer-events: none;
}
.logo { text-align: center; line-height: 1; z-index: 1; }
.logo-kick {
  font-size: clamp(28px, 6vh, 56px);
  display: inline-block;
  animation: bob 2.2s ease-in-out infinite;
}
@keyframes bob { 50% { transform: translateY(-10px) rotate(-12deg); } }
.logo h1 {
  font-family: 'Bungee', sans-serif;
  font-size: clamp(34px, 9vh, 86px);
  letter-spacing: 1px;
  background: linear-gradient(180deg, #fff, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.25);
}
.logo h1 span {
  display: block;
  background: linear-gradient(180deg, var(--p2), var(--p1));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.tagline {
  margin-top: 6px;
  color: var(--muted);
  font-weight: 800;
  letter-spacing: 3px;
  text-transform: uppercase;
  font-size: clamp(10px, 1.7vh, 14px);
}

/* Menu options panel */
.menu-panel {
  display: grid;
  gap: clamp(8px, 1.6vh, 16px);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 16px;
  padding: clamp(12px, 2.4vh, 22px) clamp(16px, 3vw, 28px);
  z-index: 1;
  width: min(520px, 92%);
}
.opt-group { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.opt-label {
  font-weight: 800;
  color: var(--muted);
  font-size: clamp(11px, 1.7vh, 14px);
  text-transform: uppercase;
  letter-spacing: 1px;
  white-space: nowrap;
}
.seg {
  display: inline-flex;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 11px;
  padding: 4px;
  gap: 4px;
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
  font-size: clamp(11px, 1.7vh, 14px);
  font-family: inherit;
  padding: 7px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, color 0.15s ease;
}
.seg-btn:hover { color: var(--ink); }
.seg-btn:active { transform: scale(0.94); }
.seg-btn.is-active {
  background: linear-gradient(180deg, var(--accent), #f0a500);
  color: #2a1c00;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}

/* Play button */
.btn-play {
  z-index: 1;
  font-family: 'Bungee', sans-serif;
  font-size: clamp(20px, 4vh, 34px);
  letter-spacing: 2px;
  color: #08210f;
  background: linear-gradient(180deg, #54f08a, #16b85b);
  border: 0;
  border-radius: 16px;
  padding: clamp(12px, 2.2vh, 18px) clamp(36px, 8vw, 70px);
  cursor: pointer;
  box-shadow: 0 7px 0 #0c7a3a, 0 14px 30px rgba(0, 0, 0, 0.4);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}
.btn-play:hover { transform: translateY(-2px); }
.btn-play:active { transform: translateY(5px); box-shadow: 0 2px 0 #0c7a3a, 0 6px 14px rgba(0, 0, 0, 0.4); }
.btn-play.small { font-size: clamp(16px, 2.6vh, 22px); padding: 12px 34px; }

.menu-foot { display: flex; gap: 22px; z-index: 1; flex-wrap: wrap; justify-content: center; }
.link-btn {
  background: none;
  border: 0;
  color: var(--muted);
  font-family: inherit;
  font-weight: 700;
  font-size: clamp(12px, 1.9vh, 15px);
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 8px;
  transition: color 0.15s ease;
}
.link-btn:hover { color: var(--ink); }

/* Cards (howto, pause, gameover) */
.card {
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 18px;
  padding: clamp(20px, 4vh, 40px);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 2.2vh, 20px);
  width: min(560px, 94%);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}
.card h2 {
  font-family: 'Bungee', sans-serif;
  font-size: clamp(24px, 5vh, 44px);
}
.howto-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; width: 100%; }
.ctrl-col {
  background: rgba(0, 0, 0, 0.25);
  border-radius: 12px;
  padding: 14px;
  text-align: left;
}
.ctrl-col h3 { font-size: clamp(14px, 2.2vh, 18px); margin-bottom: 8px; }
.ctrl-col ul { list-style: none; display: grid; gap: 7px; }
.ctrl-col li { font-size: clamp(12px, 1.8vh, 14px); color: var(--muted); }
.ctrl-col b { color: var(--ink); }
.p1-color { color: var(--p1); }
.p2-color { color: var(--p2); }
.howto-tip { color: var(--muted); font-size: clamp(12px, 1.8vh, 14px); max-width: 46ch; }

/* How to Play (first-time onboarding) */
.howto-card { width: min(600px, 94%); }
.howto-goal { color: var(--muted); font-size: clamp(13px, 1.9vh, 15px); max-width: 52ch; }
.howto-goal b { color: var(--ink); }
.howto-section { width: 100%; }
.howto-rows { display: grid; gap: 8px; width: 100%; }
.howto-row {
  display: grid;
  grid-template-columns: 42% 1fr;
  gap: 12px;
  align-items: center;
  background: rgba(0, 0, 0, 0.24);
  border-radius: 10px;
  padding: 9px 13px;
  text-align: left;
}
.howto-row .k { font-weight: 800; color: var(--accent); font-size: clamp(12px, 1.7vh, 14px); }
.howto-row span:last-child { color: var(--muted); font-size: clamp(12px, 1.7vh, 14px); }
.howto-tips { display: flex; flex-direction: column; gap: 7px; width: 100%; text-align: left; }
.howto-tips span { color: var(--muted); font-size: clamp(12px, 1.7vh, 14px); }
.howto-sub { width: 100%; text-align: left; color: var(--ink); font-weight: 800; font-size: clamp(13px, 1.8vh, 15px); margin-top: 6px; }
.howto-note { width: 100%; text-align: left; color: var(--muted); font-size: clamp(11px, 1.6vh, 13px); }
.howto-row b { color: var(--ink); }
.howto-row.p1 .k { color: var(--p1); }
.howto-row.p2 .k { color: var(--p2); }

.result-sub { color: var(--muted); font-weight: 800; letter-spacing: 2px; text-transform: uppercase; font-size: clamp(11px, 1.8vh, 14px); }
.result-title-win { color: var(--accent); }
.result-score {
  font-family: 'Bungee', sans-serif;
  font-size: clamp(36px, 8vh, 72px);
  letter-spacing: 4px;
}

/* Online lobby */
.online-choose { display: flex; flex-direction: column; align-items: center; gap: 14px; width: 100%; }
.online-or { color: var(--muted); font-weight: 800; letter-spacing: 2px; font-size: 12px; text-transform: uppercase; }
.join-row { display: flex; gap: 8px; width: 100%; max-width: 320px; }
#join-code {
  flex: 1;
  min-width: 0;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--panel-line);
  border-radius: 12px;
  color: var(--ink);
  font-family: 'Bungee', monospace;
  font-size: clamp(16px, 2.4vh, 20px);
  letter-spacing: 4px;
  text-align: center;
  text-transform: uppercase;
  padding: 10px 12px;
  outline: none;
}
#join-code:focus { border-color: var(--accent); }
.room-code {
  font-family: 'Bungee', monospace;
  font-size: clamp(34px, 7vh, 60px);
  letter-spacing: 8px;
  color: var(--accent);
  margin: 2px 0;
  cursor: pointer;
  user-select: all;
  -webkit-user-select: all;
}
.room-code:active { transform: scale(0.97); }
.copy-row { display: flex; gap: 18px; flex-wrap: wrap; justify-content: center; }
.online-status { color: var(--muted); font-weight: 700; font-size: clamp(12px, 1.9vh, 15px); min-height: 1.2em; }

/* HUD pause button */
#hud { position: absolute; inset: 0; z-index: 5; pointer-events: none; }
#pause-btn {
  position: absolute;
  top: 3%;
  right: 3%;
  pointer-events: auto;
  width: clamp(38px, 5.5vh, 52px);
  height: clamp(38px, 5.5vh, 52px);
  border-radius: 12px;
  border: 1px solid var(--panel-line);
  background: rgba(10, 16, 40, 0.7);
  color: var(--ink);
  font-size: clamp(16px, 2.6vh, 22px);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
#pause-btn:hover { background: rgba(30, 44, 90, 0.9); }
#pause-btn:active { transform: scale(0.9); }

/* Mobile controls */
#touch-controls { position: absolute; inset: 0; z-index: 6; pointer-events: none; }
#joystick {
  position: absolute;
  left: 5%;
  bottom: 7%;
  width: clamp(96px, 22vw, 150px);
  height: clamp(96px, 22vw, 150px);
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.03));
  border: 2px solid rgba(255, 255, 255, 0.16);
  pointer-events: auto;
  touch-action: none;
}
#joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 42%;
  height: 42%;
  margin: -21% 0 0 -21%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffffff, #aebbe0);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.5);
  transition: transform 0.05s linear;
}
#action-btn {
  position: absolute;
  right: 6%;
  bottom: 9%;
  width: clamp(86px, 19vw, 130px);
  height: clamp(86px, 19vw, 130px);
  border-radius: 50%;
  border: 0;
  pointer-events: auto;
  font-family: 'Bungee', sans-serif;
  font-size: clamp(13px, 2.2vw, 18px);
  color: #2a1c00;
  background: radial-gradient(circle at 35% 30%, #ffe07a, #f0a500);
  box-shadow: 0 8px 0 #b97c00, 0 12px 26px rgba(0, 0, 0, 0.4);
  touch-action: none;
}
#action-btn:active { transform: translateY(5px); box-shadow: 0 3px 0 #b97c00; }

/* Rotate-to-landscape prompt — only on portrait touch devices */
#rotate { display: none; }
@media (orientation: portrait) and (pointer: coarse) {
  #rotate {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(120% 120% at 50% 0%, #16224d 0%, var(--bg-0) 60%);
    padding: 10%;
  }
  .rotate-inner { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 14px; }
  .rotate-icon { font-size: clamp(56px, 18vw, 96px); animation: rotate-spin 2.4s ease-in-out infinite; }
  .rotate-title { font-family: 'Bungee', sans-serif; font-size: clamp(22px, 7vw, 34px); color: var(--ink); }
  .rotate-sub { color: var(--muted); font-weight: 700; font-size: clamp(13px, 4vw, 16px); }
}
@keyframes rotate-spin { 0%, 60% { transform: rotate(0); } 80%, 100% { transform: rotate(-90deg); } }

/* small screens: stack howto controls */
@media (max-width: 560px) {
  .howto-grid { grid-template-columns: 1fr; }
  .opt-group { flex-direction: column; align-items: stretch; gap: 6px; }
  .opt-label { text-align: center; }
  .seg { justify-content: center; }
}
