: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.62);
  display: none;
  flex-direction: column;
  gap: 14px;
  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); } }
.spinner-overlay .spinner { width: 54px; height: 54px; border-width: 5px; }
.spin-label {
  background: #fff;
  color: var(--btn, #111);
  border: 2px solid var(--btn, #111);
  border-radius: 12px;
  padding: 0.55rem 1.1rem;
  font-size: 1.2rem;
  font-weight: 700;
  box-shadow: 0 2px 10px rgba(0,0,0,0.12);
}
@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; }
body.nivel-experto { --bg: #f7efe6; --btn: #7a3a10; --accent: #f2d2b6; }
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: #f2d2b6; }

/* ===== v13: capa de trazado ===== */
body.trazado { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; overscroll-behavior: none; }
body.trazado .sheet-area { touch-action: none; }
#preview { position: relative; }
#trace {
  position: absolute; left: 0; top: 0;
  touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  z-index: 3; cursor: crosshair;
}
.trace-stats { margin-left: auto; display: flex; gap: 0.9rem; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.trace-stats span { background: #fff; border: 2px solid rgba(0,0,0,0.25); border-radius: 999px; padding: 0.25rem 0.8rem; }
.trace-stats .ts-h b { color: var(--fail); }
.row-gen button.tool {
  flex: 0 0 auto; cursor: pointer; border: 2px solid var(--btn, #111); border-radius: 14px;
  padding: 0.95rem 1.05rem; font-size: 1.15rem; font-weight: 700; background: #fff; color: var(--btn, #111);
  min-width: 7.2rem;
}
.row-gen button.tool.strong { background: var(--accent, #eee); }
.row-gen button.tool.icon { min-width: 4rem; font-size: 1.4rem; padding: 0.8rem 0.9rem; }
.row-gen button.tool:active { transform: scale(0.98); opacity: 0.9; }
.tmodal { position: fixed; inset: 0; background: rgba(0,0,0,0.35); display: none; align-items: center; justify-content: center; z-index: 50; }
.tmodal.on { display: flex; }
.tbox { background: #fff; border-radius: 20px; padding: 1.3rem 1.6rem 1.2rem; min-width: min(92vw, 520px); box-shadow: 0 10px 40px rgba(0,0,0,0.25); }
.tbox h2 { margin: 0 0 0.9rem; font-size: 1.5rem; }
.tbox .sum { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.8rem; margin-bottom: 1rem; }
.tbox .sum div { background: #f4f4f4; border-radius: 14px; padding: 0.7rem; text-align: center; }
.tbox .sum span { display: block; color: #555; font-size: 0.95rem; }
.tbox .sum b { display: block; font-size: 2rem; margin-top: 0.2rem; }
.tbox .trow { display: flex; gap: 0.6rem; margin-top: 0.6rem; }
.tbox .tbtn { flex: 1; font-size: 1.15rem; font-weight: 700; padding: 0.8rem; border-radius: 14px; border: 2px solid #111; background: #fff; cursor: pointer; }
.tbox .srow { display: flex; align-items: center; gap: 0.6rem; margin: 0.6rem 0; }
.tbox .srow span { width: 7rem; font-weight: 700; font-size: 1.1rem; }
.tbox .seg { flex: 1; font-size: 1.1rem; padding: 0.8rem; border-radius: 12px; border: 2px solid #999; background: #fff; cursor: pointer; }
.tbox .seg.sel { border-color: #111; background: #111; color: #fff; font-weight: 700; }
.tbox .snote { color: #555; font-size: 0.9rem; margin: 0.6rem 0 0.2rem; }
@media print { #trace, .trace-stats, .tmodal { display: none !important; } }

/* ===== v14: pantalla completa, control de salida, Historial ===== */
.row-gen button.tool.fs { min-width: 11rem; }
@media (max-width: 1100px) {
  .row-gen button.tool { min-width: 6.2rem; padding: 0.95rem 0.7rem; font-size: 1.05rem; }
  .row-gen button.tool.fs { min-width: 9.6rem; }
}
body.immersive header.bar, body.immersive .controls, body.immersive .flash { display: none !important; }
body.immersive .sheet-area {
  position: fixed; inset: 0; z-index: 30; padding: 0; margin: 0;
  background: #000; display: flex; align-items: center; justify-content: center;
  touch-action: none; overscroll-behavior: none;
}
body.immersive #preview { border: none; box-shadow: none; }
body.immersive .app { height: 100%; }
.fs-exit {
  position: fixed; top: 10px; right: 10px; width: 84px; height: 84px; z-index: 45;
  display: none; align-items: center; justify-content: center; border-radius: 50%;
  background: rgba(0,0,0,0.55); color: #fff; opacity: 0; transition: opacity 0.35s;
  touch-action: none; -webkit-user-select: none; user-select: none;
}
body.immersive .fs-exit { display: flex; pointer-events: none; }
body.immersive .fs-exit.on { opacity: 1; pointer-events: auto; }
.fs-exit svg { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.fs-exit circle { fill: none; stroke-width: 6; }
.fs-exit circle.bg { stroke: rgba(255,255,255,0.25); }
.fs-exit circle.ring { stroke: #fff; stroke-dasharray: 213.6; stroke-dashoffset: 213.6; stroke-linecap: round; }
.fs-exit span { position: relative; font-weight: 800; font-size: 1rem; text-align: center; line-height: 1.05; }
.fs-exit small { display: block; font-weight: 500; font-size: 0.56rem; letter-spacing: -0.02em; opacity: 0.85; }
.kidsel, .kidname { flex: 1; font-size: 1.05rem; padding: 0.6rem; border-radius: 10px; border: 2px solid #999; background: #fff; }
.kidname { -webkit-user-select: text; user-select: text; }
a.hist-link {
  margin-top: 1.6rem; display: inline-block; text-decoration: none; color: #111; font-weight: 800; font-size: 1.3rem;
  background: #fff; border: 3px solid rgba(0,0,0,0.15); border-radius: 18px; padding: 0.9rem 1.8rem;
}
body.historial { background: #f7f5f0; height: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch; }
body.historial header.bar { position: sticky; top: 0; z-index: 2; }
.hist-wrap { padding: 0.9rem 1.1rem 2rem; }
.hist-filters { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; margin-bottom: 0.9rem; }
.hist-filters label { font-weight: 700; font-size: 1.05rem; display: flex; gap: 0.5rem; align-items: center; }
.hist-filters select { font-size: 1.05rem; padding: 0.55rem 0.7rem; border-radius: 10px; border: 2px solid #999; background: #fff; }
.hbtn { font-size: 1.05rem; font-weight: 700; padding: 0.7rem 1.2rem; border-radius: 12px; border: 2px solid #111; background: #fff; cursor: pointer; margin-left: auto; }
.charts { display: grid; grid-template-columns: 1fr; gap: 0.9rem; margin-bottom: 1rem; }
.charts figure.chart { max-width: 900px; }
figure.chart { margin: 0; background: #fff; border-radius: 16px; padding: 0.7rem 0.8rem 0.3rem; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
figure.chart figcaption { font-size: 0.95rem; color: #333; margin-bottom: 0.2rem; }
figure.chart .leg { display: block; color: #555; font-size: 0.85rem; margin-top: 0.15rem; }
figure.chart .leg i { display: inline-block; width: 18px; height: 4px; vertical-align: middle; margin: 0 0.25rem 0 0.7rem; border-radius: 2px; }
figure.chart .leg i.lt { background: #1565c0; } figure.chart .leg i.lh { background: #d32f2f; }
figure.chart .leg i.sq { width: 11px; height: 11px; border-radius: 3px; } figure.chart .leg i.sq.bien { background: #2e7d32; } figure.chart .leg i.sq.falla { background: #c62828; }
figure.chart svg { width: 100%; height: auto; display: block; }
figure.chart .grid { stroke: #e6e6e6; stroke-width: 1; }
figure.chart .ax { font-size: 16px; fill: #555; } figure.chart .ax.t { fill: #1565c0; } figure.chart .ax.h { fill: #d32f2f; }
figure.chart .lt { fill: none; stroke: #1565c0; stroke-width: 3; } figure.chart .lh { fill: none; stroke: #d32f2f; stroke-width: 3; stroke-dasharray: 7 5; }
figure.chart .dt { fill: #1565c0; } figure.chart .dh { fill: #d32f2f; }
figure.chart .vv.bien { fill: #2e7d32; } figure.chart .vv.falla { fill: #c62828; } figure.chart .vv.none { fill: #ccc; }
.hist-list table { width: 100%; border-collapse: collapse; background: #fff; border-radius: 14px; overflow: hidden; font-size: 0.98rem; }
.hist-list th, .hist-list td { padding: 0.55rem 0.6rem; border-bottom: 1px solid #eee; text-align: left; vertical-align: top; }
.hist-list th { background: #eee; font-size: 0.9rem; }
.hist-list td.fic small { color: #666; }
.hist-list .del { font-size: 0.95rem; padding: 0.5rem 0.8rem; border-radius: 10px; border: 2px solid #b01a1a; color: #b01a1a; background: #fff; cursor: pointer; }
.v-bien { color: #1a7a3a; font-weight: 700; } .v-falla { color: #b01a1a; font-weight: 700; } .v-none { color: #888; }
.empty { color: #666; font-size: 1rem; padding: 0.6rem 0.2rem; }
