/**
 * css/style.css
 * Pure Vanilla CSS3 Stylesheet for Ball Maze Escape.
 * Modern, responsive, mobile-first design with fluid layout and safe area handling.
 */

/* ==========================================================================
   Reset & Root Variables
   ========================================================================== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --bg-primary: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-elevated: #f1f5f9;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --color-accent: #f59e0b; /* Golden Amber */
  --color-accent-hover: #d97706;
  --color-emerald: #10b981;
  --color-emerald-hover: #059669;
  --color-crimson: #ef4444;
  --color-crimson-hover: #dc2626;
  --color-border: #e2e8f0;
  --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.08), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.05);
  --shadow-modal: 0 20px 25px -5px rgba(15, 23, 42, 0.15), 0 8px 10px -6px rgba(15, 23, 42, 0.1);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-full: 9999px;
  --touch-target: 46px;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  user-select: none;
  -webkit-user-select: none;
}

#appRoot {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  background: radial-gradient(circle at center, #ffffff 0%, #f1f5f9 100%);
}

/* ==========================================================================
   Screen Management
   ========================================================================== */
.screen {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: none;
  flex-direction: column;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
  z-index: 10;
}

.screen.active,
#screenGameplay.active {
  display: flex !important;
  opacity: 1;
  visibility: visible;
}

/* ==========================================================================
   Loading Screen
   ========================================================================== */
#screenLoading {
  align-items: center;
  justify-content: center;
  background: #ffffff;
  z-index: 100;
}

.loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.loading-ball-spinner {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #ffffff 0%, #f59e0b 45%, #b45309 100%);
  box-shadow: 0 8px 16px rgba(245, 158, 11, 0.35);
  animation: bounceSpin 1.2s infinite ease-in-out;
}

@keyframes bounceSpin {
  0%, 100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-24px) scale(0.95);
  }
}

.loading-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

/* ==========================================================================
   Main Menu Screen
   ========================================================================== */
#screenMainMenu {
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.menu-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 36px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: var(--shadow-modal);
  text-align: center;
}

.menu-logo-container {
  margin-bottom: 20px;
  position: relative;
}

.menu-logo {
  width: 88px;
  height: 88px;
  filter: drop-shadow(0 6px 12px rgba(245, 158, 11, 0.25));
  animation: floatSubtle 3s ease-in-out infinite;
}

@keyframes floatSubtle {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.menu-title {
  font-size: 32px;
  font-weight: 800;
  color: var(--text-primary);
  letter-spacing: -0.03em;
  line-height: 1.15;
}

.menu-subtitle {
  font-size: 15px;
  color: var(--text-secondary);
  margin-top: 6px;
  margin-bottom: 28px;
}

.menu-buttons {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ==========================================================================
   Button Archetypes
   ========================================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch-target);
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 600;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease-in-out;
  text-decoration: none;
  line-height: 1.2;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--color-accent);
  color: #ffffff;
  border-color: #d97706;
  box-shadow: 0 4px 8px rgba(245, 158, 11, 0.25);
}

.btn-primary:hover {
  background: var(--color-accent-hover);
}

.btn-emerald {
  background: var(--color-emerald);
  color: #ffffff;
  border-color: #059669;
  box-shadow: 0 4px 8px rgba(16, 185, 129, 0.25);
}

.btn-emerald:hover {
  background: var(--color-emerald-hover);
}

.btn-secondary {
  background: var(--bg-surface-elevated);
  color: var(--text-primary);
  border-color: var(--color-border);
}

.btn-secondary:hover {
  background: #e2e8f0;
}

.btn-danger {
  background: var(--color-crimson);
  color: #ffffff;
  border-color: var(--color-crimson-hover);
}

.btn-danger:hover {
  background: var(--color-crimson-hover);
}

.btn-icon {
  width: var(--touch-target);
  height: var(--touch-target);
  min-height: var(--touch-target);
  padding: 0;
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
}

/* ==========================================================================
   Level Select Screen
   ========================================================================== */
#screenLevelSelect {
  padding: 20px;
  align-items: center;
}

.level-select-container {
  width: 100%;
  max-width: 600px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.level-select-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  flex-shrink: 0;
}

.level-select-title {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.level-grid {
  flex: 1;
  overflow-y: auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 12px;
  padding-bottom: 24px;
  -webkit-overflow-scrolling: touch;
}

.level-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 14px 8px;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 96px;
  text-align: center;
}

.level-card.unlocked:hover {
  border-color: var(--color-accent);
  box-shadow: var(--shadow-md);
  transform: translateY(-2px);
}

.level-card.unlocked:active {
  transform: scale(0.96);
}

.level-card.completed {
  border-color: var(--color-emerald);
  background: #f0fdf4;
}

.level-card.locked {
  opacity: 0.55;
  cursor: not-allowed;
  background: #f1f5f9;
}

.level-card-num {
  font-size: 20px;
  font-weight: 800;
  color: var(--text-primary);
}

.level-card-tier {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-secondary);
  margin-top: 2px;
}

.level-card-status {
  font-size: 12px;
  font-weight: 600;
  margin-top: 4px;
  color: var(--color-accent);
}

.level-card.completed .level-card-status {
  color: var(--color-emerald);
}

.level-card-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ==========================================================================
   Gameplay Screen & HUD
   ========================================================================== */
#screenGameplay {
  flex-direction: column;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.game-hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--color-border);
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
  z-index: 20;
}

.hud-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hud-level-info {
  display: flex;
  flex-direction: column;
}

.hud-level-title {
  font-size: 16px;
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1.1;
}

.hud-tier-badge {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
}

.hud-lives {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 16px;
}

.life-heart {
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.life-heart.lost {
  opacity: 0.25;
  filter: grayscale(1);
}

.hud-timer-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-surface-elevated);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  border: 1px solid var(--color-border);
}

.hud-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: var(--radius-sm);
  background: var(--bg-surface-elevated);
  border: 1px solid var(--color-border);
  color: var(--text-primary);
  cursor: pointer;
  font-size: 16px;
  font-weight: 600;
  transition: all 0.15s ease;
}

.hud-btn:active {
  transform: scale(0.94);
}

.hud-hint-btn {
  background: #fef3c7;
  border-color: #fde68a;
  color: #b45309;
  font-size: 13px;
  width: auto;
  padding: 0 10px;
  gap: 4px;
}

.hud-levels-btn {
  width: auto;
  padding: 0 10px;
  font-size: 13px;
  gap: 4px;
  background: var(--bg-surface-elevated);
}

/* ==========================================================================
   Game Canvas Area
   ========================================================================== */
.game-viewport {
  position: relative;
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: #f8fafc;
}

#gameCanvasContainer {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#gameCanvas {
  display: block;
  touch-action: none;
  cursor: crosshair;
}

/* Screen shake effect on error */
.screen-shake {
  animation: screenShakeAnim 0.35s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

@keyframes screenShakeAnim {
  10%, 90% { transform: translate3d(-2px, 0, 0); }
  20%, 80% { transform: translate3d(3px, 0, 0); }
  30%, 50%, 70% { transform: translate3d(-4px, 0, 0); }
  40%, 60% { transform: translate3d(4px, 0, 0); }
}

/* Damage flash overlay */
#damageFlashOverlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  background: radial-gradient(circle at center, transparent 40%, rgba(239, 68, 68, 0.4) 100%);
  opacity: 0;
  transition: opacity 0.1s ease-out;
  z-index: 15;
}

#damageFlashOverlay.flash-active {
  opacity: 1;
}

/* ==========================================================================
   Modals & Popups
   ========================================================================== */
.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.modal-overlay.active {
  display: flex;
  opacity: 1;
}

.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-surface);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-modal);
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  transform: scale(0.96);
  transition: transform 0.2s ease;
}

.modal-overlay.active .modal-card {
  transform: scale(1);
}

.modal-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.modal-title {
  font-size: 24px;
  font-weight: 800;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.modal-desc {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 6px;
  margin-bottom: 20px;
  line-height: 1.4;
}

.modal-stats {
  display: flex;
  width: 100%;
  justify-content: space-around;
  background: var(--bg-surface-elevated);
  border-radius: var(--radius-md);
  padding: 12px;
  margin-bottom: 20px;
  border: 1px solid var(--color-border);
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.stat-label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
}

.stat-value {
  font-size: 18px;
  font-weight: 800;
  color: var(--text-primary);
  margin-top: 2px;
}

.modal-buttons {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: 10px;
}

/* ==========================================================================
   Settings Modal & Form Controls
   ========================================================================== */
.settings-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
  margin-bottom: 20px;
  text-align: left;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.switch-control {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.switch-control input {
  opacity: 0;
  width: 0;
  height: 0;
}

.switch-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #cbd5e1;
  transition: .2s;
  border-radius: 24px;
}

.switch-slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .2s;
  border-radius: 50%;
}

input:checked + .switch-slider {
  background-color: var(--color-emerald);
}

input:checked + .switch-slider:before {
  transform: translateX(20px);
}

.range-slider {
  width: 120px;
  accent-color: var(--color-accent);
}

.settings-select {
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--bg-surface);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

/* ==========================================================================
   Toast Notification Banner
   ========================================================================== */
.toast-banner {
  position: absolute;
  top: 64px;
  left: 50%;
  transform: translateX(-50%) translateY(-20px);
  padding: 8px 18px;
  border-radius: var(--radius-full);
  font-size: 13px;
  font-weight: 700;
  box-shadow: var(--shadow-lg);
  pointer-events: none;
  opacity: 0;
  transition: all 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  z-index: 150;
  white-space: nowrap;
}

.toast-banner.active {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast-info {
  background: #0f172a;
  color: #ffffff;
}

.toast-error {
  background: #ef4444;
  color: #ffffff;
}

.toast-warning {
  background: #f59e0b;
  color: #ffffff;
}

/* ==========================================================================
   Desktop / Large Viewport Adaptations
   ========================================================================== */
@media (min-width: 768px) {
  .touch-controls-bar {
    display: none; /* Hide virtual D-pad on desktop since keyboard and mouse direct clicks work */
  }

  .game-hud {
    padding: 12px 28px;
  }
}
