:root {
  --bg: #11161c;
  --surface: #1a222c;
  --surface-alt: #242f3d;
  --border: #314052;
  --text: #e6edf3;
  --muted: #9aa7b8;
  --accent: #7ec850;
  --accent-dark: #4f8f2d;
  --bezel: #323942;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.hidden {
  display: none !important;
}

.icon {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.icon-sm {
  width: 15px;
  height: 15px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.65rem 1.25rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: 0.04em;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.toolbar-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}

.btn,
.icon-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  background: var(--surface-alt);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.85rem;
  cursor: pointer;
  user-select: none;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.btn {
  padding: 0.44rem 0.8rem;
}

.icon-btn {
  width: 38px;
  height: 38px;
  padding: 0;
}

.btn:hover:not(:disabled):not(.btn-disabled),
.icon-btn:hover:not(:disabled):not(.btn-disabled) {
  border-color: var(--accent);
  background: #2b3849;
}

.icon-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(126, 200, 80, 0.12);
}

.btn:disabled,
.icon-btn:disabled,
.btn-disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent-dark);
  border-color: var(--accent);
  color: #ffffff;
  font-weight: 600;
}

.btn-primary:hover {
  background: #5ca237;
}

.btn-sm {
  padding: 0.3rem 0.65rem;
  font-size: 0.8rem;
}

/* Color dinámico del ícono de paleta según la paleta DMG activa */
.palette-btn[data-palette="0"] {
  color: #88c070;
}

.palette-btn[data-palette="1"] {
  color: #d8d8ce;
}

.palette-btn[data-palette="2"] {
  color: #e6a84c;
}

.palette-btn .swatch-dot {
  fill: currentColor;
  stroke: none;
}

.workspace {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
  gap: 1.25rem;
  padding: 1.25rem;
}

/* Barra lateral izquierda: juegos precargados */
.library-panel {
  width: min(100%, 270px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.9rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.library-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.library-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--accent);
}

.library-title h2 {
  margin: 0;
  font-size: 0.95rem;
  color: var(--text);
}

/* Botón ? y globo de texto explicativo */
.info-popover-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.info-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--accent);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}

.info-btn:hover,
.info-popover-wrap.open .info-btn {
  border-color: var(--accent);
  background: rgba(126, 200, 80, 0.14);
}

.info-popover {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 235px;
  padding: 0.65rem 0.75rem;
  border-radius: 10px;
  border: 1px solid var(--accent);
  background: #131a22;
  color: var(--text);
  font-size: 0.76rem;
  line-height: 1.45;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.55);
  z-index: 30;
  opacity: 0;
  visibility: hidden;
  transform: translateY(-4px);
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
  pointer-events: none;
}

.info-popover::before {
  content: "";
  position: absolute;
  top: -6px;
  right: 9px;
  width: 10px;
  height: 10px;
  background: #131a22;
  border-left: 1px solid var(--accent);
  border-top: 1px solid var(--accent);
  transform: rotate(45deg);
}

.info-popover-wrap:hover .info-popover,
.info-popover-wrap:focus-within .info-popover,
.info-popover-wrap.open .info-popover {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
  pointer-events: auto;
}

.rom-catalog {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.rom-card {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.55rem 0.65rem;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  transition: border-color 0.15s, background 0.15s;
}

.rom-card:hover {
  border-color: #465b75;
}

.rom-card.playing {
  border-color: var(--accent);
  background: rgba(126, 200, 80, 0.08);
}

.rom-card-title {
  margin: 0;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.btn-play-icon {
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent-dark);
  color: #ffffff;
  padding: 0;
}

.btn-play-icon:hover:not(:disabled) {
  background: #5ca237;
  transform: scale(1.04);
}

.btn-play-icon svg {
  fill: currentColor;
  stroke: none;
}

.btn-play-icon.is-loading {
  opacity: 0.7;
}

.catalog-empty {
  margin: 0;
  font-size: 0.82rem;
  color: var(--muted);
}

/* Consola central */
.console-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.95rem;
  width: min(100%, 540px);
  transition: width 0.2s ease;
}

.console-section.expanded {
  width: min(100%, 780px);
}

.console-section.drag-active .bezel {
  outline: 3px dashed var(--accent);
  outline-offset: 4px;
}

.bezel {
  width: 100%;
  background: var(--bezel);
  border: 1px solid var(--border);
  border-radius: 14px 14px 36px 14px;
  padding: 0.75rem 1.05rem 0.9rem;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
}

.bezel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.55rem;
}

.power-led {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #553030;
  transition: background 0.2s, box-shadow 0.2s;
}

.power-led.on {
  background: #ff4545;
  box-shadow: 0 0 8px #ff4545;
}

.bezel-tools {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.bezel-btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(17, 22, 28, 0.45);
  color: var(--muted);
  cursor: pointer;
}

.bezel-btn:hover {
  color: var(--text);
  border-color: var(--accent);
}

.screen-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 160 / 144;
  background: #081820;
  border-radius: 6px;
  overflow: hidden;
  border: 2px solid #1b2026;
}

.screen-wrap:fullscreen {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000000;
  border: none;
  border-radius: 0;
}

.screen-wrap:fullscreen #screen {
  width: auto;
  height: 100%;
  max-width: 100vw;
  aspect-ratio: 160 / 144;
}

#screen {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.drop-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 1.2rem;
  background: rgba(8, 24, 32, 0.84);
  color: var(--text);
}

.status-bar {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 0.5rem;
  min-height: 1.1rem;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.touch-controls {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.9rem;
  align-items: center;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  touch-action: none;
}

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 46px);
  grid-template-rows: repeat(3, 46px);
  justify-content: center;
  align-items: center;
}

.pad-btn {
  width: 46px;
  height: 46px;
  background: #222933;
  color: var(--text);
  border: 1px solid var(--border);
  font-size: 1rem;
  cursor: pointer;
  touch-action: none;
  user-select: none;
}

.pad-up {
  grid-column: 2;
  grid-row: 1;
  border-radius: 8px 8px 0 0;
}

.pad-left {
  grid-column: 1;
  grid-row: 2;
  border-radius: 8px 0 0 8px;
}

.pad-center {
  grid-column: 2;
  grid-row: 2;
  width: 46px;
  height: 46px;
  background: #222933;
}

.pad-right {
  grid-column: 3;
  grid-row: 2;
  border-radius: 0 8px 8px 0;
}

.pad-down {
  grid-column: 2;
  grid-row: 3;
  border-radius: 0 0 8px 8px;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 1rem;
}

.round-btn {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 2px solid #9c3852;
  background: #7c233c;
  color: #ffffff;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  touch-action: none;
  user-select: none;
}

.round-btn:nth-child(1) {
  transform: translateY(10px);
}

.round-btn:nth-child(2) {
  transform: translateY(-10px);
}

.meta-buttons {
  grid-column: 1 / -1;
  display: flex;
  justify-content: center;
  gap: 1rem;
}

.pill-btn {
  padding: 0.35rem 1.1rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #2b3440;
  color: var(--text);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  cursor: pointer;
  touch-action: none;
  user-select: none;
}

.pad-btn.pressed,
.round-btn.pressed,
.pill-btn.pressed {
  filter: brightness(1.35);
  border-color: var(--accent);
}

/* Leyenda visual ordenada de controles de teclado */
.controls-card {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 0.55rem;
  padding: 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.control-item {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  padding: 0.45rem 0.6rem;
  background: rgba(17, 22, 28, 0.55);
  border: 1px solid rgba(49, 64, 82, 0.7);
  border-radius: 8px;
}

.control-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.key-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.76rem;
  color: var(--muted);
}

kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  padding: 0.12rem 0.4rem;
  border-radius: 5px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  background: var(--surface-alt);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.74rem;
  font-weight: 600;
}

/* Panel lateral derecho: debug */
.debug-panel {
  width: min(100%, 350px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.debug-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.debug-header h2 {
  margin: 0;
  font-size: 1rem;
}

.debug-fps {
  font-size: 0.74rem;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.debug-actions {
  display: flex;
  gap: 0.45rem;
}

.btn-debug-action {
  padding: 0.36rem 0.68rem;
  border-radius: 8px;
  font-size: 0.78rem;
  font-weight: 600;
  background: linear-gradient(180deg, #2b3849 0%, #212b38 100%);
  border: 1px solid #3c4e63;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

.btn-debug-action:hover:not(:disabled) {
  border-color: var(--accent);
  color: #ffffff;
  background: linear-gradient(180deg, #334357 0%, #263342 100%);
}

.btn-debug-action:active:not(:disabled) {
  transform: translateY(1px);
}

.debug-block h3 {
  margin: 0 0 0.4rem;
  font-size: 0.82rem;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.debug-block pre {
  margin: 0;
  padding: 0.6rem;
  background: #0d1218;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.78rem;
  line-height: 1.4;
  overflow-x: auto;
  color: #d6e4f0;
}

#tiles-canvas {
  width: 256px;
  height: 384px;
  max-width: 100%;
  display: block;
  margin: 0 auto;
  background: #081820;
  border: 1px solid var(--border);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 1.25rem;
  font-size: 0.78rem;
  color: var(--muted);
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.footer-left {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
}

.footer-roms-link a {
  color: var(--accent);
  text-decoration: underline;
}

.footer-roms-link a:hover {
  color: #a4e478;
}
