:root {
  --ink: #f7f4e8;
  --muted: #a8b3ad;
  --panel: #18232b;
  --panel-2: #202e35;
  --line: #3c4a4f;
  --red: #e84855;
  --gold: #f9c74f;
  --green: #43aa8b;
  --blue: #4d96ff;
  --black: #0b0f14;
  --shadow: 0 22px 70px rgba(0, 0, 0, 0.36);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 14%, rgba(232, 72, 85, 0.14), transparent 28%),
    linear-gradient(135deg, #101820 0%, #172226 52%, #121619 100%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
a {
  color: inherit;
  font: inherit;
}

button {
  cursor: pointer;
}

.gamebox-shell {
  min-height: 100vh;
  padding: 18px;
}

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 16px;
  max-width: 1440px;
  margin: 0 auto 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  font-weight: 900;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--red);
  color: #fff;
  font-size: 13px;
  box-shadow: 4px 4px 0 var(--black);
}

.status-strip {
  display: flex;
  justify-content: center;
  gap: 10px;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.status-strip span {
  min-height: 34px;
  padding: 8px 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: rgba(11, 15, 20, 0.38);
  white-space: nowrap;
}

.icon-button,
.toolbar button {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 3px 3px 0 var(--black);
  font-weight: 900;
}

.icon-button {
  width: 42px;
}

.stage {
  display: grid;
  grid-template-columns: minmax(220px, 300px) minmax(420px, 1fr) minmax(260px, 340px);
  gap: 16px;
  max-width: 1440px;
  margin: 0 auto;
  align-items: stretch;
}

.library,
.controls {
  min-width: 0;
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(24, 35, 43, 0.88);
  box-shadow: var(--shadow);
}

.library-head h1,
.game-list h2,
.translation-panel h2,
.keymap h2 {
  margin: 0 0 8px;
  font-size: 24px;
  line-height: 1.1;
  letter-spacing: 0;
}

.game-list h2 {
  margin-top: 18px;
  font-size: 15px;
  color: var(--gold);
}

.library-search {
  margin: 10px 0 8px;
}

.library-search input {
  width: 100%;
  min-height: 38px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #10181f;
  color: var(--ink);
  font: inherit;
  font-weight: 800;
}

.library-search input::placeholder {
  color: #71807f;
}

.library-count {
  min-height: 20px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.game-card-list {
  max-height: min(48vh, 520px);
  margin-top: 8px;
  padding-right: 4px;
  overflow-y: auto;
}

.library-head p,
.keymap p {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.translation-panel {
  margin-top: 18px;
}

.device-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  margin-top: 12px;
}

.device-row select,
.device-row button {
  min-height: 38px;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #10181f;
  color: var(--ink);
  font-weight: 800;
}

.device-row select {
  padding: 0 10px;
}

.device-row button {
  padding: 0 12px;
}

.toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 36px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.toggle-row input {
  width: 18px;
  height: 18px;
  accent-color: var(--green);
}

.system-list {
  display: grid;
  gap: 10px;
  margin-top: 14px;
}

.system-card {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 62px;
  padding: 10px 12px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #111b22;
}

.system-card.is-active {
  border-color: var(--blue);
  box-shadow: inset 0 0 0 2px rgba(77, 150, 255, 0.2);
}

.system-card.is-disabled {
  opacity: 0.68;
}

.system-card strong,
.system-card small {
  display: block;
}

.system-card small {
  color: var(--muted);
  font-size: 12px;
}

.game-card {
  display: grid;
  grid-template-columns: 58px 1fr;
  gap: 12px;
  width: 100%;
  min-height: 82px;
  margin-top: 12px;
  padding: 10px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
}

.game-card.is-active {
  border-color: var(--gold);
  box-shadow: inset 0 0 0 2px rgba(249, 199, 79, 0.2);
}

.game-card strong,
.game-card small {
  display: block;
}

.game-card strong {
  margin: 2px 0 6px;
  font-size: 15px;
}

.game-card small {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.game-thumb {
  width: 58px;
  height: 58px;
  border-radius: 6px;
  image-rendering: pixelated;
}

.game-thumb.is-animated {
  background-size: calc(100% * var(--thumb-frames)) 100%;
  background-position: 0 50%;
  animation: thumb-preview var(--thumb-duration, 2.1s) steps(var(--thumb-frames), end) infinite;
}

@keyframes thumb-preview {
  to {
    background-position: calc(100% * var(--thumb-frames) * -1) 50%;
  }
}

.lj65-thumb {
  background:
    linear-gradient(#e84855 0 0) 12px 10px / 12px 12px no-repeat,
    linear-gradient(#f9c74f 0 0) 24px 10px / 12px 12px no-repeat,
    linear-gradient(#43aa8b 0 0) 24px 22px / 12px 12px no-repeat,
    linear-gradient(#4d96ff 0 0) 36px 22px / 12px 12px no-repeat,
    #11172b;
}

.croom-thumb {
  background:
    repeating-linear-gradient(90deg, #4d96ff 0 10px, #f7f4e8 10px 12px, #43aa8b 12px 22px),
    #18232b;
}

.console {
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 12px;
  min-width: 0;
}

.screen-bezel {
  position: relative;
  display: grid;
  min-height: 0;
  place-items: center;
  padding: clamp(10px, 2vw, 20px);
  border: 12px solid #2a3034;
  border-radius: 8px;
  background: #070a0f;
  box-shadow: var(--shadow), inset 0 0 0 2px #555d62;
  overflow: hidden;
}

#emulator-screen {
  position: relative;
  display: grid;
  width: min(100%, calc((100vh - 170px) * 1.3333));
  max-width: 100%;
  height: auto;
  min-height: 260px;
  place-items: center;
  aspect-ratio: var(--screen-aspect, 4 / 3);
}

#emulator-screen canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100% !important;
  height: 100% !important;
  max-width: 100% !important;
  max-height: 100% !important;
  object-fit: fill;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.system-empty {
  display: grid;
  width: 100%;
  min-height: 240px;
  place-items: center;
  align-content: center;
  gap: 10px;
  padding: 24px;
  border: 1px dashed rgba(255, 255, 255, 0.24);
  border-radius: 8px;
  color: var(--muted);
  text-align: center;
}

.system-empty strong {
  color: var(--ink);
  font-size: 22px;
}

.system-empty span {
  max-width: 420px;
  line-height: 1.6;
}

.scanlines {
  pointer-events: none;
  position: absolute;
  inset: 12px;
  z-index: 2;
  opacity: 0.12;
  background: repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.2) 0 1px, transparent 1px 4px);
  mix-blend-mode: screen;
}

.translation-layer {
  pointer-events: none;
  position: absolute;
  inset: 20px;
  z-index: 5;
  display: none;
  overflow: visible;
}

.translation-layer.is-active {
  display: block;
}

.translation-label {
  position: absolute;
  transform: translate(-50%, -100%);
  min-width: 74px;
  max-width: min(240px, 72%);
  padding: 4px 8px;
  border: 1px solid rgba(249, 199, 79, 0.78);
  border-radius: 6px;
  background: rgba(5, 7, 12, 0.9);
  color: var(--gold);
  font-size: 12px;
  font-weight: 900;
  line-height: 1.25;
  text-align: center;
  text-shadow: 1px 1px 0 #000;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.34);
  white-space: normal;
}

.translation-label::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -5px;
  width: 8px;
  height: 8px;
  border-right: 1px solid rgba(249, 199, 79, 0.78);
  border-bottom: 1px solid rgba(249, 199, 79, 0.78);
  background: rgba(5, 7, 12, 0.9);
  transform: translateX(-50%) rotate(45deg);
}

.translation-label[data-placement="below"] {
  transform: translate(-50%, 0);
}

.translation-label[data-placement="below"]::after {
  top: -5px;
  bottom: auto;
  transform: translateX(-50%) rotate(225deg);
}

.pause-layer {
  pointer-events: none;
  position: absolute;
  inset: 12px;
  z-index: 8;
  display: none;
  place-items: center;
  align-content: center;
  gap: 10px;
  background: rgba(0, 0, 0, 0.56);
  text-align: center;
}

.pause-layer.is-visible {
  display: grid;
}

.pause-layer strong {
  font-size: clamp(36px, 8vw, 84px);
  letter-spacing: 0;
}

.pause-layer span {
  color: var(--gold);
  font-weight: 800;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.nes-pad {
  display: grid;
  gap: 18px;
  margin-top: 22px;
}

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

.dpad button,
.round-buttons button,
.system-buttons button {
  border: 1px solid #05070c;
  background: #303941;
  box-shadow: 3px 3px 0 #05070c;
  font-weight: 900;
  user-select: none;
  touch-action: none;
}

.dpad button {
  border-radius: 8px;
}

.dpad [data-touch="up"] {
  grid-column: 2;
}

.dpad [data-touch="left"] {
  grid-column: 1;
  grid-row: 2;
}

.dpad [data-touch="right"] {
  grid-column: 3;
  grid-row: 2;
}

.dpad [data-touch="down"] {
  grid-column: 2;
  grid-row: 3;
}

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

.round-buttons button {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: var(--red);
}

.round-buttons [data-touch="a"] {
  background: var(--gold);
  color: #141414;
}

.system-buttons button {
  min-width: 92px;
  min-height: 38px;
  border-radius: 999px;
  background: var(--green);
  font-size: 12px;
}

[data-pressed="true"] {
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 #05070c;
}

@media (max-width: 1080px) {
  .stage {
    grid-template-columns: 1fr;
  }

  .library {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 12px;
  }

  .game-card {
    margin: 0;
  }

  .controls {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 18px;
    align-items: center;
  }

  .nes-pad {
    margin: 0;
  }

  #emulator-screen {
    width: min(100%, calc((100vh - 210px) * 1.3333));
    height: auto;
    min-height: 0;
  }
}

@media (max-width: 720px) {
  .gamebox-shell {
    padding: 10px;
  }

  .topbar {
    grid-template-columns: 1fr auto;
  }

  .status-strip {
    grid-column: 1 / -1;
    justify-content: flex-start;
    overflow-x: auto;
  }

  .library,
  .controls {
    padding: 12px;
  }

  .library {
    grid-template-columns: 1fr;
  }

  .controls {
    grid-template-columns: 1fr;
  }

  .screen-bezel {
    border-width: 8px;
  }

  #emulator-screen {
    width: 100%;
  }

  .toolbar {
    grid-template-columns: 1fr 1fr;
  }
}
