/* Reset and base styles */
* {
  box-sizing: border-box;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell,
    sans-serif;
  background-color: #1a1a1a;
  color: #fff;
  margin: 0;
  padding: 0;
  min-height: 100vh;
  overflow-x: hidden;
  max-width: 100vw;
}

/* Main layout container */
.main-container {
  display: flex;
  flex-direction: row;
  min-height: 100vh;
  height: 100vh;
  gap: 20px;
  padding: 20px;
  overflow: hidden;
  align-items: stretch;
  max-width: 100vw;
  box-sizing: border-box;
}

/* Game area - takes most of the space */
.game-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px 0;
  overflow: auto;
  min-width: 0; /* Allow flex item to shrink below content size */
  max-width: 100%;
  position: relative;
  z-index: 1;
}

.header-section {
  text-align: center;
  margin-bottom: 30px;
  flex-shrink: 0;
}

.header-section h1 {
  margin: 0 0 10px 0;
  font-size: 2.5em;
  font-weight: 700;
}

.header-section p {
  margin: 5px 0;
  font-size: 1.1em;
  opacity: 0.9;
}

.loading-indicator {
  margin: 10px 0;
}

.loading-indicator p {
  margin: 0;
  font-size: 1em;
  opacity: 0.8;
}

#game-container {
  width: 100%;
  display: flex;
  justify-content: center;
  min-height: 500px;
  flex-grow: 1;
  max-width: 100vw;
  overflow: hidden;
  position: relative;
  z-index: 1;
}

.mobile-device .controls-sidebar {
  display: none;
}

.mobile-controls {
  display: none;
  position: fixed;
  bottom: 10px;
  left: 0;
  right: 0;
  z-index: 9999;
  background: linear-gradient(180deg, transparent, rgba(0, 0, 0, 0.8));
  padding: 8px;
  flex-direction: column;
  box-sizing: border-box;
  max-width: 100vw;
  overflow: visible;
  pointer-events: none;
  /* Ensure controls stay above everything */
  transform: translateZ(0);
  will-change: transform;
}

/* CSS Variables */
:root {
  --mobile-control-button-size: 45px;
  --mobile-control-button-size-small: 25px;
}

/* Use smaller buttons when device is rotated horizontally (landscape mode) */
/* This gives more vertical space for the game since landscape has less height */
@media (orientation: landscape) {
  :root {
    --mobile-control-button-size: var(--mobile-control-button-size-small);
  }

  .control-btn {
    font-size: 12px; /* Smaller font for smaller buttons */
    margin: 1px; /* Tighter spacing */
  }

  .action-btn {
    font-size: 14px; /* Smaller font for action buttons too */
  }

  .enter-btn {
    font-size: 8px; /* Even smaller for landscape */
  }

  .escape-btn {
    font-size: 7px; /* Even smaller for landscape */
  }

  .mobile-controls {
    height: 80px; /* Reduced height for smaller buttons but accommodate 4 buttons */
    padding: 4px; /* Less padding */
    bottom: 5px; /* Move up slightly in landscape */
  }

  .mobile-device .game-area {
    padding-bottom: 0px; /* Remove padding to prevent overflow */
    padding-top: 5px;
    position: relative;
    z-index: 1;
    max-height: calc(100vh - 110px);
    height: calc(100vh - 110px);
    overflow: hidden;
  }

  .mobile-device #game-container {
    min-height: calc(100vh - 115px); /* Adjust container height for landscape */
    max-height: calc(100vh - 115px);
    width: 100%;
    max-width: 100vw;
    overflow: hidden;
  }

  .mobile-device .main-container {
    padding: 5px;
  }

  .action-buttons-horizontal {
    gap: 4px; /* Tighter spacing between action buttons in landscape */
  }
}

/* Extra compact layout for very narrow screens (Samsung Galaxy, etc.) */
@media (max-width: 380px) {
  :root {
    --mobile-control-button-size: 42px;
  }

  .control-btn {
    font-size: 11px;
    margin: 0px;
    border-radius: 4px;
  }

  .action-btn {
    font-size: 9px;
  }

  .enter-btn {
    font-size: 6px;
  }

  .escape-btn {
    font-size: 5px;
  }

  .mobile-controls {
    padding: 2px;
    padding-bottom: calc(2px + env(safe-area-inset-bottom));
    height: 90px;
    bottom: 5px;
  }

  .mobile-device .game-area {
    padding-bottom: 0px; /* Remove padding to prevent overflow */
    position: relative;
    z-index: 1;
    max-height: calc(100vh - 115px);
    height: calc(100vh - 115px);
    overflow: hidden;
  }

  .mobile-device #game-container {
    min-height: calc(100vh - 120px); /* Account for controls + padding */
    max-height: calc(100vh - 120px);
    position: relative;
    z-index: 1;
    overflow: hidden;
  }

  .action-buttons-horizontal {
    gap: 2px;
  }

  .movement-pad {
    gap: 0px;
  }

  .arrow-pad {
    gap: 0px;
  }

  .movement-row {
    gap: 0px;
    margin-top: 0px;
  }

  .arrow-row {
    gap: 0px;
    margin-top: 0px;
  }
}

.mobile-device .mobile-controls {
  display: flex;
}

/* Mobile controls will show when mobile-device class is applied */

/* New horizontal layout for mobile controls */
.mobile-controls-top {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  padding-bottom: 5px;
}

.action-buttons-horizontal {
  display: flex;
  gap: 4px;
  justify-content: center;
  align-items: center;
}

.mobile-controls-bottom {
  display: flex;
  justify-content: space-between;
  align-items: end;
  flex: 1;
  padding: 0 10px;
}

/* Base button styles */
.control-btn {
  background: linear-gradient(145deg, #4a4a4a, #2a2a2a);
  border: 2px solid #666;
  border-radius: 6px;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
  transition: all 0.1s ease;
  width: var(--mobile-control-button-size);
  height: var(--mobile-control-button-size);
  margin: 1px;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  position: relative;
  z-index: 10000;
}

.control-btn:active {
  background: linear-gradient(145deg, #2a2a2a, #4a4a4a);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  transform: translateY(2px);
}

/* Movement pad (WASD) - now in bottom row */
.mobile-controls-bottom .mobile-controls-left {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.movement-pad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.movement-row {
  display: flex;
  gap: 1px;
  margin-top: 1px;
}

.movement-btn {
  background: linear-gradient(145deg, #4a7c59, #2d5233);
  border-color: #5a8a69;
}

.movement-btn:active {
  background: linear-gradient(145deg, #2d5233, #4a7c59);
}

/* Arrow pad - now on right side of bottom row */
.mobile-controls-bottom .mobile-controls-right {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.arrow-pad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.arrow-row {
  display: flex;
  gap: 1px;
  margin-top: 1px;
}

.arrow-btn {
  background: linear-gradient(145deg, #7c4a4a, #523333);
  border-color: #8a5a5a;
}

.arrow-btn:active {
  background: linear-gradient(145deg, #523333, #7c4a4a);
}

/* Action buttons - now horizontal in top row */
/* Old vertical action buttons styles removed - now using .action-buttons-horizontal */

.action-btn {
  background: linear-gradient(145deg, #4a4a7c, #333352);
  border-color: #5a5a8a;
  font-size: 12px;
}

.action-btn:active {
  background: linear-gradient(145deg, #333352, #4a4a7c);
}

/* Special styling for Enter and Escape buttons */
.enter-btn {
  background: linear-gradient(145deg, #4a7c4a, #335233);
  border-color: #5a8a5a;
  font-size: 8px;
  font-weight: bold;
  color: #fff;
}

.enter-btn:active {
  background: linear-gradient(145deg, #335233, #4a7c4a);
}

.escape-btn {
  background: linear-gradient(145deg, #7c4a4a, #523333);
  border-color: #8a5a5a;
  font-size: 7px;
  font-weight: bold;
  color: #fff;
}

.escape-btn:active {
  background: linear-gradient(145deg, #523333, #7c4a4a);
}

/* Mobile layout adjustments */
.mobile-device .game-area {
  padding-bottom: 0px; /* Remove padding to prevent overflow */
  padding-top: 10px;
  position: relative;
  z-index: 1;
  max-height: calc(100vh - 120px);
  height: calc(100vh - 120px);
  overflow: hidden;
}

.mobile-device #game-container {
  display: flex;
  justify-content: center;
  flex-direction: column;
  min-height: calc(100vh - 130px); /* Account for mobile controls + padding */
  max-height: calc(100vh - 130px);
  width: 100%;
  max-width: 100vw;
  position: relative;
  z-index: 1;
  overflow: hidden;
}

.mobile-device .main-container {
  padding: 10px;
  gap: 10px;
}

/* Controls sidebar */
.controls-sidebar {
  width: 280px;
  min-width: 280px;
  flex-shrink: 0;
}

.controls-card {
  background: linear-gradient(145deg, #2a2a2a, #1e1e1e);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
  border: 1px solid #333;
  position: sticky;
  top: 20px;
}

.controls-card h2 {
  margin: 0 0 20px 0;
  font-size: 1.4em;
  font-weight: 600;
  text-align: center;
  color: #fff;
}

.controls-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.control-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  transition: background-color 0.2s ease;
}

.control-item:hover {
  background: rgba(255, 255, 255, 0.1);
}

.key {
  font-weight: 600;
  color: #ffd700;
  font-size: 0.9em;
  min-width: 80px;
}

.action {
  font-size: 0.85em;
  color: #ccc;
  text-align: right;
  line-height: 1.3;
}

/* Intermediate screen sizes - keep sidebar but adjust spacing */
@media (max-width: 900px) and (min-width: 601px) {
  .main-container {
    gap: 15px;
    padding: 15px;
  }

  .controls-sidebar {
    width: 240px;
    min-width: 240px;
  }

  .controls-card {
    padding: 18px;
  }
}

/* Responsive design - only switch to vertical layout on actual mobile devices */
@media (max-width: 600px) {
  .main-container {
    flex-direction: column;
    padding: 5px;
    gap: 10px;
    height: auto;
    overflow: visible;
    max-width: 100vw;
  }

  .game-area {
    max-width: 100%;
    order: 1;
    height: auto;
    min-height: 40vh;
    max-height: calc(100vh - 140px);
    padding: 5px 0;
    overflow: hidden;
    position: relative;
    z-index: 1;
  }

  .controls-sidebar {
    width: 100%;
    order: 0;
  }

  .controls-card {
    position: static;
    padding: 12px;
    margin: 0;
  }

  .controls-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 6px;
  }

  .header-section h1 {
    font-size: 1.8em;
  }

  .control-item {
    padding: 6px 8px;
  }

  .key {
    font-size: 0.75em;
    min-width: 60px;
  }

  .action {
    font-size: 0.75em;
  }

  #game-container {
    min-height: 300px;
    max-height: calc(100vh - 140px);
    width: 100%;
    max-width: 100vw;
    overflow: hidden;
  }

  .start-prompt {
    height: min(400px, 60vh);
    width: min(400px, 95vw);
    font-size: clamp(16px, 5vw, 20px);
  }
}

/* Start prompt styling */
.start-prompt {
  display: flex;
  justify-content: center;
  align-items: center;
  height: min(640px, 70vh);
  width: min(640px, 90vw);
  max-width: 640px;
  margin: 0 auto;
  background: #1a1a1a;
  border: 2px solid #333;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  font-size: clamp(18px, 4vw, 24px);
  font-weight: bold;
  color: #ff6666;
  text-shadow: 0 0 10px rgba(255, 102, 102, 0.5);
  cursor: pointer;
  transition: all 0.3s ease;
  user-select: none;
  aspect-ratio: 1;
}

.start-prompt:hover {
  background: #2a2a2a;
  color: #ff8888;
  text-shadow: 0 0 15px rgba(255, 136, 136, 0.7);
  border-color: #666;
}
