:root {
  --bg-color: #090d13;
  --card-bg: rgba(22, 27, 34, 0.92);
  --border-color: #30363d;
  --accent: #58a6ff;
  --accent-hover: #79c0ff;
  --danger: #f85149;
  --success: #2ea043;
  --gold: #d29922;
  --purple: #a855f7;
  --text: #c9d1d9;
  --text-bright: #f0f6fc;
  --font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

html {
  min-width: 320px;
  background: #070a0e;
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
}

body {
  min-width: 320px;
  background: radial-gradient(circle at center, #161b22 0%, #070a0e 100%);
  color: var(--text);
  font-family: var(--font-family);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 6px calc(8px + env(safe-area-inset-bottom, 0px));
  overflow-x: hidden;
}

/* Header & Navigation Bar */
.header-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  max-width: 1200px;
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}

.mode-switch {
  display: flex;
  gap: 4px;
  background: rgba(13, 17, 23, 0.85);
  padding: 3px;
  border-radius: 12px;
  border: 1px solid var(--border-color);
}

.mode-btn {
  background: transparent;
  border: none;
  color: var(--text);
  padding: 7px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  flex-shrink: 0;
}

.mode-btn.active {
  background: #238636;
  color: #fff;
  box-shadow: 0 2px 8px rgba(35, 134, 54, 0.4);
}

.user-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(13, 17, 23, 0.85);
  border: 1px solid var(--border-color);
  padding: 6px 12px;
  border-radius: 10px;
  font-size: 0.85rem;
  color: var(--text-bright);
  cursor: pointer;
  transition: all 0.15s ease;
}

.user-badge:hover {
  border-color: var(--accent);
  background: rgba(22, 27, 34, 0.95);
}

.verified-icon {
  color: #58a6ff;
  font-weight: bold;
  margin-left: 2px;
}

/* Common Buttons */
.btn {
  background: linear-gradient(135deg, #238636, #2ea043);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 7px 13px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  box-shadow: 0 4px 12px rgba(35, 134, 54, 0.3);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  white-space: nowrap;
  flex-shrink: 0;
}

.btn:hover {
  background: linear-gradient(135deg, #2ea043, #3fb950);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(1px);
}

.btn-secondary {
  background: #21262d;
  border: 1px solid var(--border-color);
  box-shadow: none;
  color: var(--text);
}

.btn-secondary:hover {
  background: #30363d;
  color: var(--text-bright);
}

.btn-accent {
  background: #1f6feb;
  box-shadow: 0 4px 12px rgba(31, 111, 235, 0.3);
}

.btn-accent:hover {
  background: #388bfd;
}

.btn-gold {
  background: linear-gradient(135deg, #b08800, #d29922);
  color: #fff;
  box-shadow: 0 4px 12px rgba(210, 153, 34, 0.3);
}
.btn-gold:hover {
  background: linear-gradient(135deg, #d29922, #e3b341);
}

.btn-quick {
  background: linear-gradient(135deg, #8a2be2, #a855f7);
  box-shadow: 0 4px 12px rgba(168, 85, 247, 0.3);
}

/* Sub-bars (Multiplayer, Bot, Tetris 99) */
.sub-bar {
  display: none;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 10px 16px;
  margin-bottom: 12px;
  max-width: 1000px;
  width: 100%;
}

.sub-bar.visible {
  display: flex;
}

.mp-input {
  background: #0d1117;
  border: 1px solid var(--border-color);
  color: var(--text-bright);
  padding: 6px 10px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-family: monospace;
  outline: none;
}

.mp-status {
  font-size: 0.85rem;
  color: var(--accent);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #8b949e;
}
.status-dot.online { background: #2ea043; }
.status-dot.connecting { background: #d29922; }
.status-dot.error { background: #f85149; }

/* Main Arena Container & Responsive Grid */
.arena-container {
  display: flex;
  flex-direction: row;
  gap: 16px;
  align-items: flex-start;
  justify-content: center;
  width: 100%;
  max-width: 1400px;
  margin: 0 auto;
}

.player-section {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  background: var(--card-bg);
  padding: 16px;
  border-radius: 16px;
  border: 1px solid var(--border-color);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.6), 0 0 20px rgba(88, 166, 255, 0.08);
  position: relative;
  box-sizing: border-box;
}

.game-stage-row {
  display: flex;
  flex-direction: row;
  gap: 14px;
  align-items: flex-start;
  justify-content: center;
  width: 100%;
}

.opponent-section {
  display: none !important;
}

.opponent-section.visible {
  display: flex !important;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 110px;
}

.panel-box {
  background: rgba(13, 17, 23, 0.7);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 6px 4px;
  text-align: center;
}

.panel-title {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: #8b949e;
  margin-bottom: 2px;
}

.panel-value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-bright);
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
}

.media-controls-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  width: 100%;
  margin-top: 2px;
}

.media-btn {
  padding: 4px 3px !important;
  font-size: 0.72rem !important;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  border-radius: 6px !important;
}

.media-select {
  grid-column: span 2;
  width: 100%;
  font-size: 0.7rem !important;
  padding: 3px 4px !important;
  margin-top: 1px;
  border-radius: 6px !important;
}

/* Abilities Bar (2-Level Tactical Mini-Cards exactly under board) */
.abilities-bar {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 6px;
  margin-top: 8px;
  width: 240px;
  max-width: 240px;
  box-sizing: border-box;
  justify-content: space-between;
  align-items: stretch;
}

.ability-btn {
  display: flex !important;
  flex-direction: column !important;
  justify-content: center !important;
  align-items: stretch !important;
  flex: 0 0 76px !important;
  width: 76px !important;
  max-width: 76px !important;
  height: 38px !important;
  min-height: 38px !important;
  padding: 2px 4px !important;
  box-sizing: border-box !important;
  overflow: hidden !important;
  background: rgba(33, 38, 45, 0.95) !important;
  border: 1px solid rgba(88, 166, 255, 0.4) !important;
  border-radius: 6px !important;
  color: #f0f6fc !important;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  transition: all 0.15s ease;
  margin: 0 !important;
}

.ability-btn:hover:not(:disabled) {
  background: rgba(56, 139, 253, 0.25) !important;
  border-color: #58a6ff !important;
  transform: translateY(-1px);
}

.ability-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.ability-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  line-height: 1;
}

.ability-hotkey-icon {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 0.72rem;
  font-weight: 700;
  color: #f0f6fc;
}

.ability-key {
  color: #58a6ff;
  font-size: 0.65rem;
  font-family: monospace;
}

.ability-cost {
  color: #ffd700;
  font-size: 0.65rem;
  font-weight: 700;
  background: rgba(255, 215, 0, 0.15);
  padding: 1px 3px;
  border-radius: 3px;
}

.ability-label {
  font-size: 0.62rem;
  color: #8b949e;
  font-weight: 600;
  text-align: center;
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

.mini-canvas {
  width: 80px;
  height: 64px;
  margin: 0 auto;
  display: block;
  background: #0d1117;
  border-radius: 4px;
}

/* ==========================================================================
   FlyConnectome 3D Brain Visualizer & Telemetry Section (Dedicated 3rd Column)
   ========================================================================== */
.fly-brain-section {
  display: none;
  flex-direction: column;
  width: 310px;
  max-width: 320px;
  background: var(--card-bg);
  border: 1px solid rgba(56, 189, 248, 0.4);
  border-radius: 16px;
  padding: 14px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.6), 0 0 25px rgba(56, 189, 248, 0.12);
  box-sizing: border-box;
  animation: flyHudGlow 3.5s infinite alternate;
  gap: 10px;
}

@keyframes flyHudGlow {
  0% { border-color: rgba(56, 189, 248, 0.35); box-shadow: 0 0 15px rgba(56, 189, 248, 0.15); }
  100% { border-color: rgba(251, 146, 60, 0.5); box-shadow: 0 0 22px rgba(251, 146, 60, 0.25); }
}

.fly-brain-header {
  font-size: 0.78rem;
  font-weight: 800;
  color: #38bdf8;
  letter-spacing: 0.8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-transform: uppercase;
}

.fly-pulse-dot {
  width: 8px;
  height: 8px;
  background: #22c55e;
  border-radius: 50%;
  box-shadow: 0 0 8px #22c55e;
  animation: flyDotPulse 1s infinite alternate;
}

@keyframes flyDotPulse {
  0% { opacity: 0.4; transform: scale(0.8); }
  100% { opacity: 1; transform: scale(1.3); }
}

.fly-canvas-box {
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid rgba(56, 189, 248, 0.25);
  background: #030712;
}

.fly-brain-canvas-large {
  width: 100%;
  height: 180px;
  display: block;
  background: #030712;
}

.fly-metrics-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  width: 100%;
}

.fly-card {
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.fly-card.full-width {
  grid-column: span 2;
}

.fly-card-label {
  font-size: 0.62rem;
  color: #8b949e;
  font-weight: 600;
  text-transform: uppercase;
}

.fly-card-value {
  font-size: 0.95rem;
  font-weight: 700;
  font-family: monospace;
}

.text-amber { color: #fbbf24; }
.text-sky { color: #38bdf8; }
.text-emerald { color: #4ade80; }
.text-gold { color: #ffd700; }
.text-cyan { color: #22d3ee; }

.fly-clusters-breakdown {
  background: rgba(13, 17, 23, 0.7);
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 6px 8px;
}

.cluster-title {
  font-size: 0.65rem;
  color: #8b949e;
  font-weight: 700;
  margin-bottom: 4px;
}

.cluster-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.cluster-item {
  font-size: 0.62rem;
  color: #c9d1d9;
  display: flex;
  align-items: center;
  gap: 5px;
}

.cluster-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.cluster-dot.visual { background: #38bdf8; box-shadow: 0 0 4px #38bdf8; }
.cluster-dot.mushroom { background: #fb923c; box-shadow: 0 0 4px #fb923c; }
.cluster-dot.central { background: #4ade80; box-shadow: 0 0 4px #4ade80; }
.cluster-dot.dopamine { background: #ffd700; box-shadow: 0 0 4px #ffd700; }
.cluster-dot.motor { background: #f43f5e; box-shadow: 0 0 4px #f43f5e; }

.fly-stream-box {
  background: #030712;
  border: 1px solid rgba(56, 189, 248, 0.2);
  border-radius: 6px;
  padding: 6px 8px;
}

.stream-title {
  font-size: 0.62rem;
  color: #8b949e;
  font-weight: 700;
  margin-bottom: 2px;
}

.fly-thought-stream {
  font-size: 0.65rem;
  color: #38bdf8;
  font-family: monospace;
  line-height: 1.3;
  min-height: 28px;
}

/* Board Wrappers & Canvases */
.board-wrapper {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid #30363d;
}

#tetrisCanvas, #opponentCanvas {
  background: #090d12;
  display: block;
  box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.9);
}

.board-wrapper.slow-motion-active {
  box-shadow: 0 0 25px rgba(0, 195, 255, 0.9), inset 0 0 15px rgba(0, 195, 255, 0.5) !important;
  border-color: #00c3ff !important;
  animation: slowPulse 1.2s infinite alternate;
}

@keyframes slowPulse {
  0% { box-shadow: 0 0 15px rgba(0, 195, 255, 0.6); }
  100% { box-shadow: 0 0 30px rgba(0, 195, 255, 1); }
}

/* Multi-Player Grid (2 to 99 Players Arena) */
.multiplayer-grid-section {
  display: none;
  flex-direction: column;
  gap: 8px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 12px;
  max-width: 540px;
  width: 100%;
}

.multiplayer-grid-section.visible {
  display: flex;
}

.opponents-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 10px;
  max-height: 480px;
  overflow-y: auto;
  padding: 4px;
}

.opp-mini-card {
  background: #0d1117;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
}

.opp-mini-card.eliminated {
  opacity: 0.5;
  border-color: #f85149;
}

.opp-mini-card.eliminated::after {
  content: 'K.O.';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #f85149;
  font-weight: 900;
  font-size: 1.2rem;
}

/* Garbage Gauge */
.garbage-gauge {
  width: 8px;
  background: #161b22;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  display: flex;
  flex-direction: column-reverse;
  overflow: hidden;
  margin-left: -6px;
  margin-right: 4px;
}

.garbage-fill {
  width: 100%;
  background: linear-gradient(to top, #d29922, #f85149);
  transition: height 0.2s ease;
  height: 0%;
}

/* Floating FX Layer */
.fx-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: hidden;
  z-index: 5;
}

.floating-text {
  position: absolute;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 1px;
  text-shadow: 0 0 10px rgba(0, 0, 0, 0.9), 0 0 20px currentColor;
  animation: floatUp 0.85s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards;
  transform-origin: center;
}

@keyframes floatUp {
  0% { opacity: 0; transform: translateY(10px) scale(0.6); }
  20% { opacity: 1; transform: translateY(0px) scale(1.2); }
  80% { opacity: 1; transform: translateY(-25px) scale(1); }
  100% { opacity: 0; transform: translateY(-40px) scale(0.8); }
}

/* Overlay & Modals */
.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(13, 17, 23, 0.92);
  backdrop-filter: blur(5px);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.overlay.active {
  opacity: 1;
  pointer-events: all;
}

.overlay h2 {
  color: var(--text-bright);
  font-size: 1.5rem;
  text-transform: uppercase;
  letter-spacing: 2px;
  text-align: center;
  padding: 0 8px;
}

.modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#profileModal {
  z-index: 300 !important;
}

.interactive-toast,
#interactiveToastContainer,
.score-toast {
  z-index: 1000 !important;
}

.modal-backdrop.open {
  opacity: 1;
  pointer-events: all;
}

.modal-box {
  background: #161b22;
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 20px;
  max-width: 540px;
  width: 90%;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8), 0 0 30px rgba(88, 166, 255, 0.15);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-title {
  font-size: 1.25rem;
  font-weight: 800;
  color: var(--text-bright);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Inputs & Form Elements inside Modals */
.name-input {
  background: #0d1117 !important;
  border: 1px solid var(--border-color) !important;
  color: #f0f6fc !important;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 0.95rem;
  outline: none;
  width: 100%;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.name-input::placeholder {
  color: #6e7681;
}

.name-input:focus {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 3px rgba(88, 166, 255, 0.25);
}

.auth-tab-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  padding: 8px 16px;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s ease;
}

.auth-tab-btn:hover {
  color: var(--text-bright);
}

.auth-tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* Chat Box */
.multiplayer-chat-section {
  display: none;
  flex-direction: column;
  width: 280px;
  background: var(--card-bg);
  border: 1px solid var(--border-color);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.6), 0 0 20px rgba(88, 166, 255, 0.08);
  box-sizing: border-box;
  align-self: stretch;
  min-height: 520px;
}

.chat-container {
  display: flex;
  flex-direction: column;
  flex: 1;
  height: 100%;
  background: #090d12;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  overflow: hidden;
}

.chat-messages {
  flex: 1;
  padding: 8px;
  overflow-y: auto;
  font-size: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 380px;
}

.chat-input-row {
  display: flex;
  border-top: 1px solid var(--border-color);
}

.chat-input {
  flex: 1;
  background: transparent;
  border: none;
  padding: 8px;
  color: var(--text-bright);
  font-size: 0.85rem;
  outline: none;
}

/* Toast */
.score-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(22, 27, 34, 0.95);
  border: 1px solid #30363d;
  border-radius: 10px;
  padding: 10px 20px;
  color: #f0f6fc;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6), 0 0 15px rgba(88, 166, 255, 0.2);
  z-index: 1000;
  font-size: 0.95rem;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

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

.score-toast.is-record {
  border-color: #d29922;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.6), 0 0 20px rgba(210, 153, 34, 0.35);
  color: #e3b341;
}

/* Badges & Tables */
.mode-badge {
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 6px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.badge-abilities { background: rgba(168, 85, 247, 0.2); color: #c084fc; border: 1px solid rgba(168, 85, 247, 0.4); }
.badge-battle { background: rgba(248, 81, 73, 0.2); color: #ff7b72; border: 1px solid rgba(248, 81, 73, 0.4); }
.badge-sprint { background: rgba(88, 166, 255, 0.2); color: #79c0ff; border: 1px solid rgba(88, 166, 255, 0.4); }
.badge-ultra { background: rgba(227, 179, 65, 0.2); color: #e3b341; border: 1px solid rgba(227, 179, 65, 0.4); }
.badge-cheese { background: rgba(46, 160, 67, 0.2); color: #56d364; border: 1px solid rgba(46, 160, 67, 0.4); }

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.leaderboard-table th {
  text-align: left;
  padding: 8px;
  color: #8b949e;
  border-bottom: 1px solid var(--border-color);
  font-size: 0.75rem;
  text-transform: uppercase;
}

.leaderboard-table td {
  padding: 8px;
  border-bottom: 1px solid rgba(48, 54, 61, 0.5);
  color: var(--text);
}

.clickable-row {
  cursor: pointer;
  transition: background 0.15s ease;
}
.clickable-row:hover td {
  background: rgba(88, 166, 255, 0.1);
  color: #fff;
}

.t99-swarm-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(13, 17, 23, 0.7);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 6px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
}

.t99-target-btn {
  background: #21262d;
  border: 1px solid #30363d;
  color: #c9d1d9;
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.t99-target-btn.active {
  background: #1f6feb;
  color: #fff;
  border-color: #58a6ff;
  box-shadow: 0 0 10px rgba(88, 166, 255, 0.4);
}

.controls-help {
  margin-top: 14px;
  font-size: 0.75rem;
  color: #8b949e;
  text-align: center;
  line-height: 1.6;
}

.key {
  display: inline-block;
  background: #21262d;
  border: 1px solid #30363d;
  border-radius: 4px;
  padding: 1px 6px;
  color: var(--text-bright);
  font-family: monospace;
  font-size: 0.75rem;
}

/* ==========================================================================
   Virtual Touch Controls (Mobile HUD)
   ========================================================================== */
.touch-controls {
  width: 100%;
  max-width: 480px;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
  margin-top: 6px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  z-index: 20;
}

.touch-cluster {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.touch-left {
  align-items: flex-start;
}

.touch-right {
  align-items: flex-end;
}

.touch-center {
  flex-direction: row;
  gap: 4px;
  align-self: center;
  margin-bottom: 2px;
}

.touch-dpad {
  display: flex;
  gap: 5px;
}

.touch-row-actions {
  display: flex;
  gap: 5px;
}

.touch-btn {
  background: rgba(33, 38, 45, 0.9);
  border: 1.5px solid rgba(88, 166, 255, 0.35);
  color: #f0f6fc;
  border-radius: 12px;
  font-weight: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform 0.05s ease, background 0.1s ease, border-color 0.1s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), inset 0 1px 1px rgba(255, 255, 255, 0.1);
}

.touch-btn:active,
.touch-btn.active {
  background: rgba(88, 166, 255, 0.35);
  border-color: #58a6ff;
  transform: scale(0.93);
  box-shadow: 0 0 12px rgba(88, 166, 255, 0.6);
}

/* D-Pad Arrow Buttons */
.touch-btn-dir {
  width: 48px;
  height: 48px;
  font-size: 1.25rem;
  background: rgba(22, 27, 34, 0.92);
}

/* Hold Button */
.touch-btn-hold {
  width: 100%;
  height: 36px;
  font-size: 0.75rem;
  flex-direction: row;
  gap: 4px;
  background: rgba(46, 32, 70, 0.85);
  border-color: rgba(187, 128, 255, 0.4);
  color: #d2a8ff;
  border-radius: 8px;
  margin-bottom: 2px;
}

/* Rotation Buttons */
.touch-btn-rot {
  width: 46px;
  height: 46px;
  font-size: 1.3rem;
  background: rgba(31, 111, 235, 0.25);
  border-color: rgba(88, 166, 255, 0.5);
  color: #58a6ff;
}

.touch-btn-primary-rot {
  width: 52px;
  height: 52px;
  background: rgba(31, 111, 235, 0.4);
  border-color: #58a6ff;
  color: #fff;
  font-size: 1.45rem;
}

/* Hard Drop Button */
.touch-btn-harddrop {
  width: 104px;
  height: 42px;
  font-size: 0.85rem;
  background: rgba(218, 54, 51, 0.25);
  border-color: rgba(248, 81, 73, 0.5);
  color: #ff7b72;
  border-radius: 10px;
}

.touch-btn-harddrop:active,
.touch-btn-harddrop.active {
  background: rgba(248, 81, 73, 0.5);
  border-color: #f85149;
  box-shadow: 0 0 14px rgba(248, 81, 73, 0.7);
}

/* Mobile Abilities Buttons */
.touch-btn-sub {
  width: 38px;
  height: 38px;
  font-size: 1.05rem;
  border-radius: 8px;
  background: rgba(210, 153, 34, 0.2);
  border-color: rgba(210, 153, 34, 0.5);
  color: #ffd700;
}

/* ==========================================================================
   Responsive Mobile Media Queries
   ========================================================================== */
@media (max-width: 900px) {
  .arena-container {
    flex-direction: column;
    align-items: center;
  }
  .player-section {
    padding: 10px;
    gap: 8px;
  }
  .side-panel {
    width: 80px;
  }
}

@media (max-width: 600px) {
  body {
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    justify-content: flex-start;
  }

  .header-bar {
    width: 100%;
    max-width: 100vw;
    overflow-x: auto;
    flex-wrap: nowrap;
    justify-content: flex-start;
    padding: 2px 4px 6px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    margin-bottom: 8px;
  }
  .header-bar::-webkit-scrollbar {
    display: none;
  }

  .mode-switch {
    flex-shrink: 0;
    display: flex;
    gap: 3px;
  }
  .mode-btn {
    padding: 5px 9px;
    font-size: 0.75rem;
    white-space: nowrap;
    flex-shrink: 0;
  }
  .header-bar > .btn,
  .header-bar > div {
    flex-shrink: 0;
    font-size: 0.75rem;
    padding: 5px 9px;
    white-space: nowrap;
  }

  .sub-bar {
    width: 100%;
    max-width: 100vw;
    overflow-x: auto;
    flex-wrap: nowrap;
    justify-content: flex-start;
    padding: 6px 8px;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
    margin-bottom: 6px;
  }
  .sub-bar::-webkit-scrollbar {
    display: none;
  }
  .sub-bar > * {
    flex-shrink: 0;
    white-space: nowrap;
  }

  .arena-container {
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 0 auto;
  }

  .player-section {
    padding: 6px 4px;
    gap: 6px;
    border-radius: 12px;
    width: auto;
    max-width: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    flex-shrink: 0;
  }

  .game-stage-row {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 6px;
    align-items: flex-start;
    justify-content: center;
    width: 100%;
  }

  .side-panel {
    width: 58px;
    gap: 4px;
    flex-shrink: 0;
  }

  .panel-box {
    padding: 3px 2px;
    border-radius: 6px;
  }

  .panel-title {
    font-size: 0.58rem;
    letter-spacing: 0;
  }

  .panel-value {
    font-size: 0.9rem;
  }

  .mini-canvas {
    width: 50px;
    height: 40px;
  }

  .board-wrapper {
    border-radius: 8px;
  }

  #tetrisCanvas,
  #opponentCanvas {
    width: 160px;
    height: 320px;
    max-width: 48vw;
    flex-shrink: 0;
  }

  .garbage-bar {
    width: 5px;
  }

  .abilities-bar {
    display: none !important; /* Controlled by Touch HUD on mobile */
  }

  .controls-help {
    display: none;
  }

  .touch-controls {
    width: 100%;
    max-width: 360px;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-top: 4px;
    padding: 2px 2px calc(4px + env(safe-area-inset-bottom, 0px));
  }

  .touch-btn-dir {
    width: 42px;
    height: 42px;
    font-size: 1.1rem;
  }

  .touch-btn-hold {
    height: 32px;
    font-size: 0.7rem;
  }

  .touch-btn-rot {
    width: 40px;
    height: 40px;
    font-size: 1.15rem;
  }

  .touch-btn-primary-rot {
    width: 46px;
    height: 46px;
    font-size: 1.3rem;
  }

  .touch-btn-harddrop {
    width: 90px;
    height: 36px;
    font-size: 0.75rem;
  }

  .touch-btn-sub {
    width: 34px;
    height: 34px;
    font-size: 0.95rem;
  }

  .modal-box {
    width: 95%;
    max-width: 95vw;
    max-height: 90vh;
    padding: 14px 10px;
  }

  .t99-swarm-panel {
    max-width: 95vw;
    margin-bottom: 8px;
  }
}
