/* ==========================================================================
   Connect Four — board, discs and the drop animation.

   The board is drawn the way the physical toy is built: a coloured front
   plate with holes cut through it, and the discs sitting behind it. That is
   what the mask on .c4-grid does — one plate element, 42 transparent holes —
   so a disc genuinely appears *through* the board instead of on top of it,
   and there is no per-cell border to line up.
   ========================================================================== */

/* ------------------------------------------------------------ palettes --
   Six schemes. The discs, the turn dots, the landing ghost and the board all
   read from these four variables, so a scheme is four colours and nothing
   else has to know about it. "contrast" is the deuteranopia-safe pair —
   red and yellow are the one combination a red-green colour blind player
   cannot reliably separate, and this game is nothing but telling two colours
   apart. */
[data-connect-four] {
  --c4-p1: #f87171; --c4-p1-deep: #b91c1c;
  --c4-p2: #fde68a; --c4-p2-deep: #d97706;
  --c4-board: #2563eb; --c4-board-deep: #1e3a8a;
}
[data-connect-four][data-discs="contrast"] {
  --c4-p1: #60a5fa; --c4-p1-deep: #1d4ed8;
  --c4-p2: #fb923c; --c4-p2-deep: #c2410c;
  --c4-board: #334155; --c4-board-deep: #0f172a;
}
[data-connect-four][data-discs="ocean"] {
  --c4-p1: #22d3ee; --c4-p1-deep: #0e7490;
  --c4-p2: #f472b6; --c4-p2-deep: #be185d;
  --c4-board: #1e293b; --c4-board-deep: #0b1220;
}
[data-connect-four][data-discs="forest"] {
  --c4-p1: #a3e635; --c4-p1-deep: #4d7c0f;
  --c4-p2: #fbbf24; --c4-p2-deep: #b45309;
  --c4-board: #166534; --c4-board-deep: #052e16;
}
[data-connect-four][data-discs="sunset"] {
  --c4-p1: #fb7185; --c4-p1-deep: #9f1239;
  --c4-p2: #fcd34d; --c4-p2-deep: #b45309;
  --c4-board: #6d28d9; --c4-board-deep: #3b0764;
}
[data-connect-four][data-discs="mono"] {
  --c4-p1: #f8fafc; --c4-p1-deep: #94a3b8;
  --c4-p2: #475569; --c4-p2-deep: #0f172a;
  --c4-board: #64748b; --c4-board-deep: #334155;
}

.c4-stage { display: grid; justify-items: center; gap: 0.9rem; }

.c4-turn {
  display: flex; align-items: center; justify-content: center; gap: 0.55rem;
  min-height: 2.2rem; font-weight: 800; font-size: 1rem; text-align: center;
}
.c4-turn .c4-dot {
  width: 20px; height: 20px; border-radius: 50%; flex: 0 0 auto;
  box-shadow: inset 0 -2px 4px rgba(0, 0, 0, 0.28), inset 0 2px 3px rgba(255, 255, 255, 0.45);
}
.c4-dot.is-red { background: radial-gradient(circle at 34% 30%, var(--c4-p1), var(--c4-p1-deep)); }
.c4-dot.is-yellow { background: radial-gradient(circle at 34% 30%, var(--c4-p2), var(--c4-p2-deep)); }
.c4-turn[data-state="thinking"] .c4-dot { animation: c4-pulse 0.9s ease-in-out infinite; }
@keyframes c4-pulse { 50% { transform: scale(0.82); opacity: 0.6; } }

/* ------------------------------------------------------------- board ---- */
.c4-board {
  /* --c4-gap is a length, not a percentage. A percentage gap on the grid and
     the same percentage on the button overlay resolve against different boxes,
     which is exactly how the two drifted apart. */
  --c4-gap: clamp(4px, 1.5vw, 12px);
  position: relative; width: 100%; max-width: 540px;
  border-radius: 20px; padding: clamp(8px, 2.4vw, 16px);
  background: linear-gradient(160deg, var(--c4-board), var(--c4-board-deep));
  box-shadow: 0 26px 60px -34px var(--c4-board-deep), inset 0 2px 0 rgba(255, 255, 255, 0.25);
  touch-action: manipulation;
}
/* No aspect-ratio here: the height is whatever six rows of square cells plus
   five gaps come to. Fixing it to 7/6 made the rows overflow the box. */
.c4-grid {
  position: relative; display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: var(--c4-gap);
}
.c4-cell { position: relative; aspect-ratio: 1; }
/* the hole in the front plate */
.c4-cell::after {
  content: ''; position: absolute; inset: 0; border-radius: 50%;
  background: var(--gm-tint-1);
  box-shadow: inset 0 3px 7px rgba(15, 23, 42, 0.35), inset 0 -2px 4px rgba(255, 255, 255, 0.5);
}
[data-theme="dark"] .c4-cell::after { background: #0b1220; box-shadow: inset 0 3px 8px rgba(0, 0, 0, 0.7); }

.c4-disc {
  position: absolute; inset: 0; border-radius: 50%; z-index: 2;
  transform: scale(0);
  box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3), inset 0 3px 5px rgba(255, 255, 255, 0.42),
    0 3px 8px rgba(15, 23, 42, 0.28);
}
.c4-cell[data-player="1"] .c4-disc { transform: scale(1); background: radial-gradient(circle at 34% 30%, var(--c4-p1), var(--c4-p1-deep)); }
.c4-cell[data-player="2"] .c4-disc { transform: scale(1); background: radial-gradient(circle at 34% 30%, var(--c4-p2), var(--c4-p2-deep)); }

/* The disc falls from above the board; the distance is the number of rows it
   passed on the way down, handed over as --drop by the script. */
.c4-cell.is-dropping .c4-disc { animation: c4-drop 0.34s cubic-bezier(0.36, 0.05, 0.3, 1.2); }
@keyframes c4-drop {
  0% { transform: translateY(calc(var(--drop, 3) * -118%)) scale(1); }
  100% { transform: translateY(0) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
  .c4-cell.is-dropping .c4-disc { animation: none; }
}

/* the four that won, with everything else stepped back */
.c4-board.is-over .c4-cell:not(.is-win) .c4-disc { opacity: 0.42; }
.c4-cell.is-win .c4-disc { animation: c4-win 1.1s ease-in-out infinite; z-index: 3; }
@keyframes c4-win {
  0%, 100% { box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3), 0 0 0 0 rgba(255, 255, 255, 0.9); }
  50% { box-shadow: inset 0 -3px 6px rgba(0, 0, 0, 0.3), 0 0 0 6px rgba(255, 255, 255, 0.42); }
}

/* --------------------------------------------------------- the columns -- */
/* One button per column, the full height of the board, so a tap anywhere in
   a column drops there — and so keyboard and screen-reader users get seven
   real, labelled controls rather than 42 cells to wade through. */
/* Anchored to the grid, not the board, with the same track definition and the
   same gap variable — so the seven buttons are the seven columns, exactly. */
.c4-columns { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(7, 1fr); gap: var(--c4-gap); z-index: 4; }
.c4-col {
  border: 0; padding: 0; background: transparent; cursor: pointer; border-radius: 12px;
  transition: background 0.15s ease;
}
.c4-col:disabled { cursor: not-allowed; }
.c4-col:hover:not(:disabled), .c4-col:focus-visible { background: rgba(255, 255, 255, 0.16); }
.c4-col:focus-visible { outline: 3px solid #fff; outline-offset: -3px; }

/* the ghost disc showing where this drop would land */
.c4-ghost {
  position: absolute; inset: 0; border-radius: 50%; z-index: 1; opacity: 0;
  transition: opacity 0.12s ease;
  border: 2px dashed rgba(255, 255, 255, 0.85);
}
.c4-cell.is-landing .c4-ghost { opacity: 0.9; }
/* the ghost tints itself from whichever scheme is active */
.c4-cell.is-landing.is-red .c4-ghost { background: color-mix(in srgb, var(--c4-p1) 38%, transparent); }
.c4-cell.is-landing.is-yellow .c4-ghost { background: color-mix(in srgb, var(--c4-p2) 42%, transparent); }

/* ------------------------------------------------------------ scoreboard */
.c4-score { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.c4-score div {
  text-align: center; padding: 0.6rem 0.3rem; border-radius: 14px;
  border: 1px solid var(--border); background: var(--surface-strong);
}
.c4-score b { display: block; font-size: 1.35rem; font-weight: 900; line-height: 1.1; }
.c4-score span { font-size: 0.72rem; font-weight: 700; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }
.c4-streak { margin: 0.7rem 0 0; font-size: 0.85rem; color: var(--text-muted); text-align: center; }

.c4-result {
  margin: 0; padding: 0.75rem 0.9rem; border-radius: 14px; font-weight: 700; text-align: center;
  border: 1px solid var(--gm-a-soft); background: var(--gm-tint-1);
}
[data-theme="dark"] .c4-result { background: rgba(30, 41, 59, 0.6); border-color: rgba(148, 163, 184, 0.2); }
.c4-result[data-tone="win"] { border-color: rgba(34, 197, 94, 0.5); background: rgba(34, 197, 94, 0.12); }
.c4-result[data-tone="loss"] { border-color: rgba(239, 68, 68, 0.45); background: rgba(239, 68, 68, 0.1); }
.c4-result[data-tone="draw"] { border-color: rgba(245, 158, 11, 0.5); background: rgba(245, 158, 11, 0.12); }

.c4-hint { color: var(--text-muted); font-size: 0.82rem; text-align: center; margin: 0; }
.c4-controls { display: flex; flex-wrap: wrap; gap: 0.45rem; justify-content: center; }

/* confetti rides above the board without changing its layout */
.c4-stage { position: relative; }
.c4-stage .confetti-layer { position: absolute; inset: 0; pointer-events: none; overflow: hidden; z-index: 6; }

/* ---------------------------------------------------------- small screens */
@media (max-width: 560px) {
  .c4-turn { font-size: 0.92rem; }
  .c4-turn .c4-dot { width: 17px; height: 17px; }
  .c4-controls .gm-btn { flex: 1 1 auto; justify-content: center; }
  .c4-hint { font-size: 0.78rem; }
  .c4-score b { font-size: 1.15rem; }
  .c4-result { font-size: 0.9rem; }
}

/* a row of swatches instead of a colour name nobody can picture */
.c4-schemes { display: grid; grid-template-columns: repeat(6, 1fr); gap: 0.4rem; }
.c4-scheme {
  width: 100%; height: 30px; padding: 0; cursor: pointer;
  border-radius: 10px; border: 2px solid var(--border);
  display: grid; grid-template-columns: 1fr 1fr; overflow: hidden;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.c4-scheme:hover { transform: translateY(-1px); }
.c4-scheme.is-active { border-color: var(--gm-a); box-shadow: 0 0 0 2px var(--gm-a-soft); }
.c4-scheme span { display: block; }
.c4-scheme .a { background: linear-gradient(140deg, var(--sw-1), var(--sw-1-deep)); }
.c4-scheme .b { background: linear-gradient(140deg, var(--sw-2), var(--sw-2-deep)); }
