/* ==========================================================================
   PixelCrate — Game Player Page ("Paper Wireframe / Sketchbook")
   The game sits in a wireframe window (title bar + stage); a paper
   "Play now" cover; index-card control bar; "Lights off" focus mode;
   theater mode; native + fallback fullscreen; ad rails that never touch
   the game.
   Depends on: variables.css, main.css
   ========================================================================== */

/* ---------- Layout: [left rail] [game column] [right rail] ---------- */
.play-layout {
  --rail-gap: var(--space-8);          /* always ≥ --ad-clearance */
  --stage-max: 1120px;
  --windowbar-h: 42px;
  --controlbar-h: 64px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--rail-gap);
  width: 100%;
  max-width: var(--container);
  margin-inline: auto;
  padding: var(--space-6) var(--gutter) 0;
}

.play-rail { display: none; }

/* Skyscrapers only on very wide screens, so laptops never squeeze the game. */
@media (min-width: 1440px) {
  .play-layout {
    grid-template-columns: var(--skyscraper-w) minmax(0, 1fr) var(--skyscraper-w);
    max-width: 1760px;
  }
  .play-rail { display: block; }
  .play-rail .ad-slot {
    position: sticky;
    top: calc(var(--nav-h) + var(--space-4));
    z-index: var(--z-sticky-ad);
  }
}

.play-main { min-width: 0; }

/* ---------- Game window: wireframe title bar + stage ---------- */
.game-window {
  --ar: 1.7778;                        /* set from games.json aspectRatio */
  --stage-offset: calc(var(--nav-h) + var(--windowbar-h) + var(--controlbar-h) + 60px);
  /* widest the window can be while game + control bar still fit on screen */
  --h-limit: max(300px, calc((100vh - var(--stage-offset)) * var(--ar)));
  position: relative;
  width: min(100%, var(--stage-max), var(--h-limit));
  margin-inline: auto;
  border: var(--border-width) solid var(--border-ink);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: var(--shadow-card);
  transition: width var(--dur-slow) var(--ease);
}
@supports (height: 100svh) {
  /* "small" viewport height: always fits with the mobile toolbars showing, and
     stays the same while they slide in and out, so scrolling never resizes
     (and re-lays-out) the game */
  .game-window { --h-limit: max(300px, calc((100svh - var(--stage-offset)) * var(--ar))); }
}

.game-window__bar {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  height: var(--windowbar-h);
  padding: 0 6px 0 12px;
  border-bottom: var(--border-width) solid var(--border-ink);
  background: var(--bg-surface-alt);
}
.game-window__controls { display: flex; gap: 6px; flex-shrink: 0; }
.game-window__controls i {
  display: block;
  width: 12px;
  height: 12px;
  border: 1.5px solid var(--border-ink);
  border-radius: 50%;
  background: var(--bg-surface);
}
.game-window__controls i:first-child { background: var(--ink-primary); }
.game-window__title {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  color: var(--ink-primary);
  font: 700 var(--fs-xs) var(--font-mono);
  letter-spacing: 0.08em;
  text-align: center;
  text-overflow: ellipsis;
  text-transform: uppercase;
  white-space: nowrap;
}
.game-window__btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  flex-shrink: 0;
  border: 1.5px solid var(--border-ink);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: 2px 2px 0 var(--shadow-color);
  color: var(--ink-primary);
  font: 700 11px var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.game-window__btn:hover { transform: translate(-1px, -1px); box-shadow: 3px 3px 0 var(--shadow-color); }
.game-window__btn:active { transform: translate(2px, 2px); box-shadow: none; }
.game-window__btn[aria-pressed='true'] { background: var(--accent-highlighter); color: var(--on-highlighter); }

/* ---------- Stage (the game viewport) ---------- */
.stage {
  position: relative;
  z-index: var(--z-stage);
  width: 100%;
  aspect-ratio: var(--ar);
  overflow: hidden;
  background: var(--stage-bg);
  view-transition-name: game-stage;
}

.stage__frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: #000;
}

/* ---------- Click-to-play cover: the picture taped onto graph paper ---------- */
.stage__cover {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-4);
  width: 100%;
  padding: var(--space-6);
  border: 0;
  background-color: var(--bg-canvas);
  background-image: radial-gradient(circle, var(--grid-dot-color) 1.2px, transparent 1.6px);
  background-size: var(--grid-size) var(--grid-size);
  color: var(--ink-primary);
  text-align: center;
  overflow: hidden;
  cursor: pointer;
}
.stage__cover-bg { display: none; }
.stage__cover-thumb {
  width: min(38%, 320px);
  aspect-ratio: 16 / 9;
  object-fit: cover;
  padding: 6px;
  border: var(--border-width) solid var(--border-ink);
  background: var(--bg-surface);
  box-shadow: var(--shadow-card);
  transform: rotate(-1.5deg);
  transition: transform var(--dur-slow) var(--ease-out);
}
.stage__cover:hover .stage__cover-thumb { transform: rotate(0deg) translate(-2px, -2px); }
.stage__cover-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.1rem, 0.8rem + 1.6vw, 2rem);
  font-weight: 700;
  text-transform: uppercase;
}
.stage__cover-play {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  min-height: 52px;
  padding: 0 var(--space-8);
  border: var(--border-width) solid var(--border-ink);
  border-radius: var(--radius-sm);
  background: var(--accent-highlighter);
  color: var(--on-highlighter);
  box-shadow: var(--shadow-card);
  font: 700 var(--fs-md) var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.stage__cover-play .icon { width: 20px; height: 20px; }
.stage__cover:hover .stage__cover-play,
.stage__cover:focus-visible .stage__cover-play { transform: translate(-2px, -2px); box-shadow: var(--shadow-card-hover); }
.stage__cover:active .stage__cover-play { transform: translate(2px, 2px); box-shadow: var(--shadow-button-active); }
.stage__cover-hint {
  color: var(--ink-secondary);
  font: 700 var(--fs-xs) var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
}

@media (max-width: 560px) {
  .stage__cover { gap: var(--space-3); padding: var(--space-3); }
  .stage__cover-thumb { display: none; }
  .stage__cover-play { min-height: 46px; padding: 0 var(--space-6); font-size: var(--fs-sm); }
  .stage__cover-hint { display: none; }
}

/* ---------- Loading overlay (shown while the build downloads) ---------- */
.stage__loading {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: var(--space-3);
  background: var(--bg-canvas);
  color: var(--ink-secondary);
  font: 700 var(--fs-xs) var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
  pointer-events: none;
  transition: opacity var(--dur-slow) var(--ease);
}
.stage__loading.is-done { opacity: 0; }

/* a pencilled dashed circle that turns */
.spinner {
  width: 42px;
  height: 42px;
  border: 3px dashed var(--ink-primary);
  border-radius: 50%;
  animation: spin 1.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Error / not-found state inside the stage ---------- */
.stage__message {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: var(--space-3);
  padding: var(--space-6);
  background: var(--bg-canvas);
  color: var(--ink-primary);
  text-align: center;
}
.stage__message h1, .stage__message h2 { margin: 0; font-size: var(--fs-lg); text-transform: uppercase; }
.stage__message p { margin: 0; max-width: 420px; color: var(--ink-secondary); }

/* Exit button for the CSS fallback fullscreen (iPhone Safari) */
.stage__exit {
  position: absolute;
  top: calc(var(--space-3) + env(safe-area-inset-top, 0px));
  right: calc(var(--space-3) + env(safe-area-inset-right, 0px));
  z-index: 4;
  display: none;
  place-items: center;
  width: 42px;
  height: 42px;
  border: var(--border-width) solid #1C1B18;
  border-radius: var(--radius-sm);
  background: #FFFFFF;
  box-shadow: 3px 3px 0 #1C1B18;
  color: #1C1B18;
}

/* ---------- Fullscreen ---------- */
html .stage:fullscreen {
  width: 100vw;
  height: 100vh;
  max-width: none;
  aspect-ratio: auto;
}
html .stage:-webkit-full-screen {
  width: 100vw;
  height: 100vh;
  max-width: none;
}
html .stage.is-pseudo-fullscreen {
  position: fixed;
  inset: 0;
  z-index: var(--z-stage-fullscreen);
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  max-width: none;
  aspect-ratio: auto;
}
.stage.is-pseudo-fullscreen .stage__exit { display: grid; }
body.has-pseudo-fullscreen { overflow: hidden; }

/* ---------- Lights off: dim everything except the game and its controls ---------- */
body.is-lights-off::after {
  content: '';
  position: fixed;
  inset: 0;
  z-index: var(--z-scrim);
  background: var(--scrim);
  pointer-events: none;
  animation: lights-dim var(--dur-slow) var(--ease) both;
}
.is-lights-off .game-window,
.is-lights-off .control-bar { position: relative; z-index: var(--z-focus); }
@keyframes lights-dim { from { opacity: 0; } to { opacity: 1; } }

/* ---------- Control bar (directly below the game window) ---------- */
.control-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  width: min(100%, var(--stage-max));
  min-height: var(--controlbar-h);
  margin: var(--space-4) auto 0;
  padding: var(--space-2) var(--space-2) var(--space-2) var(--space-3);
  border: var(--border-width) solid var(--border-ink);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: var(--shadow-card);
}

.control-bar__info {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}
.control-bar__thumb {
  width: 68px;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  border: 1.5px solid var(--border-ink);
  flex-shrink: 0;
}
.control-bar__text { min-width: 0; }
.control-bar__title {
  margin: 0;
  font-size: var(--fs-md);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.control-bar__meta {
  color: var(--ink-secondary);
  font: 700 11px var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
}
.control-bar__meta a {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--ink-primary);
}
.control-bar__meta a::before {
  content: '';
  width: 7px;
  height: 7px;
  border: 1.5px solid currentColor;
}

.control-bar__actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-shrink: 0;
}

.ctrl-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: 42px;
  min-width: 42px;
  padding: 0 var(--space-3);
  border: var(--border-width) solid var(--border-ink);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: 2px 2px 0 var(--shadow-color);
  color: var(--ink-primary);
  font: 700 var(--fs-xs) var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
  transition: transform var(--dur) var(--ease), box-shadow var(--dur) var(--ease), background-color var(--dur) var(--ease);
}
.ctrl-btn:hover { transform: translate(-2px, -2px); box-shadow: 4px 4px 0 var(--shadow-color); }
.ctrl-btn:active { transform: translate(2px, 2px); box-shadow: none; }
.ctrl-btn[aria-pressed='true'] { background: var(--accent-highlighter); color: var(--on-highlighter); }
.ctrl-btn--fav[aria-pressed='true'] { background: var(--bg-surface); color: var(--accent-red-ink); border-color: var(--accent-red-pen); }
.ctrl-btn--fav[aria-pressed='true'] .icon { fill: currentColor; animation: heart-pop 0.4s var(--ease-out); }
.ctrl-btn--primary { background: var(--accent-highlighter); color: var(--on-highlighter); }

@media (max-width: 1023px) {
  .ctrl-btn--theater { display: none; }
}
@media (max-width: 860px) {
  .ctrl-btn__label { display: none; }
  .ctrl-btn { padding: 0; width: 42px; }
}
@media (max-width: 420px) {
  .control-bar__thumb { display: none; }
  .game-window__btn-label { display: none; }
}

.rotate-hint {
  display: none;
  align-items: center;
  gap: var(--space-2);
  width: min(100%, var(--stage-max));
  margin: var(--space-3) auto 0;
  color: var(--ink-secondary);
  font: 700 var(--fs-xs) var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
}
@media (orientation: portrait) and (max-width: 760px) {
  .rotate-hint.is-relevant { display: flex; }
}

/* ---------- Ad below the control bar ---------- */
.ad-wrap--player-bottom {
  display: flex;
  justify-content: center;
  /* control bar sits between the game and this ad, so clearance is always > 16px */
  margin-top: var(--space-8);
}

/* ---------- Mobile: edge-to-edge game window ---------- */
@media (max-width: 640px) {
  .play-layout { padding-inline: 0; padding-top: 0; gap: 0; }
  .game-window { border-inline: 0; border-top: 0; border-radius: 0; box-shadow: none; }
  .control-bar { margin-inline: var(--gutter); width: auto; }
  .rotate-hint { padding-inline: var(--gutter); }
  .play-main > .details,
  .play-main > .related,
  .play-main > .ad-wrap--player-bottom { padding-inline: var(--gutter); }
}

/* ---------- Theater mode ---------- */
.is-theater .play-layout {
  grid-template-columns: minmax(0, 1fr);
  max-width: none;
}
.is-theater .play-rail { display: none !important; }
/* Up to 90vw (height permitting) — and never smaller than the normal layout. */
.is-theater .game-window {
  --stage-offset: calc(var(--nav-h) + var(--windowbar-h) + var(--controlbar-h) + 44px);
  width: max(min(100%, var(--stage-max), var(--h-limit)), min(90vw, var(--h-limit)));
  max-width: none;
}
.is-theater .control-bar { width: max(min(100%, var(--stage-max)), min(90vw, 1600px)); }
.is-theater .play-main > .details,
.is-theater .play-main > .related { max-width: var(--container); margin-inline: auto; }

/* ---------- Below-the-fold content ---------- */
.details {
  display: grid;
  grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr);
  gap: var(--space-6);
  max-width: var(--stage-max);
  margin: var(--space-12) auto 0;
}
@media (max-width: 960px) {
  .details { grid-template-columns: minmax(0, 1fr); }
}

.panel {
  padding: var(--space-6);
  border: var(--border-width) solid var(--border-ink);
  border-radius: var(--radius-sm);
  background: var(--bg-surface);
  box-shadow: var(--shadow-card);
}
.panel + .panel { margin-top: var(--space-6); }
.panel h2 { font-size: var(--fs-md); margin-bottom: var(--space-4); text-transform: uppercase; }

/* Controls / instructions: a dashed note with keycaps */
#controls-panel {
  border-style: dashed;
  border-color: var(--border-dashed);
  box-shadow: none;
}

.details__about h2 { font-size: var(--fs-lg); }
.details__about p:last-child { margin-bottom: 0; }

.controls-table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--fs-sm);
}
.controls-table th,
.controls-table td {
  padding: var(--space-3) 0;
  border-bottom: 1.5px dashed var(--border-dashed);
  text-align: left;
  vertical-align: middle;
}
.controls-table tr:last-child th,
.controls-table tr:last-child td { border-bottom: 0; }
.controls-table th { width: 50%; padding-right: var(--space-3); font-weight: 400; }
.controls-table td { color: var(--ink-secondary); }

/* keycaps: solid border + a 3px "lip" underneath */
.kbd {
  display: inline-block;
  margin: 2px 4px 5px 0;
  padding: 2px 8px;
  border: var(--border-width) solid var(--border-ink);
  border-radius: 3px;
  background: var(--bg-surface);
  box-shadow: 0 3px 0 var(--border-ink);
  color: var(--ink-primary);
  font: 700 12px/1.6 var(--font-mono);
  text-transform: uppercase;
  white-space: nowrap;
}

.controls-text { color: var(--ink-primary); font-weight: 600; }

.meta-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--space-3) var(--space-4);
  margin: 0;
  font-size: var(--fs-sm);
}
.meta-list dt {
  color: var(--ink-secondary);
  font: 700 11px/1.9 var(--font-mono);
  letter-spacing: var(--tracking-meta);
  text-transform: uppercase;
}
.meta-list dd { margin: 0; color: var(--ink-primary); }
.meta-list .tag { margin: 0 4px 4px 0; }

.related {
  max-width: var(--stage-max);
  margin: var(--space-12) auto 0;
  padding-bottom: var(--space-12);
}
.related .section__meta { display: inline-flex; align-items: center; gap: 4px; color: var(--ink-primary); }
.related .game-row--fit { padding-block: 6px 10px; }
