:root {
  color-scheme: light dark;
  --fondo: #f2f5f9;
  --papel: #ffffff;
  --papel-alt: #f8fafc;
  --tinta: #142230;
  --suave: #566676;
  --linea: #dce3eb;
  --barra: #14304b;
  --accion: #0e5ea3;
  --accion-suave: #e6f1fb;
  --accion-tinta: #ffffff;
  --sel: #e1effb;
  --baja: #b42318;
  --baja-bg: #fef3f2;
  --bien: #157347;
  --bien-bg: #ecfdf3;
  --aviso: #fff6dc;
  --aviso-tinta: #6a4b00;
  --sombra: 0 1px 3px rgb(16 24 40 / .06), 0 1px 2px rgb(16 24 40 / .04);
  --radio: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --fondo: #0e151d;
    --papel: #17212b;
    --papel-alt: #1d2935;
    --tinta: #edf2f7;
    --suave: #9eb0c2;
    --linea: #2a3947;
    --barra: #0b1926;
    --accion: #6db8f7;
    --accion-suave: #193046;
    --accion-tinta: #071e36;
    --sel: #1f3952;
    --baja: #ff9a8d;
    --baja-bg: #3b1c1a;
    --bien: #7cdba1;
    --bien-bg: #153324;
    --aviso: #3e3216;
    --aviso-tinta: #ffe19c;
    --sombra: 0 1px 3px rgb(0 0 0 / .35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--fondo);
  color: var(--tinta);
  font: 16px/1.42 system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
  overscroll-behavior-y: contain;
}
button, input, select { font: inherit; color: inherit; }
button, a { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 3px solid var(--accion); outline-offset: 2px; }
h2 {
  font-size: .95rem;
  font-weight: 700;
  letter-spacing: .01em;
  margin: 22px 0 10px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}
h2 small, .suave { color: var(--suave); font-weight: 400; font-size: .875rem; }
p { margin: 8px 0; }

/* Barra superior */
#barra {
  position: sticky;
  top: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 4px;
  min-height: 60px;
  padding: env(safe-area-inset-top) 8px 0;
  background: var(--barra);
  color: #fff;
  box-shadow: 0 2px 8px rgb(0 0 0 / .16);
}
#barra h1 { flex: 1; min-width: 0; margin: 0; padding: 8px 6px; font-size: 1.0625rem; line-height: 1.2; }
#barra h1 span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#barra h1 small { display: block; font-weight: 400; font-size: .8125rem; color: #c4d5e6; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#barra a, #barra button.icono {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 8px;
  border: 0;
  background: none;
  color: #fff;
  text-decoration: none;
  border-radius: 10px;
  cursor: pointer;
}
#barra a:active, #barra button.icono:active { background: rgb(255 255 255 / .14); }
#barra svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Indicador de sincronización en barra */
.sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 34px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: .78rem;
  font-weight: 650;
  white-space: nowrap;
  border: 1px solid rgb(255 255 255 / .22);
  background: rgb(255 255 255 / .12);
  color: #fff;
  text-decoration: none;
  cursor: pointer;
}
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: #4ade80; flex-shrink: 0; }
.sync-pill.pend { background: rgb(251 191 36 / .2); border-color: rgb(251 191 36 / .5); color: #fef08a; }
.sync-pill.pend .dot { background: #fbbf24; }
.sync-pill.err { background: rgb(248 113 113 / .25); border-color: rgb(248 113 113 / .55); color: #fecaca; }
.sync-pill.err .dot { background: #f87171; }
.sync-pill.off { background: rgb(148 163 184 / .2); border-color: rgb(148 163 184 / .4); color: #cbd5e1; }
.sync-pill.off .dot { background: #94a3b8; }

/* Contenedor principal */
#vista { max-width: 680px; margin: 0 auto; padding: 14px 14px calc(36px + env(safe-area-inset-bottom)); }
body.teclado #vista { padding-bottom: 470px; }

/* Tarjetas de resumen y KPI */
.resumen-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.kpi {
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 10px 12px;
  box-shadow: var(--sombra);
}
.kpi b { display: block; font-size: 1.2rem; font-variant-numeric: tabular-nums; line-height: 1.2; }
.kpi small { display: block; color: var(--suave); font-size: .76rem; margin-top: 2px; }
.kpi.bien-kpi b { color: var(--bien); }
.kpi.baja-kpi b { color: var(--baja); }

/* Buscador y filtros */
.herramientas {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
  align-items: center;
}
.buscador {
  position: relative;
  flex: 1 1 180px;
  display: flex;
  align-items: center;
}
.buscador input {
  width: 100%;
  min-height: 44px;
  padding: 8px 38px 8px 36px;
  border: 1px solid var(--linea);
  border-radius: 12px;
  background: var(--papel);
  box-shadow: var(--sombra);
}
.buscador input:focus { border-color: var(--accion); outline: none; box-shadow: 0 0 0 3px var(--accion-suave); }
.buscador .lupa {
  position: absolute;
  left: 11px;
  width: 18px;
  height: 18px;
  color: var(--suave);
  pointer-events: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.buscador .limpiar {
  position: absolute;
  right: 4px;
  width: 36px;
  height: 36px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--suave);
  font-size: 1.2rem;
  cursor: pointer;
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 6px 13px;
  border: 1px solid var(--linea);
  border-radius: 12px;
  background: var(--papel);
  color: var(--tinta);
  font-size: .875rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: var(--sombra);
}
.chip[aria-pressed="true"] {
  background: var(--accion);
  border-color: var(--accion);
  color: var(--accion-tinta);
}

/* Barra de progreso */
.progreso-box {
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  padding: 10px 14px;
  margin-bottom: 12px;
  box-shadow: var(--sombra);
}
.progreso-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: .85rem;
  margin-bottom: 6px;
}
.progreso-top b { font-variant-numeric: tabular-nums; }
progress.barra-prog {
  display: block;
  width: 100%;
  height: 8px;
  border: 0;
  border-radius: 999px;
  background: var(--linea);
  overflow: hidden;
  appearance: none;
  accent-color: var(--accion);
}
progress.barra-prog::-webkit-progress-bar { background: var(--linea); border-radius: 999px; }
progress.barra-prog::-webkit-progress-value { background: var(--accion); border-radius: 999px; transition: width .2s ease; }
progress.barra-prog::-moz-progress-bar { background: var(--accion); border-radius: 999px; }
progress.barra-prog.completa { accent-color: var(--bien); }
progress.barra-prog.completa::-webkit-progress-value { background: var(--bien); }
progress.barra-prog.completa::-moz-progress-bar { background: var(--bien); }

/* Listas y filas */
.lista {
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  overflow: hidden;
  box-shadow: var(--sombra);
}
.fila {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 58px;
  padding: 10px 14px;
  border: 0;
  border-top: 1px solid var(--linea);
  background: none;
  color: inherit;
  text-align: left;
  text-decoration: none;
}
.fila:first-child { border-top: 0; }
button.fila, a.fila { cursor: pointer; }
.fila:active, .fila.sel { background: var(--sel); }
.fila .n { min-width: 24px; color: var(--suave); font-size: .85rem; text-align: right; font-variant-numeric: tabular-nums; }
.fila .txt { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.fila .txt small { display: block; color: var(--suave); font-size: .84rem; margin-top: 2px; }
.fila .val { font-weight: 650; font-size: 1.125rem; font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.fila .val small { display: block; font-weight: 400; font-size: .78rem; color: var(--suave); }
.fila progress.mini-prog { display: block; height: 5px; width: 72px; margin-top: 5px; margin-left: auto; border: 0; border-radius: 999px; background: var(--linea); overflow: hidden; appearance: none; accent-color: var(--accion); }
.fila progress.mini-prog::-webkit-progress-bar { background: var(--linea); border-radius: 999px; }
.fila progress.mini-prog::-webkit-progress-value { background: var(--accion); border-radius: 999px; }
.fila progress.mini-prog::-moz-progress-bar { background: var(--accion); border-radius: 999px; }
.fila progress.mini-prog.ok { accent-color: var(--bien); }
.fila progress.mini-prog.ok::-webkit-progress-value { background: var(--bien); }
.fila progress.mini-prog.ok::-moz-progress-bar { background: var(--bien); }

.baja { color: var(--baja); }
.bien { color: var(--bien); }
.falta { color: var(--suave); font-weight: 400; }
.tag {
  display: inline-block;
  padding: 2px 7px;
  border-radius: 6px;
  font-size: .75rem;
  font-weight: 650;
  background: var(--accion-suave);
  color: var(--accion);
  margin-left: 6px;
  vertical-align: middle;
}
.tag.baja { background: var(--baja-bg); color: var(--baja); }
.tag.bien { background: var(--bien-bg); color: var(--bien); }

/* Cuadrícula de cursos */
.cursos { display: grid; grid-template-columns: repeat(auto-fill, minmax(152px, 1fr)); gap: 10px; }
.cursos a {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 14px;
  min-height: 82px;
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  color: inherit;
  text-decoration: none;
  box-shadow: var(--sombra);
  transition: transform .1s ease, border-color .15s ease;
}
.cursos a:active { background: var(--sel); transform: scale(.985); }
.cursos b { display: block; font-size: 1.15rem; margin-bottom: 4px; }

/* Pestañas */
.pestanas {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  margin-bottom: 14px;
  padding: 4px;
  gap: 4px;
  background: var(--papel);
  border: 1px solid var(--linea);
  border-radius: var(--radio);
  box-shadow: var(--sombra);
}
.pestanas a {
  display: grid;
  place-items: center;
  min-height: 42px;
  border-radius: 10px;
  color: var(--suave);
  font-weight: 600;
  text-decoration: none;
}
.pestanas a[aria-current] { background: var(--accion); color: var(--accion-tinta); font-weight: 700; }

.nota-info {
  padding: 12px 14px;
  border-radius: var(--radio);
  background: var(--aviso);
  color: var(--aviso-tinta);
  font-size: .92rem;
  margin-bottom: 12px;
}
.nota-info a { color: inherit; font-weight: 700; }

/* Botones */
.boton {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 46px;
  padding: 8px 16px;
  border: 1px solid var(--accion);
  border-radius: 11px;
  background: var(--papel);
  color: var(--accion);
  font-weight: 650;
  text-decoration: none;
  cursor: pointer;
}
.boton.lleno { background: var(--accion); color: var(--accion-tinta); }
.boton.peligro { border-color: var(--baja); color: var(--baja); }
.boton:disabled { opacity: .5; cursor: not-allowed; }
.botones { display: flex; flex-wrap: wrap; gap: 10px; margin: 14px 0; }
.vacio { padding: 28px 12px; text-align: center; color: var(--suave); }

/* Formularios y diálogos */
label { display: block; margin: 14px 0; font-weight: 600; font-size: .92rem; }
label input, label select {
  display: block;
  width: 100%;
  min-height: 46px;
  margin-top: 6px;
  padding: 8px 12px;
  border: 1px solid var(--linea);
  border-radius: 11px;
  background: var(--papel-alt);
  font-weight: 400;
}
label input:focus, label select:focus { border-color: var(--accion); outline: none; box-shadow: 0 0 0 3px var(--accion-suave); }
dialog {
  width: min(440px, calc(100vw - 28px));
  padding: 20px;
  border: 1px solid var(--linea);
  border-radius: 18px;
  background: var(--papel);
  color: var(--tinta);
  box-shadow: 0 20px 50px rgb(0 0 0 / .28);
}
dialog::backdrop { background: rgb(0 0 0 / .52); }
dialog h2 { margin: 0 0 8px; font-size: 1.15rem; }
dialog .error { color: var(--baja); font-weight: 600; }

/* Teclado numérico profesional */
#teclado {
  position: fixed;
  inset: auto 0 0;
  z-index: 5;
  max-width: 680px;
  margin: 0 auto;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  background: var(--papel);
  border-top: 1px solid var(--linea);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 28px rgb(0 0 0 / .2);
}
.pantalla { display: flex; align-items: center; gap: 8px; min-height: 52px; padding: 0 4px 6px; }
.pantalla div { flex: 1; min-width: 0; }
.pantalla b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: .98rem; }
.pantalla output { min-width: 76px; font-size: 1.9rem; font-weight: 750; line-height: 1; text-align: right; font-variant-numeric: tabular-nums; }
.rapidas, .marcas, .teclas { display: grid; gap: 6px; }
.rapidas { grid-auto-flow: column; grid-auto-columns: 1fr; margin-bottom: 6px; }
.marcas { grid-auto-flow: column; grid-auto-columns: 1fr; margin-bottom: 6px; }
.teclas { grid-template-columns: repeat(4, 1fr); }
#teclado button {
  min-height: 50px;
  border: 1px solid var(--linea);
  border-radius: 11px;
  background: var(--fondo);
  font-size: 1.32rem;
  font-weight: 650;
  cursor: pointer;
  user-select: none;
}
#teclado .rapidas button {
  min-height: 38px;
  font-size: .9rem;
  background: var(--accion-suave);
  border-color: transparent;
  color: var(--accion);
}
#teclado .rapidas button.ultima {
  background: var(--bien-bg);
  color: var(--bien);
}
#teclado .marcas button { min-height: 42px; font-size: .98rem; }
#teclado button:active, #teclado button.sel { background: var(--sel); }
#teclado .chica { font-size: .95rem; }
#teclado .sig {
  grid-row: span 2;
  border-color: var(--accion);
  background: var(--accion);
  color: var(--accion-tinta);
  font-size: 1rem;
}
#teclado .nav-k {
  min-height: 40px;
  min-width: 40px;
  padding: 0 6px;
  border: 1px solid var(--linea);
  border-radius: 10px;
  background: var(--fondo);
  font-size: 1rem;
  color: var(--tinta);
}
#teclado .cerrar { min-height: 40px; min-width: 52px; border: 0; background: none; font-size: .95rem; color: var(--accion); font-weight: 700; }

/* Avisos flotantes */
#aviso {
  position: fixed;
  z-index: 6;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100vw - 28px);
  padding: 11px 16px;
  border-radius: 12px;
  background: var(--tinta);
  color: var(--fondo);
  font-size: .92rem;
  box-shadow: 0 10px 26px rgb(0 0 0 / .24);
}
#aviso.error { background: #b42318; color: #fff; }
#aviso button { min-height: 40px; margin-left: 10px; border: 0; background: none; color: inherit; font-weight: 700; text-decoration: underline; cursor: pointer; }

/* Tabla anual */
.tabla { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th, td { padding: 10px 8px; border-top: 1px solid var(--linea); text-align: right; white-space: nowrap; }
thead th { border-top: 0; color: var(--suave); font-size: .8rem; background: var(--papel-alt); }
th:first-child { position: sticky; left: 0; min-width: 128px; max-width: 155px; background: var(--papel); text-align: left; white-space: normal; font-weight: 550; font-size: .875rem; line-height: 1.25; }
thead th:first-child { background: var(--papel-alt); }
th a { display: flex; align-items: center; min-height: 44px; color: inherit; text-decoration: none; }
td small { display: block; color: var(--suave); font-size: .75rem; }
td button { min-height: 42px; min-width: 60px; border: 1px solid var(--accion); border-radius: 9px; background: none; color: var(--accion); font-weight: 650; cursor: pointer; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
