:root {
  --brand: #8b74e8;
  --brand-hover: #a18fff;
  --brand-pressed: #6043c8;
  --brand-hot: #9205f2;
  --bg: #0d1117;
  --bg-2: #161b22;
  --bg-3: #1c2129;
  --line: rgba(240, 246, 252, 0.1);
  --text: #e6edf3;
  --muted: #8b949e;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", Roboto, system-ui, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
body { padding-top: var(--safe-top); }
img { display: block; max-width: 100%; }
button, input { font: inherit; color: inherit; background: none; border: 0; }
[hidden] { display: none !important; }
a { color: inherit; text-decoration: none; }

.topbar {
  position: sticky;
  top: var(--safe-top);
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 10px 14px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(18px);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}
.logo {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: #f3f4f6;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  padding: 7px;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--brand) 45%, transparent);
}
.logo i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #333;
}
.logo i:nth-child(1), .logo i:nth-child(2) { background: #f0f0f0; box-shadow: inset 0 0 0 1px #bbb; }
.logo i:nth-child(3), .logo i:nth-child(4) { background: #8a8a8a; }
.brand-text { text-align: left; }
.brand-text strong {
  display: block;
  font-size: 16px;
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.brand-text small {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}
.search {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-2);
  color: var(--muted);
}
.search:focus-within {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 22%, transparent);
}
.search input {
  width: 100%;
  outline: none;
  color: var(--text);
}

.plat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  padding: 4px 16px calc(28px + var(--safe-bottom));
}
@media (max-width: 600px) {
  .plat-grid { grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: 10px; }
}
.plat-tile {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px 16px 18px;
  position: relative;
  transition: background .18s ease, border-color .18s ease, transform .18s ease;
}
.plat-tile:active { transform: scale(.97); }
.plat-tile:hover, .plat-tile:focus-visible {
  background: var(--bg-3);
  border-color: color-mix(in srgb, var(--brand) 55%, var(--line));
  box-shadow: 0 8px 28px rgba(0,0,0,.35), 0 0 0 1px color-mix(in srgb, var(--brand) 40%, transparent);
}
.plat-tile__icon {
  width: 72px;
  height: 72px;
  display: grid;
  place-items: center;
  opacity: .95;
  transition: transform .18s ease;
}
.plat-tile:hover .plat-tile__icon { transform: scale(1.05); }
.plat-tile__icon img { width: 72px; height: 72px; object-fit: contain; }
.plat-tile__name {
  font-size: 12px;
  font-weight: 650;
  color: var(--text);
  text-align: center;
  line-height: 1.35;
}
.plat-tile__count { font-size: 11px; }
@media (max-width: 600px) {
  .plat-tile { gap: 6px; padding: 12px 8px 10px; }
  .plat-tile__icon, .plat-tile__icon img { width: 52px; height: 52px; }
  .plat-tile__name { font-size: 10px; }
  .plat-tile__count { font-size: 9px; }
}
.game-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px 8px;
}
.toolbar-icon { width: 36px; height: 36px; object-fit: contain; }
.pad-status {
  position: absolute;
  left: 10px;
  bottom: calc(8px + var(--safe-bottom));
  z-index: 25;
  font-size: 11px;
  color: #c9d1d9;
  background: rgba(13,17,23,.72);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 10px;
  max-width: calc(100% - 20px);
  pointer-events: none;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 10px 16px 8px;
}
.toolbar h2 { font-size: 18px; font-weight: 700; letter-spacing: -0.03em; }
.toolbar span { color: var(--muted); font-size: 12px; }

.gallery {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  padding: 0 12px calc(28px + var(--safe-bottom));
}
@media (min-width: 720px) { .gallery { grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 14px; } }
@media (min-width: 1100px) { .gallery { grid-template-columns: repeat(7, minmax(0, 1fr)); } }

.rom-card {
  cursor: pointer;
  animation: fadeUp .45s ease both;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(12px) scale(.98); }
  to { opacity: 1; transform: none; }
}
.rom-cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  background: var(--bg-3);
  box-shadow: var(--shadow);
}
.rom-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transform: scale(1.04);
  transition: opacity .4s ease, transform .55s ease;
}
.rom-cover img.on {
  opacity: 1;
  transform: scale(1);
}
.rom-play {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, transparent 40%, rgba(13, 17, 23, .72));
  opacity: 0;
  transition: opacity .2s ease;
}
.rom-play span {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand);
  box-shadow: 0 8px 24px rgba(139, 116, 232, .45);
  transform: scale(.86);
  transition: transform .2s ease;
}
.rom-card:active .rom-play, .rom-card.open .rom-play { opacity: 1; }
.rom-card:active .rom-play span { transform: scale(1); }
.rom-meta { padding: 7px 2px 2px; }
.rom-meta h3 {
  font-size: 12px;
  font-weight: 650;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 30px;
}
.rom-meta small { color: var(--muted); font-size: 11px; }

.ph {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  color: #667;
  font-weight: 800;
  letter-spacing: .08em;
  background: linear-gradient(160deg, #242a33, #161b22);
}

.skeletons { display: contents; }
.sk {
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  background: linear-gradient(90deg, #161b22 25%, #242b36 50%, #161b22 75%);
  background-size: 200% 100%;
  animation: shine 1.1s linear infinite;
}
@keyframes shine { to { background-position: -200% 0; } }

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(1, 4, 9, .62);
  z-index: 50;
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s ease;
}
.backdrop.show { opacity: 1; pointer-events: auto; }
.rom-page {
  position: fixed;
  inset: 0;
  z-index: 51;
  background: var(--bg);
  overflow: auto;
  transform: translateX(104%);
  transition: transform .32s cubic-bezier(.2, .8, .2, 1);
  padding: calc(12px + var(--safe-top)) 16px calc(24px + var(--safe-bottom));
}
.rom-page.show { transform: none; }
.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  background: var(--bg-2);
  border: 1px solid var(--line);
  font-size: 20px;
}
.rom-hero {
  display: grid;
  gap: 16px;
  margin-top: 12px;
}
@media (min-width: 720px) {
  .rom-hero { grid-template-columns: 240px 1fr; align-items: start; }
}
.rom-hero img, .rom-hero .ph {
  width: min(220px, 58vw);
  height: auto;
  aspect-ratio: 2 / 3;
  border-radius: 8px;
  object-fit: cover;
  box-shadow: var(--shadow);
  animation: fadeUp .45s ease both;
}
.rom-hero h2 {
  font-size: 28px;
  letter-spacing: -0.04em;
  line-height: 1.1;
  margin: 6px 0 10px;
}
.facts { display: flex; flex-wrap: wrap; gap: 8px; }
.fact {
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
  background: var(--bg-2);
  border-radius: 999px;
  padding: 5px 10px;
}
.summary {
  color: #c9d1d9;
  font-size: 15px;
  line-height: 1.6;
  margin-top: 8px;
}
.play-btn {
  margin-top: 18px;
  height: 48px;
  width: 100%;
  max-width: 320px;
  border-radius: 8px;
  font-weight: 800;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #fff;
  background: linear-gradient(180deg, var(--brand-hover), var(--brand));
  box-shadow: 0 8px 24px rgba(139, 116, 232, .35);
  position: relative;
  overflow: hidden;
}
.play-btn::after {
  content: "";
  position: absolute;
  inset: 0 auto 0 -40%;
  width: 40%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,.28), transparent);
  animation: sweep 2.4s ease infinite;
}
@keyframes sweep { to { transform: translateX(420%); } }
.play-btn:active { background: var(--brand-pressed); transform: scale(.98); }

.empty {
  grid-column: 1 / -1;
  text-align: center;
  color: var(--muted);
  padding: 48px 12px;
}

.player-body {
  height: 100dvh;
  background: #000;
  overflow: hidden;
  touch-action: none;
}
.player-top {
  position: absolute;
  top: 0; left: 0; right: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(8px + var(--safe-top)) 10px 10px;
  background: linear-gradient(180deg, rgba(13,17,23,.85), transparent);
  pointer-events: none;
}
.player-top .icon-btn { pointer-events: auto; }
.player-top strong { font-size: 14px; }
.player-top span { display: block; font-size: 11px; color: var(--muted); }
#game { width: 100%; height: 100dvh; background: #000; outline: none; }
.boot {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: var(--bg);
  text-align: center;
  padding: 24px;
}
.boot img {
  width: 148px;
  height: 222px;
  object-fit: cover;
  border-radius: 8px;
  margin: 0 auto 16px;
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 40%, transparent), var(--shadow);
  animation: pulseCover 1.6s ease-in-out infinite;
}
@keyframes pulseCover {
  50% { transform: scale(1.03); box-shadow: 0 0 0 6px color-mix(in srgb, var(--brand) 28%, transparent), var(--shadow); }
}
.boot h2 { font-size: 20px; margin-bottom: 6px; }
.boot p { color: var(--muted); }

.ejs_virtualGamepad_parent { z-index: 12 !important; }
.ejs_virtualGamepad_left,
.ejs_virtualGamepad_right {
  width: 168px !important;
  height: 168px !important;
  bottom: 78px !important;
}
.ejs_virtualGamepad_button {
  width: 58px !important;
  height: 58px !important;
  line-height: 58px !important;
  font-size: 15px !important;
  border: 0 !important;
  color: #fff !important;
  text-shadow: 0 1px 2px rgba(0,0,0,.45);
  box-shadow: 0 6px 16px rgba(0,0,0,.35), inset 0 1px 0 rgba(255,255,255,.25);
  backdrop-filter: blur(4px);
}
.ejs_virtualGamepad_button_down { transform: scale(.92); filter: brightness(.8); }
.ejs_dpad_main { opacity: .92 !important; }
.ejs_dpad_horizontal, .ejs_dpad_vertical {
  background: rgba(22, 27, 34, .82) !important;
  box-shadow: inset 0 0 0 1px rgba(139,116,232,.35);
}
.ejs_virtualGamepad_bottom .ejs_virtualGamepad_button {
  width: 54px !important;
  height: 28px !important;
  line-height: 28px !important;
  border-radius: 8px !important;
  font-size: 10px !important;
  background: rgba(22,27,34,.8) !important;
}
.btn-A { background: #e60012 !important; }
.btn-B { background: #ffc107 !important; color: #111 !important; }
.btn-X { background: #4d73d6 !important; }
.btn-Y { background: #3dbb5a !important; }
.btn-L, .btn-R, .btn-L1, .btn-R1, .btn-ZL, .btn-ZR, .btn-Z {
  background: #2b3340 !important;
  border-radius: 10px !important;
}
.btn-C, .btn-CUP, .btn-CDOWN, .btn-CLEFT, .btn-CRIGHT { background: #f0c400 !important; color: #111 !important; }
.btn-△, .btn-TRI { background: #2ecc71 !important; }
.btn-○, .btn-CIRCLE { background: #e74c3c !important; }
.btn-✕, .btn-XPS { background: #3498db !important; }
.btn-□, .btn-SQUARE { background: #ff79c6 !important; }
.pad-psx .btn-X { background: #3498db !important; }
.pad-n64 .btn-A { background: #1e90ff !important; }
.pad-n64 .btn-B { background: #2ecc71 !important; color: #fff !important; }
.pad-neogeomvs .btn-A { background: #e60012 !important; }
.pad-neogeomvs .btn-B { background: #ffc107 !important; color: #111 !important; }
.pad-neogeomvs .btn-C { background: #3dbb5a !important; }
.pad-neogeomvs .btn-D { background: #4d73d6 !important; }

.device-tag {
  margin-left: auto;
  flex: 0 0 auto;
  font-size: 10px;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px 8px;
  white-space: nowrap;
}

body.device-pc .ejs_virtualGamepad_parent,
body.device-tv .ejs_virtualGamepad_parent {
  display: none !important;
}

body.device-tablet .ejs_virtualGamepad_left,
body.device-tablet .ejs_virtualGamepad_right {
  width: 210px !important;
  height: 210px !important;
  bottom: 88px !important;
}
body.device-tablet .ejs_virtualGamepad_button {
  width: 68px !important;
  height: 68px !important;
  line-height: 68px !important;
  font-size: 16px !important;
}

body.device-tv .plat-tile:focus,
body.device-tv .rom-card:focus,
body.device-tv .play-btn:focus,
body.device-tv .icon-btn:focus {
  outline: none;
  transform: scale(1.06);
  border-color: var(--brand);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 72%, transparent), 0 10px 28px rgba(0,0,0,.4);
}
body.device-tv .rom-card:focus .rom-play { opacity: 1; }
body.device-tv .search { max-width: 220px; opacity: .55; }
