:root {
  --bg: #e8e8e8;
  --card: #ffffff;
  --ink: #111111;
  --muted: #555555;
  --border: #cccccc;
  --ok: #1a7a3a;
  --ok-bg: #d8f5e2;
  --fail: #b01a1a;
  --fail-bg: #fde0e0;
  --btn: #111111;
  --spin: #333;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--ink);
  height: 100%;
  overflow: hidden;
}
.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100dvh;
}
header.bar {
  flex: 0 0 auto;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0.55rem 0.9rem;
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  flex-wrap: wrap;
}
header.bar h1 { margin: 0; font-size: 1.15rem; letter-spacing: 0.02em; }
header.bar .tag { color: var(--muted); font-size: 0.78rem; }
.sheet-area {
  flex: 1 1 auto;
  min-height: 0;
  padding: 0.45rem 0.55rem 0.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
#preview {
  background: #fff;
  border: 1px solid #bbb;
  box-shadow: 0 2px 10px rgba(0,0,0,0.08);
  width: 100%;
  height: 100%;
  max-height: 100%;
  aspect-ratio: 297 / 210;
  max-width: min(100%, calc((100dvh - 210px) * 297 / 210));
}
#preview svg { display: block; width: 100%; height: 100%; }
/* La hoja blanca mantiene exactamente la proporción A4 (antes se estiraba y parecía tener
   márgenes laterales vacíos). */
@supports (width: 1cqw) {
  .sheet-area { container-type: size; }
  #preview {
    width: min(100cqw, 100cqh * 297 / 210);
    height: auto;
    max-width: none; max-height: none;
    aspect-ratio: 297 / 210;
  }
}
.controls {
  flex: 0 0 auto;
  background: var(--card);
  border-top: 1px solid var(--border);
  padding: 0.55rem 0.7rem 0.7rem;
  padding-bottom: max(0.7rem, env(safe-area-inset-bottom));
}
.row-gen { display: flex; gap: 0.55rem; margin-bottom: 0.5rem; }
button#btn-generar {
  flex: 1;
  cursor: pointer;
  border: 2px solid #000;
  border-radius: 14px;
  padding: 0.95rem 1rem;
  font-size: 1.35rem;
  font-weight: 700;
  background: var(--btn);
  color: #fff;
  letter-spacing: 0.01em;
}
button#btn-generar:active { opacity: 0.88; transform: scale(0.99); }
button#btn-generar:disabled { opacity: 0.55; cursor: wait; }
.row-caps {
  display: flex;
  gap: 0.55rem;
  margin-bottom: 0.45rem;
}
.cap {
  flex: 1;
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 999px;
  padding: 0.72rem 0.8rem;
  font-size: 1.15rem;
  font-weight: 700;
  text-align: center;
}
.cap:disabled { opacity: 0.4; cursor: default; }
.cap.bien {
  background: var(--ok-bg);
  color: var(--ok);
  border-color: var(--ok);
}
.cap.falla {
  background: var(--fail-bg);
  color: var(--fail);
  border-color: var(--fail);
}
.cap:active:not(:disabled) { transform: scale(0.98); opacity: 0.9; }
.status {
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.4;
  min-height: 2.6em;
}
.status strong { color: var(--ink); font-weight: 600; }
.status .ok { color: var(--ok); }
.status .bad { color: var(--fail); }
.flash {
  position: fixed;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.82);
  color: #fff;
  padding: 0.85rem 1.4rem;
  border-radius: 12px;
  font-size: 1.25rem;
  font-weight: 700;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
  z-index: 40;
}
.flash.show { opacity: 1; }
.spinner-overlay {
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.55);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 5;
  border-radius: 0;
}
.sheet-area { position: relative; }
.spinner-overlay.on { display: flex; }
.spinner {
  width: 36px; height: 36px;
  border: 3px solid #ddd;
  border-top-color: var(--spin);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media print {
  header.bar, .controls, .flash, .spinner-overlay { display: none !important; }
  .app { display: block; height: auto; overflow: visible; }
  .sheet-area { padding: 0; container-type: normal; display: block; }
  #preview {
    border: none; box-shadow: none;
    width: 297mm; height: 210mm;
    max-width: none; max-height: none;
  }
  @page { size: A4 landscape; margin: 0; }
}

/* ===== Traza unificada: niveles + pantalla de inicio ===== */
body.nivel-facil   { --bg: #eef7f1; --btn: #111111; --accent: #bfe8cf; }
body.nivel-medio   { --bg: #e4eaf0; --btn: #1a3a5c; --ink: #142433; --accent: #bcd9f2; }
body.nivel-dificil { --bg: #f1ecf7; --btn: #4b2a66; --accent: #d9c8f0; }
header.bar { align-items: center; }
body[class*="nivel-"] header.bar { background: var(--accent); }
a.back {
  display: inline-flex; align-items: center; gap: 0.3rem;
  text-decoration: none; color: #111; font-weight: 700; font-size: 1.05rem;
  background: #fff; border: 2px solid #111; border-radius: 999px;
  padding: 0.45rem 1rem; margin-right: 0.4rem;
}
a.back:active { transform: scale(0.97); }

body.home { background: #f7f5f0; overflow: auto; }
.home-wrap {
  min-height: 100%; box-sizing: border-box;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 2rem 2.2rem calc(2rem + env(safe-area-inset-bottom));
}
.home-wrap h1 { font-size: 2.6rem; margin: 0 0 0.3rem; letter-spacing: 0.04em; }
.home-wrap p.sub { margin: 0 0 1.8rem; color: #666; font-size: 1.1rem; }
.levels {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1.6rem;
  width: min(100%, 980px);
}
.level {
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  min-height: 230px; border-radius: 28px; text-decoration: none; color: #1b1b1b;
  border: 3px solid rgba(0,0,0,0.12); box-shadow: 0 6px 18px rgba(0,0,0,0.08);
  transition: transform 0.1s;
}
.level:active { transform: scale(0.98); }
.level .ico { font-size: 3.2rem; line-height: 1; margin-bottom: 0.5rem; }
.level .name { font-size: 2.4rem; font-weight: 800; }
.level .info { font-size: 1.05rem; color: #444; margin-top: 0.35rem; }
.level.facil   { background: #bfe8cf; }
.level.medio   { background: #bcd9f2; }
.level.dificil { background: #d9c8f0; }
.level.experto {
  background: #e4e4e4; color: #9a9a9a; border-style: dashed; box-shadow: none;
  cursor: not-allowed; filter: grayscale(1);
}
.level.experto .info { color: #8a8a8a; font-weight: 700; }
