* {
  box-sizing: border-box;
}

:root {
  --safe-area-top: env(safe-area-inset-top, 0px);
  --safe-area-right: env(safe-area-inset-right, 0px);
  --safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --safe-area-left: env(safe-area-inset-left, 0px);
}

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:
    calc(48px + var(--safe-area-top))
    calc(24px + var(--safe-area-right))
    calc(28px + var(--safe-area-bottom))
    calc(24px + var(--safe-area-left));
  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;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.game-canvas-wrap {
  --game-aspect-ratio: inherit;
  --game-aspect-width: inherit;
  --game-aspect-height: inherit;
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: var(--game-aspect-ratio);
  width: 100%;
}

.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;
}

.mobile-controls {
  display: none;
}

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

.game-files-dialog {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid #2d333c;
  border-radius: 8px;
  background: #171a1f;
  color: #d6dde8;
}

.game-files-dialog.hidden {
  display: none;
}

.game-files-dialog__tabs {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  border-bottom: 1px solid #2d333c;
}

.game-files-tab {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  margin-bottom: -1px;
  padding: 6px 10px;
  border: 1px solid transparent;
  border-bottom-color: #2d333c;
  border-radius: 6px 6px 0 0;
  background: transparent;
  color: #aeb8c5;
  font: inherit;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
}

.game-files-tab:hover,
.game-files-tab:focus {
  color: #eef3f9;
  outline: none;
}

.game-files-tab:focus-visible {
  outline: 2px solid #61758b;
  outline-offset: 2px;
}

.game-files-tab--active {
  border-color: #2d333c;
  border-bottom-color: #171a1f;
  background: #171a1f;
  color: #eef3f9;
}

.game-files-tab__count {
  min-width: 22px;
  padding: 1px 6px;
  border-radius: 999px;
  background: #27303a;
  color: #b9c5d3;
  font-size: 12px;
  text-align: center;
}

.game-files-panel {
  min-width: 0;
}

.game-files-panel.hidden {
  display: none;
}

.plugin-panel {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.plugin-panel__summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

.plugin-panel__count {
  min-width: 0;
  color: #aeb8c5;
  font-size: 12px;
}

.plugin-clear {
  flex: 0 0 auto;
  min-height: 28px;
  padding: 4px 9px;
  border: 1px solid #3b4652;
  border-radius: 6px;
  background: #202833;
  color: #f1b6aa;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}

.plugin-clear:hover {
  border-color: #8b6158;
  background: #3a2728;
  color: #ffd2c8;
}

.plugin-clear.hidden,
.plugin-selected.hidden {
  display: none;
}

.plugin-selected {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.plugin-section-title {
  color: #d6dde8;
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.plugin-selected-list {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.plugin-selected-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 8px;
  min-height: 34px;
  padding: 6px 8px 6px 10px;
  border: 1px solid #303943;
  border-radius: 6px;
  background: #1d2229;
}

.plugin-selected-row__label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #eef3f9;
  font-size: 13px;
  font-weight: 650;
}

.plugin-selected-row__meta {
  color: #7f8d9e;
  font-size: 11px;
  white-space: nowrap;
}

.plugin-disable {
  min-height: 26px;
  padding: 3px 8px;
  border: 1px solid #3b4652;
  border-radius: 5px;
  background: #202833;
  color: #f1b6aa;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}

.plugin-disable:hover {
  border-color: #8b6158;
  background: #3a2728;
  color: #ffd2c8;
}

.plugin-list {
  display: grid;
  max-height: 260px;
  overflow: auto;
  border: 1px solid #2d333c;
  border-radius: 6px;
  background: #111419;
}

.plugin-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-bottom: 1px solid #262c35;
}

.plugin-row:last-child {
  border-bottom: 0;
}

.plugin-row--disabled {
  color: #8f9aaa;
}

.plugin-choice {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  gap: 8px;
  align-items: start;
  min-width: 0;
}

.plugin-checkbox {
  width: 16px;
  height: 16px;
  margin: 2px 0 0;
}

.plugin-row__body {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.plugin-row__title {
  color: #eef3f9;
  font-size: 13px;
  font-weight: 650;
}

.plugin-row--disabled .plugin-row__title {
  color: #b5bfcc;
}

.plugin-row__meta {
  color: #7f8d9e;
  font-size: 11px;
}

.plugin-row__description {
  color: #aeb8c5;
  font-size: 12px;
  line-height: 1.4;
}

.plugin-download {
  min-height: 28px;
  padding: 5px 8px;
  border: 1px solid #3b4652;
  border-radius: 6px;
  background: #202833;
  color: #eef3f9;
  font-size: 12px;
  text-decoration: none;
  white-space: nowrap;
}

.plugin-download:focus {
  outline: 2px solid #61758b;
  outline-offset: 2px;
}

.save-panel {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.save-panel__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #d6dde8;
  font-size: 13px;
  font-weight: 650;
}

.save-panel__title,
.save-panel__actions {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.save-panel__count {
  min-width: 24px;
  padding: 2px 7px;
  border-radius: 999px;
  background: #27303a;
  color: #b9c5d3;
  text-align: center;
  font-size: 12px;
}

.save-import {
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid #3b4652;
  border-radius: 6px;
  background: #202833;
  color: #eef3f9;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}

.save-import:hover:not(:disabled) {
  border-color: #61758b;
  background: #283240;
}

.save-import:disabled {
  opacity: 0.48;
  cursor: default;
}

.save-import__input {
  display: none;
}

.save-panel__message {
  color: #ffb7a8;
  font-size: 12px;
  line-height: 1.35;
}

.save-empty {
  min-height: 38px;
  padding: 9px 10px;
  border: 1px solid #303943;
  border-radius: 6px;
  background: #14181e;
  color: #7f8d9e;
  font-size: 13px;
}

.save-panel__message.hidden,
.save-empty.hidden,
.save-list.hidden {
  display: none;
}

.save-list {
  display: grid;
  gap: 6px;
  min-width: 0;
}

.save-file {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  min-width: 0;
  min-height: 38px;
  padding: 6px 8px 6px 10px;
  border: 1px solid #303943;
  border-radius: 6px;
  background: #1d2229;
  color: #eef3f9;
  font: inherit;
  font-size: 13px;
  text-align: left;
}

.save-file:hover,
.save-file:focus-within {
  border-color: #56687b;
  background: #242b34;
}

.save-file__name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.save-file__actions {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.save-file__action {
  min-height: 26px;
  padding: 3px 8px;
  border: 1px solid #3b4652;
  border-radius: 5px;
  background: #202833;
  color: #b9cff4;
  font: inherit;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}

.save-file__action:hover {
  border-color: #61758b;
  background: #283240;
  color: #e8f1ff;
}

.save-file__action--delete {
  color: #f1b6aa;
}

.save-file__action--delete:hover {
  border-color: #8b6158;
  background: #3a2728;
  color: #ffd2c8;
}

/* 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:
      calc(24px + var(--safe-area-top))
      calc(16px + var(--safe-area-right))
      calc(24px + var(--safe-area-bottom))
      calc(16px + var(--safe-area-left));
  }
  .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: calc(12px + var(--safe-area-bottom));
    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;
    gap: 0;
    background: #000;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }

  .app[data-view="playing"] .game-canvas-wrap {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
    border-radius: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--safe-area-top) var(--safe-area-right)
      var(--safe-area-bottom) var(--safe-area-left);
  }

  .app[data-view="playing"] .game-files-dialog {
    display: none;
  }

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

  .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: calc(18px + var(--safe-area-left));
    bottom: calc(18px + var(--safe-area-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-button-panel {
    position: absolute;
    right: calc(18px + var(--safe-area-right));
    bottom: calc(18px + var(--safe-area-bottom));
    display: grid;
    gap: 8px;
    pointer-events: none;
  }

  .mobile-button-tabs {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
    width: 128px;
    justify-self: end;
    pointer-events: none;
  }

  .mobile-button-tab {
    height: 28px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 6px;
    padding: 0;
    background: rgba(0, 0, 0, 0.34);
    color: rgba(255, 255, 255, 0.7);
    font: 700 10px/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-tab.is-active {
    border-color: rgba(255, 255, 255, 0.58);
    background: rgba(255, 255, 255, 0.18);
    color: rgba(255, 255, 255, 0.92);
  }

  .mobile-button-group-stack {
    display: block;
  }

  .mobile-buttons {
    display: grid;
    grid-template-columns: repeat(2, 58px);
    grid-auto-rows: 58px;
    gap: 12px;
    align-items: center;
    justify-items: center;
    pointer-events: none;
  }

  .mobile-buttons:not(.is-active) {
    display: 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;
  }
}
