:root {
  --bg0: #0b1220;
  --bg1: #132033;
  --panel: #15253b;
  --line: #2a3f5c;
  --text: #e8f0ff;
  --muted: #8aa0bd;
  --accent: #3dd6c6;
  --shadow: rgba(0, 0, 0, 0.45);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  font-family: "Segoe UI", "Trebuchet MS", sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  color: var(--text);
  background:
    radial-gradient(1200px 600px at 20% -10%, #1a3a55 0%, transparent 55%),
    radial-gradient(900px 500px at 90% 10%, #1e2f4a 0%, transparent 50%),
    linear-gradient(160deg, var(--bg0), var(--bg1));
}

button {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.65rem 0.8rem;
  background: #1b314a;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

button:hover:not(:disabled) {
  background: #234064;
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

button.off {
  opacity: 0.55;
  color: var(--muted);
}

.game.hidden {
  display: none !important;
}

.btn-switch {
  border-color: var(--accent);
  background: #1a3d4a;
  color: #b8fff6;
  font-size: 0.85rem;
  line-height: 1.25;
}

.btn-switch:hover:not(:disabled) {
  background: #245a68;
  border-color: #7ef9ef;
}

.overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-content: center;
  gap: 0.5rem;
  padding: 0.75rem;
  text-align: center;
  background: rgba(5, 12, 22, 0.78);
  backdrop-filter: blur(2px);
}

.overlay.hidden {
  display: none;
}

.overlay p:first-child {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: 0.05em;
}

.hint {
  margin: 0;
  font-size: 0.75rem;
  line-height: 1.4;
  color: var(--muted);
}

.audio-nudge {
  position: absolute;
  left: 50%;
  bottom: 0.6rem;
  z-index: 5;
  transform: translateX(-50%);
  padding: 0.3rem 0.55rem;
  border-radius: 8px;
  font-size: 0.68rem;
  background: rgba(20, 40, 70, 0.92);
  border: 1px solid var(--line);
  pointer-events: none;
  white-space: nowrap;
}

.audio-nudge.hidden {
  display: none;
}

.clear-banner {
  position: absolute;
  left: 50%;
  top: 42%;
  z-index: 4;
  transform: translate(-50%, -50%) scale(0.85);
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  pointer-events: none;
  opacity: 0;
  font-size: 0.85rem;
  transition: opacity 0.12s ease, transform 0.18s ease;
  text-shadow: 0 0 18px currentColor;
}

.clear-banner.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.clear-banner.hidden {
  display: none;
}

.clear-banner.n1 { color: #9ae6ff; background: rgba(40, 90, 140, 0.55); }
.clear-banner.n2 { color: #7dffa8; background: rgba(30, 110, 70, 0.55); }
.clear-banner.n3 { color: #ffd27d; background: rgba(130, 90, 20, 0.55); }
.clear-banner.n4 { color: #ff9ad8; background: rgba(120, 30, 90, 0.65); }
.clear-banner.n5 { color: #ffe08a; background: rgba(140, 80, 20, 0.72); }

.clear-banner.pf-banner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.12rem;
  border-radius: 14px;
  padding: 0.55rem 0.9rem 0.5rem;
  min-width: 7.5rem;
  letter-spacing: 0.08em;
  text-align: center;
}

.clear-banner.pf-banner .pf-banner-flavor {
  font-size: 1.05rem;
  line-height: 1.1;
}

.clear-banner.pf-banner .pf-banner-combo {
  font-size: 0.78rem;
  opacity: 0.95;
  letter-spacing: 0.14em;
}

.clear-banner.pf-banner .pf-banner-blocks {
  font-size: 0.68rem;
  font-weight: 700;
  opacity: 0.88;
  letter-spacing: 0.1em;
}

/* ========== DESKTOP ========== */
.layout-mobile {
  display: none;
}

.layout-desktop {
  display: grid;
  grid-template-columns: 140px minmax(240px, 300px) 140px;
  gap: 1.25rem;
  justify-content: center;
  align-items: start;
  width: 100%;
  max-width: 920px;
  margin: 0 auto;
  padding: 1.5rem 1rem 2rem;
  min-height: 100vh;
  align-content: center;
}

.layout-desktop-mp {
  grid-template-columns: 140px minmax(320px, 640px) 150px;
  max-width: 1100px;
}

.layout-desktop-rr {
  grid-template-columns: 140px minmax(280px, 480px) 150px;
  max-width: 980px;
}

.board-desktop-rr {
  position: relative;
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  border-radius: 12px;
  overflow: hidden;
  border: 2px solid var(--line);
  background: #0a1810;
  box-shadow: 0 12px 30px var(--shadow);
  aspect-ratio: 480 / 640;
}

.board-desktop-rr canvas.js-rr-board {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.panel {
  background: color-mix(in srgb, var(--panel) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 1rem;
  box-shadow: 0 12px 30px var(--shadow);
}

.stat {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-bottom: 1rem;
}

.stat:last-child {
  margin-bottom: 0;
}

.label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.board-desktop {
  position: relative;
  width: 300px;
  max-width: 100%;
  margin: 0 auto;
  border-radius: 12px;
  overflow: hidden;
  border: 2px solid var(--line);
  box-shadow: 0 16px 40px var(--shadow);
  background: #071018;
}

.board-desktop canvas.js-board,
.board-desktop canvas.js-fx {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.board-desktop canvas.js-fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

.board-desktop-mp {
  position: relative;
  width: 100%;
  max-width: 640px;
  aspect-ratio: 16 / 9;
  margin: 0 auto;
  border-radius: 12px;
  overflow: hidden;
  border: 2px solid #3a4a2a;
  box-shadow: 0 16px 40px var(--shadow);
  background: #0a0e14;
}

.board-desktop-mp canvas.js-mp-board {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

.mp-controls-hint {
  margin: 0.75rem 0 0;
  font-size: 0.72rem;
  line-height: 1.45;
  color: var(--muted);
}

.next-canvas {
  display: block;
  width: 120px;
  height: 120px;
  background: #0a1522;
  border-radius: 8px;
  border: 1px solid var(--line);
  image-rendering: pixelated;
}

.controls {
  display: grid;
  gap: 0.6rem;
  margin-top: 0.5rem;
}

/* ========== MOBILE ========== */
@media (max-width: 720px) {
  html,
  body {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: none;
    height: 100%;
    height: 100dvh;
    position: fixed;
    inset: 0;
    width: 100%;
  }

  .app {
    height: 100%;
    width: 100%;
    overflow: hidden;
  }

  .layout-desktop {
    display: none;
  }

  .layout-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    max-height: 100%;
    margin: 0 auto;
    padding:
      max(0.2rem, var(--safe-t))
      max(0.25rem, var(--safe-r))
      max(0.2rem, var(--safe-b))
      max(0.25rem, var(--safe-l));
    overflow: hidden;
    box-sizing: border-box;
  }

  /* One tight cluster: score | board | next+buttons */
  .play-row {
    --rail: 2.55rem;
    --gap: 0.28rem;
    display: flex;
    flex-direction: row;
    align-items: stretch;
    gap: var(--gap);
    box-sizing: border-box;
    max-width: 100%;
    max-height: 100%;
    /* Prefer full available height; shrink if width can't fit board+rails */
    height: min(
      100%,
      calc((100vw - var(--rail) * 2 - var(--gap) * 2 - var(--safe-l) - var(--safe-r) - 0.5rem) * 2)
    );
    width: auto;
  }

  .play-row-moon {
    --rail: 2.7rem;
    height: min(
      100%,
      calc((100vw - var(--rail) * 2 - var(--gap) * 2 - var(--safe-l) - var(--safe-r) - 0.5rem) * 9 / 16)
    );
    width: min(
      100%,
      calc(
        (100dvh - var(--safe-t) - var(--safe-b) - 0.6rem) * 16 / 9
        + var(--rail) * 2
        + var(--gap) * 2
      )
    );
  }

  .rail-left,
  .rail-right {
    flex: 0 0 var(--rail);
    width: var(--rail);
    max-width: var(--rail);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 0;
    box-sizing: border-box;
  }

  .rail-left {
    justify-content: flex-start;
    gap: 0.65rem;
    padding-top: 0.2rem;
  }

  .rail-right {
    justify-content: flex-start;
    gap: 0.45rem;
    padding-top: 0.1rem;
  }

  .rail-right-mp {
    justify-content: flex-start;
    gap: 0.28rem;
  }

  .chip {
    display: block;
    width: 100%;
    text-align: center;
    font-size: 0.72rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
    overflow-wrap: anywhere;
  }

  .chip.muted {
    color: var(--muted);
    font-size: 0.58rem;
    font-weight: 600;
  }

  .hud-actions {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.4rem;
    margin-top: 0.15rem;
  }

  .hud-actions-mp {
    flex-direction: row;
    flex-wrap: nowrap;
    gap: 0.28rem;
    margin-top: 0;
    margin-left: auto;
  }

  .mp-mobile-hud .hud-actions-mp {
    flex-shrink: 0;
  }

  .btn-mini {
    width: 2.35rem;
    height: 2.35rem;
    padding: 0;
    font-size: 0.82rem;
    line-height: 1;
    flex-shrink: 0;
  }

  .hud-actions-mp .btn-mini {
    width: 2.2rem;
    height: 2.2rem;
    font-size: 0.78rem;
  }

  .board-mobile {
    position: relative;
    flex: 0 1 auto;
    height: 100%;
    width: auto;
    aspect-ratio: 1 / 2;
    max-height: 100%;
    max-width: calc(100vw - var(--rail) * 2 - var(--gap) * 2 - var(--safe-l) - var(--safe-r) - 0.5rem);
    border-radius: 10px;
    overflow: hidden;
    border: 2px solid var(--line);
    background: #050b12;
    box-shadow: 0 8px 24px var(--shadow);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
  }

  .board-mobile canvas.js-board-m,
  .board-mobile canvas.js-fx-m {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
  }

  .board-mobile canvas.js-fx-m {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 2;
  }

  .board-mobile-mp {
    position: relative;
    flex: 0 1 auto;
    height: 100%;
    width: auto;
    aspect-ratio: 16 / 9;
    max-height: 100%;
    max-width: calc(100vw - var(--rail) * 2 - var(--gap) * 2 - var(--safe-l) - var(--safe-r) - 0.5rem);
    border-radius: 10px;
    overflow: hidden;
    border: 2px solid #3a4a2a;
    background: #0a0e14;
    box-shadow: 0 8px 24px var(--shadow);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
  }

  .board-mobile-mp canvas.js-mp-board-m {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
  }

  .next-preview-m {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.12rem;
    padding: 0;
    pointer-events: none;
    flex-shrink: 0;
  }

  .next-label-m {
    font-size: 0.48rem;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--muted);
    font-weight: 600;
    line-height: 1;
    text-align: center;
  }

  .next-canvas-m {
    display: block;
    width: 2.4rem;
    height: 2.4rem;
    flex-shrink: 0;
    background: #0a1522;
    border-radius: 6px;
    border: 1px solid var(--line);
    image-rendering: pixelated;
  }

  /* Moon Rider mobile: full-width landscape board + bottom controls */
  .game-moon .layout-mobile-mp {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    width: 100%;
    height: 100%;
    max-height: 100%;
    padding:
      max(0.2rem, var(--safe-t))
      max(0.35rem, var(--safe-r))
      max(0.35rem, var(--safe-b))
      max(0.35rem, var(--safe-l));
    box-sizing: border-box;
    overflow: hidden;
  }

  .mp-mobile-hud {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.35rem;
    width: min(100%, 96vw);
    max-width: 640px;
  }

  .mp-mobile-hud .chip {
    width: auto;
    font-size: 0.78rem;
  }

  .game-moon .board-mobile-mp {
    position: relative;
    flex: 0 1 auto;
    width: min(100%, calc((100dvh - 7.5rem - var(--safe-t) - var(--safe-b)) * 16 / 9));
    max-width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
    max-height: calc(100dvh - 7.5rem - var(--safe-t) - var(--safe-b));
    border-radius: 10px;
    overflow: hidden;
    border: 2px solid var(--line);
    background: #050b12;
    box-shadow: 0 8px 24px var(--shadow);
    touch-action: none;
    user-select: none;
  }

  .game-moon .board-mobile-mp canvas.js-mp-board-m {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
  }

  .mp-mobile-controls {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: 1fr 1.4fr 1.4fr 1fr;
    gap: 0.35rem;
    width: min(100%, 96vw);
    max-width: 640px;
  }

  .btn-mp {
    min-height: 2.75rem;
    padding: 0.35rem 0.4rem;
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.04em;
  }

  .game-river:not(.hidden) {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    min-height: 100%;
  }

  .game-river .layout-desktop-rr {
    display: none !important;
  }

  .game-river .layout-mobile-rr {
    display: flex !important;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 0.25rem;
    width: 100%;
    height: 100%;
    max-height: 100%;
    flex: 1;
    padding:
      max(0.15rem, var(--safe-t))
      max(0.3rem, var(--safe-r))
      max(0.2rem, var(--safe-b))
      max(0.3rem, var(--safe-l));
    box-sizing: border-box;
    overflow: hidden;
  }

  .rr-mobile-hud {
    flex: 0 0 auto;
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.2rem;
    width: 100%;
    max-width: 100%;
    overflow: hidden;
  }

  .rr-mobile-hud .chip {
    width: auto;
    min-width: 0;
    padding: 0.2rem 0.35rem;
    font-size: 0.65rem;
  }

  .rr-mobile-hud .hud-actions-rr {
    display: flex;
    gap: 0.2rem;
    margin-left: auto;
    flex-shrink: 0;
  }

  .rr-mobile-hud .hud-actions-rr .btn-mini {
    width: 1.95rem;
    height: 1.95rem;
    font-size: 0.7rem;
  }

  .game-river .board-mobile-rr {
    position: relative;
    flex: 1 1 auto;
    width: min(100%, calc((100dvh - 5.2rem - var(--safe-t) - var(--safe-b)) * 480 / 640));
    max-width: min(96vw, 420px);
    height: auto;
    aspect-ratio: 480 / 640;
    max-height: calc(100dvh - 5.2rem - var(--safe-t) - var(--safe-b));
    min-height: 0;
    border-radius: 8px;
    overflow: hidden;
    border: 2px solid var(--line);
    background: #0a1810;
    box-shadow: 0 8px 24px var(--shadow);
    touch-action: none;
    user-select: none;
  }

  .game-river .board-mobile-rr canvas.js-rr-board-m {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    image-rendering: pixelated;
  }

  .rr-mobile-controls {
    flex: 0 0 auto;
    display: grid;
    grid-template-columns: 1fr 1.25fr 1.25fr 1fr;
    gap: 0.25rem;
    width: min(100%, 96vw);
    max-width: 420px;
  }

  .rr-mobile-controls .btn-mp {
    min-height: 2.35rem;
    font-size: 0.7rem;
    padding: 0.25rem 0.3rem;
  }

}

/* ========== PUZZLE FIGHTER ========== */
.board-desktop-pf canvas.js-pf-board {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.game-puzzle .board-desktop {
  /* 6×12 cells at same 300×600 footprint as Tetris */
  aspect-ratio: 1 / 2;
}

@media (max-width: 720px) {
  .board-mobile-pf canvas.js-pf-board-m {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
  }
}

/* ========== KLEJNOTY / BĄBELKI ========== */
.layout-desktop-sq,
.layout-desktop-bb {
  grid-template-columns: 140px minmax(280px, 420px) 140px;
}

.board-desktop-jw,
.board-desktop-bb {
  position: relative;
  aspect-ratio: 1 / 1;
  max-width: 420px;
}

.board-desktop-bb {
  aspect-ratio: 400 / 560;
}

.board-desktop-jw canvas,
.board-desktop-bb canvas {
  display: block;
  width: 100%;
  height: auto;
}

.board-desktop-jw canvas.js-jw-fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
}

.jw-banner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  border-radius: 14px;
  padding: 0.55rem 1rem;
  min-width: 8rem;
  letter-spacing: 0.1em;
  text-align: center;
}

.jw-banner .jw-banner-flavor {
  font-size: 1.15rem;
  line-height: 1.1;
  font-weight: 800;
}

.jw-banner .jw-banner-combo {
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  opacity: 0.9;
  text-transform: none;
  font-weight: 600;
}

@media (max-width: 720px) {
  .board-mobile-jw,
  .board-mobile-bb {
    position: relative;
    flex: 1;
    aspect-ratio: 1 / 1;
    max-height: min(72vw, 420px);
  }

  .board-mobile-bb {
    aspect-ratio: 400 / 560;
    max-height: min(78vh, 520px);
  }

  .board-mobile-jw canvas,
  .board-mobile-bb canvas {
    display: block;
    width: 100%;
    height: 100%;
  }

  .board-mobile-jw canvas.js-jw-fx-m {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 2;
  }

  .layout-desktop-sq,
  .layout-desktop-bb {
    display: none;
  }
}
