/* Escape Klotski - adaptacion movil de Readingg
   Inspirado en Another_Klotski (MIT). Estilos propios, sin recursos externos. */

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  overscroll-behavior: none;
}

.klotski {
  --pad: clamp(48px, 13vw, 56px);
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  max-height: 100dvh;
  gap: 8px;
  padding: calc(var(--rg-safe-top) + 10px) calc(var(--rg-safe-right) + 12px)
    calc(var(--rg-safe-bottom) + 10px) calc(var(--rg-safe-left) + 12px);
  overflow: hidden;
}

.klotski-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
}

.klotski-head__text {
  min-width: 0;
}

.klotski-eyebrow {
  margin: 0 0 3px;
  color: var(--rg-muted);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.klotski-head .rg-title {
  font-size: 22px;
  line-height: 1.05;
  letter-spacing: -0.03em;
}

.klotski-moves {
  flex: 0 0 auto;
  min-width: 78px;
  padding: 7px 10px 8px;
  border: 1px solid var(--rg-border);
  border-radius: 14px;
  background: linear-gradient(180deg, var(--rg-surface-2), var(--rg-surface));
  text-align: center;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}

.klotski-moves strong {
  display: block;
  font-size: 20px;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.klotski-moves small {
  display: block;
  margin-top: 3px;
  color: var(--rg-muted);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.klotski-level {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--rg-muted);
  font-size: 12.5px;
  font-weight: 700;
  line-height: 1.3;
}

.klotski-level > span:first-child {
  color: var(--rg-text);
}

.klotski-goal-chip {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border: 1px solid rgba(252, 206, 144, 0.32);
  border-radius: 999px;
  background: rgba(252, 206, 144, 0.1);
  color: var(--rg-gold);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.klotski-goal-chip svg {
  width: 12px;
  height: 12px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.klotski-stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1px 0;
}

/* Tablero ---------------------------------------------------------------- */

.board {
  --cell: 72px;
  position: relative;
  flex: 0 0 auto;
  width: 288px;
  height: 360px;
  border-radius: 20px;
  background:
    radial-gradient(120% 90% at 50% -10%, rgba(76, 109, 240, 0.16), transparent 58%),
    linear-gradient(180deg, var(--rg-surface-2), var(--rg-surface));
  box-shadow:
    inset 0 0 0 1px var(--rg-border),
    inset 0 1px 0 rgba(255, 255, 255, 0.05),
    var(--rg-shadow);
  overflow: hidden;
  touch-action: none;
  outline: none;
}

.board::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 5;
  border-radius: inherit;
  box-shadow: inset 0 0 34px rgba(0, 0, 0, 0.22);
  pointer-events: none;
}

.board:focus-visible {
  box-shadow:
    inset 0 0 0 2px var(--rg-primary),
    var(--rg-shadow);
}

.board-grid {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(5, 1fr);
  pointer-events: none;
}

.board-cell {
  border-right: 1px solid rgba(255, 255, 255, 0.045);
  border-bottom: 1px solid rgba(255, 255, 255, 0.045);
}

.board-cell:nth-child(4n) {
  border-right: 0;
}

.board-cell:nth-child(n + 17) {
  border-bottom: 0;
}

.board-cell.is-alt {
  background: rgba(255, 255, 255, 0.022);
}

[data-theme="light"] .board-cell {
  border-color: rgba(21, 24, 33, 0.06);
}

[data-theme="light"] .board-cell.is-alt {
  background: rgba(21, 24, 33, 0.035);
}

.board-goal {
  position: absolute;
  left: 25%;
  top: 60%;
  z-index: 0;
  width: 50%;
  height: 40%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 1px;
  padding-bottom: 7px;
  border: 1px dashed rgba(252, 206, 144, 0.46);
  border-radius: 16px;
  background:
    radial-gradient(80% 80% at 50% 100%, rgba(252, 206, 144, 0.16), transparent 72%),
    rgba(252, 206, 144, 0.045);
  box-shadow: inset 0 0 24px rgba(252, 206, 144, 0.06);
  pointer-events: none;
}

.board-goal__arrow {
  display: flex;
  opacity: 0.65;
}

.board-goal__arrow svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: var(--rg-gold);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  animation: goal-bob 1.8s ease-in-out infinite;
}

.board-goal__label {
  color: var(--rg-gold);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  opacity: 0.85;
}

.board.is-solved .board-goal {
  border-color: rgba(252, 206, 144, 0.85);
  background:
    radial-gradient(80% 80% at 50% 100%, rgba(252, 206, 144, 0.34), transparent 74%),
    rgba(252, 206, 144, 0.1);
}

@keyframes goal-bob {
  0%,
  100% {
    transform: translateY(0);
    opacity: 0.55;
  }
  50% {
    transform: translateY(3px);
    opacity: 0.9;
  }
}

[data-theme="light"] .klotski-goal-chip {
  border-color: rgba(138, 90, 18, 0.34);
  background: rgba(252, 206, 144, 0.22);
  color: #8a5a12;
}

[data-theme="light"] .board-goal {
  border-color: rgba(138, 90, 18, 0.45);
  background:
    radial-gradient(80% 80% at 50% 100%, rgba(240, 169, 76, 0.2), transparent 72%),
    rgba(252, 206, 144, 0.08);
}

[data-theme="light"] .board-goal__arrow svg {
  stroke: #8a5a12;
}

[data-theme="light"] .board-goal__label {
  color: #8a5a12;
}

/* Piezas ----------------------------------------------------------------- */

.board-pieces {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.piece {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(var(--cell) * 0.04);
  border: 0;
  border-radius: calc(var(--cell) * 0.18);
  color: #ffffff;
  font-family: var(--rg-font);
  line-height: 1;
  text-align: center;
  cursor: grab;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  transform: translate3d(0, 0, 0);
  transition:
    transform 170ms cubic-bezier(0.2, 0.8, 0.25, 1),
    box-shadow 160ms ease,
    filter 160ms ease;
  will-change: transform;
  box-shadow:
    0 6px 14px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.24),
    inset 0 -1px 0 rgba(0, 0, 0, 0.16);
}

.piece::before {
  content: "";
  position: absolute;
  inset: max(3px, calc(var(--cell) * 0.045));
  z-index: 0;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: calc(var(--cell) * 0.14);
  pointer-events: none;
}

.piece::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  border-radius: inherit;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.2), transparent 44%);
  pointer-events: none;
}

.piece > * {
  position: relative;
  z-index: 1;
}

.piece__name {
  max-width: 100%;
  padding: 0 3px;
  font-size: clamp(9px, calc(var(--cell) * 0.145), 16px);
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0.94;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.12);
}

.piece__mark {
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0.92;
}

.piece__mark svg {
  width: calc(var(--cell) * 0.34);
  height: calc(var(--cell) * 0.34);
  fill: currentColor;
}

.piece__dot {
  width: calc(var(--cell) * 0.16);
  height: calc(var(--cell) * 0.16);
  border-radius: 50%;
  background: rgba(42, 26, 5, 0.28);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25);
}

.piece--hero {
  color: #ffffff;
  background:
    radial-gradient(90% 90% at 25% 10%, rgba(255, 255, 255, 0.28), transparent 52%),
    linear-gradient(150deg, #ff5bb7 0%, #fd39a1 26%, #d81b8a 52%, #a03fe0 76%, #6d4df0 100%);
  border-radius: calc(var(--cell) * 0.2);
  box-shadow:
    0 10px 24px rgba(253, 57, 161, 0.34),
    0 4px 10px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.piece--hero::before {
  border-color: rgba(255, 255, 255, 0.26);
}

.piece--hero .piece__name {
  font-size: clamp(11px, calc(var(--cell) * 0.17), 19px);
  letter-spacing: 0.06em;
  text-shadow: 0 2px 6px rgba(71, 10, 52, 0.35);
}

.piece--tall {
  background:
    radial-gradient(90% 70% at 30% 8%, rgba(255, 255, 255, 0.22), transparent 56%),
    linear-gradient(180deg, #6f8cff 0%, #4c6df0 30%, var(--rg-primary-strong) 55%, #3450c8 100%);
}

.piece--wide {
  color: #06222d;
  background:
    radial-gradient(90% 90% at 20% 0%, rgba(255, 255, 255, 0.4), transparent 58%),
    linear-gradient(100deg, #8ee9ff 0%, #58cef2 52%, #2ba6d6 100%);
}

.piece--wide::before {
  border-color: rgba(255, 255, 255, 0.34);
}

.piece--wide .piece__name {
  color: #072a36;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.28);
}

.piece--single {
  color: #2a1a05;
  background:
    radial-gradient(90% 90% at 28% 4%, rgba(255, 255, 255, 0.52), transparent 58%),
    linear-gradient(150deg, #ffe3b5 0%, var(--rg-gold) 52%, #f0a94c 100%);
}

.piece--single::before {
  border-color: rgba(120, 70, 10, 0.14);
}

.piece.is-selected {
  z-index: 4;
  outline: 2px solid var(--rg-gold);
  outline-offset: 2px;
  box-shadow:
    0 0 0 6px rgba(252, 206, 144, 0.14),
    0 10px 24px rgba(0, 0, 0, 0.36),
    inset 0 1px 0 rgba(255, 255, 255, 0.32);
}

.piece.is-dragging {
  z-index: 6;
  cursor: grabbing;
  transition: none;
  filter: saturate(1.06) brightness(1.03);
  outline-color: rgba(252, 206, 144, 0.85);
}

.piece:focus-visible {
  outline: 2px solid var(--rg-primary);
  outline-offset: 2px;
}

.board.is-laying .piece {
  transition: none !important;
}

.board.is-solved .piece--hero {
  animation: hero-win 760ms ease-in-out infinite alternate;
}

@keyframes hero-win {
  from {
    filter: brightness(1);
    box-shadow:
      0 10px 24px rgba(253, 57, 161, 0.34),
      inset 0 1px 0 rgba(255, 255, 255, 0.4);
  }
  to {
    filter: brightness(1.15);
    box-shadow:
      0 0 0 6px rgba(252, 206, 144, 0.22),
      0 12px 28px rgba(253, 57, 161, 0.5),
      inset 0 1px 0 rgba(255, 255, 255, 0.4);
  }
}

/* Ayuda y controles ------------------------------------------------------ */

.klotski-hint {
  margin: 0;
  min-height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--rg-muted);
  font-size: 11.5px;
  line-height: 1.35;
  text-align: center;
}

.klotski-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.klotski-control-row {
  display: flex;
  align-items: stretch;
  gap: 10px;
}

.dpad {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(3, var(--pad));
  grid-template-rows: repeat(2, var(--pad));
  gap: 6px;
}

.dpad-btn {
  width: var(--pad);
  height: var(--pad);
  margin: 0;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--rg-border);
  border-radius: 15px;
  background: linear-gradient(180deg, var(--rg-surface-2), var(--rg-surface));
  color: var(--rg-text);
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
  transition:
    transform 120ms ease,
    background 120ms ease,
    color 120ms ease,
    opacity 120ms ease;
}

.dpad-btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.dpad-btn:not(:disabled):active {
  transform: scale(0.94);
  background: var(--rg-primary);
  border-color: var(--rg-primary);
  color: #ffffff;
}

.dpad-btn:focus-visible {
  outline: 2px solid var(--rg-primary);
  outline-offset: 2px;
}

.dpad-btn--up {
  grid-column: 2;
  grid-row: 1;
}

.dpad-btn--left {
  grid-column: 1;
  grid-row: 2;
}

.dpad-btn--down {
  grid-column: 2;
  grid-row: 2;
}

.dpad-btn--right {
  grid-column: 3;
  grid-row: 2;
}

.klotski-actions {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.klotski-actions .rg-btn {
  flex: 1 1 0;
  width: 100%;
  min-height: 48px;
  padding: 0 10px;
  font-size: 13.5px;
}

.klotski-actions .rg-btn svg {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.klotski-actions .rg-btn--primary {
  background: var(--rg-primary-strong);
  border-color: var(--rg-primary-strong);
}

.rg-btn:disabled,
.dpad-btn:disabled {
  opacity: 0.36;
  cursor: not-allowed;
}

.rg-btn:disabled:active {
  transform: none;
}

.level-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.level-btn {
  min-height: 48px;
  padding: 0 6px;
  font-size: 12.5px;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.level-btn:focus-visible {
  outline: 2px solid var(--rg-primary);
  outline-offset: 2px;
}

.level-btn[aria-pressed="true"] {
  border-color: var(--rg-primary-strong);
  background: var(--rg-primary-strong);
}

.klotski-credit {
  margin: 0;
  color: var(--rg-muted);
  font-size: 9.5px;
  letter-spacing: 0.02em;
  text-align: center;
  opacity: 0.72;
}

/* Modal de victoria ------------------------------------------------------ */

.klotski-modal {
  position: relative;
  max-width: 340px;
}

.win-badge {
  width: 58px;
  height: 58px;
  margin: 0 auto 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background:
    radial-gradient(90% 90% at 30% 10%, rgba(255, 255, 255, 0.28), transparent 58%),
    linear-gradient(150deg, #ff5bb7, #b04cf0 62%, #6d4df0);
  color: #ffffff;
  box-shadow:
    0 0 0 8px rgba(253, 57, 161, 0.12),
    0 10px 24px rgba(253, 57, 161, 0.28);
}

.win-badge svg {
  width: 30px;
  height: 30px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.win-eyebrow {
  margin: 0 0 4px;
  color: var(--rg-gold);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.klotski-modal .win-eyebrow {
  margin: 0 0 4px;
  color: var(--rg-gold);
  font-size: 10px;
  line-height: 1.4;
}

[data-theme="light"] .klotski-modal .win-eyebrow {
  color: #8a5a12;
}

.klotski-modal h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

.klotski-modal p {
  margin: 0 0 20px;
  font-size: 13.5px;
  line-height: 1.45;
}

.klotski-modal .rg-actions {
  gap: 8px;
}

.klotski-modal .rg-btn {
  min-height: 48px;
  padding: 0 14px;
}

.klotski-modal .rg-btn--accent {
  background: #d81b8a;
  border-color: #d81b8a;
}

.rg-overlay[hidden] {
  display: none !important;
}

.rg-overlay {
  animation: overlay-in 180ms ease-out;
}

.klotski-modal {
  animation: modal-in 240ms cubic-bezier(0.2, 0.85, 0.3, 1.05);
}

@keyframes overlay-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* Pausa controlada por ReadinggGame ------------------------------------- */

.klotski.is-paused .board-goal__arrow,
.klotski.is-paused .piece {
  animation-play-state: paused !important;
}

.klotski.is-paused .piece {
  transition: none !important;
}

/* Ajustes para pantallas bajas ------------------------------------------ */

@media (max-height: 700px) {
  .klotski {
    gap: 6px;
    padding-top: calc(var(--rg-safe-top) + 8px);
    padding-bottom: calc(var(--rg-safe-bottom) + 8px);
  }

  .klotski-eyebrow,
  .klotski-hint,
  .klotski-credit {
    display: none;
  }

  .klotski-head .rg-title {
    font-size: 19px;
  }

  .klotski-moves {
    padding: 6px 9px;
  }

  .klotski-moves strong {
    font-size: 18px;
  }

  .klotski-level {
    font-size: 11.5px;
  }

  .klotski-control-row {
    gap: 8px;
  }

  .klotski-controls {
    gap: 6px;
  }
}

@media (max-width: 340px) {
  .klotski {
    padding-left: calc(var(--rg-safe-left) + 8px);
    padding-right: calc(var(--rg-safe-right) + 8px);
  }

  .klotski-actions .rg-btn {
    font-size: 12.5px;
  }

  .level-btn {
    font-size: 11.5px;
  }
}
