:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef0f6;
  --text: #1c1f2a;
  --muted: #5b6275;
  --border: #dde1ea;
  --accent: #4f46e5;
  --accent-text: #ffffff;
  --good: #16a34a;
  --warn: #ca8a04;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 2px 8px rgba(16, 24, 40, .06);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11131a;
    --surface: #1a1d27;
    --surface-2: #242836;
    --text: #e8eaf0;
    --muted: #9aa1b4;
    --border: #2e3343;
    --accent: #818cf8;
    --accent-text: #11131a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
a { color: var(--accent); }
h1, h2 { line-height: 1.2; }

.site-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 1.15rem; color: var(--text); text-decoration: none; }
.brand-mark { color: var(--accent); }
.site-header nav a { margin-left: 16px; color: var(--muted); text-decoration: none; }
.site-header nav a:hover { color: var(--text); }

.container { max-width: 1120px; margin: 0 auto; padding: 16px; }
.hero { text-align: center; padding: 24px 0 8px; }
.hero h1 { font-size: clamp(1.5rem, 4vw, 2.25rem); margin: 0 0 8px; }
.hero p { color: var(--muted); margin: 0; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0; }
.chip {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  border-radius: 999px; padding: 6px 14px; font: inherit; cursor: pointer;
}
.chip.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }

.game-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 16px; }
.game-grid.compact { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.game-card {
  display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; align-items: center;
  padding: 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  color: var(--text); text-decoration: none; box-shadow: var(--shadow); transition: transform .12s, border-color .12s;
}
.game-card:hover { transform: translateY(-2px); border-color: var(--accent); }
.game-card[hidden] { display: none; }
.game-icon { font-size: 2rem; grid-row: span 2; }
.game-meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.game-blurb { grid-column: 1 / -1; color: var(--muted); font-size: .9rem; }
.compact .game-blurb { display: none; }
.compact .game-icon { font-size: 1.5rem; grid-row: auto; }
.tag { font-size: .75rem; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 2px 8px; }

/* Ads: reserve space so slots never shift the layout when they load. */
.ad-slot { margin: 16px auto; width: 100%; overflow: hidden; }
.ad-top, .ad-bottom { min-height: 90px; max-width: 970px; }
.ad-side { min-height: 600px; width: 300px; position: sticky; top: 16px; margin: 0; }
.ad-placeholder {
  display: flex; align-items: center; justify-content: center;
  border: 1px dashed var(--border); border-radius: 8px; color: var(--muted);
  font-size: .75rem; letter-spacing: .08em; text-transform: uppercase; background: var(--surface-2);
}

.game-layout { display: grid; grid-template-columns: 1fr 300px; gap: 24px; align-items: start; }
@media (max-width: 900px) {
  .game-layout { grid-template-columns: 1fr; }
  .game-side { display: none; }
}
.game-main h1 { font-size: 1.5rem; margin: 0 0 12px; }
.game-root {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
  min-height: 420px; touch-action: manipulation; user-select: none; -webkit-user-select: none;
}
.how-to, .more-games { margin-top: 24px; }
.how-to ol { padding-left: 1.25rem; color: var(--muted); }
.prose { max-width: 720px; }
.site-footer { text-align: center; color: var(--muted); font-size: .85rem; padding: 32px 16px; }
.site-footer p { margin: 4px 0; }

/* Shared game UI */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
.stat { background: var(--surface-2); border-radius: 8px; padding: 4px 10px; font-size: .9rem; }
.stat b { font-variant-numeric: tabular-nums; }
.btn {
  font: inherit; font-size: .9rem; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary, .btn.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
select.btn { padding-right: 8px; }
.row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.msg { min-height: 1.5em; text-align: center; color: var(--muted); margin: 8px 0 0; }

.overlay {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .45); border-radius: var(--radius); z-index: 1000;
}
.overlay-card { background: var(--surface); padding: 24px; border-radius: var(--radius); text-align: center; max-width: 320px; margin: 16px; }
.overlay-card h2 { margin: 0 0 8px; }
.overlay-card p { color: var(--muted); margin: 0 0 16px; }

/* 2048 */
.g2048-board {
  --gap: 10px; position: relative; display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--gap);
  background: #bbada0; padding: var(--gap); border-radius: 10px; width: min(100%, 420px); aspect-ratio: 1; margin: 0 auto;
}
.g2048-cell {
  display: flex; align-items: center; justify-content: center; border-radius: 6px; background: rgba(238, 228, 218, .35);
  font-weight: 800; font-size: clamp(1.1rem, 6vw, 2rem); color: #776e65;
}
.g2048-cell.pop { animation: pop .15s ease-out; }
@keyframes pop { from { transform: scale(.85); } to { transform: scale(1); } }
.v2 { background: #eee4da; } .v4 { background: #ede0c8; }
.v8 { background: #f2b179; color: #fff; } .v16 { background: #f59563; color: #fff; }
.v32 { background: #f67c5f; color: #fff; } .v64 { background: #f65e3b; color: #fff; }
.v128 { background: #edcf72; color: #fff; } .v256 { background: #edcc61; color: #fff; }
.v512 { background: #edc850; color: #fff; } .v1024 { background: #edc53f; color: #fff; font-size: clamp(.9rem, 5vw, 1.6rem); }
.v2048, .vbig { background: #edc22e; color: #fff; font-size: clamp(.9rem, 5vw, 1.6rem); }
.vbig { background: #3c3a32; }

/* Snake */
.snake-canvas { display: block; margin: 0 auto; width: min(100%, 440px); aspect-ratio: 1; border-radius: 8px; background: #0f172a; }
.dpad { display: grid; grid-template-columns: repeat(3, 56px); gap: 6px; justify-content: center; margin-top: 12px; }
.dpad .btn { height: 48px; font-size: 1.2rem; }
@media (hover: hover) and (pointer: fine) { .dpad { display: none; } }

/* Minesweeper */
.ms-wrap { overflow-x: auto; }
.ms-board { display: grid; gap: 2px; margin: 0 auto; width: max-content; }
.ms-cell {
  width: var(--cell, 30px); height: var(--cell, 30px); display: flex; align-items: center; justify-content: center;
  font-weight: 800; font-size: calc(var(--cell, 30px) * .55); border-radius: 4px; cursor: pointer;
  background: #94a3b8; color: #fff; border: 0; padding: 0;
}
.ms-cell:hover { filter: brightness(1.08); }
.ms-cell.open { background: var(--surface-2); cursor: default; filter: none; }
.ms-cell.boom { background: var(--bad); }
.ms-cell.n1 { color: #2563eb; } .ms-cell.n2 { color: #16a34a; } .ms-cell.n3 { color: #dc2626; }
.ms-cell.n4 { color: #7c3aed; } .ms-cell.n5 { color: #b45309; } .ms-cell.n6 { color: #0891b2; }
.ms-cell.n7 { color: #111827; } .ms-cell.n8 { color: #6b7280; }

/* Sudoku */
.sd-board {
  display: grid; grid-template-columns: repeat(9, 1fr); width: min(100%, 450px); aspect-ratio: 1; margin: 0 auto;
  border: 2px solid var(--text); background: var(--border); gap: 1px;
}
.sd-cell {
  position: relative; display: flex; align-items: center; justify-content: center; background: var(--surface);
  font-size: clamp(1rem, 5vw, 1.6rem); cursor: pointer; color: var(--accent); font-weight: 600;
}
.sd-cell.given { color: var(--text); font-weight: 700; }
.sd-cell.peer { background: var(--surface-2); }
.sd-cell.same { background: color-mix(in srgb, var(--accent) 22%, var(--surface)); }
.sd-cell.sel { background: color-mix(in srgb, var(--accent) 38%, var(--surface)); }
.sd-cell.bad { color: var(--bad); }
.sd-cell.br { border-right: 2px solid var(--text); }
.sd-cell.bb { border-bottom: 2px solid var(--text); }
.sd-notes { position: absolute; inset: 2px; display: grid; grid-template-columns: repeat(3, 1fr); font-size: clamp(.45rem, 1.8vw, .65rem); color: var(--muted); font-weight: 400; }
.sd-notes span { display: flex; align-items: center; justify-content: center; }
.numpad { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; width: min(100%, 450px); margin: 12px auto 0; }
.numpad .btn { padding: 10px 0; font-size: 1.1rem; }

/* Word guess */
.wg-grid { display: grid; grid-template-rows: repeat(6, 1fr); gap: 6px; width: min(100%, 320px); margin: 0 auto; }
.wg-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.wg-row.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.wg-tile {
  aspect-ratio: 1; display: flex; align-items: center; justify-content: center; border: 2px solid var(--border);
  font-weight: 800; font-size: 1.6rem; text-transform: uppercase; border-radius: 4px;
}
.wg-tile.filled { border-color: var(--muted); }
.wg-tile.correct, .wg-key.correct { background: #16a34a; border-color: #16a34a; color: #fff; }
.wg-tile.present, .wg-key.present { background: #ca8a04; border-color: #ca8a04; color: #fff; }
.wg-tile.absent, .wg-key.absent { background: #6b7280; border-color: #6b7280; color: #fff; }
.wg-keys { display: grid; gap: 6px; margin: 16px auto 0; max-width: 500px; }
.wg-keyrow { display: flex; gap: 5px; justify-content: center; }
.wg-key {
  flex: 1; max-width: 44px; height: 54px; border: 0; border-radius: 6px; background: var(--surface-2); color: var(--text);
  font: inherit; font-weight: 700; text-transform: uppercase; cursor: pointer; padding: 0;
}
.wg-key.wide { max-width: 68px; flex: 1.5; font-size: .75rem; }

/* Four groups */
.fg-solved, .fg-grid { width: min(100%, 560px); margin: 0 auto; }
.fg-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.fg-word {
  aspect-ratio: 1.6; display: flex; align-items: center; justify-content: center; text-align: center;
  background: var(--surface-2); border: 0; border-radius: 8px; font: inherit; font-weight: 700;
  font-size: clamp(.65rem, 2.6vw, .95rem); text-transform: uppercase; cursor: pointer; padding: 4px; color: var(--text);
  overflow-wrap: anywhere;
}
.fg-word.sel { background: #4b5563; color: #fff; }
.fg-group { border-radius: 8px; padding: 12px; text-align: center; margin-bottom: 8px; color: #111; }
.fg-group strong { display: block; text-transform: uppercase; }
.fg-group span { text-transform: uppercase; font-size: .9rem; }
.fg-0 { background: #f9df6d; } .fg-1 { background: #a0c35a; } .fg-2 { background: #b0c4ef; } .fg-3 { background: #ba81c5; }
.fg-dots { display: flex; gap: 6px; justify-content: center; margin: 12px 0; align-items: center; color: var(--muted); }
.fg-dots i { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); display: inline-block; }

/* Connect four */
.c4-board {
  display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; background: #1d4ed8; padding: 8px;
  border-radius: 12px; width: min(100%, 480px); margin: 0 auto; cursor: pointer;
}
.c4-cell { aspect-ratio: 1; border-radius: 50%; background: var(--surface); }
.c4-cell.p1 { background: #ef4444; } .c4-cell.p2 { background: #facc15; }
.c4-cell.win { box-shadow: inset 0 0 0 4px #fff; }
.c4-cell.drop { animation: drop .25s ease-in; }
@keyframes drop { from { transform: translateY(-200%); } to { transform: translateY(0); } }

/* Solitaire */
.game-root { container-type: inline-size; }
.sol { --cw: min(calc((100cqw - 20px) / 7.8), 76px); --ch: calc(var(--cw) * 1.4); --fan: calc(var(--ch) * .24); background: #166534; border-radius: 10px; padding: 10px; }
.sol-row { display: grid; grid-template-columns: repeat(7, var(--cw)); gap: calc(var(--cw) * .12); justify-content: center; }
.sol-row + .sol-row { margin-top: 14px; }
.sol-pile { position: relative; width: var(--cw); height: var(--ch); border-radius: 6px; border: 2px solid rgba(255, 255, 255, .25); }
.sol-tab { min-height: calc(var(--ch) * 3.2); border-color: transparent; }
.sol-tab.empty-pile { border-color: rgba(255, 255, 255, .25); height: var(--ch); min-height: 0; }
.sol-card {
  position: absolute; left: -2px; width: var(--cw); height: var(--ch); border-radius: 6px; background: #fff; color: #111;
  border: 1px solid #cbd5e1; font-weight: 700; font-size: calc(var(--cw) * .26); line-height: 1; padding: 3px 4px; cursor: pointer;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
}
.sol-card .big { position: absolute; right: 4px; bottom: 3px; font-size: calc(var(--cw) * .42); }
.sol-card.red { color: #dc2626; }
.sol-card.back { background: repeating-linear-gradient(45deg, #1e40af 0 6px, #2563eb 6px 12px); border-color: #1e3a8a; }
.sol-card.sel { outline: 3px solid #facc15; outline-offset: -1px; }
.sol-pile .label { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: rgba(255, 255, 255, .35); font-size: calc(var(--cw) * .4); }

/* Mahjong */
.mj-wrap { overflow: hidden; display: flex; justify-content: center; }
.mj-board { position: relative; margin: 8px auto; }
.mj-tile {
  position: absolute; width: var(--tw); height: var(--th); border-radius: 5px; background: #fffdf5; border: 1px solid #b8a984;
  box-shadow: 3px 3px 0 #d6c9a6, 4px 4px 0 #9c8f6c; display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; font-weight: 700; line-height: 1; color: #111;
}
.mj-tile .rank { font-size: calc(var(--tw) * .5); }
.mj-tile .suit { font-size: calc(var(--tw) * .32); margin-top: 2px; }
.mj-tile.honor .rank { font-size: calc(var(--tw) * .62); }
.mj-tile.blocked { filter: brightness(.82); cursor: default; }
.mj-tile.sel { background: #fde68a; }
.mj-tile.hint { background: #bbf7d0; }
.mj-dots { color: #1d4ed8; } .mj-bam { color: #15803d; } .mj-chr { color: #b91c1c; } .mj-wind { color: #111827; } .mj-drg-r { color: #b91c1c; } .mj-drg-g { color: #15803d; } .mj-drg-w { color: #1d4ed8; }
