* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0;
  background: #1a1a2e;
  font-family: 'Courier New', monospace;
  color: #fff;
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
}

/* Scale the whole game to the viewport while keeping the 5:3 ratio.
   Width grows until limited by either 96% of width or the available height
   (minus the HUD bar + help line ~96px). Canvas internal res stays 800x480. */
#game-wrap {
  position: relative;
  width: min(96vw, calc((100vh - 96px) * 5 / 3));
  max-width: 1400px;
  margin: 0 auto;
}

.hidden { display: none !important; }

#hud {
  display: flex; justify-content: space-between;
  background: #000; color: #fff;
  padding: 10px 14px; font-size: 13px; letter-spacing: 1px;
  border: 3px solid #444; border-bottom: none; text-transform: uppercase;
  font-weight: bold;
}
#hud b { color: #ffe66d; margin-left: 6px; }
#hud #hud-item { font-size: 16px; }
#mute-btn {
  background: none; border: none; cursor: pointer;
  font-size: 15px; padding: 0 2px; line-height: 1;
}

#stage { position: relative; line-height: 0; }

canvas {
  display: block; background: #5c94fc;
  border: 3px solid #444;
  image-rendering: pixelated; image-rendering: crisp-edges;
  width: 100%; height: auto;
}

/* ---------- overlays ---------- */
/* fixed to the viewport (not the short canvas) + scrollable so the lobby is
   fully reachable on small/portrait screens; panel auto-centers when it fits. */
.overlay {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; align-items: center;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: rgba(8, 10, 30, 0.82);
  line-height: 1.4;
  padding: 10px; z-index: 20;
}
.overlay.hidden { display: none; }
#countdown { justify-content: center; }   /* countdown stays centered, no scroll needed */

.panel {
  background: #14162e;
  border: 4px solid #ffe66d;
  border-radius: 10px;
  padding: 18px 22px;
  width: 420px; max-width: 94%;
  margin: auto;          /* centers vertically when it fits, top stays reachable when it doesn't */
  box-shadow: 0 10px 0 rgba(0,0,0,0.4);
  text-align: center;
}

.logo {
  margin: 0 0 14px;
  font-size: 34px; font-weight: 900; letter-spacing: 2px;
  color: #ff4757; text-shadow: 3px 3px 0 #000;
}
.logo span { color: #2ed573; }
.logo.small { font-size: 26px; color: #ffe66d; }

.tag { font-size: 12px; color: #c9c9e6; margin: 0 0 16px; }

.field { display: block; text-align: left; margin-bottom: 14px; }
.field span { display: block; font-size: 11px; color: #9aa; margin-bottom: 6px; text-transform: uppercase; letter-spacing: 1px; }
.field input {
  width: 100%; padding: 10px 12px; font-size: 16px;
  background: #0c0e22; color: #fff;
  border: 2px solid #3a3f6a; border-radius: 6px; font-family: inherit;
}
.field input:focus { outline: none; border-color: #ffe66d; }
.field input.err { border-color: #ff4757; }
.field input.err::placeholder { color: #ff8a8a; }

.or-divider { font-size: 10px; color: #889; text-transform: uppercase; letter-spacing: 1px; margin: 14px 0 8px; }
.join-row { display: flex; gap: 6px; }
.join-row input {
  flex: 1; padding: 10px 12px; font-size: 14px; font-family: inherit;
  background: #0c0e22; color: #fff; border: 2px solid #3a3f6a; border-radius: 6px;
  text-transform: uppercase; letter-spacing: 1px;
}
.join-row input::placeholder { letter-spacing: 1px; }
.join-row input:focus { outline: none; border-color: #2ed573; }
.join-row input.err { border-color: #ff4757; }
.join-row .btn { padding: 10px 18px; }

.btn {
  font-family: inherit; font-weight: bold; cursor: pointer;
  border: none; border-radius: 6px; padding: 12px 18px;
  font-size: 15px; letter-spacing: 1px; text-transform: uppercase;
  transition: transform 0.05s;
}
.btn:active { transform: translateY(2px); }
.btn.primary { width: 100%; background: #2ed573; color: #06321a; box-shadow: 0 4px 0 #1c8f4c; }
.btn.primary:active { box-shadow: 0 2px 0 #1c8f4c; }
.btn.small { background: #ffe66d; color: #4a3b00; box-shadow: 0 3px 0 #b59a1e; padding: 8px 12px; font-size: 12px; }

.room-row { display: flex; justify-content: space-between; align-items: center; font-size: 12px; margin-bottom: 10px; }
.room-row b { color: #ffe66d; }
.status { color: #7fe3ff; font-size: 11px; }

.share { display: flex; gap: 6px; margin-bottom: 6px; }
.share input {
  flex: 1; padding: 8px 10px; font-size: 11px;
  background: #0c0e22; color: #9fe; border: 2px solid #3a3f6a; border-radius: 6px; font-family: inherit;
}
.hint { font-size: 10px; color: #888; margin: 4px 0 12px; }

/* prize box */
.prize-box { margin-bottom: 12px; text-align: left; }
.prize-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #ffe66d; margin-bottom: 6px; }
.prize-row { display: flex; gap: 6px; }
.prize-row input {
  flex: 1; padding: 8px 10px; font-size: 12px; font-family: inherit;
  background: #0c0e22; color: #ffe66d; border: 2px solid #6a5a1e; border-radius: 6px;
}
.prize-row input:focus { outline: none; border-color: #ffe66d; }
.prize-display {
  background: #2a230c; border: 2px solid #ffe66d; border-radius: 6px;
  padding: 8px 10px; font-size: 13px; color: #ffe66d; text-align: center;
}
.prize-display b { color: #fff; }

/* character selector */
.char-list {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
  margin-bottom: 12px;
}
.char-cell {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: #0c0e22; border: 2px solid #3a3f6a; border-radius: 6px;
  padding: 4px 2px 3px; cursor: pointer; font-family: inherit;
}
.char-cell canvas { width: 40px; height: 46px; image-rendering: pixelated; }
.char-cell .char-name { font-size: 9px; color: #cdd; }
.char-cell.picked { background: #1a1c38; }
.char-cell.picked .char-name { color: #fff; font-weight: bold; }
.char-cell.taken { opacity: 0.35; cursor: not-allowed; filter: grayscale(0.7); }
.cname { font-size: 10px; color: #8ad; }

.vote-label { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #9aa; margin-bottom: 6px; }
.vote-label span { text-transform: none; letter-spacing: 0; color: #2ed573; }

.legend {
  display: flex; flex-direction: column; gap: 4px;
  background: #0c0e22; border-radius: 6px; padding: 9px 11px;
  margin-bottom: 12px; text-align: left; font-size: 11px; color: #cdd;
}
.legend b { color: #ffe66d; }
.legend .hint2 { color: #889; font-size: 10px; margin-top: 3px; }
.legend .hint2 b { color: #ff8a8a; }
.vote-label span { color: #2ed573; }
.level-list { display: flex; gap: 6px; margin-bottom: 12px; }
.level-btn {
  flex: 1; position: relative; cursor: pointer;
  background: #0c0e22; color: #cdd; border: 2px solid #3a3f6a; border-radius: 6px;
  padding: 8px 4px; font-family: inherit; font-size: 11px; font-weight: bold;
}
.level-btn.picked { border-color: #ffe66d; color: #ffe66d; background: #1a1c38; }
.level-btn .vcount {
  position: absolute; top: -7px; right: -7px;
  background: #2ed573; color: #06321a; border-radius: 50%;
  width: 18px; height: 18px; line-height: 18px; font-size: 11px;
}

.players {
  list-style: none; margin: 0 0 16px; padding: 8px;
  background: #0c0e22; border-radius: 6px;
  max-height: 150px; overflow-y: auto; text-align: left;
}
.players li, .results-list li {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 6px; font-size: 13px;
}
.players li:not(:last-child) { border-bottom: 1px solid #23264a; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 auto; border: 1px solid rgba(255,255,255,0.4); }
.pname { flex: 1; }
.badge { color: #ffe66d; font-weight: bold; }
.pts { color: #ffe66d; font-weight: bold; min-width: 24px; text-align: right; }
.hosttag { font-size: 9px; color: #06321a; background: #2ed573; border-radius: 4px; padding: 1px 4px; margin-left: 4px; vertical-align: middle; }

.results-prize {
  background: #2a230c; border: 2px solid #ffe66d; border-radius: 6px;
  padding: 8px 10px; margin-bottom: 12px; font-size: 13px; color: #ffe66d; text-align: center;
}
.results-prize b { color: #fff; }
.results-prize .champ { color: #ffd24a; font-size: 12px; }
.results-list .gained { color: #2ed573; font-weight: bold; font-size: 12px; margin: 0 6px; }
.results-list .rcoins { color: #ffd24a; font-size: 11px; }

.results-list { list-style: none; margin: 0 0 16px; padding: 0; }
.results-list li { background: #0c0e22; margin-bottom: 5px; border-radius: 6px; padding: 8px 10px; }
.results-list .place { width: 28px; font-size: 16px; }
.results-list .rtime { color: #9fe; font-size: 12px; }

/* ---------- countdown ---------- */
#countdown { background: rgba(8, 10, 30, 0.35); pointer-events: none; }
#countdown-num {
  font-size: 120px; font-weight: 900; color: #ffe66d;
  text-shadow: 6px 6px 0 #000; animation: pop 1s ease-out infinite;
}
@keyframes pop { 0% { transform: scale(0.6); opacity: 0.2; } 40% { transform: scale(1.1); opacity: 1; } 100% { transform: scale(1); opacity: 0.85; } }

#help {
  margin-top: 12px; font-size: 11px; color: #aaa;
  text-align: center; letter-spacing: 0.5px;
}
#help b { color: #ffe66d; }

/* ---------- mobile touch controls ---------- */
/* fixed to the VIEWPORT bottom (not the canvas) so the pad sits at the screen
   edge in any orientation; below the lobby overlay (z 20) so it's hidden in menus. */
#touch-controls {
  display: none;
  position: fixed; left: 0; right: 0; bottom: 0;
  justify-content: space-between; align-items: flex-end;
  padding: 10px; pointer-events: none; z-index: 15;
}
/* show on touch devices, or on phone-sized viewports in EITHER orientation */
body.touch #touch-controls { display: flex; }
@media (pointer: coarse)                          { #touch-controls { display: flex; } }
@media (max-width: 560px)                          { #touch-controls { display: flex; } }
@media (orientation: landscape) and (max-height: 560px) { #touch-controls { display: flex; } #help { display: none; } }
#touch-controls .pad-left, #touch-controls .pad-right { display: flex; gap: 12px; align-items: flex-end; }
.tbtn {
  pointer-events: auto;
  width: 60px; height: 60px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.5); background: rgba(0,0,0,0.4);
  color: #fff; font-size: 22px; line-height: 1; cursor: pointer;
  -webkit-user-select: none; user-select: none; touch-action: none;
}
.tbtn:active { background: rgba(255,255,255,0.3); }
.tbtn.jump { width: 72px; height: 72px; background: rgba(46,213,115,0.35); border-color: #2ed573; }
.tbtn.item { background: rgba(255,230,109,0.3); border-color: #ffe66d; color: #ffe66d; }
.tbtn.atk { background: rgba(255,71,87,0.35); border-color: #ff4757; }
.tbtn.run { background: rgba(0,184,212,0.3); border-color: #00b8d4; }

#reconnect-toast {
  position: fixed; top: 8px; left: 50%; transform: translateX(-50%);
  background: rgba(255,71,87,0.95); color: #fff; z-index: 50;
  padding: 6px 14px; border-radius: 14px; font-size: 12px; font-weight: bold;
  box-shadow: 0 3px 0 rgba(0,0,0,0.3);
}

/* ---------- force landscape on portrait phones ---------- */
#rotate-prompt { display: none; }
@media (orientation: portrait) and (max-width: 600px) {
  #rotate-prompt {
    display: flex; position: fixed; inset: 0; z-index: 9999;
    flex-direction: column; align-items: center; justify-content: center;
    background: #14162e; color: #fff; text-align: center; padding: 24px;
  }
}
.rotate-inner { max-width: 280px; }
.rotate-icon {
  font-size: 64px; display: inline-block;
  animation: rotateHint 1.8s ease-in-out infinite;
}
#rotate-prompt h2 { color: #ffe66d; margin: 14px 0 6px; font-size: 20px; }
#rotate-prompt p { font-size: 13px; color: #cdd; line-height: 1.6; }
#rotate-prompt b { color: #2ed573; }
@keyframes rotateHint {
  0%, 35% { transform: rotate(0deg); }
  55%, 100% { transform: rotate(-90deg); }
}

/* compact HUD + smaller touch buttons on narrow screens */
@media (max-width: 560px) {
  #help { display: none; }
  #hud { font-size: 10px; flex-wrap: wrap; gap: 2px 10px; padding: 6px 8px; }
  #touch-controls { padding: 8px; }
  .tbtn { width: 52px; height: 52px; font-size: 19px; }
  .tbtn.jump { width: 62px; height: 62px; }
  .char-list { gap: 4px; }
  .char-cell canvas { width: 34px; height: 40px; }
}

/* short landscape (phone on its side): smaller buttons so they don't dominate */
@media (orientation: landscape) and (max-height: 480px) {
  .tbtn { width: 48px; height: 48px; font-size: 18px; }
  .tbtn.jump { width: 56px; height: 56px; }
}
