:root {
  --bg: #121213;
  --text: #f8f8f8;
  --muted: #818384;
  --border: #3a3a3c;
  --border-filled: #565758;
  --key: #818384;
  --correct: #538d4e;
  --present: #b59f3b;
  --absent: #3a3a3c;
  --tile: min(62px, calc((100vw - 32px - (var(--len, 5) - 1) * 6px) / var(--len, 5)), calc((100dvh - 530px) / 6));
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Clear Sans', 'Helvetica Neue', Arial, sans-serif;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 16px;
}

header {
  position: relative;
  width: 100%;
  max-width: 500px;
  text-align: center;
  border-bottom: 1px solid var(--border);
  padding: 10px 0 8px;
}
header h1 { margin: 0; font-size: 32px; letter-spacing: 4px; font-weight: 800; }
header p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
header img { display: block; width: 88px; height: 72px; margin: 6px auto 0; image-rendering: pixelated; }

#ranking-btn {
  position: absolute;
  right: 0;
  top: 14px;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  font-size: 20px;
  cursor: pointer;
}
#ranking-btn:hover { background: var(--border); }

footer { color: var(--muted); font-size: 13px; padding: 0 0 10px; }

#ranking {
  position: fixed;
  inset: 0;
  background: rgb(0 0 0 / 0.6);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 2;
}
#ranking[hidden] { display: none; }
#ranking .panel {
  position: relative;
  width: 100%;
  max-width: 420px;
  max-height: 80dvh;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 20px;
}
#ranking h2 { margin: 0 0 12px; text-align: center; font-size: 20px; }
#ranking-close {
  position: absolute;
  top: 10px;
  right: 10px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
}
.ranking-list { list-style: none; margin: 0; padding: 0; }
.ranking-list li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}
.ranking-list li.empty { justify-content: center; color: var(--muted); border: 0; }
.ranking-list .pts { font-weight: 700; white-space: nowrap; }
.ranking-list .sub { color: var(--muted); font-size: 12px; }
#ranking .hint { color: var(--muted); font-size: 12px; text-align: center; margin: 12px 0 0; }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 100%;
}

#board { display: grid; gap: 6px; }
.row { display: grid; grid-template-columns: repeat(var(--len), var(--tile)); gap: 6px; }
.row.shake { animation: shake 0.5s; }

.tile {
  width: var(--tile);
  height: var(--tile);
  display: grid;
  place-items: center;
  border: 2px solid var(--border);
  font-size: calc(var(--tile) * 0.55);
  font-weight: 700;
  text-transform: uppercase;
}
.tile.filled { border-color: var(--border-filled); animation: pop 0.1s; }
.tile.correct, .tile.present, .tile.absent { border-color: transparent; }
.tile.correct { background: var(--correct); }
.tile.present { background: var(--present); }
.tile.absent { background: var(--absent); }
.tile.flip { animation: flip 0.5s ease-in both; }
.row.win .tile { animation: bounce 0.6s ease both; }

#keyboard {
  width: 100%;
  max-width: 500px;
  padding: 8px 0 12px;
  display: grid;
  gap: 8px;
}
.krow { display: flex; gap: 6px; justify-content: center; }
.key {
  flex: 1;
  max-width: 43px;
  height: 56px;
  border: 0;
  border-radius: 4px;
  background: var(--key);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
}
.key.wide { max-width: 66px; flex: 1.5; font-size: 12px; }
.key.correct { background: var(--correct); }
.key.present { background: var(--present); }
.key.absent { background: var(--absent); }
.key:active { filter: brightness(1.2); }

#toast {
  position: absolute;
  top: 12px;
  background: var(--text);
  color: var(--bg);
  font-weight: 700;
  padding: 12px 16px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
  z-index: 1;
}
#toast.show { opacity: 1; }

#result { margin-top: 16px; text-align: center; font-size: 16px; }
#result strong { text-transform: uppercase; letter-spacing: 2px; }

@keyframes pop { 50% { transform: scale(1.1); } }
@keyframes flip {
  0% { transform: rotateX(0); }
  50% { transform: rotateX(-90deg); }
  100% { transform: rotateX(0); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-20px); }
  60% { transform: translateY(4px); }
  100% { transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .tile, .row { animation: none !important; }
}

#side-ranking {
  width: 100%;
  max-width: 420px;
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
}
#side-ranking[hidden] { display: none; }
#side-ranking h2 { margin: 0 0 4px; font-size: 16px; text-align: center; }
#side-ranking .ranking-list li:last-child { border-bottom: 0; }

/* Con sitio, el ranking va en un lateral fijo en vez de debajo del tablero. */
@media (min-width: 960px) {
  #side-ranking {
    position: fixed;
    top: 50%;
    right: 24px;
    width: 300px;
    max-height: 80dvh;
    overflow-y: auto;
    margin: 0;
    transform: translateY(-50%);
  }
}

#hints { margin-top: 12px; text-align: center; }
#hints[hidden] { display: none; }
.hint-buttons { display: flex; gap: 8px; justify-content: center; }
.hint-buttons button {
  border: 1px solid var(--border-filled);
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  padding: 6px 12px;
  cursor: pointer;
}
.hint-buttons button:hover:not(:disabled) { background: var(--border); }
.hint-buttons button:disabled { opacity: 0.4; cursor: default; }
.hint-buttons small { color: var(--muted); font-weight: 400; font-size: 12px; }
#category { margin: 0 0 12px; font-size: 15px; text-align: center; }
#category:empty { display: none; }
#hint-info { margin: 8px 0 0; font-size: 14px; }
#hint-info[hidden] { display: none; }
