:root {
    --sf-primary: #0ea5e9; --sf-dark: #0369a1; --sf-bg: #f0f9ff;
    --sf-white: #ffffff; --sf-border: #bae6fd; --sf-text: #0f172a;
    --sf-gray: #64748b; --sf-green: #22c55e;
}
body { background: #f8fafc; margin: 0; }
.sf-wrap {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--sf-bg); color: var(--sf-text);
    max-width: 900px; margin: 20px auto; padding: 20px;
    border-radius: 16px; box-shadow: 0 4px 20px rgba(14, 165, 233, 0.1);
    box-sizing: border-box; position: relative;
}
.sf-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; flex-wrap: wrap; gap: 10px;}
.sf-logo-img { height: 60px; width: auto; object-fit: contain; border-radius: 4px; }
.sf-controls { display: flex; align-items: center; gap: 10px; }

/* Boton Descarga PDF */
.sf-download-box {
    display: flex; align-items: center; justify-content: center; gap: 12px;
    background-color: #e0f2fe; border: 1px solid #bae6fd;
    color: var(--sf-dark); text-decoration: none; padding: 12px 20px;
    margin-bottom: 20px; border-radius: 10px; transition: all 0.2s ease; cursor: pointer;
}
.sf-download-box:hover { background-color: #bae6fd; transform: translateY(-1px); }
.sf-dl-icon { font-size: 24px; }
.sf-dl-content { display: flex; flex-direction: column; text-align: left; }
.sf-dl-title { font-weight: 700; font-size: 15px; line-height: 1.2; }
.sf-dl-date { font-size: 12px; color: var(--sf-gray); font-weight: 500; margin-top: 2px; }

/* Switcher y Botones */
.sf-price-switch { background: #e0f2fe; padding: 4px; border-radius: 8px; display: flex; }
.sf-price-switch label {
    padding: 8px 12px; font-size: 13px; color: var(--sf-dark);
    border-radius: 6px; cursor: pointer; font-weight: 500; transition: all 0.2s;
}
.sf-price-switch label.active {
    background: var(--sf-white); color: var(--sf-primary);
    box-shadow: 0 1px 3px rgba(0,0,0,0.1); font-weight: 700;
}
.sf-price-switch input { display: none; }
.sf-icon-btn {
    background: #e0f2fe; color: var(--sf-dark); border: none;
    width: 38px; height: 38px; border-radius: 50%; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.sf-icon-btn:hover { color: #dc2626; }

/* Cards Layout */
.sf-card {
    background: var(--sf-white); border: 1px solid var(--sf-border);
    border-radius: 12px; padding: 15px; margin-bottom: 15px;
}
.sf-card-header {
    display: flex; justify-content: space-between; align-items: center;
    margin-bottom: 10px; cursor: pointer; font-weight: 700; color: var(--sf-dark);
}
.sf-chevron { transition: transform 0.2s; }
.sf-card-header.collapsed .sf-chevron { transform: rotate(-90deg); }

/* Form Grid */
.sf-form-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.sf-form-grid.hidden { display: none; }
.sf-input-group { display: flex; flex-direction: column; gap: 4px; }
.sf-input-group label { font-size: 11px; color: var(--sf-gray); text-transform: uppercase; font-weight: 700; }
.sf-input-group input, .sf-input-group select, textarea {
    border: 1px solid var(--sf-border); border-radius: 8px; padding: 10px;
    font-size: 16px; background: #fcfdfe; width: 100%; box-sizing: border-box;
}
.sf-input-group input:focus { outline: 2px solid var(--sf-border); background: #fff; }

/* Search y Manual Item */
.sf-search-row { display: flex; gap: 10px; margin-bottom: 15px; align-items: stretch; }
.sf-search-bar { position: relative; flex-grow: 1; }
.sf-search-input-wrap { position: relative; display: flex; align-items: center; height: 100%; }
.sf-icon-search { position: absolute; left: 12px; font-size: 16px; color: var(--sf-gray); pointer-events: none; z-index: 2; }
#sf-search {
    padding: 12px 12px 12px 40px; border: 2px solid var(--sf-border);
    border-radius: 8px; width: 100%; box-sizing: border-box; height: 100%;
}
#sf-search:focus { border-color: var(--sf-primary); outline: none; }

.sf-btn-manual {
    background: #e0f2fe; border: 1px solid var(--sf-border);
    color: var(--sf-dark); border-radius: 8px; padding: 0 15px;
    font-weight: 600; cursor: pointer; white-space: nowrap;
    display: flex; align-items: center; gap: 5px; font-size: 13px;
}
.sf-btn-manual:active { background: #bae6fd; transform: scale(0.95); }

/* Resultados Busqueda */
.sf-results-list {
    position: absolute; top: 100%; left: 0; right: 0;
    background: #fff; border: 1px solid var(--sf-border);
    max-height: 250px; overflow-y: auto; z-index: 100;
    border-radius: 8px; box-shadow: 0 10px 20px rgba(0,0,0,0.1);
    display: none; margin-top: 5px;
}
.sf-results-list.show { display: block; }
.sf-res-item {
    padding: 12px; border-bottom: 1px solid #f0f9ff;
    cursor: pointer; display: flex; justify-content: space-between; align-items: center;
}
.sf-res-item:hover { background: #f0f9ff; }
.sf-res-name { font-weight: 600; font-size: 14px; color: var(--sf-text); }
.sf-res-price { font-size: 14px; color: var(--sf-primary); font-weight: 700; }

/* Tabla y Estilos Editables */
.sf-table-wrap { margin-bottom: 10px; border: 1px solid #e2e8f0; border-radius: 8px; overflow: hidden; background: #fff;}
.sf-table { width: 100%; border-collapse: collapse; background: #fff; }
.sf-table th {
    text-align: left; font-size: 11px; color: var(--sf-gray); text-transform: uppercase;
    background: #f8fafc; padding: 10px; border-bottom: 1px solid #e2e8f0;
}
.sf-table td { border-bottom: 1px solid #f1f5f9; padding: 8px; vertical-align: middle; }

/* --- INPUTS EDITABLES (Mejorados) --- */
.sf-edit-name, .sf-edit-price, .sf-qty-input {
    width: 100%; 
    border: none; /* Sin borde por defecto */
    border-bottom: 1px solid transparent; /* Reserva espacio para borde */
    background: transparent;
    padding: 6px 4px;
    border-radius: 0; /* Cuadrado para que se vea mas profesional en lista */
    font-family: inherit; font-size: 14px;
    color: var(--sf-text); box-sizing: border-box;
    transition: border-color 0.2s;
    -webkit-appearance: none; margin: 0;
}
/* Al hacer foco, solo borde abajo azul, sin fondo gris */
.sf-edit-name:focus, .sf-edit-price:focus, .sf-qty-input:focus { 
    outline: none; 
    border-bottom: 2px solid var(--sf-primary);
    background: transparent; 
}
.sf-edit-price { text-align: right; font-weight: 500; }
.sf-qty-input { text-align: center; }

/* Boton Borrar */
.sf-del-btn { color: #ef4444; background: none; border: none; font-size: 20px; cursor: pointer; padding: 0 10px;}

/* Footer Panel */
.sf-footer-panel {
    background: #f8fafc; border-radius: 8px; padding: 15px;
    display: grid; grid-template-columns: 1fr; gap: 15px;
}
.sf-obs-area textarea { resize: none; font-size: 13px; }
.sf-total-area { background: #fff; border: 1px solid var(--sf-border); border-radius: 8px; padding: 15px; }
.sf-total-line { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 13px; color: var(--sf-gray); }
.sf-total-big {
    display: flex; justify-content: space-between; border-top: 1px solid #e2e8f0; padding-top: 10px;
    font-size: 18px; color: var(--sf-primary); font-weight: 700;
}
.sf-discount-row input {
    width: 80px; padding: 4px; border: 1px solid var(--sf-border); 
    border-radius: 4px; text-align: right;
}
.sf-footer-checks { 
    margin-top: 10px; padding-top: 10px; border-top: 1px dashed #e2e8f0;
    display: flex; flex-direction: column; gap: 5px; font-size: 12px; color: #555;
}

/* Actions */
.sf-actions { display: grid; grid-template-columns: 1fr; gap: 15px; margin-top: 20px; }
.sf-btn {
    border: none; padding: 14px; border-radius: 10px; color: #fff;
    font-weight: 700; font-size: 15px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.btn-wa { background: var(--sf-green); }
.btn-pdf { background: var(--sf-primary); }
.sf-empty { text-align: center; padding: 20px; color: var(--sf-gray); font-size: 14px; }
.sf-spinner {
    display: none; width: 16px; height: 16px; border: 2px solid #ddd; border-top-color: var(--sf-primary);
    border-radius: 50%; animation: spin 1s linear infinite; position: absolute; right: 12px;
}

/* Modal */
.sf-modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5); z-index: 1000;
    display: flex; align-items: center; justify-content: center;
}
.sf-modal-overlay.hidden { display: none; }
.sf-modal-box {
    background: #fff; border-radius: 12px; width: 90%; max-width: 350px;
    padding: 20px; box-shadow: 0 10px 25px rgba(0,0,0,0.2);
    text-align: center; animation: popIn 0.2s ease-out;
}
@keyframes popIn { from { transform: scale(0.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }
.sf-modal-body { margin: 20px 0; text-align: left; }
.sf-modal-body input { width: 100%; padding: 10px; font-size: 18px; border: 2px solid var(--sf-border); border-radius: 8px; box-sizing: border-box; }
.sf-modal-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sf-btn-primary { background: var(--sf-primary); color: white; border: none; padding: 10px; border-radius: 8px; cursor: pointer; font-weight: bold; }
.sf-btn-secondary { background: #e2e8f0; color: var(--sf-text); border: none; padding: 10px; border-radius: 8px; cursor: pointer; font-weight: bold; }

/* --- OPTIMIZACION MOVIL (Transformacion a Cards) --- */
@media (max-width: 768px) {
    .sf-table thead { display: none; }
    
    .sf-table tbody tr {
        display: flex; flex-wrap: wrap; 
        border-bottom: 1px solid #e2e8f0; 
        padding: 12px 10px; /* Mas padding para dedos */
        position: relative;
    }
    
    /* Nombre del producto (full width) */
    .sf-table td:nth-child(1) {
        width: 100%; padding: 0 0 8px 0; order: 1;
        font-weight: 600; font-size: 15px; border: none;
    }
    .sf-edit-name { font-size: 16px; font-weight: 600; padding: 5px 0; }
    
    /* Nota */
    .sf-table td:nth-child(2) { display: none; } /* Ocultamos notas en mobile para limpiar */
    
    /* Inputs Numericos mas grandes en mobile */
    .sf-qty-input, .sf-edit-price {
        font-size: 16px; padding: 8px;
    }

    /* Layout Fila Inferior */
    .sf-table td:nth-child(3) { order: 3; width: 25%; border: none; padding: 0; } /* Qty */
    .sf-table td:nth-child(4) { order: 4; width: 35%; border: none; padding: 0 0 0 10px; } /* Precio */
    .sf-table td:nth-child(5) { 
        order: 5; width: 40%; border: none; padding: 0; 
        display: flex; align-items: center; justify-content: flex-end;
        font-size: 15px; font-weight: 700;
    } /* Total */

    .sf-table td:nth-child(6) {
        position: absolute; top: 5px; right: 0; 
        order: 0; border: none; padding: 5px;
    }
    
    .sf-search-row { flex-wrap: wrap; }
    .sf-search-bar { width: 100%; }
    .sf-btn-manual { width: 100%; justify-content: center; padding: 12px; margin-top: 5px;}
}

@media (min-width: 769px) {
    .sf-form-grid { grid-template-columns: 1fr 1fr 1fr; }
    .sf-input-group.full-width { grid-column: span 3; }
    .sf-footer-panel { grid-template-columns: 2fr 1fr; }
    .sf-actions { grid-template-columns: 1fr 1fr; }
}