:root {
  --bg-dark: #0f172a;
  --bg-deep: #020617;
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --secondary: #ec4899;
  --accent: #06b6d4;
  --success: #10b981;
  --danger: #ef4444;

  --text-main: #f8fafc;
  --text-muted: #94a3b8;

  --glass-bg: rgba(30, 41, 59, 0.6);
  --glass-border: rgba(255, 255, 255, 0.08);
  --glass-shine: rgba(255, 255, 255, 0.05);

  --shadow-sm: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 20px 25px -5px rgba(0, 0, 0, 0.2), 0 10px 10px -5px rgba(0, 0, 0, 0.1);
  --glow-primary: 0 0 25px rgba(99, 102, 241, 0.25);
}

* {
  box-sizing: border-box;
}

html {
  background: #020617;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  height: 100vh;
  overflow: hidden;
  position: fixed;
  width: 100%;
  touch-action: none;
  background: radial-gradient(circle at top center, #1e293b 0%, #020617 100%);
  color: var(--text-main);
  font-family: "Inter", sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

/* Subtle animated grid background */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
  background-size: 50px 50px;
  pointer-events: none;
  z-index: 0;
  mask-image: radial-gradient(circle at center, black 40%, transparent 100%);
}

#mainContainer {
  display: flex;
  flex-direction: row;
  align-items: center;
  /* Center align items vertically */
  justify-content: center;
  gap: 24px;
  width: 100%;
  max-width: 1100px;
  position: relative;
  z-index: 1;
}

#gameContainer {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  border-radius: 16px;
  padding: 0;
  /* Remove padding for cleaner look */
  background: black;
  box-shadow:
    0 0 0 1px var(--glass-border),
    0 20px 50px -10px rgba(0, 0, 0, 0.5),
    0 0 30px rgba(99, 102, 241, 0.15);
  /* Slight glow */
  overflow: hidden;
  flex-shrink: 0;
}

#canvas {
  display: block;
  background: #000;
  touch-action: none;
}

/* Countdown overlay */
#countdown {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: "Outfit", sans-serif;
  font-size: 140px;
  font-weight: 900;
  color: white;
  text-shadow: 0 0 40px var(--primary);
  z-index: 100;
  opacity: 0;
  transition:
    opacity 0.2s,
    transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
}

#countdown.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1.1);
}

/* Victory overlay */
#victoryOverlay {
  position: absolute;
  inset: 0;
  background: rgba(2, 6, 23, 0.85);
  backdrop-filter: blur(12px);
  border-radius: 14px;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.4s ease;
}

#victoryOverlay.show {
  display: flex;
  opacity: 1;
}

#victoryText {
  font-family: "Outfit", sans-serif;
  font-size: 64px;
  font-weight: 800;
  margin-bottom: 30px;
  text-align: center;
  letter-spacing: -1px;
}

.status-win {
  background: linear-gradient(to right, #4ade80, #3b82f6);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 30px rgba(74, 222, 128, 0.3);
}

.status-lose {
  background: linear-gradient(to right, #f472b6, #ef4444);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 30px rgba(239, 68, 68, 0.3);
}

#playAgainBtn {
  padding: 16px 40px;
  font-size: 18px;
  font-weight: 700;
  font-family: "Outfit", sans-serif;
  letter-spacing: 1px;
  color: white;
  background: var(--primary);
  border: none;
  border-radius: 100px;
  cursor: pointer;
  box-shadow: 0 10px 20px -5px rgba(99, 102, 241, 0.4);
  transition: all 0.2s ease;
  text-transform: uppercase;
}

#playAgainBtn:hover {
  transform: translateY(-2px);
  background: var(--primary-hover);
  box-shadow: 0 15px 25px -5px rgba(99, 102, 241, 0.5);
}

.victory-buttons {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.btn-quit-overlay {
  padding: 16px 40px;
  font-size: 18px;
  font-weight: 700;
  font-family: "Outfit", sans-serif;
  letter-spacing: 1px;
  color: white;
  background: #dc2626;
  border: none;
  border-radius: 100px;
  cursor: pointer;
  box-shadow: 0 10px 20px -5px rgba(220, 38, 38, 0.4);
  transition: all 0.2s ease;
  text-transform: uppercase;
}

.btn-quit-overlay:hover {
  transform: translateY(-2px);
  background: #b91c1c;
  box-shadow: 0 15px 25px -5px rgba(220, 38, 38, 0.5);
}

/* Side Panel */
#sidePanel {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 320px;
  flex-shrink: 0;
}

/* Card Style */
.panel-card {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  border-radius: 16px;
  padding: 24px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Score Section */
#scoreContainer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
}

#score {
  font-family: "Outfit", sans-serif;
  font-size: 56px;
  font-weight: 800;
  color: white;
  margin: 0;
  text-align: center;
  letter-spacing: -1px;
  line-height: 1;
  transition: transform 0.1s cubic-bezier(0.4, 0, 0.2, 1);
}

#score.flash {
  transform: scale(1.15);
  color: var(--accent);
  text-shadow: 0 0 20px rgba(6, 182, 212, 0.5);
}

#winCondition {
  text-align: center;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 2px;
  color: var(--text-muted);
  font-weight: 600;
  margin-top: 4px;
}

/* Metrics */
#metrics {
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 0;
  background: transparent;
  border: none;
  box-shadow: none;
  opacity: 0.6;
  transition: opacity 0.2s;
}

#metrics:hover {
  opacity: 1;
}

.metric-row {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.metric-label {
  margin-right: 6px;
}

.metric-value {
  font-weight: 600;
  color: var(--text-main);
}

/* UI Controls */
#playBtn {
  width: 100%;
  padding: 20px;
  font-family: "Outfit", sans-serif;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  background: linear-gradient(135deg, var(--primary) 0%, #4f46e5 100%);
  border: none;
  color: white;
  border-radius: 12px;
  cursor: pointer;
  box-shadow:
    0 4px 6px rgba(0, 0, 0, 0.1),
    0 10px 15px -3px rgba(99, 102, 241, 0.3);
  transition: all 0.2s ease;
}

#playBtn:hover {
  transform: translateY(-2px);
  filter: brightness(1.1);
  box-shadow: 0 20px 25px -5px rgba(99, 102, 241, 0.4);
}

#playBtn:disabled {
  background: #334155;
  color: #64748b;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

#playBtn.playing {
  background: rgba(16, 185, 129, 0.2);
  border: 1px solid rgba(16, 185, 129, 0.4);
  color: #6ee7b7;
  box-shadow: none;
  cursor: default;
  pointer-events: none;
  padding: 14px 20px;
  font-size: 14px;
  letter-spacing: 1px;
}

.btn-challenge {
  width: 100%;
  padding: 14px 20px;
  background: linear-gradient(135deg, #ec4899 0%, #8b5cf6 100%);
  border: none;
  border-radius: 10px;
  color: white;
  font-family: "Outfit", sans-serif;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(236, 72, 153, 0.3);
  transition: all 0.2s ease;
}

.btn-challenge:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(236, 72, 153, 0.4);
}

.btn-challenge:disabled {
  background: #334155;
  box-shadow: none;
  cursor: not-allowed;
  transform: none;
}

.btn-quit {
  width: 100%;
  padding: 14px 20px;
  background: linear-gradient(135deg, #dc2626 0%, #991b1b 100%);
  border: none;
  border-radius: 10px;
  color: white;
  font-family: "Outfit", sans-serif;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 4px 15px rgba(220, 38, 38, 0.3);
  transition: all 0.2s ease;
  margin-top: 10px;
}

.btn-quit:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(220, 38, 38, 0.4);
}

.waiting-state {
  text-align: center;
  padding: 8px 0;
}

.waiting-text {
  font-size: 13px;
  color: var(--text-main);
  margin-bottom: 8px;
}

.waiting-subtext {
  font-size: 11px;
  color: var(--success);
  margin-bottom: 12px;
}

.btn-leave {
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 8px;
  color: #fca5a5;
  font-weight: 600;
  font-size: 11px;
  transition: all 0.2s;
  text-transform: uppercase;
}

.btn-leave:hover {
  background: rgba(239, 68, 68, 0.1);
  color: #f87171;
  border-color: rgba(239, 68, 68, 0.4);
}

/* Action Buttons (Copy/Share) */
.header-btn {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--glass-border);
  color: white;
  font-family: "Outfit", sans-serif;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  font-size: 13px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  backdrop-filter: blur(4px);
}

.header-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.header-btn:active {
  transform: translateY(0);
}

/* Share Input */
#shareLinkInput {
  background: rgba(0, 0, 0, 0.4) !important;
  border: 1px solid var(--glass-border) !important;
  color: #e2e8f0 !important;
  font-family: "Inter", sans-serif;
  font-size: 13px !important;
  padding: 10px 12px !important;
  border-radius: 8px !important;
  transition: all 0.2s;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

#shareLinkInput:focus {
  border-color: var(--primary) !important;
  outline: none;
  box-shadow:
    inset 0 2px 4px rgba(0, 0, 0, 0.3),
    0 0 0 2px rgba(99, 102, 241, 0.2);
}

.controls-info {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--glass-border);
  text-align: center;
  font-size: 11px;
  color: #64748b;
  line-height: 1.6;
}

/* Hidden helper input for match code */
#matchCode {
  display: none;
}

/* Game Code Display */
.game-code-display {
  text-align: center;
  margin-bottom: 12px;
}

.game-code-label {
  font-size: 11px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 4px;
}

.game-code-value {
  font-family: "Outfit", monospace;
  font-size: 28px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 4px;
}

/* Join with Code */
.join-code-container {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--glass-border);
}

.join-code-toggle {
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px dashed var(--glass-border);
  border-radius: 8px;
  color: var(--text-muted);
  font-family: "Inter", sans-serif;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.join-code-toggle:hover {
  border-color: var(--primary);
  color: var(--text-main);
}

.join-code-form {
  display: none;
  flex-direction: column;
  gap: 8px;
}

.join-code-form.show {
  display: flex;
}

.join-code-input {
  width: 100%;
  padding: 12px;
  background: rgba(0, 0, 0, 0.4);
  border: 1px solid var(--glass-border);
  border-radius: 8px;
  color: var(--text-main);
  font-family: "Outfit", monospace;
  font-size: 20px;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 4px;
}

.join-code-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.join-code-input::placeholder {
  color: var(--text-muted);
  opacity: 0.5;
  letter-spacing: 2px;
  font-size: 14px;
}

.btn-join {
  width: 100%;
  padding: 12px;
  background: var(--success);
  border: none;
  border-radius: 8px;
  color: white;
  font-family: "Outfit", sans-serif;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-join:hover {
  background: #0d9669;
  transform: translateY(-1px);
}

.btn-join:disabled {
  background: #334155;
  cursor: not-allowed;
  transform: none;
}

/* Game code row with inline buttons */
.game-code-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

/* Icon buttons */
.btn-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-main);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.btn-icon:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateY(-1px);
}

.btn-icon:active {
  transform: translateY(0);
}

.btn-icon-primary {
  background: var(--primary);
  border-color: var(--primary);
}

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

/* Ko-fi Button */
.share-container {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 12px;
  padding: 16px;
  margin: 10px 0;
  border: 1px solid var(--glass-border);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.share-row {
  display: flex;
  gap: 10px;
  width: 100%;
  justify-content: center;
}

/* Style input to look like a text label */
#shareLinkInput {
  width: 100%;
  background: transparent !important;
  border: none !important;
  color: var(--text-muted) !important;
  font-family: "Outfit", sans-serif !important;
  font-size: 11px !important;
  text-align: center;
  padding: 0 !important;
  height: auto !important;
  margin: 0 !important;
  outline: none !important;
  box-shadow: none !important;
  opacity: 0.7;
  pointer-events: none;
  /* Make it feel like static text */
}

.btn-copy,
.btn-share {
  flex: 1;
  padding: 0 20px;
  height: 48px;
  /* Taller, easier to hit */
  border-radius: 10px;
  font-family: "Outfit", sans-serif;
  font-weight: 800;
  font-size: 14px;
  text-transform: uppercase;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  letter-spacing: 0.5px;
}

.btn-copy {
  background: var(--glass-border);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.btn-copy:hover {
  background: rgba(255, 255, 255, 0.15);
}

.btn-share {
  background: var(--primary);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.btn-share:hover {
  background: var(--primary-hover);
  transform: translateY(-1px);
}

/* Cancel Button Polish */
.btn-leave {
  width: 100%;
  padding: 12px;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 10px;
  color: #fca5a5;
  font-family: "Outfit", sans-serif;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: 1px;
  transition: all 0.2s;
  text-transform: uppercase;
  margin-top: 8px;
}

.btn-leave:hover {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(239, 68, 68, 0.5);
  color: white;
}

/* Game Title */
.game-title {
  font-family: "Outfit", sans-serif;
  font-weight: 900;
  font-size: 56px;
  /* Reduced from 80px */
  letter-spacing: 8px;
  background: linear-gradient(to bottom, #fff 40%, #94a3b8 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 30px rgba(99, 102, 241, 0.4);
  margin-bottom: 5px;
  z-index: 10;
  position: relative;
  pointer-events: none;
  text-transform: uppercase;
}

/* Mobile touch buttons - subtle design for side panel */
.touch-controls {
  display: none;
  /* Hidden by default, shown on mobile */
  flex-direction: column;
  gap: 8px;
  padding: 5px;
  align-items: center;
  justify-content: center;
}

.touch-btn {
  width: 40px;
  height: 80px;
  /* Taller for easier pressing */
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 8px;
  background: rgba(99, 102, 241, 0.2);
  color: rgba(255, 255, 255, 0.7);
  font-size: 18px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  cursor: pointer;
  transition:
    background 0.1s,
    transform 0.1s;
}

.touch-btn:active,
.touch-btn.pressed {
  background: rgba(99, 102, 241, 0.5);
  color: white;
  transform: scale(0.95);
}

/* 
   ========================================
   Global Mobile-First Vertical Layout
   ========================================
*/

body {
  align-items: center;
  overflow: hidden;
}

/* Smaller title globally */
.game-title {
  font-size: 24px;
  letter-spacing: 3px;
  margin-bottom: 5px;
  text-shadow: 0 0 15px rgba(99, 102, 241, 0.3);
}

#mainContainer {
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  padding: 5px 0;
}

.share-row {
  flex-wrap: wrap;
}

.share-row input {
  flex-basis: 100%;
  margin-bottom: 4px;
}

.share-row button {
  flex: 1;
}

#sidePanel {
  width: 95%;
  max-width: 360px;
  order: 2;
  margin-bottom: 5px;
  flex-shrink: 1;
  min-height: 0;
  overflow: hidden;
  /* Use row layout for touch controls by default if visible */
  flex-direction: row;
  align-items: stretch;
}

/* Compact Card */
.panel-card {
  padding: 10px;
  gap: 6px;
  background: rgba(30, 41, 59, 0.85);
  backdrop-filter: blur(10px);
  flex: 1;
}

/* Compact Score */
#score {
  font-size: 32px;
}

#scoreContainer {
  margin-bottom: 0;
}

#winCondition {
  font-size: 9px;
  margin-bottom: 4px;
  margin-top: 2px;
}

/* Hide metrics and controls info globally to save space */
#metrics,
.controls-info {
  display: none;
}

/* Compact buttons */
#playBtn {
  font-size: 14px;
  padding: 10px;
}

.btn-challenge {
  font-size: 11px;
  padding: 8px 14px;
}

.btn-quit,
.btn-leave {
  padding: 8px;
  font-size: 11px;
}

#gameContainer {
  order: 1;
  width: calc(100% - 10px);
  max-width: none;
  flex-shrink: 0;
  flex-grow: 0;
  min-height: 0;
  padding: 0;
  margin: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--glass-border);
  border-radius: 12px;
  box-shadow: var(--shadow-lg);
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.03), rgba(255, 255, 255, 0.01));
  overflow: visible;
  aspect-ratio: 4 / 3;
  max-height: 60vh;
}

#canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 0;
}

/* Mobile-specific join code styling */
.join-code-container {
  margin-top: 8px;
  padding-top: 8px;
}

.join-code-input {
  font-size: 16px;
  padding: 10px;
}

.game-code-value {
  font-size: 24px;
}

/* Hide old buttons if present */
.mobile-btn {
  display: none !important;
}

/* 
   ========================================
   Conditional Visibility
   ========================================
*/

/* Touch Controls: HIDE by default */
.touch-controls {
  display: none !important;
}

/* Show Touch Controls ONLY if enabled */
body.touch-enabled .touch-controls {
  display: flex !important;
}

/* Pop Out Button: Desktop style */
.popout-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  background: rgba(30, 41, 59, 0.6);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  color: rgba(255, 255, 255, 0.8);
  font-family: "Outfit", sans-serif;
  font-weight: 600;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
}

.popout-btn:hover {
  background: rgba(4, 182, 212, 0.2);
  border-color: rgba(6, 182, 212, 0.4);
  color: #fff;
  transform: translateY(-1px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.2);
}

.popout-btn:active {
  transform: translateY(0);
}

/* Hide Popout on Touch Devices */
body.touch-enabled .popout-btn {
  display: none !important;
}

/* Hide Popout if window is small (likely already popped out) */
@media (max-width: 600px) {
  .popout-btn {
    display: none !important;
  }
}
