:root {
  color-scheme: dark;
  --paper: #eee9df;
  --muted: #8c9291;
  --ink: #090d0f;
  --panel: #101617;
  --line: #2a3232;
  --rose: #d68ba1;
  --blood: #8f1830;
  font-family: "Trebuchet MS", ui-sans-serif, system-ui, sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  place-items: center;
  color: var(--paper);
  background: #070a0b;
}

button, input, select { font: inherit; }
button { color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .45; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--rose); outline-offset: 2px; }
.app { width: min(800px, calc(100% - 24px), calc((100vh - 92px) * 4 / 3)); padding: 16px 0; }
.app.replay-mode { width: min(800px, calc(100% - 24px)); }
header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 13px; }
.brand { font: 700 24px ui-sans-serif, system-ui, sans-serif; letter-spacing: -.04em; line-height: 1; }
.brand-group { display: flex; align-items: center; gap: 16px; }
.mode-switch { display: flex; gap: 10px; font-size: 9px; text-transform: uppercase; }
.mode-switch a { color: var(--muted); text-decoration: none; padding: 4px 0; }
.mode-switch a:hover, .mode-switch a[aria-current="page"] { color: var(--paper); }
.mode-switch a[aria-current="page"] { box-shadow: 0 1px var(--rose); }
.mode-switch a:focus-visible { outline: 2px solid var(--rose); outline-offset: 3px; }
.header-actions { display: flex; align-items: center; gap: 8px; }
.github-link { display: grid; place-items: center; flex: 0 0 32px; width: 32px; height: 32px; color: var(--muted); border-radius: 4px; }
.github-link:hover, .github-link:focus-visible { color: var(--paper); }
.github-link:focus-visible { outline: 2px solid var(--rose); outline-offset: 2px; }
.header-actions > button { padding: 6px 9px; border: 1px solid #303838; background: #101617aa; color: #b4b8b5; font-size: 9px; text-transform: uppercase; }

.layout { display: flex; flex-direction: column; gap: 9px; }
.game-frame {
  position: relative;
  overflow: hidden;
  aspect-ratio: 4 / 3;
  border: 1px solid #364142;
  background: #080d0f;
  box-shadow: 0 20px 80px #000b, inset 0 0 0 5px #0b1011;
}
canvas { display: block; width: 100%; height: 100%; cursor: default; touch-action: none; }

.runtime-loading {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 16px;
  padding: 30px;
  text-align: center;
  background: #000;
}
.runtime-loading[hidden] { display: none; }
.runtime-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #fff4;
  border-top-color: #fff;
  border-radius: 50%;
  animation: runtime-spin .8s linear infinite;
}
.runtime-loading strong {
  color: #fff;
  font-size: 12px;
  letter-spacing: .16em;
}

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

kbd { margin-left: 4px; color: #858c8c; font-size: 7px; }

.modal {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(340px, calc(100% - 40px)); padding: 26px; text-align: center;
  border: 1px solid #a38e90; background: #0a0f10ec; box-shadow: 0 18px 65px #000d;
}
.modal[hidden], .pause-mark[hidden] { display: none; }
.modal-kicker { color: var(--rose); font-size: 9px; font-weight: 900; letter-spacing: .2em; }
.modal h1 { margin: 7px 0; font: italic 700 28px Georgia, serif; }
.modal p { margin: 8px 0 18px; color: #929898; font-size: 12px; line-height: 1.5; }
.primary { min-width: 120px; padding: 10px 18px; border: 1px solid #ebdfcf; background: #e8dfd1; color: #171414; font-weight: 800; text-transform: uppercase; font-size: 10px; letter-spacing: .12em; }
.modal-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.modal-actions > button:not(.primary) { padding: 10px 14px; border: 1px solid #755861; background: #151011; color: #eadce0; font-size: 10px; text-transform: uppercase; }
.pause-mark { position: absolute; inset: 0; display: grid; place-items: center; background: #05080988; color: #e7d9cd; letter-spacing: .32em; font-weight: 800; pointer-events: none; }
.toast { position: fixed; bottom: 24px; left: 50%; transform: translate(-50%, 12px); opacity: 0; padding: 8px 13px; border: 1px solid #755861; background: #151011; color: #eadce0; font-size: 10px; transition: .18s; pointer-events: none; }
.toast.show { transform: translate(-50%, 0); opacity: 1; }

.replay-controls { display: grid; gap: 8px; padding: 10px; border: 1px solid var(--line); background: var(--panel); }
.replay-controls[hidden] { display: none; }
.replay-meta { display: flex; justify-content: space-between; gap: 12px; color: var(--muted); font-size: 10px; }
.replay-meta strong { overflow: hidden; color: var(--paper); text-overflow: ellipsis; white-space: nowrap; }
.replay-transport { display: grid; grid-template-columns: auto auto auto auto minmax(100px, 1fr) auto auto; align-items: center; gap: 7px; }
.replay-transport button { padding: 6px 9px; border: 1px solid #303838; background: #0c1112; color: #c5cac7; font-size: 9px; text-transform: uppercase; }
.replay-speed { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 9px; text-transform: uppercase; }
.replay-speed select { padding: 5px 6px; border: 1px solid #303838; background: #0c1112; color: #c5cac7; }
.replay-scrubber { width: 100%; accent-color: var(--rose); }
#replayPosition { min-width: 104px; color: #aeb4b1; font: 9px ui-monospace, monospace; text-align: right; }
.replay-error { color: #efabbc; font-size: 10px; }
.app-error { margin-top: 8px; padding: 9px 11px; border: 1px solid #755861; background: #151011; color: #efabbc; font-size: 10px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.soundtrack { width: min(100%, 320px); margin: 10px auto 0; }
.soundtrack iframe { display: block; width: 100%; border: 0; border-radius: 12px; }
.soundtrack p { margin: 4px 0 0; color: var(--muted); font-size: 10px; line-height: 1.4; }

@media (max-width: 540px) {
  .app { width: min(calc(100% - 12px), calc((100svh - 64px) * 4 / 3)); padding: 8px 0; }
  .app.replay-mode { width: calc(100% - 12px); }
  header { margin-bottom: 8px; }
  .brand { font-size: 20px; }
  .brand-group { flex-direction: column; align-items: flex-start; gap: 3px; }
  .header-actions { gap: 4px; }
  .header-actions > button { padding: 5px 6px; }
  .game-frame { border-left: 0; border-right: 0; }
  .header-actions kbd { display: none; }
  .replay-transport { grid-template-columns: auto auto auto 1fr; }
  .replay-scrubber { grid-column: 1 / -1; grid-row: 2; }
  #replayPosition { grid-column: 1 / 3; text-align: left; }
  #exitReplayButton { grid-column: 3 / 5; }
}

@media (prefers-reduced-motion: reduce) {
  .runtime-spinner { animation-duration: 0.001ms; animation-iteration-count: 1; }
}
