/* primes! — washi paper and ink; colour belongs to the tiles. */
:root {
  --paper: #F4EFE6;
  --paper-sunk: #E8E0D2;
  --card: #FBF8F2;
  --lip: #DCD3C2;
  --line: #E1D9CA;
  --ink: #24221F;
  --ink-soft: #5F5A52;
  --ink-faint: #9A9387;
  --scrim: rgba(36, 34, 31, .28);
  --cell: #DED5C5;
  --tile-shadow: rgba(60, 45, 25, .18);
  --gap: 10px;
  --radius: 14px;
  --display: 'Fraunces', Georgia, serif;
  --ui: 'Figtree', system-ui, -apple-system, 'Segoe UI', sans-serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #131518;
    --paper-sunk: #0D0E10;
    --card: #1C1F24;
    --lip: #08090B;
    --line: #2A2E35;
    --ink: #ECE7DD;
    --ink-soft: #A9A397;
    --ink-faint: #6C6A66;
    --scrim: rgba(0, 0, 0, .55);
    --cell: #1D2026;
    --tile-shadow: rgba(0, 0, 0, .45);
  }
}
:root[data-theme="dark"] {
  --paper: #131518; --paper-sunk: #0D0E10; --card: #1C1F24; --lip: #08090B; --line: #2A2E35;
  --ink: #ECE7DD; --ink-soft: #A9A397; --ink-faint: #6C6A66; --scrim: rgba(0, 0, 0, .55);
  --cell: #1D2026; --tile-shadow: rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; }

.app {
  max-width: 520px;
  min-height: 100%;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) calc(env(safe-area-inset-right, 0px) + 16px)
           calc(env(safe-area-inset-bottom, 0px) + 16px) calc(env(safe-area-inset-left, 0px) + 16px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* header */
.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.wordmark {
  margin: 0;
  font-family: var(--display);
  font-variation-settings: 'SOFT' 100;
  font-weight: 600;
  font-size: 40px;
  letter-spacing: -.02em;
  line-height: 1;
}
.wordmark .bang { color: #DE7044; }
.scores { display: flex; gap: 8px; }
.score-box {
  min-width: 76px;
  padding: 7px 12px 8px;
  border-radius: 12px;
  background: var(--card);
  box-shadow: 0 2px 0 var(--lip);
  display: flex; flex-direction: column; align-items: center;
}
.label { font-size: 12px; font-weight: 600; letter-spacing: .06em; color: var(--ink-soft); }
.value { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.score-box .value.bump { animation: bump 260ms ease-out; }
@keyframes bump { 40% { transform: scale(1.14); } }

/* bar */
.bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.next { display: flex; align-items: center; gap: 8px; }
.next-tile { width: 40px; height: 40px; border-radius: 10px; position: relative; overflow: hidden; }
.next-tile .tile-inner { border-radius: 10px; }
.next-tile .num { font-size: 15px; }
.next-tile .face { width: 80%; }
.bar-buttons { display: flex; gap: 8px; }
.btn {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  font-size: 15px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, .25);
  cursor: pointer;
  transition: transform 60ms, box-shadow 60ms;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, .25); }
.btn.ghost { background: var(--card); color: var(--ink); box-shadow: 0 3px 0 var(--lip); }
.btn.ghost:active { box-shadow: 0 1px 0 var(--lip); }
.btn.round { width: 44px; padding: 0; font-family: var(--display); font-size: 19px; }
.btn:focus-visible { outline: 2px solid var(--ink-soft); outline-offset: 2px; }
.btn.confirm { background: #DE7044; color: #fff; }

/* speech line */
.say {
  min-height: 62px;
  display: flex; align-items: center; gap: 10px;
  padding: 8px 12px 8px 8px;
  border-radius: 16px;
  background: var(--card);
  box-shadow: 0 2px 0 var(--lip);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 240ms cubic-bezier(.3, .7, .2, 1), transform 240ms cubic-bezier(.3, .7, .2, 1);
}
.say.show { opacity: 1; transform: none; }
.say-face { flex: 0 0 46px; width: 46px; height: 46px; border-radius: 12px; position: relative; overflow: hidden; }
.say-face .num { font-size: 15px; }
.say-text { margin: 0; font-size: 15px; line-height: 1.35; color: var(--ink); }

/* board */
.board-wrap { flex: 1; display: flex; justify-content: center; align-items: center; padding-bottom: 9vh; }
.board {
  position: relative;
  width: min(100%, 460px);
  aspect-ratio: 1;
  padding: var(--gap);
  border-radius: 20px;
  background: var(--paper-sunk);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, .06);
  touch-action: none;
}
.grid, .tiles { position: absolute; inset: var(--gap); }
.grid { display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr); gap: var(--gap); }
.grid i { border-radius: var(--radius); background: var(--cell); }

.tile {
  position: absolute;
  left: 0; top: 0;
  width: calc((100% - 3 * var(--gap)) / 4);
  height: calc((100% - 3 * var(--gap)) / 4);
  transform: translate(calc(var(--c) * (100% + var(--gap))), calc(var(--r) * (100% + var(--gap))));
  will-change: transform;
  cursor: pointer;
}
.tile.under { z-index: 0; }
.tile-inner {
  position: absolute; inset: 0;
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--fg);
  box-shadow: 0 3px 0 rgba(0, 0, 0, .12), 0 6px 14px var(--tile-shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  overflow: hidden;
}
.tile-inner::after { /* soft top light */
  content: ''; position: absolute; inset: 0; border-radius: inherit;
  background: linear-gradient(180deg, rgba(255, 255, 255, .22), rgba(255, 255, 255, 0) 45%);
  pointer-events: none;
}
.face { width: 84%; height: auto; display: block; margin-top: -2%; overflow: visible; }
.num {
  font-family: var(--display);
  font-variation-settings: 'SOFT' 100;
  font-weight: 650;
  font-size: clamp(18px, 7.4vw, 32px);
  line-height: 1;
  margin-top: 1%;
  font-variant-numeric: lining-nums;
  letter-spacing: -.01em;
}
.tile.appear .tile-inner { animation: appear 170ms cubic-bezier(.2, .9, .25, 1.15) both; }
.tile.pop .tile-inner { animation: pop 130ms cubic-bezier(.2, .9, .25, 1.15) both; }
.tile.pop .face { animation: face-in 220ms 40ms cubic-bezier(.2, .9, .25, 1.15) both; }
.tile.wiggle .tile-inner { animation: wiggle 420ms ease-in-out; }
.tile.hop .tile-inner { animation: hop 520ms cubic-bezier(.3, .7, .2, 1); }
@keyframes appear { from { transform: scale(.2); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes pop { 0% { transform: scale(.7); } 55% { transform: scale(1.13); } 100% { transform: scale(1); } }
@keyframes face-in { from { transform: translateY(6px) scale(.6); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes wiggle { 25% { transform: rotate(-5deg); } 50% { transform: rotate(4deg); } 75% { transform: rotate(-2deg); } }
@keyframes hop { 30% { transform: translateY(-14%) scale(1.04); } 60% { transform: translateY(0) scale(.98, 1.02); } }

.hint { margin: 0; text-align: center; font-size: 13px; color: var(--ink-soft); line-height: 1.4; }
.version-tag { margin: -4px 0 0; text-align: center; font-size: 11px; color: var(--ink-faint); letter-spacing: 0.02em; }
.version-tag--sheet { margin: 14px 0 0; }

/* sheets */
.scrim { position: fixed; inset: 0; background: var(--scrim); z-index: 10; animation: fade 240ms both; }
.scrim[hidden], .sheet[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }
@keyframes fade-in { from { opacity: 0.25; } to { opacity: 1; } }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  z-index: 11;
  max-width: 520px; margin: 0 auto;
  max-height: 86vh; overflow-y: auto;
  padding: 10px calc(env(safe-area-inset-right, 0px) + 20px) calc(env(safe-area-inset-bottom, 0px) + 18px) calc(env(safe-area-inset-left, 0px) + 20px);
  background: var(--card);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .18);
  animation: rise 420ms cubic-bezier(.2, .9, .25, 1.05) both;
  user-select: text; -webkit-user-select: text;
}
@keyframes rise { from { transform: translateY(100%); } }
.grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet h2 { margin: 4px 0 10px; font-family: var(--display); font-variation-settings: 'SOFT' 100; font-weight: 600; font-size: 28px; text-align: center; }
.sheet .small { font-size: 14px; color: var(--ink-soft); line-height: 1.45; margin: 8px 0; }
.sheet-actions { display: flex; gap: 10px; margin-top: 16px; }
.sheet-actions .btn { flex: 1; }
.rules { margin: 0; padding-left: 22px; font-size: 16px; line-height: 1.5; }
.rules li { margin: 6px 0; }

.over-hero { display: flex; align-items: center; gap: 14px; margin: 6px 0 14px; }
.over-tile { flex: 0 0 92px; width: 92px; height: 92px; position: relative; border-radius: 18px; }
.over-tile .tile-inner { border-radius: 18px; }
.over-tile .num { font-size: 30px; }
.over-line { margin: 0; font-size: 16px; line-height: 1.4; }
.over-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.over-stats > div { display: flex; flex-direction: column; align-items: center; padding: 8px; border-radius: 12px; background: var(--paper); }

.sheet.tall { max-height: 90vh; }
.cast-list { display: flex; flex-direction: column; gap: 10px; }
.cast-row { display: flex; gap: 12px; align-items: flex-start; padding: 10px; border-radius: 14px; background: var(--paper); }
.cast-row .mini { flex: 0 0 58px; width: 58px; height: 58px; position: relative; border-radius: 12px; }
.cast-row .mini .tile-inner { border-radius: 12px; }
.cast-row .mini .num { font-size: 18px; }
.cast-row p { margin: 0 0 4px; font-size: 14px; line-height: 1.4; }
.cast-row .who { font-weight: 700; font-size: 13px; color: var(--ink-soft); letter-spacing: .04em; }
.cast-row.unmet .mini { border: 2px dashed var(--ink-faint); display: flex; align-items: center; justify-content: center; font-family: var(--display); font-size: 18px; color: var(--ink-faint); }
.cast-row.unmet p { color: var(--ink-faint); }

@media (max-height: 720px) {
  .wordmark { font-size: 32px; }
  .say { min-height: 54px; }
  .app { gap: 10px; }
}
@media (prefers-reduced-motion: reduce) {
  /* Tiles jump to their cells; new and merged tiles still fade gently. */
  .tile.appear .tile-inner, .tile.pop .tile-inner { animation: fade-in 160ms ease-out both; }
  .tile.pop .face, .tile.wiggle .tile-inner, .tile.hop .tile-inner { animation-duration: 1ms; }
  .sheet { animation: fade 150ms both; }
}

/* introduction card: the first time a prime appears on this device */
.meet { position: fixed; inset: 0; z-index: 12; display: flex; align-items: center; justify-content: center;
        padding: calc(env(safe-area-inset-top, 0px) + 20px) calc(env(safe-area-inset-right, 0px) + 20px)
                 calc(env(safe-area-inset-bottom, 0px) + 20px) calc(env(safe-area-inset-left, 0px) + 20px);
        cursor: pointer; }
.meet[hidden] { display: none; }
.meet-card {
  width: min(100%, 360px);
  padding: 26px 22px 18px;
  border-radius: 28px;
  background: var(--card);
  box-shadow: 0 4px 0 var(--lip), 0 24px 60px rgba(0, 0, 0, .25);
  text-align: center;
  animation: meet-rise 520ms cubic-bezier(.2, .9, .25, 1.12) both;
}
.meet.leaving .meet-card { animation: meet-leave 200ms cubic-bezier(.3, .7, .2, 1) both; }
@keyframes meet-rise { from { transform: translateY(40px) scale(.92); opacity: 0; } }
@keyframes meet-leave { to { transform: translateY(16px) scale(.96); opacity: 0; } }
.meet-tile { width: 132px; height: 132px; margin: 0 auto 14px; position: relative; border-radius: 26px;
             animation: meet-hello 900ms 300ms cubic-bezier(.3, .7, .2, 1) both; }
.meet-tile .tile-inner { border-radius: 26px; }
.meet-tile .num { font-size: 42px; }
@keyframes meet-hello { 0% { transform: scale(.6) rotate(-8deg); } 45% { transform: scale(1.06) rotate(3deg); } 70% { transform: rotate(-2deg); } 100% { transform: none; } }
.meet h2 { margin: 0 0 8px; font-family: var(--display); font-variation-settings: 'SOFT' 100; font-weight: 600; font-size: 30px; }
.meet-intro { margin: 0 0 14px; font-size: 17px; line-height: 1.45; }
.meet-maths { padding: 12px 14px; border-radius: 16px; background: var(--paper); text-align: left; }
.meet-maths p { margin: 4px 0 0; font-size: 15px; line-height: 1.45; }
.meet-tap { margin: 14px 0 0; font-size: 13px; color: var(--ink-soft); }

/* gallery silhouettes */
.cast-row.unmet .mini { border: 0; }
.mini.silhouette .tile-inner { background: var(--ink-faint); box-shadow: none; opacity: .32; }
.mini.silhouette .tile-inner::after { display: none; }
.mini.silhouette svg * { fill: var(--card); stroke: var(--card); }
.mini.silhouette .num { color: var(--card); }
@media (prefers-reduced-motion: reduce) {
  .meet-card, .meet-tile, .meet.leaving .meet-card { animation-duration: 1ms; }
}
