:root {
  --bg: #f5f6f8; --panel: #ffffff; --borde: #e3e6eb; --borde-fuerte: #cfd4dc;
  --texto: #1d2330; --suave: #6b7385; --primario: #ff9900; --primario-osc: #e68a00;
  --azul: #146eb4; --verde: #0f8a4b; --verde-bg: #e3f5ea; --amarillo: #9a6b00; --amarillo-bg: #fff4d6;
  --rojo: #c4312b; --rojo-bg: #fde8e7; --gris-bg: #eef0f3; --calc-bg: #f8fafc; --foco: #2563eb;
  --sombra: 0 6px 24px rgba(20, 30, 50, .14);
  --fila: 64px;
}
* { box-sizing: border-box; }
body { margin: 0; font: 13px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--texto); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ── Login ── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #232f3e, #37475a); }
.login-caja { width: min(380px, 100%); background: var(--panel); border-radius: 14px; padding: 28px; box-shadow: var(--sombra); display: grid; gap: 12px; }
.login-caja .logo { font-size: 38px; text-align: center; }
.login-caja h1 { margin: 0; text-align: center; font-size: 20px; }
.login-caja .sub { margin: -6px 0 6px; text-align: center; color: var(--suave); }
.login-caja label { display: grid; gap: 4px; font-weight: 600; font-size: 12px; color: var(--suave); }
.login-caja input { padding: 10px 12px; border: 1px solid var(--borde-fuerte); border-radius: 8px; font-size: 14px; }
.error { color: var(--rojo); min-height: 1.2em; margin: 0; }

/* ── Botones ── */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border: 1px solid var(--borde-fuerte); border-radius: 8px; background: var(--panel); cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { background: var(--gris-bg); }
.btn.primario { background: var(--primario); border-color: var(--primario); color: #111; font-weight: 600; }
.btn.primario:hover { background: var(--primario-osc); }

/* ── Barra ── */
.barra { position: sticky; top: 0; z-index: 30; display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 10px 16px; background: #232f3e; color: #fff; }
.barra .titulo { font-weight: 700; font-size: 15px; margin-right: 6px; }
.barra input[type=search] { flex: 1 1 220px; max-width: 340px; padding: 7px 10px; border-radius: 8px; border: 0; color: var(--texto); }
.barra .acciones { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.barra .btn:not(.primario) { background: #37475a; border-color: #4a5b70; color: #fff; }
.barra .btn:not(.primario):hover { background: #485a70; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 4px 10px; border-radius: 999px; border: 1px solid #4a5b70; background: transparent; color: #d7dde6; cursor: pointer; font-size: 12px; }
.chip.activo { background: #fff; color: #232f3e; border-color: #fff; font-weight: 600; }
.chip .n { opacity: .7; margin-left: 4px; }
.menu-usuario { position: relative; }
.menu { position: absolute; z-index: 60; min-width: 190px; background: var(--panel); color: var(--texto); border: 1px solid var(--borde); border-radius: 10px; box-shadow: var(--sombra); padding: 6px; display: grid; }
.menu-usuario .menu { right: 0; top: calc(100% + 6px); }
.menu button { text-align: left; padding: 8px 10px; border: 0; background: none; border-radius: 6px; cursor: pointer; }
.menu button:hover { background: var(--gris-bg); }
.menu .peligro { color: var(--rojo); }
.menu .pista { padding: 6px 10px; color: var(--suave); font-size: 12px; border-top: 1px solid var(--borde); border-bottom: 1px solid var(--borde); margin: 4px 0; }
.menu-imagen { position: fixed; }
#menuFila { position: fixed; }

.sub-barra { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--borde); }
.sub-barra .etiqueta { color: var(--suave); }
.toggles { display: flex; flex-wrap: wrap; gap: 6px; }
.toggle { padding: 3px 9px; border-radius: 6px; border: 1px solid var(--borde-fuerte); background: var(--panel); cursor: pointer; font-size: 12px; }
.toggle.activo { background: #e8f1fb; border-color: #9cc2e8; color: var(--azul); font-weight: 600; }
.resumen { margin-left: auto; color: var(--suave); }
.resumen b { color: var(--texto); }

/* ── Tabla ── */
.tabla-wrap { overflow: auto; height: calc(100vh - 106px); }
table { border-collapse: separate; border-spacing: 0; min-width: 100%; background: var(--panel); }
thead th { position: sticky; z-index: 10; background: #f0f2f5; border-bottom: 1px solid var(--borde-fuerte); border-right: 1px solid var(--borde); text-align: left; font-weight: 600; white-space: nowrap; }
thead tr.grupos th { top: 0; height: 26px; padding: 4px 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--suave); }
thead tr.columnas th { top: 26px; height: 34px; padding: 6px 8px; cursor: pointer; user-select: none; }
thead tr.columnas th:hover { background: #e6e9ee; }
thead th .ayuda { color: var(--suave); font-weight: 400; margin-left: 3px; cursor: help; }
/* El nombre del grupo se queda visible al desplazarse (no lo tapan las fijas). */
thead tr.grupos th .grupo-txt { position: sticky; left: 310px; }
thead th.orden-asc::after { content: " ▲"; color: var(--azul); }
thead th.orden-desc::after { content: " ▼"; color: var(--azul); }
th.g-Resultados, td.calc { background: var(--calc-bg); }
thead tr.grupos th.g-Resultados { color: var(--verde); }

tbody td { border-bottom: 1px solid var(--borde); border-right: 1px solid var(--borde); height: var(--fila); padding: 0; vertical-align: middle; position: relative; }
tbody tr:hover td { background: #fbfcfe; }
tbody tr:hover td.calc { background: #f3f6fa; }
tbody tr.recien td { animation: brillo 1.6s ease-out; }
@keyframes brillo { from { background: #fff7e6; } }

/* columnas fijas a la izquierda */
.fija-0 { position: sticky !important; left: 0; z-index: 5; background: var(--panel); }
.fija-1 { position: sticky !important; left: 76px; z-index: 5; background: var(--panel); box-shadow: 2px 0 0 var(--borde-fuerte); }
thead .fija-0, thead .fija-1 { z-index: 15; background: #f0f2f5; }

/* celdas editables */
.celda { width: 100%; height: 100%; min-height: var(--fila); border: 0; background: transparent; padding: 6px 8px; outline: none; }
input.celda:hover, select.celda:hover, textarea.celda:hover { background: #f7f9fc; }
.celda:focus { background: #fff; box-shadow: inset 0 0 0 2px var(--foco); }
.celda.num { text-align: right; font-variant-numeric: tabular-nums; }
select.celda { cursor: pointer; appearance: none; background-image: linear-gradient(45deg, transparent 50%, #889 50%), linear-gradient(135deg, #889 50%, transparent 50%); background-position: calc(100% - 12px) 50%, calc(100% - 7px) 50%; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 20px; }
textarea.celda { resize: none; display: block; line-height: 1.35; overflow: hidden; }
textarea.celda:focus { position: absolute; inset: 0 auto auto 0; height: auto; min-height: 140px; z-index: 20; width: 340px; box-shadow: var(--sombra), inset 0 0 0 2px var(--foco); overflow: auto; }
td.guardando::after { content: ""; position: absolute; right: 4px; top: 4px; width: 7px; height: 7px; border-radius: 50%; background: var(--primario); }
td.guardado::after { content: "✓"; position: absolute; right: 4px; top: 1px; color: var(--verde); font-size: 11px; }
td.con-error { box-shadow: inset 0 0 0 2px var(--rojo); }

.url-celda { display: flex; align-items: center; height: 100%; }
.url-celda .celda { flex: 1; min-width: 0; color: var(--azul); }
.url-celda a { padding: 0 8px; text-decoration: none; font-size: 15px; color: var(--azul); }

.estrellas { display: flex; gap: 1px; padding: 0 8px; height: 100%; align-items: center; }
.estrellas button { border: 0; background: none; padding: 0 1px; font-size: 17px; cursor: pointer; color: #d5d9e0; line-height: 1; }
.estrellas button.on { color: #f5a623; }

.img-celda { width: 76px; height: var(--fila); display: grid; place-items: center; cursor: pointer; outline: none; }
.img-celda:focus { box-shadow: inset 0 0 0 2px var(--foco); }
.img-celda img { max-width: 64px; max-height: 56px; border-radius: 6px; object-fit: contain; background: #fff; }
.img-celda .sin { width: 56px; height: 52px; border: 1.5px dashed var(--borde-fuerte); border-radius: 8px; display: grid; place-items: center; color: var(--suave); font-size: 18px; }
.img-celda.cargando .sin, .img-celda.cargando img { opacity: .35; }
.img-celda.cargando::after { content: ""; position: absolute; width: 18px; height: 18px; border: 2px solid var(--primario); border-right-color: transparent; border-radius: 50%; animation: gira .8s linear infinite; }
@keyframes gira { to { transform: rotate(360deg); } }

.calc-valor { padding: 6px 8px; text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--verde); font-weight: 600; }
.neg { color: var(--rojo); font-weight: 600; }
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-weight: 600; font-size: 12px; }
.badge.verde { background: var(--verde-bg); color: var(--verde); }
.badge.amarillo { background: var(--amarillo-bg); color: var(--amarillo); }
.badge.rojo { background: var(--rojo-bg); color: var(--rojo); }
.badge.incompleto { background: var(--gris-bg); color: var(--suave); }
.estado-Descartado .celda[data-campo=nombre] { text-decoration: line-through; color: var(--suave); }
.meta { padding: 6px 8px; color: var(--suave); font-size: 12px; white-space: nowrap; }
.btn-fila { border: 0; background: none; cursor: pointer; font-size: 18px; color: var(--suave); width: 100%; height: 100%; }
.btn-fila:hover { color: var(--texto); background: var(--gris-bg); }

tfoot td { position: sticky; bottom: 0; background: #f0f2f5; border-top: 1px solid var(--borde-fuerte); padding: 8px; font-weight: 600; white-space: nowrap; z-index: 4; }

.vacio { text-align: center; padding: 60px 16px; color: var(--suave); }

/* ── Vista previa, diálogos, toasts ── */
.preview { position: fixed; z-index: 70; pointer-events: none; background: #fff; padding: 6px; border-radius: 10px; box-shadow: var(--sombra); }
.preview img { display: block; max-width: 320px; max-height: 320px; }
.dialogo-fondo { position: fixed; inset: 0; z-index: 80; background: rgba(15, 22, 35, .45); display: grid; place-items: center; padding: 16px; }
.dialogo { width: min(420px, 100%); background: var(--panel); border-radius: 12px; padding: 20px; box-shadow: var(--sombra); }
.dialogo h3 { margin: 0 0 12px; }
.dialogo label { display: grid; gap: 4px; margin-bottom: 10px; font-size: 12px; color: var(--suave); font-weight: 600; }
.dialogo input { padding: 9px 10px; border: 1px solid var(--borde-fuerte); border-radius: 8px; font-size: 14px; }
.dlg-acciones { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 90; display: grid; gap: 8px; }
.toast { background: #232f3e; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--sombra); max-width: 380px; }
.toast.error { background: var(--rojo); }

@media (max-width: 700px) {
  th.fija-1, td.fija-1 { min-width: 130px !important; max-width: 150px; }
  thead tr.grupos th .grupo-txt { left: 214px; }
  .barra .acciones { margin-left: 0; width: 100%; }
  .tabla-wrap { height: calc(100vh - 190px); }
  .resumen { margin-left: 0; width: 100%; }
}
