/* Reset and base styles */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -webkit-touch-callout: none;
}

body {
  overflow: hidden;
  background: #000;
  margin: 0;
  height: 100vh;
  width: 100vw;
  color: #fff;
  font-family: monospace;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Game container */
#gameContainer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

/* Canvas */
#gameCanvas {
  font-family: monospace;
  image-rendering: pixelated;
  background: #000;
  z-index: 1000;
  position: absolute;
  touch-action: none;
  -webkit-tap-highlight-color: transparent; /* Remove tap highlight */
}

#globeCanvas {
  z-index: 1001;
  position: absolute;
  touch-action: none;
  display: none;
}

/* Joystick */
#joystick {
  width: min(80px, 15vw);
  height: min(80px, 15vw);
  background: rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  position: absolute;
  display: none;
  z-index: 1000;
  touch-action: none;
}

/* Inventory panel */
#inventoryPanel {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.9);
  color: #fff;
  padding: 15px;
  z-index: 1003;
  font-family: monospace;
  overflow-y: auto;
  display: none;
  flex-direction: column;
}

#inventoryTabs {
  display: flex;
  gap: 5px;
  margin-bottom: 10px;
}

#inventoryTabs button {
  flex: 1;
  padding: 8px;
  background: #444;
  border: 1px solid #666;
  color: #fff;
  cursor: pointer;
  font-size: clamp(12px, 2.5vw, 16px);
  touch-action: manipulation;
}

#inventoryTabs button.active {
  background: #666;
}

.tab-content {
  display: none;
}

.tab-content.active {
  display: block;
}

#inventoryContent h3 {
  margin: 10px 0;
  font-size: clamp(14px, 3vw, 18px);
}

#inventoryContent ul {
  list-style: disc;
  margin-left: 20px;
  font-size: clamp(12px, 2.5vw, 16px);
}

/* Start screen */
#startScreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to bottom, #111, #333);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1002;
  color: #fff;
  font-family: monospace;
  text-align: center;
  padding: 20px;
  animation: fadeIn 1s ease-in;
  pointer-events: auto;
}

#startScreen h1 {
  font-size: clamp(24px, 5vw, 36px);
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
}

#startScreen p {
  font-size: clamp(14px, 3vw, 18px);
  margin: 10px 0;
  max-width: 90%;
  line-height: 1.5;
}

#startScreen button {
  margin-top: 20px;
  padding: 10px 40px;
  min-width: 160px;
  font-size: clamp(14px, 3vw, 18px);
  background: #555;
  color: #fff;
  border: 2px solid #777;
  border-radius: 8px;
  cursor: pointer;
  text-align: center;
  white-space: nowrap;
  transition: background 0.2s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 48px;
  pointer-events: auto;
  touch-action: manipulation;
}

#startScreen button:hover {
  background: #666;
  transform: scale(1.05);
}

#startScreen button:active {
  background: #777;
  transform: scale(0.95);
}

/* High scores */
#highScores {
  margin: 20px 0;
  max-width: 90%;
  overflow-x: auto;
}

#highScoreTable {
  border-collapse: collapse;
  width: 100%;
  color: #fff;
  font-family: monospace;
  font-size: clamp(12px, 2.5vw, 16px);
  background: rgba(0, 0, 0, 0.6);
  border: 2px solid #666;
}

#highScoreTable th,
#highScoreTable td {
  padding: 8px;
  text-align: left;
  border: 1px solid #666;
}

#highScoreTable th {
  background: #444;
}

#highScoreTable tbody tr:nth-child(odd) {
  background: rgba(255, 255, 255, 0.1);
}

#highScores h2 {
  font-size: clamp(16px, 4vw, 24px);
  margin-bottom: 10px;
  text-align: center;
}

/* Game over screen */
#gameOverScreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(to bottom, #111, #333);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 1002;
  color: #fff;
  font-family: monospace;
  text-align: center;
  padding: 20px;
  animation: fadeIn 1s ease-in;
  pointer-events: auto;
}

#gameOverScreen h1 {
  font-size: clamp(24px, 5vw, 36px);
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
  color: #f55;
}

#gameOverScreen p {
  font-size: clamp(14px, 3vw, 18px);
  margin: 10px 0;
}

#scoreForm {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-top: 20px;
}

#scoreForm label {
  font-size: clamp(14px, 3vw, 18px);
}

#playerName {
  padding: 8px;
  font-size: clamp(12px, 2.5vw, 16px);
  font-family: monospace;
  background: #444;
  color: #fff;
  border: 2px solid #666;
  border-radius: 5px;
  width: min(300px, 80vw);
  user-select: text;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
  pointer-events: auto;
  outline: none;
}

#playerName:focus {
  border-color: #fff;
  background: #666;
}

#submitScoreBtn,
#restartBtn {
  padding: 10px 40px;
  min-width: 160px;
  font-size: clamp(14px, 3vw, 18px);
  background: #555;
  color: #fff;
  border: 2px solid #777;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
  height: 48px;
  pointer-events: auto;
  touch-action: manipulation;
}

#submitScoreBtn:hover,
#restartBtn:hover {
  background: #666;
  transform: scale(1.05);
}

#submitScoreBtn:active,
#restartBtn:active {
  background: #777;
  transform: scale(0.95);
}

/* World map screen */
#worldMapScreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: transparent;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  z-index: 1002;
  color: #fff;
  font-family: monospace;
  text-align: center;
  padding: 20px;
  animation: fadeIn 1s ease-in;
  pointer-events: none;
}

#worldMapScreen h1 {
  font-size: clamp(20px, 4.5vw, 32px);
  margin: 20px 0 10px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
  background: rgba(0, 0, 0, 0.6);
  padding: 8px 16px;
  border-radius: 5px;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.5s ease;
}

#worldMapScreen p {
  font-size: clamp(12px, 2.5vw, 16px);
  margin: 10px 0;
  max-width: 90%;
  line-height: 1.5;
  background: rgba(0, 0, 0, 0.6);
  padding: 8px 16px;
  border-radius: 5px;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.5s ease;
}

#worldMapScreen h1.hidden,
#worldMapScreen p.hidden {
  opacity: 0;
  pointer-events: none;
}

/* Menu bar */
#menuBar {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 1004;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

#menuBar[style*="display: none"] {
  opacity: 0;
  pointer-events: none;
}

#menuBar[style*="display: block"] {
  opacity: 1;
}

.menu-toggle {
  width: min(70px, 14vw);
  height: min(70px, 14vw);
  font-size: clamp(16px, 4vw, 24px);
  background: #555;
  color: #fff;
  border: 2px solid #777;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.2s;
  touch-action: manipulation;
  pointer-events: auto;
}

.menu-toggle:hover {
  background: #666;
  transform: scale(1.05);
}

.menu-toggle:active {
  background: #777;
  transform: scale(0.95);
}

.menu-buttons {
  position: absolute;
  bottom: 35px;
  right: 35px;
  width: min(240px, 60vw);
  height: min(240px, 60vw);
  background: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  padding: 20px;
  opacity: 0;
  visibility: hidden;
  transform: scale(0.8);
  transform-origin: center center;
  transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s;
  pointer-events: none;
}

.menu-buttons.open {
  opacity: 1;
  visibility: visible;
  transform: scale(1);
}

.menu-btn {
  position: absolute;
  width: min(50px, 11vw);
  height: min(50px, 11vw);
  font-size: clamp(12px, 2.5vw, 16px);
  background: #444;
  color: #fff;
  border: 2px solid #666;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, transform 0.3s ease, opacity 0.3s ease;
  touch-action: manipulation;
  opacity: 0;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: auto;
}

.menu-btn.center {
  background: #666;
  font-size: clamp(16px, 3vw, 20px);
  z-index: 1005;
}

.menu-buttons.open .menu-btn {
  opacity: 1;
}

/* Circular positioning for buttons */
.menu-buttons.open .menu-btn:nth-child(1) { transform: translate(-50%, calc(-50% - 80px)); transition-delay: 0.05s; }
.menu-buttons.open .menu-btn:nth-child(2) { transform: translate(calc(-50% + 56px), calc(-50% - 56px)); transition-delay: 0.1s; }
.menu-buttons.open .menu-btn:nth-child(3) { transform: translate(calc(-50% + 80px), -50%); transition-delay: 0.15s; }
.menu-buttons.open .menu-btn:nth-child(4) { transform: translate(calc(-50% + 56px), calc(-50% + 56px)); transition-delay: 0.2s; }
.menu-buttons.open .menu-btn:nth-child(5) { transform: translate(-50%, calc(-50% + 80px)); transition-delay: 0.25s; }
.menu-buttons.open .menu-btn:nth-child(6) { transform: translate(calc(-50% - 56px), calc(-50% + 56px)); transition-delay: 0.3s; }
.menu-buttons.open .menu-btn:nth-child(7) { transform: translate(calc(-50% - 80px), -50%); transition-delay: 0.35s; }
.menu-buttons.open .menu-btn:nth-child(8) { transform: translate(calc(-50% - 56px), calc(-50% - 56px)); transition-delay: 0.4s; }
.menu-buttons.open .menu-btn:nth-child(9) { transform: translate(-50%, -50%); transition-delay: 0.45s; }

.menu-btn:hover {
  background: #666;
  transform: scale(1.1) translate(-50%, -50%);
}

.menu-btn:active {
  background: #777;
  transform: scale(0.95) translate(-50%, -50%);
}

/* Loading screen */
#loadingScreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: #111;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1002;
}

.loading-text {
  color: #fff;
  font-family: monospace;
  font-size: clamp(16px, 4vw, 24px);
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.5; }
  100% { opacity: 1; }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Plot popup */
.plot-popup {
  position: absolute;
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 6px 10px;
  border-radius: 5px;
  border: 1px solid #666;
  font-family: monospace;
  font-size: clamp(10px, 2vw, 12px);
  display: none;
  z-index: 1003;
  pointer-events: none;
  max-width: 150px;
  text-align: center;
}

.plot-popup span {
  display: block;
}

#plotName {
  font-weight: bold;
  margin-bottom: 3px;
}

#plotCoords {
  font-size: clamp(8px, 1.8vw, 10px);
}

/* Responsive adjustments */
@media (max-width: 600px) {
  .menu-toggle {
    width: min(60px, 14vw);
    height: min(60px, 14vw);
    font-size: clamp(14px, 3.5vw, 18px);
  }
  .menu-buttons {
    bottom: 30px;
    right: 30px;
    width: min(210px, 52.5vw);
    height: min(210px, 52.5vw);
    padding: 15px;
  }
  .menu-btn {
    width: min(45px, 10vw);
    height: min(45px, 10vw);
    font-size: clamp(10px, 2vw, 14px);
  }
  .menu-btn.center {
    font-size: clamp(14px, 2.5vw, 18px);
  }
  .menu-buttons.open .menu-btn:nth-child(1) { transform: translate(-50%, calc(-50% - 70px)); }
  .menu-buttons.open .menu-btn:nth-child(2) { transform: translate(calc(-50% + 49px), calc(-50% - 49px)); }
  .menu-buttons.open .menu-btn:nth-child(3) { transform: translate(calc(-50% + 70px), -50%); }
  .menu-buttons.open .menu-btn:nth-child(4) { transform: translate(calc(-50% + 49px), calc(-50% + 49px)); }
  .menu-buttons.open .menu-btn:nth-child(5) { transform: translate(-50%, calc(-50% + 70px)); }
  .menu-buttons.open .menu-btn:nth-child(6) { transform: translate(calc(-50% - 49px), calc(-50% + 49px)); }
  .menu-buttons.open .menu-btn:nth-child(7) { transform: translate(calc(-50% - 70px), -50%); }
  .menu-buttons.open .menu-btn:nth-child(8) { transform: translate(calc(-50% - 49px), calc(-50% - 49px)); }
  .menu-buttons.open .menu-btn:nth-child(9) { transform: translate(-50%, -50%); }
  #inventoryPanel {
    padding: 10px;
  }
  #inventoryTabs button {
    font-size: clamp(10px, 2vw, 14px);
  }
  #startScreen h1 {
    font-size: clamp(20px, 4.5vw, 28px);
  }
  #startScreen p {
    font-size: clamp(12px, 2.5vw, 16px);
  }
  #startScreen button {
    font-size: clamp(12px, 2.5vw, 16px);
    padding: 8px 32px;
    min-width: 140px;
    height: 40px;
  }
  #highScores h2 {
    font-size: clamp(14px, 3.5vw, 20px);
  }
  #highScoreTable {
    font-size: clamp(10px, 2vw, 14px);
  }
  #highScoreTable th,
  #highScoreTable td {
    padding: 5px;
  }
  #gameOverScreen h1 {
    font-size: clamp(20px, 4.5vw, 28px);
  }
  #gameOverScreen p {
    font-size: clamp(12px, 2.5vw, 16px);
  }
  #scoreForm label {
    font-size: clamp(12px, 2.5vw, 16px);
  }
  #playerName {
    font-size: clamp(10px, 2vw, 14px);
    padding: 6px;
    width: min(250px, 80vw);
  }
  #submitScoreBtn,
  #restartBtn {
    font-size: clamp(12px, 2.5vw, 16px);
    padding: 8px 32px;
    min-width: 140px;
    height: 40px;
  }
  #worldMapScreen h1 {
    font-size: clamp(18px, 4vw, 26px);
  }
  #worldMapScreen p {
    font-size: clamp(10px, 2vw, 14px);
  }
  .plot-popup {
    padding: 5px 8px;
    font-size: clamp(8px, 1.8vw, 10px);
    max-width: 120px;
    border: 1px solid #666;
  }
}

/* Handle landscape orientation */
@media (orientation: landscape) {
  #menuBar {
    bottom: 20px;
    right: 20px;
  }
  .menu-toggle {
    width: min(60px, 12vh);
    height: min(60px, 12vh);
    font-size: clamp(14px, 3vh, 18px);
  }
  .menu-buttons {
    bottom: 30px;
    right: 30px;
    width: min(210px, 52.5vh);
    height: min(210px, 52.5vh);
    padding: 15px;
  }
  .menu-btn {
    width: min(45px, 10vh);
    height: min(45px, 10vh);
    font-size: clamp(10px, 2vh, 14px);
  }
  .menu-btn.center {
    font-size: clamp(14px, 2.5vh, 18px);
  }
  .menu-buttons.open .menu-btn:nth-child(1) { transform: translate(-50%, calc(-50% - 70px)); }
  .menu-buttons.open .menu-btn:nth-child(2) { transform: translate(calc(-50% + 49px), calc(-50% - 49px)); }
  .menu-buttons.open .menu-btn:nth-child(3) { transform: translate(calc(-50% + 70px), -50%); }
  .menu-buttons.open .menu-btn:nth-child(4) { transform: translate(calc(-50% + 49px), calc(-50% + 49px)); }
  .menu-buttons.open .menu-btn:nth-child(5) { transform: translate(-50%, calc(-50% + 70px)); }
  .menu-buttons.open .menu-btn:nth-child(6) { transform: translate(calc(-50% - 49px), calc(-50% + 49px)); }
  .menu-buttons.open .menu-btn:nth-child(7) { transform: translate(calc(-50% - 70px), -50%); }
  .menu-buttons.open .menu-btn:nth-child(8) { transform: translate(calc(-50% - 49px), calc(-50% - 49px)); }
  .menu-buttons.open .menu-btn:nth-child(9) { transform: translate(-50%, -50%); }
}

/* Ensure safe-area-insets are respected */
@supports (bottom: env(safe-area-inset-bottom)) {
  #menuBar {
    bottom: calc(20px + env(safe-area-inset-bottom));
    right: calc(20px + env(safe-area-inset-right));
  }
  .menu-buttons {
    bottom: calc(35px + env(safe-area-inset-bottom));
    right: calc(35px + env(safe-area-inset-right));
  }
}

/* Ensure playerName input is focusable and allows text selection */
#playerName {
  user-select: text;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
}