/* ==========================================================================
   Game host page
   ========================================================================== */

.game-page {
  --header-h: 64px;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  color: #fff;
  background:
    radial-gradient(900px 520px at 50% -10%, rgba(255, 143, 196, 0.28), transparent 65%),
    radial-gradient(700px 420px at 100% 100%, rgba(150, 112, 255, 0.2), transparent 70%),
    #1d1024;
}

.game-shell {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- Header ---------- */

.game-header {
  position: relative;
  z-index: 20;
  flex: none;
  height: var(--header-h);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: 0 18px;
  padding-top: env(safe-area-inset-top);
  background: rgba(23, 12, 28, 0.72);
  border-bottom: 1px solid rgba(255, 197, 226, 0.14);
  backdrop-filter: blur(14px);
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  width: fit-content;
  min-height: 40px;
  padding: 0.35rem 0.75rem 0.35rem 0.55rem;
  border-radius: 999px;
  color: rgba(255, 239, 247, 0.86);
  font-family: var(--font-display);
  font-size: 0.92rem;
  font-weight: 500;
  text-decoration: none;
  transition: background-color 160ms ease, color 160ms ease;
}

.back-link:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.back-link:focus-visible,
.icon-btn:focus-visible {
  outline-color: #B79CF6;
}

.game-title-wrap {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
  justify-self: center;
}

.game-dot {
  width: 9px;
  height: 9px;
  flex: none;
  border-radius: 50%;
  background: #ff8fc4;
  box-shadow: 0 0 0 4px rgba(255, 143, 196, 0.16);
}

.game-title-wrap h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: #fff3f9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.game-subtitle {
  padding: 0.18rem 0.55rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 201, 227, 0.16);
  color: rgba(255, 217, 235, 0.72);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  justify-self: end;
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid rgba(255, 201, 227, 0.24);
  background: rgba(255, 255, 255, 0.06);
  color: #ffeaf4;
  transition: background-color 160ms ease, transform 160ms ease;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.14);
  transform: translateY(-1px);
}

.icon-btn .icon-exit {
  display: none;
}

.icon-btn[aria-pressed="true"] .icon-enter {
  display: none;
}

.icon-btn[aria-pressed="true"] .icon-exit {
  display: block;
}

/* ---------- Stage & canvas ---------- */

.game-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px 20px 14px;
  /* Wadah query: canvas mengisi ruang yang benar-benar tersedia. */
  container-type: size;
}

.canvas-wrap {
  position: relative;
  /* Fallback untuk browser tanpa container query units. */
  width: min(100%, calc((100dvh - 150px) * 16 / 9));
  max-height: calc((100dvh - 150px));
  aspect-ratio: 16 / 9;
  flex: none;
  overflow: hidden;
  border-radius: 22px;
  border: 3px solid rgba(255, 255, 255, 0.16);
  background: #2b1630;
  contain: paint;
  box-shadow:
    0 40px 90px -30px rgba(0, 0, 0, 0.75),
    0 0 0 1px rgba(255, 143, 196, 0.15),
    0 0 70px -20px rgba(255, 143, 196, 0.28);
}

.canvas-box {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: #2b1630;
}

@supports (width: 1cqh) {
  .canvas-wrap {
    width: min(100cqw, calc((100cqh - var(--hint-space, 40px)) * 16 / 9));
    max-height: calc(100cqh - var(--hint-space, 40px));
  }
}

/* Vignette statis: jauh lebih murah daripada gradient layar penuh per frame. */
.canvas-box::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  background: radial-gradient(
    ellipse at center,
    rgba(52, 20, 50, 0) 45%,
    rgba(52, 20, 50, 0.18) 100%
  );
}

#gameCanvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  outline: none;
  cursor: default;
  image-rendering: auto;
}

#gameCanvas:focus-visible {
  outline: 3px solid #b79cf6;
  outline-offset: -3px;
}

.game-hint {
  font-family: var(--font-display);
  font-size: 0.8rem;
  font-weight: 400;
  letter-spacing: 0.02em;
  color: rgba(255, 226, 240, 0.55);
  text-align: center;
}

/* ---------- Touch controls ---------- */

.touch-controls {
  display: none;
  position: absolute;
  inset: auto 0 0 0;
  padding: 0;
  pointer-events: none;
  z-index: 10;
  --touch-gap: 12px;
  --touch-left-inset: max(16px, env(safe-area-inset-left));
  --touch-right-inset: max(18px, env(safe-area-inset-right));
}

body.has-touch[data-screen="playing"] .touch-controls {
  display: block;
}

.touch-btn {
  position: absolute;
  bottom: calc(16px + env(safe-area-inset-bottom));
  display: grid;
  place-items: center;
  width: 76px;
  height: 76px;
  padding: 0;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 240, 248, 0.55);
  background: linear-gradient(180deg, rgba(92, 45, 82, 0.88), rgba(44, 18, 42, 0.94));
  color: #fff2f9;
  box-shadow:
    0 14px 26px -12px rgba(0, 0, 0, 0.78),
    inset 0 1px 0 rgba(255, 255, 255, 0.22);
  /* Tanpa backdrop-filter: blur di atas canvas yang beranimasi mahal di GPU
     ponsel dan membuat input terasa terlambat. */
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 80ms ease, background 80ms ease, box-shadow 80ms ease;
}

/* Area sentuh lebih luas daripada lingkaran visual: tap yang sedikit meleset
   tetap terdaftar, terutama saat jempol bergerak cepat. */
.touch-btn::before {
  content: '';
  position: absolute;
  inset: -12px;
  border-radius: 50%;
}

.touch-btn svg {
  pointer-events: none;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.35));
}

.touch-btn:active,
.touch-btn.is-active {
  transform: scale(0.93);
  border-color: rgba(255, 226, 240, 0.95);
  background: linear-gradient(180deg, rgba(255, 155, 203, 0.95), rgba(206, 66, 136, 0.95));
  box-shadow:
    0 6px 14px -10px rgba(0, 0, 0, 0.8),
    inset 0 0 0 3px rgba(255, 255, 255, 0.2);
}

.touch-left {
  left: var(--touch-left-inset);
}

.touch-right {
  left: calc(var(--touch-left-inset) + 76px + var(--touch-gap));
}

.touch-jump {
  right: var(--touch-right-inset);
  width: 92px;
  height: 92px;
  bottom: calc(14px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: linear-gradient(180deg, rgba(255, 148, 198, 0.96), rgba(203, 58, 129, 0.96));
  border-color: rgba(255, 236, 245, 0.92);
  box-shadow:
    0 18px 30px -14px rgba(255, 92, 160, 0.85),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.touch-jump svg {
  width: 32px;
  height: 32px;
}

.touch-jump span {
  font-family: var(--font-display);
  font-size: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  pointer-events: none;
}

/* ---------- Message/error overlay ---------- */

.game-message {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 2rem;
  background: rgba(31, 15, 37, 0.86);
  color: #fff4fa;
  text-align: center;
  font-family: var(--font-display);
  font-size: 1.1rem;
}

.game-message[hidden] {
  display: none;
}

.game-message a {
  color: #ffb7da;
}

/* ---------- Fullscreen ---------- */

.canvas-wrap:fullscreen,
.canvas-wrap:-webkit-full-screen {
  width: 100vw;
  height: 100vh;
  max-height: none;
  aspect-ratio: auto;
  border-radius: 0;
  border: 0;
}

.canvas-wrap:fullscreen .canvas-box,
.canvas-wrap:-webkit-full-screen .canvas-box {
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-wrap:fullscreen #gameCanvas,
.canvas-wrap:-webkit-full-screen #gameCanvas {
  width: min(100vw, 177.7778vh);
  height: min(100vh, 56.25vw);
  flex: none;
}

.canvas-wrap:fullscreen .touch-controls,
.canvas-wrap:-webkit-full-screen .touch-controls {
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
}

/* Fallback untuk browser yang tidak mendukung Fullscreen API (mis. iOS). */
.canvas-wrap.is-pseudo-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 100;
  width: 100vw;
  height: 100dvh;
  max-height: none;
  aspect-ratio: auto;
  border-radius: 0;
  border: 0;
  background: #1b0f22;
}

.canvas-wrap.is-pseudo-fullscreen .canvas-box {
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-wrap.is-pseudo-fullscreen #gameCanvas {
  width: min(100vw, 177.7778dvh);
  height: min(100dvh, 56.25vw);
  flex: none;
}

.canvas-wrap.is-pseudo-fullscreen .touch-controls {
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
}

/* ---------- Responsive ---------- */

@media (max-width: 760px) {
  .game-page {
    --header-h: 56px;
  }

  .game-header {
    grid-template-columns: auto 1fr auto;
    gap: 0.5rem;
    padding: 0 10px;
  }

  .back-link {
    padding: 0.3rem 0.6rem 0.3rem 0.4rem;
    font-size: 0.82rem;
  }

  .game-subtitle {
    display: none;
  }

  .game-title-wrap {
    justify-self: start;
    padding-left: 0.3rem;
  }

  .game-title-wrap h1 {
    font-size: 1rem;
  }

  .game-title-wrap .game-dot {
    width: 7px;
    height: 7px;
  }

  .game-stage {
    padding: 8px 10px 10px;
    gap: 8px;
    --hint-space: 0px;
  }

  .canvas-wrap {
    border-radius: 16px;
    border-width: 2px;
  }

  .game-hint {
    display: none;
  }
}

@media (max-width: 440px) {
  .back-link span {
    display: none;
  }

  .back-link {
    width: 40px;
    height: 40px;
    min-height: 0;
    justify-content: center;
    padding: 0;
    border-radius: 12px;
  }

  .touch-btn {
    width: 68px;
    height: 68px;
  }

  .touch-right {
    left: calc(var(--touch-left-inset) + 68px + var(--touch-gap));
  }

  .touch-jump {
    width: 84px;
    height: 84px;
    right: var(--touch-right-inset);
  }

  .touch-jump span {
    display: none;
  }
}

/* Ponsel landscape: ruang vertikal sempit, jadi kontrol dipadatkan. */
@media (max-height: 420px) and (orientation: landscape) {
  .game-hint {
    display: none;
  }

  .game-page {
    --header-h: 48px;
  }

  .game-header {
    height: 48px;
  }

  .game-stage {
    padding: 4px 8px;
    --hint-space: 0px;
  }

  .touch-btn {
    width: 64px;
    height: 64px;
    bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .touch-right {
    left: calc(var(--touch-left-inset) + 64px + var(--touch-gap));
  }

  .touch-jump {
    width: 80px;
    height: 80px;
  }

  .touch-jump span {
    display: none;
  }
}

/* Saat bermain di perangkat sentuh, header disembunyikan agar canvas
   memakai seluruh layar (keluar lewat menu jeda). */
@media (pointer: coarse) {
  .game-page.in-play .game-header {
    display: none;
  }
}

/* ==========================================================================
   Ponsel portrait: seluruh shell game diputar 90° supaya tampil landscape.
   Begitu perangkat benar-benar diputar, media query berhenti berlaku dan
   layout kembali normal tanpa lompatan. Saat bermain, header disembunyikan
   agar area canvas memakai seluruh layar.
   ========================================================================== */

@media (orientation: portrait) and (pointer: coarse) {
  .game-page:not(.no-rotate) .game-shell {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 50;
    width: 100dvh;
    height: 100vw;
    transform-origin: top left;
    transform: rotate(90deg) translateY(-100%);
    background: #1d1024;
  }

  .game-page.in-play .touch-controls {
    --touch-left-inset: max(22px, env(safe-area-inset-left));
    --touch-right-inset: max(22px, env(safe-area-inset-right));
  }
}
