* { margin: 0; padding: 0; box-sizing: border-box; user-select: none; -webkit-user-select: none; }

:root {
  --primary: #ffce4b;
  --secondary: #4a9cff;
  --danger: #ff5a6a;
  --success: #4bd964;
  --dark: #0d1526;
  --light: #f0f0f0;
}

html, body {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #1c2c50 0%, #14203c 100%);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: #fff;
  overflow: hidden;
}

#app {
  width: 100%;
  height: 100%;
  position: relative;
  --ad-height: 0px;
  --ad-gap: 0px;
  --ad-total: 0px;
}

#app.has-ad-banner {
  /* --ad-height is set inline by ADS; gap is the accidental-click safety band */
  --ad-gap: 150px;
  --ad-total: calc(var(--ad-height) + var(--ad-gap));
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
}

/* When the ad strip is active, shrink the canvas BOX so resizeRenderer() (which reads
   canvas.clientHeight) sizes the renderer to the displaced area — board never overlaps the ad. */
#app.has-ad-banner #game-canvas { height: calc(100% - var(--ad-total)); }

/* HUD Overlay */
.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.hud-bar {
  position: absolute;
  left: 0;
  right: 0;
  padding: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  pointer-events: auto;
  background: linear-gradient(180deg, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.2) 100%);
  backdrop-filter: blur(10px);
}

.hud-bar.top {
  top: 0;
}

.player-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: rgba(255,255,255,0.05);
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.1);
}

.avatar {
  font-size: 32px;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
}

.avatar.red {
  background: rgba(255, 90, 106, 0.2);
  border: 2px solid #ff5a6a;
}

.avatar.blue {
  background: rgba(74, 156, 255, 0.2);
  border: 2px solid #4a9cff;
}

.player-name {
  font-weight: 700;
  font-size: 14px;
}

.pieces-count {
  font-size: 12px;
  opacity: 0.8;
  color: var(--primary);
}

.turn-indicator {
  font-size: 18px;
  font-weight: 700;
  color: var(--primary);
  text-shadow: 0 2px 8px rgba(0,0,0,0.5);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

/* Bottom Panel */
.hud-panel {
  position: absolute;
  left: 0;
  right: 0;
  padding: 16px;
  pointer-events: auto;
}

.hud-panel.bottom {
  bottom: 0;
  background: linear-gradient(180deg, rgba(0,0,0,0.2) 0%, rgba(0,0,0,0.6) 100%);
  backdrop-filter: blur(10px);
}

#app.has-ad-banner .hud-panel.bottom { bottom: var(--ad-total); }
/* Lift the bottom-anchored reaction controls above the ad+spacer so no clickable
   element sits in the safety zone (the react buttons are pointer-events:auto). The
   floating glyphs are decorative but lifted too so reactions don't rise from behind the ad. */
#app.has-ad-banner .react-bar { bottom: calc(92px + var(--ad-total)); }
#app.has-ad-banner .react-float { bottom: calc(120px + var(--ad-total)); }

.panel-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  max-width: 900px;
  margin: 0 auto;
}

.game-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.info-row code {
  background: rgba(0,0,0,0.3);
  padding: 4px 12px;
  border-radius: 6px;
  font-family: monospace;
  font-weight: 700;
  color: var(--primary);
}

.btn-icon {
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  padding: 4px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
}

.btn-icon:hover {
  background: rgba(255,255,255,0.15);
}

.status-text {
  font-size: 13px;
  opacity: 0.9;
}

.button-group {
  display: flex;
  gap: 8px;
}

/* Buttons */
button {
  font-weight: 700;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-primary {
  background: linear-gradient(180deg, var(--primary) 0%, #f0a929 100%);
  color: #4a2d00;
  padding: 10px 20px;
  font-size: 13px;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(255, 206, 75, 0.4);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: default;
  transform: none;
}

.btn-secondary {
  background: rgba(255,255,255,0.1);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
  padding: 10px 20px;
  font-size: 13px;
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(255,255,255,0.15);
  transform: translateY(-2px);
}

.btn-secondary:disabled {
  opacity: 0.5;
  cursor: default;
}

.btn-large {
  padding: 14px 32px;
  font-size: 14px;
  width: 100%;
}

/* Modals */
.modal {
  position: fixed;        /* pin to the viewport, not #app (which can exceed it on mobile) */
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  /* flex-start + margin:auto on the card = centered when it FITS, but pinned to the top and
     fully scrollable when it's TALLER than the viewport (centering would clip/strand the top). */
  align-items: flex-start;
  justify-content: center;
  z-index: 20;
  pointer-events: auto;
  animation: fadeIn 0.3s;
  overflow-y: auto;       /* the overlay itself scrolls when content is taller than the screen */
  -webkit-overflow-scrolling: touch;
  padding: 16px;
}

.modal.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-content {
  background: linear-gradient(135deg, #2c4a7c 0%, #1c3a5c 100%);
  border: 2px solid rgba(255,255,255,0.1);
  border-radius: 16px;
  padding: 32px;
  width: 100%;
  max-width: 500px;       /* a centered card on desktop, never edge-to-edge */
  flex: 0 0 auto;         /* don't let flexbox stretch the card to fill the overlay */
  margin: auto;           /* vertical+horizontal centering when shorter than the viewport;
                             yields to the top when taller (with align-items:flex-start) */
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  animation: slideUp 0.3s;
}

@keyframes slideUp {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal-content h2 {
  font-size: 28px;
  margin-bottom: 24px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-option {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-divider {
  text-align: center;
  opacity: 0.5;
  margin: 8px 0;
}

.input-field {
  padding: 12px;
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 8px;
  color: #fff;
  font-size: 14px;
  text-transform: uppercase;
}

.input-field::placeholder {
  color: rgba(255,255,255,0.5);
}

.hint {
  font-size: 12px;
  opacity: 0.6;
  text-align: center;
}

/* Settings */
.settings-btn {
  position: absolute;
  top: 104px;
  right: 16px;
  font-size: 24px;
  cursor: pointer;
  pointer-events: auto;
  z-index: 15;
  padding: 8px;
  background: rgba(255,255,255,0.05);
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.1);
  transition: all 0.2s;
}

.settings-btn:hover {
  background: rgba(255,255,255,0.1);
  transform: scale(1.1);
}

.settings-panel {
  position: absolute;
  top: 148px;
  right: 16px;
  background: linear-gradient(135deg, #2c4a7c 0%, #1c3a5c 100%);
  border: 2px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 16px;
  min-width: 200px;
  pointer-events: auto;
  z-index: 15;
}

.settings-panel.hidden {
  display: none;
}

.setting {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
  font-size: 13px;
}

.setting:last-child {
  margin-bottom: 0;
}

.btn-toggle {
  padding: 4px 12px;
  background: rgba(255,255,255,0.1);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 6px;
}

select {
  padding: 4px 8px;
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 6px;
  color: #fff;
  font-size: 12px;
}

/* Responsive */
@media (max-width: 768px) {
  /* Keep the top bar HORIZONTAL but compact — stacking it made it ~240px tall and
     squeezed the board, which is why mobile felt broken. Shrink instead of stack. */
  .hud-bar { padding: 8px 10px; }
  .hud-bar.top {
    gap: 6px;
    /* respect notches / status bars */
    padding-top: calc(8px + env(safe-area-inset-top, 0px));
  }

  .player-card { padding: 5px 8px; gap: 6px; }
  .avatar { font-size: 20px; width: 32px; height: 32px; border-radius: 8px; }
  .player-name { font-size: 12px; }
  .pieces-count { font-size: 11px; }
  .turn-indicator { font-size: 14px; text-align: center; flex: 1; }

  /* Bottom panel: stack info above buttons so the board keeps maximum height. */
  .panel-content { flex-direction: column; gap: 10px; }
  .hud-panel.bottom { padding: 10px calc(10px + env(safe-area-inset-left, 0px)) calc(10px + env(safe-area-inset-bottom, 0px)); }
  .button-group { width: 100%; }
  .button-group .btn-primary, .button-group .btn-secondary { flex: 1; }

  /* Below-bar chrome clears the now-compact (~52px) single-row top bar. */
  .settings-btn  { top: 64px; font-size: 20px; padding: 6px; }
  .settings-panel { top: 108px; min-width: 0; width: calc(100vw - 32px); max-width: 280px; }
  /* Watcher pill: align under the gear on the right (base rule's right:64px left it
     stranded mid-screen on mobile, overlapping the board). */
  .watcher-pill   { top: 110px; right: 12px; font-size: 12px; padding: 5px 10px; }
  .spectating-tag { top: 110px; left: 12px; }

  /* Emoji bar vertical position is set in JS (REACT.reposition) to sit just above the
     bottom panel — its height varies per device, so a fixed offset overlapped it. */

  /* Modals: base rule already top-aligns + scrolls; just add safe-area padding for notches
     and tighten the card on small screens. */
  .modal {
    padding: calc(12px + env(safe-area-inset-top, 0px)) 12px calc(12px + env(safe-area-inset-bottom, 0px));
  }
  .modal-content {
    padding: 20px;
    max-width: 460px;
  }
  .modal-content h2 { font-size: 22px; margin-bottom: 16px; }

  /* App dialog matches. */
  .app-dialog-box { min-width: 0; width: 88%; max-width: 360px; }
  .app-dialog-buttons { flex-direction: column-reverse; }
  .app-dialog-btn { width: 100%; }

  /* Reaction bar: tighter so 5 emojis fit a narrow screen. */
  .react-bar { gap: 4px; }
  .react-btn { font-size: 22px; padding: 6px 8px; }
}

/* Very small phones: trim further so the board still gets vertical room. */
@media (max-width: 380px) {
  .turn-indicator { font-size: 13px; }
  .player-name { font-size: 11px; }
  .avatar { width: 28px; height: 28px; font-size: 18px; }
}

#coin-panel {
  position: fixed;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  width: 220px;
  background: rgba(20, 28, 48, 0.92);
  border: 1px solid #2f3a6a;
  border-radius: 12px;
  padding: 16px;
  color: #e8ecf5;
  font-family: inherit;
  z-index: 50;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}
#coin-panel.hidden { display: none; }
.coin-title { font-weight: 700; margin-bottom: 12px; text-align: center; }
.coin-calls { display: flex; gap: 8px; margin-bottom: 10px; }
.coin-call-btn {
  flex: 1; padding: 10px 0; border-radius: 8px; border: 2px solid transparent;
  cursor: pointer; font-weight: 700; text-transform: none;
}
#coin-call-heads { background: #b62b35; color: #fff; }
#coin-call-tails { background: #2a5ac0; color: #fff; }
.coin-call-btn.selected { border-color: #e8c46a; }
.coin-call-btn:disabled { opacity: 0.6; cursor: default; }
.coin-flip-btn, .coin-start-btn {
  width: 100%; padding: 10px 0; border-radius: 8px; border: none; cursor: pointer;
  font-weight: 700; background: #e8c46a; color: #2a2410; text-transform: none;
}
.coin-flip-btn:disabled { opacity: 0.5; cursor: default; }
.coin-result { margin: 12px 0; min-height: 1.2em; text-align: center; line-height: 1.35; }
.coin-start-btn.hidden, .coin-call-btn.hidden, .coin-flip-btn.hidden { display: none; }

/* Visibility segment (create form) */
.vis-seg { display: flex; gap: 8px; }
.vis-btn {
  flex: 1; padding: 10px 0; border-radius: 8px; border: 1px solid rgba(255,255,255,0.2);
  background: rgba(0,0,0,0.3); color: rgba(255,255,255,0.7); text-transform: none; font-size: 13px;
}
.vis-btn.selected { background: var(--secondary); border-color: var(--secondary); color: #fff; }

/* Public-games lobby list */
.lobby-header { font-weight: 700; margin-bottom: 8px; font-size: 14px; }
.lobby-list { display: flex; flex-direction: column; gap: 8px; max-height: 160px; overflow-y: auto; }
.lobby-empty { opacity: 0.6; font-size: 13px; text-align: center; padding: 12px 0; }

/* Custom-game disclosure: keeps the modal short by hiding Create/Join/Lobby until wanted. */
.custom-toggle {
  background: none;
  border: none;
  color: var(--secondary);
  font-size: 13px;
  text-transform: none;
  letter-spacing: 0;
  padding: 4px;
  align-self: center;
  cursor: pointer;
}
.custom-toggle:hover { text-decoration: underline; }
.custom-game { display: flex; flex-direction: column; gap: 16px; }
.custom-game.hidden { display: none; }
.lobby-row {
  display: flex; align-items: center; gap: 12px; background: rgba(0,0,0,0.25);
  border: 1px solid rgba(255,255,255,0.1); border-radius: 10px; padding: 10px 12px;
}
.lobby-info { flex: 1; min-width: 0; }
.lobby-name { font-weight: 700; font-size: 14px; }
.lobby-meta { display: flex; gap: 10px; font-size: 11px; opacity: 0.85; margin-top: 3px; }
.lobby-live { color: var(--danger); font-weight: 700; }
.lobby-open { color: var(--success); font-weight: 700; }
.lobby-eye { color: var(--secondary); }
.lobby-actions { display: flex; gap: 6px; flex: none; }
.lobby-play, .lobby-watch { padding: 7px 13px; border-radius: 7px; font-size: 12px; text-transform: none; }
.lobby-play { background: var(--secondary); color: #fff; }
.lobby-watch { background: rgba(255,255,255,0.1); color: #fff; border: 1px solid rgba(255,255,255,0.2); }

/* Watcher counter pill — sits below the top bar, left of the settings gear (avoids
   overlapping the player cards in #top-bar, which spans the full width). */
.watcher-pill {
  position: absolute; top: 104px; right: 64px; display: flex; align-items: center; gap: 6px;
  background: rgba(20,28,48,0.78); border: 1px solid rgba(255,255,255,0.15); border-radius: 20px;
  padding: 6px 12px; font-size: 13px; font-weight: 700; pointer-events: auto; z-index: 15;
  white-space: nowrap;
}
.watcher-pill.hidden { display: none; }
.watcher-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--danger); box-shadow: 0 0 7px var(--danger); animation: pulse 2s infinite; flex: none; }

/* Spectating tag (spectators only) — below the top bar on the left, clear of the P1 card. */
.spectating-tag {
  position: absolute; top: 104px; left: 16px; background: rgba(74,156,255,0.25);
  border: 1px solid var(--secondary); color: #cfe0ff; border-radius: 20px;
  padding: 5px 12px; font-size: 11px; font-weight: 700; pointer-events: none; z-index: 15;
}
.spectating-tag.hidden { display: none; }

/* Emoji reaction bar (bottom-center, above the bottom panel) + floating glyphs */
.react-bar {
  position: absolute; bottom: 92px; left: 50%; transform: translateX(-50%);
  display: flex; gap: 8px; pointer-events: auto; z-index: 16;
}
.react-bar.hidden { display: none; }
.react-btn {
  width: 40px; height: 40px; border-radius: 50%; font-size: 18px; line-height: 1;
  background: rgba(20,28,48,0.8); border: 1px solid rgba(255,255,255,0.15); text-transform: none;
}
.react-layer { position: absolute; inset: 0; pointer-events: none; z-index: 16; overflow: hidden; }
.react-float {
  position: absolute; bottom: 120px; font-size: 26px;
  animation: reactFloat 1.5s ease-out forwards;
}
@keyframes reactFloat {
  0% { transform: translateY(0); opacity: 0; }
  15% { opacity: 1; }
  100% { transform: translateY(-180px); opacity: 0; }
}

.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 22, 40, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40; /* above modals (20) */
  pointer-events: auto;
}
.loading-overlay.hidden { display: none; }
.loading-box { text-align: center; color: #e8eef6; width: 280px; }
.loading-title { font-size: 18px; margin-bottom: 14px; text-shadow: 0 2px 4px rgba(0,0,0,0.5); }
.loading-bar { height: 10px; background: rgba(255,255,255,0.12); border-radius: 6px; overflow: hidden; }
.loading-fill { height: 100%; width: 0%; background: linear-gradient(90deg, #ffd700, #ffce4b); transition: width 0.2s; }
.loading-pct { margin-top: 8px; font-size: 14px; opacity: 0.85; }

/* ---------- AdSense bottom banner ---------- */
.ad-banner {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--ad-height);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  z-index: 5; /* below the HUD (z-index:10); HUD is offset above it, never over it */
  background: rgba(0,0,0,0.65);
  transition: height 0.25s ease, opacity 0.25s ease;
}
.ad-banner.collapsed {
  height: 0;
  opacity: 0;
  pointer-events: none;
}
.ad-banner .adsbygoogle { width: 100%; height: 100%; }

/* Non-interactive safety gap between the bottom HUD buttons and the ad. */
.ad-spacer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: var(--ad-height);
  height: var(--ad-gap);
  pointer-events: none;
  z-index: 4;
}

/* In-game dialog (MODAL.confirm/alert) — replaces native browser popups.
   Sits above all other overlays (game-over modal 40, reactions 16). */
.app-dialog {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  pointer-events: auto;
  animation: fadeIn 0.2s;
}
.app-dialog.hidden { display: none; }

.app-dialog-box {
  /* inherits gradient/border/radius/shadow from .modal-content */
  min-width: 300px;
  max-width: 420px;
  text-align: center;
}
.app-dialog-title {
  font-size: 22px;
  margin-bottom: 14px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}
.app-dialog-msg {
  font-size: 16px;
  line-height: 1.5;
  margin-bottom: 24px;
  color: rgba(255,255,255,0.92);
}
.app-dialog-buttons {
  display: flex;
  gap: 12px;
  justify-content: center;
}
.app-dialog-btn { min-width: 110px; }

/* ---- Matchmaking overlay + pill ---- */
.mm-primary #btn-find-match { font-size: 1.15rem; }
.mm-overlay-content { text-align: center; display: flex; flex-direction: column; gap: 14px; align-items: center; }
.mm-overlay-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.mm-status { font-weight: 600; font-size: 1.1rem; }
.mm-elapsed { color: #97a0c8; font-size: 0.9rem; }
.mm-spinner {
  width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid rgba(255,255,255,0.15); border-top-color: #4a6cf7;
  animation: mm-spin 0.9s linear infinite;
}
@keyframes mm-spin { to { transform: rotate(360deg); } }

.mm-pill {
  position: fixed; right: 14px; bottom: 14px; z-index: 60;
  display: flex; align-items: center; gap: 10px;
  background: #1c2030; color: #e8ecff; border: 1px solid #2e3450;
  border-radius: 999px; padding: 8px 12px; box-shadow: 0 4px 14px rgba(0,0,0,0.35);
  font-size: 0.9rem;
}
.mm-pill.hidden { display: none; }
.mm-pill-spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.2); border-top-color: #4a6cf7;
  animation: mm-spin 0.9s linear infinite;
}
.mm-pill-cancel {
  border: none; background: transparent; color: #ff8a96; cursor: pointer;
  font-weight: 600; padding: 2px 6px;
}
