* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: radial-gradient(1200px 800px at 50% 0%, #0e5d9e 0%, #083a66 60%, #062c4d 100%);
  color: #fff;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

canvas {
  position: fixed;
  inset: 0;
  display: block;
}

.hidden { display: none !important; }

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* flex-start + auto-margin on .panel: centred when it fits, top-reachable
     and scrollable when the panel is taller than the screen. */
  justify-content: flex-start;
  background: rgba(4, 22, 38, 0.72);
  backdrop-filter: blur(4px);
  z-index: 20;
  padding: 20px;
  text-align: center;
  /* Allow the panel to scroll into view on short screens (e.g. a phone held
     horizontally) instead of being clipped top and bottom. */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.panel {
  background: linear-gradient(180deg, rgba(12, 48, 78, 0.96), rgba(7, 30, 52, 0.96));
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 18px;
  padding: 30px 34px;
  max-width: 460px;
  width: 100%;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.5);
  /* auto margins keep the panel centred when it fits and scrollable when tall */
  margin: auto;
}

h1 {
  margin: 0 0 4px;
  font-size: 52px;
  letter-spacing: 2px;
  background: linear-gradient(180deg, #9be4ff, #fff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: none;
}

.tagline {
  margin: 0 0 26px;
  color: #bfe3f7;
  font-size: 15px;
}

.btn {
  display: block;
  width: 100%;
  margin: 10px 0;
  padding: 14px 18px;
  font-size: 17px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(180deg, #1b78c9, #0f5598);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.12s ease;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: scale(0.97); }
.btn.secondary { background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.25); }
.btn.small { display: inline-block; width: auto; margin: 6px; padding: 10px 16px; font-size: 14px; }

.row { display: flex; gap: 10px; justify-content: center; }

.field {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin: 14px 0;
  font-size: 15px;
  text-align: left;
}
.field label { flex: 0 0 auto; }
.field select, .field input[type="range"] { flex: 1 1 auto; min-width: 0; }
.field input[type="checkbox"] { width: 20px; height: 20px; accent-color: #2ea8ff; }
.field select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.3);
  background: #0b3455;
  color: #fff;
  font-size: 15px;
}
.field input[type="range"] { accent-color: #2ea8ff; }
.field .val { min-width: 42px; text-align: right; color: #9be4ff; font-weight: 700; }

.panel-footer {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  font-size: 14px;
}
.panel-footer a {
  color: #9be4ff;
  font-weight: 700;
  text-decoration: none;
}
.panel-footer a:hover { text-decoration: underline; }
.panel-footer a:focus-visible { outline: 2px solid #9be4ff; outline-offset: 3px; border-radius: 4px; }

/* HUD */
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
  padding: 12px 14px;
}
.hud-bar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: stretch;
  gap: 12px;
  max-width: 1180px;
  margin: 0 auto;
}
.hud-players {
  grid-column: 1;
  display: flex;
  align-items: stretch;
  justify-content: flex-start;
  gap: 8px;
  min-width: 0;
}
.score {
  display: grid;
  grid-template-columns: auto auto;
  grid-template-areas:
    "name value"
    "meta meta";
  column-gap: 12px;
  row-gap: 1px;
  align-items: baseline;
  align-content: center;
  min-width: 112px;
  padding: 7px 11px 6px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(6, 25, 42, 0.62);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
  font-weight: 800;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}
.score.p1 { color: #8fd0ff; }
.score.p2 { color: #ff9c9c; }
.score-name { grid-area: name; font-size: 15px; letter-spacing: 0.5px; }
.score-value {
  grid-area: value;
  justify-self: end;
  font-size: 20px;
  color: #fff;
  font-variant-numeric: tabular-nums;
}
.score-meta {
  grid-area: meta;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 11px;
  letter-spacing: 0;
  opacity: 0.95;
}
.score-meta .lives { color: #ff7f9f; letter-spacing: 1px; white-space: nowrap; }
.score-meta .dmg { color: #ffd54a; letter-spacing: 0; }
.game-over-text {
  margin: 12px 0 22px;
  color: #dff0ff;
  font-size: 18px;
  line-height: 1.5;
  white-space: pre-line;
}
#levelHud {
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 1px;
  color: #ffd54a;
  background: rgba(255, 213, 74, 0.1);
  border: 1px solid rgba(255, 213, 74, 0.35);
  border-radius: 999px;
  padding: 0 5px;
  white-space: nowrap;
}
#hint {
  position: absolute;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 14px;
  color: #dff0ff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  background: rgba(6, 25, 42, 0.5);
  padding: 6px 16px;
  border-radius: 999px;
}
.hud-buttons {
  grid-column: 3;
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 56px;
  padding: 4px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(6, 25, 42, 0.62);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
  pointer-events: auto;
}
.hud-buttons button {
  height: 42px;
  min-width: 42px;
  padding: 0 11px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.hud-buttons button:hover {
  background: rgba(46, 168, 255, 0.24);
  border-color: rgba(155, 228, 255, 0.45);
}
.hud-buttons .pause-btn { color: #ffd54a; }

/* Wind indicator with countdown */
#windHud {
  grid-column: 2;
  justify-self: center;
  width: clamp(232px, 24vw, 300px);
  display: grid;
  grid-template-rows: auto auto;
  gap: 4px;
  background: rgba(6, 25, 42, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  padding: 5px 8px;
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
/* Green = wind shifting right, red = wind shifting left (sailing convention) */
#windHud.right {
  border-color: #4be08a;
  box-shadow: 0 0 0 2px rgba(75, 224, 138, 0.18), 0 0 20px rgba(75, 224, 138, 0.28);
}
#windHud.left {
  border-color: #ff6b6b;
  box-shadow: 0 0 0 2px rgba(255, 107, 107, 0.18), 0 0 20px rgba(255, 107, 107, 0.28);
}
.wind-shift {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 50px;
  padding: 2px 5px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: #cfe8fa;
  font-weight: 900;
  line-height: 1.05;
}
.wind-shift #windShiftChev { font-size: 12px; letter-spacing: -2px; }
.wind-shift #windShiftText { font-size: 8px; letter-spacing: 0.5px; }
.wind-shift.right {
  color: #4be08a;
  border-color: #4be08a;
  background: rgba(75, 224, 138, 0.15);
  box-shadow: 0 0 14px rgba(75, 224, 138, 0.3);
}
.wind-shift.left {
  color: #ff6b6b;
  border-color: #ff6b6b;
  background: rgba(255, 107, 107, 0.15);
  box-shadow: 0 0 14px rgba(255, 107, 107, 0.3);
}
#windHud.shifting .wind-shift { animation: shiftpulse 0.7s ease-in-out infinite; }
@keyframes shiftpulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}
#windHud.pulse {
  animation: windpulse 0.5s ease;
}
@keyframes windpulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.04); }
  100% { transform: scale(1); }
}
.wind-head {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
}
.wind-main {
  display: grid;
  grid-template-columns: 34px minmax(48px, 1fr) auto;
  align-items: center;
  gap: 7px;
}
.wind-reading {
  display: grid;
  gap: 2px;
  min-width: 0;
}
.wind-arrow-wrap {
  position: relative;
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
}
/* fixed "N" reference so it is obvious what the arrow points at / from */
.wind-arrow-n {
  position: absolute;
  top: -3px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 7px;
  font-weight: 800;
  letter-spacing: 0;
  color: #9be4ff;
}
.wind-arrow {
  width: 27px;
  height: 27px;
  color: #fff;
  transition: transform 0.15s linear;
  transform-origin: 50% 50%;
  filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.6));
}
.wind-arrow svg { display: block; width: 100%; height: 100%; fill: currentColor; }
#windLabel {
  justify-self: center;
  min-width: 0;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 1px;
  color: #bfe3f7;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#windSeconds {
  font-size: 14px;
  font-weight: 900;
  color: #fff;
  font-variant-numeric: tabular-nums;
}
.wind-bar {
  height: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.15);
  overflow: hidden;
}
#windBarFill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, #2ea8ff, #ffd54a);
  transition: width 0.1s linear;
}
#windDeg {
  min-width: 0;
  font-size: 9px;
  font-weight: 700;
  color: #9be4ff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Banner / countdown / toast */
#banner {
  position: fixed;
  top: 24%;
  left: 50%;
  transform: translateX(-50%) scale(0.6);
  z-index: 30;
  font-size: 44px;
  font-weight: 900;
  color: #ffd54a;
  text-shadow: 0 4px 18px rgba(0, 0, 0, 0.7);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
#banner.show { opacity: 1; transform: translateX(-50%) scale(1); }

#countdown {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 25;
  font-size: 140px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 8px 40px rgba(0, 0, 0, 0.7);
  pointer-events: none;
}

#toast {
  position: fixed;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  z-index: 40;
  background: rgba(8, 25, 40, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.25);
  padding: 10px 18px;
  border-radius: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  font-size: 14px;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.code-display {
  margin: 14px auto;
  padding: 14px;
  font-size: 34px;
  font-weight: 900;
  letter-spacing: 6px;
  color: #9be4ff;
  background: rgba(255, 255, 255, 0.08);
  border: 1px dashed rgba(255, 255, 255, 0.4);
  border-radius: 12px;
}
.status {
  margin: 12px 0;
  color: #cfe8fa;
  font-size: 15px;
}

#roomCodeInput {
  width: 100%;
  padding: 13px 14px;
  font-size: 20px;
  letter-spacing: 4px;
  text-transform: uppercase;
  text-align: center;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: #0b3455;
  color: #fff;
}

#howPanel .how-text {
  text-align: left;
  font-size: 14.5px;
  line-height: 1.65;
  color: #d8eefc;
}
#howPanel .how-text b { color: #9be4ff; }

/* Compact pause bar (no full-screen popup; the field stays visible) */
#pausePanel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-radius: 14px;
  background: rgba(6, 25, 42, 0.82);
  border: 1px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  z-index: 25;
  pointer-events: auto;
}
#pausePanel .pause-label {
  font-weight: 900;
  letter-spacing: 1px;
  margin-right: 6px;
  color: #fff;
  font-size: 15px;
}
#pausePanel button {
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(180deg, #1b78c9, #0f5598);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  cursor: pointer;
}
#pausePanel button.secondary { background: rgba(255, 255, 255, 0.12); }
#pausePanel button:hover { filter: brightness(1.12); }

/* On-screen touch controls (phones / tablets) */
/* Full-screen "tap anywhere to tack" surface. Sits above the canvas but below
   the HUD, so the pause/mute buttons keep working. */
#touchTap {
  position: fixed;
  inset: 0;
  z-index: 5;
  background: transparent;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  user-select: none;
}

#touchControls {
  position: fixed;
  inset: 0;
  z-index: 12;
  pointer-events: none; /* the layer itself is transparent to taps */
}
.tc-cluster {
  position: absolute;
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
  display: flex;
  align-items: center;
  gap: 12px;
  pointer-events: none;
}
.tc-cluster.tc-left { left: calc(18px + env(safe-area-inset-left, 0px)); }
.tc-cluster.tc-right { right: calc(18px + env(safe-area-inset-right, 0px)); }
.tc-btn {
  pointer-events: auto;
  width: 74px;
  height: 74px;
  padding: 0;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(6, 25, 42, 0.55);
  color: #eaf6ff;
  font-size: 26px;
  font-weight: 800;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
  transition: transform 0.06s ease, background 0.06s ease;
}
.tc-btn.pressed,
.tc-btn:active {
  transform: scale(0.93);
  background: rgba(46, 168, 255, 0.6);
  border-color: #9be4ff;
}
.tc-btn.tc-fire {
  border-radius: 999px; /* the shoot button is round (the only on-screen button) */
  background: rgba(255, 213, 74, 0.22);
  border-color: #ffd54a;
  color: #ffd54a;
}
.tc-btn.tc-fire.pressed,
.tc-btn.tc-fire:active { background: rgba(255, 213, 74, 0.6); }

/* Single player: tack on the left thumb, fire on the right thumb. */
#touchControls.solo .tc-left .tc-fire {
  position: fixed;
  right: calc(18px + env(safe-area-inset-right, 0px));
  bottom: calc(18px + env(safe-area-inset-bottom, 0px));
}

/* Nudge the hint/toast above the buttons while touch controls are on. */
body.touch-on #hint { bottom: calc(112px + env(safe-area-inset-bottom, 0px)); }
body.touch-on #toast { bottom: calc(120px + env(safe-area-inset-bottom, 0px)); }

/* Small touch screens don't need the control tips: the on-screen buttons speak
   for themselves and the tip only covers water. (Keyboard tips stay on small
   non-touch windows, where they are the only instructions.) */
@media (max-width: 700px), (max-height: 560px) {
  body.touch-on #hint { display: none; }
}

/* Slightly smaller buttons on very small screens. */
@media (max-width: 560px) {
  .tc-btn { width: 62px; height: 62px; font-size: 22px; border-radius: 17px; }
}
@media (max-width: 480px) {
  .tc-cluster { gap: 8px; }
  .tc-btn { width: 54px; height: 54px; font-size: 20px; border-radius: 15px; }
}

/* ---------------------------------------------------------------------------
   Short / small screens (landscape phones, small windows)
   --------------------------------------------------------------------------- */

/* Compact menu / dialog panels so they fit a short screen. */
@media (max-height: 620px) {
  .panel { padding: 18px 20px; border-radius: 14px; }
  .panel h1 { font-size: 30px; }
  .tagline { margin-bottom: 14px; font-size: 13px; }
  .btn { margin: 6px 0; padding: 10px 14px; font-size: 15px; }
  .field { margin: 8px 0; font-size: 14px; }
  #howPanel .how-text { font-size: 13px; line-height: 1.5; }
}

/* Short but wide (landscape phone / small window): the menu buttons become a
   two-column grid so the whole menu fits without scrolling. */
@media (max-height: 620px) and (min-width: 560px) {
  #menu .panel { max-width: 640px; }
  .menu-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }
  .menu-actions .btn { margin: 0; }
  .menu-actions .btn:last-child { grid-column: 1 / -1; }
}

/* Very short screens (landscape phones): scroll the help text itself so the
   "Got it" button always stays on screen. */
@media (max-height: 540px) {
  #howPanel .how-text { max-height: 46vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
}

/* Tablets, phones and short windows: keep the same three zones, but put the
   player/actions row above the wind module so nothing has to be floated into a
   random corner. */
@media (max-width: 820px), (max-height: 560px) {
  #hud { padding: 8px 10px; }
  .hud-bar {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas:
      "players actions"
      "wind wind";
    gap: 8px;
  }
  .hud-players { grid-area: players; gap: 6px; }
  .hud-buttons { grid-area: actions; }
  #windHud {
    grid-area: wind;
    justify-self: center;
    width: 100%;
    max-width: 340px;
    padding: 5px 8px;
    gap: 4px;
  }
  .wind-main { grid-template-columns: 32px minmax(44px, 1fr) auto; gap: 6px; }
  .wind-arrow-wrap { width: 32px; height: 32px; }
  .wind-arrow { width: 25px; height: 25px; }
  .wind-arrow-n { font-size: 7px; top: -3px; }
  .wind-shift { min-width: 48px; padding: 2px 4px; }
  .wind-shift #windShiftChev { font-size: 11px; }
  .wind-shift #windShiftText { font-size: 8px; }
  #windLabel { font-size: 9.5px; letter-spacing: 0.8px; }
  #windSeconds { font-size: 13px; }
  #windDeg { font-size: 8.5px; }
  .wind-bar { height: 4px; }
  .hud-buttons { min-height: 46px; }
  .hud-buttons button { height: 34px; min-width: 34px; padding: 0 8px; font-size: 14px; }
  .score { min-width: 100px; padding: 5px 9px; }
  .score-name { font-size: 13px; }
  .score-value { font-size: 17px; }
  .score-meta { font-size: 10px; }
  #hint { font-size: 12px; padding: 4px 12px; }
  #banner { font-size: 30px; top: 30%; }
  #countdown { font-size: 90px; }
  .tc-cluster { bottom: calc(12px + env(safe-area-inset-bottom, 0px)); gap: 10px; }
  .tc-btn { width: 60px; height: 60px; font-size: 22px; border-radius: 17px; }
}

/* Portrait phones: compact player cards and icon-only actions so the top row
   always fits, with the wind module spanning the full width underneath. */
@media (max-width: 619px) {
  #hud { padding: 7px 8px; }
  .hud-bar { gap: 6px; }
  .hud-players { gap: 5px; }
  .score {
    min-width: 0;
    column-gap: 7px;
    padding: 4px 7px;
    border-radius: 10px;
  }
  .score-name { font-size: 12px; }
  .score-value { font-size: 16px; }
  .score-meta { gap: 5px; font-size: 9px; }
  .score-meta .lives { letter-spacing: 0; }
  .hud-buttons { min-height: 40px; padding: 3px; gap: 2px; border-radius: 10px; }
  .hud-buttons .btn-label { display: none; }
  .hud-buttons button { height: 30px; min-width: 30px; padding: 0 6px; font-size: 13px; border-radius: 7px; }
  #windHud { max-width: 320px; border-radius: 11px; }
  body.touch-on #toast { top: auto; bottom: calc(150px + env(safe-area-inset-bottom, 0px)); }
}
