:root {
  --cream: #f4e8d4;
  --wood: #5c3d2e;
  /* Warmer, slightly more golden cream (closer to the pre–flat-wash look). */
  --paper: #fff1d6;
  --accent: #ffb703;
  --danger: #bf360c;
  /* Warm wash over scrollable BG only; GameScene reads this for canvas draw (Phaser single canvas). */
  --background-overlay-opacity: 0.18;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

/*
 * Page chrome is intentionally a single solid cream that matches the Phaser
 * canvas backgroundColor and the loading overlay. The previous html+body
 * `linear-gradient` produced visible horizontal banding on tall scrollable
 * pages (the gradient gets re-tiled per ~100vh on some browsers / Safari
 * with body { overflow-y: auto }), which read as "brown horizontal lines"
 * during the load before any game art was painted.
 */
html,
body {
  background: var(--paper);
}

body {
  margin: 0;
  color: #3e2723;
  font-family: "Segoe UI", system-ui, sans-serif;
  overflow-x: hidden;
  overflow-y: auto;
  /* `none` can block taps/clicks on some browsers; the canvas can still call setPointerCapture for the game. */
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  /* Wider frame on desktop — most browsers had unused side margins at 1000px. */
  max-width: min(1280px, 96vw);
  margin: 0 auto;
  padding: 0.5rem 0.75rem 0.45rem;
}

@media (min-width: 1100px) {
  #app {
    max-width: min(1420px, 94vw);
    padding: 0.6rem 1rem 0.5rem;
  }
}

#app[hidden] {
  display: none !important;
}

.leaderboard-panel[hidden],
#leaderboard-panel[hidden] {
  display: none !important;
}

.pre-game:not([hidden]) {
  min-height: 100dvh;
}

.pre-game {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}

/*
 * `[hidden]` is display:none in the UA stylesheet, but `.pre-game { display:flex }`
 * has equal specificity — it wins depending on cascade order and the landing
 * screen stays on screen, stacking above the game.
 */
.pre-game[hidden],
#pre-game[hidden] {
  display: none !important;
}

.pre-game-card {
  width: min(420px, 100%);
  background: linear-gradient(165deg, rgba(244, 232, 212, 0.98), rgba(201, 169, 110, 0.92));
  border-radius: 14px;
  padding: 1.75rem 1.5rem 1.5rem;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.35),
    inset 0 0 0 3px rgba(92, 61, 46, 0.35);
}

.pre-game-brand {
  margin: 0 0 0.35rem;
  font-family: "Courier New", monospace;
  font-size: 2rem;
  font-weight: 900;
  color: var(--wood);
  letter-spacing: -0.02em;
  text-align: center;
}

.pre-game-lede {
  margin: 0 0 1.25rem;
  text-align: center;
  font-size: 0.92rem;
  color: rgba(62, 39, 35, 0.85);
}

.player-form {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.player-form .field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--wood);
}

.player-form .field span {
  padding-left: 2px;
}

.player-form input {
  font: inherit;
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  border: 2px solid rgba(92, 61, 46, 0.35);
  background: rgba(255, 248, 238, 0.95);
  color: #3e2723;
}

.player-form input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 183, 3, 0.35);
}

.form-error {
  margin: 0;
  min-height: 0;
  font-size: 0.82rem;
  font-weight: 600;
}

.form-error:not(:empty) {
  color: #ffcdd2;
  background: rgba(191, 54, 12, 0.92);
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
}

.btn-primary {
  margin-top: 0.25rem;
  padding: 0.65rem 1rem;
  border: none;
  border-radius: 10px;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  color: var(--wood);
  background: linear-gradient(180deg, #ffe066, var(--accent));
  box-shadow: 0 4px 0 rgba(92, 61, 46, 0.45);
}

.btn-primary:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 rgba(92, 61, 46, 0.45);
}

.btn-secondary {
  margin: 0;
  padding: 0.55rem 0.9rem;
  border: 1px solid rgba(92, 61, 46, 0.35);
  border-radius: 10px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  color: var(--wood);
  background: rgba(255, 248, 238, 0.9);
}

.btn-secondary:hover,
.btn-secondary:focus-visible {
  background: rgba(255, 224, 102, 0.55);
}

.resume-banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
  margin: 0 0 0.75rem;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  background: rgba(255, 248, 238, 0.55);
  border: 1px solid rgba(92, 61, 46, 0.18);
}

.resume-banner[hidden] {
  display: none !important;
}

.resume-banner-text {
  flex: 1 1 100%;
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--wood);
}

.resume-clear-btn {
  background: none;
  border: none;
  padding: 0.25rem 0.4rem;
  margin: 0;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 700;
  color: rgba(62, 39, 35, 0.7);
  text-decoration: underline;
  cursor: pointer;
}

.resume-clear-btn:hover,
.resume-clear-btn:focus-visible {
  color: var(--danger);
}

.admin-login-link {
  display: block;
  width: fit-content;
  margin: 0.85rem auto 0;
  color: rgba(62, 39, 35, 0.78);
  font-size: 0.82rem;
  font-weight: 700;
  text-decoration: none;
}

.admin-login-link:hover,
.admin-login-link:focus-visible {
  color: var(--wood);
  text-decoration: underline;
}

.leaderboard-head {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.35rem;
  margin-bottom: 0.5rem;
}

.leaderboard-head-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
  width: 100%;
}

.leaderboard-head-row h2 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.05rem;
  letter-spacing: 0.02em;
}

.leaderboard-restart-btn {
  flex-shrink: 0;
  margin: 0;
  padding: 0.4rem 0.85rem;
  border: none;
  border-radius: 8px;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 800;
  cursor: pointer;
  color: var(--wood);
  background: linear-gradient(180deg, #ffe066, var(--accent));
  box-shadow: 0 3px 0 rgba(92, 61, 46, 0.45);
  white-space: nowrap;
}

.leaderboard-restart-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(92, 61, 46, 0.45);
}

.leaderboard-hint {
  margin: 0;
  font-size: 0.82rem;
  opacity: 0.85;
  color: #ffcdd2;
}

.leaderboard-scroll {
  overflow: auto;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.22);
  box-shadow: inset 0 0 0 1px rgba(255, 248, 238, 0.12);
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.leaderboard-table th,
.leaderboard-table td {
  padding: 0.45rem 0.55rem;
  text-align: left;
  border-bottom: 1px solid rgba(255, 248, 238, 0.08);
}

.leaderboard-table th {
  font-weight: 700;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.8;
  background: rgba(0, 0, 0, 0.2);
}

.leaderboard-table tbody tr:hover td {
  background: rgba(255, 255, 255, 0.04);
}

.lb-rank {
  width: 2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.lb-user {
  font-weight: 700;
}

.lb-num {
  font-variant-numeric: tabular-nums;
}

.lb-date {
  white-space: nowrap;
  opacity: 0.85;
  font-size: 0.78rem;
}

.lb-empty {
  text-align: center;
  padding: 1rem !important;
  opacity: 0.85;
}

/* Frame + overlays share one clipped card (Phaser canvas paints inside). */
.game-stage {
  position: relative;
  width: 100%;
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 10px 36px rgba(0, 0, 0, 0.18),
    inset 0 0 0 1px rgba(92, 61, 46, 0.18);
  /* Match Phaser canvas + loading overlay so nothing dark bleeds through during load. */
  background: var(--paper);
}

#game-container {
  position: relative;
  z-index: 1;
  overflow: hidden;
  /* Same as Phaser backgroundColor so Scale.FIT letterboxing is not dark brown. */
  background: var(--paper);
}

.game-loading-overlay[hidden] {
  display: none !important;
}

.game-loading-overlay:not([hidden]) {
  display: flex;
}

.game-loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 15;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 1rem;
  background: linear-gradient(
    165deg,
    var(--paper) 0%,
    var(--cream) 55%,
    #e8dcc8 100%
  );
  box-shadow: inset 0 0 0 1px rgba(92, 61, 46, 0.12);
  pointer-events: none;
}

.game-loading-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.85rem;
}

.game-loading-spinner {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 4px solid rgba(92, 61, 46, 0.2);
  border-top-color: var(--wood);
  border-right-color: rgba(92, 61, 46, 0.45);
  animation: game-loading-spin 0.72s linear infinite;
}

.game-loading-text {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--wood);
  opacity: 0.88;
}

.game-loading-bar {
  width: min(220px, 60vw);
  height: 8px;
  border-radius: 999px;
  background: rgba(92, 61, 46, 0.18);
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(92, 61, 46, 0.12);
}

.game-loading-fill {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--wood));
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 160ms ease-out;
}

.game-loading-percent {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--wood);
  opacity: 0.7;
}

.game-loading-reload-btn {
  margin-top: 0.4rem;
  padding: 0.55rem 1rem;
  border: 1px solid rgba(92, 61, 46, 0.35);
  border-radius: 10px;
  font: inherit;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--wood);
  background: rgba(255, 224, 102, 0.85);
  cursor: pointer;
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
}

.game-loading-reload-btn[hidden] {
  display: none !important;
}

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

/*
 * Fullscreen toggle — touch devices only. Desktop Chrome / mice get
 * `(pointer: fine) + (hover: hover)` and hide this via CSS; bootstrap.js
 * also skips binding so we never call requestFullscreen there.
 */
.fullscreen-toggle-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 30;
  width: 32px;
  height: 32px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0;
  padding: 0;
  border: 1px solid rgba(92, 61, 46, 0.35);
  border-radius: 8px;
  background: rgba(255, 248, 238, 0.85);
  color: var(--wood);
  cursor: pointer;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.18);
  -webkit-tap-highlight-color: transparent;
}

.fullscreen-toggle-btn:hover,
.fullscreen-toggle-btn:focus-visible {
  background: rgba(255, 224, 102, 0.85);
  outline: none;
}

.fullscreen-toggle-btn[aria-pressed="true"] svg {
  transform: rotate(45deg);
}

@media (pointer: fine) and (hover: hover) {
  .fullscreen-toggle-btn {
    display: none !important;
  }
}

/*
 * iPhone fullscreen tooltip. The Fullscreen API isn't available on iOS
 * Safari for non-video elements, so the button tap shows this short hint
 * pointing at Add-to-Home-Screen instead of failing silently.
 */
.ios-fullscreen-hint {
  position: fixed;
  top: 48px;
  right: 8px;
  z-index: 50;
  max-width: min(320px, 84vw);
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  background: rgba(28, 22, 18, 0.94);
  color: var(--paper);
  font-size: 0.82rem;
  line-height: 1.35;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
  -webkit-tap-highlight-color: transparent;
}

/*
 * Layering reference: gameplay is painted on one Phaser canvas. The warm wash is
 * drawn in GameScene between background art and sprites; this class is unused in
 * DOM but documents the same treatment as the in-canvas overlay.
 */
.background-overlay {
  position: absolute;
  inset: 0;
  background: rgba(245, 235, 220, var(--background-overlay-opacity, 0.18));
  pointer-events: none;
  z-index: 2;
}

/* Full-bleed over the game stage (Phaser draws session + HUD in-frame). */
.game-stage .leaderboard-panel.leaderboard-overlay:not([hidden]) {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  z-index: 25;
  margin: 0;
  gap: 0.4rem;
  padding: 0.55rem 0.65rem 0.75rem;
  overflow-x: hidden;
  overflow-y: auto;
  background: rgba(18, 12, 8, 0.94);
  /* Body is now cream — keep panel text legible against the dark overlay. */
  color: var(--paper);
  border-radius: 10px;
  box-shadow: inset 0 0 0 1px rgba(255, 248, 238, 0.1);
  pointer-events: auto;
}

.game-stage .leaderboard-overlay .leaderboard-scroll {
  max-height: min(58vh, 22rem);
  flex-shrink: 1;
  min-height: 0;
}

#game-container canvas {
  display: block;
  touch-action: none;
}

.page-footer {
  margin-top: 0.6rem;
  text-align: center;
  font-size: 0.82rem;
  color: rgba(62, 39, 35, 0.78);
}

kbd {
  display: inline-block;
  padding: 0.1rem 0.35rem;
  border-radius: 3px;
  background: rgba(92, 61, 46, 0.12);
  border: 1px solid rgba(92, 61, 46, 0.28);
  color: var(--wood);
  font-size: 0.78rem;
  margin: 0 1px;
}

@media (max-width: 768px) {
  #app {
    padding: 0;
  }

  .game-stage {
    border-radius: 0;
    box-shadow: none;
  }

  .page-footer {
    display: none;
  }
}

@media (hover: none) and (pointer: coarse) {
  .page-footer {
    display: none;
  }
}

/*
 * Rotate-to-landscape prompt
 *
 * Hidden by default. Shown only on coarse-pointer devices (phones / tablets)
 * that are currently in portrait orientation. Sits above everything else so
 * the user has nothing to interact with until they rotate.
 */
.rotate-prompt {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: linear-gradient(160deg, #2c241c 0%, #4a3f36 60%, #1d1714 100%);
  color: var(--paper);
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  text-align: center;
}

.rotate-prompt-card {
  max-width: 26rem;
  width: 100%;
}

.rotate-prompt-icon {
  color: var(--accent);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1rem;
  animation: rotate-prompt-wiggle 2.4s ease-in-out infinite;
}

@keyframes rotate-prompt-wiggle {
  0%, 100% { transform: rotate(0deg); }
  35% { transform: rotate(-90deg); }
  65% { transform: rotate(-90deg); }
}

.rotate-prompt-title {
  margin: 0 0 0.5rem;
  font-family: "Courier New", monospace;
  font-size: 1.6rem;
  font-weight: 900;
  letter-spacing: 0.02em;
}

.rotate-prompt-text {
  margin: 0;
  font-size: 0.95rem;
  opacity: 0.9;
  line-height: 1.4;
}

/*
 * Only require landscape once the player has left the join form (#pre-game) and
 * the game shell (#app) is shown. Login / resume banner stay usable in portrait.
 */
@media (orientation: portrait) and (hover: none) and (pointer: coarse) and (max-width: 900px) {
  body.opsy-game-shell .rotate-prompt {
    display: flex;
  }
  /*
   * Hide the game shell under the overlay so the canvas can't flash through and
   * coarse scrolling doesn't reveal it before the user rotates.
   */
  body.opsy-game-shell > #app {
    display: none !important;
  }
}

/*
 * Touch devices in landscape: let the game-stage fill the entire viewport.
 * The Phaser canvas uses Scale.FIT and will letterbox itself inside this
 * stage, so the larger the stage, the larger the playfield gets.
 *
 * No max-height cap here — phones (height ~390–430px in landscape), large
 * phones (~500px), and tablets (768px+) all benefit from going full-bleed.
 * The previous `max-height: 540px` left every device above that threshold
 * stuck with the desktop 1000px frame, which felt tiny on a phone screen.
 *
 * Everything below is scoped to `body.opsy-game-shell` (the game is showing).
 * Without that guard, the pre-game form was unscrollable in iPhone landscape:
 * `html, body { height: 100%; overflow: hidden }` fired as soon as the user
 * rotated, locking the body to the visible viewport even though the form
 * was taller (resume banner + 3 fields + button + admin link).
 */
@media (orientation: landscape) and (hover: none) and (pointer: coarse) {
  /*
   * `html { height: 100% }` is required so child `height: 100%` declarations
   * resolve against the viewport rather than collapsing to 0. Without it,
   * iOS Safari sometimes reports `#game-container` height as the full screen
   * (taller than the visible 100dvh area), which let Phaser size the canvas
   * past the bottom of the screen — the painted floor disappeared and the
   * dog appeared to run mid-air. We pair this with `body.opsy-game-shell`
   * locking overflow only while the game is showing, so the pre-game form
   * still scrolls in landscape.
   */
  html:has(body.opsy-game-shell) {
    height: 100%;
    overflow: hidden;
  }
  body.opsy-game-shell {
    height: 100%;
    overflow: hidden;
  }
  body.opsy-game-shell #app {
    max-width: none;
    /*
     * `--opsy-vh` is set by main.js from `visualViewport.height / 100`, so
     * `calc(var(--opsy-vh) * 100)` is the JS-measured visible height. On
     * iPhone PWA, 100dvh occasionally reports the full-screen height even
     * when the visible area is shorter (status bar / home indicator chrome
     * is not subtracted). `min(100dvh, …)` picks whichever is correct,
     * falling back to plain 100dvh on platforms where the variable hasn't
     * been set yet (the JS sets it on first paint via main.js).
     */
    height: 100dvh;
    height: min(100dvh, calc(var(--opsy-vh, 1dvh) * 100));
    width: 100vw;
    padding: 0;
    margin: 0;
  }
  body.opsy-game-shell .game-stage {
    height: 100dvh;
    height: min(100dvh, calc(var(--opsy-vh, 1dvh) * 100));
    width: 100vw;
    border-radius: 0;
    box-shadow: none;
  }
  /*
   * NO flex centering here — Phaser's `Scale.CENTER_BOTH` already centers
   * the canvas via auto margins. Stacking flex centering on top of that on
   * iOS Safari / PWA produced a doubled offset where the canvas sat above
   * the visible bottom and clipped the painted floor. Letting Phaser handle
   * placement keeps the dog planted on the ground.
   */
  body.opsy-game-shell #game-container {
    height: 100%;
    width: 100%;
    overflow: hidden;
  }
  body.opsy-game-shell .page-footer {
    display: none;
  }
}

/*
 * Narrow leaderboards: the "Played" column carries long timestamps that push
 * the table wider than the viewport on phones. Hide it below ~640px so the
 * remaining columns fit and the panel doesn't need a horizontal scroll bar.
 */
@media (max-width: 640px) {
  .leaderboard-table th:nth-child(6),
  .leaderboard-table td:nth-child(6) {
    display: none;
  }

  .leaderboard-table th,
  .leaderboard-table td {
    padding: 0.4rem 0.4rem;
    font-size: 0.78rem;
  }

  .leaderboard-table .lb-rank {
    width: 1.6rem;
  }
}
