@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
    --primary: #2dbda8;
    --primary-light: #e6f6f4;
    --dark: #1e293b;
    --bg-main: #f8fafc;
    --border-color: #e2e8f0;
    --sidebar-width: 250px;
    --navbar-height: 60px;
    
    /* Box Shadows (Alegra Style) */
    --shadow-soft: 0px 4px 12px rgba(0, 0, 0, 0.03), 0px 1px 3px rgba(0, 0, 0, 0.05);

    /* Paleta Cromática Suave de Textos (Cero Negro Puro) */
    --text-main: #0c1a30;
    --text-body: #4a5568;
    --text-muted: #718096;
    --text-link: #0070f3;
    
    /* Variables de Grosor Estricto */
    --weight-regular: 400;
    --weight-medium: 500;
    --weight-semibold: 600;
    --weight-bold: 700;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif !important;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

body {
    background-color: var(--bg-main);
    color: var(--text-body);
}

/* Tipografía y Normalización */
.text-metric {
    font-size: 24px !important;
    font-weight: 750 !important;
}

.text-title {
    font-size: 18px !important;
    font-weight: 600 !important;
}

.text-body, .menu-item, .dropdown-trigger, .search-input {
    font-size: 14px !important;
}

.text-subtext {
    font-size: 12px !important;
    font-weight: 400 !important;
}

/* Sombras y Tarjetas */
.shadow-sm, .card, .dropdown-content, .dropdown-menu {
    box-shadow: var(--shadow-soft) !important;
}

#app-container {
    display: flex;
    min-height: 100vh;
}

/* SIDEBAR */
#sidebar {
    width: var(--sidebar-width);
    background-color: #fff;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 1000;
    overflow-y: auto;
}

.brand {
    height: var(--navbar-height);
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid var(--border-color);
}

.brand .logo {
    max-height: 35px;
}

.menu {
    padding: 15px 0;
    display: flex;
    flex-direction: column;
}

.menu-item, .dropdown-trigger {
    display: flex;
    align-items: center;
    padding: 10px 20px;
    color: #475569;
    text-decoration: none;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    background: none;
    border: none;
    width: 100%;
    text-align: left;
    transition: all 0.2s;
}

.menu-item:hover, .dropdown-trigger:hover {
    background-color: #f8fafc;
    color: var(--primary);
}

.menu-item.active {
    background-color: var(--primary-light);
    color: var(--primary);
    border-right: 3px solid var(--primary);
}

.menu .icon {
    width: 24px;
    margin-right: 10px;
    text-align: center;
    font-size: 16px;
}

.menu .arrow {
    margin-left: auto;
    font-size: 10px;
    transition: transform 0.3s;
}

/* DROPDOWNS */
.menu-dropdown .dropdown-content {
    display: none;
    flex-direction: column;
    background-color: #f8fafc;
}

.menu-dropdown.open .dropdown-content {
    display: flex;
}

.menu-dropdown.open .arrow {
    transform: rotate(180deg);
}

.dropdown-content a {
    padding: 8px 20px 8px 54px;
    color: #64748b;
    text-decoration: none;
    font-size: 13px;
}

.dropdown-content a:hover {
    color: var(--primary);
}

.dropdown-content a.active {
    color: var(--primary);
    font-weight: 600;
}

/* MAIN WRAPPER */
#main-wrapper {
    margin-left: var(--sidebar-width);
    flex: 1;
    display: flex;
    flex-direction: column;
    width: calc(100% - var(--sidebar-width));
}

/* Sin sesión: el main-wrapper ocupa el 100% (sin el hueco del sidebar) */
#app-container.unauthenticated #main-wrapper {
    margin-left: 0;
    width: 100%;
}


/* NAVBAR */
#navbar {
    height: var(--navbar-height);
    background-color: #fff;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px;
    position: sticky;
    top: 0;
    z-index: 999;
    box-shadow: var(--shadow-soft);
}

.search-box {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    max-width: 500px;
}

#global-search {
    flex: 1;
    padding: 8px 16px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    outline: none;
    font-size: 14px;
}

#global-search:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-light);
}

.btn-add {
    background-color: #fff;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    width: 34px;
    height: 34px;
    font-size: 20px;
    color: #475569;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.btn-add:hover {
    background-color: #f1f5f9;
}

.user-profile {
    display: flex;
    align-items: center;
}

#user-avatar {
    width: 36px;
    height: 36px;
    background-color: var(--primary);
    color: #fff;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 14px;
}

/* VIEWPORT & FALLBACK STYLES */
#view-viewport {
    padding: 24px;
    flex: 1;
}

.placeholder-container {
    background-color: #fff;
    padding: 40px 24px;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    max-width: 800px;
    margin: 40px auto;
    text-align: center;
}

.placeholder-container h3 {
    margin-bottom: 10px;
    font-size: 24px;
}

.placeholder-container p {
    color: var(--text-muted);
}

/* ==============================================================
   MOTOR DE IMPRESIÓN (SINGLE SOURCE OF TRUTH)
   ============================================================== */
@media screen {
    .print-document-template {
        position: absolute !important;
        opacity: 0 !important;
        pointer-events: none !important;
        z-index: -9999 !important;
        height: 1px !important;
        overflow: hidden !important;
    }
}

@media print {
    @page {
        size: letter;
        margin: 0.5in;
    }

    #app-container {
        position: absolute !important;
        height: 0 !important;
        overflow: hidden !important;
        opacity: 0 !important;
        pointer-events: none !important;
    }
    
    .sidebar, .navbar, .topbar, .global-actions-bar, input, button, select, textarea {
        display: none !important;
    }
    
    body {
        background-color: white !important;
    }
    
    .print-document-template {
        display: block !important;
        width: 100%;
        position: relative !important;
        background: white;
        padding: 40px;
        font-family: 'Inter', sans-serif;
        color: #212529;
    }

    /* Regla de tamaño dinámico según cantidad de ítems */
    .print-document-template.media-hoja { 
        min-height: 148mm; 
        max-height: none; 
    }
    
    .print-document-template.hoja-completa { 
        height: auto; 
        page-break-inside: auto; 
    }
}

/* ==========================================================
   Responsive Fixes (Reemplaza las reglas rotas de app.css)
   ========================================================== */
@media (max-width: 768px) {
    #sidebar { 
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        position: fixed;
        z-index: 1040;
        height: 100vh;
    }
    #sidebar.open { 
        transform: translateX(0);
        box-shadow: 4px 0 15px rgba(0,0,0,0.1);
    }
    #main-wrapper { 
        margin-left: 0; 
        width: 100%; 
    }
    
    #view-viewport {
        padding: 12px !important;
    }
    
    /* Adaptar cabecera superior (Top Bar) en columnas en móviles */
    .module-container > .d-flex:first-of-type {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 16px !important;
    }
    
    .module-container > .d-flex:first-of-type > div,
    .module-container > .d-flex:first-of-type > .d-flex {
        width: 100% !important;
    }
    
    .module-container > .d-flex:first-of-type > .d-flex {
        justify-content: flex-start !important;
        gap: 12px !important;
    }

    /* Área de toque mínima de 44px para botones del Top Bar y menú móvil */
    .module-container > .d-flex:first-of-type button,
    .module-container > .d-flex:first-of-type .btn,
    #mobile-menu-toggle {
        min-height: 44px !important;
        min-width: 44px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
    
    /* Prevenir que las tablas grandes rompan la pantalla en móvil */
    .table-responsive,
    .form-table-wrapper,
    table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Tabla de detalles responsiva estilo tarjetas en formularios */
    #tabla-detalles, 
    #tabla-detalles thead, 
    #tabla-detalles tbody { 
        display: block !important; 
        width: 100% !important;
    }
    
    #tabla-detalles thead {
        display: none !important;
    }
    
    #tabla-detalles tbody tr {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 12px 2% !important;
        margin-bottom: 20px !important;
        border: 1px solid var(--border-color) !important;
        padding: 16px !important;
        border-radius: 8px !important;
        background-color: #f8fafc !important;
        position: relative !important;
        width: 100% !important;
    }
    
    #tabla-detalles tbody td {
        display: block !important;
        padding: 0 !important;
        border: none !important;
        margin: 0 !important;
    }

    /* Fila 1: Número de línea (título de la tarjeta) - 100% */
    #tabla-detalles tbody td:nth-of-type(1) {
        width: 100% !important;
        font-weight: 700 !important;
        color: var(--primary) !important;
        border-bottom: 1px solid var(--border-color) !important;
        padding-bottom: 8px !important;
        margin-bottom: 4px !important;
    }

    /* Fila 2: Producto o servicio - 100% */
    #tabla-detalles tbody td:nth-of-type(2) {
        width: 100% !important;
        margin-bottom: 4px !important;
    }
    #tabla-detalles tbody td:nth-of-type(2)::before { content: "Producto o servicio"; font-size: 11px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }

    /* Fila 3: Cantidad (49%) y Precio (49%) */
    #tabla-detalles tbody td:nth-of-type(3) {
        width: 49% !important;
    }
    #tabla-detalles tbody td:nth-of-type(3)::before { content: "Cantidad"; font-size: 11px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }
    
    #tabla-detalles tbody td:nth-of-type(4) {
        width: 49% !important;
    }
    #tabla-detalles tbody td:nth-of-type(4)::before { content: "Precio"; font-size: 11px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }

    /* Fila 4: Descuento (49%) e Impuesto (49%) - Ocultos por defecto en móvil */
    #tabla-detalles tbody tr:not(.show-discount-tax) td:nth-of-type(5),
    #tabla-detalles tbody tr:not(.show-discount-tax) td:nth-of-type(6) {
        display: none !important;
    }

    #tabla-detalles tbody td:nth-of-type(5) {
        width: 49% !important;
    }
    #tabla-detalles tbody td:nth-of-type(5)::before { content: "Descuento %"; font-size: 11px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }
    
    #tabla-detalles tbody td:nth-of-type(6) {
        width: 49% !important;
    }
    #tabla-detalles tbody td:nth-of-type(6)::before { content: "Impuesto %"; font-size: 11px; font-weight: 600; color: var(--text-muted); display: block; margin-bottom: 4px; }

    /* Fila 5: Subtotal - 100% */
    #tabla-detalles tbody td:nth-of-type(7) {
        width: 100% !important;
        margin-top: 8px !important;
        border-top: 1px dashed var(--border-color) !important;
        padding-top: 12px !important;
        display: flex !important;
        justify-content: space-between !important;
        align-items: center !important;
    }
    #tabla-detalles tbody td:nth-of-type(7)::before {
        content: "Subtotal";
        font-size: 13px;
        font-weight: 600;
        color: var(--text-muted);
    }

    /* Fila 6: Botón de eliminar (posicionado absoluto) */
    #tabla-detalles tbody td:nth-of-type(8) {
        position: absolute !important;
        top: 16px !important;
        right: 16px !important;
        width: auto !important;
    }

    /* Layout resumen conciliacion móvil */
    #resumen-conciliacion-row {
        flex-wrap: wrap !important;
    }
    #resumen-conciliacion-row > .col-3 {
        width: 50% !important;
        margin-bottom: 16px !important;
        border-bottom: 1px solid var(--border-color) !important;
    }
    #resumen-conciliacion-row > .col-3:nth-child(2),
    #resumen-conciliacion-row > .col-3:nth-child(4) {
        border-right: none !important;
    }
}
