/**
 * AppShell — capa de PRODUCTO (MOWIS) sobre los tokens de SISTEMA de style.css.
 *
 * No se toca frontend/style.css en esta tarea (ver docs/WORKBOARD.md, mapa de colisiones).
 * Todas las clases de este fichero usan el prefijo `appshell-` para no interferir con el
 * CSS legado (`.sidebar`, `.sidebar-nav`, `.topbar`, ...) que siguen usando las paginas
 * todavia no migradas.
 *
 * Capas de marca:
 *   style.css      tokens de SISTEMA (--background, --surface, --border, --text-*, ...)
 *   este fichero    tokens de PRODUCTO MOWIS (--brand-*), hoy alias de los de MOWIS en
 *                   style.css para no duplicar hex — un Product Brand futuro (o un
 *                   override de tenant) redefine solo estas variables, no las de sistema.
 */
:root {
  --brand-primary: var(--primary, #2563eb);
  --brand-secondary: var(--secondary, #475569);
  --brand-accent: var(--cyan, #22c7e6);
  --brand-sidebar-bg: var(--sidebar-bg, #0e1c34);
  --brand-sidebar-text: var(--sidebar-text, #cbd5e1);
  --brand-sidebar-text-hover: var(--sidebar-text-hover, #ffffff);
  --brand-sidebar-active: var(--sidebar-active, rgba(34, 199, 230, 0.14));
  --brand-sidebar-border: var(--sidebar-border, #1e293b);
  --brand-gradient: var(--brand-grad, linear-gradient(135deg, #22c7e6, #2563eb));

  --appshell-sidebar-width: 250px;
  --appshell-sidebar-rail-width: 68px;
  --appshell-transition: 0.2s ease;
}

/* ==========================================================================
   Layout host — el mount point vive dentro del wrapper flex existente de
   cada pagina (`.layout-app` o el `display:flex` inline equivalente), asi
   que solo necesitamos que la sidebar tenga el tamano correcto como hijo
   flex; no redefinimos el layout de la pagina.

   UNA excepcion, deliberada: `frontend/style.css::.layout-app` (legado, no se
   toca en esta tarea) apila sidebar+contenido en columna por debajo de
   `max-width: 768px` -- pensado para el `.sidebar` legado, no para el rail
   compacto de tablet que este fichero define para 768-1023px mas abajo. A
   EXACTAMENTE 768px (limite inclusivo de esa regla, y el ancho real de un
   iPad en vertical) ambas reglas coincidian a la vez: el rail se angostaba a
   68px pero el `<main>` quedaba apilado DEBAJO de un sidebar de 1024px de
   alto en vez de al lado -- el contenido principal aterrizaba fuera del
   viewport (bug real, encontrado con Playwright en 768x1024). Se fuerza fila
   desde 768px SOLO cuando hay una sidebar de AppShell montada (`:has()`), sin
   tocar el layout de las paginas que aun usan el `.sidebar` legado. */
@media (min-width: 768px) {
  .layout-app:has(.appshell-sidebar) {
    flex-direction: row;
  }
}

.appshell-sidebar {
  width: var(--appshell-sidebar-width);
  min-width: var(--appshell-sidebar-width);
  background-color: var(--brand-sidebar-bg);
  color: var(--brand-sidebar-text);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  border-right: 1px solid var(--border, #e2e8f0);
  transition: width var(--appshell-transition), min-width var(--appshell-transition);
  z-index: 100;
}

.appshell-sidebar[data-collapsed="true"] {
  width: var(--appshell-sidebar-rail-width);
  min-width: var(--appshell-sidebar-rail-width);
}

/* ---- Brand header ---- */
.appshell-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 20px;
  border-bottom: 1px solid var(--brand-sidebar-border);
  cursor: pointer;
  min-height: 32px;
}

.appshell-brand-mark {
  width: 30px;
  height: 30px;
  min-width: 30px;
  border-radius: var(--radius-sm, 6px);
  background: var(--brand-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
}

.appshell-brand-text { overflow: hidden; white-space: nowrap; }
.appshell-brand-text .appshell-product-name {
  margin: 0;
  color: #ffffff;
  font-weight: 600;
  font-size: 1.05rem;
  line-height: 1.2;
}
.appshell-brand-text .appshell-powered-by {
  font-size: 0.68rem;
  color: var(--brand-accent);
  margin: 0;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.85;
}
.appshell-sidebar[data-collapsed="true"] .appshell-brand-text { display: none; }

/* ---- Nav ---- */
.appshell-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 0;
}

.appshell-nav-group-label {
  padding: 14px 20px 6px;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: rgba(203, 213, 225, 0.55);
  white-space: nowrap;
}
.appshell-sidebar[data-collapsed="true"] .appshell-nav-group-label { display: none; }

.appshell-nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  color: var(--brand-sidebar-text);
  text-decoration: none;
  font-weight: 500;
  font-size: 0.9rem;
  border-left: 3px solid transparent;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color var(--appshell-transition), color var(--appshell-transition);
  background: none;
  border-top: none;
  border-right: none;
  border-bottom: none;
  width: 100%;
  text-align: left;
}
.appshell-nav-item i, .appshell-nav-item svg { flex: none; width: 16px; height: 16px; }

.appshell-nav-item:hover,
.appshell-nav-item.is-active {
  background-color: var(--brand-sidebar-active);
  color: var(--brand-sidebar-text-hover);
  border-left-color: var(--brand-accent);
}
.appshell-nav-item.is-active i,
.appshell-nav-item.is-active svg { color: var(--brand-accent); }

.appshell-nav-item.is-soon {
  opacity: 0.45;
  cursor: not-allowed;
}
.appshell-nav-item.is-soon:hover { background: none; border-left-color: transparent; }
.appshell-nav-item .appshell-soon-badge {
  margin-left: auto;
  font-size: 0.62rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(203, 213, 225, 0.15);
  color: var(--brand-sidebar-text);
}
.appshell-sidebar[data-collapsed="true"] .appshell-nav-item span:not(.appshell-soon-badge) { display: none; }
.appshell-sidebar[data-collapsed="true"] .appshell-nav-item .appshell-soon-badge { display: none; }

/* ---- Footer: identidad + logout + collapse ---- */
.appshell-sidebar-footer {
  border-top: 1px solid var(--brand-sidebar-border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.appshell-user-avatar {
  width: 32px;
  height: 32px;
  min-width: 32px;
  border-radius: 50%;
  background: var(--brand-gradient);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.8rem;
}

.appshell-user-meta { flex: 1; min-width: 0; overflow: hidden; }
.appshell-user-meta .appshell-user-name {
  display: block;
  color: #fff;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.appshell-user-meta .appshell-user-role {
  display: block;
  color: var(--brand-sidebar-text);
  font-size: 0.7rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.appshell-sidebar[data-collapsed="true"] .appshell-user-meta { display: none; }

.appshell-logout-btn, .appshell-collapse-btn {
  background: transparent;
  border: none;
  color: var(--brand-sidebar-text);
  cursor: pointer;
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: var(--radius-sm, 6px);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color var(--appshell-transition), color var(--appshell-transition);
}
.appshell-logout-btn:hover { background: rgba(239, 68, 68, 0.15); color: var(--danger, #ef4444); }
.appshell-collapse-btn:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.appshell-logout-btn i, .appshell-logout-btn svg,
.appshell-collapse-btn i, .appshell-collapse-btn svg { width: 16px; height: 16px; }

/* Rail (collapsed) toggle lives just above the footer, desktop/tablet only */
.appshell-collapse-row {
  display: none;
  padding: 8px 16px 0;
  justify-content: flex-end;
}

/* ==========================================================================
   Topbar centralizado — usado por las paginas que declaran `options.topbar`
   en `AppShell.mount()`. El shell construye `<header class="appshell-topbar">`
   una sola vez y reubica ahi el buscador/filtros/botones propios de cada
   pagina (ver `frontend/app-shell.js`); nunca inventa ese contenido.
   ========================================================================== */
.appshell-topbar {
  height: 60px;
  background-color: var(--surface, #ffffff);
  border-bottom: 1px solid var(--border, #e2e8f0);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: 90;
}

.appshell-topbar-left {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  overflow: hidden;
}

.appshell-topbar-right {
  flex: none;
  display: flex;
  align-items: center;
  gap: 16px;
}

.appshell-topbar-user {
  display: flex;
  align-items: center;
  gap: 12px;
}

.appshell-topbar-user-info { text-align: right; }
.appshell-topbar-user-info .appshell-user-name {
  display: block;
  font-weight: 600;
  color: var(--text-primary, #1e293b);
  font-size: 0.9rem;
  white-space: nowrap;
}
.appshell-topbar-user-info .appshell-user-role {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary, #64748b);
  white-space: nowrap;
}

.appshell-topbar-logout {
  background: transparent;
  color: var(--danger, #ef4444);
  border: 1px solid var(--danger, #ef4444);
  padding: 6px 10px;
  border-radius: var(--radius-md, 10px);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
  transition: background-color var(--appshell-transition), color var(--appshell-transition);
}
.appshell-topbar-logout:hover { background: var(--danger, #ef4444); color: #fff; }
.appshell-topbar-logout i, .appshell-topbar-logout svg { width: 14px; height: 14px; }

@media (max-width: 767px) {
  /* Deja hueco para el boton flotante de apertura de la sidebar (fixed, top-left). */
  .appshell-topbar { padding-left: 64px; }
}

/* ==========================================================================
   Mobile drawer toggle — flotante, inyectado por el shell (independiente de
   si la pagina usa topbar centralizado o no).
   ========================================================================== */
.appshell-mobile-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 210;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-md, 10px);
  border: 1px solid var(--border, #e2e8f0);
  background: var(--surface, #fff);
  color: var(--text-primary, #1e293b);
  box-shadow: var(--shadow-md, 0 4px 6px -1px rgba(0,0,0,0.1));
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.appshell-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 150;
}

@media (max-width: 1023px) {
  .appshell-collapse-row { display: none !important; }
}

@media (min-width: 1024px) {
  .appshell-collapse-row { display: flex; }
}

/* Tablet: rail collapsed by default (icon-only), expandible on demand */
@media (min-width: 768px) and (max-width: 1023px) {
  /* Sin condicionar a [data-collapsed]: en tablet el boton de colapso manual esta
     oculto (ver `.appshell-collapse-row` mas abajo, solo visible en desktop), asi
     que el rail compacto es el UNICO estado posible en este ancho — no depende de
     que `mount()` haya puesto `data-collapsed="false"` por defecto. */
  .appshell-sidebar {
    width: var(--appshell-sidebar-rail-width);
    min-width: var(--appshell-sidebar-rail-width);
  }
  .appshell-sidebar .appshell-brand-text,
  .appshell-sidebar .appshell-nav-group-label,
  .appshell-sidebar .appshell-nav-item span:not(.appshell-soon-badge),
  .appshell-sidebar .appshell-user-meta {
    display: none;
  }
}

/* Mobile: off-canvas drawer */
@media (max-width: 767px) {
  .appshell-mobile-toggle { display: flex; }

  .appshell-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    width: 260px;
    min-width: 260px;
    transform: translateX(-100%);
    transition: transform var(--appshell-transition);
    z-index: 200;
  }
  .appshell-sidebar[data-mobile-open="true"] {
    transform: translateX(0);
  }
  .appshell-sidebar[data-collapsed="true"] {
    width: 260px;
    min-width: 260px;
  }
  .appshell-sidebar[data-collapsed="true"] .appshell-brand-text,
  .appshell-sidebar[data-collapsed="true"] .appshell-nav-group-label,
  .appshell-sidebar[data-collapsed="true"] .appshell-nav-item span:not(.appshell-soon-badge),
  .appshell-sidebar[data-collapsed="true"] .appshell-user-meta {
    display: block;
  }
  .appshell-sidebar[data-collapsed="true"] .appshell-nav-item { display: flex; }

  .appshell-backdrop[data-open="true"] { display: block; }
}

/* dashboard.html reubica su widget de embudo (legado, frontend/style.css) dentro de la
   sidebar montada por el shell; aqui solo se fija su altura para que no compita por
   espacio flex:1 con `.appshell-nav`. */
.appshell-sidebar .sidebar-funnel {
  flex: 0 0 auto;
  max-height: 240px;
}

/* Focus visibility for keyboard navigation (accesibilidad basica) */
.appshell-nav-item:focus-visible,
.appshell-logout-btn:focus-visible,
.appshell-collapse-btn:focus-visible,
.appshell-mobile-toggle:focus-visible {
  outline: 2px solid var(--brand-accent);
  outline-offset: -2px;
}
