/* ==========================================================================
   Memory Matching Game — Styles
   Optimized for mouse, touch, stylus and large interactive whiteboards.
   ========================================================================== */

:root {
  --bg-base: #140f2b;
  /* Per-theme diffused background — overridden in JS via applyThemeBackground()
     when a theme loads; these are just the pre-JS / no-JS fallback (red/blue/yellow). */
  --bg-c1: 239, 68, 68;
  --bg-c2: 37, 99, 235;
  --bg-c3: 250, 204, 21;
  --panel: rgba(255, 255, 255, 0.08);
  --panel-solid: #241f3d;
  --text: #f5f3ff;
  --text-dim: #c4b5fd;
  --accent: #fbbf24;
  --accent-2: #34d399;
  --danger: #f87171;
  --card-back-1: #4338ca;
  --card-back-2: #6d28d9;
  --radius: 16px;
  --topbar-h: 72px;
  --statsbar-h: 52px;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden; /* no page scrolling — the board always fits the viewport */
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  background-color: var(--bg-base);
  background-image:
    radial-gradient(circle at 15% 15%, rgba(var(--bg-c1), 0.75) 0%, rgba(var(--bg-c1), 0) 60%),
    radial-gradient(circle at 85% 20%, rgba(var(--bg-c2), 0.75) 0%, rgba(var(--bg-c2), 0) 60%),
    radial-gradient(circle at 50% 100%, rgba(var(--bg-c3), 0.65) 0%, rgba(var(--bg-c3), 0) 65%);
  background-attachment: fixed;
  color: var(--text);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------------------- */
/* Top bar                                                                 */
/* ---------------------------------------------------------------------- */

.topbar {
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding: 10px 20px 10px 200px;
  background-color: #0c0c12;
  background-image:
    linear-gradient(rgba(8, 6, 16, 0.6), rgba(8, 6, 16, 0.6)),
    url("../../assets/textures/carbon-fiber.jpg");
  background-repeat: no-repeat, repeat;
  background-size: cover, 180px 135px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}

.brand {
  position: fixed;
  top: 12px;
  left: 18px;
  z-index: 70;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.brand-link {
  display: block;
  line-height: 0;
  transition: opacity 0.15s ease;
}

.brand-link:hover {
  opacity: 0.85;
}

.brand-logo {
  height: 44px;
  width: auto;
  display: block;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.35));
}

/* mobile-only home button, swapped in for the full logo — see media query below */
.brand-link-mobile {
  display: none;
}

.brand-home-icon {
  display: block;
  font-size: 26px;
  line-height: 1;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
}

/* On desktop each row is "unwrapped" so its children flow directly into
   .topbar's own flex layout (identical to the old single-row markup).
   Only the small-screen media query turns them into real 2-row groups. */
.topbar-row {
  display: contents;
}

.control {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
  padding: 6px 12px;
  min-height: 44px;
}

.control-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  white-space: nowrap;
}

.control select,
.control input[type="number"] {
  font: inherit;
  font-size: 15px;
  color: var(--text);
  background: transparent;
  border: none;
  outline: none;
  min-height: 32px;
  min-width: 3ch;
}

.control select option {
  color: #111;
}

.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  padding: 12px 18px;
  min-height: 44px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #f59e0b);
  color: #1a1a1a;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-ghost {
  background: var(--panel);
  color: var(--text);
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 18px;
  padding: 10px 14px;
}

/* ---------------------------------------------------------------------- */
/* Stats bar                                                               */
/* ---------------------------------------------------------------------- */

.statsbar {
  min-height: var(--statsbar-h);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
  flex-wrap: wrap;
  padding: 6px 16px;
  background: rgba(0, 0, 0, 0.15);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.stat-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}

.stat-value {
  font-size: 17px;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

/* ---------------------------------------------------------------------- */
/* Board area                                                              */
/* ---------------------------------------------------------------------- */

#boardArea {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  overflow: hidden;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), var(--card-w, 100px));
  grid-auto-rows: var(--card-h, 140px);
  gap: var(--gap, 12px);
  justify-content: center;
  align-content: center;
}

.board.hidden,
.info-panel.hidden,
.win-overlay.hidden {
  display: none !important;
}

/* ---------------------------------------------------------------------- */
/* Card                                                                    */
/* ---------------------------------------------------------------------- */

.card {
  width: var(--card-w);
  height: var(--card-h);
  perspective: 1200px;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.4, 0.15, 0.2, 1);
  border-radius: calc(var(--card-w, 100px) * 0.09);
  will-change: transform;
}

.card.flipped .card-inner,
.card.matched .card-inner {
  transform: rotateY(180deg);
}

.card:not(.flipped):not(.matched) .card-inner:hover {
  transform: translateY(-4px) scale(1.015);
}

.card-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: inherit;
  overflow: hidden;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-face img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
  -webkit-user-drag: none;
  user-select: none;
}

.card-back {
  background: linear-gradient(135deg, var(--card-back-1), var(--card-back-2));
  border: 2px solid rgba(255, 255, 255, 0.18);
}

.card-back.css-back {
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.06) 0 10px, transparent 10px 20px),
    linear-gradient(135deg, var(--card-back-1), var(--card-back-2));
}

.card-back .card-back-mark {
  font-size: calc(var(--card-w, 100px) * 0.32);
  opacity: 0.9;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.4));
}

.card-front {
  background: #fff;
  transform: rotateY(180deg);
  border: 2px solid rgba(255, 255, 255, 0.5);
}

.card-front .css-front {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-front .symbol {
  font-size: calc(var(--card-w, 100px) * 0.42);
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.25));
}

.card.matched .card-front {
  box-shadow: 0 0 0 4px var(--accent-2) inset, 0 6px 16px rgba(0, 0, 0, 0.35);
}

.card.matched .card-inner {
  animation: matchPulse 0.5s ease;
}

.card.matched {
  cursor: default;
}

@keyframes matchPulse {
  0% { transform: rotateY(180deg) scale(1); }
  45% { transform: rotateY(180deg) scale(1.1); }
  100% { transform: rotateY(180deg) scale(1); }
}

/* ---------------------------------------------------------------------- */
/* Info panels (loading / missing images)                                 */
/* ---------------------------------------------------------------------- */

.info-panel {
  position: absolute;
  inset: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  background: var(--panel-solid);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: var(--radius);
  padding: 32px;
  max-width: 720px;
  margin: auto;
}

.info-icon {
  font-size: 56px;
}

.info-panel h2 {
  margin: 0;
  font-size: 26px;
}

.info-panel p {
  margin: 0;
  color: var(--text-dim);
  max-width: 60ch;
}

.info-hint {
  font-size: 14px;
  margin-top: 6px;
}

.missing-list {
  list-style: none;
  margin: 6px 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  max-width: 60ch;
}

.missing-list li {
  background: rgba(248, 113, 113, 0.15);
  border: 1px solid rgba(248, 113, 113, 0.4);
  color: #fecaca;
  padding: 5px 12px;
  border-radius: 8px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
}

.info-spinner {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 4px solid rgba(255, 255, 255, 0.2);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------------------------------------------------------------------- */
/* Win overlay                                                             */
/* ---------------------------------------------------------------------- */

.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 5, 30, 0.82);
  backdrop-filter: blur(4px);
  cursor: pointer;
  touch-action: manipulation;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.win-content {
  text-align: center;
  padding: 48px 56px;
  border-radius: 24px;
  background: linear-gradient(160deg, #312e81, #581c87);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: winPop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
  max-width: 90vw;
}

@keyframes winPop {
  0% { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.win-emoji {
  font-size: 72px;
  margin-bottom: 6px;
}

.win-content h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 5vw, 48px);
  color: var(--accent);
}

.win-content p {
  margin: 4px 0;
  font-size: clamp(16px, 2.2vw, 22px);
  color: var(--text);
}

.win-stats {
  color: var(--text-dim) !important;
  font-size: clamp(14px, 2vw, 18px) !important;
}

.win-tap {
  margin-top: 18px !important;
  font-weight: 700;
  color: var(--accent-2) !important;
  animation: tapPulse 1.4s ease-in-out infinite;
}

@keyframes tapPulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

/* ---------------------------------------------------------------------- */
/* Small screens                                                           */
/* ---------------------------------------------------------------------- */

@media (max-width: 640px) {
  /* Two explicit rows instead of the desktop's fixed-corner logo + wrapping
     controls: row 1 = icon-only logo + theme; row 2 = everything else. */
  .topbar {
    min-height: 0;
    flex-direction: column;
    align-items: stretch;
    padding: 8px 10px;
    gap: 6px;
  }

  .topbar-row {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .topbar-row2 {
    flex-wrap: wrap;
  }

  .brand {
    position: static;
    top: auto;
    left: auto;
  }

  .brand-link-desktop {
    display: none;
  }

  .brand-link-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--panel);
    border: 1px solid rgba(255, 255, 255, 0.12);
  }

  .control-theme {
    flex: 1;
    min-width: 0;
  }

  .control-theme select {
    width: 100%;
  }

  .control-label { display: none; }

  .control {
    padding: 4px 10px;
    min-height: 36px;
  }

  .btn {
    padding: 8px 12px;
    font-size: 13px;
    min-height: 36px;
  }

  /* keep row 2 to a single line: New Game becomes icon-only, like Sound/Fullscreen already are */
  .topbar-row2 .btn-label { display: none; }
  #newGameBtn { padding: 8px 14px; }

  .statsbar { gap: 14px; }
  .win-content { padding: 32px 24px; }
}
