:root {
  --bg: #0e1117;
  --bg-2: #161b22;
  --fg: #f0f6fc;
  --fg-dim: #8b949e;
  --primary: #f6c945;
  --accent: #58a6ff;
  --border: #30363d;
  --tile-bg: #1c2128;
  --good: #56d364;
  --bad: #f85149;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  overflow-x: hidden;
}

/* ===========================================
   Mobile-first base (>= 320px)
   =========================================== */

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem 0.75rem;
  padding: 0.75rem 0.9rem;
  padding-top: max(env(safe-area-inset-top), 0.75rem);
  padding-left: max(env(safe-area-inset-left), 0.9rem);
  padding-right: max(env(safe-area-inset-right), 0.9rem);
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  min-width: 0;
  flex: 1 1 auto;
}
.brand .logo {
  font-size: clamp(1.1rem, 4vw, 1.5rem);
  color: var(--primary);
  letter-spacing: 0.2em;
  flex-shrink: 0;
}
.brand h1 {
  margin: 0;
  font-size: clamp(1.1rem, 4vw, 1.4rem);
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.5rem;
  font-variant-numeric: tabular-nums;
  font-size: 0.95rem;
}
.player .separator { color: var(--fg-dim); }
#player-balance { color: var(--primary); font-weight: 600; }

main {
  padding: 1rem 0.9rem;
  padding-left: max(env(safe-area-inset-left), 0.9rem);
  padding-right: max(env(safe-area-inset-right), 0.9rem);
  max-width: min(100%, 1100px);
  margin: 0 auto;
}

main h2 {
  margin-top: 0;
  font-weight: 500;
  color: var(--fg-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 0.85rem;
}

.games-grid {
  display: grid;
  /* On phones (~320–479px), single column; auto-fill bumps up as width allows. */
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 220px), 1fr));
  gap: 0.9rem;
}

.game-tile {
  background: var(--tile-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.game-tile:hover:not(.placeholder) {
  transform: translateY(-2px);
  border-color: var(--primary);
}

.game-tile.placeholder { opacity: 0.55; }

.game-tile .art {
  width: 100%;
  /* Aspect-ratio keeps art proportional regardless of column width. */
  aspect-ratio: 16 / 10;
  height: auto;
  min-height: 110px;
  background: linear-gradient(135deg, #2c3138, #1a1d22);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(2.5rem, 10vw, 3.5rem);
}

.game-tile .art.coin {
  background: radial-gradient(circle, #f6c945, #b8870b);
}

.coin-face {
  font-size: clamp(2.25rem, 9vw, 3rem);
  font-weight: 800;
  color: #4a3000;
  text-shadow: 0 2px 0 rgba(0,0,0,0.2);
}

.game-tile h3 { margin: 0.2rem 0 0; font-size: 1.05rem; text-align: center; }
.game-tile p { margin: 0; color: var(--fg-dim); font-size: 0.85rem; text-align: center; }

.play-btn {
  margin-top: 0.4rem;
  background: var(--primary);
  color: #1c1500;
  border: none;
  border-radius: 8px;
  padding: 0.65rem 1.2rem;
  min-height: 44px;
  min-width: 96px;
  font-weight: 600;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s;
  touch-action: manipulation;
}
.play-btn:hover:not(:disabled) { background: #ffd95a; }
.play-btn:disabled { background: #555; color: #999; cursor: not-allowed; }

#game-host {
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.game-host-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.4rem 0.6rem;
  padding: 0.6rem 0.8rem;
  background: #0d1117;
  border-bottom: 1px solid var(--border);
}

.game-host-header button {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--border);
  padding: 0.55rem 0.9rem;
  min-height: 40px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  touch-action: manipulation;
}
.game-host-header button:hover { border-color: var(--accent); }

#game-title {
  font-weight: 600;
  color: var(--fg-dim);
  font-size: 0.9rem;
  flex: 1 1 100%;
  text-align: center;
  order: 3;
}

#game-iframe {
  width: 100%;
  /* Fill viewport on phones (minus the lobby header + game-host-header), capped on desktop. */
  height: min(75dvh, 640px);
  min-height: 480px;
  border: none;
  background: #0d1117;
  display: block;
}

/* Fullscreen: the game host fills the screen; the iframe takes all space below the header. */
#game-host:fullscreen,
#game-host:-webkit-full-screen {
  width: 100vw;
  height: 100vh;
  border: 0;
  border-radius: 0;
}
#game-host:fullscreen #game-iframe,
#game-host:-webkit-full-screen #game-iframe {
  flex: 1 1 auto;
  height: auto;
  max-height: none;
}

footer {
  text-align: center;
  padding: 1rem 0.9rem;
  padding-bottom: max(env(safe-area-inset-bottom), 1rem);
  padding-left: max(env(safe-area-inset-left), 0.9rem);
  padding-right: max(env(safe-area-inset-right), 0.9rem);
  color: var(--fg-dim);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  line-height: 1.8;
}
footer a { color: var(--accent); text-decoration: none; }
footer a:hover { text-decoration: underline; }

/* --- Auth pages (signin / signup) --- */
body.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;      /* fallback */
  min-height: 100dvh;
  padding: 1rem;
  padding-bottom: max(env(safe-area-inset-bottom), 1rem);
  padding-top: max(env(safe-area-inset-top), 1rem);
}
.auth-card {
  background: var(--bg-2);
  padding: 1.5rem 1.25rem;
  border-radius: 12px;
  border: 1px solid var(--border);
  width: 100%;
  max-width: 420px;
}
.auth-card h1 { margin-top: 0; font-size: clamp(1.25rem, 5vw, 1.6rem); }
.auth-card .subtle { color: var(--fg-dim); margin-top: -0.5rem; }
.auth-card label { display: block; margin: 1rem 0; }
.auth-card label span { display: block; color: var(--fg-dim); font-size: 0.85rem; margin-bottom: 0.3rem; }
.auth-card input {
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  /* 16px+ prevents iOS auto-zoom on focus. */
  font-size: 16px;
  min-height: 44px;
}
.auth-card button.primary {
  width: 100%;
  padding: 0.85rem;
  margin-top: 0.5rem;
  min-height: 48px;
  border: 0;
  border-radius: 8px;
  background: var(--primary);
  color: #1a1300;
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  touch-action: manipulation;
}
.auth-card .error {
  color: var(--bad);
  background: rgba(248,81,73,0.1);
  padding: 0.6rem 0.8rem;
  border-radius: 6px;
  font-size: 0.9rem;
}
.auth-card .muted { color: var(--fg-dim); font-size: 0.9rem; }
.auth-card a { color: var(--accent); }

.signout-btn {
  margin-left: 0.5rem;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--fg-dim);
  padding: 0.5rem 0.8rem;
  min-height: 40px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
  touch-action: manipulation;
}
.signout-btn:hover { color: var(--fg); border-color: var(--fg-dim); }

/* ===========================================
   Small landscape phones (>= 480px)
   =========================================== */
@media (min-width: 480px) {
  header { padding: 0.9rem 1.2rem; }
  main { padding: 1.5rem 1.2rem; }
  .games-grid { gap: 1rem; }
  .auth-card { padding: 1.75rem 2rem; }
  #game-title { flex: 0 1 auto; order: 0; font-size: 1rem; }
}

/* ===========================================
   Tablet (>= 768px)
   =========================================== */
@media (min-width: 768px) {
  header { padding: 1rem 1.5rem; }
  .brand { gap: 1rem; }
  main { padding: 1.75rem 1.5rem; }
  .games-grid { gap: 1rem; }
  .game-tile .art { aspect-ratio: auto; height: 130px; min-height: 0; }
  #game-iframe { height: min(80dvh, 640px); min-height: 520px; }
  .auth-card { padding: 2rem 2.5rem; }
}

/* ===========================================
   Desktop (>= 1024px)
   =========================================== */
@media (min-width: 1024px) {
  header { padding: 1rem 2rem; }
  main { padding: 2rem; }
  #game-iframe { height: 640px; }
  .game-tile .art { height: 130px; }
}
