/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  /* Height reserved for the touch controller. Scaled down on narrow screens
     below. Used by both #touch-controller and the canvas container so the
     two stay in sync. */
  --touch-h: clamp(150px, 30vh, 200px);
}
body {
  font-family: 'Inter', system-ui, sans-serif;
  background: #000;
  color: #f2f2f2;
  overflow: hidden;
}
canvas { touch-action: none; }
button { font-family: inherit; }
[hidden] { display: none !important; }

/* ===== App Layout ===== */
#app {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #000;
}

/* ===== Header / Toolbar ===== */
#toolbar {
  flex-shrink: 0;
  height: 44px;
  background: #111;
  border-bottom: 1px solid #222;
  display: flex;
  align-items: center;
  padding: 0 10px;
  gap: 6px;
  z-index: 10;
}
.logo { font-weight: 900; font-size: 16px; letter-spacing: -0.5px; margin-right: 4px; white-space: nowrap; }
.logo-nes { color: #e53e3e; }
.logo-play { color: #fff; }
#rom-label {
  font-size: 11px;
  color: #888;
  font-family: monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 140px;
}
.spacer { flex: 1; }

/* Header buttons */
.hdr-btn {
  background: none;
  border: 1px solid transparent;
  border-radius: 5px;
  color: #ccc;
  font-size: 16px;
  cursor: pointer;
  padding: 3px 6px;
  line-height: 1;
  white-space: nowrap;
  user-select: none;
}
.hdr-btn:hover { border-color: #555; }
.hdr-btn.active { background: #c53030; border-color: #c53030; color: #fff; }
.hdr-btn.active:hover { border-color: #c53030; }

#volume-slider {
  width: 72px;
  accent-color: #e53e3e;
  cursor: pointer;
}

/* ===== Main Area ===== */
#main-area {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: #000;
}

/* ===== Drop Zone ===== */
#dropzone {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.dropzone-inner {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  cursor: pointer;
  user-select: none;
  border: 2px dashed #4b5563;
  border-radius: 12px;
  padding: 48px;
  min-height: 280px;
  background: rgba(17, 24, 39, 0.6);
  transition: all 0.2s;
}
.dropzone-inner:hover {
  border-color: #b91c1c;
  background: rgba(31, 41, 55, 0.6);
}
.dropzone-inner.dragging {
  border-color: #ef4444;
  background: rgba(69, 10, 10, 0.3);
  transform: scale(1.02);
}
.dropzone-icon { font-size: 60px; user-select: none; }
.dropzone-text { text-align: center; }
.dropzone-title { font-size: 20px; font-weight: 700; color: #fff; }
.dropzone-subtitle { font-size: 14px; color: #9ca3af; margin-top: 4px; }
.dropzone-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: #6b7280;
}
.dropzone-ext {
  padding: 4px 8px;
  border-radius: 4px;
  background: #1f2937;
  font-family: monospace;
}

/* ===== Canvas ===== */
#canvas-container {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#nes-canvas {
  width: 100%;
  height: 100%;
  /* contain keeps the 256:240 NES aspect ratio regardless of container shape.
     Without this, a portrait viewport stretches the picture vertically. */
  object-fit: contain;
  image-rendering: pixelated;
  display: block;
}

/* Portrait: pin the screen to the top and reserve the bottom band for the
   gamepad. The reserved space is always present in portrait so the layout
   does not jump when the touch controller is toggled. */
@media (orientation: portrait) {
  #canvas-container {
    bottom: var(--touch-h);
    align-items: flex-start;
  }
  #nes-canvas {
    object-position: center top;
  }
  #pause-overlay {
    bottom: var(--touch-h);
  }
}

/* Pause overlay */
#pause-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.55);
  cursor: pointer;
}
.pause-btn {
  padding: 12px 28px;
  background: #c53030;
  border-radius: 10px;
  color: #fff;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 1px;
  pointer-events: none;
}

/* ===== Side Panel ===== */
#panel-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: flex-end;
  pointer-events: none;
  z-index: 20;
}
#panel-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.4);
  pointer-events: auto;
}
#panel-box {
  position: relative;
  pointer-events: auto;
  width: 340px;
  max-width: 90vw;
  max-height: calc(100% - 16px);
  margin: 8px;
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 10px;
  padding: 16px;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.7);
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
#panel-title { color: #fff; font-weight: 700; font-size: 14px; }
#panel-close {
  background: none;
  border: none;
  color: #888;
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}

/* ===== Panel: Saves ===== */
.panel-section { display: flex; flex-direction: column; gap: 12px; }
.panel-section h3 { font-size: 14px; font-weight: 600; color: #d1d5db; }
.panel-hint { font-size: 12px; color: #9ca3af; }

.save-input-row, .genie-input-row { display: flex; gap: 8px; }
.panel-input {
  flex: 1;
  background: #1f2937;
  border: 1px solid #4b5563;
  border-radius: 4px;
  padding: 6px 12px;
  color: #fff;
  font-size: 14px;
  outline: none;
}
.panel-input::placeholder { color: #6b7280; }
.panel-input:focus { border-color: #ef4444; }
.panel-input.mono { font-family: monospace; text-transform: uppercase; font-size: 13px; }

.panel-btn-primary {
  padding: 6px 16px;
  background: #b91c1c;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.panel-btn-primary:hover { background: #dc2626; }

.panel-btn-full {
  width: 100%;
  padding: 8px 0;
  background: #c53030;
  color: #fff;
  border: none;
  border-radius: 6px;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.panel-btn-full:hover { background: #dc2626; }

/* Save state item */
.save-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #1f2937;
  border-radius: 4px;
  padding: 8px;
}
.save-thumb {
  width: 64px;
  height: 48px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid #4b5563;
  image-rendering: pixelated;
}
.save-info { flex: 1; min-width: 0; }
.save-name { font-size: 14px; color: #fff; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.save-date { font-size: 12px; color: #9ca3af; }
.save-load-btn {
  padding: 4px 8px;
  background: #15803d;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
.save-load-btn:hover { background: #16a34a; }
.save-del-btn {
  padding: 4px 8px;
  background: #374151;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
.save-del-btn:hover { background: #b91c1c; }

.save-states-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 256px;
  overflow-y: auto;
  padding-right: 4px;
}

/* Battery section */
.battery-section {
  border-top: 1px solid #374151;
  padding-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.battery-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.battery-btn {
  padding: 6px 12px;
  background: #374151;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
.battery-btn:hover { background: #4b5563; }
.battery-import-label {
  padding: 6px 12px;
  background: #374151;
  color: #fff;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
.battery-import-label:hover { background: #4b5563; }

.success-msg {
  font-size: 12px;
  color: #4ade80;
  background: rgba(20, 83, 45, 0.3);
  border-radius: 4px;
  padding: 4px 8px;
}
.error-msg { font-size: 12px; color: #f87171; }

/* ===== Panel: Game Genie ===== */
.genie-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #1f2937;
  border-radius: 4px;
  padding: 8px 12px;
}
.genie-item input[type="checkbox"] { accent-color: #ef4444; }
.genie-code { font-family: monospace; font-size: 14px; flex: 1; }
.genie-code.enabled { color: #fff; }
.genie-code.disabled { color: #6b7280; text-decoration: line-through; }
.genie-addr { font-size: 12px; color: #6b7280; }
.genie-remove {
  background: none;
  border: none;
  color: #6b7280;
  font-size: 12px;
  cursor: pointer;
}
.genie-remove:hover { color: #f87171; }
.genie-list { display: flex; flex-direction: column; gap: 8px; }
.genie-empty { font-size: 12px; color: #6b7280; font-style: italic; }

/* ===== Panel: Keymap ===== */
.keymap-listening {
  background: rgba(113, 63, 18, 0.5);
  border: 1px solid #ca8a04;
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 12px;
  color: #fde047;
}
.keymap-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.keymap-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #1f2937;
  border-radius: 4px;
  padding: 8px 12px;
}
.keymap-label { font-size: 12px; color: #9ca3af; width: 56px; }
.keymap-btn {
  flex: 1;
  text-align: center;
  font-size: 12px;
  font-family: monospace;
  border-radius: 4px;
  padding: 4px 8px;
  cursor: pointer;
  border: 1px solid #4b5563;
  background: #374151;
  color: #fff;
}
.keymap-btn:hover { border-color: #ef4444; }
.keymap-btn.listening {
  border-color: #eab308;
  background: rgba(113, 63, 18, 0.5);
  color: #fde047;
  animation: pulse 1.5s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}
.keymap-reset {
  font-size: 12px;
  color: #9ca3af;
  background: none;
  border: none;
  cursor: pointer;
  text-decoration: underline;
}
.keymap-reset:hover { color: #fff; }
.keymap-hint { font-size: 12px; color: #6b7280; }
.keymap-hint p { margin: 2px 0; }

/* ===== Touch Controller ===== */
#touch-controller {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  height: var(--touch-h);
  pointer-events: none;
}
.touch-dpad {
  position: absolute;
  left: 24px;
  bottom: 24px;
  width: 168px;
  height: 168px;
  pointer-events: auto;
}
.touch-dpad-bg {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  background: rgba(17, 24, 39, 0.9);
  border: 2px solid #374151;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}
.touch-dpad-cross {
  position: absolute;
  left: 12px;
  top: 12px;
  width: 144px;
  height: 144px;
}
.dpad-btn {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 18px;
  user-select: none;
  cursor: pointer;
  background: #374151;
  border: 2px solid #6b7280;
  pointer-events: auto;
}
.dpad-btn:active { background: #4b5563; }
.dpad-up    { left: 33.3%; top: 0;     width: 33.3%; height: 33.3%; border-radius: 8px 8px 0 0; }
.dpad-down  { left: 33.3%; bottom: 0;  width: 33.3%; height: 33.3%; border-radius: 0 0 8px 8px; }
.dpad-left  { left: 0;     top: 33.3%; width: 33.3%; height: 33.3%; border-radius: 8px 0 0 8px; }
.dpad-right { right: 0;    top: 33.3%; width: 33.3%; height: 33.3%; border-radius: 0 8px 8px 0; }
.dpad-center {
  position: absolute;
  left: 33.3%;
  top: 33.3%;
  width: 33.4%;
  height: 33.4%;
  background: #374151;
  border: 2px solid #6b7280;
}

.touch-center {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 24px;
  display: flex;
  gap: 12px;
  pointer-events: auto;
}
.touch-meta-btn {
  width: 56px;
  height: 24px;
  background: #374151;
  border-radius: 12px;
  border: 2px solid #6b7280;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  user-select: none;
  cursor: pointer;
}
.touch-meta-btn:active { background: #4b5563; }

.touch-ab {
  position: absolute;
  right: 24px;
  bottom: 24px;
  width: 168px;
  height: 168px;
  pointer-events: auto;
}
.touch-ab-bg {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  background: rgba(17, 24, 39, 0.9);
  border: 2px solid #374151;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}
.touch-ab-inner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.touch-ab-btns {
  position: relative;
  width: 130px;
  height: 100px;
}
.ab-btn {
  position: absolute;
  width: 56px;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 900;
  font-size: 20px;
  user-select: none;
  cursor: pointer;
  border-radius: 50%;
  border: 4px solid;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}
.ab-btn:active { opacity: 0.7; }
.ab-b {
  background: #4b5563;
  border-color: #9ca3af;
  left: 0;
  top: 22px;
}
.ab-a {
  background: #b91c1c;
  border-color: #f87171;
  right: 0;
  top: 0;
}

/* ===== Touch Controller — responsive sizing =====
   At default sizes the D-pad (168px + 24px left margin) and A/B pad
   (168px + 24px right margin) need ~384px between them, plus ~124px for
   the SELECT/START cluster in the middle. On narrower screens they
   collide, so we step the buttons down to keep clearances at every size.
*/
@media (max-width: 600px) {
  .touch-dpad,
  .touch-ab {
    width: 140px;
    height: 140px;
    bottom: 16px;
  }
  .touch-dpad { left: 16px; }
  .touch-ab    { right: 16px; }
  .touch-dpad-cross {
    left: 10px;
    top: 10px;
    width: 120px;
    height: 120px;
  }
  .touch-ab-btns { width: 110px; height: 86px; }
  .ab-btn { width: 48px; height: 48px; font-size: 18px; border-width: 3px; }
  .ab-b   { top: 19px; }
  .touch-center { bottom: 20px; }
}

@media (max-width: 400px) {
  .touch-dpad,
  .touch-ab {
    width: 118px;
    height: 118px;
    bottom: 12px;
  }
  .touch-dpad { left: 10px; }
  .touch-ab    { right: 10px; }
  .touch-dpad-cross {
    left: 9px;
    top: 9px;
    width: 100px;
    height: 100px;
  }
  .touch-ab-btns { width: 92px; height: 72px; }
  .ab-btn { width: 40px; height: 40px; font-size: 15px; border-width: 3px; }
  .ab-b   { top: 16px; }
  .touch-meta-btn { width: 48px; height: 22px; font-size: 9px; }
  .touch-center { gap: 8px; bottom: 14px; }
}

@media (max-width: 340px) {
  .touch-dpad,
  .touch-ab {
    width: 102px;
    height: 102px;
  }
  .touch-dpad { left: 8px; }
  .touch-ab    { right: 8px; }
  .touch-dpad-cross {
    left: 8px;
    top: 8px;
    width: 86px;
    height: 86px;
  }
  .touch-ab-btns { width: 80px; height: 64px; }
  .ab-btn { width: 36px; height: 36px; font-size: 13px; }
  .ab-b   { top: 14px; }
  .touch-meta-btn { width: 42px; height: 20px; font-size: 8px; }
  .touch-center { gap: 6px; }
}
