/* ============================================================
   Arch 2.0 — Base Layout: Reset, Topnav, Sidebar, Main
   ============================================================ */

/* --- Reset -------------------------------------------------- */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: var(--font-body);
    color: var(--text-primary);
    background: var(--bg-app);
    line-height: 1.5;
    min-height: 100vh;
}

a {
    color: var(--text-link);
    text-decoration: none;
    transition: color var(--duration) var(--ease);
}

a:hover {
    color: var(--brand-strong-hover);
}

img, svg {
    display: block;
    max-width: 100%;
}

/* Scrollbar (Webkit) */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: var(--border-strong);
    border-radius: var(--radius-full);
}

/* --- Topnav ------------------------------------------------- */
.topnav {
    position: fixed;
    top: 0;
    left: var(--sidebar-width);
    right: 0;
    height: var(--topnav-height);
    background: var(--bg-topnav);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-6);
    z-index: 100;
    transition: left var(--duration) var(--ease);
}

/* Nombre de empresa largo en pantallas angostas (≤850px): hacen falta las
   TRES piezas juntas, medido — `.topnav-left` se deja encoger (min-width: 0),
   el nombre se corta con elipsis y `.topnav-right` no cede su ancho. Con solo
   las dos primeras el cluster de la derecha igual se aplasta. */
.topnav-left {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
}

.topnav-left .empresa-name {
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: var(--fs-sm);
    color: var(--text-primary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.topnav-right {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    flex-shrink: 0;
}


.topnav-datetime {
    text-align: right;
    line-height: 1.3;
}
.topnav-date {
    font-size: var(--fs-xs);
    color: var(--text-tertiary);
}
.topnav-greeting {
    font-size: var(--fs-sm);
    color: var(--text-secondary);
}
.topnav-greeting-name {
    color: var(--brand-strong);
    font-weight: 600;
}

.role-badge {
    font-size: var(--fs-xs);
    font-weight: 500;
    padding: 2px 8px;
    border-radius: var(--radius-full);
}

.role-badge-brand {
    background: var(--brand-subtle);
    color: var(--brand-strong);
}

.role-badge-info {
    background: var(--info-bg);
    color: var(--info-text);
}

.role-badge-warning {
    background: var(--warning-bg);
    color: var(--warning-text);
}

.role-badge-secondary {
    background: var(--bg-surface-alt);
    color: var(--text-tertiary);
}

.topnav-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: none;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: var(--fs-md);
    transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}

.topnav-btn:hover {
    background: var(--bg-surface-hover);
    color: var(--text-primary);
}

.topnav-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: var(--radius-md);
    color: var(--text-tertiary);
    font-size: var(--fs-md);
    transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}

.topnav-link:hover {
    background: var(--danger-bg);
    color: var(--danger-text);
}

/* --- Mobile kebab menu (reusable for any topnav context) ----- */
.topnav-kebab {
    display: none;             /* Hidden on desktop, shown below 768px */
    position: relative;
    align-items: center;
}

.kebab-trigger {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-secondary);
    font-size: var(--fs-lg);
    cursor: pointer;
    transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}

.kebab-trigger:hover {
    background: var(--bg-surface-hover);
    color: var(--text-primary);
}

.kebab-panel {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 240px;
    padding: var(--space-2);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-menu);
    z-index: 120;
}

.kebab-user {
    padding: var(--space-3);
    border-bottom: 1px solid var(--border-subtle);
    margin-bottom: var(--space-2);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    align-items: flex-start;
}

.kebab-greet {
    font-size: var(--fs-sm);
    color: var(--text-secondary);
}

.kebab-greet strong {
    color: var(--brand-strong);
    font-weight: 600;
}

.kebab-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    padding: var(--space-3);
    border: none;
    background: transparent;
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-size: var(--fs-sm);
    text-align: left;
    cursor: pointer;
    transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
    text-decoration: none;
}

.kebab-item:hover {
    background: var(--bg-surface-hover);
}

.kebab-item i {
    font-size: var(--fs-md);
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}

[x-cloak] { display: none !important; }

/* --- Sidebar ------------------------------------------------ */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 200;
    transition: transform var(--duration) var(--ease);
}

/* Pie del menú anclado (subido de Core, 2026-08-11 → canon v1.0.5): cuando el
   menú tiene su lista en `<nav class="sidebar-nav">`, el scroll pasa del
   <aside> entero a esa lista y el pie (Sesión / Cerrar sesión / Panel) queda
   siempre a la vista. Va condicionado a propósito: un producto
   cuyo sidebar todavía no envuelve la lista en `.sidebar-nav` conserva el
   scroll del <aside> entero en vez de quedarse sin scroll. La condición es la
   clase `sidebar--anclado` del <aside> (la pone `_sidebar.html`; Safari 13.2
   no tiene `:has()` y sin la clase quedaban dos scrolls anidados y el pie sin
   anclar) y, de red, `:has(> .sidebar-nav)`.
   Las tres piezas de `.sidebar-nav` van juntas: `flex: 1` la estira hasta el
   pie, `min-height: 0` la deja encogerse por debajo de su contenido (sin eso
   `overflow-y: auto` no scrollea y empuja el pie fuera de pantalla). */
.sidebar.sidebar--anclado {
    overflow-y: hidden;
}
/* Red: un menú propio que no puso la clase pero sí envuelve la lista en
   `.sidebar-nav`. APARTE (no en la lista de arriba): un `:has()` no soportado
   invalidaría la regla entera, también el selector de la clase. */
.sidebar:has(> .sidebar-nav) {
    overflow-y: hidden;
}

.sidebar-nav {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

/* Pantallas bajas (celular horizontal, ~360px de alto): el pie mide ~320px en
   Forge y con la lista encogida a 0 quedaría cortado sin scroll («Cerrar
   sesión» inalcanzable). Debajo de 520px de alto el aside entero vuelve a
   scrollear como antes y la lista deja de encogerse. 520px: holgura sobre los
   ~360-430px de un móvil en horizontal, y por encima de eso (tablet, desktop)
   el pie anclado cabe con lista visible. */
@media (max-height: 520px) {
    .sidebar.sidebar--anclado {
        overflow-y: auto;
    }

    .sidebar:has(> .sidebar-nav) {
        overflow-y: auto;
    }

    .sidebar-nav {
        flex: none;
        overflow: visible;
    }
}

.sidebar-logo {
    text-decoration: none;   /* es <a> a Inicio: mismo aspecto que antes */
    color: inherit;
    padding: var(--space-5) var(--space-5) var(--space-4);
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid var(--border-subtle);
    margin-bottom: var(--space-2);
}

.sidebar-logo-text {
    font-family: var(--font-heading);
    font-weight: 700;
    font-size: var(--fs-lg);
    color: var(--text-primary);
    line-height: 1.2;
    font-style: normal;
}

.sidebar-logo-divider {
    width: 1px;
    height: 20px;
    background: var(--border-strong);
    flex-shrink: 0;
}

.sidebar-logo-sub {
    font-size: var(--fs-xs);
    color: var(--text-tertiary);
    letter-spacing: 0.02em;
    font-weight: 400;
}

.sidebar-section {
    padding: var(--space-4) var(--space-5) var(--space-2);
    font-size: 0.6875rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-tertiary);
}

.sidebar-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-5);
    margin: 1px var(--space-2);
    border-radius: var(--radius-md);
    font-size: var(--fs-sm);
    font-weight: 500;
    color: var(--text-secondary);
    border-left: 3px solid transparent;
    transition: background var(--duration) var(--ease),
                color var(--duration) var(--ease),
                border-color var(--duration) var(--ease);
    cursor: pointer;
}

.sidebar-item:hover {
    background: var(--bg-surface-hover);
    color: var(--text-primary);
}

.sidebar-item.active {
    background: var(--brand-subtle);
    color: var(--brand-strong);
    border-left-color: var(--brand);
    font-weight: 600;
}

.sidebar-item.disabled {
    color: var(--text-tertiary);
    pointer-events: none;
    opacity: var(--opacity-inactivo, .55);
}

.sidebar-subitem {
    padding-left: var(--space-8);
    font-size: var(--fs-xs);
}

.sidebar-item i {
    font-size: var(--fs-base);
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}

.sidebar-bottom {
    margin-top: auto;
    padding: var(--space-3) 0 var(--space-4);
    border-top: 1px solid var(--border-subtle);
}

/* --- Pie de sidebar compartido (tier2 A2) -------------------- */
.sidebar-session {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-3);
    pointer-events: none;
}
.sidebar-session-label {
    font-size: var(--fs-xs);
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.sidebar-session-name {
    font-size: var(--fs-sm);
    color: var(--text-secondary);
}
.sidebar-logout { color: var(--danger-text); }

/* Acceso al panel (god mode): destacado, anclado al fondo. */
.sidebar-item.is-panel {
    background: var(--success-bg);
    color: var(--success-text);
    font-weight: 600;
}
.sidebar-item.is-panel:hover { background: var(--success-bg); color: var(--success-text); }
/* Sobre --success-bg el sólido daba 2,35:1 (ni 3:1): el ícono va en el tono de texto. */
.sidebar-item.is-panel i { color: var(--success-text); }

/* Selector de empresa (pie del sidebar, SuperAdmin) */
.empresa-switcher {
    position: relative;
    margin: 0 var(--space-2) var(--space-2);
}
.empresa-switcher-toggle {
    width: 100%;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    background: var(--bg-surface-alt);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: var(--fs-sm);
    text-align: left;
    cursor: pointer;
    transition: background var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.empresa-switcher-toggle:hover {
    background: var(--bg-surface-hover);
    border-color: var(--border-strong);
}
.empresa-switcher-name {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
    color: var(--text-primary);
}
.empresa-switcher-caret {
    color: var(--text-tertiary);
    font-size: var(--fs-xs);
}
.empresa-switcher-menu {
    position: absolute;
    bottom: calc(100% + 4px);
    left: 0;
    right: 0;
    z-index: 50;
    padding: var(--space-1);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    max-height: 260px;
    overflow-y: auto;
}
.empresa-switcher-menu form {
    margin: 0;
}
.empresa-switcher-item {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    border: 0;
    border-radius: var(--radius-sm);
    background: none;
    color: var(--text-secondary);
    font-family: var(--font-body);
    font-size: var(--fs-sm);
    text-align: left;
    cursor: pointer;
}
.empresa-switcher-item:hover {
    background: var(--brand-subtle);
    color: var(--text-primary);
}
.empresa-switcher-item.is-active {
    color: var(--brand-strong);
    font-weight: 600;
}

/* Hamburger toggle (hidden on desktop) */
.sidebar-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: none;
    border-radius: var(--radius-md);
    background: transparent;
    color: var(--text-secondary);
    font-size: var(--fs-md);
    cursor: pointer;
    transition: background var(--duration) var(--ease);
}

.sidebar-toggle:hover {
    background: var(--bg-surface-hover);
}

/* Sidebar overlay (mobile) */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--overlay);
    z-index: 150;
}

/* --- App Main ----------------------------------------------- */
.app-main {
    margin-left: var(--sidebar-width);
    margin-top: var(--topnav-height);
    padding: var(--space-8);
    min-height: calc(100vh - var(--topnav-height));
    transition: margin-left var(--duration) var(--ease);
}

/* --- Responsive --------------------------------------------- */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        width: 260px;
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: var(--shadow-lg);
    }

    .sidebar-overlay.open {
        display: block;
    }

    .sidebar-toggle {
        display: inline-flex;
    }

    .topnav {
        left: 0;
    }

    .app-main {
        margin-left: 0;
        padding: var(--space-4);
    }

    .topnav-right-desktop { display: none; }
    .topnav-kebab { display: inline-flex; }
}

@media (max-width: 480px) {
    .app-main {
        padding: var(--space-3);
    }

    .topnav {
        padding: 0 var(--space-4);
    }
}
