/* ==========================================================================
   Yupi-Factura · Tema unificado MD3 (Azul Corporativo)
   ========================================================================== */

:root {
    --yupi-primary:        #0B57D0;
    --yupi-primary-dark:   #0842A0;
    --yupi-primary-light:  #A8C7FA;

    --yupi-dark-gradient: linear-gradient(145deg,
        #0A369D 0%,
        #07246E 40%,
        #041445 75%,
        #020A2E 100%);

    --md-surface:           #FFFFFF;
    --md-surface-container: #F4F6FA;
    --md-surface-variant:   #E3E6F0;
    --md-outline:           #C7CCD9;

    --app-header-h:  88px;
    --sidebar-width: 288px;

    --bs-primary: var(--yupi-primary);
    --bs-primary-rgb: 11, 87, 208;
    --bs-body-bg: var(--md-surface);
}

[data-bs-theme="dark"] {
    --md-surface:           #11151C;
    --md-surface-container: #1A1F2A;
    --md-surface-variant:   #232A38;
    --md-outline:           #3A4253;
    --bs-body-bg: var(--md-surface);
}

/* ---------- Base ---------- */
html, body { height: 100%; }

body {
    font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    background-color: var(--bs-body-bg);
    color: var(--bs-body-color);
    transition: background-color .3s ease, color .3s ease;
}

/* Tarjetas y Paneles MD3 */
.dashboard-panel {
    border: 1px solid var(--md-outline);
    background-color: var(--md-surface);
    transition: border-color .2s ease, box-shadow .2s ease;
}

.kpi-label {
    font-size: .75rem;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--bs-secondary-color);
}

/* ==========================================================================
   INPUTS Y GRUPOS EN FORMA DE PÍLDORA (Rounded-Pill / Suaves)
   ========================================================================== */
.input-group.rounded-pill,
.input-group-pill {
    display: flex;
    align-items: center;
    width: 100%;
    border-radius: 50rem !important;
    overflow: hidden;
    border: 1px solid var(--md-outline, #C7CCD9) !important;
    background-color: var(--bs-body-tertiary, #f8f9fa);
    transition: border-color .2s ease, box-shadow .2s ease, background-color .2s ease;
}

.input-group.rounded-pill:focus-within,
.input-group-pill:focus-within {
    border-color: var(--yupi-primary, #0B57D0) !important;
    box-shadow: 0 0 0 3px rgba(11, 87, 208, 0.15) !important;
    background-color: var(--md-surface, #ffffff);
}

.input-group.rounded-pill .form-control,
.input-group.rounded-pill .input-group-text,
.input-group.rounded-pill .btn,
.input-group-pill .form-control,
.input-group-pill .input-group-text,
.input-group-pill .btn {
    border: none !important;
    background: transparent !important;
    box-shadow: none !important;
}

.input-group.rounded-pill .form-control,
.input-group-pill .form-control {
    padding-left: 1rem;
    padding-right: 1rem;
}

.input-group.rounded-pill .input-group-text,
.input-group-pill .input-group-text {
    padding-left: 1.15rem;
    padding-right: 0.5rem;
    color: var(--bs-secondary-color, #64748b);
}

/* ==========================================================================
   HERO AZUL (login)
   ========================================================================== */
.bg-yupi-hero {
    background-color: #0A369D;
    background-image: var(--yupi-dark-gradient);
    background-repeat: no-repeat;
    background-size: cover;
    color: #fff;
    position: relative;
    isolation: isolate;
}

.bg-yupi-hero::before {
    content: '';
    position: absolute;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    background-image:
        radial-gradient(circle at 10% 20%, rgba(255,255,255,.08) 0%, transparent 40%),
        radial-gradient(circle at 90% 80%, rgba(13,110,253,.18) 0%, transparent 50%);
}

.bg-yupi-hero > * { position: relative; z-index: 1; }

.feature-glass {
    background: rgba(255, 255, 255, 0.10);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid rgba(255, 255, 255, 0.18);
    color: #fff;
    transition: transform .3s cubic-bezier(.16,1,.3,1),
                background .3s ease, border-color .3s ease;
}
.feature-glass:hover {
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.35);
    transform: translateY(-5px);
}
.feature-glass i,
.feature-glass span { color: #fff; }

/* ==========================================================================
   BOTÓN FLOTANTE DE TEMA MD3
   ========================================================================== */
.theme-toggle {
    position: fixed;
    top: 1.25rem;
    right: 1.25rem;
    z-index: 1060;
    width: 48px;
    height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--md-outline);
    border-radius: 50%;
    background: var(--md-surface);
    color: var(--yupi-primary);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, .12);
    transition: transform .3s ease, background-color .3s ease, color .3s ease;
    cursor: pointer;
}
.theme-toggle:hover {
    transform: scale(1.08) rotate(10deg);
    background: var(--yupi-primary);
    color: #fff;
    border-color: var(--yupi-primary);
}
.theme-toggle:focus-visible {
    outline: 3px solid var(--yupi-primary-light);
    outline-offset: 3px;
}

/* ==========================================================================
   INDEX · Header
   ========================================================================== */
.app-header {
    position: fixed;
    inset: 0 0 auto 0;
    z-index: 1035;
    background-color: #0A369D;
    background-image: var(--yupi-dark-gradient);
    color: #fff;
    border-bottom: 1px solid rgba(255,255,255,.08);
    box-shadow: 0 8px 24px rgba(0,0,0,.18);
}

.app-header .header-top {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 10px 18px;
}

.app-header .header-top > .row {
    flex: 1;
    min-width: 0;
    margin: 0;
    display: grid;
    grid-template-columns: 110px minmax(0,1fr) auto auto;
    gap: 16px;
    align-items: center;
}
.app-header .header-top > .row > div {
    width: auto;
    min-width: 0;
    padding: 0;
}
.app-header #empresa_info,
.app-header #empresa_info2,
.app-header #empresa_info3 { color: #fff; overflow-wrap: anywhere; }

.app-header #empresa_info img {
    max-height: 48px;
    max-width: 100%;
    object-fit: contain;
    display: block;
}
.app-header #empresa_info2 h2 {
    font-size: clamp(1rem, 1.6vw, 1.4rem);
    line-height: 1.25;
    margin: 0;
}

/* ==========================================================================
   INDEX · Sidebar
   ========================================================================== */
#appSidebar {
    position: fixed;
    top: var(--app-header-h);
    bottom: 0;
    left: 0;
    width: var(--sidebar-width);
    padding: 12px;
    overflow-y: auto;
    overflow-x: hidden;
    overscroll-behavior: contain;
    background: var(--md-surface);
    border-right: 1px solid var(--md-outline);
    z-index: 1034;
    transition: transform .25s ease, visibility .25s ease;
    scrollbar-width: thin;
    scrollbar-color: var(--yupi-primary) transparent;
}
#appSidebar::-webkit-scrollbar { width: 10px; }
#appSidebar::-webkit-scrollbar-thumb {
    background: rgba(11,87,208,.3);
    border-radius: 999px;
    border: 2px solid transparent;
    background-clip: padding-box;
}

.sidebar-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 8px 12px;
    color: var(--bs-secondary-color);
    font-size: .72rem;
    font-weight: 700;
    letter-spacing: .12em;
    text-transform: uppercase;
}

.sidebar-control {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 44px;
    min-width: 44px;
    padding: 8px 12px;
    border: 1px solid rgba(255,255,255,.14);
    border-radius: 12px;
    background: rgba(255,255,255,.08);
    color: #fff;
    cursor: pointer;
    transition: background-color .2s ease, transform .2s ease;
}
.sidebar-control:hover {
    background: rgba(255,255,255,.16);
    transform: translateY(-1px);
}
.sidebar-control i { font-size: 1.15rem; }
#sidebarClose { display: none; }

/* ==========================================================================
   INDEX · Navegación
   ========================================================================== */
.app-topnav {
    border-radius: 16px;
    background: transparent;
    border: none;
    padding: 0;
}

#topNav { gap: 6px; width: 100%; }

.menu-link {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    min-height: 46px;
    padding: 10px 14px;
    border-radius: 12px;
    border: 1px solid var(--md-outline);
    background: var(--md-surface-container);
    color: var(--bs-body-color);
    font-weight: 600;
    font-size: .92rem;
    text-decoration: none;
    transition: background-color .2s ease, color .2s ease, border-color .2s ease;
}
[data-bs-theme="dark"] .menu-link {
    background: var(--md-surface-variant);
}
.menu-link i { color: var(--yupi-primary); font-size: 1.05rem; }

.menu-link:hover,
.menu-link:focus,
.menu-link.show {
    background: rgba(11,87,208,.08);
    border-color: var(--yupi-primary);
    color: var(--bs-body-color);
}
.menu-link.active {
    background: linear-gradient(135deg, rgba(11,87,208,.16), rgba(168,199,250,.14));
    color: var(--yupi-primary) !important;
    border-color: var(--yupi-primary);
}
[data-bs-theme="dark"] .menu-link.active {
    color: var(--yupi-primary-light) !important;
    background: linear-gradient(135deg, rgba(11,87,208,.28), rgba(168,199,250,.10));
    border-color: var(--yupi-primary-light);
}

/* ==========================================================================
   INDEX · Desplegables y Menú Desplegable Limpio (Sin exceso de bordes)
   ========================================================================== */
.dropdown-menu {
    border-radius: 14px;
    padding: 8px;
    border: 1px solid var(--md-outline);
    background: var(--md-surface);
    box-shadow: 0 16px 32px rgba(0,0,0,.12);
    max-width: calc(100vw - 32px);
    z-index: 1080;
}

/* Acordeón limpio dentro del Sidebar sin bordes redundantes */
#appSidebar .dropdown-menu {
    position: static !important;
    float: none;
    width: 100% !important;
    margin-top: 6px;
    padding: 6px;
    box-shadow: none !important;
    border: 1px solid var(--md-outline);
    background: var(--md-surface-container);
    transform: none !important;
}

[data-bs-theme="dark"] #appSidebar .dropdown-menu {
    background: var(--md-surface-variant);
    border-color: var(--md-outline);
}

.mega-menu-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.mega-menu-section {
    padding: 4px;
    border: none;
    background: transparent;
}

.mega-section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 8px 4px 6px;
    padding: 0 4px;
    font-size: .7rem;
    font-weight: 800;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--bs-secondary-color);
}
.mega-section-title i { color: var(--yupi-primary); }

.dropdown-item.mega-link {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid transparent;
    color: var(--bs-body-color);
    background: var(--md-surface);
    white-space: normal;
    margin-bottom: 4px;
    box-shadow: 0 2px 6px rgba(0,0,0,.03);
    transition: background-color .2s ease, border-color .2s ease, transform .2s ease;
}
[data-bs-theme="dark"] .dropdown-item.mega-link {
    background: rgba(255, 255, 255, 0.03);
}

.dropdown-item.mega-link:hover,
.dropdown-item.mega-link:focus {
    background: rgba(11,87,208,.08);
    border-color: rgba(11,87,208,.2);
    transform: translateX(2px);
}
.dropdown-item.mega-link.active {
    background: linear-gradient(135deg, rgba(11,87,208,.16), rgba(168,199,250,.12));
    border-color: var(--yupi-primary);
    color: var(--yupi-primary);
}

/* Iconos de las opciones: Garantizamos colores sólidos visibles en modo claro y oscuro */
.mega-link-icon {
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #ffffff !important;
    box-shadow: 0 4px 10px rgba(0,0,0,.15);
}
.mega-link-icon i { 
    color: #ffffff !important; 
    font-size: .95rem; 
    -webkit-text-fill-color: #ffffff !important;
}

.mega-link-text strong { display: block; font-size: .86rem; font-weight: 700; line-height: 1.2; color: var(--bs-body-color); }
.mega-link-text small  { color: var(--bs-secondary-color); font-size: .73rem; line-height: 1.15; }

/* ==========================================================================
   INDEX · Main
   ========================================================================== */
body > .app-main {
    margin-top: var(--app-header-h);
    margin-left: var(--sidebar-width);
    height: calc(100vh - var(--app-header-h));
    height: calc(100dvh - var(--app-header-h));
    padding: 18px;
    overflow-y: auto;
    overflow-x: hidden;
    scroll-behavior: smooth;
    transition: margin-left .25s ease;
}
body > .app-main::-webkit-scrollbar { width: 11px; }
body > .app-main::-webkit-scrollbar-thumb {
    background: rgba(11,87,208,.25);
    border-radius: 999px;
    border: 2px solid transparent;
    background-clip: padding-box;
}
#contenido { min-height: 100%; border-radius: 20px; }

/* Dashboard */
.home-dashboard { padding: 8px; }

.home-hero {
    display: flex;
    align-items: center;
    gap: 18px;
    padding: 22px;
    border-radius: 20px;
    border: 1px solid var(--md-outline);
    background: var(--md-surface);
    box-shadow: 0 8px 24px rgba(0,0,0,.06);
    margin-bottom: 24px;
}
.home-hero-icon {
    flex-shrink: 0;
    width: 68px;
    height: 68px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.9rem;
    color: #fff;
    background: var(--yupi-dark-gradient);
    box-shadow: 0 8px 20px rgba(11,87,208,.28);
}
.home-section-title {
    font-size: 1.05rem;
    font-weight: 700;
    margin-bottom: 16px;
    color: var(--bs-emphasis-color);
}

.dashboard-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.75fr) minmax(280px, .95fr);
    gap: 18px;
}

.dashboard-panel,
.kpi-card,
.app-tile {
    border: 1px solid var(--md-outline);
    border-radius: 20px;
    background: var(--md-surface);
    box-shadow: 0 6px 18px rgba(0,0,0,.05);
    transition: transform .2s ease, box-shadow .2s ease, border-color .2s ease;
}
.dashboard-panel { padding: 18px; }
.kpi-card        { padding: 18px; }

.kpi-label {
    font-size: .74rem;
    font-weight: 800;
    letter-spacing: .08em;
    text-transform: uppercase;
    color: var(--bs-secondary-color);
    margin-bottom: 10px;
}
.kpi-value {
    font-size: clamp(1.3rem, 2vw, 2rem);
    line-height: 1;
    font-weight: 800;
    margin-bottom: 8px;
    color: var(--bs-emphasis-color);
}
.kpi-foot {
    font-size: .85rem;
    color: var(--bs-secondary-color);
    margin: 0;
}

.tone-cf  { background: linear-gradient(135deg, rgba(11,87,208,.12), rgba(6,182,212,.08)); }
.tone-ccf { background: linear-gradient(135deg, rgba(22,163,74,.12), rgba(16,185,129,.08)); }

.app-tile {
    padding: 20px 14px;
    text-align: center;
    cursor: pointer;
    min-height: 148px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    user-select: none;
}
.app-tile:hover {
    transform: translateY(-4px);
    border-color: var(--yupi-primary);
    box-shadow: 0 14px 30px rgba(0,0,0,.12);
}
.app-tile-icon {
    width: 64px;
    height: 64px;
    border-radius: 18px;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
    color: #fff;
    background: var(--yupi-dark-gradient);
    box-shadow: 0 8px 20px rgba(11,87,208,.25);
}
.app-tile-title    { font-weight: 700; font-size: .95rem; }
.app-tile-subtitle { font-size: .8rem; color: var(--bs-secondary-color); }

.app-launcher {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px,1fr));
    gap: 16px;
}

/* Utilidades */
.user-dropdown {
    position: relative;
    display: inline-block;
    padding: 6px 10px;
    border-radius: 12px;
    cursor: pointer;
}
.dropdown-content {
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    min-width: 170px;
    padding: 6px;
    background: var(--md-surface);
    border: 1px solid var(--md-outline);
    border-radius: 12px;
    box-shadow: 0 16px 30px rgba(0,0,0,.18);
    opacity: 0;
    visibility: hidden;
    transform: translateY(8px);
    transition: opacity .2s ease, transform .2s ease, visibility .2s ease;
    z-index: 20;
}
.user-dropdown:hover .dropdown-content,
.user-dropdown.user-open .dropdown-content {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}
.btn-logout {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-radius: 10px;
    color: #dc3545;
    font-weight: 600;
    text-decoration: none;
    transition: background .25s ease, color .25s ease;
}
.btn-logout:hover {
    background: #dc3545;
    color: #fff !important;
}
:focus-visible {
    outline: 2px solid var(--yupi-primary-light);
    outline-offset: 2px;
}

/* Animaciones */
@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(24px); }
    to   { opacity: 1; transform: translateY(0); }
}
.animate-fade-up { animation: fadeInUp .7s cubic-bezier(.16,1,.3,1) both; }

@media (prefers-reduced-motion: reduce) {
    .animate-fade-up { animation: none; }
}

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 1199.98px) {
    #appSidebar {
        width: min(320px, calc(100vw - 40px));
        transform: translateX(-100%);
        visibility: hidden;
        box-shadow: 16px 0 40px rgba(0,0,0,.22);
    }
    body.sidebar-open #appSidebar { transform: translateX(0); visibility: visible; }
    body > .app-main { margin-left: 0; }
    body.sidebar-open > .app-main { overflow: hidden; }

    .app-header .header-top > .row {
        grid-template-columns: 80px minmax(0,1fr) auto;
        gap: 8px 12px;
    }
    .app-header .header-top > .row > div:nth-child(3) { grid-column: 2; grid-row: 2; }
    .app-header .header-top > .row > div:nth-child(4) { grid-column: 3; grid-row: 1 / 3; }
    .app-header .header-top > .row > div:first-child { grid-row: 1 / 3; }

    .dashboard-grid { grid-template-columns: 1fr; }
    #sidebarClose { display: inline-flex; }
}

@media (max-width: 575.98px) {
    .app-header .header-top { padding: 8px; gap: 8px; align-items: flex-start; }
    .app-header .header-top > .row {
        grid-template-columns: minmax(0,1fr) auto;
        gap: 6px;
    }
    .app-header .header-top > .row > div:first-child { grid-column: 1; grid-row: 1; }
    .app-header .header-top > .row > div:nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
    .app-header .header-top > .row > div:nth-child(3) { grid-column: 1 / -1; grid-row: 3; }
    .app-header .header-top > .row > div:nth-child(4) { grid-column: 2; grid-row: 1; }

    body > .app-main { padding: 8px; }
    .home-dashboard { padding: 4px; }
    .home-hero { padding: 14px; border-radius: 16px; flex-wrap: wrap; }
    .home-hero-icon { width: 52px; height: 52px; font-size: 1.5rem; }
    .app-launcher { grid-template-columns: repeat(2, minmax(0,1fr)); gap: 12px; }
    .app-tile { min-height: 130px; padding: 14px 10px; }
    .app-tile-icon { width: 52px; height: 52px; font-size: 1.5rem; }
}

body.sidebar-collapsed #appSidebar { transform: translateX(-100%); visibility: hidden; }
body.sidebar-collapsed > .app-main { margin-left: 0; }