:root {
  color-scheme: light;
  font-family: system-ui, -apple-system, "Microsoft YaHei", sans-serif;
  background: #d6e8f5;
  color: #33302b;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; min-height: 100%; }
body { display: flex; align-items: center; justify-content: center; min-height: 100vh; min-height: 100svh; padding: 24px; overflow-x: hidden; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
button, input { font: inherit; }
button { cursor: pointer; -webkit-appearance: none; appearance: none; touch-action: manipulation; }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 3px solid #1e78ba; outline-offset: 3px; }

.console {
  position: relative;
  width: min(980px, 100%);
  padding: 53px 16px 17px;
  background: #cf242a;
  border: 8px solid #b71219;
  border-radius: 28px;
  box-shadow: 0 0 0 10px #3e3a38, 0 20px 45px #0004;
}
.top-menu { position: absolute; top: 10px; left: 16px; right: 16px; display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.menu-button {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 29px; padding: 3px 10px; border: 2px solid #edcc8e;
  border-radius: 6px; background: #692020; color: #fff6da;
  font-size: 12px; font-weight: 700; text-decoration: none;
}
[hidden] { display: none !important; }
.panel {
  display: flex; align-items: stretch; gap: 8px; min-height: 342px;
  padding: 12px 16px; border-radius: 20px; background: #f7e9ba;
  box-shadow: inset 6px 6px #fff8df, inset -5px -5px #d7bd80;
}
.left-controls, .right-controls { flex: 0 0 162px; display: flex; align-items: end; justify-content: center; padding-bottom: 14px; }
.center-area { min-width: 0; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; }
.screen-frame {
  position: relative; width: min(100%, 472px); aspect-ratio: 4 / 3;
  border: 16px solid #30373a; border-radius: 23px; background: #080a0c;
  box-shadow: inset 0 0 0 5px #62605b, 4px 7px 0 #c2a66c;
  overflow: hidden;
}
#game-screen { display: block; width: 100%; height: 100%; object-fit: fill; image-rendering: pixelated; background: #000; }
.probe-banner { position: absolute; inset: 0; z-index: 2; display: grid; place-items: center; padding: 16px; background: #15191b; color: #fff5d8; font-size: 15px; font-weight: 700; text-align: center; }
.standby { position: absolute; inset: 0; display: grid; place-items: center; background: #111; }
#static-screen { width: 100%; height: 100%; object-fit: cover; image-rendering: pixelated; opacity: .34; }
.standby-content { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 12px; text-align: center; }
#game-label { margin: 0; color: #fff0cc; font-weight: 800; font-size: clamp(12px, 1.5vw, 17px); text-shadow: 0 2px 5px #000, 0 0 8px #d2191a; }
.standby-actions { display: flex; gap: 9px; justify-content: center; flex-wrap: wrap; }
.primary-button, .secondary-button { min-height: 42px; padding: 8px 14px; border: 3px solid #fff4d0; border-radius: 9px; color: #fff; font-weight: 900; box-shadow: 0 4px 0 #600d0d; }
.primary-button { background: linear-gradient(#ee3333,#bd1018); }
.secondary-button { background: linear-gradient(#5a676a,#303d40); }
.primary-button:disabled { opacity: .6; cursor: wait; }

/* The local sprite sheet is cropped once into a stable D-pad canvas. */
.dpad { position: relative; flex: none; width: 162px; height: 162px; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; cursor: pointer; }
.dpad-canvas { display: block; width: 100%; height: 100%; pointer-events: none; }
.sprite-button { display: block; flex: none; border: 0; background: none; padding: 0; touch-action: none; user-select: none; -webkit-user-select: none; }
.sprite-button .sprite { display: block; background: var(--skin-url, url("assets/buttons.webp")) no-repeat; pointer-events: none; transform-origin: 0 0; }
.system-controls { display: flex; align-items: center; justify-content: center; gap: 7px; min-height: 58px; }
.system-controls .sprite-button { width: 106px; height: 57px; }
.system-controls .sprite { width: 235px; height: 125px; transform: scale(.45); }
.select .sprite { background-position: -205px -515px; }
.select.pressed .sprite { background-position: -445px -515px; }
.start .sprite { background-position: -788px -515px; }
.start.pressed .sprite { background-position: -1035px -515px; }
.right-controls { display: grid; align-content: end; grid-template-columns: repeat(2, 76px); grid-template-rows: 76px 76px 76px; gap: 4px; touch-action: none; }
.action-button { width: 76px; height: 76px; overflow: hidden; }
.action-button .sprite { width: 205px; height: 205px; transform: scale(.37); }
.a .sprite { background-position: -205px -650px; }
.a.pressed .sprite { background-position: -467px -650px; }
.b .sprite { background-position: -808px -650px; }
.b.pressed .sprite { background-position: -1051px -650px; }
.aa .sprite { background-position: -10px -850px; }
.aa.pressed .sprite { background-position: -223px -850px; }
.bb .sprite { background-position: -469px -850px; }
.bb.pressed .sprite { background-position: -674px -850px; }
.combo { grid-column: 1 / -1; display: flex; align-items: center; justify-content: center; justify-self: center; width: 104px; height: 76px; }
.combo .sprite { flex: none; width: 263px; height: 161px; transform: scale(.395); transform-origin: center center; background-position: -907px -880px; }
.combo.pressed .sprite { background-position: -1180px -880px; }

.modal { width: min(680px, calc(100vw - 26px)); max-height: min(85dvh, 750px); padding: 20px; border: 4px solid #f7e9ba; border-radius: 14px; background: #30383a; color: #fff5d8; box-shadow: 0 18px 55px #0009; }
.modal::backdrop { background: #000b; backdrop-filter: blur(3px); }
.modal.narrow { max-width: 450px; }
#game-dialog[open] { display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; justify-content: space-between; border-bottom: 2px solid #6d7475; padding-bottom: 9px; margin-bottom: 12px; }
.modal-head h2 { margin: 0; font-size: 19px; }
.close-button { width: 32px; height: 32px; border: 2px solid #fff; border-radius: 50%; background: #c91d24; color: #fff; font-weight: 900; font-size: 22px; line-height: 22px; }
.search { display: block; width: 100%; padding: 9px 12px; border: 2px solid #a9a39b; border-radius: 8px; background: #fef8e6; color: #20282a; -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }
.game-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(116px, 1fr)); align-content: start; gap: 14px; margin-top: 12px; padding: 8px 4px 12px; min-height: 120px; max-height: 55dvh; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.game-card { position: relative; min-width: 0; border: 2px solid #b98514; border-radius: 9px 9px 4px 4px; padding: 22px 5px 7px; background: #ffcf21; color: #9d1719; font-weight: 800; text-align: center; box-shadow: 0 4px 6px #0006, inset 0 4px #fff9; }
.game-card::before { content: ''; position: absolute; top: 7px; left: 21%; width: 58%; height: 2px; border-radius: 2px; background: #9c751a66; box-shadow: 0 5px #9c751a66; }
.cartridge-face { display: block; min-width: 0; padding: 5px 5px 7px; border-radius: 4px; background: #fffdf0; box-shadow: inset 0 0 4px #0004; }
.cover-frame { position: relative; display: grid; place-items: center; width: 100%; aspect-ratio: 4 / 3; overflow: hidden; border: 1px solid #45413a; border-radius: 3px; background: linear-gradient(145deg,#792324,#231d22); }
.cover-fallback { color: #f8dc95; font: 900 clamp(20px, 4vw, 34px) Georgia,serif; letter-spacing: .12em; text-shadow: 0 2px 3px #0009; }
.game-card img { position: absolute; inset: 0; display: block; width: 100%; height: 100%; object-fit: cover; }
.game-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 6px; font-size: 12px; }
.game-card:active { transform: translateY(2px); }
.game-sentinel { grid-column: 1 / -1; height: 2px; }
.catalog-retry { grid-column: 1 / -1; justify-self: center; padding: 8px 14px; border: 2px solid #f3d982; border-radius: 8px; background: #b71d24; color: #fff7dc; font-weight: 800; }
.game-list-status { margin: 8px 0 0; color: #d9c9a7; font-size: 12px; text-align: right; }
.save-list { display: grid; gap: 9px; max-height: 55dvh; overflow-y: auto; }
.save-row { display: flex; align-items: center; gap: 8px; padding: 9px; border: 1px solid #aa9c8a; border-radius: 8px; background: #475255; }
.save-row .label { flex: 1; min-width: 0; font-size: 13px; }
.save-row button { padding: 5px 9px; border: 2px solid #fff2d0; border-radius: 6px; background: #ba2024; color: white; font-weight: 700; }
.save-row button:disabled { opacity: .45; cursor: default; }
.support-text { line-height: 1.6; }
.donation-image { display: block; width: min(240px, 100%); margin: 0 auto; border: 4px solid #dec78d; border-radius: 10px; }
.source-link { margin: 14px 0 0; text-align: center; font-size: 12px; }
.source-link a { color: #ffe3a3; }
.log-hint { margin: 0 0 10px; font-size: 13px; line-height: 1.5; }
.log-text { display: block; width: 100%; height: min(50dvh, 340px); margin-bottom: 12px; padding: 10px; border: 1px solid #a9a39b; border-radius: 8px; background: #fffdf5; color: #20282a; font: 12px/1.45 ui-monospace, monospace; resize: vertical; -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }
.toast { position: fixed; top: max(12px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 50; min-width: 120px; max-width: min(90vw, 500px); padding: 8px 15px; border: 2px solid #d73b3b; border-radius: 25px; background: #20282ae8; color: #fff; text-align: center; opacity: 0; pointer-events: none; transition: opacity .2s; }
.toast.visible { opacity: 1; }

@media (max-width: 740px), (orientation: portrait) {
  html, body { overflow: hidden; overscroll-behavior: none; }
  body { position: fixed; inset: 0; width: 100%; height: 100dvh; min-height: 0; align-items: stretch; justify-content: flex-start; flex-direction: column; padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom)); }
  .console { display: flex; flex-direction: column; width: min(480px, 100%); height: calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom))); min-height: 0; margin: 0 auto; padding: 43px 8px 8px; border-width: 6px; border-radius: 20px; box-shadow: 0 0 0 5px #3e3a38, 0 12px 30px #0004; touch-action: none; }
  .top-menu { top: 6px; left: 6px; right: 6px; gap: 4px; flex-wrap: nowrap; }
  .menu-button { min-height: 25px; padding: 2px 5px; font-size: 10px; white-space: nowrap; }
  .panel { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) 236px; grid-template-areas: "screen screen" "pad actions"; align-items: start; gap: 3px 8px; min-height: 0; padding: 8px; }
  .center-area { grid-area: screen; width: 100%; height: 100%; justify-content: space-between; gap: 3px; }
  .screen-frame { width: min(100%, 430px); border-width: 10px; border-radius: 16px; }
  .system-controls { min-height: 47px; transform: scale(.8); }
  .left-controls { grid-area: pad; flex: none; min-width: 0; width: 100%; height: 236px; align-self: end; align-items: center; justify-content: center; padding: 0; }
  .right-controls { grid-area: actions; flex: none; justify-self: center; align-self: end; align-content: center; width: 156px; height: 236px; padding: 0; }
  .dpad { width: 150px; height: 150px; }
  .game-list { grid-template-columns: repeat(auto-fill, minmax(105px, 1fr)); gap: 10px; }
}
@media (max-width: 380px) and (orientation: portrait) {
  .panel { grid-template-rows: minmax(0, 1fr) 212px; }
  .right-controls { width: 140px; height: 212px; grid-template-columns: repeat(2, 68px); grid-template-rows: repeat(3, 68px); gap: 4px; }
  .action-button { width: 68px; height: 68px; }
  .action-button .sprite { transform: scale(.33); }
  .combo { width: 96px; height: 68px; }
  .combo .sprite { transform: scale(.365); }
  .left-controls { height: 212px; }
  .dpad { width: 136px; height: 136px; }
}
@media (max-width: 340px) and (orientation: portrait) {
  .panel { grid-template-rows: minmax(0, 1fr) 200px; }
  .right-controls { width: 128px; height: 200px; grid-template-columns: repeat(2, 62px); grid-template-rows: repeat(3, 62px); }
  .action-button { width: 62px; height: 62px; }
  .action-button .sprite { transform: scale(.30); }
  .combo { width: 88px; height: 62px; }
  .combo .sprite { transform: scale(.334); }
  .left-controls { height: 200px; }
  .dpad { width: 124px; height: 124px; }
  .menu-button { font-size: 9px; padding-inline: 3px; }
}
@media (orientation: portrait) {
  .system-controls, .right-controls { position: relative; top: -14px; }
  .system-controls .sprite-button { overflow: hidden; }
  .system-controls .sprite { margin: 9px 0 0 16px; transform: scale(.315); }
  .action-button .sprite { margin: 11px 0 0 11px; transform: scale(.259); }
  .combo .sprite { margin: 0; transform: scale(.2765); }
}
@media (max-width: 380px) and (orientation: portrait) {
  .action-button .sprite { margin: 10px 0 0 10px; transform: scale(.231); }
  .combo .sprite { margin: 0; transform: scale(.2555); }
}
@media (max-width: 340px) and (orientation: portrait) {
  .action-button .sprite { margin: 9.5px 0 0 9.5px; transform: scale(.21); }
  .combo .sprite { margin: 0; transform: scale(.2338); }
}
@media (orientation: landscape) and (max-height: 500px) {
  body { flex-direction: row; align-items: center; justify-content: center; padding: 6px; }
  .console { width: min(980px, 100%); margin: 0 auto; padding: 39px 6px 6px; border-width: 5px; border-radius: 18px; }
  .top-menu { top: 4px; right: 8px; }
  .panel { display: flex; flex-wrap: nowrap; align-items: center; height: calc(100dvh - 68px); max-height: 330px; min-height: 0; padding: 4px 8px; gap: 4px; }
  .left-controls, .right-controls { flex: 0 0 122px; padding: 0; }
  .left-controls { width: 122px; height: auto; align-items: center; justify-content: flex-start; }
  .dpad { transform: scale(.75); transform-origin: left center; }
  .right-controls { width: 162px; height: 184px; align-self: center; align-content: start; transform: scale(.74); transform-origin: right center; }
  .center-area { order: 0; flex: 1 1 auto; gap: 2px; }
  .screen-frame { width: min(100%, calc((100dvh - 120px) * 4 / 3)); border-width: 10px; border-radius: 15px; }
  .system-controls { min-height: 40px; height: 40px; transform: scale(.72); }
}
