:root {
  --bg-top: #e6e0d6;
  --bg-bottom: #cfc5b6;
  --tray: #c2b6a4;
  --ink: #3a3138;
  --slot-empty: rgba(255, 255, 255, 0.28);
  --accent: #e5483f;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: "Trebuchet MS", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  user-select: none;
}

#app {
  position: relative;
  max-width: 480px;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  inset: 0 0 auto 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  pointer-events: none;
}
#hud > * { pointer-events: auto; }
#level-title {
  font-weight: 900;
  font-size: 20px;
  letter-spacing: 1px;
  color: #fff;
  -webkit-text-stroke: 1.5px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 2px 0 var(--ink);
}
.hud-right { display: flex; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px;
  border: none; border-radius: 12px;
  background: rgba(255, 255, 255, 0.7);
  color: var(--ink);
  font-weight: 800; font-size: 18px;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.15);
  cursor: pointer;
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.15); }

/* ---------- Escena ---------- */
#stage { flex: 1; min-height: 0; position: relative; }
#stage canvas { display: block; width: 100%; height: 100%; }

#hint {
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  padding: 4px 16px 8px;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 50% { opacity: 0.45; } }

/* ---------- Cajas ---------- */
.box {
  width: 62px; height: 50px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center; gap: 3px;
  font-weight: 900; font-size: 18px;
  color: #fff;
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.22), inset 0 2px 0 rgba(255, 255, 255, 0.35), 0 3px 4px rgba(0, 0, 0, 0.15);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
  transition: transform 0.12s, filter 0.2s;
}
.box svg { width: 18px; height: 18px; opacity: 0.75; }
.box.light { color: var(--ink); text-shadow: none; }
.box.hidden-box { background: #8b8490 !important; color: #fff; }
.box.hidden-box svg { display: none; }

/* ---------- Ranuras ---------- */
#slots {
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 8px 12px 12px;
}
.slot {
  width: 62px; height: 50px;
  border-radius: 10px;
  background: var(--slot-empty);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.08);
}
.slot .box { animation: drop-in 0.22s ease-out; }
@keyframes drop-in { from { transform: translateY(40px) scale(0.8); opacity: 0; } }
#slots.shake { animation: shake 0.35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-6px); }
  40%, 80% { transform: translateX(6px); }
}

/* ---------- Bandeja ---------- */
#tray {
  background: var(--tray);
  box-shadow: inset 0 6px 10px rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 14px 12px max(18px, env(safe-area-inset-bottom));
  min-height: 210px;
}
.col { display: flex; flex-direction: column; gap: 8px; width: 62px; }
.col .box:not(.front) { filter: brightness(0.82) saturate(0.75); }
.col .box.front { cursor: pointer; }
.col .box.front:hover { transform: translateY(-3px); }
.col .box.front:active { transform: translateY(1px) scale(0.97); }
.col .more { text-align: center; font-size: 12px; font-weight: 800; opacity: 0.6; }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: rgba(40, 32, 36, 0.45);
  backdrop-filter: blur(3px);
  padding: 16px;
}
.hidden { display: none !important; }
.panel {
  background: #f6f1e9;
  border-radius: 20px;
  padding: 20px;
  width: min(360px, 100%);
  text-align: center;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  animation: pop 0.25s ease-out;
}
@keyframes pop { from { transform: scale(0.85); opacity: 0; } }
.panel h2 { margin: 0 0 14px; font-size: 24px; }
.panel p { margin: 10px 0; font-weight: 700; }
#result-art { width: 180px; height: 180px; image-rendering: pixelated; }

.panel-actions { display: flex; gap: 10px; justify-content: center; margin-top: 14px; }
.btn {
  border: none; border-radius: 12px;
  padding: 10px 18px;
  font-weight: 800; font-size: 16px;
  background: var(--accent); color: #fff;
  box-shadow: 0 4px 0 #a72d27;
  cursor: pointer;
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #a72d27; }
.btn.ghost { background: #ddd3c4; color: var(--ink); box-shadow: 0 4px 0 #b6aa98; }

#levels-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
.level-btn {
  aspect-ratio: 1;
  border: none; border-radius: 14px;
  background: #fff;
  font-weight: 900; font-size: 20px;
  color: var(--ink);
  box-shadow: 0 4px 0 #cfc3b2;
  cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.level-btn small { font-size: 10px; font-weight: 700; opacity: 0.7; }
.level-btn.done { background: #c9ecb8; box-shadow: 0 4px 0 #8fc678; }
.level-btn.locked { opacity: 0.45; cursor: not-allowed; }
.level-btn.current { outline: 3px solid var(--accent); }

/* ---------- Cajas enlazadas ---------- */
.box { position: relative; }
.box.link-right::after {
  content: '';
  position: absolute;
  right: -14px; top: 50%;
  width: 20px; height: 9px;
  transform: translateY(-50%);
  border-radius: 5px;
  background: #2b2730;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25);
  z-index: 1;
}
#tray .col { overflow: visible; }
#hint.warn { color: #b3261e; animation: none; }

/* ---------- Login ---------- */
#auth-form label {
  display: block;
  text-align: left;
  font-weight: 800;
  font-size: 14px;
  margin: 10px 0;
}
#auth-form input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 10px 12px;
  border: 2px solid #d6cbbb;
  border-radius: 10px;
  font-size: 16px;
  background: #fff;
  color: var(--ink);
}
#auth-form input:focus { outline: none; border-color: var(--accent); }
.tabs { display: flex; gap: 6px; background: #e6ddcf; padding: 4px; border-radius: 12px; }
.tab {
  flex: 1; border: none; border-radius: 9px; padding: 8px;
  font-weight: 800; background: transparent; color: var(--ink); cursor: pointer;
}
.tab.active { background: #fff; box-shadow: 0 2px 0 rgba(0, 0, 0, 0.1); }
.error { color: #b3261e; min-height: 1.2em; font-size: 14px; }
.user-line { margin: -6px 0 12px; font-size: 14px; }
.user-line a { color: var(--accent); }

/* ---------- Selector de niveles (38) ---------- */
.panel-wide { width: min(440px, 100%); }
#levels-grid {
  grid-template-columns: repeat(5, 1fr);
  max-height: 60vh;
  overflow-y: auto;
  padding: 4px 2px 8px;
}
.level-btn { font-size: 17px; }
.level-btn small { font-size: 9px; line-height: 1.1; padding: 0 2px; }

/* ---------- Paginación de niveles ---------- */
.pager { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.pager .icon-btn:disabled { opacity: 0.35; cursor: default; }
#page-title { font-weight: 900; font-size: 18px; }
.page-sub { font-size: 12px; font-weight: 700; opacity: 0.7; }
.progress-line { font-size: 13px; margin: 8px 0 0; }
.progress-line a { color: var(--accent); }

/* ---------- Botón para volver al selector de juegos ---------- */
.hud-left { display: flex; gap: 8px; }
a.icon-btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
