/* ═══════════════════════════════
   CSS CUSTOM PROPERTIES
   (opt-in for games via SDK)
   ═══════════════════════════════ */
:root {
  --bg-primary: #0a0a14;
  --bg-secondary: #12121f;
  --bg-card: #1a1a2e;
  --bg-card-hover: #1f1f3a;
  --bg-input: #14142a;
  --accent: #e94560;
  --accent-hover: #ff6b81;
  --accent-soft: rgba(233, 69, 96, 0.15);
  --text-primary: #eaeaea;
  --text-secondary: #9a9ab0;
  --text-muted: #555570;
  --border: #2a2a40;
  --border-light: #3a3a55;
  --success: #4ecdc4;
  --success-soft: rgba(78, 205, 196, 0.15);
  --warning: #f0c040;
  --warning-soft: rgba(240, 192, 64, 0.15);
  --danger: #e74c3c;
  --danger-soft: rgba(231, 76, 60, 0.15);
  --radius: 14px;
  --radius-sm: 10px;
  --radius-xs: 6px;
  --shadow: 0 8px 32px rgba(0,0,0,0.35);
  --shadow-sm: 0 2px 12px rgba(0,0,0,0.2);
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
  --transition: 0.2s ease;
  --transition-slow: 0.35s ease;
  --danger-soft: rgba(231, 76, 60, 0.15);
}

/* ═══ RESET ═══ */
*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  -webkit-tap-highlight-color: transparent;
}

/* ═══ SCREENS ═══ */
.screen {
  display: none;
  position: fixed;
  inset: 0;
  flex-direction: column;
  overflow: hidden;
}
.screen.active { display: flex; }

/* ═══ LOADING ═══ */
.loading-container {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 1rem;
  color: var(--text-secondary);
}
.loading-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); } }

/* ═══ IDENTITY ═══ */
#screen-identity {
  align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, #1a1a2e 0%, var(--bg-primary) 70%);
}

.center-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem 2rem 1.5rem;
  max-width: 440px; width: 92%;
  text-align: center;
  box-shadow: var(--shadow);
}
.center-card h1 { font-size: 2rem; margin-bottom: 0.25rem; }
.subtitle { color: var(--text-secondary); margin-bottom: 1.5rem; font-size: 0.95rem; }

.badge-picker {
  display: flex; flex-wrap: wrap; gap: 6px;
  justify-content: center; margin-bottom: 0.75rem;
  max-height: 130px; overflow-y: auto; padding: 4px;
}
.badge-option {
  width: 46px; height: 46px; font-size: 1.5rem;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  border: 2px solid transparent;
  cursor: pointer; transition: var(--transition);
  background: var(--bg-secondary);
}
.badge-option:hover { border-color: var(--border-light); transform: scale(1.1); }
.badge-option.selected { border-color: var(--accent); background: var(--accent-soft); }

.selected-badge { font-size: 4rem; margin: 0.25rem 0 1rem; line-height: 1; }

input[type="text"] {
  width: 100%; padding: 13px 16px;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg-input); color: var(--text-primary);
  font-size: 1rem; outline: none; transition: var(--transition);
}
input[type="text"]:focus { border-color: var(--accent); }
input[type="text"]::placeholder { color: var(--text-muted); }

/* ═══ BUTTONS ═══ */
.btn-primary {
  display: inline-flex; align-items: center; justify-content: center;
  width: 100%; padding: 14px 24px; border: none;
  border-radius: var(--radius-sm); background: var(--accent);
  color: #fff; font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: var(--transition); margin-top: 1rem;
  gap: 8px;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled { opacity: 0.4; cursor: default; }

.btn-secondary {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 12px 24px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--bg-secondary);
  color: var(--text-primary); font-size: 1rem;
  cursor: pointer; transition: var(--transition); gap: 8px;
}
.btn-secondary:hover { border-color: var(--accent); }

.btn-accent {
  padding: 13px 24px; border: none;
  border-radius: var(--radius-sm); background: var(--accent);
  color: #fff; font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: var(--transition);
  white-space: nowrap;
}
.btn-accent:hover { background: var(--accent-hover); }

.btn-icon {
  background: none; border: none; color: var(--text-primary);
  font-size: 1.2rem; cursor: pointer; padding: 8px;
  border-radius: var(--radius-xs); transition: var(--transition);
  display: inline-flex; align-items: center; justify-content: center;
}
.btn-icon:hover { background: var(--bg-card); }

.btn-large { padding: 16px 28px; font-size: 1.05rem; }
.btn-small { font-size: 0.9rem; padding: 4px 8px; }

.btn-danger-sm {
  background: none; border: none; color: var(--danger);
  cursor: pointer; font-size: 1rem; padding: 2px 6px;
  border-radius: var(--radius-xs); transition: var(--transition);
}
.btn-danger-sm:hover { background: var(--danger-soft); }

/* ═══ TOP BAR ═══ */
.top-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1.25rem; background: var(--bg-secondary);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
  min-height: 56px; gap: 12px;
}
.top-title { font-size: 1.1rem; font-weight: 600; }
.top-actions { display: flex; gap: 4px; }

.player-chip {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-card); padding: 6px 14px 6px 10px;
  border-radius: 999px; border: 1px solid var(--border);
  cursor: pointer; transition: var(--transition); flex-shrink: 0;
}
.player-chip:hover { border-color: var(--accent); }
.chip-badge { font-size: 1.3rem; }
.chip-name { font-weight: 600; font-size: 0.9rem; }
.chip-wins { font-size: 0.8rem; color: var(--warning); }

/* ═══ BLE WARNING ═══ */
.ble-warning {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 1.25rem; background: var(--warning-soft);
  border-bottom: 1px solid rgba(240,192,64,0.3);
  font-size: 0.85rem; color: var(--warning); flex-shrink: 0;
}
.ble-dismiss { background: none; border: none; color: var(--warning); cursor: pointer; font-size: 1rem; padding: 4px; }

/* ═══ LOBBY ═══ */
.lobby-body { flex: 1; overflow-y: auto; padding: 1.25rem; }
.lobby-section { margin-bottom: 2rem; }

.section-title {
  font-size: 1rem; font-weight: 600; color: var(--text-secondary);
  margin-bottom: 0.75rem; display: flex; align-items: center; gap: 8px;
  border-bottom: 1px solid var(--border); padding-bottom: 0.5rem;
}
.section-desc { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1rem; }

.empty-state {
  color: var(--text-muted); font-style: italic;
  text-align: center; padding: 1.5rem; font-size: 0.9rem;
}

/* Room actions */
.room-actions { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.room-actions .btn-primary { flex: 1; min-width: 160px; margin-top: 0; }
.join-row { display: flex; flex: 1; min-width: 180px; gap: 6px; }
.join-row input {
  flex: 1; text-transform: uppercase; letter-spacing: 4px;
  text-align: center; font-weight: 700; font-size: 1.1rem;
}

/* Room list */
.room-list { display: flex; flex-direction: column; gap: 8px; }
.room-list-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px; background: var(--bg-card);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; transition: var(--transition);
}
.room-list-item:hover { border-color: var(--accent); background: var(--bg-card-hover); }
.room-info { display: flex; align-items: center; gap: 12px; }
.room-code-chip {
  font-weight: 700; font-size: 1.1rem; letter-spacing: 3px;
  color: var(--accent); background: var(--accent-soft);
  padding: 4px 10px; border-radius: var(--radius-xs);
}
.room-host { font-size: 0.9rem; color: var(--text-secondary); }
.room-count { font-size: 0.9rem; color: var(--text-secondary); display: flex; align-items: center; gap: 4px; }

/* Game grid */
.game-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}
.game-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
  cursor: pointer; transition: var(--transition); position: relative;
}
.game-card:hover { border-color: var(--accent); transform: translateY(-3px); box-shadow: var(--shadow-sm); }
.game-card.disabled { opacity: 0.35; pointer-events: none; }
.game-card.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent), var(--shadow-sm); }
.game-card.non-interactive { cursor: default; }
.game-card.non-interactive:hover { transform: none; box-shadow: none; }

.game-poster {
  width: 100%; aspect-ratio: 16/10;
  display: flex; align-items: center; justify-content: center;
  font-size: 3rem; background: var(--bg-secondary);
  overflow: hidden;
}
.game-poster img { width: 100%; height: 100%; object-fit: cover; }

.game-info { padding: 12px 14px; }
.game-name { font-weight: 600; font-size: 0.95rem; margin-bottom: 4px; }
.game-meta {
  display: flex; gap: 10px; font-size: 0.78rem;
  color: var(--text-secondary); flex-wrap: wrap;
}

.game-badges {
  position: absolute; top: 8px; right: 8px;
  display: flex; gap: 4px;
}
.game-badge-tag {
  background: rgba(0,0,0,0.7); padding: 3px 8px;
  border-radius: var(--radius-xs); font-size: 0.7rem;
  color: var(--text-primary); backdrop-filter: blur(4px);
}
.game-badge-tag.ble { background: rgba(0,100,240,0.7); }
.game-badge-tag.solo { background: rgba(78,205,196,0.6); }

.game-disabled-reason {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10,10,20,0.75); font-size: 0.85rem;
  color: var(--text-secondary); padding: 1rem; text-align: center;
  border-radius: var(--radius);
}

/* Stats */
.stats-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px;
}
.stat-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 14px; text-align: center;
}
.stat-value { font-size: 1.6rem; font-weight: 700; color: var(--accent); }
.stat-label { font-size: 0.8rem; color: var(--text-secondary); margin-top: 4px; }

/* ═══ ROOM ═══ */
.room-body { flex: 1; overflow-y: auto; padding: 1.25rem; }
.room-section { margin-bottom: 1.5rem; }

.room-code-bar { display: flex; align-items: center; gap: 8px; }
.room-label { font-size: 0.85rem; color: var(--text-secondary); }
.room-code {
  font-weight: 700; font-size: 1.3rem; letter-spacing: 4px;
  color: var(--accent); background: var(--accent-soft);
  padding: 4px 12px; border-radius: var(--radius-xs);
}

.host-only-label { font-size: 0.8rem; color: var(--text-muted); font-weight: 400; }

/* Player list */
.player-list { display: flex; flex-wrap: wrap; gap: 8px; }
.player-pill {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg-card); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 999px; font-size: 0.9rem;
  transition: var(--transition);
}
.player-pill.is-host { border-color: var(--warning); }
.player-pill.is-me { border-color: var(--accent); }
.host-crown { font-size: 0.75rem; color: var(--warning); }

/* Selected game panel */
.selected-game-panel {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1.25rem; margin-bottom: 1.5rem;
}
.selected-game-header { display: flex; gap: 1rem; align-items: flex-start; margin-bottom: 1rem; }
.selected-game-poster {
  width: 100px; height: 65px; border-radius: var(--radius-sm);
  background: var(--bg-secondary); display: flex; align-items: center;
  justify-content: center; font-size: 2rem; overflow: hidden; flex-shrink: 0;
}
.selected-game-poster img { width: 100%; height: 100%; object-fit: cover; }
.selected-game-info { flex: 1; }
.selected-game-info h3 { margin-bottom: 4px; }

.sel-game-meta { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.meta-tag {
  font-size: 0.75rem; padding: 3px 8px; border-radius: var(--radius-xs);
  background: var(--bg-secondary); border: 1px solid var(--border);
  color: var(--text-secondary);
}
.ble-tag { background: rgba(0,100,240,0.15); border-color: rgba(0,100,240,0.4); color: #6db3f2; }

/* Config form */
.config-form {
  display: flex; flex-direction: column; gap: 1rem; margin-bottom: 1rem;
}
.config-field label {
  display: block; font-size: 0.85rem; color: var(--text-secondary);
  margin-bottom: 4px; font-weight: 500;
}
.config-field select,
.config-field input[type="number"] {
  width: 100%; padding: 10px 12px;
  border-radius: var(--radius-xs); border: 1px solid var(--border);
  background: var(--bg-input); color: var(--text-primary); font-size: 0.95rem;
}
.config-field select { cursor: pointer; }
.config-field select option { background: var(--bg-secondary); }

/* Chat */
.room-chat-section { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-box {
  flex: 1; display: flex; flex-direction: column;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; min-height: 200px;
}
.chat-messages {
  flex: 1; overflow-y: auto; padding: 12px;
  display: flex; flex-direction: column; gap: 4px;
}
.chat-msg { font-size: 0.85rem; line-height: 1.4; }
.chat-author { font-weight: 600; color: var(--accent); }
.chat-badge-inline { margin-right: 2px; }
.chat-msg.system { color: var(--text-muted); font-style: italic; font-size: 0.8rem; }

.chat-input-row { display: flex; border-top: 1px solid var(--border); }
.chat-input-row input {
  flex: 1; border: none; border-radius: 0;
  background: var(--bg-input); padding: 12px 14px;
}
.chat-input-row .btn-icon { border-radius: 0; padding: 12px 16px; font-size: 1.1rem; }

/* ═══ MODAL ═══ */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.7);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
  backdrop-filter: blur(4px);
}
.modal-card {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 2rem; text-align: center;
  max-width: 380px; width: 92%; box-shadow: var(--shadow);
}
.modal-card h3 { margin-bottom: 0.25rem; }
.qr-container { margin: 1rem auto; }
.qr-container canvas { border-radius: var(--radius-sm); }
.room-code-big {
  font-size: 2.2rem; font-weight: 700; letter-spacing: 6px;
  color: var(--accent); margin: 0.5rem 0;
}
.qr-url { font-size: 0.75rem; color: var(--text-muted); word-break: break-all; margin-bottom: 1rem; }

/* ═══ GAME SCREEN ═══ */
.game-topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 6px 12px; background: var(--bg-secondary);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
  min-height: 40px;
}
.game-topbar-title { font-weight: 600; font-size: 0.9rem; flex: 1; }
.game-topbar-room { font-size: 0.8rem; color: var(--text-secondary); }
.btn-exit-game { color: var(--danger); }

.game-iframe { flex: 1; width: 100%; border: none; background: #000; }

/* ═══ RESULTS ═══ */
#screen-results {
  align-items: center; justify-content: center;
  background: radial-gradient(ellipse at center, #1a1a2e 0%, var(--bg-primary) 70%);
}
.results-card { max-width: 500px; }
.results-card h2 { margin-bottom: 1.25rem; font-size: 1.8rem; }

.result-winner-banner {
  font-size: 1.4rem; font-weight: 700; color: var(--warning);
  margin-bottom: 1rem; padding: 0.75rem;
  background: var(--warning-soft); border-radius: var(--radius-sm);
}
.result-scores { display: flex; flex-direction: column; gap: 6px; margin-bottom: 1rem; }
.result-score-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 14px; background: var(--bg-secondary);
  border-radius: var(--radius-sm); border: 1px solid var(--border);
}
.result-score-row.winner { border-color: var(--warning); }
.result-summary { color: var(--text-secondary); font-size: 0.9rem; margin-bottom: 1rem; text-align: center; }

.results-votes { margin: 0.75rem 0; }
.vote-chips { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.vote-chip {
  padding: 4px 12px; border-radius: 999px; font-size: 0.8rem;
  background: var(--bg-secondary); border: 1px solid var(--border);
}
.vote-chip.replay { border-color: var(--success); color: var(--success); }
.vote-chip.lobby { border-color: var(--warning); color: var(--warning); }
.vote-chip.pending { color: var(--text-muted); }

.results-actions { display: flex; gap: 0.75rem; margin-top: 1rem; }
.results-actions .btn-primary,
.results-actions .btn-secondary { flex: 1; margin-top: 0; }

.results-host { margin-top: 1.25rem; padding-top: 1rem; border-top: 1px solid var(--border); }

/* ═══ TOAST ═══ */
.toast-container {
  position: fixed; top: 12px; right: 12px; z-index: 2000;
  display: flex; flex-direction: column; gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 12px 18px;
  box-shadow: var(--shadow); pointer-events: auto;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
  font-size: 0.9rem; max-width: 340px;
}
.toast.error { border-color: var(--danger); color: var(--danger); }
.toast.success { border-color: var(--success); color: var(--success); }
.toast.info { border-color: var(--accent); }

@keyframes toastIn { from { opacity: 0; transform: translateX(40px); } }
@keyframes toastOut { to { opacity: 0; transform: translateX(40px); } }

/* ═══ UTILITIES ═══ */
.text-center { text-align: center; }
.text-muted { color: var(--text-muted); font-size: 0.85rem; }
.hidden { display: none !important; }

/* ═══ SCROLLBAR ═══ */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ═══ READY SCREEN ═══ */
.ready-body {
  flex: 1; overflow-y: auto; padding: 1.25rem;
  display: flex; flex-direction: column; gap: 1.25rem;
  max-width: 600px; margin: 0 auto; width: 100%;
}

.ready-game-info { text-align: center; }
.ready-game-info h2 { font-size: 1.5rem; margin-bottom: 0.25rem; }

.ready-section {
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 1rem;
}
.ready-section .section-title { border: none; padding: 0; margin-bottom: 0.75rem; }

.ready-ble-card {
  background: var(--bg-secondary); border-radius: var(--radius-sm);
  padding: 12px;
}

.ble-status-row {
  display: flex; align-items: center; gap: 10px; font-size: 0.9rem;
}

.ready-btn-test {
  padding: 12px; text-align: center; border-radius: var(--radius-sm);
  background: var(--bg-card); border: 1px solid var(--border);
  font-size: 0.9rem; transition: 0.2s;
}
.ready-btn-test.pressed {
  background: var(--success-soft); border-color: var(--success);
  color: var(--success); font-weight: 600;
}

.config-summary {
  display: grid; grid-template-columns: auto 1fr; gap: 4px 12px;
  font-size: 0.9rem;
}
.config-summary .cfg-key { color: var(--text-secondary); }

.ready-player-list {
  display: flex; flex-direction: column; gap: 8px;
}

.ready-player-row {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px; background: var(--bg-secondary);
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  font-size: 0.9rem;
}
.ready-player-row.is-ready { border-color: var(--success); }
.ready-player-row.not-ready { border-color: var(--border); }

.ready-status {
  margin-left: auto; font-size: 0.85rem; font-weight: 600;
}
.ready-status.yes { color: var(--success); }
.ready-status.no { color: var(--text-muted); }

.ready-actions {
  display: flex; flex-direction: column; gap: 0.75rem;
}

.badge-row {
  display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px;
}
.win-badge {
  font-size: 0.7rem; padding: 2px 8px; border-radius: 999px;
  background: var(--warning-soft); border: 1px solid rgba(240,192,64,0.3);
  color: var(--warning);
}

/* ═══ RESPONSIVE ═══ */
@media (max-width: 600px) {
  .game-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .room-actions { flex-direction: column; }
  .room-actions .btn-primary { min-width: auto; }
  .top-title { display: none; }
  .selected-game-header { flex-direction: column; align-items: center; text-align: center; }
  .selected-game-poster { width: 140px; height: 90px; }
}

/* ═══ LIGHT THEME ═══ */
[data-theme="light"] {
  --bg-primary: #f0f2f5;
  --bg-secondary: #ffffff;
  --bg-card: #ffffff;
  --bg-card-hover: #f5f5fa;
  --bg-input: #f0f0f5;
  --accent: #e94560;
  --accent-hover: #d63050;
  --accent-soft: rgba(233, 69, 96, 0.1);
  --text-primary: #1a1a2e;
  --text-secondary: #555570;
  --text-muted: #999;
  --border: #ddd;
  --border-light: #ccc;
  --success: #2ecc71;
  --success-soft: rgba(46, 204, 113, 0.1);
  --warning: #e67e22;
  --warning-soft: rgba(230, 126, 34, 0.1);
  --danger: #e74c3c;
  --danger-soft: rgba(231, 76, 60, 0.1);
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --shadow-sm: 0 1px 6px rgba(0,0,0,0.05);
}
