/* styles.css (clean + fresh)
   - Start screen: tap theme thumbnail to start
   - Game screen: header + centred 4x4 board + tiny status (no scrolling)
   - Gameplay theme background: SOLID COLOUR via CSS var --gameBgColor (set by game.js)
   - Start/success screens can still use background images
   - Rabbit R1 optimised for ~240 x 282
   - Match popup + overlays: single, Safari-safe rules
*/

:root{
  --bg: #f6f7fb;
  --panel: rgba(255,255,255,0.92);
  --panelBorder: rgba(15, 23, 42, 0.12);
  --text: #0f172a;
  --muted: rgba(15, 23, 42, 0.65);

  --tileBorder: rgba(15, 23, 42, 0.18);

  /* Defaults (desktop/tablet) */
  --gap: clamp(6px, 1.2vw, 10px);
  --boardPad: clamp(10px, 2vw, 18px);
  --imgPad: clamp(6%, 1.6vw, 10%);

  /* Screen backgrounds */
  --startBg: url("assets/start-bg.jpg");
  --successBg: url("assets/success-bg.jpg");
}

*{ box-sizing: border-box; }
html, body{ height: 100%; }

body{
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);

  /* Gameplay background colour comes from game.js -> --gameBgColor */
  background-color: var(--gameBgColor, var(--bg));
  background-image: var(--gameBgImage, none);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;


  touch-action: manipulation;
}

.hidden{ display: none !important; }

/* ===========================
   START SCREEN
   =========================== */

.screen{
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: 14px;
  padding-top: clamp(72px, 10vh, 96px);

}

.startScreen{
  background-image: var(--startBg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.startCard{
  width: min(640px, 94vw);
  background-color: rgba(255,255,255,0.78);
  border: 1px solid rgba(15,23,42,0.12);
  box-shadow: 0 18px 48px rgba(15,23,42,0.20);
  padding: 16px 14px;

  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

.startTitle{
  font-size: 20px;
  font-weight: 900;
  letter-spacing: 0.2px;
}

.startSub{
  margin-top: 6px;
  font-size: 13px;
  font-weight: 700;
  color: rgba(15,23,42,0.75);
}

.themeList{
  margin-top: 12px;
  display: grid;
  gap: 10px;
}

.themeOption{
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  align-items: center;

  padding: 10px;
  border: 1px solid rgba(15,23,42,0.12);
  background: rgba(255,255,255,0.88);
  cursor: pointer;
  user-select: none;
}

.themeOption:hover{ background: rgba(255,255,255,0.96); }
.themeOption:active{ transform: translateY(1px); }

.themeThumb{
  width: 72px;
  height: 72px;
  border: 1px solid rgba(15,23,42,0.14);
  background: rgba(15,23,42,0.04);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.themeLabel{
  font-weight: 900;
  font-size: 15px;
  line-height: 1.1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ===========================
   GAME SCREEN LAYOUT
   =========================== */

#gameScreen{
  min-height: 100svh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 8px;
  padding: 12px 10px;
}

/* Constrain inner content width on larger screens */
#gameScreen > .top,
#gameScreen > .board,
#gameScreen > .status{
  width: min(980px, 100%);
  justify-self: center;
}

.top{
  background: var(--panel);
  border: 1px solid var(--panelBorder);
  border-radius: 12px;
  padding: 10px 12px;

  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;

  box-shadow: 0 10px 22px rgba(15,23,42,0.10);
}

.titleRow{
  display: flex;
  gap: 10px;
  align-items: baseline;
  min-width: 0;
}

.title{
  font-weight: 900;
  font-size: 16px;
  line-height: 1.1;
  letter-spacing: 0.2px;
  white-space: nowrap;
}

.themePill{
  font-size: 12px;
  font-weight: 850;
  color: rgba(15,23,42,0.70);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 50vw;
}

.headerActions{
  display: grid;
  grid-auto-flow: column;
  gap: 10px;
  align-items: center;
}

button{
  appearance: none;
  border: 1px solid var(--panelBorder);
  background: #ffffff;
  color: var(--text);
  padding: 7px 10px;
  border-radius: 10px;
  font-weight: 800;
  font-size: 12px;
  cursor: pointer;
}
button:hover{ background: rgba(15,23,42,0.04); }
button:active{ transform: translateY(1px); }

/* ===========================
   BOARD
   =========================== */

.board{
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);

  padding: var(--boardPad);

  /* smooth white panel on top of solid background colour */
  background-color: rgba(255,255,255,0.70);

  border: 1px solid rgba(15,23,42,0.12);
  box-shadow: 0 16px 40px rgba(15,23,42,0.12);

  /* Give the grid a real width so 1fr works everywhere */
  width: min(92vmin, 760px);
  max-width: 100%;

  justify-self: center;
  align-self: center;

  /* Slight upward bias on normal screens */
  transform: translateY(clamp(-60px, -6vh, -18px));
}

/* ===========================
   TILES
   =========================== */

.tile{
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  perspective: 900px;
}

.tile button{
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: pointer;
}

.card{
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 200ms ease;
  box-shadow: 0 10px 22px rgba(15,23,42,0.18);
}

.face{
  position: absolute;
  inset: 0;
  backface-visibility: hidden;

  border-radius: 0; /* square corners */
  border: 1px solid var(--tileBorder);

  display: grid;
  place-items: center;
  overflow: hidden;
}

/* Unflipped tile – slightly transparent so colour shows through */
.face.front{
  background-color: rgba(255,255,255,0.55);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.frontInner{
  width: 82%;
  height: 82%;
  border-radius: 0;
  border: 1px dashed rgba(15,23,42,0.18);
  background-color: rgba(255,255,255,0.14);
}

.face.back{
  background: rgba(255,255,255,0.88);
  transform: rotateY(180deg);
}

.face.back img{
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: var(--imgPad);
  box-sizing: border-box;
}

.tile.flipped .card{ transform: rotateY(180deg); }
.tile.matched .face{ border-color: rgba(34,197,94,0.40); }

/* ===========================
   STATUS
   =========================== */

.status{
  min-height: 16px;
  display: grid;
  place-items: center;
}
.message{
  font-size: 12px;
  color: var(--muted);
}

/* ===========================
   OVERLAYS
   =========================== */

.overlay{
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,0.45);
  display: none;
  place-items: center;
  padding: 14px;
  z-index: 60;
}
.overlay.show{ display: grid; }

.overlayCard{
  width: min(420px, 92vw);
  background: rgba(255,255,255,0.92);
  border: 1px solid rgba(15,23,42,0.12);
  box-shadow: 0 24px 60px rgba(15,23,42,0.25);
  padding: 18px 16px;
  text-align: center;
}

.overlayTitle{
  font-size: 16px;
  font-weight: 900;
}

.overlayScore{
  margin-top: 10px;
  font-size: 14px;
  font-weight: 800;
  color: rgba(15,23,42,0.75);
}

.overlayBtns{
  margin-top: 14px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.overlayBtn,
.overlayBtnSecondary{
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 900;
}

.overlayBtnSecondary{
  background: rgba(15,23,42,0.06);
}

/* Success overlay background image */
.overlaySuccess{
  background-image: var(--successBg);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}
.overlayCardSuccess{
  transform: translateY(60px);
}

/* ===========================
   MATCH POPUP (single source of truth, Safari-safe)
   =========================== */

.matchPopup{
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;

  display: flex;
  align-items: center;
  justify-content: center;

  pointer-events: none;
  z-index: 99999;

  opacity: 0;
  visibility: hidden;
}

.matchPopup.show{
  opacity: 1;
  visibility: visible;
}

.matchPopupInner{
  width: 80vw;
  max-width: 420px;
  height: 80vw;
  max-height: 420px;

  display: flex;
  align-items: center;
  justify-content: center;

  background: transparent;

  opacity: 1;
  transform: scale(1);
}

.matchPopup.show .matchPopupInner{
  animation: matchPopFade 1000ms ease forwards;
}

.matchPopupInner img{
  width: 100%;
  height: 100%;
  object-fit: contain;
  padding: 6%;
  box-sizing: border-box;
}

@keyframes matchPopFade{
  0%   { transform: scale(0.82); opacity: 0; }
  20%  { transform: scale(1.06); opacity: 1; }
  60%  { transform: scale(1.00); opacity: 1; }
  100% { transform: scale(0.96); opacity: 0; }
}

/* ===================================================================== */
/* =================== Rabbit R1 optimisation (240x282) ================= */
/* ===================================================================== */

@media (max-width: 260px), (max-height: 300px){

  /* START screen: reduce width, centre theme options */
  .screen{ padding: 8px; }

  .startCard{
    width: 92vw;
    padding: 10px 10px;
    background-color: rgba(255,255,255,0.72);
    box-shadow: 0 12px 30px rgba(15,23,42,0.18);
  }
  .startTitle{ font-size: 16px; }
  .startSub{ font-size: 11px; margin-top: 4px; }

  .themeList{
    gap: 8px;
    margin-top: 10px;
    align-items: center;
    justify-items: center;
  }

  .themeOption{
    width: 200px;
    max-width: 100%;
    margin: 0 auto;

    grid-template-columns: 48px 1fr;
    gap: 6px;
    padding: 7px;
  }

  .themeThumb{ width: 48px; height: 48px; }
  .themeLabel{ font-size: 11px; line-height: 1.1; }

  /* GAME screen: compact */
  #gameScreen{
    padding: 6px 6px;
    gap: 6px;
  }

  .top{
    padding: 6px 8px;
    border-radius: 10px;
    gap: 6px;
  }

  .title{ font-size: 12px; }

  /* Give theme name breathing room from Quit button */
  .titleRow{ margin-right: 6px; }
  .themePill{
    font-size: 10px;
    max-width: 34vw;
    margin-left: 2px;
  }
  .headerActions{ margin-left: 6px; }

  button{
    padding: 6px 8px;
    border-radius: 10px;
    font-size: 11px;
  }

  :root{
    --gap: 4px;
    --boardPad: 6px;
    --imgPad: 6%;
  }

  /* Fit board into 240x282 comfortably */
  .board{
    width: min(200px, 92vw);
    max-width: 92vw;
    padding: var(--boardPad);
    transform: translateY(-2vh);
  }

  .card{ box-shadow: 0 6px 14px rgba(15,23,42,0.16); }

  .status{ min-height: 12px; }
  .message{ font-size: 10px; }

  /* Popup and overlays: never clip */
  .matchPopupInner{
    width: 72vw;
    height: 72vw;
    max-width: 220px;
    max-height: 220px;
  }

  .overlay{ padding: 10px; }
  .overlayCard{
    width: 92vw;
    padding: 12px 10px;
  }
  .overlayTitle{ font-size: 13px; }
  .overlayScore{ font-size: 12px; margin-top: 8px; }
  .overlayBtn,
  .overlayBtnSecondary{
    padding: 9px 10px;
    font-size: 12px;
  }

  .overlayCardSuccess{ transform: translateY(18px); }
}

@media (max-height: 240px){
  .board{ transform: none; }
}
