/* ========== Design Tokens ========== */
:root {
  --game-bg: #0a0a0c;
  --game-surface: #16161a;
  --game-surface-elevated: #1e1e24;
  --accent: #e9af51;
  --accent-bright: #f5c76b;
  --accent-glow: rgba(233, 175, 81, 0.25);
  --text-primary: #f4f4f5;
  --text-muted: #a1a1aa;
  --border: 2px solid var(--accent);
  --radius: 8px;
  --radius-lg: 12px;
  /* Responsive breakpoints (for reference in media queries) */
  --bp-sm-mobile: 480px;
  --bp-mobile: 640px;
  --bp-tablet-portrait: 768px;
  --bp-tablet-landscape: 1024px;
  --bp-desktop: 1025px;
}

/* ========== Layout & Structure ========== */
body {
  font-family: "Press Start 2P", cursive;
  background-color: var(--game-bg);
  color: var(--text-primary);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

#game-ui {
  position: relative;
  z-index: 100;
  background-color: var(--game-bg);
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: stretch;
  gap: 0.25rem;
  padding: 0.5rem;
  min-height: 3rem;
  box-sizing: border-box;
}

/* When start popup is open: hide entire top bar (home, score, wave, lives) */
#game-ui.game-ui--start-screen {
  display: none !important;
  visibility: hidden !important;
}

.game-container {
  position: relative;
  z-index: 1;
  height: calc(100vh - 4rem);
}

/* When start popup is open, game UI is hidden so use full height */
body.start-popup-open .game-container {
  height: 100vh;
}

.start-popup {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, var(--game-bg) 0%, #0f0f12 50%, var(--game-bg) 100%);
  padding: 1.5rem;
  padding-top: max(1.5rem, env(safe-area-inset-top));
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  padding-left: max(1rem, env(safe-area-inset-left));
  padding-right: max(1rem, env(safe-area-inset-right));
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start !important;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

/* When visible: fixed so popup fills viewport and is never clipped (all screen sizes / landscape) */
.start-popup:not(.hidden) {
  position: fixed !important;
  inset: 0 !important;
  z-index: 9999;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  min-height: -webkit-fill-available;
}

/* ========== Start Screen ========== */
.start-screen-panel {
  width: 100%;
  max-width: 440px;
  margin: 0 auto;
  padding: 2rem 1.75rem;
  background: var(--game-surface);
  border: var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 0 0 1px rgba(233, 175, 81, 0.06), 0 12px 40px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0;
  box-sizing: border-box;
  flex-shrink: 0;
  min-height: min-content;
}

.start-screen-header {
  text-align: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid rgba(233, 175, 81, 0.2);
  min-width: 0;
  overflow: hidden;
}

@keyframes titleGlow {
  0%, 100% { text-shadow: 0 0 20px var(--accent-glow); }
  50% { text-shadow: 0 0 28px var(--accent-glow), 0 0 40px rgba(233, 175, 81, 0.15); }
}

.start-screen-title {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: 100%;
  line-height: 0;
}

.start-screen-logo {
  width: min(380px, 90vw);
  max-height: 120px;
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 0 20px var(--accent-glow));
}

.start-screen-subtitle {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin: 0.5rem 0 0;
  letter-spacing: 0.12em;
}

.start-screen-card {
  background: var(--game-surface-elevated);
  border: 1px solid rgba(233, 175, 81, 0.35);
  border-radius: var(--radius);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.5rem;
  box-sizing: border-box;
  overflow-x: auto;
  min-width: 0;
}

.start-screen-card-title {
  font-size: 0.7rem;
  color: var(--accent);
  margin: 0 0 1rem;
  letter-spacing: 0.08em;
  text-align: center;
}

.start-screen-instructions {
  list-style: none;
  margin: 0;
  padding: 0;
}

.start-screen-instructions li {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  font-size: 0.7rem;
  line-height: 1.9;
  color: var(--text-primary);
  margin-bottom: 0.6rem;
}

.start-screen-instructions li:last-child {
  margin-bottom: 0;
}

.start-screen-instructions .instruction-text {
  flex: 1;
  min-width: 0;
  word-wrap: break-word;
}

.start-screen-instructions li strong {
  color: var(--accent-bright);
  text-shadow: 0 0 8px var(--accent-glow);
}

/* Bullet style: small accent bar */
.instruction-icon {
  flex-shrink: 0;
  width: 4px;
  min-height: 12px;
  border-radius: 2px;
  margin-top: 0.35em;
}

.instruction-icon-move { background: var(--accent); }
.instruction-icon-dot { background: #fff; }
.instruction-icon-pellet { background: #fff; box-shadow: 0 0 6px rgba(255,255,255,0.5); }
.instruction-icon-power { background: #22d3ee; }
.instruction-icon-wave { background: #a78bfa; }
.instruction-icon-combo { background: var(--accent); box-shadow: 0 0 6px var(--accent-glow); }

.start-screen-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.start-screen-button {
  font-family: "Press Start 2P", cursive;
  font-size: 0.7rem;
  padding: 0.9rem 2rem;
  width: 100%;
  max-width: 280px;
  background: var(--accent);
  color: #0a0a0c;
  border: var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.2s, transform 0.15s, box-shadow 0.2s;
  box-shadow: 0 4px 0 #b8860b, 0 6px 12px rgba(0, 0, 0, 0.3);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.2;
}

.start-screen-button:hover {
  background: var(--accent-bright);
  transform: translateY(-1px);
  box-shadow: 0 5px 0 #b8860b, 0 8px 16px rgba(0, 0, 0, 0.35);
}

.start-screen-button:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #b8860b, 0 2px 8px rgba(0, 0, 0, 0.3);
}

.start-screen-button--large {
  padding: 1.1rem 2.25rem;
  font-size: 0.85rem;
  max-width: none;
}

.start-screen-tagline {
  font-size: 0.55rem;
  color: var(--text-muted);
  margin: 0;
  line-height: 1.7;
  text-align: center;
  max-width: 100%;
}

/* ========== Message Box (Game Over / Victory) ========== */
.message-box {
  background: rgba(10, 10, 12, 0.92);
  backdrop-filter: blur(8px);
}

.message-box-inner {
  text-align: center;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
}

.message-box-title {
  font-size: 1.5rem;
  margin: 0;
  line-height: 1.5;
  max-width: 90vw;
  color: var(--text-primary);
}

.message-victory {
  color: #00ff00;
  text-shadow: 0 0 20px #00ff00;
}

.message-gameover {
  color: #ff0000;
  text-shadow: 0 0 20px #ff0000;
}

/* Responsive: allow full viewport usage on all devices */
body {
  min-height: 100vh;
  min-height: 100dvh;
  min-width: 100vw;
}

canvas {
  background-color: var(--game-surface);
  display: block;
  margin: 0 auto;
  border: var(--border);
  border-radius: var(--radius);
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* Add bottom margin to canvas on mobile screens */
@media (max-width: 768px) {
  canvas {
    margin-bottom: 80px;
  }
}

/* Extra margin for very small screens */
@media (max-width: 480px) {
  canvas {
    margin-bottom: 100px;
  }
}

.ui-panel {
  background-color: var(--game-surface-elevated);
  border: var(--border);
  border-radius: var(--radius);
  box-shadow: 0 0 0 1px rgba(233, 175, 81, 0.06);
  min-width: 0;
  flex: 1;
  margin: 0;
  padding: 0.5rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-size: 0.65rem;
  box-sizing: border-box;
}

.ui-panel * {
  line-height: inherit;
  margin: 0;
}

.ui-panel--score {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 0;
}

.score-text {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
}

.score-panel-spacer {
  width: 1.25rem;
  flex-shrink: 0;
  visibility: hidden;
  pointer-events: none;
}

.ui-panel--lives {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.home-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.25rem;
  height: 1.25rem;
  margin: 0;
  padding: 0;
  background: none;
  color: var(--accent);
  border: none;
  border-radius: 0;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, transform 0.1s;
  box-sizing: content-box;
}

.home-button-icon {
  width: 1.25rem;
  height: 1.25rem;
  display: block;
}

.home-button:hover {
  background: none;
  color: var(--accent-bright);
  transform: scale(1.1);
}

.home-button:hover .home-button-icon {
  color: var(--accent-bright);
}

.home-button:active {
  transform: scale(0.95);
}

#lives {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  margin: 0;
}

#lives span {
  margin: 0;
}

.add-life-button {
  display: none; /* Shown only when lives === 1 (see updateUI in script.js) */
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  margin: 0;
  padding: 0 0.25rem;
  font-family: inherit;
  font-size: 1rem;
  font-weight: bold;
  line-height: 1;
  color: var(--accent);
  background: none;
  border: none;
  border-radius: 0;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.15s, transform 0.1s;
  box-sizing: content-box;
}


.add-life-button:hover {
  color: var(--accent-bright);
  transform: scale(1.1);
}

.add-life-button:active {
  transform: scale(0.95);
}

/* Mobile Controls Styles - Analog Joystick */
#mobile-controls {
  /* Ensure controller is fully visible with proper margins */
  margin-bottom: 20px;
  margin-left: 10px;
}

/* Hide mobile controls on large screens and web browsers */
@media (min-width: 769px) {
  #mobile-controls {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
  }
}

.joystick-base,
#joystick-base {
  background: linear-gradient(145deg, #374151, #1f2937);
  border: 3px solid #4b5563;
  box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.1),
    0 8px 16px rgba(0, 0, 0, 0.4);
}

.joystick-area,
#joystick-area {
  background: linear-gradient(145deg, #4b5563, #374151);
  border: 2px solid #6b7280;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

.joystick-handle,
#joystick-handle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(calc(-50% + var(--joy-x, 0%)), calc(-50% + var(--joy-y, 0%)));
  background: linear-gradient(145deg, #fbbf24, #f59e0b);
  border: 2px solid #e9af51;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3), 0 0 10px rgba(233, 175, 81, 0.3);
  z-index: 10;
  user-select: none;
  touch-action: none;
  will-change: transform;
  transition: transform 0.05s ease-out;
}

#joystick-handle:active {
  background: linear-gradient(145deg, #e9af51, #d97706);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4), 0 0 15px rgba(233, 175, 81, 0.6);
  transform: scale(0.95);
  transition: transform 0.1s ease-out;
}

/* Joystick movement area indicator */
#joystick-area::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: 2px;
  background: rgba(233, 175, 81, 0.3);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

/* Pulse animation for controller */
@keyframes controllerPulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.02);
  }
  100% {
    transform: scale(1);
  }
}

#mobile-controls .relative {
  animation: controllerPulse 3s ease-in-out infinite;
  filter: drop-shadow(0 8px 16px rgba(0, 0, 0, 0.4));
}

/* Responsive adjustments for smaller screens */
@media (max-width: 480px) {
  #mobile-controls {
    bottom: 20px !important;
    left: 20px !important;
  }

  #mobile-controls .w-32 {
    width: 6rem;
    height: 6rem;
  }

  #mobile-controls .w-24 {
    width: 4.5rem;
    height: 4.5rem;
  }

  #mobile-controls .w-12 {
    width: 2.5rem;
    height: 2.5rem;
  }

  /* UI panel adjustments for very small screens */
  .ui-panel {
    font-size: 0.75rem;
    padding: 0.5rem !important;
    height: 2.5rem !important;
    margin: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }
}

/* ========== RESPONSIVE: All breakpoints ========== */
/* Base: scrollable popup and message box */
#start-popup,
#message-box {
  -webkit-overflow-scrolling: touch;
}

/* --- Small Mobile (≤480px) --- */
@media (max-width: 480px) {
  .game-container {
    height: calc(100vh - 3.5rem);
    height: calc(100dvh - 3.5rem);
  }
  body.start-popup-open .game-container {
    height: 100vh;
    height: 100dvh;
  }
  .start-popup {
    padding: max(0.75rem, 4vw);
    padding-top: max(1rem, 6vh, env(safe-area-inset-top));
    padding-bottom: max(1.5rem, 8vh, env(safe-area-inset-bottom));
  }
  .start-screen-panel {
    max-width: 100%;
    margin: 0.5rem auto 1.5rem;
    padding: 1rem 1rem;
  }
  .start-screen-header {
    margin-bottom: 0.75rem;
    padding-bottom: 0.75rem;
  }
  .start-screen-logo {
    width: min(300px, 88vw);
    max-height: 96px;
  }
  .start-screen-subtitle {
    font-size: 0.5rem;
  }
  .start-screen-card {
    padding: 0.75rem 1rem;
    margin-bottom: 1rem;
  }
  .start-screen-card-title {
    font-size: 0.5rem;
    margin-bottom: 0.5rem;
  }
  .start-screen-instructions li {
    font-size: 0.5rem;
    line-height: 1.8;
  }
  .start-screen-button {
    font-size: 0.55rem;
    padding: 0.75rem 1.5rem;
    max-width: 100%;
  }
  .start-screen-tagline {
    font-size: 0.45rem;
  }
  .message-box-inner {
    padding: 1.5rem;
  }
  .message-box-title {
    font-size: 1rem;
  }
  .ui-panel {
    font-size: 0.55rem;
    padding: 0.4rem;
    min-height: 2.5rem;
  }
  #mobile-controls {
    bottom: 1rem !important;
    left: 1rem !important;
  }
  #mobile-controls .w-32 { width: 5.5rem; height: 5.5rem; }
  #mobile-controls .w-24 { width: 4rem; height: 4rem; }
  #mobile-controls .w-12 { width: 2.25rem; height: 2.25rem; }
}

/* --- Mobile (481px–640px) --- */
@media (min-width: 481px) and (max-width: 640px) {
  .start-popup {
    padding: max(1rem, 3vw);
    padding-top: max(1.25rem, 5vh, env(safe-area-inset-top));
    padding-bottom: max(1.5rem, 6vh, env(safe-area-inset-bottom));
  }
  .start-screen-panel {
    max-width: 100%;
    margin: 0.75rem auto 1.5rem;
    padding: 1.25rem 1.25rem;
  }
  .start-screen-header {
    margin-bottom: 1rem;
    padding-bottom: 1rem;
  }
  .start-screen-logo {
    width: min(330px, 84vw);
    max-height: 104px;
  }
  .start-screen-subtitle {
    font-size: 0.55rem;
  }
  .start-screen-card {
    padding: 1rem 1.25rem;
    margin-bottom: 1.25rem;
  }
  .start-screen-card-title {
    font-size: 0.55rem;
    margin-bottom: 0.6rem;
  }
  .start-screen-instructions li {
    font-size: 0.55rem;
    line-height: 1.85;
  }
  .start-screen-button {
    font-size: 0.6rem;
    padding: 0.8rem 1.75rem;
  }
  .start-screen-tagline {
    font-size: 0.5rem;
  }
  .ui-panel {
    font-size: 0.6rem;
    padding: 0.45rem;
    min-height: 2.75rem;
  }
}

/* --- Tablet Portrait (641px–768px) --- */
@media (min-width: 641px) and (max-width: 768px) {
  .start-popup {
    padding: 1.25rem 1.5rem;
    padding-top: max(1.5rem, 5vh, env(safe-area-inset-top));
    padding-bottom: max(1.5rem, 5vh, env(safe-area-inset-bottom));
  }
  .start-screen-panel {
    max-width: 420px;
    margin: 1rem auto 2rem;
    padding: 1.5rem 1.5rem;
  }
  .start-screen-logo {
    width: min(360px, 70vw);
    max-height: 112px;
  }
  .start-screen-subtitle {
    font-size: 0.6rem;
  }
  .start-screen-card-title {
    font-size: 0.6rem;
  }
  .start-screen-instructions li {
    font-size: 0.6rem;
  }
  .start-screen-button {
    font-size: 0.65rem;
  }
  .start-screen-tagline {
    font-size: 0.52rem;
  }
  .ui-panel {
    font-size: 0.65rem;
  }
}

/* --- Tablet Landscape (769px–1024px) --- */
@media (min-width: 769px) and (max-width: 1024px) {
  .start-popup {
    padding: 1.5rem 2rem;
    padding-top: max(1.5rem, 4vh, env(safe-area-inset-top));
    padding-bottom: max(1.5rem, 4vh, env(safe-area-inset-bottom));
  }
  .start-screen-panel {
    max-width: 440px;
    margin: 1rem auto 2rem;
    padding: 1.75rem 1.75rem;
  }
  .start-screen-logo {
    width: 380px;
    max-height: 120px;
  }
  .start-screen-subtitle {
    font-size: 0.65rem;
  }
  .ui-panel {
    font-size: 0.68rem;
  }
}

/* --- Desktop (≥1025px) --- */
@media (min-width: 1025px) {
  .start-popup {
    padding: 1.5rem 2rem;
    padding-top: max(1.5rem, 4vh, env(safe-area-inset-top));
    padding-bottom: max(1.5rem, 4vh, env(safe-area-inset-bottom));
  }
  .start-screen-panel {
    max-width: 440px;
    margin: 1.5rem auto 2rem;
    padding: 2rem 1.75rem;
  }
}

/* --- Landscape Mobile (orientation: landscape, short height) --- */
@media (orientation: landscape) and (max-height: 500px) {
  .game-container {
    height: calc(100vh - 3rem);
    height: calc(100dvh - 3rem);
  }
  body.start-popup-open .game-container {
    height: 100vh;
    height: 100dvh;
  }
  .start-popup {
    padding-left: max(1rem, 4vw);
    padding-right: max(1rem, 4vw);
    padding-top: max(1rem, 6vh, env(safe-area-inset-top));
    padding-bottom: max(2rem, 10vh, env(safe-area-inset-bottom));
  }
  .start-screen-panel {
    margin: 0.5rem auto 1.5rem;
    padding: 0.75rem 1.25rem;
    max-width: min(440px, 90vw);
  }
  .start-screen-header {
    margin-bottom: 0.5rem;
    padding-bottom: 0.5rem;
  }
  .start-screen-logo {
    width: min(280px, 56vw);
    max-height: 80px;
  }
  .start-screen-subtitle {
    font-size: 0.45rem;
  }
  .start-screen-card {
    padding: 0.5rem 1rem;
    margin-bottom: 0.5rem;
  }
  .start-screen-card-title {
    font-size: 0.45rem;
    margin-bottom: 0.4rem;
  }
  .start-screen-instructions li {
    font-size: 0.45rem;
    line-height: 1.6;
  }
  .start-screen-actions {
    gap: 0.5rem;
  }
  .start-screen-button {
    font-size: 0.5rem;
    padding: 0.6rem 1.25rem;
  }
  .start-screen-tagline {
    font-size: 0.4rem;
  }
  .ui-panel {
    min-height: 2.2rem;
    font-size: 0.6rem;
    padding: 0.35rem;
  }
  #mobile-controls.game-active {
    display: block !important;
    visibility: visible !important;
    opacity: 1 !important;
    bottom: 0.5rem !important;
    left: 0.5rem !important;
    z-index: 9999 !important;
  }
  #mobile-controls.game-active .w-32 { width: 4.5rem; height: 4.5rem; }
  #mobile-controls.game-active .w-24 { width: 3.25rem; height: 3.25rem; }
  #mobile-controls.game-active .w-12 { width: 1.75rem; height: 1.75rem; }
}

/* --- Landscape: any landscape with short viewport (ensure popup never clipped) --- */
@media (orientation: landscape) and (max-height: 700px) {
  .start-popup {
    padding-top: max(1.25rem, 5vh, env(safe-area-inset-top));
    padding-bottom: max(1.5rem, 8vh, env(safe-area-inset-bottom));
  }
}

/* --- Extra small (≤320px) --- */
@media (max-width: 320px) {
  .start-screen-logo {
    width: min(250px, 90vw);
    max-height: 76px;
  }
  .start-screen-subtitle {
    font-size: 0.4rem;
  }
  .start-screen-instructions li {
    font-size: 0.45rem;
  }
  .start-screen-button {
    font-size: 0.5rem;
    padding: 0.6rem 1rem;
  }
  .ui-panel {
    font-size: 0.5rem;
    padding: 0.35rem;
  }
}
