/* ==========================================================================
   Memory Match — style.css
   Deep-navy theme. Landscape (panel + board) layout on wide screens,
   portrait (stacked) on small screens. No horizontal scrolling anywhere.
   ========================================================================== */

/* ---------- Design tokens ---------- */
:root {
  --font: 'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', Arial, sans-serif;

  --ink: #e8f1ff;
  --muted: #93aacd;
  --surface: #0e2440;

  --primary: #38bdf8;
  --primary-dark: #0284c7;
  --blue: #2563eb;

  --success: #34d399;
  --danger: #fb7185;

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;

  --shadow-sm: 0 2px 8px rgba(2, 8, 20, 0.45);
  --shadow-md: 0 8px 24px rgba(2, 8, 20, 0.55);
  --shadow-lg: 0 18px 44px rgba(1, 5, 14, 0.65);

  /* Board geometry (grid shape is set from JS via --cols / --rows) */
  --gap-board: clamp(5px, 1vmin, 12px);
  --card-max: clamp(72px, 13vmin, 116px);

  /* Vertical chrome = everything around the board.
     Portrait/stacked layout needs more room than the desktop one. */
  --chrome-h: 330px;
  --avail-h: calc(100vh - var(--chrome-h));

  color-scheme: dark;
}

@supports (height: 100dvh) {
  :root {
    --avail-h: calc(100dvh - var(--chrome-h));
  }
}

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

html,
body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  overflow-x: hidden; /* never any horizontal scrolling */
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1000px 560px at 88% -12%, rgba(56, 189, 248, 0.16), transparent 60%),
    radial-gradient(820px 520px at -12% 112%, rgba(37, 99, 235, 0.22), transparent 58%),
    linear-gradient(158deg, #050f1f 0%, #081a33 42%, #0a2342 74%, #0d2c52 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;
}

:focus-visible {
  outline: 3px solid #fbbf24;
  outline-offset: 2px;
}

/* ---------- Page layout ---------- */
.layout {
  width: 100%;
  max-width: 1180px;
  margin-inline: auto;
  padding: clamp(12px, 2.5vw, 26px);
  display: flex;
  flex-direction: column; /* portrait / stacked on phones */
  gap: clamp(10px, 1.8vw, 18px);
}

/* ---------- Header ---------- */
.header {
  text-align: center;
  user-select: none;
}

.title {
  margin: 0;
  font-size: clamp(1.65rem, 5vw, 2.4rem);
  font-weight: 900;
  letter-spacing: 0.02em;
  color: #f2f8ff;
  text-shadow:
    0 0 18px rgba(56, 189, 248, 0.45),
    0 4px 0 rgba(2, 20, 44, 0.85),
    0 12px 28px rgba(0, 0, 0, 0.5);
}

.tagline {
  margin: 0.3rem 0 0;
  font-size: clamp(0.9rem, 2.6vw, 1rem);
  font-weight: 600;
  color: #a9c6ec;
}

/* ---------- Stats (HUD) ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  background: rgba(9, 24, 46, 0.88);
  border: 1px solid rgba(120, 170, 255, 0.14);
  border-radius: var(--radius-md);
  padding: 10px;
  box-shadow: var(--shadow-sm);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 0;
  text-align: center;
  background: rgba(148, 197, 255, 0.07);
  border-radius: var(--radius-sm);
  padding: 7px 6px;
}

.stat-label {
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--muted);
}

.stat-value {
  font-size: clamp(1.05rem, 3vw, 1.35rem);
  font-weight: 800;
  line-height: 1.15;
  white-space: nowrap;
  color: #f2f8ff;
}

.best-line {
  grid-column: 1 / -1;
  margin: 2px 2px 0;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--muted);
}

.best-line span {
  color: #7dd3fc;
}

/* Low-time warning */
.stat.warning .stat-value {
  color: var(--danger);
  animation: pulse-warn 0.85s ease-in-out infinite alternate;
}

/* ---------- Controls ---------- */
.controls {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

.level-bar-wrap {
  background: rgba(9, 24, 46, 0.88);
  border: 1px solid rgba(120, 170, 255, 0.14);
  border-radius: var(--radius-md);
  padding: 8px;
  box-shadow: var(--shadow-sm);
}

.level-bar {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(148, 187, 255, 0.3) transparent;
  padding-bottom: 2px;
}

.level-bar::-webkit-scrollbar {
  height: 6px;
}

.level-bar::-webkit-scrollbar-thumb {
  background: rgba(148, 187, 255, 0.28);
  border-radius: 3px;
}

.level-chip {
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 11px;
  border: 1px solid transparent;
  background: rgba(148, 197, 255, 0.08);
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 800;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, box-shadow 0.2s ease,
    transform 0.15s ease;
}

.level-chip:hover:not(:disabled):not(.current) {
  background: rgba(56, 189, 248, 0.16);
  color: #bae6fd;
  transform: translateY(-2px);
}

.level-chip.done {
  border-color: rgba(56, 189, 248, 0.4);
  background: rgba(56, 189, 248, 0.1);
  color: #7dd3fc;
}

.level-chip.current {
  background: linear-gradient(135deg, var(--primary), var(--blue));
  color: #ffffff;
  box-shadow: 0 4px 14px rgba(14, 165, 233, 0.5);
}

.level-chip.locked {
  opacity: 0.42;
  cursor: not-allowed;
}

.control-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.control-buttons .btn {
  flex: 1 1 150px;
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 47px;
  padding: 0.62rem 1.05rem;
  border: none;
  border-radius: 12px;
  font-size: 0.98rem;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.2s ease, filter 0.2s ease,
    background 0.2s ease;
}

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

.btn-primary {
  color: #ffffff;
  background: linear-gradient(135deg, #0ea5e9, #2563eb);
  box-shadow: 0 6px 18px rgba(14, 165, 233, 0.4);
}

.btn-primary:hover {
  filter: brightness(1.12);
  box-shadow: 0 8px 22px rgba(14, 165, 233, 0.5);
}

.btn-secondary {
  color: #cfe3ff;
  background: rgba(148, 197, 255, 0.09);
  border: 1px solid rgba(148, 187, 255, 0.28);
  box-shadow: var(--shadow-sm);
}

.btn-secondary:hover {
  background: rgba(148, 197, 255, 0.16);
  box-shadow: var(--shadow-md);
}

/* ---------- Board ---------- */
.board-area {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-width: 0;
  flex: 1;
}

.board {
  --cols: 4; /* overwritten inline by JS */
  --rows: 4;

  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: var(--gap-board);
  align-content: start;

  /* Fit inside the viewport both horizontally AND vertically:
     never wider than the column allows, never taller than the free
     space below the HUD (cards keep a 3:4 aspect ratio). */
  width: min(
    100%,
    calc(var(--cols) * var(--card-max) + (var(--cols) - 1) * var(--gap-board)),
    calc(
      max(
          240px,
          ((var(--avail-h) - (var(--rows) - 1) * var(--gap-board)) * 0.75 / var(--rows))
        ) * var(--cols) +
        (var(--cols) - 1) * var(--gap-board)
    )
  );
}

.board.locked {
  pointer-events: none;
}

/* ---------- Cards ---------- */
.card {
  position: relative;
  aspect-ratio: 3 / 4;
  padding: 0;
  border: none;
  background: transparent;
  cursor: pointer;
  perspective: 900px;
  user-select: none;
  touch-action: manipulation;
  transition: transform 0.2s ease;
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.34, 1.3, 0.55, 1);
}

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

.card-face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: clamp(8px, 1.6vmin, 14px);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  line-height: 1;
}

.card-front {
  color: #bcd9ff;
  font-weight: 900;
  font-size: clamp(1.1rem, calc(30vmin / var(--cols)), 3.1rem);
  text-shadow: 0 2px 8px rgba(1, 8, 20, 0.8);
  background:
    radial-gradient(circle at 30% 22%, rgba(125, 211, 252, 0.22), transparent 48%),
    repeating-linear-gradient(45deg, rgba(125, 180, 255, 0.06) 0 9px, transparent 9px 18px),
    linear-gradient(150deg, #1c4070 0%, #14305a 55%, #0e2344 100%);
  border: 1px solid rgba(125, 175, 255, 0.35);
  box-shadow:
    inset 0 1px 0 rgba(190, 220, 255, 0.18),
    var(--shadow-sm);
}

.card-back {
  transform: rotateY(180deg);
  font-size: clamp(1rem, calc(30vmin / var(--cols)), 2.9rem);
  background: #eef5ff;
  border: 2px solid rgba(125, 175, 255, 0.55);
  box-shadow: var(--shadow-sm);
}

/* Hover lift — desktop pointers only */
@media (hover: hover) and (pointer: fine) {
  .card:not(.flipped):not(.matched):hover {
    transform: translateY(-4px);
  }

  .card:not(.flipped):not(.matched):hover .card-face {
    box-shadow:
      inset 0 1px 0 rgba(190, 220, 255, 0.18),
      var(--shadow-lg);
  }
}

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

.card.matched .card-back {
  border-color: rgba(52, 211, 153, 0.85);
  box-shadow: 0 0 0 2px rgba(52, 211, 153, 0.35), 0 0 14px rgba(52, 211, 153, 0.35);
  animation: match-pop 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Mismatch */
.card.wrong {
  animation: shake 0.38s ease;
}

.card.wrong .card-front {
  background:
    radial-gradient(circle at 30% 22%, rgba(255, 160, 160, 0.25), transparent 48%),
    linear-gradient(150deg, #6e1c33 0%, #54122a 60%, #3d0e20 100%);
  border-color: rgba(251, 113, 133, 0.6);
}

/* ---------- Modals ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(2, 8, 20, 0.72);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade-in 0.22s ease both;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  position: relative;
  width: min(480px, 100%);
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  overscroll-behavior: contain;
  background: linear-gradient(170deg, #10294a, #0b1f39);
  border: 1px solid rgba(96, 165, 250, 0.24);
  border-radius: var(--radius-lg);
  padding: clamp(20px, 5vw, 30px);
  box-shadow: var(--shadow-lg);
  animation: pop-in 0.3s cubic-bezier(0.34, 1.45, 0.64, 1) both;
}

.modal h2 {
  margin: 0 0 0.8rem;
  font-size: clamp(1.3rem, 4.5vw, 1.55rem);
  font-weight: 900;
  color: #7dd3fc;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: rgba(56, 189, 248, 0.16);
  color: #bae6fd;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.15s ease;
}

.modal-close:hover {
  background: rgba(56, 189, 248, 0.3);
  transform: rotate(90deg);
}

/* Rules list with numbered badges */
.rules {
  counter-reset: step;
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.rules li {
  counter-increment: step;
  display: flex;
  gap: 0.6rem;
  align-items: flex-start;
  font-size: 0.97rem;
  line-height: 1.45;
  color: #dbe8fc;
}

.rules li::before {
  content: counter(step);
  flex: none;
  width: 27px;
  height: 27px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--blue));
  color: #ffffff;
  font-size: 0.82rem;
  font-weight: 800;
}

.tip {
  margin: 0.95rem 0 1rem;
  padding: 0.6rem 0.8rem;
  border-radius: var(--radius-sm);
  background: rgba(251, 191, 36, 0.12);
  border: 1px solid rgba(251, 191, 36, 0.25);
  color: #fcd34d;
  font-size: 0.88rem;
  font-weight: 600;
}

.modal-action {
  width: 100%;
}

/* Win / fail modals */
.modal-win {
  text-align: center;
}

.win-emoji {
  font-size: clamp(3.2rem, 12vw, 4.4rem);
  line-height: 1;
  animation: bounce-in 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) 0.15s both;
}

.win-message {
  margin: 0.2rem 0 1rem;
  color: var(--muted);
  font-weight: 600;
}

.win-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 0 0 0.9rem;
}

.win-stat {
  background: rgba(148, 197, 255, 0.08);
  border-radius: var(--radius-sm);
  padding: 10px 6px;
}

.win-stat dt {
  font-size: 0.66rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.win-stat dd {
  margin: 0.25rem 0 0;
  font-size: clamp(1.05rem, 3.5vw, 1.3rem);
  font-weight: 900;
  color: #7dd3fc;
}

.best-note {
  margin: 0 0 0.95rem;
  font-weight: 800;
  color: #fbbf24;
  animation: pulse-note 1.4s ease-in-out infinite alternate;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.modal-actions .btn {
  flex: 1 1 150px;
}

/* ---------- Desktop / large screens: landscape layout ----------
   Panel column on the left, board fills the rest of the row. */
@media (min-width: 900px), (min-width: 700px) and (orientation: landscape) {
  :root {
    --chrome-h: 48px;
  }

  .layout {
    display: grid;
    grid-template-columns: minmax(280px, 350px) minmax(0, 1fr);
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
      'header  board'
      'stats   board'
      'controls board';
    align-items: start;
    min-height: calc(100vh - 2 * clamp(12px, 2.5vw, 26px));
  }

  @supports (height: 100dvh) {
    .layout {
      min-height: calc(100dvh - 2 * clamp(12px, 2.5vw, 26px));
    }
  }

  .header {
    grid-area: header;
    text-align: left;
    padding-left: 4px;
  }

  .stats {
    grid-area: stats;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding: 12px;
    gap: 10px;
  }

  .controls {
    grid-area: controls;
    padding: 0 2px;
  }

  .board-area {
    grid-area: board;
    height: 100%;
    align-items: center; /* centre the board vertically in its column */
  }
}

/* Very small phones: tighten things up a little */
@media (max-width: 380px) {
  .btn {
    font-size: 0.92rem;
    padding: 0.58rem 0.8rem;
  }

  .level-chip {
    width: 34px;
    height: 34px;
    font-size: 0.86rem;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* ---------- Keyframes ---------- */
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pop-in {
  from {
    opacity: 0;
    transform: translateY(18px) scale(0.94);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes match-pop {
  0% { transform: scale(1); }
  45% { transform: scale(1.09); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px) rotate(-2deg); }
  40% { transform: translateX(7px) rotate(2deg); }
  60% { transform: translateX(-5px) rotate(-1.5deg); }
  80% { transform: translateX(5px) rotate(1.5deg); }
}

@keyframes bounce-in {
  0% { opacity: 0; transform: scale(0.3); }
  70% { opacity: 1; transform: scale(1.15); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes pulse-note {
  from { opacity: 0.72; }
  to { opacity: 1; }
}

@keyframes pulse-warn {
  from { opacity: 0.55; }
  to { opacity: 1; }
}
