:root {
  --bg: #f3efe7;
  --ink: #3b352d;
  --ink-soft: #7d7466;
  --board: #e2dbcf;
  --cell: #ebe5da;
  --card: #fbf9f4;
  --line: #d6cec0;
  --gap: 8px;
  --radius: 12px;
  --board-size: min(calc(100vw - 32px), 440px, calc(100svh - 230px));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #23211e;
    --ink: #ece6dc;
    --ink-soft: #a59d90;
    --board: #2f2c28;
    --cell: #383430;
    --card: #2b2825;
    --line: #48433d;
  }
}
:root[data-theme="dark"] {
  --bg: #23211e; --ink: #ece6dc; --ink-soft: #a59d90; --board: #2f2c28;
  --cell: #383430; --card: #2b2825; --line: #48433d;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: "DM Sans", system-ui, sans-serif;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  min-height: 100svh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 20px;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}

.top {
  width: var(--board-size);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.mark { margin: 0; font-size: 34px; font-weight: 700; letter-spacing: -0.02em; }
.next { display: flex; align-items: center; gap: 10px; }
.next-label { font-size: 14px; color: var(--ink-soft); }
.next-slot {
  width: 52px; height: 52px; border-radius: 10px; position: relative;
  background: var(--cell);
}
.next-slot .tile { position: absolute; inset: 0; width: 100%; height: 100%; transform: none; }

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  padding: var(--gap);
  background: var(--board);
  border-radius: calc(var(--radius) + 6px);
}

.cells, .tiles {
  position: absolute;
  inset: var(--gap);
}
/* A tile entering from beyond the edge is hidden until it crosses the board's rim. */
.tiles { clip-path: inset(calc(var(--gap) * -1) round calc(var(--radius) + 6px)); }
.cells {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--gap);
}
.cells i { background: var(--cell); border-radius: var(--radius); }

.tile {
  --x: 0; --y: 0;
  position: absolute;
  left: 0; top: 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  border-radius: var(--radius);
  transform: translate(calc(var(--x) * (100% + var(--gap))), calc(var(--y) * (100% + var(--gap))));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6%;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), inset 0 -3px 0 rgba(0, 0, 0, 0.07);
  will-change: transform;
}
.tile svg { width: 56%; height: 44%; overflow: visible; }
.tile .pips { display: flex; gap: 4px; height: 8px; align-items: center; font-size: 11px; font-weight: 700; }
.tile .pips:empty { display: none; }
.tile .pips b { width: 7px; height: 7px; border-radius: 50%; background: currentColor; display: block; }
.tile.under { z-index: 0; }
.tile.over { z-index: 2; }
.tile.pop { animation: pop 130ms ease-out; }
.tile.appear { animation: appear 160ms ease-out both; }
.tile.vanish { animation: vanish 200ms ease-in forwards; }
@keyframes pop { 0% { scale: 1; } 45% { scale: 1.12; } 100% { scale: 1; } }
@keyframes appear { from { opacity: 0; scale: 0.55; } to { opacity: 1; scale: 1; } }
@keyframes vanish { 0% { opacity: 1; scale: 1; } 40% { opacity: 1; scale: 1.08; filter: saturate(0) brightness(1.3); } 100% { opacity: 0; scale: 0.4; filter: saturate(0) brightness(1.3); } }

.controls { display: flex; gap: 12px; }
button {
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 22px;
  min-height: 44px;
  cursor: pointer;
}
button.primary { background: var(--ink); color: var(--bg); border-color: var(--ink); }
button:focus-visible { outline: 3px solid #5b82ad; outline-offset: 2px; }
.hint { margin: -8px 0 0; font-size: 13px; color: var(--ink-soft); }
.version-tag { margin: -8px 0 0; font-size: 11px; color: var(--ink-soft); letter-spacing: 0.02em; }
.version-tag--sheet { margin: 14px 0 0; text-align: center; }

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(35, 30, 25, 0.38);
  z-index: 10;
  touch-action: auto;
}
.overlay[hidden] { display: none; }
.card {
  background: var(--card);
  color: var(--ink);
  border-radius: 20px;
  padding: 22px 22px 20px;
  width: min(100%, 420px);
  max-height: calc(100svh - 32px);
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
}
.card h2 { margin: 0 0 12px; font-size: 22px; }
.rules-list { margin: 0 0 18px; padding-left: 20px; font-size: 15px; line-height: 1.5; }
.rules-list li { margin-bottom: 8px; }
.chip { display: inline-block; width: 14px; height: 14px; border-radius: 4px; vertical-align: -2px; margin: 0 2px; }
.card .primary { width: 100%; }

.end-card { text-align: center; }
.final { font-size: 56px; font-weight: 700; margin: 4px 0 10px; letter-spacing: -0.02em; }
.stats { display: flex; justify-content: center; gap: 22px; margin: 0 0 20px; }
.stats div { display: flex; flex-direction: column-reverse; }
.stats dt { font-size: 13px; color: var(--ink-soft); }
.stats dd { margin: 0; font-size: 20px; font-weight: 500; }

@media (prefers-reduced-motion: reduce) {
  /* Tiles jump to their cells; new and changed tiles still fade gently. */
  .tile.pop, .tile.appear { animation: fade-in 160ms ease-out both; }
  .tile.vanish { animation: fade-out 160ms ease-out forwards; }
}
@keyframes fade-in { from { opacity: 0.25; } to { opacity: 1; } }
@keyframes fade-out { to { opacity: 0; } }

/* ---------- introduction cards ---------- */
.intro { background: rgba(35, 30, 25, 0.30); cursor: pointer; }
.intro-card { text-align: center; outline: none; }
.intro-card.rise { animation: rise 320ms cubic-bezier(.2, .8, .3, 1) both; }
@keyframes rise { from { opacity: 0; transform: translateY(60px); } to { opacity: 1; transform: none; } }
.intro-art { display: flex; justify-content: center; gap: 12px; margin: 4px 0 14px; }
.tile.big {
  position: relative; transform: none; transition: none;
  width: 112px; height: 112px; border-radius: 22px;
}
.intro-art.pair .tile.big { width: 84px; height: 84px; border-radius: 18px; }
.tile.big .pips { height: 12px; gap: 6px; }
.tile.big .pips b { width: 10px; height: 10px; }
.intro-card h2 { font-size: 26px; margin-bottom: 8px; }
.voice { font-size: 17px; line-height: 1.45; margin: 0 0 16px; font-style: italic; }
.fact { text-align: left; background: var(--bg); border-radius: 14px; padding: 12px 14px; }
.fact-label { display: block; font-size: 12px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; color: var(--ink-soft); margin-bottom: 4px; }
.fact p { margin: 0; font-size: 15px; line-height: 1.45; }
.tap-hint { margin: 14px 0 0; font-size: 13px; color: var(--ink-soft); }

/* ---------- gallery ---------- */
.gallery-card { width: min(100%, 460px); }
.gallery-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 12px; }
.gallery-head h2 { margin: 0; }
.gallery-count { font-size: 14px; color: var(--ink-soft); }
.gallery-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-bottom: 16px; }
.gallery-item {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 6px 2px; border-radius: 12px; min-height: 0; font-size: 11px; line-height: 1.2;
  text-align: center; overflow-wrap: anywhere;
}
.gallery-item .intro-art { margin: 0; gap: 4px; }
.gallery-item .tile.big { width: 44px; height: 44px; border-radius: 10px; }
.gallery-item .intro-art.pair .tile.big { width: 26px; height: 26px; border-radius: 7px; }
.gallery-item .intro-art.pair .tile.big .pips { display: none; }
.gallery-item .tile.big .pips { height: 7px; gap: 3px; }
.gallery-item .tile.big .pips b { width: 5px; height: 5px; }
.gallery-item.unknown { cursor: default; color: var(--ink-soft); }
.tile.silhouette {
  background: var(--cell); color: var(--line); box-shadow: none;
}
