:root {
  --bg: #1f2a44;
  --card: #ffffff;
  --text: #1f2a44;
  --muted: #5c6784;
  --accent: #ff6b35;
  --accent-dark: #d9481a;
  --line: #e3e7f0;
  --font: ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

button {
  font: inherit;
  font-weight: 700;
  font-size: 1rem;
  border: 0;
  border-radius: 12px;
  padding: 12px 20px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--accent-dark);
  transition: transform 0.05s, box-shadow 0.05s;
}
button:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 var(--accent-dark); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.secondary { background: #e9edf5; color: var(--text); box-shadow: 0 4px 0 #c5ccdb; }

/* ---------- Lobby ---------- */
#lobby {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background:
    radial-gradient(circle at 20% 15%, #34477a 0, transparent 45%),
    radial-gradient(circle at 85% 80%, #3b2f5c 0, transparent 40%),
    var(--bg);
}

.card {
  background: var(--card);
  border-radius: 20px;
  padding: 24px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
  text-align: center;
}

h1 {
  margin: 0;
  font-size: 3rem;
  letter-spacing: -1px;
  color: var(--accent);
  text-shadow: 0 3px 0 var(--accent-dark);
}
h2 { margin: 8px 0 4px; font-size: 1.6rem; }
.sub { margin: 6px 0 18px; color: var(--muted); }

#joinForm { display: flex; gap: 8px; margin-bottom: 16px; }
#nameInput {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* >=16px stops iOS from zooming on focus */
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  outline: none;
}
#nameInput:focus { border-color: var(--accent); }

#playerList {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
#playerList li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 2px solid var(--line);
  border-radius: 14px;
  text-align: left;
  min-height: 64px;
}
#playerList li.me { border-color: var(--accent); background: #fff5f0; }
#playerList canvas { width: 48px; height: 48px; flex: none; }
#playerList .pname { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#playerList .tag { display: block; font-size: 0.75rem; font-weight: 600; color: var(--muted); }

.status { color: var(--muted); margin: 16px 0 12px; min-height: 1.2em; }
.error { color: #d6336c; font-weight: 600; margin: 0 0 12px; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.hint { margin: 18px 0 0; font-size: 0.85rem; color: #98a1b8; }
.touch-hint { display: none; }
body.touch .kbd-hint { display: none; }
body.touch .touch-hint { display: inline; }

/* ---------- Game ---------- */
#game {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #121829;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
#canvas { display: block; }

#banner {
  display: flex;
  align-items: center;
  gap: 10px;
  position: absolute;
  top: calc(env(safe-area-inset-top) + 56px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(31, 42, 68, 0.85);
  color: #fff;
  padding: 8px 14px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  max-width: calc(100% - 32px);
  overflow: hidden;
  text-overflow: ellipsis;
}

#toast {
  position: absolute;
  top: 22%;
  left: 50%;
  transform: translate(-50%, -8px);
  background: rgba(255, 255, 255, 0.95);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 999px;
  font-weight: 800;
  font-size: 1.1rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

#endOverlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(10, 14, 26, 0.55);
}
#endOverlay .card { max-width: 380px; padding: 20px; }
#loserIcon { width: 96px; height: 96px; }

/* ---------- Touch controls ---------- */
#touch { position: absolute; inset: 0; pointer-events: none; }
.pad {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom) + 10px);
  display: flex;
  gap: 4px;
  pointer-events: auto;
  touch-action: none;
}
.pad-left { left: calc(env(safe-area-inset-left) + 10px); }
.pad-right { right: calc(env(safe-area-inset-right) + 10px); }
.tbtn {
  width: 104px;
  height: 96px;
  border-radius: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 38px;
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
  border: 2px solid rgba(255, 255, 255, 0.5);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
}
.tbtn.jump { width: 100px; height: 100px; border-radius: 50%; font-size: 40px; }
.tbtn.active { background: rgba(255, 255, 255, 0.4); transform: scale(0.94); }

/* ---------- Overlays ---------- */
#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20;
  background: var(--bg);
  color: #fff;
  font-weight: 700;
  font-size: 1.2rem;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 16px;
}
.rotate-icon { font-size: 3rem; margin-bottom: 10px; }
@media (orientation: portrait) {
  body.touch.in-game #rotate { display: flex; }
}

#conn {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 10px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 30;
  background: #d6336c;
  color: #fff;
  padding: 6px 14px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 0.9rem;
}

#muteBtn {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 10px);
  right: calc(env(safe-area-inset-right) + 10px);
  z-index: 25;
  width: 42px;
  height: 42px;
  padding: 0;
  border-radius: 50%;
  font-size: 20px;
  line-height: 42px;
  background: rgba(20, 26, 44, 0.7);
  box-shadow: none;
}
#muteBtn:active { transform: scale(0.92); box-shadow: none; }

.avatar-btn {
  position: relative;
  flex: none;
  padding: 0;
  border-radius: 12px;
  background: transparent;
  box-shadow: none;
}
.avatar-btn:active:not(:disabled) { transform: scale(0.92); box-shadow: none; }
.avatar-btn::after {
  content: "↻";
  position: absolute;
  right: -4px;
  bottom: -4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  line-height: 18px;
}
.change-hint { color: var(--accent); }

/* Lobby labels and the mode picker's segmented control */
.round-label { display: block; font-size: 0.8rem; font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr; /* modes in rows of two */
  gap: 4px;
  padding: 4px;
  background: #eef1f7;
  border-radius: 12px;
}
.segmented button {
  flex: 1;
  padding: 8px 0;
  font-size: 0.9rem;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  box-shadow: none;
}
.segmented button:last-child:nth-child(odd) { grid-column: span 2; } /* an odd one out fills its row */
.segmented button:active:not(:disabled) { transform: none; box-shadow: none; }
.segmented button[aria-checked="true"] {
  background: #fff;
  color: var(--accent);
  box-shadow: 0 1px 3px rgba(31, 42, 68, 0.18);
}
.segmented button:disabled { opacity: 1; cursor: default; }
.segmented button:disabled:not([aria-checked="true"]) { opacity: 0.5; }

/* Map picker */
.map-picker { margin-top: 16px; }
.map-options { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.map-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 4px 4px 6px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--muted);
  font-size: 0.8rem;
  box-shadow: none;
}
.map-card canvas { width: 100%; height: auto; aspect-ratio: 120 / 68; border-radius: 8px; display: block; }
.map-card:active:not(:disabled) { transform: scale(0.97); box-shadow: none; }
.map-card[aria-checked="true"] { border-color: var(--accent); color: var(--accent); background: #fff5f0; }
.map-card:disabled { opacity: 1; cursor: default; }
.map-card:disabled:not([aria-checked="true"]) { opacity: 0.55; }
.map-card { position: relative; }
.map-badge {
  position: absolute;
  top: 7px;
  right: 7px;
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(20, 26, 44, 0.75);
  color: #fff;
  font-size: 0.6rem;
  font-weight: 800;
  letter-spacing: 0.5px;
}

/* End-of-round scoreboard */
#endOverlay .card { max-height: calc(100% - 8px); overflow-y: auto; }
#results { margin: 0 0 16px; }
#results table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
#results th { font-size: 0.72rem; font-weight: 700; color: var(--muted); text-align: left; padding: 0 6px 4px; }
#results td { padding: 3px 6px; text-align: left; border-top: 1px solid var(--line); }
#results td:nth-child(n + 3), #results th:nth-child(n + 3) { text-align: right; font-variant-numeric: tabular-nums; }
#results td:first-child { width: 28px; padding-right: 0; }
#results canvas { width: 28px; height: 28px; display: block; }
#results tr.me td { font-weight: 800; }
#results tr.loser td { background: #fff0f0; color: #c92a2a; }
@media (max-height: 480px) {
  #loserIcon { display: none; }
  #endOverlay h2 { font-size: 1.25rem; margin-top: 0; }
  #endOverlay .sub { margin: 2px 0 8px; }
  #results { margin-bottom: 10px; }
}

/* Dash button: sits up-and-left of Jump so a thumb can rock between them.
   --ready (0–1, set from JS) fills the cooldown ring. */
.pad-right { width: 178px; height: 160px; }
.pad-right .tbtn { position: absolute; }
.pad-right .jump { right: 0; bottom: 0; }
.tbtn.dash {
  left: 14px;
  top: 14px;
  width: 76px;
  height: 76px;
  border-radius: 50%;
  font-size: 30px;
  background: conic-gradient(rgba(255, 255, 255, 0.32) calc(var(--ready, 1) * 360deg), rgba(255, 255, 255, 0.05) 0);
}
.tbtn.dash.ready { border-color: #ffe066; color: #ffe066; }
.tbtn.dash.active { background: rgba(255, 255, 255, 0.4); }

/* Game mode picker */
.mode-picker { margin-top: 16px; }
/* Four modes: a 2×2 grid on a narrow card, one row when there's room */
#modeOptions { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
#modeOptions button { white-space: nowrap; }

/* Seconds pickers (round length, hot potato fuse): chunky +/- buttons around a big
   number, with a slider underneath for big jumps */
.seconds-picker { margin-top: 16px; }
.secs-box { padding: 10px 12px 8px; background: #eef1f7; border-radius: 12px; }
.secs-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.secs-row button {
  width: 48px;
  height: 44px;
  padding: 0;
  font-size: 28px;
  line-height: 1;
  touch-action: manipulation;
}
.secs-row button:disabled { background: #c9cfdc; box-shadow: 0 4px 0 #aeb6c7; opacity: 1; }
.step-value { display: flex; align-items: baseline; gap: 4px; font-variant-numeric: tabular-nums; }
.secs-num {
  min-width: 2.2ch;
  text-align: right;
  font-size: 2.4rem;
  font-weight: 800;
  line-height: 1;
  color: var(--accent);
  text-shadow: 0 3px 0 var(--accent-dark);
}
.secs-unit { font-size: 0.85rem; font-weight: 700; color: var(--muted); }
.seconds-picker.locked .secs-num { color: var(--muted); text-shadow: 0 3px 0 #c5ccdb; }

.secs-slider {
  --fill: 50%;
  display: block;
  width: 100%;
  height: 28px;
  margin: 10px 0 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
.secs-slider:disabled { cursor: default; }
.secs-slider::-webkit-slider-runnable-track {
  height: 10px;
  border-radius: 999px;
  background: linear-gradient(to right, var(--accent) var(--fill), #d5dbe7 var(--fill));
}
.secs-slider::-moz-range-track { height: 10px; border-radius: 999px; background: #d5dbe7; }
.secs-slider::-moz-range-progress { height: 10px; border-radius: 999px; background: var(--accent); }
.secs-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -8px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 2px 0 var(--accent-dark);
}
.secs-slider::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  box-shadow: 0 2px 0 var(--accent-dark);
}
.seconds-picker.locked .secs-slider::-webkit-slider-runnable-track { background: linear-gradient(to right, #aeb6c7 var(--fill), #d5dbe7 var(--fill)); }
.seconds-picker.locked .secs-slider::-webkit-slider-thumb { border-color: #aeb6c7; box-shadow: 0 2px 0 #8f98ad; }
.seconds-picker.locked .secs-slider::-moz-range-progress { background: #aeb6c7; }
.seconds-picker.locked .secs-slider::-moz-range-thumb { border-color: #aeb6c7; box-shadow: 0 2px 0 #8f98ad; }
.secs-scale { display: flex; justify-content: space-between; font-size: 0.7rem; font-weight: 700; color: var(--muted); }

/* Spectating banner + follow button */
#followBtn {
  flex: none;
  padding: 4px 10px;
  font-size: 0.8rem;
  border-radius: 999px;
  box-shadow: none;
}
#followBtn:active:not(:disabled) { transform: scale(0.95); box-shadow: none; }
#results tr.winner td { background: #fff9db; color: #a26a00; }
#results td:nth-child(n + 3) { white-space: nowrap; }
#results .results-note { margin: 6px 0 0; font-size: 0.72rem; color: var(--muted); }

/* Free roam: the lobby's session list, and the in-game leave button */
.roam-panel { margin-top: 16px; }
#roamList { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
#roamList li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 2px solid var(--line);
  border-radius: 14px;
  text-align: left;
}
#roamList li.empty { justify-content: center; color: var(--muted); font-size: 0.9rem; border-style: dashed; }
#roamList .faces { display: flex; flex: none; }
#roamList .faces canvas { width: 32px; height: 32px; margin-right: -8px; }
#roamList .info { flex: 1; min-width: 0; margin-left: 8px; }
#roamList .rname { font-weight: 700; }
#roamList .who { display: block; font-size: 0.75rem; font-weight: 600; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#roamList button { flex: none; padding: 8px 14px; font-size: 0.9rem; }
#roamLeaveBtn {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 10px);
  right: calc(env(safe-area-inset-right) + 62px);
  z-index: 25;
  height: 42px;
  padding: 0 16px;
  border-radius: 21px;
  font-size: 0.9rem;
  background: rgba(20, 26, 44, 0.7);
  box-shadow: none;
}
#roamLeaveBtn:active:not(:disabled) { transform: scale(0.95); box-shadow: none; }
