:root {
  --bg: #f3f0e8;
  --surface: #fffdf8;
  --surface-2: #ece7dc;
  --text: #22201c;
  --muted: #6b655b;
  --border: #d9d1c1;
  --accent: #5a3fcf;
  --accent-ink: #ffffff;
  --accent-soft: #e8e3fb;
  --bezel: #2a2833;
  --screen-bg: #0d0d11;
  --badge-ink: #ffffff;
  --badge-gb: #66773a;
  --badge-gbc: #5a3fcf;
  --badge-gba: #2c5db0;
  --danger: #b3261e;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 10px 28px rgb(40 30 10 / 0.08);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141318;
    --surface: #1c1b22;
    --surface-2: #26252e;
    --text: #ecebf2;
    --muted: #a39faf;
    --border: #34323e;
    --accent: #a08cff;
    --accent-ink: #15131c;
    --accent-soft: #2b2545;
    --bezel: #0b0b0f;
    --screen-bg: #000000;
    --badge-ink: #121117;
    --badge-gb: #a9bb72;
    --badge-gbc: #a08cff;
    --badge-gba: #7fa9f2;
    --danger: #f2817a;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 10px 28px rgb(0 0 0 / 0.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }

.icon {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* header */

.top {
  max-width: 1200px;
  margin: 0 auto;
  padding: max(20px, env(safe-area-inset-top)) 16px 10px;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 4px 16px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  text-decoration: none;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.01em;
}

.brand-mark {
  position: relative;
  width: 17px;
  height: 23px;
  border-radius: 3px 3px 9px 3px;
  background: var(--accent);
  align-self: center;
}
.brand-mark::after {
  content: "";
  position: absolute;
  inset: 3px 3px 10px;
  border-radius: 1px;
  background: var(--surface);
}

.subtitle { margin: 0; color: var(--muted); }

/* layout */

.layout {
  max-width: 1200px;
  margin: 0 auto;
  padding: 6px 16px 24px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 24px;
  align-items: start;
}

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
}

.player { min-width: 0; }

/* the console: screen plus on-screen buttons */

.console {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  padding: 16px;
  border-radius: 18px 18px 44px 18px;
  background: var(--bezel);
  box-shadow: var(--shadow);
}
.console:focus { outline: none; }

.screen-wrap {
  position: relative;
  width: 100%;
  height: 432px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 6px;
  background: var(--screen-bg);
}

#screen {
  display: block;
  width: 480px;
  height: 432px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.gba-host { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.gba-host iframe { display: block; border: 0; background: #000; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgb(10 10 14 / 0.74);
  backdrop-filter: blur(2px);
}

.overlay-card { max-width: 36ch; text-align: center; color: #f1eff7; }
.overlay-text { margin: 0 0 14px; font-size: 15.5px; }
.overlay-text strong { display: block; font-size: 19px; margin-bottom: 2px; }

.overlay.loading .overlay-text::after {
  content: "";
  display: block;
  width: 26px;
  height: 26px;
  margin: 14px auto 0;
  border: 3px solid rgb(255 255 255 / 0.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* buttons */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.button:hover { border-color: var(--accent); }
.button.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.button.primary:hover { filter: brightness(1.08); }
.overlay .button.primary { background: #a08cff; color: #15131c; }

/* on-screen buttons, shown on touch screens */

.touchpad {
  display: none;
  width: 100%;
  max-width: 420px;
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "dpad ab" "meta meta";
  gap: 16px 8px;
  padding: 0 4px 4px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
body.touch .touchpad { display: grid; }

.dpad { grid-area: dpad; position: relative; width: 138px; height: 138px; justify-self: start; }
.dpad-arm, .dpad-hub { position: absolute; background: #4a4857; }
.dpad-arm.up { left: 46px; top: 0; width: 46px; height: 50px; border-radius: 7px 7px 0 0; }
.dpad-arm.down { left: 46px; bottom: 0; width: 46px; height: 50px; border-radius: 0 0 7px 7px; }
.dpad-arm.left { left: 0; top: 46px; width: 50px; height: 46px; border-radius: 7px 0 0 7px; }
.dpad-arm.right { right: 0; top: 46px; width: 50px; height: 46px; border-radius: 0 7px 7px 0; }
.dpad-hub { left: 46px; top: 46px; width: 46px; height: 46px; }

.ab { grid-area: ab; position: relative; width: 150px; height: 124px; justify-self: end; }
.round {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 62px;
  height: 62px;
  border-radius: 50%;
  background: #9a2f5e;
  color: rgb(255 255 255 / 0.85);
  font-weight: 700;
}
.round.b { left: 0; bottom: 6px; }
.round.a { right: 0; top: 6px; }

.meta { grid-area: meta; display: flex; justify-content: center; gap: 22px; }
.pill {
  padding: 7px 18px;
  border-radius: 999px;
  background: #4a4857;
  color: rgb(255 255 255 / 0.8);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.touchpad .pressed { background: var(--accent); color: #fff; }

/* GBA games bring EmulatorJS's own on-screen pad and menu bar */
body.gba .touchpad,
body.gba .toolbar [data-gb],
body.gba .toolbar .volume,
body.gba .toolbar .tool-sep { display: none; }

/* now playing + toolbar */

.nowplaying {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 28px;
  margin: 14px 2px 8px;
}
.now-title {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 17px;
  font-weight: 650;
}
.save-status { margin-left: auto; flex: none; color: var(--muted); font-size: 13px; }

.badge {
  flex: none;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--badge-gbc);
  color: var(--badge-ink);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
}
.badge.gb { background: var(--badge-gb); }
.badge.gba { background: var(--badge-gba); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  padding: 5px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}

.tool {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.tool:hover:not(:disabled) { background: var(--surface-2); }
.tool[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.tool:disabled { opacity: 0.38; cursor: default; }
#btn-full { margin-left: auto; }

.tool-sep { width: 1px; height: 22px; margin: 0 2px; background: var(--border); }

.volume { display: inline-flex; align-items: center; }
.volume input { width: 90px; accent-color: var(--accent); }

.gba-note { margin: 8px 2px 0; color: var(--muted); font-size: 13.5px; }

@media (max-width: 600px) {
  .tool-label { display: none; }
  .tool { padding: 0 9px; }
  .volume input { width: 64px; }
  .tool-sep { display: none; }
}

@media (max-width: 440px) {
  .tool { padding: 0 7px; }
  .volume input { display: none; }
}

/* more menu (popover) */

.menu {
  position: fixed;
  inset: auto;
  margin: 0;
  min-width: 270px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.menu hr { margin: 6px 4px; border: 0; border-top: 1px solid var(--border); }

.menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover:not(:disabled) { background: var(--surface-2); }
.menu-item:disabled { opacity: 0.4; cursor: default; }

.menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 10px;
  font-size: 14px;
}
.menu-row.disabled { opacity: 0.4; }
.menu-row select {
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
.menu-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.stepper { display: inline-flex; align-items: center; gap: 6px; }
.step {
  width: 28px;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: inherit;
  font: inherit;
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
}
.step:disabled { opacity: 0.4; cursor: default; }
.step-value { min-width: 3ch; text-align: center; font-variant-numeric: tabular-nums; }

/* game shelf */

.shelf {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.shelf-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.shelf-head h2 { margin: 0; font-size: 17px; }

.library { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }

.game {
  display: grid;
  grid-template-columns: 84px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 8px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: var(--surface-2);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.game:hover { border-color: var(--accent); }
.game[aria-current="true"] { border-color: var(--accent); background: var(--accent-soft); }

.cover {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 84px;
  aspect-ratio: 10 / 9;
  border-radius: 6px;
  background: var(--bezel);
  color: rgb(255 255 255 / 0.55);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  object-fit: cover;
}
.cover.gba { aspect-ratio: 3 / 2; }

.game-title { display: flex; align-items: center; gap: 8px; font-weight: 650; }
.game-title span:first-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.game-desc {
  display: -webkit-box;
  margin-top: 2px;
  overflow: hidden;
  color: var(--muted);
  font-size: 13.5px;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.empty-lib { color: var(--muted); font-size: 14px; }

.hint { margin: 14px 0 0; color: var(--muted); font-size: 13.5px; }
code {
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--surface-2);
  font: 0.9em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.keys { margin-top: 14px; padding-top: 10px; border-top: 1px solid var(--border); }
.keys summary { cursor: pointer; font-weight: 600; }
.keys table { width: 100%; margin-top: 8px; border-collapse: collapse; font-size: 14px; }
.keys th {
  padding: 4px 10px 4px 0;
  color: var(--muted);
  font-weight: 500;
  text-align: left;
  vertical-align: top;
  white-space: nowrap;
}
.keys td { padding: 4px 0; }
.keys p { color: var(--muted); font-size: 13.5px; }

kbd {
  display: inline-block;
  margin: 1px 3px 1px 0;
  padding: 3px 6px;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface-2);
  font: 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.foot {
  max-width: 1200px;
  margin: 0 auto;
  padding: 4px 16px max(32px, env(safe-area-inset-bottom));
  color: var(--muted);
  font-size: 13px;
}

/* drag and drop, toast */

.drop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgb(20 18 30 / 0.55);
  pointer-events: none;
}
.drop-card {
  padding: 28px 44px;
  border: 2px dashed #fff;
  border-radius: 18px;
  color: #fff;
  font-size: 22px;
  font-weight: 700;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 60;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 16px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* fullscreen */

#console:fullscreen {
  width: 100vw;
  height: 100vh;
  justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  border-radius: 0;
  background: #000;
}
#console:fullscreen .screen-wrap { background: #000; }
#console::backdrop { background: #000; }
