* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  background: #111;
  color: #eee;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  min-height: 100vh;
}

.app {
  min-height: 100vh;
  padding: 48px 24px 28px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.app__main {
  flex: 1;
  width: 100%;
}

/* ── Library ─────────────────────────────────────────────── */

.library {
  max-width: 1120px;
  margin: 0 auto;
}

.library__header {
  max-width: 760px;
  margin: 0 0 28px;
}

.library__header h1 {
  margin: 0;
  font-size: 32px;
  line-height: 1.15;
  font-weight: 700;
}

.library__header p {
  margin: 10px 0 0;
  color: #b8c5d6;
  font-size: 16px;
  line-height: 1.55;
}

.library .grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 24px;
}

.game-card {
  display: block;
  background: #1b1b1f;
  border: 1px solid #2a2a30;
  border-radius: 8px;
  padding: 16px;
  cursor: pointer;
  color: inherit;
  font: inherit;
  text-align: left;
  text-decoration: none;
  transition: background-color 120ms, border-color 120ms;
}

.game-card:hover {
  background: #202026;
  border-color: #555;
}

.game-thumb {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 4 / 3;
  object-fit: cover;
  border-radius: 6px;
  background: #000;
  image-rendering: pixelated;
}

.game-card__title {
  margin-top: 12px;
  font-size: 18px;
  font-weight: 600;
}

.game-card__meta {
  margin-top: 4px;
  font-size: 13px;
  opacity: 0.55;
}

.game-card__description {
  margin: 10px 0 0;
  color: #b9b9c1;
  font-size: 13px;
  line-height: 1.45;
}

/* ── Game page ───────────────────────────────────────────── */

.game-page {
  max-width: 800px;
  margin: 0 auto;
}

.back {
  display: inline-block;
  background: transparent;
  border: 1px solid #333;
  color: #ccc;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  text-decoration: none;
}

.back:hover {
  border-color: #666;
  color: #fff;
}

.game-title {
  margin: 16px 0 20px;
  font-size: 24px;
  font-weight: 600;
}

.game-byline {
  margin-top: -10px;
  color: #9da8b8;
  font-size: 14px;
}

.game-description {
  margin: 10px 0 20px;
  color: #d0d3da;
  font-size: 16px;
  line-height: 1.55;
}

.game-screen-wrap {
  --game-aspect-ratio: 4 / 3;
  --game-aspect-width: 4;
  --game-aspect-height: 3;
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: var(--game-aspect-ratio);
}

.game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  aspect-ratio: var(--game-aspect-ratio);
  image-rendering: pixelated;
  outline: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.game-status {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #aaa;
  font-size: 14px;
  pointer-events: none;
  background: rgba(0, 0, 0, 0.5);
}

.game-status.hidden {
  display: none;
}

.game-perf {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 2;
  margin: 0;
  padding: 8px 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.72);
  color: #f3f5f8;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  pointer-events: none;
  white-space: pre;
}

.game-perf.hidden {
  display: none;
}

.mobile-controls {
  display: none;
}

.mobile-rotate-hint {
  display: none;
}

/* Static fallback shown before the WebAssembly app mounts. */

.seo-fallback {
  max-width: 760px;
  margin: 0 auto;
  padding: 48px 24px;
  line-height: 1.55;
}

.seo-fallback h1 {
  margin: 0 0 12px;
  font-size: 32px;
  line-height: 1.15;
}

.seo-fallback p {
  margin: 0 0 20px;
  color: #b8c5d6;
}

.seo-fallback ul {
  margin: 0;
  padding-left: 20px;
}

.seo-fallback li {
  margin: 8px 0;
}

.seo-fallback li span {
  display: block;
  color: #b9b9c1;
  font-size: 14px;
}

.seo-fallback a {
  color: #c6d7ff;
}

.seo-fallback img {
  display: block;
  width: min(100%, 640px);
  margin: 20px 0 0;
  image-rendering: pixelated;
  border-radius: 8px;
}

/* Site footer */

.site-footer {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
  color: #8d8d94;
  font-size: 12px;
  line-height: 1.5;
  text-align: center;
}

.powered-by {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 6px 10px;
}

.powered-by__label,
.powered-by__separator {
  opacity: 0.72;
}

.build-crate {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  white-space: nowrap;
}

.build-crate__name {
  color: #d7d7dc;
  font-weight: 600;
  text-decoration: none;
}

.build-crate__name:hover {
  color: #fff;
  text-decoration: underline;
}

.build-crate__version {
  color: #a9a9b0;
}

.build-crate__sha {
  color: #c6d7ff;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-decoration: none;
}

.build-crate__sha:hover {
  color: #fff;
  text-decoration: underline;
}

.build-crate__sha--unknown {
  color: #777;
}

.site-footer__disclaimer {
  max-width: 640px;
  margin: 8px auto 0;
  color: #76767d;
}

@media (max-width: 1120px) {
  .library { max-width: 840px; }
  .library .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 760px) {
  .library { max-width: 540px; }
  .library .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 520px) {
  .app { padding: 24px 16px; }
  .library__header h1 { font-size: 27px; }
  .library .grid { grid-template-columns: 1fr; }
  .site-footer { text-align: left; }
  .powered-by { justify-content: flex-start; }
  .site-footer__disclaimer { margin-left: 0; }
}

@media (hover: none) and (pointer: coarse) and (orientation: portrait) {
  .mobile-rotate-hint {
    position: absolute;
    left: 50%;
    bottom: 12px;
    z-index: 4;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: calc(100% - 24px);
    min-height: 34px;
    padding: 7px 12px;
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.68);
    color: rgba(255, 255, 255, 0.9);
    font-size: 13px;
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    pointer-events: none;
    transform: translateX(-50%);
  }

  .mobile-rotate-hint__icon {
    position: relative;
    display: inline-block;
    width: 15px;
    height: 22px;
    flex: 0 0 auto;
    border: 2px solid currentColor;
    border-radius: 4px;
    transform: rotate(-18deg);
    opacity: 0.92;
  }

  .mobile-rotate-hint__icon::after {
    content: "";
    position: absolute;
    left: 50%;
    bottom: 2px;
    width: 3px;
    height: 3px;
    border-radius: 999px;
    background: currentColor;
    transform: translateX(-50%);
  }
}

@media (hover: none) and (pointer: coarse) and (orientation: landscape),
  (orientation: landscape) and (max-height: 520px) {
  .app[data-view="playing"] {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    width: 100dvw;
    height: 100dvh;
    min-height: 0;
    padding: 0;
    gap: 0;
    overflow: hidden;
    overscroll-behavior: none;
    background: #000;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .app[data-view="playing"] .app__main,
  .app[data-view="playing"] .game-page {
    width: 100%;
    height: 100%;
    min-height: 0;
    overflow: hidden;
  }

  .app[data-view="playing"] .site-footer,
  .app[data-view="playing"] .back,
  .app[data-view="playing"] .game-title,
  .app[data-view="playing"] .game-byline,
  .app[data-view="playing"] .game-description {
    display: none;
  }

  .app[data-view="playing"] .game-page {
    max-width: none;
    margin: 0;
  }

  .app[data-view="playing"] .game-screen-wrap {
    position: fixed;
    inset: 0;
    z-index: 20;
    width: 100vw;
    height: 100vh;
    width: 100dvw;
    height: 100dvh;
    aspect-ratio: auto;
    border-radius: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: env(safe-area-inset-top) env(safe-area-inset-right)
      env(safe-area-inset-bottom) env(safe-area-inset-left);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .app[data-view="playing"] .game-canvas {
    flex: 0 0 auto;
    width: min(
      100vw,
      calc(100vh * var(--game-aspect-width) / var(--game-aspect-height))
    );
    height: min(
      100vh,
      calc(100vw * var(--game-aspect-height) / var(--game-aspect-width))
    );
    width: min(
      100dvw,
      calc(100dvh * var(--game-aspect-width) / var(--game-aspect-height))
    );
    height: min(
      100dvh,
      calc(100dvw * var(--game-aspect-height) / var(--game-aspect-width))
    );
    max-width: 100%;
    max-height: 100%;
  }

  .app[data-view="playing"] .game-perf {
    top: max(10px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
  }

  .app[data-view="playing"] .mobile-controls {
    position: fixed;
    inset: 0;
    z-index: 30;
    display: block;
    pointer-events: none;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .mobile-joystick {
    --stick-x: 0;
    --stick-y: 0;
    position: absolute;
    left: max(18px, env(safe-area-inset-left));
    bottom: max(18px, env(safe-area-inset-bottom));
    width: 118px;
    height: 118px;
    pointer-events: auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .mobile-joystick__ring {
    position: relative;
    width: 100%;
    height: 100%;
    border: 2px solid rgba(255, 255, 255, 0.32);
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.24);
    box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.45);
  }

  .mobile-joystick__thumb {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 48px;
    height: 48px;
    border-radius: 999px;
    background: rgba(246, 248, 252, 0.38);
    border: 1px solid rgba(255, 255, 255, 0.5);
    transform: translate(
      calc(-50% + var(--stick-x) * 35px),
      calc(-50% + var(--stick-y) * 35px)
    );
  }

  .mobile-joystick.is-active .mobile-joystick__ring,
  .mobile-button.is-active {
    border-color: rgba(255, 255, 255, 0.62);
    background: rgba(255, 255, 255, 0.2);
  }

  .mobile-buttons {
    position: absolute;
    right: max(18px, env(safe-area-inset-right));
    bottom: max(18px, env(safe-area-inset-bottom));
    display: grid;
    grid-template-columns: repeat(2, 58px);
    grid-auto-rows: 58px;
    gap: 12px;
    align-items: center;
    justify-items: center;
    pointer-events: none;
  }

  .mobile-button {
    width: 58px;
    height: 58px;
    border: 2px solid rgba(255, 255, 255, 0.34);
    border-radius: 999px;
    padding: 0;
    background: rgba(0, 0, 0, 0.26);
    color: rgba(255, 255, 255, 0.74);
    font: 700 18px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    text-align: center;
    pointer-events: auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-button:nth-child(1):last-child,
  .mobile-button:nth-child(3):last-child {
    grid-column: 1 / -1;
  }
}
