:root {
  --ui-scale: clamp(0.95rem, 1.1vw + 0.5rem, 1.1rem);
}

/* ===== Reset ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background: #0e1014;
  color: #f2f2f2;
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
  margin: 0;
}

/* ===== Layout ===== */
body {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  row-gap: 0.5rem;
}

/* ===== Header ===== */
header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;

  padding: 0.8rem 1rem;
}

header h1 {
  grid-column: 2;
  text-align: center;

  font-size: clamp(1.6rem, 5vw, 3.2rem);
  font-weight: 600;
}

/* ===== Grid de cartas ===== */
#grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 0.5rem;

  width: 100%;
  max-height: 80svh;
  aspect-ratio: 1/1;
  justify-self: center;
  align-self: center;
  padding: clamp(0.8rem, 3vw, 2rem);
}

/* ===== Cartas ===== */
playing-card {
  width: 100%;
  height: 100%;
  border-radius: 0.75rem;
  cursor: grab;

  filter: drop-shadow(0 0.3rem 0.8rem rgba(0,0,0,0.35));
  transition:
    transform 0.2s cubic-bezier(.2,.8,.2,1),
    filter 0.2s ease;

  touch-action: none;
  -webkit-touch-callout: none;
  user-select: none;
}

/* Hover desktop */
@media (hover: hover) {
  playing-card:hover {
    transform: translateY(-4%) scale(1.03);
    filter: drop-shadow(0 0.8rem 1.6rem rgba(0,0,0,0.55));
  }
}

/* Selección */
.selected {
  transform: translateY(-6%) scale(1.08);
  filter: drop-shadow(0 1.2rem 2.4rem rgba(0,0,0,0.7));
  z-index: 3;
}

/* Drag */
.dragging {
  position: relative;
  z-index: 10;
  pointer-events: none;
  transform: scale(1.1);
  filter: drop-shadow(0 1.6rem 3rem rgba(0,0,0,0.8));
}

/* ===== Footer ===== */
footer {
  display: flex;
  justify-content: center;
  margin-top: 1rem;
  padding-bottom: env(safe-area-inset-bottom);
}

/* ===== Button ===== */
.primary-btn {
  appearance: none;
  border: none;
  border-radius: 999px;
  margin-bottom: 2rem;
  padding: 1rem 1.6rem;
  font-size: clamp(1.1rem, 4vw, 1.6rem);
  font-weight: 600;
  background: #1c2230;
  color: #f2f2f2;
  cursor: pointer;

  box-shadow: 0 0.5rem 1.4rem rgba(0,0,0,0.45);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

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

/* ===== Message ===== */
#message {
  text-align: center;
  font-size: clamp(1rem, 4vw, 2rem);
  opacity: 0.85;
  padding-bottom: 1rem;
}

/* ===== Bloqueo horizontal ===== */
@media (orientation: landscape) {
  body {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  body > * {
    display: none;
  }

  body::after {
    content: "🔄 Gira el dispositivo para jugar en vertical";
    font-size: 1.2rem;
    text-align: center;
    color: #f2f2f2;
    padding: 2rem;
  }

  #grid {
    display: none;
  }
}

/* ===== Victoria ===== */
#grid.victory playing-card {
  animation: victoryPop 0.4s ease-out both;
}

#grid.victory playing-card:nth-child(n) {
  animation-delay: calc(var(--i, 0) * 40ms);
}

@keyframes victoryPop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

/* ===== Botón reglas ===== */
.rules-btn {
  grid-column: 3;
  justify-self: end;

  width: 2.4rem;
  height: 2.4rem;
  padding: 0;

  display: grid;
  place-items: center;

  font-size: 1.2rem;
  line-height: 1;

  background: #1c2230;
  color: #f2f2f2;
  border-radius: 50%;
  cursor: pointer;

  box-shadow: 0 0.4rem 1rem rgba(0,0,0,0.4);
}

/* ===== Modal reglas ===== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: grid;
  place-items: center;
  z-index: 100;
}

.hidden {
  display: none;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;

  margin-bottom: 0.5rem;
}

.modal-header h2 {
  margin: 0;
  font-size: calc(var(--ui-scale) * 1.4);
  line-height: 1.2;
}

.modal-content {
  position: relative;
  background: #1c2230;
  color: #f2f2f2;

  padding: 1.4em;
  border-radius: 1rem;

  width: min(92vw, 42rem);
  max-height: 82svh;

  font-size: var(--ui-scale);
  line-height: 1.5;

  overflow-y: auto;
}

.modal-content h2 {
  font-size: calc(var(--ui-scale) * 1.4);
  margin-bottom: 1rem;
}

.modal-content ul {
  margin-left: 1.2em;
}

.modal-content p,
.modal-content li {
  font-size: 1em;
}

.close-btn {
  width: 2.2rem;
  height: 2.2rem;
  padding: 0;

  display: grid;
  place-items: center;

  font-size: 1.2rem;
  line-height: 1;

  background: rgba(255,255,255,0.05);
  color: #f2f2f2;

  border-radius: 50%;
  box-shadow: none;
}

@media (hover: hover) {
  .close-btn:hover {
    background: rgba(255,255,255,0.08);
  }
}

@media (max-width: 360px) {
  .rules-btn {
    width: 2.8rem;
    height: 2.8rem;
    font-size: 1.4rem;
  }
}
