:root {
  --board-green: #1e4231;
  --chalk-yellow: #ffd54f;
  --danger-red: #ef4444;
  --student-green: #10b981;
  --glass-bg: rgba(15, 23, 36, 0.80);
  --glass-border: rgba(255, 255, 255, 0.16);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-touch-callout: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  touch-action: none;
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  background: #101820;
  color: #f8fafc;
}

#canvas-container {
  position: fixed;
  inset: 0;
  z-index: 1;
}

#danger-vignette {
  position: fixed;
  inset: 0;
  z-index: 4;
  pointer-events: none;
  box-shadow: inset 0 0 110px rgba(239, 68, 68, 0);
  transition: box-shadow 0.2s ease;
}

#danger-vignette.active {
  animation: dangerPulse 0.65s infinite alternate;
}

@keyframes dangerPulse {
  from { box-shadow: inset 0 0 80px rgba(239, 68, 68, 0.35); }
  to { box-shadow: inset 0 0 155px rgba(239, 68, 68, 0.78); }
}

.hidden { display: none !important; }

/* Top-Right Telemetry & Mobile Toggle */
#perf-badge {
  position: fixed;
  top: 10px;
  right: 12px;
  z-index: 60;
  display: flex;
  align-items: center;
  gap: 7px;
  background: rgba(15, 23, 36, 0.78);
  backdrop-filter: blur(8px);
  border: 1px solid var(--glass-border);
  padding: 5px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  color: #cbd5e1;
}

#perf-badge .sep { opacity: 0.35; }

.toggle-chip {
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.22);
  color: #f8fafc;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  cursor: pointer;
}

.toggle-chip.on {
  background: #10b981;
  border-color: #34d399;
  color: #052e16;
}

#mute-btn {
  background: none;
  border: none;
  color: #fff;
  cursor: pointer;
  font-size: 14px;
}

/* Overlay Cards */
.overlay-screen {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at center, rgba(15, 23, 42, 0.25), rgba(10, 15, 28, 0.72));
  padding: 14px;
  overflow-y: auto;
}

.card {
  background: var(--glass-bg);
  backdrop-filter: blur(18px);
  border: 1px solid var(--glass-border);
  border-radius: 22px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.48);
  padding: 24px 28px;
  max-width: 96vw;
  max-height: 92vh;
  overflow-y: auto;
}

.home-card { width: 550px; text-align: center; }

.stamp {
  display: inline-block;
  background: linear-gradient(135deg, #b91c1c, #dc2626);
  color: #fef08a;
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.6px;
  padding: 5px 14px;
  border-radius: 999px;
  border: 1.5px solid #fde047;
  margin-bottom: 10px;
}

.game-title {
  font-size: 38px;
  font-weight: 900;
  letter-spacing: 1px;
  line-height: 1.15;
}
.game-title span { color: var(--chalk-yellow); }

.subtitle {
  color: #cbd5e1;
  font-size: 13.5px;
  margin-top: 7px;
  margin-bottom: 18px;
}

.input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.input-row label { font-size: 14px; font-weight: 700; white-space: nowrap; }

input[type="text"] {
  flex: 1;
  background: rgba(15, 23, 42, 0.72);
  border: 1.5px solid rgba(255, 255, 255, 0.22);
  border-radius: 11px;
  color: #fff;
  padding: 10px 13px;
  font-size: 15px;
  outline: none;
  user-select: text;
}
input[type="text"]:focus { border-color: var(--chalk-yellow); }

.btn {
  border: none;
  border-radius: 12px;
  padding: 11px 16px;
  font-size: 14.5px;
  font-weight: 800;
  cursor: pointer;
  color: #fff;
  transition: transform 0.12s, filter 0.15s;
}
.btn:hover { filter: brightness(1.08); transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }

.btn-green { background: linear-gradient(135deg, #059669, #10b981); }
.btn-red { background: linear-gradient(135deg, #b91c1c, #ef4444); }
.btn-primary { background: linear-gradient(135deg, #2563eb, #3b82f6); }
.btn-amber { background: linear-gradient(135deg, #d97706, #f59e0b); color: #111827; }
.btn-ghost { background: rgba(255, 255, 255, 0.12); border: 1px solid rgba(255, 255, 255, 0.2); }
.btn-sm { padding: 7px 13px; font-size: 13px; border-radius: 9px; }

.quick-solo-group { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.divider { margin: 16px 0 12px; position: relative; text-align: center; }
.divider::before {
  content: ""; position: absolute; left: 0; right: 0; top: 50%;
  height: 1px; background: rgba(255, 255, 255, 0.16);
}
.divider span {
  position: relative; background: #172132; padding: 0 12px;
  font-size: 12px; color: #94a3b8; border-radius: 99px;
}

.multi-row { display: grid; grid-template-columns: 1fr 1.15fr; gap: 10px; }
.join-box { display: flex; gap: 6px; }
.join-box input { width: 96px; text-transform: uppercase; letter-spacing: 2px; text-align: center; font-weight: 800; }

.error-banner {
  margin-top: 10px;
  background: rgba(239, 68, 68, 0.24);
  border: 1px solid #ef4444;
  color: #fecaca;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 13px;
}

.rules-mini {
  margin-top: 15px;
  text-align: left;
  background: rgba(30, 66, 49, 0.58);
  border: 1px solid rgba(167, 243, 208, 0.25);
  border-radius: 12px;
  padding: 11px 13px;
  font-size: 12.5px;
  line-height: 1.65;
  color: #e2e8f0;
}

/* Lobby */
.lobby-card { width: 760px; }
.lobby-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; flex-wrap: wrap; gap: 8px; }
.tag { font-size: 12px; color: var(--chalk-yellow); font-weight: 700; }
.code-pill { background: #ffd54f; color: #111827; padding: 2px 11px; border-radius: 8px; letter-spacing: 3px; font-weight: 900; }
.lobby-actions-top { display: flex; gap: 8px; }

.role-selector { display: flex; align-items: center; gap: 7px; margin-bottom: 12px; font-size: 13px; flex-wrap: wrap; }
.role-btn {
  background: rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.2);
  color: #e2e8f0; padding: 6px 12px; border-radius: 999px; cursor: pointer; font-size: 13px; font-weight: 700;
}
.role-btn.active { background: var(--chalk-yellow); color: #111827; border-color: var(--chalk-yellow); }

.lobby-grid { display: grid; grid-template-columns: 1.1fr 1fr; gap: 14px; margin-bottom: 14px; }
.players-panel, .chat-panel {
  background: rgba(15, 23, 42, 0.55); border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px; padding: 12px; display: flex; flex-direction: column; height: 240px;
}
.players-panel h3, .chat-panel h3 { font-size: 14px; margin-bottom: 8px; color: #fde047; }
#player-list { list-style: none; flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
#player-list li {
  display: flex; justify-content: space-between; align-items: center;
  background: rgba(255, 255, 255, 0.08); padding: 7px 10px; border-radius: 9px; font-size: 13px;
}
.badge-ready { color: #34d399; font-weight: 800; font-size: 12px; }
.badge-pref { color: #93c5fd; font-size: 12px; margin-left: 5px; }
#chat-log { flex: 1; overflow-y: auto; font-size: 13px; line-height: 1.5; margin-bottom: 8px; }
.chat-input-row { display: flex; gap: 6px; }
.chat-input-row input { padding: 7px 10px; font-size: 13px; }
.hint-text { font-size: 12px; color: #94a3b8; margin-top: 6px; }
.lobby-footer { display: flex; justify-content: space-between; align-items: center; }

/* ==================== IN-GAME HUD ==================== */
#game-hud {
  position: fixed;
  inset: 0;
  z-index: 15;
  pointer-events: none;
}

/* TOP-LEFT: Chaos Bar + Real-Time Score Ranking */
#top-left-panel {
  position: absolute;
  top: 12px;
  left: 12px;
  width: 248px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tl-role-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.hud-role-badge {
  background: rgba(15, 23, 36, 0.85);
  border: 2px solid var(--chalk-yellow);
  padding: 5px 11px;
  border-radius: 11px;
  font-weight: 900;
  font-size: 13.5px;
}

.tl-alive-pill {
  background: rgba(15, 23, 36, 0.82);
  border: 1px solid var(--glass-border);
  padding: 5px 10px;
  border-radius: 10px;
  font-size: 12px;
  color: #cbd5e1;
}
.tl-alive-pill b { color: #fde047; font-size: 13.5px; }

.tl-chaos-card, .tl-rank-card {
  background: rgba(15, 23, 36, 0.78);
  backdrop-filter: blur(10px);
  border: 1px solid var(--glass-border);
  border-radius: 13px;
  padding: 8px 11px;
}

.chaos-header {
  display: flex;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 800;
  margin-bottom: 5px;
}

.chaos-track {
  height: 11px;
  background: rgba(255, 255, 255, 0.14);
  border-radius: 999px;
  overflow: hidden;
}

#hud-chaos-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #f59e0b, #ef4444);
  transition: width 0.12s linear;
}

.rank-title {
  font-size: 12px;
  font-weight: 800;
  color: #fde047;
  margin-bottom: 6px;
}

#live-rank-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 168px;
  overflow-y: auto;
}

#live-rank-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  padding: 3px 6px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
}

#live-rank-list li.is-me {
  background: rgba(250, 204, 21, 0.22);
  border: 1px solid rgba(250, 204, 21, 0.55);
}

#live-rank-list li.is-caught {
  opacity: 0.7;
  background: rgba(220, 38, 38, 0.26);
}

.rk-score { font-weight: 900; color: #34d399; font-variant-numeric: tabular-nums; }

/* TOP-CENTER TIMER */
#top-center-timer {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 36, 0.82);
  backdrop-filter: blur(10px);
  border: 1px solid var(--glass-border);
  border-radius: 14px;
  padding: 5px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
#top-center-timer .label { font-size: 10.5px; color: #94a3b8; }
#hud-timer { font-size: 22px; font-weight: 900; font-variant-numeric: tabular-nums; }
#hud-timer.urgent { color: #f87171; animation: dangerPulse 0.45s infinite alternate; }

#crosshair {
  position: absolute;
  top: 50%; left: 50%;
  width: 18px; height: 18px;
  transform: translate(-50%, -50%);
  border: 2px solid rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  box-shadow: 0 0 6px rgba(0, 0, 0, 0.65);
}
#crosshair::after {
  content: ""; position: absolute; top: 50%; left: 50%;
  width: 4px; height: 4px; background: #fde047; border-radius: 50%;
  transform: translate(-50%, -50%);
}

.lock-hint {
  position: absolute;
  top: 66px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 36, 0.84);
  border: 1px solid var(--chalk-yellow);
  color: #fef08a;
  padding: 6px 16px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 700;
  white-space: nowrap;
}

.context-prompt {
  position: absolute;
  bottom: 108px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(16, 185, 129, 0.92);
  color: #052e16;
  border: 2px solid #ecfdf5;
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 900;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  white-space: nowrap;
}

#event-feed {
  position: absolute;
  top: 56px;
  right: 12px;
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.feed-item {
  background: rgba(15, 23, 36, 0.84);
  border-left: 4px solid #38bdf8;
  padding: 7px 10px;
  border-radius: 8px;
  font-size: 12.5px;
  font-weight: 600;
  animation: slideIn 0.2s ease-out;
}
.feed-item.caught { border-left-color: #ef4444; background: rgba(127, 29, 29, 0.88); }
.feed-item.warn { border-left-color: #facc15; }
.feed-item.student { border-left-color: #10b981; background: rgba(6, 78, 59, 0.88); }

@keyframes slideIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

/* PC Bottom HUD Bars */
.hud-bottom {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 9px;
  pointer-events: auto;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 97vw;
}

.stat-pill, .skill-pill, .controls-pill {
  background: rgba(15, 23, 36, 0.82);
  backdrop-filter: blur(10px);
  border: 1px solid var(--glass-border);
  padding: 8px 12px;
  border-radius: 12px;
  font-size: 12.5px;
  display: flex;
  align-items: center;
  gap: 7px;
}
.stat-pill.compact { flex-direction: column; align-items: flex-start; gap: 2px; padding: 6px 11px; }

.stam-track {
  width: 82px;
  height: 10px;
  background: rgba(255, 255, 255, 0.16);
  border-radius: 999px;
  overflow: hidden;
}
#teacher-stam-fill {
  height: 100%;
  width: 100%;
  background: linear-gradient(90deg, #10b981, #34d399);
  transition: width 0.1s linear;
}
#teacher-stam-fill.low { background: linear-gradient(90deg, #dc2626, #f59e0b); }

.pips { display: flex; gap: 4px; }
.pip { width: 9px; height: 20px; border-radius: 3px; background: #fff; }
.pip.empty { background: rgba(255, 255, 255, 0.18); }

.prank-hotbar { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }

.prank-btn {
  background: rgba(15, 23, 36, 0.86);
  border: 2px solid rgba(255, 255, 255, 0.22);
  color: #f8fafc;
  padding: 8px 11px;
  border-radius: 11px;
  font-size: 12.5px;
  font-weight: 800;
  cursor: pointer;
}
.prank-btn.active {
  background: rgba(245, 158, 11, 0.94);
  color: #111827;
  border-color: #fef08a;
  transform: translateY(-3px);
}

kbd {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: inherit;
  font-size: 11.5px;
  font-weight: 800;
}

/* ==================== MOBILE VIRTUAL CONTROLS ==================== */
#mobile-controls {
  position: fixed;
  inset: 0;
  z-index: 25;
  pointer-events: none;
}

#mobile-joystick-zone {
  position: absolute;
  left: 22px;
  bottom: 24px;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

#joy-base {
  position: relative;
  width: 132px;
  height: 132px;
  border-radius: 50%;
  background: rgba(15, 23, 36, 0.56);
  border: 2px solid rgba(255, 255, 255, 0.34);
  backdrop-filter: blur(6px);
  touch-action: none;
}

.joy-arrow {
  position: absolute;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.45);
  pointer-events: none;
}
.joy-arrow.up { top: 8px; left: 50%; transform: translateX(-50%); }
.joy-arrow.down { bottom: 8px; left: 50%; transform: translateX(-50%); }
.joy-arrow.left { left: 10px; top: 50%; transform: translateY(-50%); }
.joy-arrow.right { right: 10px; top: 50%; transform: translateY(-50%); }

#joy-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 52px;
  height: 52px;
  margin-left: -26px;
  margin-top: -26px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fef08a, #f59e0b);
  border: 2px solid #ffffff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}

.joy-label {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.78);
  text-shadow: 0 1px 4px #000;
}

.mobile-action-pad {
  position: absolute;
  right: 18px;
  bottom: 22px;
  pointer-events: auto;
  display: grid;
  grid-template-columns: repeat(3, 66px);
  gap: 9px;
}

.m-btn {
  width: 66px;
  height: 66px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.45);
  background: rgba(15, 23, 36, 0.78);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  cursor: pointer;
  touch-action: none;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.4);
}

.m-btn span { font-size: 18px; line-height: 1.1; }
.m-btn small { font-size: 10px; opacity: 0.9; margin-top: 2px; }
.m-btn:active, .m-btn.active { transform: scale(0.92); filter: brightness(1.22); border-color: #fde047; }

.m-btn-main { background: linear-gradient(135deg, #d97706, #f59e0b); color: #111827; }
.m-btn-red { background: linear-gradient(135deg, #b91c1c, #ef4444); color: #fff; }
.m-btn-blue { background: linear-gradient(135deg, #1d4ed8, #3b82f6); }
.m-btn-purple { background: linear-gradient(135deg, #7e22ce, #a855f7); }
.m-btn-green { background: linear-gradient(135deg, #047857, #10b981); }
.m-btn-amber { background: linear-gradient(135deg, #b45309, #fbbf24); color: #111827; }
.m-btn-gray { background: rgba(71, 85, 105, 0.85); }

/* End Screen */
.end-card { width: 580px; text-align: center; }
.end-stats-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 16px; }
.mini-stat {
  background: rgba(255, 255, 255, 0.08); border-radius: 11px; padding: 9px 6px;
  display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: #94a3b8;
}
.mini-stat b { font-size: 15px; color: #fff; }
.score-table { width: 100%; border-collapse: collapse; margin-bottom: 18px; font-size: 13.5px; }
.score-table th, .score-table td { padding: 8px; border-bottom: 1px solid rgba(255, 255, 255, 0.12); }
.score-table th { color: #fde047; font-size: 12.5px; }
.end-actions { display: flex; justify-content: center; gap: 12px; }

/* Responsive Small Screens */
@media (max-width: 768px) {
  #top-left-panel { width: 185px; top: 8px; left: 8px; }
  .tl-rank-card { padding: 6px 8px; }
  #live-rank-list { max-height: 98px; }
  #event-feed { width: 195px; top: 48px; right: 8px; }
  .lobby-grid { grid-template-columns: 1fr; }
  .players-panel, .chat-panel { height: 170px; }
  .game-title { font-size: 28px; }
  .quick-solo-group, .multi-row { grid-template-columns: 1fr; }
  .context-prompt { bottom: 178px; font-size: 12.5px; }
}
