/* Train Your Replacement (E4, spec 4.2): one game frame that fills the box it is given. The slot's size is reserved from viewport
   units alone, before any script runs, so nothing on the page moves when the game starts, changes screen or is refitted. The script
   measures the slot (clientWidth x clientHeight) and draws the frame at a whole number of device pixels per art pixel, centred in it
   (or, for boxes too small for that, through a smooth soft copy). On the embed page (/train-your-replacement/embed/) the box is the
   whole viewport. Copied from Human in the Loop with hl- renamed tr-. */

/* no sideways overscroll (browser back-swipe) on the game page; the script also sets this at runtime */
html:has(.tr-stage),body:has(.tr-stage){overscroll-behavior-x:none}

/* ---------- the stage: game first on screen, head first in the DOM ---------- */
.tr-stage,.app .hero.tight.tr-stage{padding-top:12px;padding-bottom:28px}
.tr-stage>.in,.tr-stage>.in.wide{display:flex;flex-direction:column;gap:18px;max-width:none;
  padding-left:max(16px,env(safe-area-inset-left,0px));padding-right:max(16px,env(safe-area-inset-right,0px))}
.tr-slot{order:1;position:relative;display:grid;place-items:center;box-sizing:border-box;width:100%;overflow:hidden;overflow:clip;overscroll-behavior:auto;
  height:max(420px,calc(100vh - 63px));height:max(420px,calc(100svh - 63px))}
.tr-head{order:2;display:flex;flex-direction:column;gap:14px;align-items:flex-start;width:100%;max-width:1080px;margin:0 auto;box-sizing:border-box}
.tr-stage noscript{order:3}
.app .hero.tight.tr-stage h1,.tr-stage h1{font-size:clamp(26px,3.4vw,40px)}
.tr-keys{margin:0;width:100%;max-width:none;font:500 13px/1.7 var(--mono);color:var(--ink)}
.tr-keys span{display:inline-block;margin-right:18px}
.tr-keys b{display:inline-block;font-weight:500;letter-spacing:.06em;text-transform:uppercase;background:var(--ink);color:var(--paper);padding:1px 8px;border-radius:var(--r-pill);margin-right:4px}

/* landscape windows: the slot is as wide as the page and at most 16:9, never taller than what is left under the site bar */
@media (min-aspect-ratio:1/1) and (min-width:600px){
  .tr-slot{height:min(calc(100vh - 79px),calc((100vw - 32px)*.5625));height:min(calc(100svh - 79px),calc((100vw - 32px)*.5625));min-height:300px}
}
/* portrait windows: a phone held upright. The frame runs edge to edge under the site bar. */
@media not all and (min-aspect-ratio:1/1) and (min-width:600px){
  .tr-stage,.app .hero.tight.tr-stage{padding-top:4px}
  .tr-stage>.in,.tr-stage>.in.wide{padding-left:0;padding-right:0;gap:20px}
  .tr-head,.tr-stage noscript{padding:0 16px}
  .tr-slot{background:#15131a;border-bottom:3px solid var(--ink)}
  .tr-keys span{display:block;margin-right:0}
}
/* under about 375px wide the site bar's links wrap it to 64px (52px above), so the slot leaves 69px: bar, 4px of padding, 1px spare */
@media (max-width:374px){.tr-slot{height:max(420px,calc(100vh - 69px));height:max(420px,calc(100svh - 69px))}}
/* a phone on its side (or any very short window): the site bar shrinks to a 32px strip so the floor gets the height, and the
   bottom edge keeps clear of the home indicator. Only this page's bar; the links stay, just smaller. */
@media (orientation:landscape) and (max-height:500px){
  body:has(.tr-stage):not(.embed) header.top .in{min-height:32px;gap:12px}
  body:has(.tr-stage):not(.embed) header.top .logo{font-size:20px;padding:4px 2px}
  body:has(.tr-stage):not(.embed) header.top .logo::after{inset:-2px -6px}
  body:has(.tr-stage):not(.embed) header.top nav{gap:2px;margin-right:-8px}
  body:has(.tr-stage):not(.embed) header.top nav a{padding:10px 10px}
  .tr-stage,.app .hero.tight.tr-stage{padding-top:4px}
  .tr-slot{min-height:240px;
    height:calc(100vh - 36px - max(6px,env(safe-area-inset-bottom,0px)));height:calc(100svh - 36px - max(6px,env(safe-area-inset-bottom,0px)))}
}

/* ---------- the frame ---------- */
.tr-frame{position:relative;width:100%;height:100%;flex:none;box-sizing:content-box;background:#15131a;border:0;outline:3px solid var(--ink);box-shadow:8px 8px 0 var(--orange);line-height:0;
  touch-action:manipulation;-webkit-user-select:none;user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent}
@media not all and (min-aspect-ratio:1/1) and (min-width:600px){
  .tr-frame{outline-width:0;box-shadow:none}
}
/* Until the script has fitted the frame (it writes the size as an inline style) the frame is not shown, so it is never seen at the
   wrong size. The animation is only a safety net: if the size never arrives, show the frame anyway. */
.tr-frame:not([style]){width:240px;height:240px;visibility:hidden;animation:tr-show 0s linear 4s forwards}
@keyframes tr-show{to{visibility:visible}}
/* The script sizes and measures these at once. The site turns every transition into a .01ms one when the device asks for less
   motion, which would make a measurement read the old box. Nothing here ever transitions or animates. */
.tr-stage,.tr-slot,.tr-frame[style],.tr-frame canvas,.tr-hits,.tr-hits button,.tr-big,.tr-sr,.tr-svh{transition:none!important;animation:none!important}
.tr-frame:focus{outline:3px solid var(--ink)}
.tr-frame:focus-visible{outline:2px solid #ffd23f;outline-offset:-2px}
.tr-frame canvas{display:block;width:100%;height:100%;image-rendering:pixelated;image-rendering:crisp-edges;
  -webkit-user-select:none;user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent}
/* the soft copy (small boxes): the browser's smooth scaling of a whole-number enlargement, so the art pixels stay even */
.tr-frame canvas.tr-soft{position:absolute;left:0;top:0;image-rendering:auto;pointer-events:none}

/* Touch: the page scrolls normally with a swipe over the title and the menus; while the screen is play or pause (the release card
   is part of play) a finger on the floor never pans or zooms. The shell toggles tr-play only on a change of screen. */
.tr-frame,.tr-frame canvas,.tr-hits button{touch-action:manipulation}
.tr-frame.tr-play,.tr-frame.tr-play canvas,.tr-frame.tr-play .tr-hits button,.tr-frame.tr-play .tr-big{touch-action:none}
/* The in-post player has nothing to scroll, so a pinch on any screen must never zoom it (in a post it would stay zoomed and crop). */
body.embed,body.embed .tr-frame,body.embed .tr-frame canvas,body.embed .tr-hits button,body.embed .tr-big{touch-action:none!important}

/* ---------- the hit layer: real buttons over drawn controls ---------- */
.tr-hits{position:absolute;inset:0;overflow:hidden;pointer-events:none}
.tr-hits button{position:absolute;margin:0;padding:0;border:0;background:transparent;color:transparent;font-size:0;cursor:pointer;pointer-events:auto;
  -webkit-appearance:none;appearance:none;border-radius:0;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;-webkit-user-select:none;user-select:none}
.tr-hits button:disabled{cursor:default}
.tr-hits button[hidden]{display:none}
.tr-hits button[data-h="any"]{cursor:pointer}
.tr-hits button:focus{outline:none}
.tr-hits button:focus-visible{outline:2px solid #ffd23f;outline-offset:-2px}

/* screen-reader only */
.tr-sr,.tr-hits .tr-sronly{position:absolute;left:0;top:0;width:1px;height:1px;margin:0;padding:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;font-size:16px;line-height:1.2}
/* the viewport probe the shell adds to read svh */
.tr-svh{position:fixed;left:0;top:0;width:0;height:100vh;height:100svh;visibility:hidden;pointer-events:none}

/* ---------- the full-screen control: an <a> inside the frame over the HUD's full cell; its icon is drawn on the canvas. The shell
   shows it (inline display) only where there is a cell for it. ---------- */
.tr-big{position:absolute;z-index:3;display:none;box-sizing:border-box;margin:0;padding:0;background:transparent;color:transparent;border:0;font-size:0;line-height:0;
  text-decoration:none;cursor:pointer;pointer-events:auto;touch-action:manipulation;-webkit-tap-highlight-color:transparent;-webkit-touch-callout:none;overflow:hidden}
.tr-big span{position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
.tr-big:focus{outline:none}
.tr-big:focus-visible{outline:2px solid #ffd23f;outline-offset:-2px}

/* ---------- full screen: the slot alone fills the screen and the game refits to it ---------- */
.tr-slot:fullscreen{width:100vw!important;height:100vh!important;max-width:none;min-height:0;margin:0;padding:0;border:0;background:#15131a;display:grid;place-items:center}
.tr-slot:-webkit-full-screen{width:100vw!important;height:100vh!important;max-width:none;min-height:0;margin:0;padding:0;border:0;background:#15131a;display:grid;place-items:center}
.tr-slot::backdrop{background:#15131a}
.tr-slot:fullscreen .tr-frame,.tr-slot:fullscreen .tr-frame:focus{outline:0;box-shadow:none}
.tr-slot:-webkit-full-screen .tr-frame{outline:0;box-shadow:none}
.tr-slot:fullscreen .tr-frame:focus-visible{outline:2px solid #ffd23f;outline-offset:-2px}

/* ---------- the art contact sheet, appended by #sheet ---------- */
.tr-sheet{display:flex;flex-wrap:wrap;gap:14px;align-items:flex-end;padding:16px 0}
.tr-sheet figure{margin:0;display:flex;flex-direction:column;gap:4px;font:500 11px/1.2 var(--mono)}
.tr-sheet canvas{image-rendering:pixelated;image-rendering:crisp-edges;border:2px solid var(--ink);background:#15131a}

/* ---------- the embed page: the game alone, filling the player box exactly, no scroll ---------- */
html:has(body.embed){overflow:hidden;overscroll-behavior:none;background:#15131a}
body.embed{margin:0;padding:0;background:#15131a;overflow:hidden;overscroll-behavior:none;min-height:100vh;min-height:100svh}
body.embed .embed-main{margin:0;padding:0}
body.embed .tr-stage,body.embed.app .hero.tight.tr-stage,body.embed .hero.first.tight.tr-stage{padding:0!important;margin:0;border:0;background:#15131a;min-height:0}
body.embed .tr-stage>.in,body.embed .tr-stage>.in.wide{padding:0;gap:0;margin:0;max-width:none}
body.embed .tr-slot{height:100vh;height:100svh;min-height:0;width:100vw;background:#15131a;border:0;place-items:center}
body.embed .tr-frame,body.embed .tr-frame:focus{box-shadow:none;outline:0}
body.embed .tr-frame:focus-visible{outline:2px solid #ffd23f;outline-offset:-2px}
/* the page link stays in the DOM (the full-screen control copies its href), visually hidden */
body.embed .embed-foot{position:absolute;left:0;top:0;width:1px;height:1px;margin:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap}
/* the link itself is clipped to its 1px box too, so nothing is ever wider than the player; on keyboard focus it becomes a readable bar along the bottom that wraps to fit even a 300px box */
body.embed .embed-foot a{display:block;width:100%;max-width:100vw;overflow:hidden;box-sizing:border-box}
body.embed .embed-foot:focus-within{position:fixed;left:6px;right:6px;top:auto;bottom:6px;width:auto;height:auto;max-width:calc(100vw - 12px);overflow:visible;clip-path:none;white-space:normal;z-index:20}
body.embed .embed-foot:focus-within a{width:auto;padding:8px 10px;background:#15131a;color:#ffd23f;border:2px solid #ffd23f;font:700 14px/1.3 system-ui,sans-serif;text-align:center;overflow-wrap:break-word;outline:0;text-decoration:underline}
/* kit.js appends a visually hidden live region to the body; pinned to the top so it cannot add a pixel of scroll under the frame */
body.embed>.vh{left:0;top:0;margin:0}
