/* Chimp Match — CSS custom properties are the presentational authority.
   The shell may override any --cm-* token (e.g. for dark mode); [data-theme]
   below is only the standalone default. */

:root {
  --cm-bg: #f4ecdf;
  --cm-surface: #fffaf0;
  --cm-mat: var(--cm-surface);
  /* shows in the gaps around non-square (contained) faces */
  --cm-ink: #3b2f26;
  --cm-muted: #8a7a68;
  --cm-accent: #d98a3d;
  /* warm chimp orange */
  --cm-accent-ink: #7a4a15;
  --cm-good: #4a9d6b;
  --cm-bad: #d1584e;
  --cm-tile-shadow: 0 2px 0 rgba(0, 0, 0, .12), 0 6px 14px rgba(0, 0, 0, .10);
  --cm-radius: 14px;
  --cm-gap: clamp(4px, 1.6vw, 10px);
  --cm-flip-ms: 340ms;
}

:root[data-theme='dark'] {
  --cm-bg: #221c16;
  --cm-surface: #2e261e;
  --cm-ink: #f0e6d8;
  --cm-muted: #b3a290;
  --cm-accent: #e6a15a;
  --cm-accent-ink: #ffe9cf;
  --cm-tile-shadow: 0 2px 0 rgba(0, 0, 0, .35), 0 6px 16px rgba(0, 0, 0, .40);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--cm-bg);
  color: var(--cm-ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#app {
  max-width: 560px;
  margin: 0 auto;
  padding: clamp(8px, 3vw, 20px);
  padding-top: max(clamp(8px, 3vw, 20px), env(safe-area-inset-top));
  padding-bottom: max(clamp(8px, 3vw, 20px), env(safe-area-inset-bottom));
  padding-left: max(clamp(8px, 3vw, 20px), env(safe-area-inset-left));
  padding-right: max(clamp(8px, 3vw, 20px), env(safe-area-inset-right));
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 2.5vw, 16px);
  min-height: 100dvh;
}

/* --- HUD --- */
.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr 1fr;
  align-items: center;
  gap: 8px;
  background: var(--cm-surface);
  border-radius: var(--cm-radius);
  padding: 10px 14px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .08);
}

.stat {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}

.stat .label {
  font-size: .68rem;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--cm-muted);
}

.stat .val {
  font-size: 1.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.streak {
  min-width: 3.4em;
  text-align: center;
  font-weight: 800;
  color: var(--cm-accent-ink);
  opacity: 0;
  transform: scale(.6);
  transition: opacity .18s, transform .18s;
}

.streak.lit {
  opacity: 1;
  transform: scale(1);
}

/* --- Difficulty selector (segmented control) --- */
.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  background: var(--cm-surface);
  border-radius: 999px;
  padding: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .08);
}

.seg button {
  appearance: none;
  border: 0;
  cursor: pointer;
  border-radius: 999px;
  padding: 8px 4px;
  font: inherit;
  font-weight: 600;
  font-size: .9rem;
  color: var(--cm-muted);
  background: transparent;
  transition: background .15s, color .15s;
}

.seg button[aria-pressed='true'] {
  background: var(--cm-accent);
  color: #fff;
}

.seg button:focus-visible {
  outline: 3px solid var(--cm-accent);
  outline-offset: 2px;
}

/* --- Board --- */
.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 6), 1fr);
  gap: var(--cm-gap);
}

.tile {
  appearance: none;
  border: 0;
  padding: 0;
  background: transparent;
  aspect-ratio: 1 / 1;
  perspective: 700px;
  cursor: pointer;
  border-radius: var(--cm-radius);
}

.tile:focus-visible {
  outline: 3px solid var(--cm-accent);
  outline-offset: 3px;
}

.tile[disabled] {
  cursor: default;
}

.tile-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform var(--cm-flip-ms) cubic-bezier(.2, .8, .3, 1.2);
}

.tile.flipped .tile-inner {
  transform: rotateY(180deg);
}

.face {
  position: absolute;
  inset: 0;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  border-radius: var(--cm-radius);
  box-shadow: var(--cm-tile-shadow);
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  overflow: hidden;
}

/* Back is a single shared image; contain + mat keeps a non-square back from
   cropping, matching how the faces are handled. Square backs fill as before. */
.face.back {
  background-size: contain;
  background-color: var(--cm-mat);
}

.face.front {
  transform: rotateY(180deg);
  background: var(--cm-mat);
}

.face-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  padding: 5px;
}

/* Matched tiles settle in with a pop, then dim slightly so unmatched pop more. */
.tile.matched .tile-inner {
  animation: pop .34s ease;
}

.tile.matched .face.front {
  box-shadow: 0 0 0 3px var(--cm-good) inset, var(--cm-tile-shadow);
}

.tile.matched {
  opacity: .82;
}

/* Mismatch: a soft shake, never a harsh flash. */
.tile.wrong .tile-inner {
  animation: shake .32s ease;
}

.tile.wrong .face.front {
  box-shadow: 0 0 0 3px var(--cm-bad) inset, var(--cm-tile-shadow);
}

@keyframes pop {
  0% {
    transform: rotateY(180deg) scale(1);
  }

  45% {
    transform: rotateY(180deg) scale(1.08);
  }

  100% {
    transform: rotateY(180deg) scale(1);
  }
}

@keyframes shake {

  0%,
  100% {
    transform: rotateY(180deg) translateX(0);
  }

  25% {
    transform: rotateY(180deg) translateX(-5px);
  }

  75% {
    transform: rotateY(180deg) translateX(5px);
  }
}

/* --- Dev + overlay --- */
.devbar {
  display: flex;
  justify-content: center;
}

.ghost {
  background: transparent;
  border: 1px solid var(--cm-muted);
  color: var(--cm-muted);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: .85rem;
  cursor: pointer;
}

.loading {
  text-align: center;
  color: var(--cm-muted);
  padding: 3rem 0;
}

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, .45);
  padding: 24px;
  z-index: 20;
}

.overlay.hidden {
  display: none;
}

.card {
  background: var(--cm-surface);
  color: var(--cm-ink);
  border-radius: 20px;
  padding: 26px 28px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .35);
  max-width: 320px;
  width: 100%;
  animation: rise .28s ease;
}

.card h2 {
  margin: .2em 0 .3em;
}

.card p {
  margin: .2em 0;
  color: var(--cm-muted);
}

.card .best {
  font-size: .9rem;
}

.card .tagline {
  margin: 0 0 .5em;
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--cm-accent-ink);
}

.stars {
  font-size: 2.4rem;
  letter-spacing: .1em;
  color: var(--cm-accent);
}

.primary {
  margin-top: 16px;
  background: var(--cm-accent);
  color: #fff;
  border: 0;
  border-radius: 999px;
  padding: 12px 22px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 3px 0 var(--cm-accent-ink);
}

.primary:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 var(--cm-accent-ink);
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(12px);
  }

  to {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile-inner {
    transition-duration: 1ms;
  }

  .tile.matched .tile-inner,
  .tile.wrong .tile-inner,
  .card {
    animation: none;
  }
}