/* Tenpai title screen, animated in CSS (issue #72). A phone showing the app's home screen idling: the swirl, the
   drifting tiles, the bobbing TENPAI letters and the floating chun tile, with the timings of
   MahjongApp/Scenes/TitleScene.swift and the swirl of MahjongApp/Rendering/Shaders.swift.

   All art in img/hero/ comes from tools/gen_site_hero.py, which renders it from the app's Swift sources and
   rewrites the GENERATED block at the end of this file (positions and sizes). Rerun it when the title screen,
   the tiles, the font or the shader change.

   Markup: see site/lab/hero.html. No scripts and no inline styles (CSP in site/_headers).
   Size: set --sw (screen width) on .th-phone; everything else follows. The screen is an iPhone 17, 402 x 874 pt
   (--wpt x --hpt, from the generator), the device site/img/title.jpg was captured on.

   Units, all derived from --sw:
     --pt  one app point (screen width / --wpt)
     --lp, --tp, --bp, --up, --rp  one art pixel of the logo, hero tile, buttons, footer, drift tiles
     --cp  one cell of the swirl (4 pt)
   Where the browser supports round(), group positions snap to device pixels. Art pixel sizes are not rounded:
   rounding them breaks the app's proportions (the button column grows into the footer at some widths), and at
   DPR 2-3 the at-most-one-device-pixel unevenness doesn't show. */

.th-phone {
  --sw: 322px;
  position: relative;
  display: inline-block;
  vertical-align: top;
  padding: 6px;
  background: #000;
  border: 3px solid #17111f;
  border-radius: calc(var(--sw) * 0.137 + 9px);
  box-shadow: 8px 8px 0 #0a0710;
  line-height: 0;
}

/* Dynamic Island: 125 x 36 pt, 11 pt below the top edge, centered (measured on site/img/title.jpg). */
.th-phone::after {
  content: "";
  position: absolute;
  z-index: 3;
  left: 50%;
  top: calc(9px + var(--sw) * 11 / var(--wpt));
  width: calc(var(--sw) * 125 / var(--wpt));
  height: calc(var(--sw) * 36 / var(--wpt));
  translate: -50% 0;
  border-radius: 999px;
  background: #000;
}

.th-screen {
  --step: 1px;
  --pt: calc(var(--sw) / var(--wpt));
  --lp: calc(var(--pt) * var(--lpk) / 3);
  --tp: calc(var(--pt) * var(--tpk) / 3);
  --bp: calc(var(--pt) * var(--bpk) / 3);
  --up: calc(var(--pt) * var(--upk) / 3);
  --rp: calc(var(--pt) * var(--rpk) / 3);
  --cp: calc(var(--pt) * var(--cell));
  --line: max(1px, var(--pt));
  position: relative;
  width: var(--sw);
  height: calc(var(--sw) * var(--hpt) / var(--wpt));
  overflow: hidden;
  border-radius: calc(var(--sw) * 0.137);
  background: #113b3c; /* feltDark, under the swirl */
  isolation: isolate;
  contain: strict;
}
@media (min-resolution: 2dppx) { .th-screen { --step: 0.5px; } }
@media (min-resolution: 3dppx) { .th-screen { --step: calc(1px / 3); } }

.th-screen img {
  display: block;
  image-rendering: pixelated;
  user-select: none;
  -webkit-user-drag: none;
}

/* A group (logo, hero tile, footer) centered on (--x, --y) in points, --w x --h art pixels of size --a. */
.th-logo, .th-hero, .th-footer, .th-buttons {
  position: absolute;
  width: calc(var(--w) * var(--a));
  height: calc(var(--h) * var(--a));
  left: calc(var(--x) * var(--pt) - var(--w) * var(--a) / 2);
  top: calc(var(--y) * var(--pt) - var(--h) * var(--a) / 2);
}

/* Swirl: the shader rendered one image pixel per 4 pt cell, 96 frames 1/8 s apart in a 12 x 8 sheet
   (serpentine rows). Two stepped animations pick the column and the row; both alternate, so the swirl runs 12 s
   forward then 12 s back. Frame 0 is the app's Reduce Motion pose. Anchored bottom-left like the shader's cells. */
.th-swirl {
  position: absolute;
  z-index: 0;
  left: 0;
  bottom: 0;
  width: calc(var(--cols) * var(--cp));
  height: calc(var(--rows) * var(--cp));
  background: url("img/hero/swirl.png") 0 0 / calc(var(--fc) * 100%) calc(var(--fr) * 100%) no-repeat;
  image-rendering: pixelated;
  animation:
    th-swirl-x var(--row-dur) steps(var(--fc), jump-none) infinite alternate,
    th-swirl-y var(--sheet-dur) steps(var(--fr), jump-none) infinite alternate;
}
@keyframes th-swirl-x { from { background-position-x: 0%; } to { background-position-x: 100%; } }
@keyframes th-swirl-y { from { background-position-y: 0%; } to { background-position-y: 100%; } }

/* Drift tiles: rise from 60 pt below the screen to 60 pt above it, sway 20 pt, turn 2 rad per pass, 22% opaque.
   .th-dt is a column (screen height + 120 pt) tall that moves up by its own height. */
.th-drift { position: absolute; z-index: 1; inset: 0; }
.th-dt {
  --a: var(--rp);
  position: absolute;
  left: calc(var(--sx) * var(--pt) - 9 * var(--a));
  top: calc((var(--hpt) + 60) * var(--pt) - 12 * var(--a));
  width: calc(18 * var(--a));
  height: calc((var(--hpt) + 120) * var(--pt));
  animation: th-rise var(--dur) linear var(--rise) infinite;
}
.th-dt img {
  width: calc(18 * var(--a));
  height: calc(24 * var(--a));
  opacity: 0.22;
  transform: rotate(var(--r0));
  animation:
    th-sway var(--dur) var(--sway) infinite,
    th-spin var(--dur) linear var(--rise) infinite;
}
@keyframes th-rise { to { transform: translateY(-100%); } }
/* 20 pt * sin(2 pi t): 20 pt is 83.3% of an 18 art px tile at 4/3 pt per art px. Sine easing between extremes. */
@keyframes th-sway {
  0% { translate: 0 0; animation-timing-function: cubic-bezier(0.61, 1, 0.88, 1); }
  25% { translate: 83.33% 0; animation-timing-function: cubic-bezier(0.37, 0, 0.63, 1); }
  75% { translate: -83.33% 0; animation-timing-function: cubic-bezier(0.12, 0, 0.39, 0); }
  100% { translate: 0 0; }
}
@keyframes th-spin { from { rotate: 0deg; } to { rotate: -114.59deg; } }

/* Hero tile: tilts +-0.08 rad (1.1 s each way, linear) and bobs 4 art px (0.9 s each way, ease in-out);
   its shadow sits 4 right and 5 down and stays still. */
.th-hero { --a: var(--tp); --w: 18; --h: 24; z-index: 10; }
.th-hero-shadow {
  position: absolute;
  left: calc(4 * var(--a));
  top: calc(5 * var(--a));
  width: 100%;
  height: 100%;
}
.th-hero-bob {
  position: absolute;
  inset: 0;
  animation: th-bob 0.9s ease-in-out infinite alternate;
}
.th-chun {
  width: 100%;
  height: 100%;
  animation: th-tilt 1.1s linear -1.65s infinite alternate;
}
@keyframes th-tilt { from { rotate: -4.584deg; } to { rotate: 4.584deg; } }
/* 4 of 24 art px for the tile, 2 of 12 for a letter: both 16.67% of the element's own height. */
@keyframes th-bob { to { transform: translateY(-16.667%); } }

/* Logo: one image per letter, bobbing 2 art px (0.6 s each way) with a 0.12 s stagger; each letter's shadow
   (60% opaque, 3 art px lower) stays still. */
.th-logo { --a: var(--lp); z-index: 20; }
.th-letter {
  position: absolute;
  top: 0;
  left: calc(var(--lx) * var(--a));
  width: calc(var(--lw) * var(--a));
  height: 100%;
}
.th-ls {
  position: absolute;
  left: 0;
  top: calc(3.5 * var(--a));
  width: 100%;
  height: calc(11 * var(--a));
  opacity: 0.6;
}
.th-lf {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  animation: th-bob 0.6s ease-in-out var(--delay) infinite alternate;
}

/* Buttons (static): New Game, How to Play, Settings; each image is the body plus its 4 px of depth and shadow. */
.th-buttons { --a: var(--bp); --h: 0; --y: var(--top); z-index: 30; }
.th-btn {
  position: absolute;
  left: 0;
  width: 100%;
  height: calc((var(--bh) + 4) * var(--a));
}
.th-b0 { top: 0; }
.th-b1 { top: calc((var(--bh) + var(--gap)) * var(--a)); }
.th-b2 { top: calc(2 * (var(--bh) + var(--gap)) * var(--a)); }

.th-footer { --a: var(--up); z-index: 30; }

/* CRT: 1 pt scanlines at 16% black and the soft vignette of ShaderSource.crt (smoothstep 0.38..0.85 to 55%). */
.th-screen::after {
  content: "";
  position: absolute;
  z-index: 50;
  inset: 0;
  pointer-events: none;
  background:
    repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0) 0 var(--line), rgba(0, 0, 0, 0.16) var(--line) calc(2 * var(--line))),
    radial-gradient(ellipse 85% calc(85% / 1.15) at 50% 50%,
      rgba(0, 0, 0, 0) 44.7%, rgba(0, 0, 0, 0.086) 58.5%, rgba(0, 0, 0, 0.275) 72.4%,
      rgba(0, 0, 0, 0.464) 86.2%, rgba(0, 0, 0, 0.55) 100%);
}

/* Snap positions, the swirl cell and the scanline to device pixels where round() exists. */
@supports (width: round(nearest, 1.3px, 1px)) {
  .th-screen {
    --cp: round(up, calc(var(--pt) * var(--cell)), var(--step));
    --line: max(var(--step), round(nearest, var(--pt), var(--step)));
  }
  .th-logo, .th-hero, .th-footer, .th-buttons {
    left: round(nearest, calc(var(--x) * var(--pt) - var(--w) * var(--a) / 2), var(--step));
    top: round(nearest, calc(var(--y) * var(--pt) - var(--h) * var(--a) / 2), var(--step));
  }
  .th-dt { left: round(nearest, calc(var(--sx) * var(--pt) - 9 * var(--a)), var(--step)); }
}

/* Reduce Motion: hold a still pose (swirl frame 0, letters and tile at rest, drift tiles where they are). */
@media (prefers-reduced-motion: reduce) {
  .th-screen, .th-screen * { animation-play-state: paused !important; }
}

/* Full-bleed variant: no frame, the screen as large as the viewport allows. */
.th-phone.th-bleed {
  --sw: min(100vw, 100vh * 402 / 874);
  padding: 0;
  border: 0;
  border-radius: 0;
  box-shadow: none;
}
@supports (height: 100svh) {
  .th-phone.th-bleed { --sw: min(100vw, 100svh * 402 / 874); }
}
.th-bleed .th-screen { border-radius: 0; }
.th-bleed::after { display: none; }

/* Preview page only (site/lab/hero.html); safe to drop when the hero moves into style.css. */
.th-lab {
  margin: 0;
  background: #19222a;
  color: #f3e9d2;
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
.th-lab-framed { padding: 40px 16px 64px; text-align: center; }
.th-lab-framed p { color: #a7b3c4; max-width: 34em; margin: 0 auto 28px; }
.th-lab-bleed {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #0a0710;
}

/* BEGIN GENERATED */
/* Generated by tools/gen_site_hero.py: iPhone 17, 402 x 874 pt at 3x, device px per art px: logo 20, tile 15, buttons 8, ui 4, drift 4. */
.th-phone { --wpt: 402; --hpt: 874; }
.th-screen { --lpk: 20; --tpk: 15; --bpk: 8; --upk: 4; --rpk: 4; --cell: 4; }
.th-logo { --x: 201.00; --y: 362.61; --w: 51; --h: 12; }
.th-l0 { --lx: 0; --lw: 8; --delay: 0.00s; }
.th-l1 { --lx: 9; --lw: 8; --delay: 0.12s; }
.th-l2 { --lx: 18; --lw: 8; --delay: 0.24s; }
.th-l3 { --lx: 27; --lw: 8; --delay: 0.36s; }
.th-l4 { --lx: 36; --lw: 8; --delay: 0.48s; }
.th-l5 { --lx: 45; --lw: 6; --delay: 0.60s; }
.th-hero { --x: 201.00; --y: 189.28; }
.th-buttons { --x: 201.00; --top: 581.53; --w: 87; --bh: 17; --gap: 5; }
.th-footer { --x: 201.00; --y: 826.00; --w: 151; --h: 10; }
.th-d0 { --sx: 0.00; --dur: 14s; --rise: -12.000s; --sway: -12.000s; --r0: -0.00deg; }
.th-d1 { --sx: 389.94; --dur: 17s; --rise: -4.700s; --sway: -7.406s; --r0: -22.92deg; }
.th-d2 { --sx: 377.88; --dur: 20s; --rise: -17.400s; --sway: -3.766s; --r0: -45.84deg; }
.th-d3 { --sx: 365.82; --dur: 23s; --rise: -13.100s; --sway: -1.082s; --r0: -68.75deg; }
.th-d4 { --sx: 353.76; --dur: 26s; --rise: -8.800s; --sway: -25.352s; --r0: -91.67deg; }
.th-d5 { --sx: 341.70; --dur: 14s; --rise: -5.500s; --sway: -2.641s; --r0: -114.59deg; }
.th-d6 { --sx: 329.64; --dur: 17s; --rise: -15.200s; --sway: -14.434s; --r0: -137.51deg; }
.th-d7 { --sx: 317.58; --dur: 20s; --rise: -10.900s; --sway: -13.182s; --r0: -160.43deg; }
.th-d8 { --sx: 305.52; --dur: 23s; --rise: -6.600s; --sway: -12.885s; --r0: -183.35deg; }
.th-d9 { --sx: 293.46; --dur: 26s; --rise: -2.300s; --sway: -13.542s; --r0: -206.26deg; }
.th-d10 { --sx: 281.40; --dur: 14s; --rise: -13.000s; --sway: -7.282s; --r0: -229.18deg; }
.th-d11 { --sx: 269.34; --dur: 17s; --rise: -8.700s; --sway: -4.462s; --r0: -252.10deg; }
.th-swirl { --cols: 101; --rows: 219; --fc: 12; --fr: 8; --row-dur: 1.500s; --sheet-dur: 12.000s; }
/* END GENERATED */
