/**
 * Fernwork — Games page (tools/games.html): the game stage fills the viewport under the page header; menus, pause
 * and result screens are kit cards over the canvas. Kit tokens only (npm run check:type).
 */

/* The page is an app shell: no document scroll, the stage takes the remaining height. */
body[data-fw-cat="games"] { overflow: hidden; }
.gm-main {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - var(--appbar-h) - env(safe-area-inset-top));
  min-height: 0;
  padding-bottom: var(--space-4);
}
.gm-main > .page-header { flex: none; }
.gm-main > .tab-content.active { display: flex; flex: 1 1 auto; min-height: 0; }
.gm-host, .gm-game { display: flex; flex: 1 1 auto; min-width: 0; min-height: 0; }

.gm-stage {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--stage);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.gm-canvas { position: absolute; inset: 0; display: block; outline: none; }
.gm-canvas--gl { pointer-events: none; width: 100%; height: 100%; }
.gm-stage[data-screen="play"] .gm-canvas { cursor: crosshair; }

/* ───────────── HUD ───────────── */
.gm-hud {
  position: absolute; inset: 0 0 auto 0; z-index: 2;
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-2);
  pointer-events: none;
}
.gm-hud[hidden] { display: none; }
.gm-hud > * { pointer-events: auto; }
.gm-hud__btn.fw-iconbtn {
  flex: none; width: var(--hit-min); height: var(--hit-min);
  border-radius: var(--radius-pill);
  background: rgba(9, 9, 11, .5); color: #fff;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
@media (hover: hover) { .gm-hud__btn.fw-iconbtn:hover { background: rgba(9, 9, 11, .7); color: #fff; } }
.gm-hud__mid {
  flex: 0 1 280px; min-width: 0; margin-inline: auto;
  display: flex; flex-direction: column; align-items: stretch; gap: 6px;
  padding: 6px var(--space-3); border-radius: var(--radius-sm);
  background: rgba(9, 9, 11, .5); color: #fff;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.gm-hud__level { font-size: var(--text-xs); font-weight: var(--weight-semibold); text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gm-hud__bar { height: 6px; border-radius: var(--radius-pill); background: rgba(255, 255, 255, .25); overflow: hidden; }
.gm-hud__bar[hidden] { display: none; }
.gm-hud__bar > span { display: block; height: 100%; background: #4ade80; transform-origin: left center; transform: scaleX(0); transition: transform var(--dur-1) linear; }
.gm-hud__score {
  flex: none; min-width: 72px;
  display: flex; flex-direction: column; align-items: flex-end;
  padding: 4px var(--space-3); border-radius: var(--radius-sm);
  background: rgba(9, 9, 11, .5); color: #fff;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.gm-hud__label { font-size: var(--text-2xs); text-transform: uppercase; letter-spacing: .04em; opacity: .75; }
.gm-hud__score .fw-num { font-size: var(--text-md); font-weight: var(--weight-semibold); line-height: 1.2; }

.gm-hint {
  position: absolute; left: 50%; top: calc(var(--hit-min) + var(--space-5)); z-index: 2;
  transform: translateX(-50%);
  max-width: min(420px, calc(100% - 32px));
  padding: var(--space-2) var(--space-4); border-radius: var(--radius-pill);
  background: rgba(9, 9, 11, .62); color: #fff;
  font-size: var(--text-sm); font-weight: var(--weight-medium); text-align: center;
  pointer-events: none;
  animation: gm-hint 3.2s var(--ease-out) both;
}
.gm-hint[hidden] { display: none; }
@keyframes gm-hint {
  0% { opacity: 0; transform: translate(-50%, 8px); }
  10%, 85% { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, 0); }
}

/* ───────────── Screens (menu, pause, result) ───────────── */
.gm-screen {
  position: absolute; inset: 0; z-index: 3;
  display: grid; place-items: center;
  padding: var(--space-4);
  overflow: auto;
  background: color-mix(in srgb, var(--bg) 35%, transparent);
  backdrop-filter: blur(3px); -webkit-backdrop-filter: blur(3px);
  touch-action: pan-y;
}
.gm-screen[hidden] { display: none; }
.gm-card {
  width: min(460px, 100%);
  display: flex; flex-direction: column; gap: var(--space-3);
  padding: var(--space-6);
  border: 1px solid var(--border-subtle); border-radius: var(--radius-md);
  background: var(--surface-overlay); color: var(--text-1);
  box-shadow: var(--shadow-3);
  font-family: var(--font-ui);
  animation: gm-card-in var(--dur-3) var(--ease-out) both;
}
.gm-card--small { width: min(340px, 100%); text-align: center; }
.gm-art {
  display: block; width: calc(100% + 2 * var(--space-6)); max-width: none; margin: calc(-1 * var(--space-6)) calc(-1 * var(--space-6)) 0;
  aspect-ratio: 16 / 8; object-fit: cover; object-position: center 45%;
  border-radius: var(--radius-md) var(--radius-md) 0 0; background: var(--surface-sunken);
}
.gm-card .fw-btn { width: 100%; justify-content: center; }
@keyframes gm-card-in { from { opacity: 0; transform: translateY(8px) scale(.98); } }

.gm-title { margin: 0; font-size: var(--text-xl); line-height: var(--lh-xl); font-weight: var(--weight-semibold); letter-spacing: var(--tracking-tight); }
.gm-tagline { margin: var(--space-1) 0 0; color: var(--text-2); font-size: var(--text-sm); line-height: var(--lh-sm); }
.gm-howto { margin: 0; color: var(--text-3); font-size: var(--text-xs); line-height: var(--lh-xs); }
.gm-credit { margin: 0; color: var(--text-3); font-size: var(--text-xs); line-height: var(--lh-xs); }
.gm-menu__foot { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); }
.gm-menu__foot .fw-btn { width: auto; }
.gm-total { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-size: var(--text-sm); }
.gm-total .fw-icon { color: #f59e0b; fill: currentColor; }

/* Level select */
.gm-levels { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: var(--space-2); }
.gm-level {
  appearance: none; font: inherit; cursor: pointer;
  min-height: var(--hit-min);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  padding: 6px 2px;
  border: 1px solid var(--border-subtle); border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--text-1);
  transition: background var(--dur-1) var(--ease-out), border-color var(--dur-1) var(--ease-out), transform var(--dur-1) var(--ease-out);
}
@media (hover: hover) { .gm-level:not(:disabled):hover { background: var(--surface-hover); border-color: var(--border-strong); } }
.gm-level:active:not(:disabled) { transform: scale(.96); }
.gm-level:focus-visible { outline: none; box-shadow: var(--focus-ring); }
.gm-level.is-next { border-color: var(--accent-border); background: var(--accent-subtle-bg); }
.gm-level.is-locked { cursor: not-allowed; color: var(--text-disabled); background: var(--surface-sunken); }
.gm-level__n { font-size: var(--text-md); font-weight: var(--weight-semibold); line-height: 1; font-variant-numeric: tabular-nums; }
.gm-level__stars { display: inline-flex; gap: 0; }
.gm-level__stars .gm-star svg { width: 11px; height: 11px; }
.gm-level.is-locked .gm-level__stars { visibility: hidden; }

.gm-star { display: inline-flex; color: var(--border-strong); }
.gm-star.is-on { color: #f59e0b; }
.gm-star.is-on svg { fill: currentColor; }

/* Result */
.gm-result__kicker { margin: 0; color: var(--text-3); font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .06em; }
.gm-result[data-won="false"] .gm-title { color: var(--danger); }
.gm-result__stars { display: flex; justify-content: center; gap: var(--space-2); }
.gm-result__stars .gm-star svg { width: 40px; height: 40px; }
.gm-star--pop.is-on { animation: gm-star-pop 420ms var(--ease-out) both; }
.gm-star--pop.is-on:nth-child(2) { animation-delay: 160ms; }
.gm-star--pop.is-on:nth-child(3) { animation-delay: 320ms; }
@keyframes gm-star-pop { 0% { transform: scale(0) rotate(-40deg); opacity: 0; } 70% { transform: scale(1.25) rotate(8deg); opacity: 1; } 100% { transform: none; } }
.gm-result__reason { margin: 0; color: var(--text-2); font-size: var(--text-sm); line-height: var(--lh-sm); }
.gm-result__score { display: flex; justify-content: center; gap: var(--space-6); margin: 0; }
.gm-result__score div { display: flex; flex-direction: column; align-items: center; }
.gm-result__score dt { color: var(--text-3); font-size: var(--text-2xs); text-transform: uppercase; letter-spacing: .06em; }
.gm-result__score dd { margin: 0; font-size: var(--text-lg); font-weight: var(--weight-semibold); }
.gm-card [hidden] { display: none !important; }

/* ───────────── Phones ───────────── */
@media (max-width: 767px) {
  .gm-main { padding: var(--space-2) var(--space-2) max(var(--space-2), env(safe-area-inset-bottom)); }
  body[data-fw-cat="games"] .page-header.fw-page-header-host { margin-bottom: var(--space-2); }
  /* While a level runs the stage takes the whole page. */
  body.gm-playing .gm-main > .page-header { display: none; }
  .gm-levels { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .gm-card { padding: var(--space-5) var(--space-4); }
  .gm-art { width: calc(100% + 2 * var(--space-4)); margin: calc(-1 * var(--space-5)) calc(-1 * var(--space-4)) 0; aspect-ratio: 16 / 7; }
  .gm-stage { border-radius: var(--radius-sm); }
}
@media (max-width: 359px) {
  .gm-levels { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
/* Short landscape phones: keep the menu compact. */
@media (max-height: 520px) {
  .gm-card { gap: var(--space-2); padding: var(--space-4); }
  .gm-howto, .gm-art { display: none; }
}
