/* ============================================================================
   PAMEH · Shell layout  (rediseno estructural del chrome)
   ----------------------------------------------------------------------------
   Reemplaza el rol de layout de la plantilla Blankon para el "chrome":
   topbar unificada + rail de navegacion colapsable + page-header + footer.
   Se carga DESPUES de pameh-modern-ui.css. Usa clases propias `shell-*` /
   `rail-*` / `page-*`; no depende del DOM Blankon (#header, #sidebar-left...).
   Paleta institucional SEFIN: azul #19438c - blanco - dorado #ad8411.
   ============================================================================ */

/* ----------------------------------------------------------------------------
   1. TOKENS
   ---------------------------------------------------------------------------- */
:root {
  --shell-topbar-h: 56px;
  --shell-rail-w-expanded: 264px;
  --shell-rail-w-collapsed: 68px;
  --shell-rail-w: var(--shell-rail-w-expanded);

  --shell-topbar-bg: #ffffff;
  --shell-topbar-border: #e4e8f0;

  --rail-bg: #16336b;
  --rail-bg-brand: #0f2f66;
  --rail-bg-hover: rgba(255, 255, 255, 0.08);
  --rail-bg-active: rgba(255, 255, 255, 0.12);
  --rail-bg-sub: rgba(0, 0, 0, 0.16);
  --rail-text: rgba(255, 255, 255, 0.78);
  --rail-text-strong: #ffffff;
  --rail-text-dim: rgba(255, 255, 255, 0.5);
  --rail-divider: rgba(255, 255, 255, 0.09);
  --rail-accent: #d8a72b;

  --shell-content-bg: #eef1f8;

  /* Layout flotante: separacion / radio / sombra comunes a rail, canvas y footer */
  --shell-gap: 14px;
  --shell-radius: 14px;
  --shell-shadow: 0 2px 10px rgba(16, 42, 90, 0.07), 0 1px 3px rgba(16, 42, 90, 0.05);
  --shell-shadow-rail: 0 4px 18px rgba(11, 30, 72, 0.14);

  /* Por debajo de los modales de Bootstrap (backdrop 1040 / modal 1050) */
  --shell-z-topbar: 1000;
  --shell-z-rail: 1000;
  --shell-z-scrim: 995;
  --shell-z-flyout: 1010;
}

/* ----------------------------------------------------------------------------
   2. LAYOUT BASE
   ---------------------------------------------------------------------------- */
html,
body.pameh-shell {
  height: 100%;
}

/* reset.css deja html en #e9eaed; con piezas flotantes ese gris asoma en el
   overscroll -> se alinea con el fondo del shell */
html {
  background-color: var(--shell-content-bg);
}

body.pameh-shell {
  margin: 0;
  background-color: var(--shell-content-bg);
  color: var(--text, #1f2733);
  font-family: "Open Sans", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow-x: hidden;
}

body.pameh-shell[data-rail="collapsed"] {
  --shell-rail-w: var(--shell-rail-w-collapsed);
}

/* En modo off-canvas el contenido ocupa todo el ancho */
@media (max-width: 991px) {
  body.pameh-shell,
  body.pameh-shell[data-rail="collapsed"] {
    --shell-rail-w: 0px;
  }
}

/* Skip-link accesible */
.shell-skip {
  position: fixed;
  top: -60px;
  left: 12px;
  z-index: 2000;
  padding: 8px 14px;
  background: var(--sefin-primary, #19438c);
  color: #fff;
  border-radius: 6px;
  transition: top 0.15s ease;
}
.shell-skip:focus {
  top: 12px;
  color: #fff;
}

/* ----------------------------------------------------------------------------
   3. TOPBAR
   ---------------------------------------------------------------------------- */
.shell-topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--shell-topbar-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 var(--shell-gap);
  background-color: var(--shell-topbar-bg);
  border-bottom: 1px solid var(--shell-topbar-border);
  box-shadow: 0 1px 2px rgba(16, 42, 90, 0.06), 0 4px 12px rgba(16, 42, 90, 0.04);
  z-index: var(--shell-z-topbar);
}

/* Segmento de marca: chip compacto (logo + sigla del sistema) */
.shell-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
  height: 40px;
  padding: 0 14px;
  border-radius: 10px;
  background-color: var(--rail-bg-brand);
  color: #fff;
  overflow: hidden;
}
.shell-brand__logo {
  height: 28px;
  width: auto;
  flex: 0 0 auto;
}
.shell-brand__name {
  display: block;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.4px;
  white-space: nowrap;
}

/* Zona de contexto de pagina (titulo + breadcrumb) */
.shell-topbar__main {
  display: flex;
  align-items: center;
  flex: 1 1 auto;
  min-width: 0;
}
/* Separador vertical entre la identidad del sistema y el contexto de pagina */
.shell-topbar__main::before {
  content: "";
  flex: 0 0 auto;
  width: 1px;
  height: 26px;
  margin-right: 14px;
  background-color: var(--shell-topbar-border);
}

.shell-toggle {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--sefin-primary, #19438c);
  font-size: 18px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.12s ease;
}
.shell-toggle:hover {
  background-color: rgba(25, 67, 140, 0.08);
}
.shell-toggle:focus-visible {
  outline: 2px solid var(--sefin-primary, #19438c);
  outline-offset: 1px;
}

/* Contexto de pagina (titulo + breadcrumb) en la topbar */
.shell-context {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  line-height: 1.15;
}
.shell-context__title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text, #1f2733);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.shell-context__crumbs {
  font-size: 11px;
  color: var(--text-muted, #6b7480);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.shell-context__crumbs a {
  color: var(--text-muted, #6b7480);
}
.shell-context__crumbs a:hover {
  color: var(--sefin-primary, #19438c);
}
.shell-context__crumbs .sep {
  margin: 0 5px;
  opacity: 0.5;
}

/* Zona derecha */
.shell-topbar__aside {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 0 0 auto;
}

.shell-iconbtn {
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-muted, #6b7480);
  font-size: 16px;
  border-radius: 8px;
  cursor: pointer;
  position: relative;
  transition: background-color 0.12s ease, color 0.12s ease;
}
.shell-iconbtn:hover {
  background-color: rgba(25, 67, 140, 0.07);
  color: var(--sefin-primary, #19438c);
}

/* Menu de usuario */
.shell-user > a,
.shell-user .dropdown-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 10px;
  border-radius: 8px;
  color: var(--text, #1f2733);
  cursor: pointer;
}
.shell-user > a:hover {
  background-color: rgba(25, 67, 140, 0.07);
}
.shell-user__avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  background: #fff;
  border: 1px solid var(--shell-topbar-border);
}
.shell-user__name {
  font-size: 13px;
  font-weight: 600;
  max-width: 190px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.shell-user .dropdown-menu {
  margin-top: 8px;
}

.shell-topbar .btn-login {
  height: 38px;
  padding: 0 18px;
  display: inline-flex;
  align-items: center;
}

@media (max-width: 640px) {
  .shell-user__name {
    display: none;
  }
  .shell-context__crumbs {
    display: none;
  }
  .shell-brand {
    padding: 0 10px;
  }
  .shell-brand__name {
    display: none;
  }
}

/* ----------------------------------------------------------------------------
   4. RAIL (sidebar de navegacion)
   ---------------------------------------------------------------------------- */
.shell-rail {
  position: fixed;
  top: calc(var(--shell-topbar-h) + var(--shell-gap));
  left: var(--shell-gap);
  bottom: var(--shell-gap);
  width: var(--shell-rail-w);
  display: flex;
  flex-direction: column;
  background-color: var(--rail-bg);
  color: var(--rail-text);
  border-radius: var(--shell-radius);
  box-shadow: var(--shell-shadow-rail);
  z-index: var(--shell-z-rail);
  transition: width 0.18s ease, transform 0.2s ease;
  overflow: hidden;
}
.shell-rail::-webkit-scrollbar {
  width: 8px;
}

/* Bloque de contexto: perfil / operacion */
.rail-context {
  flex: 0 0 auto;
  padding: 12px;
  border-bottom: 1px solid var(--rail-divider);
  background-color: rgba(0, 0, 0, 0.12);
}
.rail-context .media {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 10px;
}
.rail-context .media img,
.rail-context .avatar img {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
}
.rail-context .media-body {
  min-width: 0;
}
.rail-context .media-heading {
  margin: 0;
  font-size: 13px;
  font-weight: 700;
  color: var(--rail-text-strong);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rail-context .media-body small {
  color: var(--rail-text-dim);
  font-size: 11px;
}
.rail-context .btn-group.btn-block {
  margin-bottom: 8px;
}
.rail-context .btn-group.btn-block:last-child {
  margin-bottom: 0;
}
.rail-context .btn-group.btn-block > button.dropdown-toggle,
.rail-context button.btn.dropdown-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background-color: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid var(--rail-divider) !important;
  color: #fff !important;
  border-radius: 7px !important;
  font-weight: 600;
  font-size: 12px;
  padding: 7px 10px;
  text-align: left;
}
.rail-context .btn-group.btn-block > button.dropdown-toggle:hover {
  background-color: rgba(255, 255, 255, 0.18) !important;
}
.rail-context .btn-group .dropdown-menu {
  width: 100%;
}
.rail-context .anysmall {
  display: none;
}

/* Navegacion scrollable */
.rail-nav {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 0 16px;
  -webkit-overflow-scrolling: touch;
}

.rail-section {
  margin-top: 4px;
}
.rail-section + .rail-section {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--rail-divider);
}
.rail-section__label {
  display: block;
  padding: 6px 16px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.7px;
  text-transform: uppercase;
  color: var(--rail-text-dim);
  white-space: nowrap;
  overflow: hidden;
}

.rail-list,
.rail-sublist {
  list-style: none;
  margin: 0;
  padding: 0;
}

.rail-link {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 9px 16px;
  border: none;
  background: transparent;
  color: var(--rail-text);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.12s ease, color 0.12s ease;
}
.rail-link:hover,
.rail-link:focus {
  background-color: var(--rail-bg-hover);
  color: var(--rail-text-strong);
  text-decoration: none;
}
.rail-link__icon {
  flex: 0 0 22px;
  text-align: center;
  font-size: 15px;
  color: inherit;
  opacity: 0.9;
}
.rail-link__text {
  flex: 1 1 auto;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.rail-link__caret {
  flex: 0 0 auto;
  font-size: 12px;
  transition: transform 0.15s ease;
  opacity: 0.7;
}
.rail-link__bullet {
  flex: 0 0 22px;
  display: flex;
  justify-content: center;
}
.rail-link__bullet::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.45;
}

/* Grupos colapsables */
.rail-item--group > .rail-sublist {
  display: none;
  background-color: var(--rail-bg-sub);
}
.rail-item--group.is-open > .rail-sublist {
  display: block;
}
.rail-item--group.is-open > .rail-link > .rail-link__caret {
  transform: rotate(90deg);
}

/* Sangria progresiva de subniveles */
.rail-sublist .rail-link {
  padding-left: 30px;
}
.rail-sublist .rail-sublist .rail-link {
  padding-left: 44px;
}
.rail-sublist .rail-sublist .rail-sublist .rail-link {
  padding-left: 56px;
}

/* Estado activo */
.rail-link.is-active {
  background-color: var(--rail-bg-active);
  color: var(--rail-text-strong);
  box-shadow: inset 3px 0 0 var(--rail-accent);
}
.rail-link.is-active .rail-link__icon,
.rail-link.is-active .rail-link__bullet::before {
  opacity: 1;
  color: var(--rail-accent);
}
.rail-item--group.has-active-child > .rail-link {
  color: var(--rail-text-strong);
}

/* Footer del rail (acciones) */
.rail-actions {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 8px 6px;
  background-color: var(--rail-bg-brand);
  border-top: 1px solid var(--rail-divider);
}
.rail-actions a {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.72);
  border-radius: 7px;
  font-size: 15px;
  transition: background-color 0.12s ease, color 0.12s ease;
}
.rail-actions a:hover {
  background-color: rgba(255, 255, 255, 0.12);
  color: #fff;
}

/* ---- Estado colapsado (rail de iconos) ----
   Regla: en <= 68px se ocultan textos; al pasar el cursor, TODO el rail se
   expande temporalmente (overlay) mostrando etiquetas. Sin flyouts por item
   (evita conflictos de overflow con el area scrollable). */
body.pameh-shell[data-rail="collapsed"] .shell-rail {
  transition: width 0.16s ease;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:hover {
  width: var(--shell-rail-w-expanded);
  box-shadow: 10px 12px 36px rgba(8, 22, 55, 0.32);
  z-index: var(--shell-z-flyout);
}

/* Estado NO-hover del rail colapsado: solo iconos */
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-context {
  padding: 10px 8px;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-context .media {
  justify-content: center;
  margin-bottom: 0;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-context .media-body,
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-context .btn-group.btn-block,
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-section__label,
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-link__text,
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-link__caret,
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-actions a:not(:first-child):not(:last-child) {
  display: none;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-link {
  justify-content: center;
  padding: 11px 0;
  gap: 0;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-sublist .rail-link {
  padding-left: 0;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-section + .rail-section {
  margin-top: 6px;
  padding-top: 6px;
}
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-actions {
  flex-direction: column;
  gap: 4px;
}
/* Los subniveles abiertos se ocultan cuando el rail esta colapsado y sin hover */
body.pameh-shell[data-rail="collapsed"] .shell-rail:not(:hover) .rail-item--group.is-open > .rail-sublist {
  display: none;
}

/* ----------------------------------------------------------------------------
   5. OFF-CANVAS (< 992px)
   ---------------------------------------------------------------------------- */
@media (max-width: 991px) {
  .shell-rail {
    width: var(--shell-rail-w-expanded);
    transform: translateX(calc(-100% - var(--shell-gap) - 16px));
    box-shadow: 10px 12px 36px rgba(8, 22, 55, 0.32);
  }
  body.pameh-shell.rail-open .shell-rail {
    transform: translateX(0);
  }
  /* En movil el rail es un panel off-canvas de ancho completo: nunca "colapsado".
     Se neutraliza el modo icono aunque data-rail sea "collapsed". */
  body.pameh-shell .shell-rail .rail-link__text,
  body.pameh-shell .shell-rail .rail-link__caret,
  body.pameh-shell .shell-rail .rail-section__label,
  body.pameh-shell .shell-rail .rail-context .media-body,
  body.pameh-shell .shell-rail .rail-context .btn-group.btn-block {
    display: block !important;
  }
  body.pameh-shell .shell-rail .rail-link {
    justify-content: flex-start !important;
    padding: 9px 16px !important;
    gap: 12px !important;
  }
  body.pameh-shell .shell-rail .rail-sublist .rail-link {
    padding-left: 30px !important;
  }
  body.pameh-shell .shell-rail .rail-context .media {
    justify-content: flex-start !important;
  }
  body.pameh-shell .shell-rail .rail-actions {
    flex-direction: row !important;
  }
  body.pameh-shell .shell-rail .rail-actions a {
    display: inline-flex !important;
  }
  body.pameh-shell[data-rail="collapsed"] .shell-rail:hover {
    width: var(--shell-rail-w-expanded);
  }
}

.shell-scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(10, 24, 55, 0.45);
  z-index: var(--shell-z-scrim);
}
@media (max-width: 991px) {
  body.pameh-shell.rail-open .shell-scrim {
    display: block;
  }
}

/* ----------------------------------------------------------------------------
   6. MAIN + PAGE-HEADER + CANVAS + FOOTER
   ---------------------------------------------------------------------------- */
.shell-main {
  margin-top: calc(var(--shell-topbar-h) + var(--shell-gap));
  margin-left: calc(var(--shell-rail-w) + var(--shell-gap) * 2);
  margin-right: var(--shell-gap);
  min-height: calc(100vh - var(--shell-topbar-h) - var(--shell-gap));
  display: flex;
  flex-direction: column;
  padding: 0 0 var(--shell-gap);
  transition: margin-left 0.18s ease;
}
@media (max-width: 991px) {
  .shell-main {
    margin-left: var(--shell-gap);
  }
}
@media (max-width: 576px) {
  :root {
    --shell-gap: 8px;
  }
}

.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
}
.page-header__titles {
  min-width: 0;
}
.page-header__title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  color: var(--text, #1f2733);
  line-height: 1.2;
}
.page-header__actions {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}
.page-header__actions:empty {
  display: none;
}
@media (max-width: 640px) {
  .page-header {
    flex-direction: column;
    gap: 10px;
  }
  .page-header__actions {
    justify-content: flex-start;
  }
}

/* Canvas: se conservan las clases panel-app / panel-body-app para las vistas.
   Llena el area de contenido (misma estructura con o sin sesion). */
.shell-main > .panel-app {
  flex: 1 1 auto;
  background-color: var(--surface, #fff);
  /* !important + hijo directo: pameh-modern-ui.css declara .panel-app con
     !important; el hijo directo evita tocar los .panel-app anidados */
  border: 1px solid rgba(16, 42, 90, 0.06) !important;
  border-radius: var(--shell-radius) !important;
  box-shadow: var(--shell-shadow) !important;
  overflow: visible;
}
.shell-main .panel-body-app {
  padding: 22px;
}
@media (max-width: 576px) {
  .shell-main .panel-body-app {
    padding: 14px !important;
  }
}

.shell-footer {
  margin-top: var(--shell-gap);
  padding: 12px 18px;
  font-size: 12px;
  color: var(--text-muted, #6b7480);
  text-align: center;
  background-color: var(--surface, #fff);
  border: 1px solid rgba(16, 42, 90, 0.06);
  border-radius: var(--shell-radius);
  box-shadow: var(--shell-shadow);
}
.shell-footer a {
  color: var(--sefin-primary, #19438c);
}

/* ----------------------------------------------------------------------------
   7. ESTADO NO AUTENTICADO
   ----------------------------------------------------------------------------
   La estructura del shell (topbar + rail + main) se mantiene identica a la
   sesion iniciada; solo faltan los bloques con datos (perfil, acciones). */
body.pameh-shell.is-anon .rail-nav:empty::before {
  content: "Inicie sesion para ver el menu";
  display: block;
  padding: 16px;
  font-size: 12px;
  color: var(--rail-text-dim);
}

/* ----------------------------------------------------------------------------
   8. BACK-TO-TOP (se conserva el elemento de Blankon, restyle)
   ---------------------------------------------------------------------------- */
#back-top {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background-color: var(--sefin-primary, #19438c) !important;
  color: #fff;
  z-index: 1020;
}
#back-top:hover {
  background-color: var(--sefin-primary-dark, #0f2f66) !important;
}

/* ----------------------------------------------------------------------------
   9. AJUSTE FINO: no scroll horizontal por contenido ancho
   ---------------------------------------------------------------------------- */
.shell-main .table-responsive {
  overflow-x: auto;
}
