:root {
  --bg-0: #09090b;
  --bg-1: #111113;
  --bg-2: #18181b;
  --bg-3: #1f1f23;
  --bg-4: #27272a;
  --border: #2e2e32;
  --border-hover: #3f3f46;
  --text: #fafafa;
  --text-2: #a1a1aa;
  --text-3: #71717a;
  --accent: #f97316;
  --accent-hover: #fb923c;
  --accent-dim: rgba(249,115,22,0.15);
  --green: #22c55e;
  --green-dim: rgba(34,197,94,0.15);
  --red: #ef4444;
  --red-dim: rgba(239,68,68,0.15);
  --blue: #3b82f6;
  --blue-dim: rgba(59,130,246,0.15);
  --purple: #a855f7;
  --yellow: #eab308;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: 150ms cubic-bezier(0.4,0,0.2,1);
  --font: 'Inter', -apple-system, system-ui, sans-serif;
  --mono: 'JetBrains Mono', monospace;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  background: var(--bg-0);
  color: var(--text);
  font-family: var(--font);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

#app { min-height: 100vh; }

.metrics-shortcut {
  position: fixed; right: 16px; bottom: 16px; z-index: 20;
  color: var(--text-3); background: rgba(17,17,19,0.9);
  border: 1px solid var(--border); border-radius: 999px;
  padding: 8px 12px; font: 0.7rem var(--mono); text-decoration: none;
  backdrop-filter: blur(10px); transition: all var(--transition);
}
.metrics-shortcut:hover { color: var(--accent); border-color: var(--accent); }

/* ===== UTILITIES ===== */
.hidden { display: none !important; }
.flex { display: flex; }
.flex-col { flex-direction: column; }
.items-center { align-items: center; }
.justify-center { justify-content: center; }
.gap-xs { gap: 0.25rem; }
.gap-sm { gap: 0.5rem; }
.gap-md { gap: 1rem; }
.gap-lg { gap: 1.5rem; }
.text-center { text-align: center; }
.w-full { width: 100%; }
.font-mono { font-family: var(--mono); }

/* ===== SCREENS ===== */
.screen {
  display: none;
  min-height: 100vh;
}
.screen.active {
  display: flex;
}

/* ===== LOBBY ===== */
#screen-lobby {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2.5rem;
  padding: 2rem;
}

.lobby-brand {
  text-align: center;
}

.lobby-brand h1 {
  font-size: 2.8rem;
  font-weight: 900;
  letter-spacing: -1px;
  background: linear-gradient(135deg, var(--accent), #ef4444, var(--purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1.1;
}

.lobby-brand .tagline {
  color: var(--text-3);
  font-size: 0.95rem;
  margin-top: 0.5rem;
}

.lobby-cards {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
  justify-content: center;
}

.lobby-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.lobby-card h2 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-2);
}

/* ===== INPUTS ===== */
.input {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.9rem;
  color: var(--text);
  font-family: var(--font);
  font-size: 0.9rem;
  outline: none;
  transition: border-color var(--transition);
  width: 100%;
}
.input:focus { border-color: var(--accent); }
.input::placeholder { color: var(--text-3); }

/* ===== BUTTONS ===== */
.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 0.65rem 1.2rem;
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}
.btn:active { transform: scale(0.97); }

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

.btn-ghost {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--border);
}
.btn-ghost:hover { border-color: var(--border-hover); color: var(--text); }

.btn-danger {
  background: var(--red-dim);
  color: var(--red);
  border: 1px solid rgba(239,68,68,0.2);
}
.btn-danger:hover { background: rgba(239,68,68,0.25); }

.btn-success {
  background: var(--green);
  color: #fff;
}
.btn-success:hover { background: #16a34a; }

.btn-lg { padding: 0.8rem 2rem; font-size: 0.95rem; }
.btn-sm { padding: 0.4rem 0.8rem; font-size: 0.75rem; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ===== ROM SELECTOR ===== */
#screen-select {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2rem;
  padding: 2rem;
}

.rom-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
  max-width: 800px;
  width: 100%;
}

.rom-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.8rem;
  transition: all var(--transition);
  cursor: pointer;
}
.rom-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 8px 32px rgba(249,115,22,0.1);
}

.rom-card .icon { font-size: 2.5rem; }
.rom-card .name { font-weight: 600; text-align: center; }
.rom-card .size { font-size: 0.8rem; color: var(--text-3); font-family: var(--mono); }

.rom-upload-zone {
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  text-align: center;
  max-width: 400px;
  width: 100%;
  transition: border-color var(--transition);
  cursor: pointer;
}
.rom-upload-zone:hover { border-color: var(--accent); }
.rom-upload-zone p { color: var(--text-3); font-size: 0.9rem; margin-top: 0.5rem; }

/* ===== ROOM ===== */
#screen-room {
  flex-direction: column;
  padding: 1.5rem 2rem;
  gap: 1.5rem;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.room-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.room-top h2 { font-size: 1.3rem; font-weight: 700; }

.room-code {
  background: var(--accent-dim);
  color: var(--accent);
  padding: 0.35rem 0.9rem;
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-weight: 600;
  font-size: 1.1rem;
  letter-spacing: 3px;
  cursor: pointer;
  transition: background var(--transition);
}
.room-code:hover { background: rgba(249,115,22,0.25); }

.player-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.8rem;
}

.player-slot {
  background: var(--bg-2);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  text-align: center;
  transition: all var(--transition);
  position: relative;
  overflow: hidden;
}
.player-slot::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--slot-color, var(--border));
}
.player-slot.empty {
  border-style: dashed;
  opacity: 0.35;
}
.player-slot .p-name { font-weight: 600; font-size: 0.95rem; }
.player-slot .p-role { font-size: 0.7rem; color: var(--text-3); text-transform: uppercase; letter-spacing: 1px; margin-top: 0.2rem; }
.player-slot .p-status { margin-top: 0.5rem; font-size: 0.8rem; font-weight: 500; }
.player-slot .p-status.ready { color: var(--green); }

/* ===== CHAT ===== */
.chat-box {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  height: 200px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.chat-msg {
  font-size: 0.8rem;
  line-height: 1.4;
}
.chat-msg .from { font-weight: 600; }
.chat-msg .msg { color: var(--text-2); }
.chat-msg.system { color: var(--text-3); font-style: italic; }

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

.chat-input-wrap .input {
  border: none;
  border-radius: 0;
  background: transparent;
}
.chat-input-wrap .input:focus { box-shadow: none; }

.chat-input-wrap .btn {
  border-radius: 0;
  border-left: 1px solid var(--border);
  padding: 0.6rem 1rem;
}

/* ===== CONTROLS GUIDE ===== */
.controls-guide {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.2rem;
}

.controls-guide h3 {
  font-size: 0.85rem;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.controls-layout {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0.3rem 1rem;
  font-size: 0.85rem;
}

.key-badge {
  background: var(--bg-4);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.15rem 0.5rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  text-align: center;
  min-width: 50px;
  color: var(--text);
}

.room-bottom {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}

.room-actions {
  display: flex;
  gap: 0.8rem;
  align-items: flex-end;
}

/* ===== GAME SCREEN ===== */
#screen-game {
  background: #000;
  width: 100vw;
  height: 100vh;
}

.game-layout {
  display: flex;
  width: 100%;
  height: 100%;
  min-height: 100vh;
}

.game-viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

#emu-holder {
  position: absolute;
  inset: 0;
}

#emu-holder canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.game-sidebar {
  width: 260px;
  background: var(--bg-1);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem;
  gap: 1rem;
  overflow-y: auto;
}

.sidebar-section h4 {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-3);
  margin-bottom: 0.5rem;
}

.player-status-card {
  background: var(--bg-3);
  border-radius: var(--radius-sm);
  padding: 0.5rem 0.7rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

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

.game-controls-bar {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: auto;
}

/* ===== OVERLAY ===== */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fadeIn 200ms ease;
}

.overlay-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  min-width: 300px;
  box-shadow: var(--shadow);
}

.overlay-card h2 { font-size: 1.2rem; }
.overlay-card p { color: var(--text-3); font-size: 0.9rem; }

.spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ===== TOAST ===== */
.toast-container {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 1rem;
  font-size: 0.85rem;
  box-shadow: var(--shadow);
  animation: slideIn 200ms ease;
  max-width: 300px;
}
.toast.error { border-color: rgba(239,68,68,0.3); color: var(--red); }
.toast.success { border-color: rgba(34,197,94,0.3); color: var(--green); }

@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } }

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--bg-4); border-radius: 3px; }

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .lobby-brand h1 { font-size: 1.8rem; }
  .lobby-cards { flex-direction: column; align-items: center; }
  .lobby-card { width: 100%; max-width: 340px; }
  .room-bottom { grid-template-columns: 1fr; }
  .game-sidebar { display: none; }
}

/* ===== MODAL ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
  backdrop-filter: blur(4px);
}
.modal-card {
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 480px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

/* ===== CONTROLS PANEL ===== */
.controls-rows {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.controls-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0;
}
.controls-label {
  font-size: 0.85rem;
  color: var(--text-2);
  min-width: 80px;
}
