:root {
  --wood: #5a3a22;
  --night: #07131f;
  --night-2: #0d2238;
  --blue: #2f7bd8;
  --blue-light: #7cc0ff;
  --gold: #f2c14e;
  --text: #e6f1ff;
  --danger: #ff7a7a;
}

* { box-sizing: border-box; }

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

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  color: var(--text);
  font-family: 'Trebuchet MS', 'Segoe UI', system-ui, sans-serif;
  background: var(--wood) url('/assets/img/background-image.png') repeat;
  background-size: 260px 130px;
  image-rendering: pixelated;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* The pixelated rendering is inherited; keep it for the tiled background only. */
.topbar, main, .overlay { image-rendering: auto; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: max(8px, env(safe-area-inset-top)) 16px 8px;
  background: linear-gradient(var(--night-2), var(--night));
  border-bottom: 3px solid var(--blue);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.title {
  margin: 0;
  font-size: clamp(1.1rem, 3.5vw, 1.8rem);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--blue-light);
  text-shadow: 0 0 10px rgba(47, 123, 216, 0.8), 2px 2px 0 #000;
}

.stats { display: flex; gap: 8px; }

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 70px;
  padding: 3px 10px;
  background: var(--night);
  border: 2px solid var(--blue);
  border-radius: 4px;
}
.stat small { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--blue-light); }
.stat b { font-size: 1.05rem; font-variant-numeric: tabular-nums; max-width: 12ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.actions { display: flex; gap: 8px; }

/* ---------- Layout ---------- */
main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 12px;
  overflow: auto;
}

#screenSetup { margin: auto; }
#screenGame { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
#screenGame[hidden], #screenSetup[hidden], .overlay[hidden], .stats[hidden] { display: none; }

.board-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board {
  display: grid;
  gap: var(--gap, 10px);
  padding: 0;
  justify-content: center;
}

.game-actions { display: flex; justify-content: center; gap: 10px; padding-bottom: env(safe-area-inset-bottom); }

/* ---------- Panels, buttons, form ---------- */
.panel {
  width: min(94vw, 460px);
  padding: 20px;
  background: linear-gradient(var(--night-2), var(--night));
  border: 3px solid var(--blue);
  border-radius: 8px;
  box-shadow: 0 0 0 3px #000, 0 10px 30px rgba(0, 0, 0, 0.6), inset 0 0 20px rgba(47, 123, 216, 0.25);
}
.panel h2 { margin: 0 0 14px; text-align: center; letter-spacing: 0.1em; text-transform: uppercase; color: var(--gold); text-shadow: 2px 2px 0 #000; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--blue-light); }

select, input[type='text'] {
  width: 100%;
  padding: 10px 12px;
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: #040c14;
  border: 2px solid var(--blue);
  border-radius: 4px;
  user-select: text;
}
select:focus, input:focus { outline: 2px solid var(--gold); outline-offset: 1px; }

.btn {
  padding: 10px 16px;
  font: inherit;
  font-weight: 700;
  color: var(--text);
  background: linear-gradient(#1b4a82, #123560);
  border: 2px solid var(--blue-light);
  border-radius: 4px;
  box-shadow: 0 3px 0 #000;
  cursor: pointer;
  transition: transform 0.08s, filter 0.15s;
}
.btn:hover { filter: brightness(1.2); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #000; }
.btn:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.btn.small { padding: 6px 12px; font-size: 0.9rem; }
.btn.icon { display: inline-flex; align-items: center; justify-content: center; padding: 6px 10px; }
.btn.icon.off { color: var(--danger); }
.btn.primary { width: 100%; font-size: 1.1rem; color: #2a1c00; background: linear-gradient(#ffd877, #d99a1f); border-color: #fff0c2; text-transform: uppercase; letter-spacing: 0.08em; }
.dialog-actions .btn.primary { width: auto; flex: 1 1 100%; }

.difficulty { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.btn.diff { display: flex; flex-direction: column; align-items: center; padding: 8px 4px; }
.btn.diff small { font-weight: 400; font-size: 0.7rem; opacity: 0.8; }
.btn.diff.active { background: linear-gradient(#ffd877, #d99a1f); color: #2a1c00; border-color: #fff0c2; }

.error { min-height: 1.2em; margin: 0 0 10px; color: var(--danger); font-size: 0.9rem; }
.note { text-align: center; color: var(--blue-light); }

/* ---------- Cards ---------- */
.card {
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  perspective: 800px;
  touch-action: manipulation;
}

.card .inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.card.flipped .inner { transform: rotateY(180deg); }
.card:not(.flipped):hover .inner { transform: translateY(-3px) scale(1.04); }

.face {
  position: absolute;
  inset: 0;
  display: block;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border: 3px solid var(--blue);
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.6), inset 0 0 0 2px #000;
}
.face.back {
  background: var(--night) url('/assets/img/card-back.png') center / cover no-repeat;
}
.face.front { transform: rotateY(180deg); border-color: var(--gold); background: var(--night); }
.face.front img { display: block; width: 100%; height: 100%; object-fit: cover; pointer-events: none; }

.card.matched { cursor: default; }
.card.matched .face.front { border-color: #6dff9c; animation: glow 0.8s ease-out; box-shadow: 0 0 14px #6dff9c, inset 0 0 0 2px #000; }

@keyframes glow {
  0% { box-shadow: 0 0 0 #6dff9c, inset 0 0 0 2px #000; }
  50% { box-shadow: 0 0 26px #fff, inset 0 0 0 2px #000; }
  100% { box-shadow: 0 0 14px #6dff9c, inset 0 0 0 2px #000; }
}

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  background: rgba(0, 0, 0, 0.7);
}
.dialog { max-height: 92dvh; overflow: auto; text-align: center; }
.dialog.wide { width: min(96vw, 640px); }
.dialog-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.dialog-actions .btn { flex: 1; }

.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tab {
  flex: 1;
  padding: 8px;
  font: inherit;
  font-weight: 700;
  color: var(--text);
  background: #0a1a2c;
  border: 2px solid var(--blue);
  border-radius: 4px 4px 0 0;
  cursor: pointer;
}
.tab.active { color: #2a1c00; background: var(--gold); border-color: #fff0c2; }

.table-wrap { max-height: 50dvh; overflow: auto; border: 2px solid var(--blue); border-radius: 4px; }
table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
th, td { padding: 7px 8px; text-align: left; white-space: nowrap; }
th { position: sticky; top: 0; background: #12365e; color: var(--blue-light); text-transform: uppercase; font-size: 0.75rem; letter-spacing: 0.06em; }
tbody tr:nth-child(even) { background: rgba(47, 123, 216, 0.1); }
tbody tr.me { background: rgba(242, 193, 78, 0.25); font-weight: 700; }
td.muted { text-align: center; color: var(--blue-light); }
td:nth-child(2) { max-width: 16ch; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Responsive ---------- */
@media (max-width: 520px) {
  .topbar { padding-left: 10px; padding-right: 10px; }
  .stat { min-width: 56px; padding: 2px 6px; }
  .panel { padding: 14px; }
  th, td { padding: 6px 5px; font-size: 0.82rem; }
}

@media (max-height: 480px) {
  .topbar { padding-top: 4px; padding-bottom: 4px; }
  main { padding: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .card .inner { transition-duration: 0.01s; }
  .card.matched .face.front { animation: none; }
}
