/* Screw Sort — Styling & Design System (iPhone & iPad Responsive) */

:root {
  --bg-main: #1f153a;
  --bg-gradient: radial-gradient(circle at 50% 32%, #2c1f52 0%, #18102e 100%);
  --surface-card: #281d4a;
  --surface-subtle: rgba(255, 255, 255, 0.08);
  --ink-primary: #ffffff;
  --ink-secondary: rgba(255, 255, 255, 0.72);
  --ink-dim: rgba(255, 255, 255, 0.45);
  
  --accent-yellow: #f8ad32;
  --accent-yellow-shadow: #b3730e;
  --accent-green: #2ecc71;
  --accent-blue: #3498db;
  --accent-pink: #ff5e97;
  
  --hud-btn-bg: #ffffff;
  --hud-btn-shadow: #c8cbd9;
  --hud-btn-ink: #3b3561;

  --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);
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-main);
  background-image: var(--bg-gradient);
  font-family: var(--font-family);
  color: var(--ink-primary);
  touch-action: none;
}

[hidden],
.hidden,
.overlay-card-backdrop[hidden],
.update-wall-overlay[hidden] {
  display: none !important;
}

.game-container {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

/* --- Stage & Canvas (Full Bleed Edge-to-Edge) --- */
.stage-wrapper {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* --- Top Header, Timer & Reset Button --- */
.hud-header {
  position: absolute;
  top: max(14px, var(--safe-top));
  left: 0;
  right: 0;
  width: 100%;
  padding: 0 max(16px, var(--safe-right)) 0 max(16px, var(--safe-left));
  box-sizing: border-box;
  height: 60px;
  z-index: 10;
  pointer-events: none;
}

/* Center: Tier, Level Title & Timer Widget */
.hud-level-info {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  pointer-events: auto;
}

/* --- Timer Widget (Under Level Title) --- */
.hud-timer {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.20);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  padding: 4px 12px;
  border-radius: 16px;
  color: #ffffff;
  pointer-events: auto;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.30);
  transition: all 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
  margin-top: 1px;
}

.timer-icon {
  width: 14px;
  height: 14px;
  color: #60a5fa;
  flex-shrink: 0;
  transition: color 0.2s ease, transform 0.2s ease;
}

.timer-text {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.8px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

/* Low Time State (≤ 10s) */
.hud-timer.low-time {
  background: rgba(239, 68, 68, 0.22);
  border-color: rgba(239, 68, 68, 0.55);
  color: #ff6b6b;
  box-shadow: 0 0 20px rgba(239, 68, 68, 0.45);
  animation: timerPulse 0.8s ease-in-out infinite alternate;
}

.hud-timer.low-time .timer-icon {
  color: #ff4b4b;
  animation: timerShake 0.4s ease-in-out infinite alternate;
}

@keyframes timerPulse {
  0% { transform: scale(1); }
  100% { transform: scale(1.06); }
}

@keyframes timerShake {
  0% { transform: rotate(-8deg); }
  100% { transform: rotate(8deg); }
}

/* Timeout Card Modal */
.timeout-card {
  border-top: 4px solid #ef4444 !important;
}

.timeout-icon-wrap {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: rgba(239, 68, 68, 0.15);
  border: 2px solid rgba(239, 68, 68, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 16px auto;
  animation: pulseTimeoutIcon 1.2s ease-in-out infinite alternate;
}

@keyframes pulseTimeoutIcon {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
  100% { transform: scale(1.05); box-shadow: 0 0 24px 4px rgba(239, 68, 68, 0.3); }
}

.timeout-title {
  color: #ff5252 !important;
  text-shadow: 0 2px 12px rgba(239, 68, 68, 0.4) !important;
}

.tier-pill {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 1.5px;
  color: #a49fc4;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  padding: 2px 10px;
  border-radius: 12px;
  text-transform: uppercase;
  transition: all 0.2s ease;
}

.tier-pill[data-tier="FLOW"] {
  background: rgba(46, 204, 113, 0.18);
  color: #2ecc71;
  border-color: rgba(46, 204, 113, 0.35);
}

.tier-pill[data-tier="MEDIUM"] {
  background: rgba(255, 184, 0, 0.15);
  color: #ffc83b;
  border-color: rgba(255, 184, 0, 0.30);
}

.tier-pill[data-tier="HARD"] {
  background: rgba(255, 75, 75, 0.18);
  color: #ff6b6b;
  border-color: rgba(255, 75, 75, 0.40);
  font-weight: 900;
}

.tier-pill[data-tier="EXPERT"] {
  background: rgba(168, 85, 247, 0.22);
  color: #c084fc;
  border-color: rgba(168, 85, 247, 0.45);
  font-weight: 900;
}

.level-title {
  font-size: 24px;
  font-weight: 900;
  letter-spacing: -0.5px;
  color: #ffffff;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}

.hud-btn-round {
  position: absolute;
  right: max(16px, var(--safe-right));
  top: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: #ffffff;
  color: #3b3561;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.1s ease;
  z-index: 20;
  pointer-events: auto;
  flex-shrink: 0;
}

.hud-btn-round svg {
  display: block;
  stroke: #3b3561;
}

.hud-btn-round:active {
  transform: scale(0.92);
  opacity: 0.85;
  box-shadow: none;
}

@keyframes restartPulse {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(243, 156, 18, 0.7); }
  50% { transform: scale(1.12); box-shadow: 0 0 0 10px rgba(243, 156, 18, 0); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(243, 156, 18, 0); }
}

.btn-pulse {
  animation: restartPulse 1.4s infinite ease-in-out !important;
  color: #ffde59 !important;
  background: rgba(255, 222, 89, 0.22) !important;
}

.deadlock-toast {
  position: absolute;
  top: calc(max(14px, var(--safe-top)) + 88px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(28, 22, 54, 0.95);
  border: 1.5px solid rgba(243, 156, 18, 0.45);
  color: #ffde59;
  font-size: 13.5px;
  font-weight: 700;
  padding: 8px 18px;
  border-radius: 20px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  pointer-events: none;
  z-index: 50;
  animation: fadeIn 0.25s ease-out;
  white-space: nowrap;
}



/* --- Overlays & Result Card --- */
.overlay-card-backdrop {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(14, 11, 28, 0.88);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  animation: fadeIn 0.25s ease-out;
}

.overlay-card {
  width: 100%;
  max-width: 360px;
  background: linear-gradient(180deg, #2b2552 0%, #1e193e 100%);
  border: 2px solid rgba(255, 255, 255, 0.15);
  border-radius: 28px;
  padding: 28px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6), 0 0 30px rgba(243, 156, 18, 0.15);
  animation: cardPop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.stars-row {
  display: flex;
  gap: 12px;
  font-size: 40px;
  color: #f1c40f;
  margin-bottom: 12px;
  text-shadow: 0 4px 12px rgba(241, 196, 15, 0.4);
}

.star-slot {
  display: inline-block;
  transform: scale(0);
  animation: starPop 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

.star-1 { animation-delay: 0.15s; }
.star-2 { animation-delay: 0.30s; }
.star-3 { animation-delay: 0.45s; }

.result-title {
  font-size: 22px;
  font-weight: 900;
  color: #ffffff;
  letter-spacing: 0.5px;
  margin-bottom: 6px;
}

.result-score {
  font-size: 34px;
  font-weight: 900;
  color: #ffde59;
  letter-spacing: -0.5px;
  margin-bottom: 6px;
}

.result-subtext {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-secondary);
  margin-bottom: 20px;
}

.result-campaign-copy {
  font-size: 15px;
  font-weight: 600;
  color: #a8d5e5;
  line-height: 1.4;
  margin-bottom: 20px;
}

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

.cta-btn {
  width: 100%;
  height: 52px;
  border-radius: 26px;
  border: none;
  font-size: 16px;
  font-weight: 800;
  letter-spacing: 0.6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s ease, opacity 0.12s ease, background-color 0.12s ease;
}

.primary-cta {
  background: linear-gradient(180deg, #2ecc71 0%, #27ae60 100%);
  color: #ffffff;
  box-shadow: 0 4px 14px rgba(46, 204, 113, 0.3);
}

.primary-cta:active {
  transform: scale(0.97);
  opacity: 0.92;
}

.secondary-cta {
  background: rgba(255, 255, 255, 0.08);
  border: 1.5px solid rgba(255, 255, 255, 0.16);
  color: #ffffff;
  box-shadow: none;
}

.secondary-cta:active {
  background: rgba(255, 255, 255, 0.15);
  transform: scale(0.97);
}

/* --- Update Wall --- */
.update-wall-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(14, 11, 28, 0.95);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.update-wall-card {
  background: var(--surface-card);
  padding: 28px 24px;
  border-radius: 20px;
  text-align: center;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.update-wall-eyebrow {
  font-size: 12px;
  font-weight: 800;
  color: var(--accent-yellow);
  letter-spacing: 1px;
}

.update-wall-card h2 {
  font-size: 20px;
  color: #ffffff;
}

.update-wall-card p {
  font-size: 14px;
  color: var(--ink-secondary);
  line-height: 1.4;
}

.update-app-link {
  display: inline-block;
  margin-top: 10px;
  padding: 12px 28px;
  background: var(--accent-yellow);
  color: #1a152e;
  font-weight: 800;
  text-decoration: none;
  border-radius: 20px;
}

/* --- Animations --- */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes cardPop {
  0% { transform: scale(0.85); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes starPop {
  0% { transform: scale(0) rotate(-45deg); }
  70% { transform: scale(1.25) rotate(10deg); }
  100% { transform: scale(1) rotate(0deg); }
}

/* --- Tablet / iPad Pro Enhancements --- */
@media (min-width: 600px) {
  .hud-header {
    top: max(20px, var(--safe-top));
    left: 0;
    right: 0;
    width: 100%;
    padding: 0 max(24px, var(--safe-right)) 0 max(24px, var(--safe-left));
    box-sizing: border-box;
  }
  .hud-level-info {
    gap: 6px;
  }
  .tier-pill {
    font-size: 14px;
    padding: 4px 14px;
    border-radius: 14px;
  }
  .level-title {
    font-size: 32px;
    letter-spacing: -0.5px;
  }
  .hud-timer {
    padding: 6px 16px;
    border-radius: 20px;
    gap: 8px;
    background: rgba(255, 255, 255, 0.14);
    border: 1.5px solid rgba(255, 255, 255, 0.24);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
    margin-top: 2px;
  }
  .hud-timer .timer-icon {
    width: 17px;
    height: 17px;
  }
  .hud-timer .timer-text {
    font-size: 17px;
    letter-spacing: 1px;
  }
  .hud-btn-round {
    right: max(24px, var(--safe-right));
    top: 0;
    width: 52px;
    height: 52px;
  }
  .hud-btn-round svg {
    width: 26px;
    height: 26px;
  }
  .overlay-card {
    max-width: 420px;
    padding: 36px 32px;
  }
  .result-title {
    font-size: 26px;
  }
  .result-score {
    font-size: 40px;
  }
  .cta-btn {
    height: 58px;
    font-size: 18px;
  }
  .deadlock-toast {
    top: calc(max(20px, var(--safe-top)) + 120px);
    font-size: 15px;
    padding: 10px 22px;
  }
}
