:root {
  --bg: #07060b;
  --red: #ff2a4d;
  --pink: #ff4fd8;
  --silver: #c8d0e0;
  --cyan: #5ef0ff;
  --panel: rgba(8, 10, 18, 0.72);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.game-root {
  position: absolute;
  top: 0;
  left: 0;
  /* JS will set exact px size from visualViewport; these are safe fallbacks */
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  background: #000;
}

/* Portrait gate */
.rotate-gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% 30%, #1a0a14, #050408 70%);
  color: var(--silver);
  padding: 24px;
  text-align: center;
}
.rotate-gate.show { display: flex; }
.rotate-card {
  max-width: 320px;
  border: 1px solid rgba(255, 42, 77, 0.45);
  background: rgba(10, 8, 16, 0.9);
  padding: 28px 22px;
}
.rotate-icon {
  font-size: 42px;
  color: var(--red);
  margin-bottom: 12px;
  animation: spin-hint 2.4s linear infinite;
}
.rotate-card h1 {
  font-size: 1.25rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  margin-bottom: 10px;
  color: #fff;
}
.rotate-card p { opacity: 0.8; line-height: 1.45; font-size: 0.95rem; }
@keyframes spin-hint {
  0% { transform: rotate(0deg); }
  30% { transform: rotate(-90deg); }
  100% { transform: rotate(-90deg); }
}

/* HUD */
.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}
.hud-top {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: start;
  padding: calc(10px + var(--safe-t)) calc(14px + var(--safe-r)) 0 calc(14px + var(--safe-l));
}
.hud-left { justify-self: start; padding-top: 48px; /* clear fullscreen/music chrome */ }
.hud-right { justify-self: end; min-width: 140px; }
.hud-center {
  justify-self: center;
  text-align: center;
  min-width: 120px;
}
.bar-block { min-width: 140px; }
.bar-label {
  font-size: 10px;
  letter-spacing: 0.22em;
  color: rgba(200, 208, 224, 0.7);
  margin-bottom: 4px;
}
.bar {
  height: 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  overflow: hidden;
}
.bar .fill {
  height: 100%;
  width: 100%;
  transform-origin: left center;
  transition: transform 0.12s linear;
}
.bar.health .fill {
  background: linear-gradient(90deg, #ff4d6a, #ffb3c0);
}
.bar.burst .fill {
  background: linear-gradient(90deg, #5a1bff, #ff4fd8 60%, #ffe08a);
}
.assist-row {
  display: flex;
  gap: 6px;
  margin-top: 8px;
  pointer-events: auto;
}
.assist-toggle {
  pointer-events: auto;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(0, 0, 0, 0.45);
  color: rgba(200, 208, 224, 0.55);
  font-size: 9px;
  letter-spacing: 0.12em;
  padding: 5px 7px;
  border-radius: 2px;
  cursor: pointer;
}
.assist-toggle.on {
  color: #9dffe8;
  border-color: rgba(94, 240, 255, 0.55);
  background: rgba(20, 60, 70, 0.55);
}

.style-rank {
  font-size: clamp(42px, 8vw, 72px);
  font-weight: 800;
  line-height: 0.9;
  letter-spacing: 0.04em;
  color: #8a93a8;
  text-shadow: 0 0 24px rgba(0, 0, 0, 0.8);
  transform: scale(1);
  transition: color 0.15s ease, transform 0.15s ease;
}
.style-rank.pop { animation: rank-pop 0.35s ease-out; }
.style-rank[data-rank="D"] { color: #8a93a8; }
.style-rank[data-rank="C"] { color: #9ad0ff; }
.style-rank[data-rank="B"] { color: #6dffb0; }
.style-rank[data-rank="A"] { color: #ffe566; }
.style-rank[data-rank="S"] { color: #ff8a3d; }
.style-rank[data-rank="SS"] { color: #ff4fd8; }
.style-rank[data-rank="SSS"] {
  color: #fff;
  background: linear-gradient(90deg, #ff4fd8, #ffe566, #5ef0ff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.style-meter {
  width: 110px;
  height: 6px;
  margin: 6px auto 0;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  overflow: hidden;
}
.style-meter .fill {
  height: 100%;
  width: 100%;
  transform-origin: left center;
  background: linear-gradient(90deg, #ff2a4d, #ff4fd8);
  transition: transform 0.1s linear;
}
@keyframes rank-pop {
  0% { transform: scale(0.4); opacity: 0.2; }
  60% { transform: scale(1.25); opacity: 1; }
  100% { transform: scale(1); }
}

.caption-banner {
  position: absolute;
  left: 50%;
  top: calc(88px + var(--safe-t));
  transform: translateX(-50%);
  width: min(720px, calc(100% - 40px));
  display: flex;
  gap: 12px;
  align-items: stretch;
  background: linear-gradient(90deg, rgba(12, 8, 18, 0.92), rgba(20, 10, 24, 0.78));
  border: 1px solid rgba(255, 79, 216, 0.35);
  padding: 10px 14px;
  pointer-events: none;
}
.caption-banner[hidden],
.prompt-toast[hidden],
.rank-flash[hidden],
.witch-vignette[hidden],
.burst-flash[hidden],
.overlay[hidden],
.stick-base[hidden] {
  display: none !important;
}
.instructor-portrait {
  width: 56px;
  height: 56px;
  object-fit: cover;
  object-position: top center;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: #111;
  flex: 0 0 auto;
}
.caption-name {
  font-size: 10px;
  letter-spacing: 0.24em;
  color: var(--pink);
  margin-bottom: 4px;
}
.caption-text {
  color: #f2f4f8;
  font-size: clamp(13px, 2.2vw, 16px);
  line-height: 1.35;
}

.prompt-toast {
  position: absolute;
  left: 50%;
  bottom: calc(150px + var(--safe-b));
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: #fff;
  padding: 8px 14px;
  font-size: 13px;
  letter-spacing: 0.06em;
  max-width: min(520px, 86vw);
  text-align: center;
}

.rank-flash {
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  font-size: clamp(64px, 16vw, 140px);
  font-weight: 900;
  letter-spacing: 0.08em;
  color: #fff;
  text-shadow: 0 0 40px rgba(255, 79, 216, 0.8);
  pointer-events: none;
  animation: flash-rank 0.7s ease-out forwards;
}
@keyframes flash-rank {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
  30% { opacity: 1; transform: translate(-50%, -50%) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1.4); }
}

.witch-vignette {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at center, transparent 35%, rgba(40, 0, 60, 0.35) 100%),
    linear-gradient(180deg, rgba(80, 0, 90, 0.15), rgba(0, 0, 0, 0.2));
  mix-blend-mode: screen;
  animation: witch-pulse 1.2s ease-in-out infinite alternate;
  pointer-events: none;
}
@keyframes witch-pulse {
  from { opacity: 0.55; }
  to { opacity: 0.9; }
}
.burst-flash {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at center, rgba(255, 79, 216, 0.45), transparent 55%);
  animation: burst-out 0.45s ease-out forwards;
  pointer-events: none;
}
@keyframes burst-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* Touch controls */
.touch-layer {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
}
.stick-zone {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 48%;
  height: 55%;
  pointer-events: auto;
  touch-action: none;
}
.stick-base {
  position: absolute;
  width: 126px;
  height: 126px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.28);
  background: rgba(0, 0, 0, 0.28);
  transform: translate(-50%, -50%);
  pointer-events: none;
}
.stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff, #c8d0e0 40%, #6a7388);
  box-shadow: 0 0 16px rgba(255, 255, 255, 0.25);
}

.action-cluster {
  position: absolute;
  right: calc(12px + var(--safe-r));
  bottom: calc(16px + var(--safe-b));
  width: 220px;
  height: 220px;
  pointer-events: none;
}
.act-btn {
  position: absolute;
  pointer-events: auto;
  border: 2px solid rgba(255, 255, 255, 0.28);
  border-radius: 50%;
  background: rgba(8, 10, 18, 0.55);
  color: #f5f7fb;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  display: grid;
  place-items: center;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.35);
}
.act-btn span { pointer-events: none; }
.act-btn:active, .act-btn.pressed {
  transform: scale(0.94);
  border-color: #fff;
  background: rgba(255, 42, 77, 0.35);
}
.act-btn.light {
  width: 86px;
  height: 86px;
  right: 58px;
  bottom: 42px;
  font-size: 11px;
  background: rgba(255, 42, 77, 0.28);
  border-color: rgba(255, 90, 120, 0.7);
}
.act-btn.heavy {
  width: 62px;
  height: 62px;
  right: 78px;
  bottom: 140px;
  background: rgba(90, 40, 160, 0.4);
}
.act-btn.gun {
  width: 62px;
  height: 62px;
  right: 150px;
  bottom: 78px;
  background: rgba(30, 80, 120, 0.45);
}
.act-btn.burst {
  width: 70px;
  height: 70px;
  right: 0;
  bottom: 78px;
  background: rgba(40, 10, 60, 0.55);
  border-color: rgba(255, 79, 216, 0.35);
  opacity: 0.45;
}
.act-btn.burst.ready {
  opacity: 1;
  border-color: #ff4fd8;
  box-shadow: 0 0 22px rgba(255, 79, 216, 0.65);
  animation: burst-glow 0.9s ease-in-out infinite alternate;
}
.act-btn.burst.locked { opacity: 0.35; }
@keyframes burst-glow {
  from { box-shadow: 0 0 12px rgba(255, 79, 216, 0.4); }
  to { box-shadow: 0 0 28px rgba(255, 79, 216, 0.9); }
}
.act-btn.jump {
  width: 52px;
  height: 52px;
  right: 96px;
  bottom: 190px;
  font-size: 9px;
}
.act-btn.dodge {
  width: 52px;
  height: 52px;
  right: 8px;
  bottom: 8px;
  font-size: 9px;
  background: rgba(20, 40, 50, 0.55);
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 50% 40%, rgba(80, 0, 40, 0.35), transparent 55%),
    rgba(0, 0, 0, 0.62);
  padding: 24px;
}
.overlay[hidden] { display: none !important; }
.overlay-panel {
  width: min(520px, 92vw);
  background: rgba(8, 8, 14, 0.92);
  border: 1px solid rgba(255, 42, 77, 0.45);
  padding: 28px 24px;
  text-align: center;
  color: #eef1f7;
}
.overlay-kicker {
  letter-spacing: 0.28em;
  font-size: 11px;
  color: var(--pink);
  margin-bottom: 10px;
}
.overlay-panel h2 {
  font-size: clamp(1.5rem, 4vw, 2rem);
  margin-bottom: 12px;
}
.overlay-body {
  color: rgba(238, 241, 247, 0.78);
  line-height: 1.45;
  margin-bottom: 18px;
}
.overlay-btn {
  pointer-events: auto;
  border: 0;
  background: linear-gradient(90deg, #ff2a4d, #ff4fd8);
  color: #fff;
  font-weight: 800;
  letter-spacing: 0.14em;
  padding: 14px 22px;
  cursor: pointer;
  min-width: 200px;
}
.overlay-btn:active { transform: scale(0.98); }
.overlay-hint {
  margin-top: 14px;
  font-size: 11px;
  color: rgba(200, 208, 224, 0.55);
  line-height: 1.4;
}
.final-rank {
  font-size: clamp(72px, 18vw, 120px);
  font-weight: 900;
  line-height: 1;
  margin: 8px 0 12px;
  color: #ffe566;
  text-shadow: 0 0 30px rgba(255, 79, 216, 0.55);
}
.final-stats {
  color: rgba(238, 241, 247, 0.7);
  margin-bottom: 10px;
  font-size: 14px;
}

/* Desktop larger cluster */
@media (min-width: 900px) {
  .action-cluster { width: 240px; height: 240px; right: calc(28px + var(--safe-r)); bottom: calc(28px + var(--safe-b)); }
  .act-btn.light { width: 94px; height: 94px; }
}

body.witch-time #game-canvas {
  filter: saturate(0.55) contrast(1.08) brightness(1.05);
}
body.burst-mode .act-btn.burst {
  border-color: #ffe566;
}

/* Top-left chrome: fullscreen + music */
.chrome-top-left {
  position: absolute;
  top: calc(10px + var(--safe-t));
  left: calc(10px + var(--safe-l));
  z-index: 40;
  display: flex;
  gap: 8px;
  pointer-events: auto;
}
.chrome-btn {
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(8, 10, 18, 0.72);
  color: #f2f4f8;
  font-size: 18px;
  line-height: 1;
  border-radius: 8px;
  cursor: pointer;
  display: grid;
  place-items: center;
  -webkit-tap-highlight-color: transparent;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
}
.chrome-btn:active { transform: scale(0.96); }
.chrome-btn[data-on="1"] {
  border-color: rgba(255, 79, 216, 0.7);
  color: #ff4fd8;
  box-shadow: 0 0 14px rgba(255, 79, 216, 0.35);
}
body.is-fullscreen .chrome-btn#btn-fullscreen,
body.immersive-play .chrome-btn#btn-fullscreen {
  border-color: rgba(94, 240, 255, 0.55);
  color: #5ef0ff;
}

/* Immersive / phone full-bleed play (works when Fullscreen API is blocked) */
html.immersive-play,
body.immersive-play {
  margin: 0 !important;
  padding: 0 !important;
  width: 100% !important;
  height: 100% !important;
  overflow: hidden !important;
  background: #000 !important;
  overscroll-behavior: none !important;
  touch-action: none !important;
}
body.immersive-play .game-root,
.game-root.immersive-play {
  position: fixed !important;
  inset: 0 !important;
  width: 100vw !important;
  width: 100dvw !important;
  height: 100vh !important;
  height: 100dvh !important;
  height: 100svh !important;
  max-width: none !important;
  max-height: none !important;
  margin: 0 !important;
  border: 0 !important;
  border-radius: 0 !important;
  z-index: 2147483000 !important;
  background: #000 !important;
  overflow: hidden !important;
}
body.immersive-play #game-canvas,
.game-root.immersive-play #game-canvas {
  width: 100% !important;
  height: 100% !important;
  object-fit: cover;
}
/* Keep chrome controls above game, safe from notches */
body.immersive-play .chrome-top-left {
  z-index: 2147483646 !important;
}
body.immersive-play .hud,
body.immersive-play .touch-layer,
body.immersive-play .overlay {
  z-index: 2147483640 !important;
}

/* Title menu */
.title-menu {
  background:
    radial-gradient(ellipse at 50% 70%, rgba(40, 8, 24, 0.55), transparent 55%),
    linear-gradient(180deg, rgba(0,0,0,0.35), rgba(0,0,0,0.78));
}
.title-shell {
  width: min(440px, 92vw);
  max-height: calc(100vh - 40px);
  max-height: calc(100dvh - 40px);
  overflow-y: auto;
  text-align: center;
  color: #eef1f7;
  padding: 0 4px;
}
.title-logo {
  font-size: clamp(1.2rem, 5vw, 2.2rem);
  letter-spacing: 0.12em;
  font-weight: 900;
  margin: 2px 0 4px;
  background: linear-gradient(90deg, #fff, #ff4fd8 55%, #ff2a4d);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.title-tag {
  color: rgba(238, 241, 247, 0.7);
  margin-bottom: 10px;
  font-size: 0.85rem;
}
.title-actions {
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.menu-btn {
  pointer-events: auto;
  width: 100%;
  text-align: left;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(8, 10, 18, 0.82);
  color: #f2f4f8;
  padding: 10px 14px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}
.menu-btn:hover:not(:disabled),
.menu-btn:focus-visible:not(:disabled) {
  border-color: rgba(255, 79, 216, 0.65);
  background: rgba(30, 12, 28, 0.9);
}
.menu-btn:active:not(:disabled) { transform: scale(0.99); }
.menu-btn.primary {
  border-color: rgba(255, 42, 77, 0.55);
  background: linear-gradient(90deg, rgba(255, 42, 77, 0.28), rgba(255, 79, 216, 0.18));
}
.menu-btn.ghost {
  margin-top: 10px;
  text-align: center;
  background: transparent;
  border-color: rgba(255,255,255,0.12);
}
.menu-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.menu-btn-label {
  display: block;
  font-weight: 800;
  letter-spacing: 0.16em;
  font-size: 0.95rem;
}
.menu-btn-sub {
  display: block;
  margin-top: 4px;
  font-size: 0.78rem;
  color: rgba(200, 208, 224, 0.65);
  letter-spacing: 0.02em;
}

/* Minimap */
.minimap {
  display: block;
  width: 118px;
  height: 118px;
  margin-top: 10px;
  margin-left: auto;
  border: 1px solid rgba(255, 79, 216, 0.4);
  background: rgba(4, 6, 12, 0.72);
  box-shadow: 0 0 18px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}
.survival-stats {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  font-size: 11px;
  letter-spacing: 0.14em;
  color: rgba(200, 208, 224, 0.85);
}
.survival-stats span {
  padding: 3px 7px;
  border: 1px solid rgba(255,255,255,0.12);
  background: rgba(0,0,0,0.35);
}

/* Upgrade shop */
.upgrade-panel {
  width: min(96vw, 560px);
  padding: 14px 12px 12px;
}
.upgrade-panel .overlay-kicker { margin-bottom: 4px; }
.upgrade-panel h2 { font-size: clamp(1.1rem, 3.5vw, 1.6rem); margin-bottom: 4px; }
.upgrade-scrap {
  color: #ffe566;
  letter-spacing: 0.16em;
  font-weight: 700;
  margin-bottom: 10px;
  font-size: 0.9rem;
}
.upgrade-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  margin-bottom: 10px;
  text-align: left;
}
.upgrade-card {
  pointer-events: auto;
  border: 1px solid rgba(255,255,255,0.14);
  background: rgba(0,0,0,0.4);
  color: #eef1f7;
  padding: 9px 8px 8px;
  cursor: pointer;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  text-align: left;
}
.upgrade-card:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}
.upgrade-card:not(:disabled):active,
.upgrade-card:not(:disabled):hover {
  border-color: rgba(255, 79, 216, 0.65);
  background: rgba(30,8,24,0.55);
}
.upgrade-card .u-name {
  font-weight: 800;
  letter-spacing: 0.06em;
  font-size: 10px;
  line-height: 1.2;
  text-transform: uppercase;
}
.upgrade-card .u-desc {
  font-size: 10px;
  color: rgba(200,208,224,0.7);
  line-height: 1.25;
  flex: 1;
}
.upgrade-card .u-cost {
  font-size: 10px;
  color: #ffe566;
  letter-spacing: 0.08em;
  font-weight: 700;
}
.upgrade-card .u-lvl {
  float: right;
  color: var(--cyan);
  font-size: 9px;
  margin-bottom: 2px;
}
.upgrade-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
}
.upgrade-actions .overlay-btn {
  flex: 1;
  max-width: 200px;
  padding: 11px 12px;
  min-width: 0;
  font-size: 0.8rem;
}
.upgrade-actions .menu-btn.ghost {
  flex: 1;
  max-width: 200px;
  padding: 10px 12px;
  margin-top: 0;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  text-align: center;
}

.hud[hidden], .touch-layer[hidden] { display: none !important; }
