:root {
  --naranja: #F26B1D;
  --oscuro: #1C1C1E;
  --oscuro-2: #2C2C2E;
  --texto: #1A1A1A;
  --texto-suave: #6B6B6B;
  --fondo: #FFFFFF;
  --fondo-2: #F2F2F4;
  --linea: #D9D9DE;
  --marca: rgba(74, 144, 226, 0.30);
  --seleccion: #F26B1D;
  --verde: #2E9E3F;
  --rojo: #D9534F;
  --ancho-ej: 150px;
  --ancho-col: 52px;
  --ancho-num: 30px;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--fondo);
  color: var(--texto);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  flex-direction: column;
  /* --alto lo ajusta app.js al alto visible (sin el teclado) */
  height: var(--alto, 100dvh);
  overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom);
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  background: var(--fondo-2);
  color: var(--texto);
  padding: 0 8px;
  min-height: 44px;
}
button:active { filter: brightness(0.9); }
button:disabled { opacity: 0.4; cursor: default; }

/* ---------- Barra superior ---------- */
.barra {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 6px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--oscuro);
  color: #fff;
}
.barra .icono {
  background: transparent;
  color: #fff;
  min-width: 40px;
  font-size: 18px;
}
.fecha {
  position: relative;
  font-size: 20px;
  font-weight: 600;
  padding: 0 4px;
  white-space: nowrap;
}
.fecha input {
  position: absolute;
  inset: 0;
  opacity: 0;
  width: 100%;
  cursor: pointer;
}
#selUsuario {
  flex: 1;
  min-width: 0;
  height: 40px;
  font: inherit;
  font-size: 16px;
  border-radius: 8px;
  border: none;
  padding: 0 8px;
  background: var(--oscuro-2);
  color: #fff;
}

.titulo {
  padding: 6px 10px;
  font-size: 20px;
  font-weight: 700;
  border-bottom: 1px solid var(--linea);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Grilla ---------- */
.cont-grilla {
  flex: 1;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.grilla {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  font-size: 14px;
}
.grilla th, .grilla td {
  border-right: 1px solid var(--linea);
  border-bottom: 1px solid var(--linea);
  height: 34px;
  padding: 0 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background-clip: padding-box;
}
.grilla th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--fondo-2);
  font-weight: 600;
  text-align: left;
}
.grilla td { background: var(--fondo); }
.grilla .num {
  width: var(--ancho-num);
  min-width: var(--ancho-num);
  max-width: var(--ancho-num);
  text-align: center;
  color: var(--texto-suave);
  font-size: 12px;
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--fondo-2);
}
.grilla th.num { z-index: 3; }
.grilla .ej {
  width: var(--ancho-ej);
  min-width: var(--ancho-ej);
  max-width: var(--ancho-ej);
  position: sticky;
  left: var(--ancho-num);
  z-index: 1;
}
.grilla th.ej { z-index: 3; }
.grilla .col {
  width: var(--ancho-col);
  min-width: var(--ancho-col);
  max-width: var(--ancho-col);
  text-align: center;
}
/* Columna ancha: el ejercicio se lee completo en varias líneas */
body.ancha { --ancho-ej: 68vw; }
body.ancha .grilla td.ej {
  white-space: normal;
  overflow-wrap: anywhere;
  height: auto;
  min-height: 34px;
  padding-top: 4px;
  padding-bottom: 4px;
}

.grilla tr.marcada td { box-shadow: inset 0 0 0 999px var(--marca); }
.grilla tr.fila-actual td.num { background: var(--naranja); color: #fff; }
.grilla td.sel { outline: 3px solid var(--seleccion); outline-offset: -3px; }
.grilla tr.titulo-seccion td.ej { font-weight: 700; }

/* ---------- Editor ---------- */
.editor {
  border-top: 1px solid var(--linea);
  padding: 6px 8px;
  background: var(--fondo-2);
}
.editor-etiqueta {
  font-size: 12px;
  color: var(--texto-suave);
  margin-bottom: 4px;
}
#editor {
  width: 100%;
  font: inherit;
  font-size: 17px;
  border: 1px solid var(--linea);
  border-radius: 8px;
  padding: 6px 8px;
  resize: none;
  background: #fff;
  color: var(--texto);
}

/* Mientras se escribe (teclado abierto) se esconde lo que no hace falta,
   así la grilla y el cuadro de texto quedan visibles arriba del teclado */
body.escribiendo .botonera,
body.escribiendo .titulo,
body.escribiendo .estado-nube { display: none; }
body.escribiendo .editor { border-top: 2px solid var(--naranja); }
.editor-fila { display: flex; gap: 6px; align-items: stretch; }
.editor-fila textarea { flex: 1; }
.listo { display: none; background: var(--naranja); color: #fff; font-weight: 700; padding: 0 14px; }
body.escribiendo .listo { display: block; }

/* ---------- Botonera ---------- */
.botonera {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 6px;
  background: var(--oscuro);
}
.botonera button {
  font-size: 13px;
  padding: 0 2px;
  background: var(--oscuro-2);
  color: #fff;
}
.botonera button.activo { background: #4A90E2; }
.botonera button.destacado { background: var(--naranja); }

/* ---------- Paleta y menú ---------- */
.paleta, .menu {
  position: fixed;
  z-index: 20;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0,0,0,.3);
  padding: 8px;
}
.paleta {
  left: 8px; right: 8px; bottom: 120px;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.paleta[hidden], .menu[hidden] { display: none; }
.paleta button {
  min-height: 48px;
  border: 1px solid var(--linea);
  font-size: 12px;
}
.menu {
  top: 56px; right: 8px;
  display: flex;
  flex-direction: column;
  min-width: 240px;
}
.menu button {
  text-align: left;
  background: transparent;
  padding: 0 12px;
}
.menu button[hidden] { display: none; }
.menu button.cuenta {
  font-weight: 600;
  color: var(--naranja);
  border-bottom: 1px solid var(--linea);
  border-radius: 0;
  white-space: normal;
  text-align: left;
}
.estado-nube {
  font-size: 12px;
  color: var(--texto-suave);
  padding: 2px 10px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-bottom: 1px solid var(--linea);
}
.estado-nube[hidden] { display: none; }

/* ---------- Diálogos ---------- */
dialog {
  border: none;
  border-radius: 14px;
  padding: 16px;
  width: min(92vw, 420px);
  box-shadow: 0 10px 40px rgba(0,0,0,.35);
}
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin: 0 0 12px; font-size: 20px; }
dialog input {
  width: 100%;
  font: inherit;
  font-size: 18px;
  padding: 10px;
  border: 1px solid var(--linea);
  border-radius: 8px;
}
.dlg-botones {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 14px;
}
.dlg-botones button { background: var(--naranja); color: #fff; padding: 0 18px; }
.dlg-botones button.secundario { background: var(--fondo-2); color: var(--texto); }
.ayuda { padding-left: 18px; margin: 0; line-height: 1.45; font-size: 15px; }
.ayuda li { margin-bottom: 8px; }

/* ---------- Temporizador ---------- */
.timer {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: #000;
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  text-align: center;
}
.timer[hidden] { display: none; }
.timer-salir {
  align-self: flex-end;
  background: transparent;
  color: #aaa;
  font-size: 16px;
}
.timer-titulo {
  font-size: 30px;
  font-weight: 700;
  color: #7FFF00;
  overflow-wrap: anywhere;
}
.timer-numero {
  font-size: min(38vw, 220px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.timer-siguiente { font-size: 18px; color: #bbb; min-height: 1.4em; }
.timer-accion {
  width: 100%;
  min-height: 110px;
  font-size: 40px;
  font-weight: 800;
  border-radius: 18px;
  color: #fff;
  background: var(--verde);
}
.timer-accion.cancelar { background: var(--rojo); }

/* ---------- Rutina recibida ---------- */
.recibido {
  position: fixed;
  left: 8px; right: 8px;
  bottom: 120px;
  z-index: 30;
  background: #fff;
  border: 2px solid var(--naranja);
  border-radius: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,.3);
  padding: 12px;
  font-size: 15px;
}
.recibido[hidden] { display: none; }
.recibido-botones { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
.recibido-botones button { background: var(--naranja); color: #fff; font-weight: 700; padding: 0 18px; }
.recibido-botones button.secundario { background: var(--fondo-2); color: var(--texto); font-weight: 400; }

/* ---------- Aviso ---------- */
.aviso {
  position: fixed;
  left: 50%;
  bottom: 140px;
  transform: translateX(-50%);
  z-index: 40;
  background: rgba(28,28,30,.95);
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  max-width: 90vw;
  font-size: 15px;
  text-align: center;
}
.aviso[hidden] { display: none; }

@media (min-width: 700px) {
  :root { --ancho-ej: 260px; --ancho-col: 64px; }
  .botonera { grid-template-columns: repeat(10, 1fr); }
}
