/* Styles généraux */
body {
  font-family: sans-serif;
  background-color: #f0f0f0;
  color: #333;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  margin: 0;
  min-height: 100vh;
  box-sizing: border-box;
}

h1,
h2 {
  color: #1a1a1a;
  margin-top: 10px;
  margin-bottom: 10px;
  text-align: center;
}

.emulator-container {
  width: 100%;
  max-width: 512px;
  margin-bottom: 15px;
  border: 1px solid #ccc;
  image-rendering: -moz-crisp-edges;
  image-rendering: -webkit-crisp-edges;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#nes-canvas {
  display: block;
  background-color: black;
  width: 100%;
  height: auto;
}

.controls {
  background-color: #fff;
  padding: 10px 15px;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 15px;
  width: 90%;
  max-width: 500px;
  text-align: center;
}

.controls label {
  margin-right: 10px;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  margin-top: 8px;
}

.button-row button {
  flex: 1;
  min-width: 80px;
  max-width: 120px;
  padding: 8px 12px;
}

.instructions {
  background-color: #fff;
  padding: 15px;
  border-radius: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
  width: 90%;
  max-width: 600px;
}

.instructions ul {
  list-style: none;
  padding: 0;
}

.instructions li {
  margin-bottom: 5px;
}

.instructions b {
  display: inline-block;
  min-width: 150px;
  text-align: right;
  margin-right: 10px;
}

/* --- Contrôles mobiles --- */

#mobile-controls {
  display: none; /* Caché par défaut */
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 500px;
  padding: 15px 10px;
  box-sizing: border-box;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.dpad {
  position: relative;
  width: 120px;
  height: 120px;
}

.dpad-btn {
  position: absolute;
  width: 50px;
  height: 50px;
  background-color: #555;
  border: 1px solid #333;
  color: white;
  font-size: 20px;
  line-height: 50px;
  text-align: center;
  cursor: pointer;
  box-shadow: inset 0 -2px 3px rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  transition: all 0.1s ease-in-out;
}

.dpad-btn:active {
  background-color: #777;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
}

.dpad-btn-up {
  top: 0;
  left: 35px;
}
.dpad-btn-left {
  top: 35px;
  left: 0;
}
.dpad-btn-right {
  top: 35px;
  right: 0;
}
.dpad-btn-down {
  bottom: 0;
  left: 35px;
}

.dpad-center {
  position: absolute;
  width: 50px;
  height: 50px;
  background: #444;
  top: 35px;
  left: 35px;
  border-radius: 8px;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

.system-buttons,
.game-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
}

.action-btn {
  border: none;
  border-radius: 50%;
  color: white;
  font-weight: bold;
  font-size: 20px;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  box-shadow: 0 4px #333;
  transition: all 0.1s ease-in-out;
}

.btn-circle {
  width: 80px;
  height: 80px;
  background-color: #c0392b;
}

.btn-circle:active {
  background-color: #e74c3c;
  transform: scale(0.95);
}

.btn-rect {
  width: 100px;
  height: 50px;
  border-radius: 25px;
  background-color: #7f8c8d;
  font-size: 18px;
  box-shadow: 0 4px #333;
  line-height: 50px;
  text-align: center;
}

.btn-rect:active {
  background-color: #95a5a6;
  transform: scale(0.95);
}

/* --- Debug Info --- */
.debug-info {
  background-color: #ffffff;
  border: 1px solid #ccc;
  border-radius: 8px;
  padding: 10px;
  margin: 20px auto;
  max-width: 300px;
  width: 90%;
  font-family: monospace;
  font-size: 14px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.debug-info h3 {
  margin-top: 0;
  font-size: 16px;
  margin-bottom: 10px;
}

.debug-info li {
  margin-bottom: 6px;
  line-height: 1.2;
}

/* Valeurs du panneau d informations : coupure propre des longues listes. */
.debug-info li span {
  display: inline-block;
  max-width: 190px;
  vertical-align: top;
  overflow-wrap: anywhere;
}

/* --- Bouton plein ecran --- */
.status-line {
  margin: 8px 0 0;
  font-family: monospace;
  font-size: 13px;
  color: #333;
  text-align: center;
  word-break: break-all;
  white-space: normal;
  max-width: 100%;
}

.fps-toggle {
  margin-left: 10px;
  font-size: 14px;
}

.fps-display {
  display: inline-block;
  min-width: 80px;
  margin-left: 6px;
  padding: 2px 8px;
  font-family: monospace;
  font-size: 14px;
  background: #222;
  color: #8f8;
  border-radius: 6px;
}

.fps-display.fps-low {
  background: #622;
  color: #fcc;
}

#reset-btn:disabled,
#stop-btn:disabled,
#save-state-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

#reset-btn,
#stop-btn {
  margin-left: 10px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  background-color: #222;
  color: #fff;
  border: 1px solid #555;
  border-radius: 6px;
}

#stop-btn {
  background-color: #8b0000;
  border-color: #a00;
}

#stop-btn:hover {
  background-color: #a00;
}

#fullscreen-btn:hover {
  background-color: #444;
}

.state-controls {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #ddd;
}

#save-state-btn {
  margin-left: 10px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  background-color: #27ae60;
  color: #fff;
  border: 1px solid #1e8c49;
  border-radius: 6px;
}

#save-state-btn:hover {
  background-color: #2ecc71;
}

.btn-file {
  display: inline-block;
  margin-left: 10px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
  background-color: #3498db;
  color: #fff;
  border: 1px solid #2980b9;
  border-radius: 6px;
}

.btn-file:hover {
  background-color: #2980b9;
}

#load-state-input {
  display: none;
}

/* --- Manette USB (Gamepad API) --- */

.player-config {
  margin: 0 auto 8px;
  border-collapse: collapse;
  font-size: 14px;
}

.player-config td {
  padding: 3px 6px;
  text-align: left;
}

.player-config-label {
  font-weight: bold;
  color: #333;
  min-width: 70px;
}

.player2-enable {
  font-size: 13px;
  margin-right: 8px;
  color: #555;
}

.gamepad-note {
  margin: 0 0 8px;
  font-size: 12px;
  color: #444;
  line-height: 1.5;
}

.gamepad-enable {
  margin-right: 6px;
  font-size: 14px;
}

.gamepad-status {
  display: block;
  margin: 8px 0 4px;
  font-family: monospace;
  font-size: 12px;
  color: #444;
  overflow-wrap: anywhere;
}

.gamepad-deadzone-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
}

.gamepad-deadzone-label input[type="range"] {
  vertical-align: middle;
  width: 120px;
}

#gamepad-deadzone-value {
  display: inline-block;
  min-width: 30px;
  font-family: monospace;
  font-size: 12px;
}

.gamepad-mapping {
  margin: 0 auto;
  border-collapse: collapse;
  font-size: 13px;
}

.gamepad-mapping td {
  padding: 2px 6px;
  text-align: left;
}

.gamepad-mapping td:first-child {
  color: #555;
  font-family: monospace;
  min-width: 70px;
}

.gamepad-mapping select {
  font-size: 12px;
  max-width: 170px;
}

/* En plein ecran, la page occupe tout l'ecran : on centre l'image. */
body:fullscreen .emulator-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}

body:fullscreen #nes-canvas {
  image-rendering: pixelated;
  max-width: 100vw;
  max-height: 100vh;
  width: auto;
  height: auto;
}

/* --- Message d'erreur de chargement d'une ROM --- */
.rom-error {
  background-color: #2b0f0f;
  color: #ffd7d7;
  border: 1px solid #a33;
  border-radius: 8px;
  padding: 12px;
  margin: 15px auto;
  max-width: 640px;
  width: 92%;
  font-family: monospace;
  font-size: 13px;
}

.rom-error strong {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
}

.rom-error pre {
  margin: 0 0 10px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.rom-error button {
  padding: 5px 12px;
  cursor: pointer;
  background-color: #611;
  color: #fff;
  border: 1px solid #a55;
  border-radius: 6px;
}

/* --- Media Queries pour Responsivité --- */

@media (max-width: 768px) {
  body {
    padding: 5px;
  }

  h1 {
    font-size: 1.3em;
  }

  .controls {
    padding: 8px 10px;
    width: 95%;
  }

  .desktop-only {
    display: none;
  }

  .mobile-only {
    display: flex !important;
  }

  .btn-circle {
    width: 90px;
    height: 90px;
    font-size: 24px;
    line-height: 90px;
  }

  .btn-rect {
    width: 110px;
    height: 60px;
    font-size: 20px;
    line-height: 60px;
    border-radius: 30px;
  }
}

/* Pour très petits écrans */
@media (max-width: 380px) {
  .btn-circle {
    width: 65px;
    height: 65px;
    font-size: 20px;
    line-height: 65px;
  }

  .btn-rect {
    width: 70px;
    height: 35px;
    font-size: 16px;
    line-height: 35px;
    border-radius: 10px;
  }

  .dpad {
    position: relative;
    width: 130px; /* Largeur totale du D-Pad */
    height: 130px; /* Hauteur totale du D-Pad */
  }

  .dpad-btn {
    position: absolute;
    width: 50px;
    height: 50px;
    font-size: 30px;
    line-height: 50px;
    text-align: center;
    background-color: #555;
    color: white;
    border-radius: 8px;
    transition: all 0.1s ease-in-out;
  }

  .dpad-center {
    position: absolute;
    top: 20px;
    left: 20px;
    width: 20px;
    height: 20px;
    background-color: #333;
    border-radius: 8px;
  }

  .dpad-btn-up {
    top: -10px;
    left: 35px;
  }
  .dpad-btn-left {
    left: -10px;
    top: 35px;
  }
  .dpad-btn-right {
    right: 0px;
    top: 35px;
  }
  .dpad-btn-down {
    bottom: 0px;
    left: 35px;
  }

  .dpad-center {
    position: absolute;
    top: 35px;
    left: 35px;
    width: 50px;
    height: 50px;
    background-color: #333;
    z-index: -1;
  }
}

/* Pour desktop */
@media (min-width: 769px) {
  .mobile-only {
    display: none !important;
  }

  .desktop-only {
    display: block;
  }

  #mobile-controls {
    display: none;
  }
}

/* --- Configuration clavier (remappage) --- */

.keyboard-mapping {
  margin: 8px auto;
  border-collapse: collapse;
  font-size: 13px;
  width: 100%;
  max-width: 480px;
}

.keyboard-mapping th,
.keyboard-mapping td {
  padding: 4px 8px;
  text-align: center;
  border: 1px solid #ddd;
}

.keyboard-mapping th {
  background-color: #f0f0f0;
  font-weight: bold;
}

.keyboard-mapping td:first-child {
  text-align: left;
  color: #555;
  font-family: monospace;
  white-space: nowrap;
}

.keyboard-mapping .key-cell {
  min-width: 90px;
  height: 32px;
  background-color: #fff;
  border: 1px solid #999;
  border-radius: 4px;
  cursor: pointer;
  user-select: none;
  transition:
    background-color 0.1s,
    border-color 0.1s;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: monospace;
  font-size: 12px;
}

.keyboard-mapping .key-cell:hover {
  background-color: #f5f5f5;
  border-color: #666;
}

.keyboard-mapping .key-cell.listening {
  background-color: #e8f0fe;
  border-color: #3a7bd5;
  box-shadow: 0 0 0 2px rgba(58, 123, 213, 0.3);
}

.keyboard-mapping .key-cell.conflict {
  background-color: #ffeaea;
  border-color: #d32f2f;
  color: #d32f2f;
}

.keyboard-config-note {
  margin: 0 0 8px;
  font-size: 12px;
  color: #555;
  line-height: 1.5;
}

#keyboard-reset {
  display: block;
  margin: 10px auto 0;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  background-color: #777;
  color: #fff;
  border: 1px solid #555;
  border-radius: 6px;
}

#keyboard-reset:hover {
  background-color: #555;
}
