:root {
  --fondo: #e7e9ec;
  --texto: #1d1f23;
  --suave: #5b6068;
  --panel: #ffffff;
  --borde: #cfd3d9;
  --acento: #0069b4;
  --nivel: #afca0a;
  --ok: #1f9d4c;
  --malo: #c8371d;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  background: var(--fondo); color: var(--texto);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden; touch-action: none; overscroll-behavior: none;
  user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
}
button {
  font: inherit; color: inherit; cursor: pointer;
  background: var(--panel); border: 1px solid var(--borde); border-radius: 10px;
  padding: 7px 12px; min-height: 38px;
  transition: background .15s, border-color .15s, transform .1s;
}
button:hover { border-color: #9aa1aa; }
button:active { transform: scale(.97); }
button:disabled { opacity: .4; cursor: default; transform: none; }
button.icono { min-width: 38px; padding: 4px 8px; font-size: 20px; line-height: 1; }
button.conmuta.activo { background: #1d1f23; color: #fff; border-color: #1d1f23; }
button.primario { background: var(--acento); color: #fff; border-color: var(--acento); }

/* Cabecera */
.barra {
  display: flex; align-items: center; gap: 12px;
  padding: calc(8px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 8px calc(16px + env(safe-area-inset-left));
  background: #141517; color: #fff;
}
.barra button { background: #26282c; border-color: #3a3d42; color: #fff; }
.logo {
  margin: 0; font-size: 19px; font-weight: 800; letter-spacing: .01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0;
}
.rueda-logo { display: inline-block; color: #fbb900; animation: gira-logo 8s linear infinite; }
@keyframes gira-logo { to { transform: rotate(360deg); } }
.navreto { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.reto-badge { display: flex; align-items: center; gap: 8px; font-weight: 700; padding: 5px 12px; }
.reto-badge b {
  background: var(--nivel); color: #fff; border-radius: 999px; min-width: 34px; padding: 2px 8px;
  text-shadow: 0 1px 1px rgba(0,0,0,.25);
}

/* Tablero */
main { flex: 1; min-height: 0; position: relative; }
#tablero { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.estado {
  text-align: center; padding: 4px 16px; min-height: 28px; font-weight: 600; color: var(--suave);
}
.estado .ok { color: var(--ok); }
.estado .malo { color: var(--malo); }

/* Herramientas */
.herramientas {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 8px 18px;
  padding: 8px 16px calc(10px + env(safe-area-inset-bottom));
  background: #f6f7f8; border-top: 1px solid var(--borde);
}
.herramientas .grupo { display: flex; gap: 6px; }
@media (max-width: 640px) {
  .herramientas button span { display: none; }
  .herramientas button { min-width: 44px; font-size: 18px; }
  .logo { font-size: 16px; white-space: normal; line-height: 1.15; }
  .reto-badge #retoNivel { display: none; }
}

/* Piezas */
.pieza { cursor: grab; transition: transform .28s cubic-bezier(.3,.7,.3,1); }
.engranajes .pieza-eng { transition: transform .28s cubic-bezier(.3,.7,.3,1); pointer-events: none; }
.pieza.arrastrando, .pieza-eng.arrastrando { transition: none; }
.pieza.arrastrando { cursor: grabbing; }
.pieza.fija { cursor: not-allowed; }
.pieza.sel .contorno line { stroke: #fff; }
.pieza.sel .contorno circle { fill: #fff; }
.cuerpos { filter: drop-shadow(0 3px 2px rgba(0,0,0,.45)); }
.rueda { cursor: pointer; }
.capa-info { pointer-events: none; }

/* Diálogos */
.dialogo {
  border: none; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 48px); box-shadow: 0 20px 60px rgba(0,0,0,.35); color: var(--texto);
}
.dialogo::backdrop { background: rgba(10,12,15,.55); }
.dialogo .cab { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; margin: 0; }
.dialogo h2 { margin: 0; font-size: 19px; }
.pestanas { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 16px; }
.pestanas button { font-weight: 700; border-width: 2px; }
.pestanas button.activo { color: #fff; }
.rejilla {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); gap: 8px;
  padding: 10px 16px; max-height: 52vh; overflow: auto;
}
.rejilla button { font-weight: 700; padding: 8px 0; position: relative; }
.rejilla button.hecho::after {
  content: "✓"; position: absolute; top: -6px; right: -4px; width: 18px; height: 18px; border-radius: 50%;
  background: var(--ok); color: #fff; font-size: 12px; line-height: 18px;
}
.rejilla button.actual { outline: 3px solid var(--texto); outline-offset: 1px; }
.dialogo .pie { padding: 4px 16px 16px; margin: 0; }
button.ancho { width: 100%; }
.texto { padding: 4px 20px 18px; }
.texto p, .texto li { margin: 6px 0; }
.texto .nota { color: var(--suave); font-size: 13px; }

/* Aviso de reto resuelto */
.aviso {
  position: fixed; inset: 0; display: grid; place-items: center; pointer-events: none;
}
.aviso[hidden] { display: none; }
.aviso .tarjeta {
  pointer-events: auto; background: var(--panel); border-radius: 18px; padding: 20px 24px; text-align: center;
  box-shadow: 0 18px 50px rgba(0,0,0,.35); animation: aparece .35s cubic-bezier(.3,1.4,.5,1);
  max-width: calc(100vw - 32px);
}
.aviso .titulo { font-size: 26px; font-weight: 800; color: var(--ok); }
.aviso .sub { color: var(--suave); margin: 4px 0 14px; }
.aviso .botones { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
@keyframes aparece { from { transform: scale(.7); opacity: 0; } }
