:root {
  color-scheme: light dark;
  --gap: 3px;
  --tray-pad: 8px;

  --page: #f3f4f7;
  --ink: #1d2029;
  --tray: #dde1e9;
  --tray-edge: #c9cfdb;
  --tile: #fbfcfe;
  --tile-top: #ffffff;
  --tile-shade: #aeb6c5;
  --tile-hover: #ffffff;
  --dug: #e9ecf1;
  --dug-edge: #d3d8e1;
  --flag: #fde6dc;
  --blast: #e0795a;
  --mine: #f1d5cc;
  --focus: #f08a3c;
  --n1: #1f6fd1; --n2: #2e8540; --n3: #d13b2e; --n4: #5b3fb5;
  --n5: #b35c12; --n6: #11807a; --n7: #1d2029; --n8: #5d6472;
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Matches the Fox Games site (frontend/public/css/site.css). */
    --page: #14161c;
    --ink: #e8e9ee;
    --tray: #1f232c;
    --tray-edge: #2d323e;
    --tile: #3b4354;
    --tile-top: #4a5367;
    --tile-shade: #171a21;
    --tile-hover: #465066;
    --dug: #262b35;
    --dug-edge: #20242d;
    --flag: #4a3a38;
    --mine: #4a3534;
    --n1: #6cb2ff; --n2: #7fd48a; --n3: #ff8f80; --n4: #b69cff;
    --n5: #f0a35a; --n6: #5fd3c9; --n7: #e8e9ee; --n8: #9aa0ad;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  font-family: system-ui, sans-serif;
  margin: 0;
  padding: 1rem;
  display: flex;
  justify-content: center;
  touch-action: manipulation;
  background: var(--page);
  color: var(--ink);
  /* 100cqw on the board = the body's content width (viewport minus padding
     and any scrollbar). */
  container-type: inline-size;
}

/* As wide as the board (at least 640px when the screen allows), so the HUD
   and leaderboards line up with it; never wider than the screen. */
.layout {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  width: fit-content;
  min-width: min(100%, 640px);
  max-width: 100%;
}

.hud {
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-template-areas: "mines difficulty time";
  align-items: center;
  width: 100%;
  gap: 0.5rem;
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 4rem;
}

.hud-item:first-child { grid-area: mines; }
.hud-item:last-child { grid-area: time; }

.difficulty-select {
  grid-area: difficulty;
  justify-self: center;
  display: flex;
  gap: var(--gap);
  padding: 4px;
  background: var(--tray);
  border: 1px solid var(--tray-edge);
  border-radius: 10px;
}

/* Same language as the board: levels are raised tiles, the chosen one is
   pressed into the tray like a revealed cell. */
.difficulty-select button {
  font: 600 0.875rem / 1 system-ui, sans-serif;
  padding: 0.5rem 0.75rem;
  border: 0;
  border-radius: 6px;
  color: var(--ink);
  background: var(--tile);
  box-shadow: inset 0 2px 0 var(--tile-top), 0 2px 0 var(--tile-shade);
  cursor: pointer;
}

.difficulty-select button:hover { background: var(--tile-hover); }

.difficulty-select button:active {
  transform: translateY(1px);
  box-shadow: inset 0 1px 0 var(--tile-top), 0 1px 0 var(--tile-shade);
}

.difficulty-select button.active {
  font-weight: 800;
  background: var(--dug);
  box-shadow: inset 0 1px 2px var(--dug-edge);
  transform: none;
  cursor: default;
}

.difficulty-select button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

/* Too narrow for one row (phones): counters on top, levels below. */
@container (max-width: 26rem) {
  .hud {
    grid-template-areas:
      "mines . time"
      "difficulty difficulty difficulty";
  }
}

.board-wrapper {
  position: relative;
  max-width: 100%;
  display: flex;
  justify-content: flex-start;
  overflow-x: auto;
}

.board {
  display: grid;
  gap: var(--gap);
  padding: var(--tray-pad);
  background: var(--tray);
  border: 1px solid var(--tray-edge);
  border-radius: 10px;
  /* Swipes scroll (the page, or an Expert board wider than a phone); taps
     and long-presses still reach input.js, which drops a press once the
     finger moves or the browser takes over the gesture (pointercancel).
     Pinch and double-tap zoom stay off. */
  touch-action: pan-x pan-y;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  /* Cells grow to fill the screen: as big as the width allows and small
     enough that the board fits the height left under the account bar and
     HUD (~9rem), capped at 48px. Floored at a tappable 20px, below which the
     board scrolls sideways (e.g. Expert on a phone).
     Board size = n * cellSize + (n - 1) * gap + 2 * (tray padding + border). */
  --cell-size: clamp(
    20px,
    min(
      (100cqw - 2 * (var(--tray-pad) + 1px) - (var(--cols, 9) - 1) * var(--gap)) / var(--cols, 9),
      (100dvh - 10rem - (var(--rows, 9) - 1) * var(--gap)) / var(--rows, 9)
    ),
    48px
  );
}

/* Hidden cells are raised tiles; revealing one digs it into the tray. */
.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  padding: 0;
  border: 0;
  border-radius: calc(var(--cell-size) * 0.14);
  font: 800 calc(var(--cell-size) * 0.55) / var(--cell-size) system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
  text-align: center;
  color: var(--ink);
  background: var(--tile);
  box-shadow:
    inset 0 2px 0 var(--tile-top),
    0 2px 0 var(--tile-shade);
  cursor: pointer;
}

.cell.hidden:hover { background: var(--tile-hover); }

.cell.hidden:active {
  transform: translateY(1px);
  box-shadow: inset 0 1px 0 var(--tile-top), 0 1px 0 var(--tile-shade);
}

.cell:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.cell.revealed {
  background: var(--dug);
  box-shadow: inset 0 1px 2px var(--dug-edge);
  cursor: default;
}

.cell.flagged {
  background: var(--flag);
  font-size: calc(var(--cell-size) * 0.5);
}

.cell.mine {
  background: var(--mine);
  box-shadow: none;
  font-size: calc(var(--cell-size) * 0.5);
}

.cell.exploded { background: var(--blast); }

.n1 { color: var(--n1); }
.n2 { color: var(--n2); }
.n3 { color: var(--n3); }
.n4 { color: var(--n4); }
.n5 { color: var(--n5); }
.n6 { color: var(--n6); }
.n7 { color: var(--n7); }
.n8 { color: var(--n8); }

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  text-align: center;
  padding: 1rem;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  width: 100%;
  min-height: 1.75rem;
}

.home-link {
  color: inherit;
  font-weight: 600;
  text-decoration: none;
}

.home-link:hover { text-decoration: underline; }

.home-link:focus-visible,
.account a:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 2px;
}

.account {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.overlay .save-note { margin: 0; }
.overlay .sign-in-link { color: #9cf; font-weight: bold; }

.leaderboards {
  width: 100%;
}

.leaderboards h2 { margin: 0 0 0.5rem; }

.leaderboard-columns {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
  gap: 1rem;
}

.leaderboard h3 { margin: 0 0 0.25rem; font-size: 1rem; }

.leaderboard ol {
  margin: 0;
  padding-left: 1.75rem;
}

.leaderboard li span + span {
  float: right;
  font-variant-numeric: tabular-nums;
}

.leaderboard li.highlight { font-weight: bold; }

.muted { opacity: 0.7; margin: 0; }
