/* Minnisleikur — memory game styles. Everything is scoped under .mg so it can sit inside the Heimar site.
   The page that hosts the game supplies the Geograph font. */

.mg {
  --mg-navy: #003449;
  --mg-teal: #58d6dc;
  --mg-paper: #f7f6f2;
  --mg-card-w: min(150px, 12vw, 19vh);
  --mg-card-h: calc(var(--mg-card-w) * 1.4);
  --mg-gap: clamp(12px, 1.6vw, 22px);
  /* Paper grain on the card backs */
  --mg-grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .08 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");

  display: flex;
  flex-direction: column;
  align-items: center;
  color: #fff;
  font-family: "Geograph", system-ui, -apple-system, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.mg *, .mg *::before, .mg *::after { box-sizing: border-box; }
.mg [hidden] { display: none !important; }
.mg img { -webkit-user-drag: none; }
.mg input, .mg textarea { -webkit-user-select: text; user-select: text; }

/* Top bar: plain stats spread across the board width, restart as a teal text link on the right */
.mg-bar {
  width: calc(var(--mg-cols) * var(--mg-card-w) + (var(--mg-cols) - 1) * var(--mg-gap));
  display: flex;
  align-items: center;
  gap: 12px 56px;
  flex-wrap: wrap;
  margin-bottom: 22px;
}
.mg-stat {
  display: inline-flex;
  align-items: baseline;
  gap: 10px;
  color: rgba(255,255,255,.72);
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 0.12em;
}
.mg-stat b {
  color: #fff;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}
.mg-sound {
  margin-left: auto;
  width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  align-self: center;
  padding: 0;
  border: 0;
  background: none;
  color: var(--mg-teal);
  cursor: pointer;
  transition: opacity .2s ease;
}
.mg-sound svg { width: 22px; height: 22px; fill: currentColor; }
.mg-sound:hover { opacity: .75; }
.mg-sound:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.mg-sound[aria-pressed="true"] .mg-sound-off, .mg-sound[aria-pressed="false"] .mg-sound-on { display: none; }
/* Restart: teal button with an outlined play arrow */
.mg-restart {
  margin-left: -40px; /* the bar's gap is 56px; this leaves ~16px next to the sound button */
  align-self: center;
  height: 33px;
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 0 14px 0 16px;
  border: 0;
  background: var(--mg-teal);
  color: var(--mg-navy);
  font: inherit;
  font-size: 0.875rem;
  font-weight: 700;
  cursor: pointer;
  transition: background-color .2s ease;
}
.mg-restart svg { width: 14px; height: 14px; flex: none; fill: currentColor; transition: transform .2s ease; }
.mg-restart:hover { background: #7ce1e6; }
.mg-restart:hover svg { transform: translateX(2px); }
.mg-restart:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
@media (max-width: 640px) {
  .mg-bar { gap: 10px 22px; padding: 0; }
  .mg-stat { gap: 6px; font-size: 0.72rem; letter-spacing: 0.08em; }
  .mg-sound { margin-left: 0; }
  .mg-restart { margin-left: -10px; }
}

/* Buttons (used in the share dialog) */
.mg-btn {
  height: 44px;
  display: inline-flex;
  align-items: center;
  padding: 0 18px;
  border: 0;
  border-radius: 0;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color .2s ease;
}
.mg-btn:focus-visible { outline: 2px solid var(--mg-teal); outline-offset: 3px; }

/* Progress: pairs found, same width as the board */
.mg-progress {
  width: calc(var(--mg-cols) * var(--mg-card-w) + (var(--mg-cols) - 1) * var(--mg-gap));
  height: 26px;
  margin-bottom: 22px;
  border-radius: 13px;
  background: rgba(255,255,255,.22);
  overflow: hidden;
}
.mg-progress span {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 100%;
  width: 0;
  min-width: 52px; /* room for "0%" */
  padding-right: 12px;
  border-radius: 13px;
  background: #ffc452;
  color: var(--mg-navy);
  font-size: 0.875rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  transition: width .5s ease;
}
@media (max-width: 640px) { .mg-progress { height: 22px; } .mg-progress span { font-size: 0.8rem; min-width: 46px; padding-right: 10px; } }

/* Board */
.mg { --mg-cols: 6; }
.mg-board {
  display: grid;
  grid-template-columns: repeat(var(--mg-cols), var(--mg-card-w));
  gap: var(--mg-gap);
}
@media (max-width: 640px) {
  .mg { --mg-cols: 3; --mg-gap: 12px;
    /* 3 × 4 board (6 pairs): as wide as 3 fit across, and as tall as 4 rows fit below the bar (≈ 230 px of bar, progress and padding) */
    --mg-card-w: min(130px, calc((100vw - 32px - 2 * 12px) / 3), calc((100svh - 230px - 3 * 12px) / 4 / 1.4)); }
}

.mg-card {
  width: var(--mg-card-w);
  height: var(--mg-card-h);
  perspective: 900px;
  cursor: pointer;
  background: none;
  border: 0;
  color: var(--mg-navy);
  font: inherit;
  padding: 0;
  border-radius: 9%/6.5%;
  transform: rotate(var(--tilt, 0deg));
  transition: transform .25s ease;
}
.mg-card:focus-visible { outline: 3px solid var(--mg-teal); outline-offset: 4px; }
.mg-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 0.5s ease;
  transform-style: preserve-3d;
}
.mg-card.flipped .mg-inner { transform: rotateY(180deg); }
.mg-face {
  position: absolute;
  inset: 0;
  border-radius: 9%/6.5%;
  background: var(--mg-grain), var(--mg-paper);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  box-shadow: 2px 3px 0 rgba(0,0,0,.18), 0 8px 18px rgba(0,0,0,.22);
  transition: box-shadow .25s ease;
  overflow: hidden;
}
/* No ::after overlays or blend modes inside the faces: they break backface-visibility in Chrome. */
.mg-back { padding: 6%; }
.mg-back img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 7%/5%;
  display: block;
}
.mg-front {
  transform: rotateY(180deg);
  background: #fff; /* plain white so it matches the drawings' white background */
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 7% 5% 9%;
}
.mg-art {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.mg-art img { width: 100%; height: 100%; object-fit: contain; } /* drawings are square with even padding baked in */
.mg-placeholder {
  width: 80%;
  aspect-ratio: 1;
  border: 2px dashed rgba(0,52,73,.3);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4%;
  text-align: center;
  font-size: calc(var(--mg-card-w) * 0.09);
  color: rgba(0,52,73,.5);
}
.mg-name {
  margin-top: 8%;
  font-size: calc(var(--mg-card-w) * 0.125);
  font-weight: 500;
  line-height: 1.1;
  text-align: center;
  white-space: nowrap;
}

/* Match: a pop and a check badge */
.mg-card.matched { cursor: default; animation: mg-pop .45s ease .4s; } /* waits for the flip */

/* Hover: lift the card and straighten it (mouse only, face-down cards only) */
@media (hover: hover) {
  .mg-card:not(.flipped):not(.matched):hover { transform: translateY(-8px) rotate(0deg) scale(1.04); animation: none; }
  .mg-card:not(.flipped):not(.matched):hover .mg-face { box-shadow: 2px 3px 0 rgba(0,0,0,.14), 0 18px 32px rgba(0,0,0,.35); }
}

/* Before the first flip: cards hop in a wave and a hint sits over the board */
.mg-board-wrap { position: relative; }
.mg.mg-idle .mg-card { animation: mg-hop 5s ease-in-out infinite; animation-delay: calc(var(--i) * 70ms + 600ms); }
@keyframes mg-hop {
  0%, 10%, 100% { transform: rotate(var(--tilt, 0deg)) translateY(0); }
  5% { transform: rotate(var(--tilt, 0deg)) translateY(-12px); }
}
.mg-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 2;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 24px;
  background: var(--mg-teal);
  color: var(--mg-navy);
  font-size: 1rem;
  font-weight: 500;
  white-space: nowrap;
  box-shadow: 0 10px 30px rgba(0,0,0,.35);
  pointer-events: none; /* clicks go straight through to the cards */
  transform: translate(-50%, -50%);
  animation: mg-pulse 2s ease-in-out infinite;
  transition: opacity .3s ease;
}
.mg-hint svg { width: 22px; height: 22px; flex: none; }
.mg:not(.mg-idle) .mg-hint { opacity: 0; animation: none; }
@keyframes mg-pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.05); }
}
@media (prefers-reduced-motion: reduce) {
  .mg.mg-idle .mg-card, .mg-hint { animation: none; }
}
/* Win: cards ripple diagonally across the board before the dialog opens */
.mg.mg-waving .mg-card { animation: mg-wave .6s cubic-bezier(.3, 0, .3, 1) var(--wave, 0ms) both; }
@keyframes mg-wave {
  0%, 100% { transform: rotate(var(--tilt, 0deg)) translateY(0) scale(1); }
  40% { transform: rotate(calc(var(--tilt, 0deg) * -2)) translateY(-22px) scale(1.06); }
  70% { transform: rotate(var(--tilt, 0deg)) translateY(3px) scale(.99); }
}
@keyframes mg-pop { 50% { transform: rotate(var(--tilt, 0deg)) scale(1.1); } }
.mg-check {
  position: absolute;
  top: 5%;
  right: 6%;
  width: 22%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--mg-teal);
  display: flex;
  align-items: center;
  justify-content: center;
  transform: scale(0);
  transition: transform .3s cubic-bezier(.3, 1.6, .5, 1) .5s;
}
.mg-check svg { width: 66%; height: 66%; color: var(--mg-navy); }
.mg-card.matched .mg-check { transform: scale(1); }

/* Win dialog: share-image preview on the left, score and share buttons on the right */
.mg-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0, 52, 73, .6);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow-y: auto;
}
.mg-overlay.show { display: flex; }
.mg-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: 0;
  color: var(--mg-navy);
  cursor: pointer;
  transition: background-color .2s;
}
.mg-close:hover { background: #f2f2f2; }
.mg-close:focus-visible { outline: 2px solid var(--mg-teal); outline-offset: 2px; }
.mg-close svg { width: 20px; height: 20px; }
.mg-dialog {
  position: relative;
  background: #fff;
  color: var(--mg-navy);
  padding: 48px;
  max-width: 780px;
  width: 100%;
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 48px;
  align-items: center;
  box-shadow: 0 5px 34px rgba(0,0,0,.15);
  -webkit-user-select: none;
  user-select: none;
}
.mg-eyebrow {
  margin: 0 0 12px;
  color: var(--mg-teal);
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}
.mg-dialog h2 {
  margin: 0 0 16px;
  color: var(--mg-navy);
  font-size: 2.25rem;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.01em;
}
.mg-dialog p { margin: 6px 0; font-size: 1.05rem; font-weight: 300; line-height: 1.5; }
.mg-dialog p b { color: var(--mg-navy); font-weight: 700; }

/* Share image preview */
.mg-preview { display: flex; flex-direction: column; align-items: center; gap: 16px; }
.mg-preview-frame {
  width: 280px;
  height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f6f6f6;
}
.mg-preview-frame img { max-width: 220px; max-height: 384px; box-shadow: 0 6px 20px rgba(0,0,0,.18); }
.mg-preview-frame[data-format="square"] img { max-width: 260px; }
.mg-preview-msg { padding: 24px; font-size: .85rem; line-height: 1.5; text-align: center; }
.mg-format { display: flex; gap: 20px; }
.mg-format button {
  font: inherit;
  font-size: .8rem;
  font-weight: 500;
  background: none;
  border: 0;
  border-bottom: 1px solid transparent;
  padding: 2px 0;
  color: #5a5a5a;
  cursor: pointer;
}
.mg-format button[aria-pressed="true"] { color: var(--mg-navy); border-bottom-color: var(--mg-teal); }

/* Share buttons */
.mg-share { margin-top: 28px; }
.mg-share-label { margin: 0 0 12px !important; font-size: .75rem !important; font-weight: 500 !important; letter-spacing: .09em; text-transform: uppercase; color: var(--mg-navy); }
.mg-share-grid { display: flex; flex-wrap: nowrap; gap: 8px; } /* always one row */
.mg-share-grid .mg-btn { flex: 1 1 0; min-width: 0; padding: 0 8px; white-space: nowrap; }
.mg-share-grid .mg-btn[data-primary] { flex: 1 1 auto; overflow: hidden; } /* Deila takes the space the icons leave */
.mg-share .mg-btn.mg-icon-btn { flex: 0 0 52px; width: 52px; padding: 0; }
.mg-share .mg-btn.mg-icon-btn svg { width: 20px; height: 20px; }
.mg-share .mg-btn {
  justify-content: center;
  gap: 10px;
  background: #fff;
  color: var(--mg-navy);
  box-shadow: inset 0 0 0 1px rgba(0,52,73,.25);
  padding: 0 14px;
}
.mg-share .mg-btn:hover { background: #f2fbfb; box-shadow: inset 0 0 0 1px var(--mg-navy); }
.mg-share .mg-btn svg { width: 18px; height: 18px; flex: none; }
.mg-share .mg-btn[data-primary] { grid-column: 1 / -1; background: var(--mg-navy); color: #fff; box-shadow: none; }
.mg-share .mg-btn[data-primary]:hover { background: #0a4a63; }
.mg-share .mg-btn[disabled] { opacity: .5; cursor: progress; }
.mg-again {
  margin-top: 24px;
  font: inherit;
  font-size: .9rem;
  font-weight: 500;
  color: var(--mg-navy);
  background: none;
  border: 0;
  padding: 4px 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 10px;
}
.mg-again svg { width: 18px; height: 18px; flex: none; transition: transform .35s ease; }
.mg-again:hover svg { transform: rotate(-200deg); }
@media (prefers-reduced-motion: reduce) { .mg-again svg { transition: none; } }
.mg-again:focus-visible, .mg-format button:focus-visible { outline: 2px solid var(--mg-teal); outline-offset: 3px; }

@media (max-width: 760px) {
  .mg-overlay { align-items: flex-start; }
  .mg-dialog { grid-template-columns: minmax(0, 1fr); gap: 24px; padding: 28px 22px; text-align: center; }
  .mg-dialog > * { min-width: 0; }
  .mg-dialog p, .mg-dialog h2 { overflow-wrap: anywhere; }
  .mg-format { flex-wrap: wrap; justify-content: center; row-gap: 6px; }
  .mg-dialog h2 { font-size: 1.9rem; }
  .mg-preview-frame { width: 100%; height: 300px; }
  .mg-preview-frame img { max-width: 165px; max-height: 284px; }
  .mg-preview-frame[data-format="square"] img { max-width: 240px; max-height: 240px; }
  .mg-share { text-align: left; }
}

/* the share card is square now: a shorter frame */
.mg-preview-frame[data-format="square"] { height: 300px; }

/* Note when a friend's share link was for the other board size (9 pairs on computers, 6 on phones) */
.mg-friend-note { margin: -8px 0 18px; padding: 8px 14px; background: rgba(255,255,255,.12); color: #fff; font-size: 0.9rem; font-weight: 500; text-align: center; }
