* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  background: #0f0f1a;
  color: #eee;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
body { touch-action: manipulation; }

/* Keep everything clear of the iPhone notch / status bar / home bar,
   and leave room at the bottom for the hosting badge. */
#app {
  max-width: 560px;
  margin: 0 auto;
  padding-top: max(10px, env(safe-area-inset-top, 0px));
  padding-left: max(10px, env(safe-area-inset-left));
  padding-right: max(10px, env(safe-area-inset-right));
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 4px;
  border-bottom: 1px solid #333;
}
header h1 { font-size: clamp(1.05rem, 5.4vw, 1.4rem); font-weight: 700; line-height: 1.2; min-width: 0; flex: 1 1 auto; overflow-wrap: break-word; }
#back-btn { flex: 0 0 auto; white-space: nowrap; }

#back-btn {
  background: #2a2a4a; color: #fff; border: none;
  padding: 10px 14px; border-radius: 10px; font-size: 1rem; cursor: pointer;
}
#back-btn.hidden { display: none; }

.screen { display: none; padding: 10px 0; }
.screen.active { display: block; }

/* ---------- Menu ---------- */
.game-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 12px;
}
.game-card {
  background: #1a1a2e;
  border-radius: 16px;
  padding: 22px 10px;
  text-align: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.15s, background 0.15s;
  border: 1px solid #2a2a4a;
}
.game-card:active { transform: scale(0.97); background: #22223a; }
.game-icon { font-size: 2.5rem; margin-bottom: 8px; }
.game-card h2 { font-size: 1.15rem; margin-bottom: 4px; }
.game-card p { font-size: 0.85rem; color: #aaa; }

/* ---------- Game controls ---------- */
.game-bar, .game-header {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.game-bar select, .game-bar button, .controls button {
  flex: 1 1 auto;
  min-height: 42px;
  background: #3a3a6a;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 16px; /* 16px+ stops iPhone Safari from auto-zooming the page when a picker is tapped */
  font-family: inherit;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  text-align: center;
}
.game-bar select:disabled { opacity: 0.45; }
.status, #sorry-status {
  font-weight: 700;
  font-size: 1.05rem;
  min-height: 1.6em;
  margin: 4px 2px 8px;
}
.hint { font-size: 0.8rem; color: #999; text-align: center; margin-top: 8px; }

/* ---------- Boards ---------- */
.board-wrap { position: relative; }
.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1;
  /* full width in portrait; limited by screen height in landscape */
  width: 100%;
  width: min(100%, max(280px, calc(100vh - 170px)));
  width: min(100%, max(280px, calc(100dvh - 170px)));
  margin: 0 auto;
  background: #222;
  border-radius: 8px;
  overflow: hidden;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
.square {
  overflow: hidden;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  min-width: 0;
}
.square.light { background: #f0d9b5; }
.square.dark { background: #b58863; }
.square.last { box-shadow: inset 0 0 0 100px rgba(255, 230, 80, 0.28); }
.square.highlight { background: #7fc97f !important; }
.square.selected { box-shadow: inset 0 0 0 3px #ffd700; }
.square.must { box-shadow: inset 0 0 0 3px #ff5a5a; }
.square.move-target::after {
  content: ""; width: 28%; height: 28%; border-radius: 50%;
  background: rgba(20, 90, 20, 0.55); position: absolute;
}
.square.capture-target { box-shadow: inset 0 0 0 4px rgba(200, 30, 30, 0.75); }
.square.in-check { background: radial-gradient(circle, #ff4d4d 30%, #b52020 90%) !important; }

/* Checkers pieces */
.piece {
  width: 76%;
  height: 76%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,0.35), 0 2px 3px rgba(0,0,0,0.4);
}
.piece.red { background: radial-gradient(circle at 35% 35%, #e65a4a, #a82315); }
.piece.black { background: radial-gradient(circle at 35% 35%, #4a5b6e, #1b2530); }
.piece.king::after { content: "♛"; color: #ffd700; font-size: min(5.5vw, 1.6rem); line-height: 1; }

/* Chess pieces */
.chess-piece {
  font-size: min(9.5vw, 2.6rem, 8.5vh);
  line-height: 1;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}
.chess-piece.white { color: #fff; text-shadow: 0 0 2px #000, 0 0 2px #000, 0 1px 2px #000; }
.chess-piece.black { color: #111; text-shadow: 0 0 1px #fff8; }

.promo {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: #1a1a2e; border: 1px solid #555; border-radius: 14px; padding: 12px;
  display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; width: 80%;
  box-shadow: 0 8px 30px rgba(0,0,0,0.6);
}
.promo.hidden { display: none; }
.promo-title { width: 100%; text-align: center; font-weight: 700; }
.promo button.chess-piece { background: #b58863; border: none; border-radius: 10px; width: 56px; height: 56px; font-size: 2.4rem; }
.promo-cancel { width: 100%; min-height: 40px; background: #3a3a6a; color: #fff; border: none; border-radius: 10px; }

.move-list {
  margin-top: 8px; font-size: 0.85rem; color: #bbb;
  white-space: nowrap; overflow-x: auto; padding: 4px 2px;
}

/* ---------- SORRY! ---------- */
.card-display { text-align: center; font-size: 1.1rem; margin: 8px 0; }
.card-options { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-bottom: 10px; }
.card-options.hidden { display: none; }
.card-options button {
  background: #3a3a6a; color: white; border: none; padding: 8px 12px;
  border-radius: 8px; font-size: 0.85rem; cursor: pointer;
}
.sorry-track {
  display: flex; flex-wrap: wrap; gap: 4px; justify-content: center;
  margin: 10px 0; max-height: 280px; overflow-y: auto;
}
.track-space {
  width: 36px; height: 36px; border-radius: 6px; background: #333;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.75rem; position: relative; border: 1px solid #444;
}
.track-space.slide { background: #4a3a1a; }
.track-space.safety { background: #1a3a2a; }
.track-space.home { background: #3a1a1a; }
.pawn { width: 22px; height: 22px; border-radius: 50%; border: 2px solid white; cursor: pointer; }
.pawn.red { background: #c0392b; }
.pawn.blue { background: #2980b9; }
.pawn.yellow { background: #f1c40f; }
.pawn.green { background: #27ae60; }
.player-info { font-size: 0.85rem; color: #bbb; margin-top: 8px; }

@media (max-width: 380px) {
  .game-bar select, .game-bar button { padding: 6px 6px; }
}
.square.jumping { box-shadow: inset 0 0 0 4px #ffd700, 0 0 12px #ffd700; z-index: 1; }
.square.jumping .piece { transform: scale(1.08); }
.square.taken::after { content: "✕"; color: rgba(160, 20, 20, 0.75); font-size: min(6vw, 1.6rem); font-weight: 700; position: absolute; }
.visit-counter { text-align: center; font-size: 0.8rem; color: #8a8aa8; margin-top: 18px; letter-spacing: 0.02em; }
.visit-counter[hidden] { display: none; }

/* Landscape phones: use the width, keep everything reachable */
@media (orientation: landscape) and (max-height: 500px) {
  #app { max-width: 820px; }
  .game-grid { grid-template-columns: repeat(4, 1fr); gap: 10px; }
  .game-card { padding: 14px 8px; }
  .game-icon { font-size: 2rem; margin-bottom: 4px; }
  header { padding: 6px 4px; }
}

/* Scoreboard */
.record-line { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 4px 10px;
  font-size: 0.82rem; color: #c8c8e0; margin: 2px 2px 4px; }
.link-btn { background: none; border: none; color: #8a8aa8; font-size: 0.75rem; text-decoration: underline; padding: 4px 0; cursor: pointer; font-family: inherit; }
.menu-record { margin-top: 18px; padding: 12px; border-radius: 12px; background: #15152a; border: 1px solid #2a2a4a;
  font-size: 0.85rem; color: #c8c8e0; text-align: center; line-height: 1.6; }
.menu-record-title { font-weight: 700; color: #eee; margin-bottom: 2px; }
.menu-record-note { color: #8a8aa8; font-size: 0.78rem; }

/* Home Screen app: never let the header sit under the iOS status bar.
   Safe-area padding normally handles it; this is a fallback if iOS reports no inset. */
html.statusbar-fallback #app { padding-top: max(59px, env(safe-area-inset-top, 0px)) !important; }
@media (display-mode: standalone) and (orientation: portrait) {
  #app { padding-top: max(10px, env(safe-area-inset-top, 0px)); }
}
