/* /css/memory.css
   Synups Memory (classic) — hardened mobile grid (prevents vertical stacking)

   Key problem pattern (your description):
   - PC OK, mobile stacks vertically
   - triplememory OK
   => Something mobile-only is overriding either:
      1) the grid container (display/grid-template-*) OR
      2) the grid items (.cell) to span full width (grid-column: 1 / -1 etc.)
   This file therefore ends with an ultra-specific MOBILE OVERRIDE that forces:
   - the memory grid container to be a real CSS Grid
   - explicit track counts (with fallback)
   - grid items to NOT span all columns
*/

/* ------------------------------------------------------------
   Reset + basics
------------------------------------------------------------ */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    sans-serif;
  background: var(--color-page-bg);
  color: var(--color-page-text);
  overflow: hidden; /* no scrollbars */
}

/* ------------------------------------------------------------
   Root defaults
------------------------------------------------------------ */
:root {
  --grid-cols: 8;
  --grid-rows: 8;

  /* Card back styling variables */
  --card-back-bg: #ffffff;
  --card-back-border: #94a3b8;
}

/* ------------------------------------------------------------
   Page layout: header + content
------------------------------------------------------------ */
.page {
  height: 100vh;  /* fallback */
  height: 100dvh; /* mobile-friendly viewport */
  display: grid;
  grid-template-rows: auto 1fr; /* header + remaining space */
}

/* ------------------------------------------------------------
   Grid wrapper (padding + background)
------------------------------------------------------------ */
.grid-wrapper {
  height: 100%;
  padding: 8px 4px;
  background: var(--color-grid-area-bg); /* visible in padding AND gaps */
  overflow: hidden;
}

/* ------------------------------------------------------------
   The Memory grid
------------------------------------------------------------ */
.grid-8x8 {
  width: 100%;
  height: 100%;
  display: grid;

  /* Default definition driven by CSS vars (JS can override) */
  grid-template-columns: repeat(var(--grid-cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--grid-rows), minmax(0, 1fr));

  grid-auto-flow: row;
  align-content: stretch;
  justify-content: stretch;

  row-gap: 10px;
  column-gap: 10px;

  background: none; /* so gaps show .grid-wrapper bg */
}

/* ------------------------------------------------------------
   Explicit tracks per level using body[data-grid]
   (works if your JS sets body.dataset.grid = `${cols}x${rows}`)
------------------------------------------------------------ */
body[data-grid="4x2"] .grid-8x8 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}
body[data-grid="4x3"] .grid-8x8 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
}
body[data-grid="4x4"] .grid-8x8 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
}
body[data-grid="4x5"] .grid-8x8 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
}
body[data-grid="4x6"] .grid-8x8 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(6, minmax(0, 1fr));
}
body[data-grid="5x6"] .grid-8x8 {
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(6, minmax(0, 1fr));
}
body[data-grid="6x6"] .grid-8x8 {
  grid-template-columns: repeat(6, minmax(0, 1fr));
  grid-template-rows: repeat(6, minmax(0, 1fr));
}
body[data-grid="6x7"] .grid-8x8 {
  grid-template-columns: repeat(6, minmax(0, 1fr));
  grid-template-rows: repeat(7, minmax(0, 1fr));
}
body[data-grid="7x8"] .grid-8x8 {
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
}
body[data-grid="8x8"] .grid-8x8 {
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
}

/* ------------------------------------------------------------
   Cells and cards
------------------------------------------------------------ */
.cell {
  display: grid;
  place-items: stretch;
  min-width: 0;
  min-height: 0;
}

.card {
  width: 100%;
  height: 100%;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
}

.card-inner {
  width: 100%;
  height: 100%;
  border-radius: var(--color-cell-radius);
  position: relative;
  overflow: hidden;
}

.card-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  backface-visibility: hidden;
  transform-style: preserve-3d;
  transition: transform 0.4s ease;
}

/* Back side: shown initially */
.card-back {
  background-color: var(--card-back-bg);
  background-image: url("/images/card-backs/256/backside_1.webp");
  background-repeat: no-repeat;
  background-position: center;
  background-size: 90% 90%;

  border-radius: var(--color-cell-radius);
  border: 1px solid var(--card-back-border);

  transform: rotateY(0deg);

  color: #64748b;
  font-size: 1.2rem;
}

/* Front side: rotated away initially */
.card-front {
  background: #ffffff;
  border-radius: var(--color-cell-radius);
  transform: rotateY(180deg);
}

.card-front img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}

/* Flipped state */
.card.is-revealed .card-back {
  transform: rotateY(180deg);
}

.card.is-revealed .card-front {
  transform: rotateY(0deg);
}

/* Matched styling */
.card.matched .card-inner {
  cursor: default;
}

.card.matched .card-front {
  background: #ffffff;
  border: 2px solid var(--color-cell-hover-bg);
}

.card.matched .card-back {
  border-color: var(--color-cell-hover-bg);
}

/* Subtle hover feedback */
.card:not(.matched):not(.is-revealed):hover .card-inner {
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.15);
}

/* ------------------------------------------------------------
   Mobile spacing per level
------------------------------------------------------------ */
@media (max-width: 767px) {
  body[data-grid="4x2"] .grid-8x8 { row-gap: 6px; column-gap: 6px; padding-top: 42%; padding-bottom: 42%; }
  body[data-grid="4x3"] .grid-8x8 { row-gap: 6px; column-gap: 6px; padding-top: 35%; padding-bottom: 35%; }
  body[data-grid="4x4"] .grid-8x8 { row-gap: 6px; column-gap: 6px; padding-top: 20%; padding-bottom: 20%; }
  body[data-grid="4x5"] .grid-8x8 { row-gap: 6px; column-gap: 6px; padding-top: 12%; padding-bottom: 12%; }
  body[data-grid="4x6"] .grid-8x8 { row-gap: 6px; column-gap: 6px; padding-top: 5%;  padding-bottom: 5%;  }

  body[data-grid="5x6"] .grid-8x8 { row-gap: 6px; column-gap: 6px; padding-top: 10%; padding-bottom: 10%; }
  body[data-grid="6x6"] .grid-8x8 { row-gap: 5px; column-gap: 5px; padding-top: 20%; padding-bottom: 20%; }
  body[data-grid="6x7"] .grid-8x8 { row-gap: 5px; column-gap: 5px; padding-top: 12%; padding-bottom: 12%; }
  body[data-grid="7x8"] .grid-8x8 { row-gap: 4px; column-gap: 4px; padding-top: 10%; padding-bottom: 10%; }
  body[data-grid="8x8"] .grid-8x8 { row-gap: 2px; column-gap: 2px; padding-top: 12%; padding-bottom: 12%; }
}

/* ------------------------------------------------------------
   Tablet and small desktop: keep grid from getting too wide
------------------------------------------------------------ */
@media (min-width: 768px) and (max-width: 1199px) {
  .grid-wrapper {
    padding-left: 10%;
    padding-right: 10%;
  }
}

/* ------------------------------------------------------------
   Large screen PC layout
------------------------------------------------------------ */
@media (min-width: 1200px) {
  body[data-grid="4x2"] .grid-8x8 { row-gap: 10px; column-gap: 10px; padding-top: 25%; padding-bottom: 25%; padding-left: 0;  padding-right: 0; }
  body[data-grid="4x3"] .grid-8x8 { row-gap: 10px; column-gap: 10px; padding-top: 14%; padding-bottom: 14%; padding-left: 0;  padding-right: 0; }
  body[data-grid="4x4"] .grid-8x8 { row-gap: 10px; column-gap: 10px; padding-top: 5%;  padding-bottom: 5%; }
  body[data-grid="4x5"] .grid-8x8 { row-gap: 10px; column-gap: 10px; padding-top: 0;  padding-bottom: 0;  padding-left: 5%;  padding-right: 5%; }
  body[data-grid="4x6"] .grid-8x8 { row-gap: 10px; column-gap: 10px; padding-top: 0;  padding-bottom: 0;  padding-left: 12%; padding-right: 12%; }

  body[data-grid="5x6"] .grid-8x8 { row-gap: 9px;  column-gap: 7px;  padding-top: 0;  padding-bottom: 0;  padding-left: 20px; padding-right: 20px; }
  body[data-grid="6x6"] .grid-8x8 { row-gap: 9px;  column-gap: 5px;  padding-top: 20px; padding-bottom: 20px; }
  body[data-grid="6x7"] .grid-8x8 { row-gap: 10px; column-gap: 5px;  padding-bottom: 0; }
  body[data-grid="7x8"] .grid-8x8 { row-gap: 9px;  column-gap: 5px;  padding-bottom: 0; }
  body[data-grid="8x8"] .grid-8x8 { row-gap: 9px;  column-gap: 3px;  padding-bottom: 0; }

  .grid-wrapper {
    padding-left: 30%;
    padding-right: 30%;
  }
}

/* ------------------------------------------------------------
   GAME FINISHED MODAL
------------------------------------------------------------ */
.stats-modal {
  position: fixed;
  inset: 0;
  display: none; /* shown via JS */
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(3px);
  z-index: 9999;
}

.stats-modal.open {
  display: flex;
}

.stats-modal-content {
  background: #ffffff;
  color: #111;
  padding: 24px 28px;
  border-radius: 10px;
  width: 90%;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 4px 28px rgba(0, 0, 0, 0.4);
}

.stats-list {
  text-align: left;
  margin: 16px 0 22px 0;
  font-size: 1rem;
  line-height: 1.6;
}

.stats-buttons {
  display: grid;
  gap: 10px;
}

.stats-btn {
  padding: 10px 16px;
  font-size: 1rem;
  border-radius: 6px;
  border: none;
  cursor: pointer;
}

.stats-btn.finish { background: #444; color: #fff; }
.stats-btn.new-game { background: #3b82f6; color: #fff; }
.stats-btn.change-level { background: #e67e22; color: #fff; }
.stats-btn.save-results { background: #16a34a; color: #fff; }

.stats-message {
  margin-top: 10px;
  font-size: 0.9rem;
}

/* ------------------------------------------------------------
   SYNUPS CODE MODAL
------------------------------------------------------------ */
.code-modal {
  position: fixed;
  inset: 0;
  display: none; /* shown via JS */
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(3px);
  z-index: 10000;
}

.code-modal.open {
  display: flex;
}

.code-modal-content {
  background: #ffffff;
  color: #111827;
  padding: 22px 24px;
  border-radius: 10px;
  width: 90%;
  max-width: 360px;
  box-shadow: 0 4px 28px rgba(0, 0, 0, 0.4);
}

.code-modal-text {
  font-size: 0.95rem;
  margin: 8px 0 14px;
}

.code-modal-label {
  display: block;
  font-size: 0.9rem;
  margin-bottom: 10px;
}

.code-modal-input {
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  font-size: 0.95rem;
  border-radius: 6px;
  border: 1px solid #cbd5e1;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
}

.code-modal-input:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 1px #3b82f6;
}

.code-modal-message {
  margin: 6px 0 12px;
  font-size: 0.9rem;
  min-height: 1em;
  color: #4b5563;
}

.code-modal-buttons {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 10px;
  margin-top: 4px;
}

.code-btn {
  padding: 9px 12px;
  font-size: 0.95rem;
  border-radius: 6px;
  border: none;
  cursor: pointer;
}

.code-btn-cancel {
  background: #e5e7eb;
  color: #111827;
}

.code-btn-connect {
  background: #3b82f6;
  color: #ffffff;
}

/* ============================================================
   FINAL MOBILE “ANTI-STACK” LOCK — MUST BE LAST IN FILE

   This targets the exact DOM used by classic memory.php:
     <div class="page">
       <main class="grid-wrapper">
         <section class="grid-8x8"> ... <div class="cell"> ...
   It wins even if another CSS file contains an !important rule
   with moderately high specificity (from the pyramid fix).

   It also neutralizes the OTHER common cause of “stacking”:
   a leaking rule that makes each .cell span all columns.
============================================================ */
@media (max-width: 767px) {
  html body .page main.grid-wrapper > section.grid-8x8 {
    display: grid !important;
    grid-auto-flow: row !important;
    align-content: stretch !important;
    justify-content: stretch !important;
    width: 100% !important;
    height: 100% !important;

    /* Fallback if body[data-grid] is missing for any reason */
    grid-template-columns: repeat(8, minmax(0, 1fr)) !important;
    grid-template-rows: repeat(8, minmax(0, 1fr)) !important;
  }

  /* If body[data-grid] exists, override fallback with correct tracks */
  html body[data-grid="4x2"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; grid-template-rows: repeat(2, minmax(0, 1fr)) !important; }
  html body[data-grid="4x3"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; grid-template-rows: repeat(3, minmax(0, 1fr)) !important; }
  html body[data-grid="4x4"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; grid-template-rows: repeat(4, minmax(0, 1fr)) !important; }
  html body[data-grid="4x5"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; grid-template-rows: repeat(5, minmax(0, 1fr)) !important; }
  html body[data-grid="4x6"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(4, minmax(0, 1fr)) !important; grid-template-rows: repeat(6, minmax(0, 1fr)) !important; }
  html body[data-grid="5x6"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(5, minmax(0, 1fr)) !important; grid-template-rows: repeat(6, minmax(0, 1fr)) !important; }
  html body[data-grid="6x6"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(6, minmax(0, 1fr)) !important; grid-template-rows: repeat(6, minmax(0, 1fr)) !important; }
  html body[data-grid="6x7"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(6, minmax(0, 1fr)) !important; grid-template-rows: repeat(7, minmax(0, 1fr)) !important; }
  html body[data-grid="7x8"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(7, minmax(0, 1fr)) !important; grid-template-rows: repeat(8, minmax(0, 1fr)) !important; }
  html body[data-grid="8x8"] .page main.grid-wrapper > section.grid-8x8 { grid-template-columns: repeat(8, minmax(0, 1fr)) !important; grid-template-rows: repeat(8, minmax(0, 1fr)) !important; }

  /* Critical: prevent any leaking rule that makes each tile span full row */
  html body .page main.grid-wrapper > section.grid-8x8 > .cell {
    grid-column: auto !important;
    grid-row: auto !important;
    width: auto !important;
    max-width: none !important;
    justify-self: stretch !important;
    align-self: stretch !important;
  }
}
