:root {
  color-scheme: dark;
  --bg: #0b0d12;
  --surface: #161b26;
  --surface-2: #1d2432;
  --border: #262e3f;
  --fg: #e9edf5;
  --muted: #97a2b8;
  --faint: #5d6880;
  --accent: #ffb648;
  --ok: #3ddc84;
  --warn: #ffd166;
  --err: #ff6b6b;
  --radius: 16px;
  --radius-sm: 10px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
}

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

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 28px clamp(16px, 4vw, 40px) 72px;
}

/* ---- chrome ---- */
.hero {
  margin-bottom: 28px;
}

.wordmark {
  margin: 0;
  font-size: clamp(28px, 4vw, 40px);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.wordmark::before {
  content: "";
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  margin-right: 0.3em;
  border-radius: 0.18em;
  background: linear-gradient(135deg, var(--accent), #ff7a45);
  vertical-align: 0.02em;
}

.tagline {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 17px;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.topbar h1 {
  flex: 1 1 200px;
  margin: 0;
  font-size: clamp(24px, 3vw, 32px);
  letter-spacing: -0.01em;
}

.page-sub {
  margin: -10px 0 18px;
  color: var(--muted);
}

.section-title {
  margin: 28px 0 12px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--faint);
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}

.btn:hover {
  border-color: var(--faint);
}

.btn:disabled {
  opacity: 0.6;
  cursor: progress;
}

.btn-link {
  border-color: transparent;
  background: none;
  color: var(--accent);
  padding-inline: 6px;
}

/* ---- consoles ---- */
.console-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}

.console {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 14px;
  align-items: center;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(150deg, var(--surface-2), var(--surface));
  color: inherit;
  text-decoration: none;
  overflow: hidden;
  transition: transform 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

a.console:hover,
a.console:focus-visible {
  transform: translateY(-2px);
  border-color: var(--accent);
  box-shadow: 0 18px 34px -24px rgba(0, 0, 0, 0.9);
}

.console-logo {
  display: grid;
  place-items: center;
  min-width: 64px;
  height: 64px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--accent);
  font: 700 18px/1 var(--mono);
  letter-spacing: 0.04em;
}

.console-name {
  font-weight: 650;
}

.console-meta {
  margin-top: 2px;
  color: var(--muted);
  font-size: 13px;
}


.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: fit-content;
  margin-top: 10px;
  padding: 2px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 650;
  color: var(--muted);
}

.pill::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: currentColor;
}

.pill-ok {
  color: var(--ok);
  border-color: color-mix(in srgb, var(--ok) 45%, transparent);
}

.pill-warn {
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 45%, transparent);
}

.pill-err {
  color: var(--err);
  border-color: color-mix(in srgb, var(--err) 45%, transparent);
}

/* ---- games ---- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin: 18px 0;
}

.input,
.select {
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--fg);
  font: inherit;
  font-size: 15px;
}

.input {
  flex: 1 1 240px;
  min-width: 0;
}

.chip {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.chip[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent);
}

.result-count {
  color: var(--faint);
  font-size: 13px;
  margin-left: auto;
}

.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
}


/* A playable card is a wrapper: the anchor stays the whole card's click
   target (via its stretched ::after), with the share ↗ above it. */
.game-wrap {
  position: relative;
}

.game-wrap > .game::after {
  content: '';
  position: absolute;
  inset: 0;
}

.game-share {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.game-share:hover,
.game-share:focus-visible {
  color: var(--fg);
  border-color: var(--accent);
}

.game {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 112px;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: inherit;
  text-decoration: none;
  transition: border-color 0.15s ease, background 0.15s ease;
}

a.game:hover,
a.game:focus-visible {
  border-color: var(--accent);
  background: var(--surface-2);
}

.game-title {
  font-weight: 650;
  line-height: 1.3;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* Keep two-line titles clear of the ↗ in the corner. */
.game-wrap .game-title {
  padding-right: 40px;
}

.game-meta {
  margin-top: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  color: var(--muted);
  font-size: 13px;
}

.game-size {
  font-family: var(--mono);
  font-size: 12px;
}

.game.unsupported {
  opacity: 0.7;
}

.game-note {
  color: var(--warn);
  font-size: 13px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.game-folder {
  color: var(--faint);
  font: 12px var(--mono);
  overflow-wrap: anywhere;
}

.game.unsupported .game-note {
  color: var(--muted);
}

.empty {
  padding: 48px 0;
  color: var(--muted);
  text-align: center;
}

/* ---- player ---- */
.player-page {
  position: fixed;
  inset: 0;
  /* The dynamic viewport: on a phone the game ends where the browser's
     toolbars begin, and grows again when they hide — instead of sliding
     underneath them. */
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  background: var(--bg);
}

/* Landscape fullscreen is all game: the bar goes away and the stage gets
   every pixel of the screen. */
.player-page:fullscreen {
  grid-template-rows: minmax(0, 1fr);
}

.player-page:fullscreen .player-bar {
  display: none;
}

.player-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px clamp(12px, 2vw, 20px);
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.player-bar h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 16px;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-bar .hint {
  display: none;
  color: var(--faint);
  font-size: 13px;
}

@media (min-width: 900px) {
  .player-bar .hint {
    display: block;
  }
}

/* The save status shows whenever it has something to say, whatever the width. */
.player-bar .save-state:not(:empty) {
  display: block;
  color: var(--ok);
  white-space: nowrap;
}

.player-bar .save-state.failed {
  color: var(--err);
}

.player-host {
  position: relative;
  min-height: 0;
}

.player-stage {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

.player-message {
  position: absolute;
  inset: 0;
  background: var(--bg);
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 14px;
  padding: 24px;
  text-align: center;
  color: var(--muted);
}

.player-message h2 {
  margin: 0;
  color: var(--fg);
}

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.notice {
  max-width: 520px;
  padding: 14px 16px;
  border: 1px solid var(--err);
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--err) 12%, transparent);
  overflow-wrap: anywhere;
}

/* ---- footer ---- */
.footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  margin-top: 40px;
  padding-top: 18px;
  border-top: 1px solid var(--border);
  color: var(--faint);
  font-size: 13px;
}

.footer .path {
  font-family: var(--mono);
  overflow-wrap: anywhere;
}

.footer-actions {
  margin-left: auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}


/* The NES canvas keeps its pixel size; the stage stretches it. */
.player-stage canvas {
  max-width: 100%;
  max-height: 100%;
}

/* ---- NES on-screen gamepad (present only on coarse-pointer screens) ---- */
.nes-pad {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 5;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: 14px max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  pointer-events: none;
  touch-action: none;
}

.nes-dpad,
.nes-meta,
.nes-actions {
  pointer-events: auto;
}

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

.nes-dpad .nes-btn:nth-child(1) { grid-area: 1 / 2; }
.nes-dpad .nes-btn:nth-child(2) { grid-area: 2 / 1; }
.nes-dpad .nes-btn:nth-child(3) { grid-area: 2 / 3; }
.nes-dpad .nes-btn:nth-child(4) { grid-area: 3 / 2; }

.nes-meta {
  display: flex;
  gap: 18px;
  align-self: flex-end;
  padding-bottom: 10px;
}

.nes-actions {
  display: flex;
  align-items: flex-end;
  gap: 18px;
}

/* Portrait is too narrow for one row of everything: Select/Start move to a
   centered row of their own, D-pad and A/B share the bottom row. */
@media (orientation: portrait) {
  .nes-pad {
    display: grid;
    grid-template:
      'meta meta' auto
      'dpad actions' auto / auto auto;
    justify-content: space-between;
    align-items: end;
    row-gap: 20px;
  }

  .nes-meta {
    grid-area: meta;
    justify-self: center;
    padding-bottom: 0;
  }

  .nes-dpad { grid-area: dpad; }
  .nes-actions { grid-area: actions; }
}

.nes-btn {
  min-width: 54px;
  min-height: 54px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: color-mix(in srgb, var(--surface) 72%, transparent);
  color: var(--fg);
  font: 650 15px/1 var(--font);
  letter-spacing: 0.04em;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.05s;
}

.nes-actions .nes-btn {
  position: relative;
  width: min(78px, 20vh);
  height: min(78px, 20vh);
  border-radius: 50%;
  font-size: 19px;
}

/* The angled NES pair: B low, A raised, so the thumb rolls between them
   instead of hunting two circles on one line. */
.nes-actions .nes-btn:last-child {
  margin-bottom: 26px;
}

/* Six pixels of invisible finger room around each of B and A — near-misses
   still land, and the release (pointerleave) tolerates a small slide. */
.nes-actions .nes-btn::after {
  content: '';
  position: absolute;
  inset: -6px;
  border-radius: 50%;
}

.nes-meta .nes-btn {
  min-width: 74px;
  min-height: 34px;
  border-radius: 17px;
  font-size: 11px;
  color: var(--muted);
}

.nes-btn.held {
  background: color-mix(in srgb, var(--accent) 38%, transparent);
  transform: scale(0.94);
}

/* ---- auth pages ---- */
.auth-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.auth-card {
  width: min(400px, 100%);
  display: grid;
  gap: 14px;
  padding: 30px 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.auth-card .wordmark {
  margin: 0;
  font-size: 22px;
}

.auth-sub,
.auth-foot {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.auth-card form {
  display: grid;
  gap: 10px;
}

.auth-card .input {
  flex: none;
  width: 100%;
}

.auth-card .btn {
  justify-content: center;
  padding-block: 11px;
}

.auth-error {
  min-height: 1.4em;
  margin: 0;
  color: var(--err);
  font-size: 14px;
}

.auth-code {
  font-family: var(--mono);
  letter-spacing: 3px;
  text-align: center;
  text-transform: uppercase;
}

/* ---- admin page ---- */
.admin-list {
  display: grid;
  gap: 10px;
  margin: 0 0 24px;
  padding: 0;
  list-style: none;
}

.admin-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.admin-row .name {
  font-weight: 650;
}

.admin-row .share-token {
  font-family: var(--mono);
}

.admin-row .meta {
  color: var(--muted);
  font-size: 13px;
}

.admin-row .btn {
  margin-left: auto;
}

.admin-row .btn + .btn {
  margin-left: 0;
}

.invite-result {
  display: grid;
  gap: 8px;
  margin: 0 0 24px;
  padding: 14px 16px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
}

.invite-result .code {
  font-family: var(--mono);
  font-size: 20px;
  letter-spacing: 3px;
}

.invite-result .link {
  overflow-wrap: anywhere;
  font-size: 13px;
}
