:root {
  color-scheme: dark;
  --bg: #000;
  --surface: rgba(18, 20, 26, 0.86);
  --border: rgba(255, 255, 255, 0.1);
  --text: #e8eaed;
  --muted: #9aa1ab;
  --accent: #7c9cff;
  --ok: #4ade80;
  --warn: #fbbf24;
  --bad: #f87171;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
}

#video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
  pointer-events: none;
}

#touch {
  position: absolute;
  inset: 0;
  touch-action: none;
}

#chip {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 12px;
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  z-index: 3;
}

#dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
}

#dot.ok { background: var(--ok); }
#dot.bad { background: var(--bad); }

#panel {
  position: fixed;
  top: max(52px, calc(env(safe-area-inset-top) + 44px));
  left: max(10px, env(safe-area-inset-left));
  width: min(420px, calc(100vw - 20px));
  max-height: calc(100vh - 80px);
  overflow: auto;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  z-index: 3;
}

#panel header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

#panel header strong { flex: 1; }

#panel button,
#room button {
  padding: 6px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font: inherit;
}

#stats {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 0;
  font-variant-numeric: tabular-nums;
}

#stats dt { color: var(--muted); }
#stats dd { margin: 0; }

.hint {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 12px;
}

#room-form {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: #000;
  z-index: 4;
}

#room {
  display: grid;
  gap: 10px;
  width: min(360px, 90vw);
}

#room input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #111;
  color: var(--text);
  font: inherit;
  font-size: 16px;
}

#library {
  position: fixed;
  inset: 0;
  overflow: auto;
  padding: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom))
    max(20px, env(safe-area-inset-left));
  background: radial-gradient(120% 90% at 10% 0%, #1b2140 0%, #0b0d14 55%, #07080c 100%);
  z-index: 2;
  -webkit-user-select: none;
  user-select: none;
}

.library-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin: 36px 0 16px;
}

.library-head h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.2px;
}

.library-head p {
  margin: 4px 0 0;
  color: var(--muted);
}

#refresh {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font: inherit;
}

#games {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

#games button {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 74px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: linear-gradient(160deg, rgba(124, 156, 255, 0.1), rgba(255, 255, 255, 0.03));
  color: var(--text);
  font: inherit;
  text-align: left;
}

#games button:active { background: rgba(124, 156, 255, 0.22); }

#games .name { font-size: 16px; font-weight: 600; }
#games .meta { color: var(--muted); font-size: 12px; }

#busy {
  position: fixed;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 14px;
  background: rgba(0, 0, 0, 0.72);
  z-index: 2;
}

#busy p { margin: 0; color: var(--text); }

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

#quickbar {
  position: fixed;
  left: 50%;
  bottom: max(10px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  gap: 6px;
  max-width: calc(100vw - 20px);
  overflow-x: auto;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  z-index: 3;
}

#quickbar button {
  flex: none;
  padding: 9px 12px;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font: inherit;
  font-size: 13px;
}

#quickbar button:active { background: rgba(124, 156, 255, 0.3); }

#quick-toggle {
  position: fixed;
  right: max(10px, env(safe-area-inset-right));
  bottom: max(10px, env(safe-area-inset-bottom));
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 18px;
  z-index: 3;
}

#close-game {
  border-color: rgba(248, 113, 113, 0.4) !important;
  color: #fecaca !important;
}

#toast {
  position: fixed;
  top: max(14px, env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  z-index: 5;
  pointer-events: none;
}

#tap-hint {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  pointer-events: none;
  z-index: 2;
}
