/* ------------------------------------------------------------------
   Flip Tiles — HTML5 port of ClickThemTiles.swf (Flash CS4, 2009).

   The board is deliberately untouched: 499 logical px, mint #a4ecdd,
   radial-gradient tiles, and Flash's default 55-degree field of view,
   which on the original 800px stage gives a focal length of 768px --
   hence `perspective: 768px`. The board is scaled as a whole so all
   game maths stays in that 499px space.

   Everything around it is Snackbox chrome: the surround is driven by
   the shared theme variables so the page follows the site's toggle.
   ------------------------------------------------------------------ */

:root {
  /* board — from the SWF, identical in both themes */
  --board:   #a4ecdd;
  --glow:    27, 126, 66;         /* GlowFilter 0x1B7E42 */

  /* Cyan + amber echoes the original's cyan panel and gold button, and
     stays distinguishable on the blue-yellow axis, so it does not rely
     on a red/green distinction. --accent is only ever a fill (dark ink
     sits on it); --accent-ink is the readable text weight. */
  --accent:     #22d3ee;
  --accent-ink: #0e7490;
  --warn:       #b45309;
  --warn-line:  rgba(180, 83, 9, 0.35);

  /* surround */
  --surface:      var(--color-bg-secondary);
  --raised:       #eef1f5;
  --raised-hover: #e3e8ef;
  --line:         var(--color-border);
  --text:         var(--color-text);
  --muted:        var(--color-text-secondary);
  --faint:        #9ca3af;
  --card-shadow:  0 18px 40px -28px rgba(0, 0, 0, 0.4);

  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Consolas, monospace;

  /* 529 board column + 24 gap + 268 HUD — the shell is exactly the game,
     so the title lines up with the board and the HUD edge. */
  --shell: 821px;
}

[data-theme="dark"] {
  --accent-ink:   #22d3ee;
  --warn:         #f5a524;
  --warn-line:    rgba(245, 165, 36, 0.35);

  --surface:      #161d24;
  --raised:       #1d262f;
  --raised-hover: #26313c;
  --line:         #2a353f;
  --faint:        #64748b;
  --card-shadow:  0 18px 40px -24px rgba(0, 0, 0, 0.9);
}

/* the shell is wider than the stock app page */
.app-container {
  max-width: calc(var(--shell) + 3rem);
}

/* -------------------------------------------------------------- game */

.game {
  width: 100%;
  max-width: var(--shell);
  margin: 0 auto;
  display: flex;
  gap: 24px;
  align-items: flex-start;
  justify-content: center;
  text-align: left;
  font-size: 15px;
  line-height: 1.5;
  /* the game is a hover-sweep; never let the browser select through it */
  -webkit-user-select: none;
  user-select: none;
}

/* The column carries the definite width: 499 board + 28 card padding + 2
   border. Everything inside is a plain 100% of it, so nothing has to
   measure its own contents to know how wide it is. */
.board-col {
  flex: 0 1 auto;
  width: min(529px, 100%, calc(100vh - 300px));
  display: flex;
  flex-direction: column;
  gap: 16px;
  align-items: center;
}

/* Card behind the board. It must NOT clip: tiles genuinely overhang
   their cells while rotating, and the original let them spill too. */
.board-card {
  width: 100%;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--card-shadow);
  /* a sweep across the board must not pan the page */
  touch-action: none;
}

/* #board is out of flow inside this, so there is no feedback loop between
   the board's scale and the space available to it. */
.board-slot {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
}

#board {
  position: absolute;
  top: 0;
  left: 0;
  width: 499px;
  height: 499px;
  transform-origin: 0 0;
  background: var(--board);
  transition: box-shadow 40ms linear;
}

/* Flash put the vanishing point at the centre of the board; CSS
   perspective on the tile container reproduces that exactly. */
#tileHolder {
  position: absolute;
  inset: 0;
  perspective: 768px;
  perspective-origin: 50% 50%;
  transform-style: preserve-3d;
}

.tile {
  position: absolute;
  left: 0;
  top: 0;
  /* hit-testing is grid arithmetic, never tile geometry */
  pointer-events: none;
  background: radial-gradient(ellipse 145% 98% at 27% 22%,
                              #ff0022 0%, #ffffff 100%);
  backface-visibility: visible;
  will-change: transform, opacity;
}

.tile.black { background: #000; }

#errorHolder {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.cross { position: absolute; overflow: visible; }

.cross line {
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
}

/* Purely decorative veil while the level is parked. It sits on the mint
   board, not the page, so it stays a dark veil in either theme.
   pointer-events:none keeps it entirely out of the game's hit-testing. */
#scrim {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: rgba(14, 18, 22, 0.38);
  backdrop-filter: blur(1px);
  transition: opacity 180ms ease;
}

#scrim[hidden] { display: block; opacity: 0; }

/* ---------------------------------------------------------- the CTA */

.begin-slot {
  min-height: 52px;
  display: grid;
  place-items: center;
  width: 100%;
}

#beginNewLevel {
  appearance: none;
  font: inherit;
  font-weight: 550;
  color: #06222a;
  background: var(--accent);
  border: 0;
  border-radius: 999px;
  padding: 12px 26px;
  cursor: pointer;
  box-shadow: 0 0 0 0 rgba(34, 211, 238, 0.5);
  animation: breathe 2.4s ease-in-out infinite;
  transition: transform 120ms ease, filter 120ms ease;
}

#beginNewLevel:hover {
  background: var(--accent);
  filter: brightness(1.08);
  transform: translateY(-1px);
}

#beginNewLevel[hidden] { display: none; }

@keyframes breathe {
  0%, 100% { box-shadow: 0 0 0 0 rgba(34, 211, 238, 0.45); }
  50%      { box-shadow: 0 0 0 12px rgba(34, 211, 238, 0); }
}

@media (prefers-reduced-motion: reduce) {
  #beginNewLevel { animation: none; }
}

/* --------------------------------------------------------------- HUD */

.hud {
  flex: 0 0 268px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.hud-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.card-head h2 {
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--faint);
}

#btnNewGame {
  appearance: none;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 11px;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}

#btnNewGame:hover { background: var(--raised-hover); border-color: var(--muted); }

/* level */
.level-line {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 10px;
}

.level-line .n {
  font-family: var(--mono);
  font-size: 26px;
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.level-line .of { color: var(--faint); font-size: 13px; }

.track {
  height: 5px;
  border-radius: 99px;
  background: var(--raised);
  overflow: hidden;
}

.track > i {
  display: block;
  height: 100%;
  width: calc(var(--p, 0) * 100%);
  background: var(--accent);
  border-radius: 99px;
  transition: width 260ms ease;
}

/* stats */
.stats { display: grid; gap: 2px; }

.stat {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  padding: 7px 0;
}

.stat + .stat { border-top: 1px solid var(--line); }

.stat dt {
  font-size: 13px;
  color: var(--muted);
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.stat dt .tag {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--warn);
  border: 1px solid var(--warn-line);
  border-radius: 4px;
  padding: 1px 4px;
}

.stat dd {
  margin: 0;
  font-family: var(--mono);
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

/* the number that matters most this level */
.stat--key { padding: 11px 0 9px; }
.stat--key dt { color: var(--text); font-weight: 550; }
.stat--key dd { font-size: 22px; font-weight: 600; color: var(--accent-ink); }

.best {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.best span { font-size: 13px; color: var(--muted); }

.best b {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* how to play */
.howto { font-size: 13px; line-height: 1.45; color: var(--muted); }
.howto ul { margin: 0; padding-left: 17px; display: grid; gap: 5px; }
.howto li::marker { color: var(--faint); }
.howto b { color: var(--text); font-weight: 550; }

.swatches {
  display: flex;
  gap: 14px;
  margin-bottom: 12px;
  font-size: 12px;
  color: var(--muted);
}

.swatch { display: flex; align-items: center; gap: 7px; }

.swatch i {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  flex: none;
  border: 1px solid var(--line);
}

/* Composited over the board colour at the same alphas the game uses
   (0.2 unflipped, 1 flipped, 0.9 black), so the legend matches the
   board rather than the page behind it. */
.swatch .pale {
  background: radial-gradient(ellipse 145% 98% at 27% 22%,
                              rgba(255, 0, 34, 0.2) 0%,
                              rgba(255, 255, 255, 0.2) 100%),
              var(--board);
}

.swatch .solid {
  background: radial-gradient(ellipse 145% 98% at 27% 22%,
                              #ff0022 0%, #ffffff 100%);
}

.swatch .dark { background: rgba(0, 0, 0, 0.9); }

/* --------------------------------------------------------- narrow */

@media (max-width: 860px) {
  .game { flex-direction: column; align-items: center; }
  .hud { flex: 1 1 auto; width: min(529px, 100%); }
  /* stacked, so the board may use the full width but less of the height */
  .board-col { width: min(529px, 100%, calc(100vh - 250px)); }
}
