/* Biblioteca › Programas (S098-j): conglomerado entrenamiento/nutri/wellness
   por semanas. Insp: Future/Trainerize multi-week templates, Apple Fitness rings stack. */

/* Un solo scroll: el panel. La grilla crece con sus filas (nada de caja interna). */
#tab-programas.tab-panel.active {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.lprog {
  --lprog-ease: cubic-bezier(0.16, 1, 0.3, 1);
  --lprog-r: calc(16px * var(--radius-scale, 1));
  --lprog-line: var(--hairline, var(--border));
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 2px 2px 0;
  position: relative;
}
#tab-programas .programas-lib-workspace { padding-bottom: 0; }
.lprog:has(.lprog-bulk:not([hidden])) { padding-bottom: 72px; }

@media (min-width: 901px) {
  dialog.modal-dialog.lib-modal.lib-modal--side#programas-editor-dialog {
    --modal-w: 1120px;
    width: min(1120px, 94vw);
    max-width: none;
  }
}
#programas-editor-dialog {
  --lprog-ease: cubic-bezier(0.16, 1, 0.3, 1);
  --lprog-r: calc(16px * var(--radius-scale, 1));
  --lprog-line: var(--hairline, var(--border));
}

/* ── Toolbar ───────────────────────────────────────────────────────── */
.lprog-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}
#tab-programas .lprog-toolbar {
  position: sticky;
  top: 0;
  z-index: 3;
  padding: 2px 0 8px;
  margin-bottom: -8px;
  background: color-mix(in srgb, var(--bg, var(--sys-bg, #0f1412)) 94%, transparent);
  backdrop-filter: blur(8px);
}

.lprog-search {
  position: relative;
  display: flex;
  align-items: center;
  flex: 1 1 180px;
  min-width: 0;
  max-width: 420px;
  margin: 0;
}
.lprog-search > .nav-ico {
  position: absolute;
  left: 12px;
  width: 18px;
  height: 18px;
  color: var(--muted);
  pointer-events: none;
}
.lprog-search input[type="search"] {
  width: 100%;
  min-height: 44px;
  margin: 0;
  padding: 0 14px 0 40px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.92rem;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}
.lprog-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.lprog-total {
  font-size: 0.82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-left: auto;
}

#tab-programas .lprog-toolbar .lprog-create,
#tab-programas #programas-lib-add {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border-radius: 999px;
  font-weight: 650;
  width: auto !important;
  max-width: none;
}
.lprog-create .nav-ico { width: 18px; height: 18px; }

/* Pie compacto pegado abajo del panel: paginación + estado de guardado. */
.lprog-foot {
  position: sticky;
  bottom: 0;
  z-index: 3;
  flex: 0 0 auto;
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 6px 2px 8px;
  border-top: 1px solid var(--lprog-line);
  background: color-mix(in srgb, var(--bg, var(--sys-bg, #0f1412)) 94%, transparent);
  backdrop-filter: blur(8px);
}
#tab-programas .lprog-foot #programas-lib-pagination {
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
}
#tab-programas .lprog-foot .usuarios-pagination-inner { min-height: 36px; gap: 8px; }
.lprog-footnote {
  flex: 0 0 auto;
  margin: 0;
  font-size: 0.72rem;
  line-height: 1.3;
  color: var(--muted);
  text-align: center;
}
@media (min-width: 1100px) {
  .lprog-foot:has(#programas-lib-pagination:not([hidden])) .lprog-footnote {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    max-width: 34%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    pointer-events: none;
  }
}

/* ── Grid de tarjetas ───────────────────────────────────────────────── */
.lprog-grid {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr));
  grid-auto-rows: max-content;
  gap: 12px;
  padding: 2px 0 10px;
  align-content: start;
}

.lprog-card {
  position: relative;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--lprog-line);
  border-radius: var(--lprog-r);
  background: var(--surface);
  overflow: hidden;
  animation: lprog-card-in 240ms var(--lprog-ease) both;
  animation-delay: calc(var(--lprog-i, 0) * 40ms);
  transition: border-color 160ms ease, box-shadow 160ms ease, transform 160ms var(--lprog-ease);
}
@keyframes lprog-card-in {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  .lprog-card { animation: none; transition: none; }
}

.lprog-card:hover {
  border-color: color-mix(in srgb, var(--primary) 35%, var(--border));
  box-shadow: 0 8px 28px color-mix(in srgb, var(--text) 8%, transparent);
}
.lprog-card.is-selected {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 22%, transparent);
}

.lprog-card-head {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 4px 4px 0 14px;
}
.lprog-weeks {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}
.lprog-weeks .nav-ico { width: 14px; height: 14px; }
.lprog-auto {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.66rem;
  font-weight: 650;
  color: var(--primary);
  background: color-mix(in srgb, var(--primary) 10%, transparent);
}
.lprog-auto .nav-ico { width: 11px; height: 11px; }

.lprog-card-check {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  margin-left: auto;
  cursor: pointer;
  opacity: 0.35;
  transition: opacity 140ms ease;
}
.lprog-card:hover .lprog-card-check,
.lprog-card.is-selected .lprog-card-check,
.lprog-card-check:focus-within { opacity: 1; }
@media (hover: none) { .lprog-card-check { opacity: 1; } }
.lprog-card-check input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--primary);
  cursor: pointer;
}

.lprog-card-body {
  flex: 1 1 auto;
  min-width: 0;
  padding: 2px 14px 12px;
}

.lprog-card-main {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  border: none;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  font: inherit;
  white-space: normal;
}
#tab-programas .lprog-name,
#tab-programas .lprog-desc { display: -webkit-box; white-space: normal; }
.lprog-card-main:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 4px;
  border-radius: 8px;
}

.lprog-name {
  margin: 0 0 4px;
  font-size: 1rem;
  font-weight: 680;
  line-height: 1.28;
  letter-spacing: -0.01em;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: calc(1.28em * 2);
}

.lprog-desc {
  margin: 0 0 12px;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: calc(1.4em * 2);
}

.lprog-card {
  --tone-entren: var(--primary);
  --tone-cardio: #e0703a;
  --tone-nutri: #3d9a5c;
  --tone-wellness: #8b6fe0;
  --tone-fisio: #2f8fb3;
  --tone-msg: #6b7a8c;
  --tone-otro: color-mix(in srgb, var(--text) 35%, transparent);
}
.lprog-mix {
  display: flex;
  gap: 2px;
  height: 6px;
  border-radius: 999px;
  overflow: hidden;
  background: color-mix(in srgb, var(--text) 6%, transparent);
}
.lprog-mix-seg { min-width: 4px; background: var(--tone-otro); transform-origin: left; animation: lprog-seg-in 420ms var(--lprog-ease) both; animation-delay: calc(var(--lprog-i, 0) * 40ms + 120ms); }
@keyframes lprog-seg-in { from { transform: scaleX(0); } to { transform: none; } }
@media (prefers-reduced-motion: reduce) { .lprog-mix-seg { animation: none; } }

.lprog-types {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  margin-top: 8px;
}
.lprog-type-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.72rem;
  color: var(--muted);
  white-space: nowrap;
}
.lprog-type-pill b { color: var(--text); font-weight: 650; font-variant-numeric: tabular-nums; }
.lprog-dot { width: 8px; height: 8px; border-radius: 3px; background: var(--tone-otro); }
.tone-entren { --tone: var(--tone-entren); }
.tone-cardio { --tone: var(--tone-cardio); }
.tone-nutri { --tone: var(--tone-nutri); }
.tone-wellness { --tone: var(--tone-wellness); }
.tone-fisio { --tone: var(--tone-fisio); }
.tone-msg { --tone: var(--tone-msg); }
.lprog-mix-seg[class*="tone-"],
.lprog-type-pill[class*="tone-"] .lprog-dot { background: var(--tone, var(--tone-otro)); }

.lprog-card-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-top: auto;
  padding: 4px 6px 6px 14px;
  border-top: 1px solid var(--lprog-line);
}
.lprog-foot-meta {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.74rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.lprog-card-actions .roster-inline-actions { gap: 0; }
.lprog-card-actions .icon-btn { min-width: 40px; min-height: 40px; border: none; background: transparent; }
.lprog-card-actions .icon-btn:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }

.lprog-empty {
  grid-column: 1 / -1;
  padding: 32px 16px;
  text-align: center;
}

/* ── Skeleton ───────────────────────────────────────────────────────── */
.lprog-skel-card {
  min-height: 168px;
  border-radius: var(--lprog-r);
  border: 1px solid var(--lprog-line);
  background: linear-gradient(
    90deg,
    color-mix(in srgb, var(--text) 4%, transparent) 0%,
    color-mix(in srgb, var(--text) 8%, transparent) 50%,
    color-mix(in srgb, var(--text) 4%, transparent) 100%
  );
  background-size: 200% 100%;
  animation: lprog-shimmer 1.2s ease-in-out infinite;
}
@keyframes lprog-shimmer {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}
@media (prefers-reduced-motion: reduce) {
  .lprog-skel-card { animation: none; }
}

/* ── Editor (semanas / tareas) ─────────────────────────────────────── */
#programas-editor-dialog .modal-body,
:is(#tab-programas, #programas-editor-dialog) .programas-editor-body {
  overflow: auto;
  overscroll-behavior: contain;
}

:is(#tab-programas, #programas-editor-dialog) .programas-editor-section {
  margin-bottom: 18px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--lprog-line);
}
:is(#tab-programas, #programas-editor-dialog) .programas-editor-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

:is(#tab-programas, #programas-editor-dialog) .programas-editor-section-title {
  margin: 0 0 10px;
  font-size: 0.92rem;
  font-weight: 650;
}

:is(#tab-programas, #programas-editor-dialog) .programas-auto-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: 10px;
}

:is(#tab-programas, #programas-editor-dialog) .programas-auto-card {
  min-height: 44px;
  padding: 12px 14px;
  border-radius: calc(var(--lprog-r) - 2px);
  border: 1px solid var(--lprog-line);
  background: var(--surface);
  text-align: left;
  cursor: pointer;
  transition: border-color 140ms ease, background 140ms ease;
}
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card.is-selected {
  border-color: var(--primary);
  background: color-mix(in srgb, var(--primary) 8%, var(--surface));
}
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 2px;
}
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card-title { font-weight: 650; font-size: 0.9rem; color: var(--text); }
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card .hint { margin: 0; font-size: 0.78rem; color: var(--muted); opacity: 1; }
:is(#tab-programas, #programas-editor-dialog) .programas-auto-check {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  border: 1.5px solid var(--border);
  background: var(--surface);
  transition: background 140ms ease, border-color 140ms ease;
}
:is(#tab-programas, #programas-editor-dialog) .programas-auto-check .nav-ico { width: 13px; height: 13px; color: transparent; }
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card.is-selected .programas-auto-check {
  background: var(--primary);
  border-color: var(--primary);
}
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card.is-selected .programas-auto-check .nav-ico { color: var(--tenant-primary-contrast, #fff); }
:is(#tab-programas, #programas-editor-dialog) .programas-auto-card:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

:is(#tab-programas, #programas-editor-dialog) .programas-content-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

:is(#tab-programas, #programas-editor-dialog) .programas-weeks-host {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

:is(#tab-programas, #programas-editor-dialog) .programas-week-card {
  border: 1px solid var(--lprog-line);
  border-radius: var(--lprog-r);
  overflow: hidden;
  background: color-mix(in srgb, var(--text) 2%, var(--surface));
  animation: lprog-week-in 200ms var(--lprog-ease) both;
}
@keyframes lprog-week-in {
  from { opacity: 0; transform: translateX(-6px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  :is(#tab-programas, #programas-editor-dialog) .programas-week-card { animation: none; }
}

:is(#tab-programas, #programas-editor-dialog) .programas-week-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--lprog-line);
  cursor: grab;
}
:is(#tab-programas, #programas-editor-dialog) .programas-week-label {
  font-weight: 650;
  font-size: 0.88rem;
}

:is(#tab-programas, #programas-editor-dialog) .programas-days-row {
  display: grid;
  grid-template-columns: repeat(7, minmax(112px, 1fr));
  gap: 6px;
  padding: 10px;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}

:is(#tab-programas, #programas-editor-dialog) .programas-day-col {
  min-width: 72px;
  border: 1px dashed var(--lprog-line);
  border-radius: 10px;
  padding: 6px;
  background: var(--surface);
  min-height: 88px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
:is(#tab-programas, #programas-editor-dialog) .programas-day-head {
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--muted);
  text-align: center;
}

:is(#tab-programas, #programas-editor-dialog) .programas-day-tasks {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 32px;
}

:is(#tab-programas, #programas-editor-dialog) .programas-task-chip {
  color: var(--text);
  min-height: 30px;
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  padding: 4px 6px;
  border-radius: 8px;
  border: 1px solid var(--lprog-line);
  background: var(--surface);
  font-size: 0.72rem;
  cursor: grab;
  text-align: left;
  transition: transform 120ms ease, box-shadow 120ms ease;
}
:is(#tab-programas, #programas-editor-dialog) .programas-task-chip:hover {
  box-shadow: 0 2px 8px color-mix(in srgb, var(--text) 10%, transparent);
}
:is(#tab-programas, #programas-editor-dialog) .programas-task-chip.is-task-selected {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}
:is(#tab-programas, #programas-editor-dialog) .programas-task-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
  line-height: 1.25;
  font-weight: 550;
}
:is(#tab-programas, #programas-editor-dialog) .programas-task-x {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1;
}
:is(#tab-programas, #programas-editor-dialog) .programas-task-x:hover { background: color-mix(in srgb, var(--text) 8%, transparent); color: var(--text); }
:is(#tab-programas, #programas-editor-dialog) .programas-day-add {
  min-height: 32px;
  border-radius: 8px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
}
:is(#tab-programas, #programas-editor-dialog) .programas-day-add:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* Task tone chips (tenant-safe accents) */
:is(#tab-programas, #programas-editor-dialog) .programas-task-chip.tone-entren { border-left: 3px solid var(--primary); }
:is(#tab-programas, #programas-editor-dialog) .programas-task-chip.tone-nutri { border-left: 3px solid color-mix(in srgb, #3d9a5c 70%, var(--primary)); }
:is(#tab-programas, #programas-editor-dialog) .programas-task-chip.tone-cardio { border-left: 3px solid color-mix(in srgb, #e85d4c 50%, var(--primary)); }
:is(#tab-programas, #programas-editor-dialog) .programas-task-chip.tone-wellness { border-left: 3px solid color-mix(in srgb, var(--primary) 60%, #9b7bff); }

.lprog-bulk {
  position: fixed;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  max-width: min(520px, calc(100vw - 24px));
}

/* Ocultar tabla legacy si queda en DOM */
:is(#tab-programas, #programas-editor-dialog) .programas-lib-table-wrap,
:is(#tab-programas, #programas-editor-dialog) .programas-lib-hero { display: none !important; }

/* ── Móvil 390 ──────────────────────────────────────────────────────── */
@media (max-width: 600px) {
  .lprog-foot { position: static; background: transparent; backdrop-filter: none; }
  .lprog-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .lprog-toolbar {
    gap: 6px;
  }
  .lprog-search {
    flex: 1 1 0;
    max-width: none;
  }
  .lprog-total {
    order: 3;
    margin-left: 0;
    width: 100%;
    text-align: left;
    font-size: 0.76rem;
  }
  #tab-programas #programas-lib-add {
    flex: 0 0 auto !important;
    margin: 0;
    padding: 0 14px;
  }
  :is(#tab-programas, #programas-editor-dialog) .programas-days-row {
    grid-template-columns: 1fr;
    padding: 8px;
    overflow: visible;
  }
  :is(#tab-programas, #programas-editor-dialog) .programas-day-col {
    min-width: 0;
    min-height: 0;
    display: grid;
    grid-template-columns: 52px 1fr auto;
    align-items: center;
    gap: 6px;
    border-style: solid;
    padding: 6px 6px 6px 10px;
  }
  :is(#tab-programas, #programas-editor-dialog) .programas-day-head { text-align: left; }
  :is(#tab-programas, #programas-editor-dialog) .programas-day-tasks { min-height: 0; }
  :is(#tab-programas, #programas-editor-dialog) .programas-day-add { width: 36px; min-height: 36px; }
}
