:root {
  --navy: #16213a;
  --navy-2: #1f2d4d;
  --cheese: #f4b63f;
  --cheese-dark: #d9922a;
  --cream: #fdf6e3;
  --muted: #a9b3c9;
  --danger: #ff7a6b;
  --ok: #5fd39a;
  --radius: 16px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--navy);
  color: var(--cream);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- Menu screens ---------- */
.screen { position: fixed; inset: 0; }
.screen-menu {
  display: grid;
  place-items: center;
  padding: calc(var(--safe-top) + 16px) calc(var(--safe-right) + 16px) calc(var(--safe-bottom) + 16px) calc(var(--safe-left) + 16px);
  overflow-y: auto;
  background:
    radial-gradient(circle at 12% 18%, rgba(244, 182, 63, .16) 0 38px, transparent 39px),
    radial-gradient(circle at 86% 74%, rgba(244, 182, 63, .12) 0 54px, transparent 55px),
    radial-gradient(circle at 78% 14%, rgba(244, 182, 63, .10) 0 22px, transparent 23px),
    radial-gradient(circle at 20% 86%, rgba(244, 182, 63, .10) 0 28px, transparent 29px),
    linear-gradient(160deg, #1d2b4a, var(--navy) 60%);
}
.menu-card {
  width: min(400px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
}
.logo { align-self: center; filter: drop-shadow(0 6px 18px rgba(244, 182, 63, .35)); }
h1 { margin: 0; font-size: 2.4rem; letter-spacing: .02em; color: var(--cheese); }
h2 { margin: 0; font-size: 1.5rem; }
.tagline { margin: 0 0 6px; color: var(--muted); line-height: 1.4; }
.hint { margin: 0; color: var(--muted); font-size: .85rem; line-height: 1.4; }
.error { margin: 0; min-height: 1.2em; color: var(--danger); font-size: .95rem; }
.muted { color: var(--muted); }

.btn {
  min-height: 52px;
  padding: 12px 18px;
  border: 0;
  border-radius: var(--radius);
  background: var(--navy-2);
  font-weight: 700;
  font-size: 1.05rem;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .25);
  transition: transform .08s ease, filter .15s ease;
}
.btn:active { transform: translateY(1px) scale(.99); filter: brightness(1.1); }
.btn-primary { background: var(--cheese); color: #2b1d05; }
.btn-ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn:disabled { opacity: .55; }
.btn:focus-visible, input:focus-visible { outline: 3px solid var(--cheese); outline-offset: 2px; }

input {
  min-height: 52px;
  padding: 10px 14px;
  border: 2px solid #33456d;
  border-radius: var(--radius);
  background: #0f1830;
  color: var(--cream);
  font: inherit;
  font-size: 1.1rem;
}
.join-form { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; text-align: left; }
.join-form label { color: var(--muted); font-size: .95rem; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 800; }

.progress { height: 14px; border-radius: 999px; background: #0f1830; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--cheese-dark), var(--cheese)); transition: width .2s ease; }

/* ---------- Play screens ---------- */
.screen-play { background: #000; overflow: hidden; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.stage {
  position: absolute;
  inset: 0;
  margin: auto;
  width: min(100vw, calc(100dvh * 4 / 3));
  height: min(100dvh, calc(100vw * 3 / 4));
}
#ejs-player, #guest-video { width: 100%; height: 100%; display: block; background: #000; }
#guest-video { object-fit: contain; }

/* EmulatorJS draws its own menus and touch buttons; this app replaces them. */
.ejs_virtualGamepad_parent, .ejs_virtualGamepad_open, .ejs_menu_bar, .ejs_context_menu { display: none !important; }

.topbar {
  position: absolute;
  top: calc(var(--safe-top) + 8px);
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  z-index: 10;
}
.chip {
  min-height: 36px;
  padding: 6px 14px;
  border: 0;
  border-radius: 999px;
  background: rgba(15, 24, 48, .72);
  color: var(--cream);
  font-weight: 700;
  font-size: .9rem;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.chip-accent { background: rgba(244, 182, 63, .9); color: #2b1d05; }
.chip-status { display: inline-flex; align-items: center; pointer-events: none; }
.chip-status.is-ok::before { content: ""; width: 8px; height: 8px; margin-right: 8px; border-radius: 50%; background: var(--ok); }

.sheet {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: calc(var(--safe-top) + 12px) calc(var(--safe-right) + 12px) calc(var(--safe-bottom) + 12px) calc(var(--safe-left) + 12px);
  background: rgba(6, 10, 22, .72);
  overflow-y: auto;
}
.sheet-card {
  width: min(420px, 100%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 18px;
  border-radius: 22px;
  background: var(--navy);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .5);
}
.sheet-row { display: flex; gap: 10px; }
.sheet-row > .btn { flex: 1; }

.invite { text-align: center; }
.invite-label { margin: 0; color: var(--muted); font-size: .85rem; text-transform: uppercase; letter-spacing: .12em; }
.invite-code { margin: 0; font-size: 3rem; font-weight: 900; letter-spacing: .25em; color: var(--cheese); }
.invite-link { margin: 0; color: var(--muted); font-size: .85rem; word-break: break-all; }
.friends { list-style: none; margin: 4px 0; padding: 0; display: flex; flex-direction: column; gap: 6px; text-align: left; }
.friends li { padding: 10px 12px; border-radius: 12px; background: #0f1830; display: flex; justify-content: space-between; gap: 10px; }
.friends .friend-meta { color: var(--muted); font-size: .9rem; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 15;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px;
  background: rgba(6, 10, 22, .8);
  text-align: center;
}
.overlay p { margin: 0; font-size: 1.1rem; max-width: 30em; line-height: 1.4; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 18px);
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(90vw, 460px);
  padding: 10px 16px;
  border-radius: 14px;
  background: rgba(15, 24, 48, .92);
  font-weight: 700;
  text-align: center;
}

.rotate { display: none; }
@media (orientation: portrait) {
  body.is-playing .rotate {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 18px;
    background: var(--navy);
    font-size: 1.2rem;
    font-weight: 700;
  }
  .rotate-phone {
    width: 56px;
    height: 96px;
    border: 4px solid var(--cheese);
    border-radius: 12px;
    animation: rotate-hint 2s ease-in-out infinite;
  }
}
@keyframes rotate-hint { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }
@media (prefers-reduced-motion: reduce) { .rotate-phone { animation: none; transform: rotate(-90deg); } }

/* Short landscape phones: keep the whole home card on screen. */
@media (orientation: landscape) and (max-height: 500px) {
  .menu-card { gap: 8px; }
  .logo { width: 52px; height: 52px; }
  h1 { font-size: 1.7rem; }
  .tagline { margin-bottom: 0; }
  .btn, input { min-height: 46px; }
  .join-form { margin-top: 4px; }
}
