* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  color: #eee;
  overflow-x: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.game-wrap {
  position: relative;
  width: 100vw;
  width: 100dvw;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  background: linear-gradient(180deg, #87ceeb 0%, #b0e0e6 60%, #daa520 100%);
}

#game {
  display: block;
  background: #87ceeb;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  cursor: pointer;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.65);
  pointer-events: none;
  transition: opacity 0.3s ease;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay.visible {
  pointer-events: auto;
}

.panel.hidden {
  display: none !important;
}

.panel {
  text-align: center;
  padding: clamp(1rem, 4vw, 2rem) clamp(1.25rem, 5vw, 3rem);
  margin: auto;
  background: rgba(30, 30, 50, 0.96);
  border-radius: clamp(12px, 3vw, 16px);
  border: 2px solid rgba(255, 200, 100, 0.4);
  max-width: min(92vw, 420px);
  width: 100%;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  flex-shrink: 0;
}

.panel h1 {
  font-size: clamp(1.65rem, 5vw, 2.5rem);
  margin-bottom: 0.5rem;
  color: #ffd54f;
  text-shadow: 0 0 20px rgba(255, 213, 79, 0.3);
  line-height: 1.2;
  font-weight: 700;
}

.panel h2 {
  font-size: clamp(1.25rem, 4vw, 1.5rem);
  color: #ffd54f;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.panel p {
  font-size: clamp(0.9rem, 2.5vw, 1.1rem);
  line-height: 1.55;
  margin-bottom: 0.5rem;
  color: #ccc;
}

#sub {
  margin-top: 1rem;
  font-size: clamp(0.85rem, 2.2vw, 0.95rem);
  color: rgba(255, 213, 79, 0.95);
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: clamp(0.6rem, 2vw, 0.75rem);
  margin-top: 1.25rem;
  align-items: stretch;
  width: 100%;
}

.btn {
  padding: clamp(0.75rem, 3vw, 0.9rem) 1.25rem;
  min-height: 48px;
  font-size: clamp(0.95rem, 2.5vw, 1rem);
  font-weight: 600;
  border-radius: 12px;
  border: 2px solid rgba(255, 200, 100, 0.5);
  cursor: pointer;
  font-family: inherit;
  width: 100%;
  max-width: 280px;
  margin: 0 auto;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn-primary {
  background: #ffd54f;
  color: #1a1a2e;
}

.btn-primary:hover,
.btn-primary:active {
  background: #ffe082;
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.12);
  color: #ffd54f;
}

.btn-secondary:hover,
.btn-secondary:active {
  background: rgba(255, 255, 255, 0.22);
}

.panel-lobby .hidden,
.panel-join .hidden {
  display: none !important;
}

.lobby-code {
  font-size: clamp(1.35rem, 4vw, 1.5rem);
  letter-spacing: 0.25em;
  font-weight: 700;
  color: #ffd54f;
  margin: 0.75rem 0;
  word-break: break-all;
}

.btn-copy-code {
  display: inline-block;
  padding: 0.4rem 0.9rem;
  font-size: 0.9rem;
  min-height: auto;
  max-width: none;
  margin-bottom: 0.25rem;
}

.lobby-copied {
  display: inline-block;
  margin-left: 0.5rem;
  font-size: 0.9rem;
  color: #2e8b57;
  font-weight: 600;
}

.lobby-copied.hidden {
  display: none !important;
}

.lobby-players {
  font-size: clamp(0.9rem, 2.2vw, 1rem);
  color: #aaa;
  margin-bottom: 1rem;
  line-height: 1.4;
}

.lobby-label {
  font-size: 0.95rem;
  color: #aaa;
  margin: 0.5rem 0;
}

.player-name-input {
  width: 100%;
  max-width: 240px;
  padding: 0.5rem 0.75rem;
  font-size: 1rem;
  margin-bottom: 0.25rem;
  border-radius: 8px;
  border: 2px solid rgba(255, 200, 100, 0.5);
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  text-align: center;
}

.player-name-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.scoreboard-panel {
  margin-top: 1.25rem;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  text-align: left;
}

.scoreboard-panel.hidden {
  display: none !important;
}

.scoreboard-panel .scoreboard-title {
  font-size: 1rem;
  color: #ffd54f;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.scoreboard-panel .scoreboard-row {
  padding: 0.35rem 0;
  font-size: 1rem;
  color: #ccc;
}

.scoreboard-panel .scoreboard-row.winner {
  color: #ffd54f;
  font-weight: 700;
  font-size: 1.1rem;
}

#btn-restart-game {
  margin-top: 1rem;
}

.dog-picker {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
  margin: 0.75rem 0;
}

.dog-picker img {
  width: 48px;
  height: 36px;
  object-fit: contain;
  border: 3px solid transparent;
  border-radius: 8px;
  cursor: pointer;
  background: rgba(0, 0, 0, 0.2);
}

.dog-picker img:hover {
  border-color: rgba(255, 213, 79, 0.6);
}

.dog-picker img.selected {
  border-color: #ffd54f;
  box-shadow: 0 0 10px rgba(255, 213, 79, 0.4);
}

.input-code {
  padding: 0.85rem 1rem;
  min-height: 48px;
  font-size: 1.25rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  width: 100%;
  max-width: 100%;
  margin: 0.75rem 0;
  border-radius: 12px;
  border: 2px solid rgba(255, 200, 100, 0.5);
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.input-code::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.best-display {
  margin-top: 0.5rem;
  font-size: clamp(0.9rem, 2.2vw, 1rem);
  color: #ffd54f;
  font-weight: 600;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin-top: 1rem;
  font-size: clamp(0.9rem, 2.2vw, 1rem);
  color: #ccc;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.setting-row input[type="checkbox"] {
  width: 1.2rem;
  height: 1.2rem;
  accent-color: #ffd54f;
  cursor: pointer;
}

/* Portrait / narrow: single full-width panel, no side-by-side */
@media (max-width: 768px), (orientation: portrait) {
  .overlay {
    justify-content: flex-start;
    padding-top: max(1rem, env(safe-area-inset-top));
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
  }

  .panel {
    max-width: 96vw;
    width: 100%;
    margin: 0 auto;
  }
}

/* Mobile: single-column layout, larger touch targets */
@media (max-width: 480px) {
  .panel {
    padding: 1.25rem 1rem;
    max-width: 94vw;
  }

  .panel h1 {
    margin-bottom: 0.4rem;
  }

  .panel p {
    margin-bottom: 0.4rem;
  }

  .menu-buttons {
    margin-top: 1rem;
    gap: 0.65rem;
  }

  .btn {
    min-height: 52px;
    max-width: none;
    padding: 0.85rem 1rem;
  }

  .lobby-code {
    letter-spacing: 0.15em;
  }

  .input-code {
    font-size: 1.15rem;
    letter-spacing: 0.15em;
    padding: 0.9rem;
  }

  .panel-join .btn,
  .panel-lobby .btn {
    min-height: 52px;
  }
}

/* Joystick: keep in safe area on mobile */
.joystick-wrap {
  position: absolute;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: max(12px, env(safe-area-inset-left));
  width: clamp(88px, 22vw, 100px);
  height: clamp(88px, 22vw, 100px);
  pointer-events: none;
  z-index: 10;
}

.joystick-wrap.visible {
  pointer-events: auto;
}

.joystick-wrap.hidden {
  display: none;
}

.btn-home {
  position: absolute;
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  z-index: 15;
  padding: 0.4rem 0.75rem;
  font-size: 0.9rem;
  font-weight: 600;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.6);
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  cursor: pointer;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}

.btn-home:hover {
  background: rgba(0, 0, 0, 0.7);
}

.btn-home.hidden {
  display: none;
}

.joystick-base {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.35);
  border: 3px solid rgba(255, 255, 255, 0.5);
  position: relative;
  touch-action: none;
}

.joystick-stick {
  position: absolute;
  width: 44px;
  height: 44px;
  left: 50%;
  top: 50%;
  margin-left: -22px;
  margin-top: -22px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  border: 2px solid rgba(0, 0, 0, 0.2);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  transition: transform 0.05s ease-out;
  pointer-events: none;
}
