/* ============================================================
   Digit Fusion — App Shell + DOM UI Layer
   CSS for native iOS/Android app and HTML overlays
   ============================================================ */

/* ---------- Fonts ---------- */
@font-face {
  font-family: "DigitFusionDisplay";
  src: url("../assets/fonts/TitanOne.ttf") format("truetype");
  font-display: swap;
}

@font-face {
  font-family: "DigitFusionBody";
  src: url("../assets/fonts/Baloo.ttf") format("truetype");
  font-display: swap;
}

/* ---------- Reset & App Shell ---------- */
* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #12121F;
  color: #E8E8F0;
  font-family: "DigitFusionBody", "Trebuchet MS", sans-serif;
}

#app-shell {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  isolation: isolate;
  background: #12121F;
}

#content {
  position: absolute;
  inset: 0;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #12121F;
  touch-action: none;
  overscroll-behavior: none;
}

#content canvas {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

#orientation {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: none;
  background: rgba(18, 18, 31, 0.98);
}

#runtime-error {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: 16px;
  z-index: 2200;
  display: none;
  padding: 14px 16px;
  border-radius: 16px;
  border: 1px solid rgba(255, 143, 45, 0.32);
  background: rgba(28, 12, 8, 0.92);
  color: #fff4ec;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  max-height: 42vh;
  overflow: auto;
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.32);
}

/* ---------- Theme Variables ---------- */
:root,
[data-theme="grove"] {
  --bg:           #1A2C24;
  --bg-deep:      #0B1A0B;
  --accent:       #66BB6A;
  --accent-glow:  rgba(102,187,106,0.35);
  --badge:        #4CAF50;
  --badge-text:   #FFFFFF;
  --text:         #FFFFFF;
  --text-muted:   #B0BEC5;
  --heading:      #E0F2F1;
  --progress:     #66BB6A;
  --panel:        rgba(26,26,46,0.88);
  --panel-border: rgba(76,175,80,0.25);
  --glass:        rgba(26,26,46,0.92);
  --glass-border: rgba(102,187,106,0.18);
}

[data-theme="sunrise"] {
  --bg:           #2C1E0E;
  --bg-deep:      #1A0F05;
  --accent:       #FFB74D;
  --accent-glow:  rgba(255,183,77,0.35);
  --badge:        #FF9800;
  --badge-text:   #FFFFFF;
  --text:         #FFFFFF;
  --text-muted:   #BCAAA4;
  --heading:      #FFF3E0;
  --progress:     #FFB74D;
  --panel:        rgba(26,26,46,0.88);
  --panel-border: rgba(255,167,38,0.25);
  --glass:        rgba(26,26,46,0.92);
  --glass-border: rgba(255,183,77,0.18);
}

[data-theme="tide"] {
  --bg:           #0D1B2A;
  --bg-deep:      #06101A;
  --accent:       #64B5F6;
  --accent-glow:  rgba(100,181,246,0.35);
  --badge:        #2196F3;
  --badge-text:   #FFFFFF;
  --text:         #FFFFFF;
  --text-muted:   #90A4AE;
  --heading:      #E1F5FE;
  --progress:     #42A5F5;
  --panel:        rgba(26,26,46,0.88);
  --panel-border: rgba(66,165,245,0.25);
  --glass:        rgba(26,26,46,0.92);
  --glass-border: rgba(100,181,246,0.18);
}

[data-theme="twilight"] {
  --bg:           #1A0E2E;
  --bg-deep:      #0F0620;
  --accent:       #CE93D8;
  --accent-glow:  rgba(206,147,216,0.35);
  --badge:        #9C27B0;
  --badge-text:   #FFFFFF;
  --text:         #FFFFFF;
  --text-muted:   #B39DDB;
  --heading:      #F3E5F5;
  --progress:     #BA68C8;
  --panel:        rgba(26,26,46,0.88);
  --panel-border: rgba(171,71,188,0.25);
  --glass:        rgba(26,26,46,0.92);
  --glass-border: rgba(206,147,216,0.18);
}

[data-theme="crown"] {
  --bg:           #2A2206;
  --bg-deep:      #1A1503;
  --accent:       #FFD54F;
  --accent-glow:  rgba(255,213,79,0.35);
  --badge:        #FFC107;
  --badge-text:   #FFFFFF;
  --text:         #FFFFFF;
  --text-muted:   #D4C08A;
  --heading:      #FFFDE7;
  --progress:     #FFD54F;
  --panel:        rgba(26,26,46,0.88);
  --panel-border: rgba(255,202,40,0.25);
  --glass:        rgba(26,26,46,0.92);
  --glass-border: rgba(255,213,79,0.18);
}

/* ---------- Shared constants ---------- */
:root {
  --radius:       20px;
  --radius-sm:    14px;
  --radius-xs:    8px;
  --font:         'GameFont', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --safe-top:     env(safe-area-inset-top, 0px);
  --safe-bottom:  env(safe-area-inset-bottom, 0px);
  --safe-left:    env(safe-area-inset-left, 0px);
  --safe-right:   env(safe-area-inset-right, 0px);
  --transition:   0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ---------- Game UI Layer ---------- */
/*
 * No z-index on #game-ui — avoids creating a stacking context that
 * iOS WKWebView treats as a touch-blocking layer above the Phaser canvas.
 * Children with their own z-index participate directly in #app-shell's
 * stacking context, so pointer-events:none properly passes events through.
 */
#game-ui {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 0;
  overflow: visible;
  pointer-events: none;
  touch-action: none;
  font-family: var(--font);
  color: var(--text);
}
/* Only truly interactive, VISIBLE elements get pointer-events:auto */
#game-ui button,
#game-ui a,
#game-ui .ctrl-btn,
.splash-page:not(.hidden),
.game-overlay.visible {
  pointer-events: auto;
}
/* All passive display children must never capture touches */
#game-ui .board-hud,
#game-ui .board-hud *,
#game-ui .board-controls,
#game-ui .hud-stat,
#game-ui .hud-goal,
#game-ui .hud-goal-info,
#game-ui .hud-goal-chip,
#game-ui .hud-goal-track,
#game-ui .hud-timer,
#game-ui .hud-timer-track,
#game-ui .hud-stats,
#game-ui .hud-combo,
#game-ui .hud-banner {
  pointer-events: none !important;
}
#game-ui button,
#game-ui .level-btn,
#game-ui .game-overlay {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
#game-ui.hidden {
  display: none;
}

/* ---------- Splash Page ---------- */
.splash-page {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
  padding-bottom: calc(20px + var(--safe-bottom));
  transition: opacity 0.3s ease;
  z-index: 20;
}
.splash-page.hidden {
  display: none;
}

.splash-logo {
  width: 80px;
  height: 80px;
  margin-bottom: 8px;
  border-radius: 20px;
}

.splash-title {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
  letter-spacing: 0.02em;
}

.splash-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 24px;
}

/* -- Status Card (Daily Challenge + Theme) -- */
.splash-card {
  width: 100%;
  max-width: 520px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 18px 22px;
  margin-bottom: 14px;
  display: flex;
  gap: 16px;
}

.splash-card-main {
  flex: 1;
  min-width: 0;
}

.splash-card-divider {
  width: 1.5px;
  background: rgba(255,255,255,0.08);
  border-radius: 1px;
  align-self: stretch;
}

.splash-card-side {
  width: 140px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.card-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.card-title {
  font-size: 22px;
  color: var(--text);
  margin: 2px 0;
  line-height: 1.2;
}

.card-meta {
  font-size: 12px;
  color: var(--text-muted);
}

.theme-switch-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--badge);
  color: var(--badge-text);
  border: none;
  border-radius: 17px;
  padding: 6px 24px;
  font-family: var(--font);
  font-size: 14px;
  cursor: pointer;
  margin-top: 6px;
  transition: transform var(--transition), opacity var(--transition);
}
.theme-switch-btn:active {
  transform: scale(0.95);
}
.theme-switch-btn:disabled {
  opacity: 0.5;
  cursor: default;
}
.theme-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* -- Level Picker -- */
.splash-levels {
  width: 100%;
  max-width: 520px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 14px 22px;
  margin-bottom: 16px;
  text-align: center;
}

.levels-label {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.levels-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.level-btn {
  width: 58px;
  height: 58px;
  border-radius: 14px;
  border: 2px solid transparent;
  background: rgba(255,255,255,0.06);
  color: var(--text);
  font-family: var(--font);
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color var(--transition), background var(--transition), opacity var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}
.level-btn.locked {
  opacity: 0.32;
  cursor: default;
}
.level-btn.selected {
  border-color: var(--accent);
  background: var(--accent-glow);
}
.level-btn:not(.locked):active {
  transform: scale(0.95);
}

/* -- Tutorial button -- */
.splash-tutorial-btn {
  position: absolute;
  top: calc(16px + var(--safe-top));
  right: calc(16px + var(--safe-right));
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-family: var(--font);
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}
.splash-tutorial-btn:active {
  background: var(--glass);
}

/* -- Play Button -- */
.play-btn {
  width: 100%;
  max-width: 520px;
  height: 64px;
  border-radius: 32px;
  border: none;
  background: var(--badge);
  color: var(--badge-text);
  font-family: var(--font);
  font-size: 26px;
  font-weight: 700;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: transform var(--transition);
}
.play-btn:active {
  transform: scale(0.97);
}
.play-btn .play-btn-sub {
  display: block;
  font-size: 12px;
  font-weight: 500;
  opacity: 0.8;
  margin-top: -2px;
}

/* ---------- Board HUD ---------- */
/*
 * No z-index on .board-hud — same rationale as #game-ui.
 * iOS WKWebView treats position+z-index stacking contexts as touch-blocking
 * layers even with pointer-events:none.
 */
.board-hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 10px 16px;
  padding-top: calc(10px + var(--safe-top));
  display: none;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  touch-action: none;
}
.board-hud.active {
  display: flex;
}

.hud-stats {
  display: flex;
  gap: 10px;
}

.hud-stat {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 8px 14px;
  text-align: center;
}
.hud-stat-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.hud-stat-value {
  font-size: 22px;
  color: var(--text);
  font-weight: 600;
}

/* -- Goal Card -- */
.hud-goal {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}
.hud-goal-info {
  flex: 1;
  min-width: 0;
}
.hud-goal-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.hud-goal-title {
  font-size: 16px;
  color: var(--text);
  margin: 1px 0;
  line-height: 1.3;
}
.hud-goal-status {
  font-size: 11px;
  color: var(--text-muted);
}
.hud-goal-chip {
  background: var(--badge);
  color: var(--badge-text);
  font-size: 11px;
  padding: 4px 12px;
  border-radius: 12px;
  white-space: nowrap;
  flex-shrink: 0;
}

/* -- Progress bar -- */
.hud-goal-track {
  height: 4px;
  background: rgba(255,255,255,0.1);
  border-radius: 2px;
  margin-top: 8px;
  overflow: hidden;
}
.hud-goal-fill {
  height: 100%;
  background: var(--progress);
  border-radius: 2px;
  transition: width 0.4s ease;
}

/* -- Timer bar -- */
.hud-timer {
  display: flex;
  align-items: center;
  gap: 8px;
}
.hud-timer-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  flex-shrink: 0;
}
.hud-timer-track {
  flex: 1;
  height: 6px;
  background: rgba(255,255,255,0.06);
  border-radius: 3px;
  overflow: hidden;
}
.hud-timer-fill {
  height: 100%;
  background: var(--progress);
  border-radius: 3px;
  transition: width 0.1s linear;
  width: 100%;
}

/* -- Combo -- */
.hud-combo {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 36px;
  font-weight: 700;
  color: #ffffff;
  text-shadow: 0 2px 12px rgba(0,0,0,0.4);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}
.hud-combo.visible {
  opacity: 1;
}

/* -- Board Controls (Menu / Audio) -- */
/* No z-index — same stacking-context touch-blocking avoidance as #game-ui. */
.board-controls {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: 10px 16px;
  padding-top: calc(10px + var(--safe-top));
  display: none;
  justify-content: space-between;
  pointer-events: none;
  touch-action: none;
}
.board-controls.active {
  display: flex;
}

.ctrl-btn {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--heading);
  font-family: var(--font);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}
.ctrl-btn:active {
  background: var(--glass);
}
.ctrl-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

/* ---------- Overlay Base ---------- */
.game-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10,10,20,0.78);
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 30;
  padding: 20px;
  padding-top: calc(20px + var(--safe-top));
  padding-bottom: calc(20px + var(--safe-bottom));
}
.game-overlay.visible {
  display: flex;
  opacity: 1;
  pointer-events: auto;
}

.overlay-panel {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  padding: 28px 24px;
  width: 100%;
  max-width: 380px;
  max-height: 100%;
  overflow-y: auto;
  text-align: center;
}
.overlay-panel::-webkit-scrollbar {
  display: none;
}

.overlay-eyebrow {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 4px;
}
.overlay-title {
  font-size: 28px;
  font-weight: 700;
  color: #ffffff;
  margin-bottom: 4px;
}
.overlay-subtitle {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 16px;
  line-height: 1.4;
}

/* -- Stat Row -- */
.overlay-stats {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
}
.overlay-stat {
  flex: 1;
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius-sm);
  padding: 10px 8px;
}
.overlay-stat-label {
  font-size: 10px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.overlay-stat-value {
  font-size: 24px;
  color: var(--text);
  font-weight: 600;
}

/* -- Challenge section in overlay -- */
.overlay-challenge {
  text-align: left;
  margin-bottom: 16px;
}
.overlay-challenge-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
}
.overlay-challenge-title {
  font-size: 16px;
  color: var(--text);
  margin: 2px 0;
}
.overlay-challenge-status {
  font-size: 12px;
  color: var(--text-muted);
}

/* -- Level picker in overlay -- */
.overlay-levels {
  margin-bottom: 16px;
}
.overlay-levels .levels-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 8px;
}

/* -- Action Button -- */
.overlay-action {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  background: rgba(255,255,255,0.06);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 14px 18px;
  color: var(--text);
  font-family: var(--font);
  font-size: 18px;
  cursor: pointer;
  transition: background var(--transition);
  margin-bottom: 8px;
  text-align: left;
}
.overlay-action:active {
  background: rgba(255,255,255,0.1);
}
.overlay-action.primary {
  background: var(--badge);
  color: var(--badge-text);
  border-color: transparent;
}
.overlay-action.primary:active {
  background: var(--accent);
}
.overlay-action-icon {
  font-size: 22px;
  flex-shrink: 0;
  width: 28px;
  text-align: center;
}
.overlay-action-text {
  flex: 1;
}
.overlay-action-hint {
  font-size: 12px;
  opacity: 0.7;
}

/* -- Inline action row (two buttons side by side) -- */
.overlay-actions-row {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.overlay-actions-row .overlay-action {
  flex: 1;
  justify-content: center;
  margin-bottom: 0;
}

/* -- Win trophy -- */
.win-trophy {
  width: 100px;
  height: 100px;
  margin: 0 auto 12px;
}
.win-aura {
  animation: spin-aura 8s linear infinite;
}
@keyframes spin-aura {
  to { transform: rotate(360deg); }
}

/* -- End Tutorial -- */
.tutorial-block-img {
  width: 88px;
  height: 88px;
  margin: 16px auto;
}
.tutorial-tap-hint {
  font-size: 18px;
  color: var(--text-muted);
  margin-top: 16px;
  animation: pulse-hint 1.2s ease-in-out infinite;
}
@keyframes pulse-hint {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

/* -- Banner (momentary notification) -- */
.hud-banner {
  position: fixed;
  left: 50%;
  top: 35%;
  transform: translateX(-50%) translateY(-8px);
  background: var(--glass);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-sm);
  padding: 10px 24px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.26s ease, transform 0.26s ease;
}
.hud-banner.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.hud-banner-title {
  font-size: 22px;
  color: #ffffff;
  font-weight: 600;
}
.hud-banner-sub {
  font-size: 14px;
  color: var(--text-muted);
}

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

/* Small phones */
@media (max-height: 680px) {
  .splash-logo { width: 56px; height: 56px; }
  .splash-title { font-size: 22px; }
  .splash-card { padding: 14px 16px; }
  .card-title { font-size: 18px; }
  .splash-levels { padding: 10px 16px; }
  .level-btn { width: 48px; height: 48px; font-size: 17px; }
  .play-btn { height: 54px; font-size: 22px; }
  .overlay-panel { padding: 20px 18px; }
  .overlay-title { font-size: 24px; }
}

/* Landscape */
@media (orientation: landscape) {
  .splash-page {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 16px;
    padding-left: calc(20px + var(--safe-left));
    padding-right: calc(20px + var(--safe-right));
  }
  .splash-card,
  .splash-levels,
  .play-btn {
    max-width: 400px;
  }
  .hud-combo {
    font-size: 28px;
  }
}

/* iPad / larger screens */
@media (min-width: 768px) {
  .splash-card { max-width: 560px; }
  .splash-levels { max-width: 560px; }
  .play-btn { max-width: 560px; height: 70px; font-size: 28px; }
  .overlay-panel { max-width: 420px; }
}

/* ---------- Hide game-ui on desktop (web wrapper) ---------- */
[data-runtime="web"] #game-ui {
  display: none;
}
