:root {
  --bg: #0b0d10;
  --panel: #151922;
  --panel-2: #1d2330;
  --text: #f5f7fb;
  --muted: #a8b0c2;
  --accent: #38bdf8;
  --accent-2: #22c55e;
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.14);
  --border: rgba(255,255,255,0.11);
  --shadow: 0 20px 55px rgba(0,0,0,0.42);
  --radius: 18px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(56,189,248,0.12), transparent 36rem),
    radial-gradient(circle at bottom right, rgba(34,197,94,0.08), transparent 30rem),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.shell {
  width: min(1240px, calc(100% - 28px));
  margin: 0 auto;
  padding: 18px 0 28px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 18px;
}

h1 {
  margin: 0;
  font-size: clamp(1.55rem, 3vw, 2.45rem);
  letter-spacing: 0.04em;
}

h2, h3 { margin: 0 0 12px; }
.subtitle { margin: 6px 0 0; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 0.88rem; }
.hidden { display: none !important; }

.connection-pill {
  flex: 0 0 auto;
  padding: 8px 13px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  background: rgba(255,255,255,0.04);
  font-size: 0.9rem;
}
.connection-pill.ok { color: #bbf7d0; border-color: rgba(34,197,94,0.35); background: rgba(34,197,94,0.1); }
.connection-pill.bad { color: #fecaca; border-color: rgba(239,68,68,0.35); background: rgba(239,68,68,0.1); }

.panel, .side-panel, .board-frame {
  background: linear-gradient(180deg, rgba(255,255,255,0.055), rgba(255,255,255,0.025));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.start-panel { padding: 18px; }
.start-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}
.start-card {
  padding: 20px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
}
.primary-card {
  background: linear-gradient(180deg, rgba(56,189,248,0.10), rgba(255,255,255,0.035));
}

label {
  display: block;
  margin: 13px 0 6px;
  color: var(--muted);
  font-weight: 600;
  font-size: 0.92rem;
}
input {
  width: 100%;
  min-height: 46px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: rgba(0,0,0,0.22);
  color: var(--text);
  padding: 0 13px;
  outline: none;
}
input:focus { border-color: rgba(56,189,248,0.7); box-shadow: 0 0 0 3px rgba(56,189,248,0.15); }
.time-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.code-input {
  text-align: center;
  font-size: 1.8rem;
  letter-spacing: 0.28em;
  font-weight: 800;
}

button {
  border: 0;
  border-radius: 13px;
  cursor: pointer;
  padding: 10px 14px;
  font-weight: 800;
  color: var(--text);
}
button:disabled { opacity: 0.55; cursor: not-allowed; }
.big-btn { width: 100%; min-height: 48px; margin-top: 16px; }
.primary { background: linear-gradient(135deg, #0ea5e9, #22c55e); }
.secondary { background: rgba(255,255,255,0.09); border: 1px solid var(--border); }
.danger-btn { background: var(--danger); }
.danger-outline { background: var(--danger-soft); color: #fecaca; border: 1px solid rgba(239,68,68,0.45); }
.tiny-btn { padding: 6px 9px; border-radius: 10px; background: rgba(255,255,255,0.09); border: 1px solid var(--border); font-size: 0.82rem; }

.created-code-box {
  margin-top: 18px;
  padding: 16px;
  border: 1px solid rgba(56,189,248,0.25);
  background: rgba(56,189,248,0.08);
  border-radius: 16px;
  text-align: center;
}
.big-code {
  margin: 10px auto;
  font-size: clamp(2.4rem, 10vw, 5rem);
  line-height: 1;
  font-weight: 950;
  letter-spacing: 0.16em;
  font-variant-numeric: tabular-nums;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(220px, 280px) minmax(310px, 560px) minmax(220px, 300px);
  gap: 18px;
  align-items: start;
}
.side-panel { padding: 16px; background-color: var(--panel); }
.room-code-line {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px;
  border-radius: 14px;
  background: rgba(0,0,0,0.22);
  margin-bottom: 14px;
}
.room-code-line strong { font-size: 1.5rem; letter-spacing: 0.14em; font-variant-numeric: tabular-nums; }

.players { display: grid; gap: 10px; }
.player-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 15px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.045);
}
.player-card.active {
  border-color: rgba(56,189,248,0.7);
  box-shadow: 0 0 0 3px rgba(56,189,248,0.13);
}
.piece-dot { width: 22px; height: 22px; border-radius: 50%; border: 2px solid rgba(255,255,255,0.4); flex: 0 0 auto; }
.white-dot { background: #f8fafc; }
.black-dot { background: #111827; }
.player-name { font-weight: 800; }
.clock {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: #dbeafe;
  font-size: 1.35rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}
.status-line {
  margin-top: 14px;
  color: var(--muted);
  line-height: 1.45;
  min-height: 42px;
}

.board-section { min-width: 0; }
.board-frame {
  padding: clamp(8px, 1.6vw, 16px);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.08), rgba(255,255,255,0.025)),
    #111827;
}
#board {
  width: min(74vmin, 560px);
  max-width: 100%;
  margin: 0 auto;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 12px 30px rgba(0,0,0,0.5);
}

/* chessboard.js professional-looking board tweaks */
.white-1e1d7 { background-color: #f0d9b5 !important; }
.black-3c85d { background-color: #b58863 !important; }
.highlight-last {
  box-shadow: inset 0 0 0 9999px rgba(255, 235, 59, 0.34) !important;
}
.highlight-selected {
  box-shadow: inset 0 0 0 4px rgba(14, 165, 233, 0.85) !important;
}

.move-history {
  min-height: 265px;
  max-height: 430px;
  overflow: auto;
  padding: 12px;
  border-radius: 14px;
  background: rgba(0,0,0,0.24);
  color: #d1d5db;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  line-height: 1.75;
  white-space: pre-wrap;
}
.controls { display: grid; gap: 9px; margin-top: 14px; }

.disconnect-notice {
  margin-bottom: 14px;
  padding: 12px 14px;
  border-radius: 15px;
  background: rgba(245,158,11,0.14);
  color: #fde68a;
  border: 1px solid rgba(245,158,11,0.35);
  text-align: center;
  font-weight: 800;
}

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(0,0,0,0.62);
  backdrop-filter: blur(6px);
}
.modal-box {
  width: min(440px, 100%);
  border-radius: 20px;
  border: 1px solid var(--border);
  background: #151922;
  box-shadow: var(--shadow);
  padding: 20px;
}
.modal-box p { color: var(--muted); line-height: 1.55; }
.modal-buttons { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.promotion-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.promotion-options button {
  background: rgba(255,255,255,0.07);
  border: 1px solid var(--border);
  padding: 10px;
}
.promotion-options img { width: 54px; height: 54px; object-fit: contain; }

.blink { animation: blink 0.7s infinite alternate; }
@keyframes blink { from { filter: brightness(1); } to { filter: brightness(1.7); } }

@media (max-width: 980px) {
  .game-layout { grid-template-columns: 1fr; }
  .top-side { order: 1; }
  .board-section { order: 2; }
  .bottom-side { order: 3; }
  #board { width: min(94vw, 560px); }
  .move-history { min-height: 110px; max-height: 150px; }
  .players { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 760px) {
  .shell { width: min(100% - 16px, 1240px); padding-top: 10px; }
  .topbar { align-items: flex-start; }
  .subtitle { font-size: 0.9rem; }
  .start-grid { grid-template-columns: 1fr; }
  .start-panel { padding: 10px; }
  .start-card { padding: 14px; }
  .players { grid-template-columns: 1fr; }
  .room-code-line strong { font-size: 1.25rem; }
  .clock { font-size: 1.15rem; }
  .side-panel { padding: 12px; }
  .board-frame { padding: 7px; }
  #board { width: min(96vw, calc(100vh - 320px)); min-width: 300px; }
}

@media (max-width: 420px) {
  .topbar { flex-direction: column; gap: 8px; }
  .connection-pill { align-self: flex-start; }
  #board { min-width: 286px; }
  .modal-buttons { justify-content: stretch; }
  .modal-buttons button { flex: 1 1 auto; }
}

/* Final simplified 4-digit code start screen */
.simple-code-panel {
  width: min(94vw, 560px);
  margin: 0 auto;
}

.simple-code-card {
  width: 100%;
  background:
    radial-gradient(circle at top left, rgba(76, 175, 80, 0.18), transparent 32%),
    linear-gradient(145deg, rgba(35,35,35,0.98), rgba(18,18,18,0.98));
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: 28px;
  padding: clamp(22px, 4vw, 38px);
  box-shadow: 0 24px 80px rgba(0,0,0,0.45);
  text-align: center;
}

.brand-mark {
  width: 68px;
  height: 68px;
  display: grid;
  place-items: center;
  margin: 0 auto 14px;
  border-radius: 22px;
  background: rgba(76,175,80,0.14);
  border: 1px solid rgba(76,175,80,0.35);
  font-size: 42px;
}

.simple-code-card h2 {
  margin: 0 0 8px;
  font-size: clamp(26px, 5vw, 38px);
}

.clean-time-grid {
  margin: 24px 0 20px;
}

.clean-time-grid input {
  text-align: center;
  font-size: 20px;
  font-weight: 800;
}

.your-code-box {
  margin: 22px auto;
  padding: 22px;
  border-radius: 24px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.10);
}

.code-label {
  color: #aaa;
  font-size: 15px;
  margin-bottom: 8px;
}

.big-code {
  font-size: clamp(46px, 12vw, 72px);
  font-weight: 900;
  letter-spacing: 0.16em;
  line-height: 1;
  font-family: "Courier New", monospace;
  color: #fff;
  text-shadow: 0 0 24px rgba(76,175,80,0.35);
}

.join-clean-box {
  margin-top: 20px;
}

.join-clean-box label {
  display: block;
  margin-bottom: 10px;
  color: #ddd;
  font-weight: 700;
}

.join-clean-box .code-input {
  width: min(100%, 300px);
  margin: 0 auto 14px;
  text-align: center;
  font-size: 34px;
  font-weight: 900;
  letter-spacing: 0.18em;
  font-family: "Courier New", monospace;
}

.note-line {
  margin-top: 14px;
}

@media (max-width: 640px) {
  .simple-code-card {
    padding: 20px;
    border-radius: 22px;
  }

  .brand-mark {
    width: 56px;
    height: 56px;
    font-size: 34px;
  }

  .join-clean-box .code-input {
    font-size: 30px;
  }
}

/* Pilapila inherited animated tile background */
body.pilapila-chess-bg {
  background:
    radial-gradient(circle at top left, rgba(250, 211, 144, 0.50), transparent 34%),
    radial-gradient(circle at bottom right, rgba(231, 127, 103, 0.28), transparent 34%),
    #f0f0f0 !important;
  color: #202124;
}

body.pilapila-chess-bg::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background: linear-gradient(135deg, rgba(255,255,255,0.16), rgba(255,255,255,0));
}

#tile-container {
  position: fixed;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.tile {
  position: absolute;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  border-radius: 18px;
  opacity: 0.72;
  will-change: transform;
  transform: translateZ(0);
  filter: saturate(1.04);
}

.app-shell,
.modal-overlay,
.promotion-overlay {
  position: relative;
  z-index: 2;
}

.simple-code-card,
.panel,
.board-card,
.side-panel {
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

@media (prefers-reduced-motion: reduce) {
  .tile {
    display: none;
  }
}

/* Z-index fix: keep Pilapila animated tiles behind chess UI */
html,
body.pilapila-chess-bg {
  min-height: 100%;
}

body.pilapila-chess-bg {
  position: relative;
  isolation: isolate;
  overflow-x: hidden;
}

/* Background layer only */
body.pilapila-chess-bg::before {
  z-index: 0 !important;
}

/* Tiles must stay behind all UI */
body.pilapila-chess-bg #tile-container {
  position: fixed !important;
  inset: 0 !important;
  z-index: 0 !important;
  pointer-events: none !important;
  overflow: hidden !important;
}

/* Individual tiles should never create a higher layer */
body.pilapila-chess-bg #tile-container .tile {
  z-index: 0 !important;
  pointer-events: none !important;
}

/* Main chess application above the background */
body.pilapila-chess-bg .app-shell,
body.pilapila-chess-bg main,
body.pilapila-chess-bg .panel,
body.pilapila-chess-bg .start-panel,
body.pilapila-chess-bg .simple-code-panel,
body.pilapila-chess-bg .simple-code-card,
body.pilapila-chess-bg .game-panel,
body.pilapila-chess-bg .board-card,
body.pilapila-chess-bg .side-panel {
  position: relative !important;
  z-index: 10 !important;
}

/* Popups must be above everything */
body.pilapila-chess-bg .modal-overlay,
body.pilapila-chess-bg .promotion-overlay {
  position: fixed !important;
  z-index: 9999 !important;
}

/* Dragged chess pieces should remain above board/background */
body.pilapila-chess-bg .dragging-piece,
body.pilapila-chess-bg img.chess-piece,
body.pilapila-chess-bg .piece-417db {
  z-index: 50 !important;
}

/* --------------------------------------------------------------------------
   Mobile chessboard fixes + responsive game UI
   Added 2026-05-24.
   Purpose:
   - Desktop keeps chessboard.js drag/drop.
   - Mobile/coarse-pointer devices get stable tap-to-move from app.js.
   - Touching the board must not scroll the page.
   - Dark backgrounds always use bright text; light backgrounds use dark text.
-------------------------------------------------------------------------- */

body.chess-dragging,
body.pilapila-chess-bg.chess-dragging {
  overflow: hidden;
  overscroll-behavior: none;
}

/* The board itself is an interactive surface, not a scroll surface. */
#board,
#board .board-b72b1,
#board [class*="square-"],
.board-section,
.board-frame {
  -ms-touch-action: none !important;
  touch-action: none !important;
}

#board,
#board * {
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

#board img {
  display: block;
  max-width: 100%;
  height: auto;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform: translateZ(0);
}

/* Keep the chess board in LTR coordinates even when the page language is RTL. */
#board,
#board *,
.board-section,
.board-frame {
  direction: ltr !important;
}

/* Game panels are dark, so text inside them must be bright. */
.game-panel,
.game-panel .side-panel,
.game-panel .board-frame {
  color: #f8fafc;
}

.game-panel .side-panel {
  background: rgba(17, 24, 39, 0.94);
  border-color: rgba(255, 255, 255, 0.16);
}

.game-panel .room-code-line,
.game-panel .player-card,
.game-panel .move-history {
  background: rgba(2, 6, 23, 0.46);
  color: #f8fafc;
  border-color: rgba(255, 255, 255, 0.14);
}

.game-panel .room-code-line span,
.game-panel .room-code-line strong,
.game-panel .player-name,
.game-panel .clock,
.game-panel h3 {
  color: #f8fafc;
}

.game-panel .status-line {
  color: #dbeafe;
}

.game-panel .move-history {
  color: #e5e7eb;
}

.game-panel .secondary,
.game-panel .tiny-btn {
  color: #f8fafc;
  background: rgba(255, 255, 255, 0.10);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.game-panel .secondary:hover,
.game-panel .tiny-btn:hover {
  background: rgba(255, 255, 255, 0.16);
}

.game-panel .danger-outline {
  color: #fee2e2;
  background: rgba(127, 29, 29, 0.48);
  border: 1px solid rgba(248, 113, 113, 0.48);
}

.modal-box {
  color: #f8fafc;
  background: #151922;
}

.modal-box p {
  color: #d1d5db;
}

.modal-box .secondary {
  color: #f8fafc;
  background: rgba(255, 255, 255, 0.10);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

/* On mobile, app.js disables chessboard.js dragging and uses tap-to-move.
   Disable image hit-testing so the square receives taps reliably. */
@media (pointer: coarse), (max-width: 980px) {
  #board img {
    pointer-events: none;
  }

  #board [class*="square-"] {
    cursor: pointer;
  }
}

/* Tablet and narrow desktop: stack the game, but keep information compact. */
@media (max-width: 980px) {
  .game-layout {
    grid-template-columns: 1fr !important;
    gap: 12px;
    align-items: stretch;
  }

  .top-side {
    order: 1;
  }

  .board-section {
    order: 2;
  }

  .bottom-side {
    order: 3;
  }

  .players {
    grid-template-columns: 1fr 1fr;
  }

  #board {
    width: min(92vw, 560px) !important;
    min-width: 0 !important;
  }

  .move-history {
    min-height: 90px;
    max-height: 135px;
  }
}

/* Phone portrait: board-first feel with compact clocks and controls. */
@media (max-width: 760px) {
  .shell {
    width: min(100% - 14px, 1240px);
    padding-top: 8px;
    padding-bottom: 12px;
  }

  .game-layout {
    gap: 9px;
  }

  .topbar {
    margin-bottom: 7px;
  }

  .connection-pill {
    padding: 7px 10px;
    font-size: 0.78rem;
  }

  .side-panel {
    padding: 10px;
    border-radius: 16px;
  }

  .top-side {
    display: grid;
    gap: 8px;
  }

  .room-code-line {
    min-height: 38px;
    margin-bottom: 0;
    padding: 8px 10px;
  }

  .room-code-line strong {
    font-size: 1.12rem;
    letter-spacing: 0.12em;
  }

  .players {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px;
  }

  .player-card {
    min-width: 0;
    gap: 8px;
    padding: 8px;
    border-radius: 13px;
  }

  .piece-dot {
    width: 18px;
    height: 18px;
  }

  .player-name {
    max-width: 100%;
    overflow: hidden;
    font-size: 0.86rem;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .clock {
    font-size: 1.06rem;
  }

  .status-line {
    min-height: 0;
    margin-top: 0;
    padding: 8px 10px;
    border-radius: 12px;
    background: rgba(2, 6, 23, 0.36);
    text-align: center;
    font-size: 0.9rem;
  }

  .board-frame {
    padding: 6px;
    border-radius: 16px;
  }

  #board {
    width: min(calc(100vw - 28px), calc(100svh - 264px), 560px) !important;
    min-width: 0 !important;
    max-width: 100% !important;
    aspect-ratio: 1 / 1;
    border-radius: 9px;
  }

  .bottom-side {
    padding: 10px;
  }

  .bottom-side h3 {
    margin-bottom: 7px;
    font-size: 0.95rem;
  }

  .move-history {
    min-height: 58px;
    max-height: 86px;
    padding: 9px;
    font-size: 0.82rem;
    line-height: 1.55;
  }

  .controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 9px;
  }

  .controls button {
    min-height: 42px;
    padding: 9px 8px;
    border-radius: 12px;
    font-size: 0.88rem;
  }
}

/* Very small phones. */
@media (max-width: 420px) {
  .shell {
    width: min(100% - 10px, 1240px);
  }

  .players {
    gap: 6px;
  }

  .player-card {
    padding: 7px;
  }

  .clock {
    font-size: 1rem;
  }

  #board {
    width: min(calc(100vw - 18px), calc(100svh - 258px), 520px) !important;
  }

  .controls {
    gap: 7px;
  }

  .controls button {
    font-size: 0.82rem;
  }
}

/* Mobile landscape: use a real landscape layout instead of a tall stacked page. */
@media (max-height: 560px) and (orientation: landscape) {
  .shell {
    width: min(100% - 16px, 1240px);
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .topbar {
    min-height: 28px;
    margin-bottom: 6px;
  }

  .game-layout {
    grid-template-columns: minmax(170px, 0.75fr) minmax(240px, 1.05fr) minmax(170px, 0.8fr) !important;
    gap: 10px;
    align-items: center;
  }

  .top-side,
  .board-section,
  .bottom-side {
    order: initial;
  }

  .side-panel {
    max-height: calc(100svh - 58px);
    overflow: auto;
    padding: 9px;
  }

  .players {
    grid-template-columns: 1fr !important;
  }

  .player-card {
    padding: 7px 8px;
  }

  .status-line {
    font-size: 0.82rem;
  }

  .board-frame {
    padding: 5px;
  }

  #board {
    width: min(calc(100svh - 74px), 46vw, 500px) !important;
    min-width: 230px !important;
  }

  .move-history {
    min-height: 58px;
    max-height: 96px;
    font-size: 0.78rem;
  }

  .controls {
    grid-template-columns: 1fr;
    gap: 7px;
  }

  .controls button {
    min-height: 36px;
    padding: 7px 8px;
    font-size: 0.8rem;
  }
}

/* Large desktop keeps the original spacious three-column design. */
@media (min-width: 981px) {
  .game-layout {
    grid-template-columns: minmax(230px, 280px) minmax(380px, 560px) minmax(230px, 300px);
  }
}


/* --------------------------------------------------------------------------
   Mobile touch/tap chessboard hard fix v2
   The board is not allowed to become a scroll/drag surface on phones.
   app.js handles mobile moves by tap source square -> tap destination square.
-------------------------------------------------------------------------- */
body.chess-touch-mode #board,
body.chess-touch-mode #board *,
body.chess-touch-mode .board-frame,
body.chess-touch-mode .board-section {
  -ms-touch-action: none !important;
  touch-action: none !important;
  overscroll-behavior: contain !important;
  -webkit-user-select: none !important;
  user-select: none !important;
  -webkit-touch-callout: none !important;
}

body.chess-touch-mode #board img,
body.chess-touch-mode #board [draggable="true"] {
  pointer-events: none !important;
  -webkit-user-drag: none !important;
  user-drag: none !important;
}

body.chess-touch-mode #board [class*="square-"] {
  cursor: pointer !important;
}

body.chess-touch-mode.chess-dragging,
body.chess-touch-mode.chess-dragging html {
  overflow: hidden !important;
  overscroll-behavior: none !important;
}

/* Keep both clocks next to each other on all phone/tablet layouts. */
@media (max-width: 980px) {
  .players {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .player-card {
    min-width: 0 !important;
  }
}

@media (max-height: 560px) and (orientation: landscape) {
  .game-layout {
    grid-template-columns: minmax(225px, 0.95fr) minmax(230px, 1fr) minmax(175px, 0.78fr) !important;
  }

  .players {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 6px !important;
  }

  .player-card {
    padding: 6px !important;
    gap: 6px !important;
  }

  .piece-dot {
    width: 14px !important;
    height: 14px !important;
  }

  .player-name {
    font-size: 0.74rem !important;
  }

  .clock {
    font-size: 0.88rem !important;
  }
}

/* Extra contrast guard: dark cards/panels always keep bright text. */
.game-panel .side-panel,
.game-panel .side-panel *,
.game-panel .board-frame,
.game-panel .move-history,
.game-panel .room-code-line,
.game-panel .player-card {
  color: #f8fafc;
}

.game-panel .status-line {
  color: #dbeafe !important;
}

.game-panel input,
.start-panel input,
.compact-start-card input {
  color: #111827;
  background: rgba(255, 255, 255, 0.88);
}

/* --------------------------------------------------------------------------
   Language + game UI polish v3
   - Game room code row is hidden after game starts.
   - Connection pill is placed in a stable floating top-center position.
   - Disconnect notice is solid/non-transparent and bilingual text comes from app.js.
   - Persian language uses Persian digits in app.js, while move history stays LTR.
-------------------------------------------------------------------------- */

/* Better place for the connection pill: centered at the top, not inside the game panels. */
body.pilapila-chess-bg .topbar {
  position: fixed !important;
  top: max(10px, env(safe-area-inset-top)) !important;
  left: 50% !important;
  right: auto !important;
  transform: translateX(-50%) !important;
  z-index: 5002 !important;
  width: auto !important;
  min-height: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  display: flex !important;
  justify-content: center !important;
  align-items: center !important;
  pointer-events: none !important;
  direction: ltr !important;
}

body.pilapila-chess-bg .topbar > * {
  pointer-events: auto !important;
}

body.pilapila-chess-bg .topbar > div:first-child {
  display: none !important;
}

body.pilapila-chess-bg .connection-pill {
  min-height: 38px !important;
  padding: 8px 14px !important;
  border-radius: 999px !important;
  border: 2px solid #475569 !important;
  color: #f8fafc !important;
  background: #334155 !important;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.28) !important;
  font-size: 0.86rem !important;
  font-weight: 900 !important;
  line-height: 1 !important;
  white-space: nowrap !important;
  opacity: 1 !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

body.pilapila-chess-bg .connection-pill.ok {
  color: #052e16 !important;
  border-color: #16a34a !important;
  background: #86efac !important;
}

body.pilapila-chess-bg .connection-pill.bad {
  color: #450a0a !important;
  border-color: #dc2626 !important;
  background: #fca5a5 !important;
}

/* Leave enough breathing room for the floating top controls. */
body.pilapila-chess-bg .shell {
  padding-top: max(64px, calc(env(safe-area-inset-top) + 58px)) !important;
}

/* B. The in-game code row is no longer needed once the game starts. */
body.pilapila-chess-bg #gamePanel .room-code-line {
  display: none !important;
}

/* C. Disconnect notice must be solid, readable, and never transparent. */
body.pilapila-chess-bg .disconnect-notice {
  background: #f59e0b !important;
  color: #111827 !important;
  border: 2px solid #92400e !important;
  box-shadow: 0 10px 24px rgba(146, 64, 14, 0.24) !important;
  opacity: 1 !important;
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
}

/* Moves are the single exception requested by the user: always English/LTR. */
body.pilapila-chess-bg #moveHistory {
  direction: ltr !important;
  unicode-bidi: plaintext !important;
  text-align: left !important;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace !important;
}

/* Clocks/codes are formatted by app.js; keep their visual direction stable. */
body.pilapila-chess-bg .clock,
body.pilapila-chess-bg #createdCode,
body.pilapila-chess-bg #roomCodeLabel {
  direction: ltr !important;
  unicode-bidi: plaintext !important;
}

/* Dark backgrounds => bright text. */
body.pilapila-chess-bg .game-panel .side-panel,
body.pilapila-chess-bg .game-panel .board-frame,
body.pilapila-chess-bg .modal-box {
  color: #f8fafc !important;
}

body.pilapila-chess-bg .game-panel .side-panel *,
body.pilapila-chess-bg .game-panel .board-frame *,
body.pilapila-chess-bg .modal-box h2,
body.pilapila-chess-bg .modal-box p {
  color: inherit;
}

body.pilapila-chess-bg .game-panel .status-line,
body.pilapila-chess-bg .game-panel .move-history,
body.pilapila-chess-bg .modal-box p {
  color: #e5e7eb !important;
}

/* Light start card => dark text. */
body.pilapila-chess-bg .compact-start-card,
body.pilapila-chess-bg .compact-start-card label,
body.pilapila-chess-bg .compact-start-card .code-label,
body.pilapila-chess-bg .compact-start-card .note-line {
  color: #111827 !important;
}

/* Responsive refinements after hiding the code row. */
@media (max-width: 980px) {
  body.pilapila-chess-bg #gamePanel .top-side {
    padding-top: 10px !important;
  }

  body.pilapila-chess-bg #gamePanel .players {
    grid-template-columns: 1fr 1fr !important;
  }
}

@media (max-width: 760px) {
  body.pilapila-chess-bg .shell {
    padding-top: max(58px, calc(env(safe-area-inset-top) + 52px)) !important;
  }

  body.pilapila-chess-bg .connection-pill {
    min-height: 34px !important;
    padding: 7px 10px !important;
    font-size: 0.78rem !important;
  }

  body.pilapila-chess-bg #gamePanel .top-side {
    padding: 8px !important;
  }

  body.pilapila-chess-bg #gamePanel .players {
    grid-template-columns: 1fr 1fr !important;
    gap: 7px !important;
  }

  body.pilapila-chess-bg #gamePanel .player-card {
    min-width: 0 !important;
    padding: 8px !important;
  }
}

@media (max-width: 430px) {
  body.pilapila-chess-bg .connection-pill {
    font-size: 0.74rem !important;
    padding: 7px 9px !important;
  }
}

@media (max-width: 360px) {
  body.pilapila-chess-bg .connection-pill {
    font-size: 0.70rem !important;
    padding: 6px 8px !important;
  }
}

@media (max-height: 560px) and (orientation: landscape) {
  body.pilapila-chess-bg .topbar {
    top: max(7px, env(safe-area-inset-top)) !important;
  }

  body.pilapila-chess-bg .shell {
    padding-top: max(48px, calc(env(safe-area-inset-top) + 42px)) !important;
  }

  body.pilapila-chess-bg .connection-pill {
    min-height: 32px !important;
    padding: 6px 10px !important;
  }
}
