/**
 * BreakPlay Mobile Responsiveness Layer
 * Injected into all games — fixes canvas scaling, overflow, touch, layout
 */

/* ── Global resets ── */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  overflow-wrap: anywhere;
}

body {
  margin: 0;
}

/* ── Universal canvas scaling ── */
canvas {
  max-width: 100% !important;
  display: block;
}

/* ── Game containers responsive ── */
.game-container,
.game-wrapper,
.canvas-wrapper,
.game-board-wrap,
.board-wrap,
[class*="game-container"],
[class*="game-wrap"] {
  max-width: 100vw !important;
  overflow-x: hidden !important;
}

/* ── Responsive score panels ── */
.game-info,
.game-header,
.score-bar,
.hud,
[class*="score-board"],
[class*="score_board"] {
  flex-wrap: wrap !important;
  gap: 6px !important;
}

/* ── Mobile viewport fix for games with body overflow:hidden ── */
@media (max-width: 640px) {
  body { overflow-x: hidden !important; }
  
  /* Auto-scale any large fixed-width containers (inline styles) */
  [style*="width: 800"],
  [style*="width:800"],
  [style*="width: 600"],
  [style*="width:600"],
  [style*="width: 560"],
  [style*="width:560"],
  [style*="width: 520"],
  [style*="width:520"] {
    width: 100% !important;
    max-width: 100% !important;
  }
  
  /* Universal containment — prevent ANY element from exceeding viewport */
  .container,
  .game-board,
  .board,
  #game,
  #board,
  #game-container,
  #game-board,
  #gameArea,
  #game-area,
  #grid-container,
  #tile-container,
  .grid-container,
  .tile-container,
  .puzzle-container,
  .card-container,
  .cards-grid,
  .rpg-container,
  .game-container,
  .game-wrapper,
  main,
  section {
    max-width: 100vw !important;
    overflow-x: hidden;
  }

  /* Scale down canvas on mobile */
  canvas {
    max-width: calc(100vw - 16px) !important;
  }
  
  /* Ensure game title doesn't overflow */
  h1, h2 {
    font-size: clamp(1rem, 5vw, 2rem) !important;
    white-space: normal;
    overflow-wrap: anywhere;
  }
}

/* ── Shared mobile game controls bar ── */
#bp-mobile-bar {
  display: none;
}

@media (hover: none) and (pointer: coarse), (max-width: 650px) {
  #bp-mobile-bar {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 9990;
    background: rgba(5, 8, 22, 0.96);
    backdrop-filter: blur(16px);
    border-top: 1px solid rgba(0, 212, 255, 0.15);
    padding: 8px 12px 12px;
    gap: 8px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  
  /* Push game content above mobile bar */
  body {
    padding-bottom: 80px !important;
  }
}

/* ── Mobile button style ── */
.bp-mob-btn {
  font-family: Orbitron, system-ui, sans-serif;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 1px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid rgba(0, 212, 255, 0.4);
  background: rgba(0, 212, 255, 0.1);
  color: #00d4ff;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: manipulation;
  transition: background 0.15s, transform 0.1s;
  min-width: 52px;
  text-align: center;
}

.bp-mob-btn:active {
  background: rgba(0, 212, 255, 0.3);
  transform: scale(0.92);
}

.bp-mob-btn-danger {
  border-color: rgba(255, 68, 68, 0.4) !important;
  background: rgba(255, 68, 68, 0.1) !important;
  color: #ff4444 !important;
}

.bp-mob-btn-danger:active {
  background: rgba(255, 68, 68, 0.3) !important;
}

/* ── Pre-start guide: shown before play, removed after Start/Skip ── */
.bp-prestart-overlay {
  position: fixed;
  inset: 0;
  z-index: 99980;
  display: grid;
  place-items: center;
  padding: max(18px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: rgba(5, 8, 22, 0.86);
  backdrop-filter: blur(16px);
}

.bp-prestart-card {
  width: min(520px, 100%);
  max-height: min(82vh, 620px);
  overflow: auto;
  border: 1px solid rgba(0, 212, 255, 0.34);
  border-radius: 14px;
  background: rgba(8, 12, 25, 0.96);
  box-shadow: 0 28px 100px rgba(0, 0, 0, 0.56);
  color: #f8fafc;
  padding: clamp(18px, 5vw, 28px);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.bp-prestart-kicker {
  margin: 0 0 8px;
  color: #00d4ff;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.bp-prestart-card h2 {
  margin: 0 0 10px;
  color: #ffffff;
  font-size: clamp(1.35rem, 7vw, 2rem) !important;
  line-height: 1.05;
  overflow-wrap: anywhere;
}

.bp-prestart-card p {
  margin: 0 0 12px;
  color: rgba(248, 250, 252, 0.78);
  line-height: 1.55;
}

.bp-prestart-card ul {
  display: grid;
  gap: 8px;
  margin: 0 0 18px;
  padding-left: 18px;
  color: rgba(248, 250, 252, 0.72);
  line-height: 1.45;
}

.bp-prestart-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.bp-prestart-actions button {
  min-height: 46px;
  min-width: 112px;
  border-radius: 10px;
  padding: 10px 16px;
  border: 1px solid rgba(0, 212, 255, 0.4);
  font: 800 0.95rem/1 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.bp-prestart-primary {
  background: #00d4ff;
  color: #050816;
}

.bp-prestart-skip {
  background: rgba(255, 255, 255, 0.06);
  color: #f8fafc;
}

.bp-game-started .breakplay-seo-panel,
.bp-game-started .bp-guide-wrap {
  display: none !important;
}

/* ── Haptic feedback helper (via CSS animation) ── */
@keyframes bp-haptic {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(0.95); }
}

.bp-haptic {
  animation: bp-haptic 0.15s ease;
}

/* ── Prevent double-tap zoom on buttons ── */
button, a, [role="button"] {
  touch-action: manipulation;
}

summary {
  display: flex;
  align-items: center;
  min-height: 44px;
}

.breakplay-seo-panel details:not([open]) {
  max-height: 48px !important;
}

.breakplay-seo-panel summary {
  display: flex !important;
  align-items: center !important;
  min-height: 44px !important;
  padding-block: 8px !important;
}

/* ── Fix SEO panel on small screens ── */
@media (max-width: 640px) {
  .breakplay-seo-panel {
    top: 50px !important;
    width: calc(100vw - 16px) !important;
    max-height: 30vh !important;
    font-size: 11px !important;
    left: 8px !important;
  }

}

/* ── Responsive D-pad ── */
.bp-dpad {
  display: grid;
  grid-template-areas: ". up ." "left center right" ". down .";
  gap: 4px;
  width: fit-content;
  margin: 0 auto;
}

.bp-dpad-up    { grid-area: up; }
.bp-dpad-left  { grid-area: left; }
.bp-dpad-right { grid-area: right; }
.bp-dpad-down  { grid-area: down; }
.bp-dpad-ctr   { grid-area: center; }

/* ── Orientation warning for portrait-only games ── */
#bp-rotate-hint {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(5, 8, 22, 0.95);
  z-index: 99999;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  font-family: Orbitron, system-ui, sans-serif;
  color: #00d4ff;
  text-align: center;
  padding: 24px;
}

#bp-rotate-hint .rotate-icon {
  font-size: 3rem;
  animation: rotateAnim 2s ease-in-out infinite;
}

@keyframes rotateAnim {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(90deg); }
}

/* ── Swipe indicator for mobile ── */
.bp-swipe-hint {
  font-family: Orbitron, system-ui, sans-serif;
  font-size: 0.6rem;
  color: rgba(0, 212, 255, 0.4);
  text-align: center;
  letter-spacing: 2px;
  padding: 4px;
  animation: fadeHint 3s ease forwards;
}

@keyframes fadeHint {
  0%, 60% { opacity: 1; }
  100% { opacity: 0; }
}

/* ── Fix bp-engage footer on mobile ── */
@media (max-width: 640px) {
  #bp-engage-footer {
    padding: 10px 8px 80px !important;
    gap: 8px !important;
  }
  
  #bp-engage-footer .bp-ef-streak,
  #bp-engage-footer #bp-share-btn,
  #bp-engage-footer .bp-ef-next {
    font-size: 0.58rem !important;
    padding: 8px 10px !important;
  }
}

/* ── Game-over overlay responsive ── */
@media (max-width: 480px) {
  #bp-go-overlay {
    padding: 16px !important;
  }
  
  #bp-go-overlay .bp-go-title {
    font-size: 1.8rem !important;
    letter-spacing: 2px !important;
  }
  
  #bp-go-overlay .bp-go-btn {
    padding: 10px 16px !important;
    font-size: 0.65rem !important;
  }
}

/* ── Smooth scrolling ── */
html {
  scroll-behavior: smooth;
}

/* ── iOS safe area ── */
@supports (padding: max(0px)) {
  #bp-nav {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }
}

/* ── Production mobile hardening layer ── */
:root {
  --bp-vh: 1vh;
  --bp-vw: 1vw;
  --bp-gamepad-height: 0px;
}

html,
body {
  width: 100%;
  max-width: 100%;
  overscroll-behavior-x: none;
}

body {
  min-height: calc(var(--bp-vh, 1vh) * 100);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

img,
svg,
video,
iframe {
  max-width: 100%;
}

iframe,
embed,
object {
  border: 0;
}

canvas:not([data-noscale]) {
  max-inline-size: min(100%, calc(100vw - 16px)) !important;
  max-block-size: calc((var(--bp-vh, 1vh) * 100) - var(--bp-gamepad-height, 0px) - 16px);
  block-size: auto;
  object-fit: contain;
  image-rendering: auto;
  touch-action: none;
}

canvas[data-bp-fullscreen="true"],
canvas#glcanvas,
canvas#c,
canvas[style*="position:absolute"],
canvas[style*="position: absolute"],
canvas[style*="position:fixed"],
canvas[style*="position: fixed"] {
  inline-size: 100% !important;
  block-size: 100% !important;
  max-inline-size: 100% !important;
  max-block-size: none !important;
}

.embedded-game-page,
.game-frame,
[class*="iframe"],
[class*="embed"] {
  max-inline-size: 100%;
}

.game-frame,
iframe.game-frame,
iframe[title*="game" i],
iframe[title*="Game" i] {
  inline-size: 100%;
  min-block-size: min(76vh, 760px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: #050816;
}

button,
[role="button"],
input,
select,
textarea,
a {
  -webkit-tap-highlight-color: transparent;
}

button,
[role="button"],
a.button,
.btn,
.game-button,
.control-button,
.mobile-control,
.bp-mob-btn {
  min-width: 44px;
  min-height: 44px;
}

#bp-mobile-bar {
  box-sizing: border-box;
}

@media (hover: none) and (pointer: coarse), (max-width: 650px) {
  #bp-mobile-bar {
    left: env(safe-area-inset-left);
    right: env(safe-area-inset-right);
    bottom: 0;
    min-height: 72px;
    padding: 8px max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }

  body.bp-has-mobile-bar {
    padding-bottom: calc(var(--bp-gamepad-height, 80px) + env(safe-area-inset-bottom)) !important;
  }

  .game-frame,
  iframe.game-frame,
  iframe[title*="game" i],
  iframe[title*="Game" i] {
    min-block-size: calc((var(--bp-vh, 1vh) * 100) - var(--bp-gamepad-height, 80px) - 64px);
  }

  .bp-mob-btn {
    min-width: 48px;
    min-height: 48px;
    padding: 10px 12px;
    border-radius: 12px;
  }

  .bp-mob-btn-wide {
    min-width: 76px;
  }
}

.bp-mobile-cluster {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.bp-mobile-cluster.bp-mobile-cluster-dpad {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  grid-template-areas:
    ". up ."
    "left center right"
    ". down .";
  gap: 4px;
}

.bp-mobile-cluster-dpad [data-bp-key="ArrowUp"] { grid-area: up; }
.bp-mobile-cluster-dpad [data-bp-key="ArrowLeft"] { grid-area: left; }
.bp-mobile-cluster-dpad [data-bp-key="ArrowRight"] { grid-area: right; }
.bp-mobile-cluster-dpad [data-bp-key="ArrowDown"] { grid-area: down; }
.bp-mobile-cluster-dpad .bp-dpad-spacer { grid-area: center; }

@media (max-width: 420px) {
  #bp-mobile-bar {
    gap: 6px;
  }

  .bp-mobile-cluster.bp-mobile-cluster-dpad {
    grid-template-columns: repeat(3, 44px);
  }

  .bp-mob-btn {
    min-width: 44px;
    min-height: 44px;
    padding-inline: 10px;
    font-size: 0.62rem;
  }
}

@media (max-width: 360px) {
  .bp-mobile-cluster {
    gap: 5px;
  }

  .bp-mobile-cluster.bp-mobile-cluster-dpad {
    grid-template-columns: repeat(3, 40px);
  }

  .bp-mob-btn {
    min-width: 40px;
    min-height: 42px;
    padding-inline: 8px;
    font-size: 0.58rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: 0.001ms !important;
  }
}

/* ── SEO panel mobile-friendly ── */
@media (max-width: 700px) {
  .breakplay-seo-panel {
    position: fixed;
    top: 48px;
    left: 4px;
    right: 4px;
    width: auto;
    max-height: 30vh;
    font-size: 11px;
    padding: 6px 8px;
    opacity: 0.92;
    animation: bp-seo-fade 0.3s ease-out;
  }
  .breakplay-seo-panel details:not([open]) {
    max-height: 22px;
    overflow: hidden;
  }
  .breakplay-seo-panel summary {
    font-size: 12px;
    padding: 2px 0;
  }
}

@keyframes bp-seo-fade {
  from { opacity: 0; transform: translateY(-8px); }
  to   { opacity: 0.92; transform: translateY(0); }
}

/* (reduced-motion handled above) */

/* ── Accessibility: Focus states ── */
*:focus-visible {
  outline: 2px solid rgba(0, 212, 255, 0.8);
  outline-offset: 2px;
}
button:focus-visible,
a:focus-visible,
select:focus-visible,
input:focus-visible {
  outline: 2px solid rgba(0, 212, 255, 0.9);
  outline-offset: 2px;
  box-shadow: 0 0 0 4px rgba(0, 212, 255, 0.15);
}

/* ── Accessibility: Touch targets ── */
@media (pointer: coarse) {
  button, a, select, input[type="button"], input[type="submit"],
  [role="button"], .btn, .button {
    min-height: 44px;
    min-width: 44px;
  }
}

@media (max-width: 820px) {
  a,
  button,
  select,
  input[type="button"],
  input[type="submit"],
  [role="button"],
  .btn,
  .button {
    min-height: 44px;
    min-width: 44px;
  }

  a,
  button,
  [role="button"],
  .btn,
  .button {
    align-items: center;
    justify-content: center;
  }

  a {
    display: inline-flex;
  }
}

/* ── Performance: Canvas rendering hints ── */
canvas {
  image-rendering: -webkit-optimize-contrast;
  image-rendering: crisp-edges;
  will-change: auto;
}

/* ── Ultra-small screens (320px) ── */
@media (max-width: 340px) {
  #bp-mobile-bar {
    gap: 4px !important;
    padding: 4px 6px 8px !important;
  }
  #bp-mobile-bar button {
    min-width: 40px !important;
    min-height: 40px !important;
    font-size: 0.55rem !important;
    padding: 4px !important;
  }
  .breakplay-seo-panel {
    font-size: 10px !important;
    padding: 4px 6px !important;
  }
}

/* ── Injected BreakPlay game nav: wrap safely on narrow phones ── */
@media (max-width: 640px) {
  #bp-nav {
    box-sizing: border-box !important;
    min-height: 48px !important;
    height: auto !important;
    padding: max(4px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 4px max(8px, env(safe-area-inset-left)) !important;
    gap: 6px !important;
    align-items: center !important;
  }

  #bp-nav .bp-nav-logo,
  #bp-nav .bp-nav-btn {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    min-width: 40px !important;
    min-height: 40px !important;
    max-width: 100% !important;
    white-space: normal !important;
    overflow: visible !important;
    overflow-wrap: anywhere !important;
    line-height: 1.1 !important;
    letter-spacing: 0 !important;
  }

  #bp-nav .bp-nav-logo {
    flex: 1 1 auto !important;
    justify-content: flex-start !important;
    font-size: 0.64rem !important;
  }

  #bp-nav .bp-nav-actions {
    display: flex !important;
    flex: 0 1 auto !important;
    flex-wrap: wrap !important;
    justify-content: flex-end !important;
    gap: 4px !important;
    min-width: 0 !important;
    max-width: 64% !important;
  }

  #bp-nav .bp-nav-btn {
    padding: 6px 8px !important;
    font-size: 0.58rem !important;
  }
}
